GPBDictionary.m 370 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import "GPBDictionary_PackagePrivate.h"
  8. #import "GPBCodedInputStream_PackagePrivate.h"
  9. #import "GPBCodedOutputStream_PackagePrivate.h"
  10. #import "GPBDescriptor_PackagePrivate.h"
  11. #import "GPBMessage_PackagePrivate.h"
  12. #import "GPBUtilities_PackagePrivate.h"
  13. // ------------------------------ NOTE ------------------------------
  14. // At the moment, this is all using NSNumbers in NSDictionaries under
  15. // the hood, but it is all hidden so we can come back and optimize
  16. // with direct CFDictionary usage later. The reason that wasn't
  17. // done yet is needing to support 32bit iOS builds. Otherwise
  18. // it would be pretty simple to store all this data in CFDictionaries
  19. // directly.
  20. // ------------------------------------------------------------------
  21. // Direct access is use for speed, to avoid even internally declaring things
  22. // read/write, etc. The warning is enabled in the project to ensure code calling
  23. // protos can turn on -Wdirect-ivar-access without issues.
  24. #pragma clang diagnostic push
  25. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  26. enum {
  27. kMapKeyFieldNumber = 1,
  28. kMapValueFieldNumber = 2,
  29. };
  30. static BOOL DictDefault_IsValidValue(int32_t value) {
  31. // Anything but the bad value marker is allowed.
  32. return (value != kGPBUnrecognizedEnumeratorValue);
  33. }
  34. // Disable clang-format for the macros.
  35. // clang-format off
  36. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  37. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  38. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  39. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  40. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  41. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  42. //% } else {
  43. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  44. //% return 0;
  45. //% }
  46. //%}
  47. //%
  48. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  49. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  50. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  51. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  52. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  53. //% } else {
  54. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  55. //% }
  56. //%}
  57. //%
  58. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  59. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  60. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  61. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  62. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  63. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  64. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  65. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  66. //% } else {
  67. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  68. //% return 0;
  69. //% }
  70. //%}
  71. //%
  72. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  73. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  74. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  75. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  76. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  77. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  78. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  79. //% } else {
  80. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  81. //% }
  82. //%}
  83. //%
  84. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  85. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, __unused GPBDataType dataType) {
  86. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  87. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  88. //%}
  89. //%
  90. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, __unused GPBDataType dataType) {
  91. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  92. //% [stream write##VALUE_NAME##:fieldNum value:value];
  93. //%}
  94. //%
  95. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  96. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  97. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  98. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  99. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  100. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  101. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  102. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  103. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  104. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  105. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  106. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  107. // This block of code is generated, do not edit it directly.
  108. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  109. if (dataType == GPBDataTypeInt32) {
  110. return GPBComputeInt32Size(fieldNum, value);
  111. } else if (dataType == GPBDataTypeSInt32) {
  112. return GPBComputeSInt32Size(fieldNum, value);
  113. } else if (dataType == GPBDataTypeSFixed32) {
  114. return GPBComputeSFixed32Size(fieldNum, value);
  115. } else {
  116. NSCAssert(NO, @"Unexpected type %d", dataType);
  117. return 0;
  118. }
  119. }
  120. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  121. if (dataType == GPBDataTypeInt32) {
  122. [stream writeInt32:fieldNum value:value];
  123. } else if (dataType == GPBDataTypeSInt32) {
  124. [stream writeSInt32:fieldNum value:value];
  125. } else if (dataType == GPBDataTypeSFixed32) {
  126. [stream writeSFixed32:fieldNum value:value];
  127. } else {
  128. NSCAssert(NO, @"Unexpected type %d", dataType);
  129. }
  130. }
  131. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  132. if (dataType == GPBDataTypeUInt32) {
  133. return GPBComputeUInt32Size(fieldNum, value);
  134. } else if (dataType == GPBDataTypeFixed32) {
  135. return GPBComputeFixed32Size(fieldNum, value);
  136. } else {
  137. NSCAssert(NO, @"Unexpected type %d", dataType);
  138. return 0;
  139. }
  140. }
  141. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  142. if (dataType == GPBDataTypeUInt32) {
  143. [stream writeUInt32:fieldNum value:value];
  144. } else if (dataType == GPBDataTypeFixed32) {
  145. [stream writeFixed32:fieldNum value:value];
  146. } else {
  147. NSCAssert(NO, @"Unexpected type %d", dataType);
  148. }
  149. }
  150. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  151. if (dataType == GPBDataTypeInt64) {
  152. return GPBComputeInt64Size(fieldNum, value);
  153. } else if (dataType == GPBDataTypeSInt64) {
  154. return GPBComputeSInt64Size(fieldNum, value);
  155. } else if (dataType == GPBDataTypeSFixed64) {
  156. return GPBComputeSFixed64Size(fieldNum, value);
  157. } else {
  158. NSCAssert(NO, @"Unexpected type %d", dataType);
  159. return 0;
  160. }
  161. }
  162. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  163. if (dataType == GPBDataTypeInt64) {
  164. [stream writeInt64:fieldNum value:value];
  165. } else if (dataType == GPBDataTypeSInt64) {
  166. [stream writeSInt64:fieldNum value:value];
  167. } else if (dataType == GPBDataTypeSFixed64) {
  168. [stream writeSFixed64:fieldNum value:value];
  169. } else {
  170. NSCAssert(NO, @"Unexpected type %d", dataType);
  171. }
  172. }
  173. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  174. if (dataType == GPBDataTypeUInt64) {
  175. return GPBComputeUInt64Size(fieldNum, value);
  176. } else if (dataType == GPBDataTypeFixed64) {
  177. return GPBComputeFixed64Size(fieldNum, value);
  178. } else {
  179. NSCAssert(NO, @"Unexpected type %d", dataType);
  180. return 0;
  181. }
  182. }
  183. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  184. if (dataType == GPBDataTypeUInt64) {
  185. [stream writeUInt64:fieldNum value:value];
  186. } else if (dataType == GPBDataTypeFixed64) {
  187. [stream writeFixed64:fieldNum value:value];
  188. } else {
  189. NSCAssert(NO, @"Unexpected type %d", dataType);
  190. }
  191. }
  192. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, __unused GPBDataType dataType) {
  193. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  194. return GPBComputeBoolSize(fieldNum, value);
  195. }
  196. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, __unused GPBDataType dataType) {
  197. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  198. [stream writeBool:fieldNum value:value];
  199. }
  200. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, __unused GPBDataType dataType) {
  201. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  202. return GPBComputeEnumSize(fieldNum, value);
  203. }
  204. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, __unused GPBDataType dataType) {
  205. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  206. [stream writeEnum:fieldNum value:value];
  207. }
  208. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, __unused GPBDataType dataType) {
  209. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  210. return GPBComputeFloatSize(fieldNum, value);
  211. }
  212. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, __unused GPBDataType dataType) {
  213. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  214. [stream writeFloat:fieldNum value:value];
  215. }
  216. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, __unused GPBDataType dataType) {
  217. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  218. return GPBComputeDoubleSize(fieldNum, value);
  219. }
  220. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, __unused GPBDataType dataType) {
  221. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  222. [stream writeDouble:fieldNum value:value];
  223. }
  224. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, __unused GPBDataType dataType) {
  225. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  226. return GPBComputeStringSize(fieldNum, value);
  227. }
  228. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, __unused GPBDataType dataType) {
  229. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  230. [stream writeString:fieldNum value:value];
  231. }
  232. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  233. if (dataType == GPBDataTypeMessage) {
  234. return GPBComputeMessageSize(fieldNum, value);
  235. } else if (dataType == GPBDataTypeString) {
  236. return GPBComputeStringSize(fieldNum, value);
  237. } else if (dataType == GPBDataTypeBytes) {
  238. return GPBComputeBytesSize(fieldNum, value);
  239. } else {
  240. NSCAssert(NO, @"Unexpected type %d", dataType);
  241. return 0;
  242. }
  243. }
  244. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  245. if (dataType == GPBDataTypeMessage) {
  246. [stream writeMessage:fieldNum value:value];
  247. } else if (dataType == GPBDataTypeString) {
  248. [stream writeString:fieldNum value:value];
  249. } else if (dataType == GPBDataTypeBytes) {
  250. [stream writeBytes:fieldNum value:value];
  251. } else {
  252. NSCAssert(NO, @"Unexpected type %d", dataType);
  253. }
  254. }
  255. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  256. // clang-format on
  257. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  258. GPBDataType mapValueType = GPBGetFieldDataType(field);
  259. size_t result = 0;
  260. NSString *key;
  261. NSEnumerator *keys = [dict keyEnumerator];
  262. while ((key = [keys nextObject])) {
  263. id obj = dict[key];
  264. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  265. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  266. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  267. }
  268. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  269. result += tagSize * dict.count;
  270. return result;
  271. }
  272. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  273. NSDictionary *dict, GPBFieldDescriptor *field) {
  274. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  275. GPBDataType mapValueType = GPBGetFieldDataType(field);
  276. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  277. NSString *key;
  278. NSEnumerator *keys = [dict keyEnumerator];
  279. while ((key = [keys nextObject])) {
  280. id obj = dict[key];
  281. // Write the tag.
  282. [outputStream writeInt32NoTag:tag];
  283. // Write the size of the message.
  284. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  285. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  286. // Write the size and fields.
  287. [outputStream writeInt32NoTag:(int32_t)msgSize];
  288. [outputStream writeString:kMapKeyFieldNumber value:key];
  289. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  290. }
  291. }
  292. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict,
  293. __unused GPBFieldDescriptor *field) {
  294. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  295. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  296. GPBMessage *msg;
  297. NSEnumerator *objects = [dict objectEnumerator];
  298. while ((msg = [objects nextObject])) {
  299. if (!msg.initialized) {
  300. return NO;
  301. }
  302. }
  303. return YES;
  304. }
  305. // Note: if the type is an object, it the retain pass back to the caller.
  306. static void ReadValue(GPBCodedInputStream *stream, GPBGenericValue *valueToFill, GPBDataType type,
  307. id<GPBExtensionRegistry> registry, GPBFieldDescriptor *field) {
  308. switch (type) {
  309. case GPBDataTypeBool:
  310. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  311. break;
  312. case GPBDataTypeFixed32:
  313. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  314. break;
  315. case GPBDataTypeSFixed32:
  316. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  317. break;
  318. case GPBDataTypeFloat:
  319. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  320. break;
  321. case GPBDataTypeFixed64:
  322. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  323. break;
  324. case GPBDataTypeSFixed64:
  325. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  326. break;
  327. case GPBDataTypeDouble:
  328. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  329. break;
  330. case GPBDataTypeInt32:
  331. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  332. break;
  333. case GPBDataTypeInt64:
  334. valueToFill->valueInt64 = GPBCodedInputStreamReadInt64(&stream->state_);
  335. break;
  336. case GPBDataTypeSInt32:
  337. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  338. break;
  339. case GPBDataTypeSInt64:
  340. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  341. break;
  342. case GPBDataTypeUInt32:
  343. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  344. break;
  345. case GPBDataTypeUInt64:
  346. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  347. break;
  348. case GPBDataTypeBytes:
  349. [valueToFill->valueData release];
  350. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  351. break;
  352. case GPBDataTypeString:
  353. [valueToFill->valueString release];
  354. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  355. break;
  356. case GPBDataTypeMessage: {
  357. GPBMessage *message = [[field.msgClass alloc] init];
  358. [stream readMessage:message extensionRegistry:registry];
  359. [valueToFill->valueMessage release];
  360. valueToFill->valueMessage = message;
  361. break;
  362. }
  363. case GPBDataTypeGroup:
  364. NSCAssert(NO, @"Can't happen");
  365. break;
  366. case GPBDataTypeEnum:
  367. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  368. break;
  369. }
  370. }
  371. void GPBDictionaryReadEntry(id mapDictionary, GPBCodedInputStream *stream,
  372. id<GPBExtensionRegistry> registry, GPBFieldDescriptor *field,
  373. GPBMessage *parentMessage) {
  374. GPBDataType keyDataType = field.mapKeyDataType;
  375. GPBDataType valueDataType = GPBGetFieldDataType(field);
  376. GPBGenericValue key;
  377. GPBGenericValue value;
  378. // Zero them (but pick up any enum default for proto2).
  379. key.valueString = value.valueString = nil;
  380. if (valueDataType == GPBDataTypeEnum) {
  381. value = field.defaultValue;
  382. }
  383. GPBCodedInputStreamState *state = &stream->state_;
  384. uint32_t keyTag = GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  385. uint32_t valueTag =
  386. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  387. BOOL hitError = NO;
  388. while (YES) {
  389. uint32_t tag = GPBCodedInputStreamReadTag(state);
  390. if (tag == keyTag) {
  391. ReadValue(stream, &key, keyDataType, registry, field);
  392. } else if (tag == valueTag) {
  393. ReadValue(stream, &value, valueDataType, registry, field);
  394. } else if (tag == 0) {
  395. // zero signals EOF / limit reached
  396. break;
  397. } else { // Unknown
  398. if (![stream skipField:tag]) {
  399. hitError = YES;
  400. break;
  401. }
  402. }
  403. }
  404. if (!hitError) {
  405. // Handle the special defaults and/or missing key/value.
  406. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  407. key.valueString = [@"" retain];
  408. }
  409. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  410. #pragma clang diagnostic push
  411. #pragma clang diagnostic ignored "-Wswitch-enum"
  412. switch (valueDataType) {
  413. case GPBDataTypeString:
  414. value.valueString = [@"" retain];
  415. break;
  416. case GPBDataTypeBytes:
  417. value.valueData = [GPBEmptyNSData() retain];
  418. break;
  419. #if defined(__clang_analyzer__)
  420. case GPBDataTypeGroup:
  421. // Maps can't really have Groups as the value type, but this case is needed
  422. // so the analyzer won't report the possibility of send nil in for the value
  423. // in the NSMutableDictionary case below.
  424. #endif
  425. case GPBDataTypeMessage: {
  426. value.valueMessage = [[field.msgClass alloc] init];
  427. break;
  428. }
  429. default:
  430. // Nothing
  431. break;
  432. }
  433. #pragma clang diagnostic pop
  434. }
  435. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  436. // mapDictionary is an NSMutableDictionary
  437. [(NSMutableDictionary *)mapDictionary setObject:value.valueString forKey:key.valueString];
  438. } else {
  439. if (valueDataType == GPBDataTypeEnum) {
  440. if (!GPBFieldIsClosedEnum(field) || [field isValidEnumValue:value.valueEnum]) {
  441. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  442. } else {
  443. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  444. forKey:&key
  445. keyDataType:keyDataType];
  446. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  447. }
  448. } else {
  449. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  450. }
  451. }
  452. }
  453. if (GPBDataTypeIsObject(keyDataType)) {
  454. [key.valueString release];
  455. }
  456. if (GPBDataTypeIsObject(valueDataType)) {
  457. [value.valueString release];
  458. }
  459. }
  460. //
  461. // Macros for the common basic cases.
  462. //
  463. // Disable clang-format for the macros.
  464. // clang-format off
  465. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  466. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  467. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  468. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  469. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  470. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  471. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  472. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  473. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  474. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  475. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  476. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  477. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  478. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
  479. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  480. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
  481. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
  482. //%#pragma mark - KEY_NAME -> VALUE_NAME
  483. //%
  484. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  485. //% @package
  486. //% NSMutableDictionary *_dictionary;
  487. //%}
  488. //%
  489. //%- (instancetype)init {
  490. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  491. //%}
  492. //%
  493. //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  494. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  495. //% ##VNAME$S## count:(NSUInteger)count {
  496. //% self = [super init];
  497. //% if (self) {
  498. //% _dictionary = [[NSMutableDictionary alloc] init];
  499. //% if (count && VNAME_VAR##s && keys) {
  500. //% for (NSUInteger i = 0; i < count; ++i) {
  501. //%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])];
  502. //% }
  503. //% }
  504. //% }
  505. //% return self;
  506. //%}
  507. //%
  508. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  509. //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  510. //% if (self) {
  511. //% if (dictionary) {
  512. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  513. //% }
  514. //% }
  515. //% return self;
  516. //%}
  517. //%
  518. //%- (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  519. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  520. //%}
  521. //%
  522. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  523. //%
  524. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  525. //%
  526. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  527. //%
  528. //%@end
  529. //%
  530. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  531. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  532. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  533. //%#pragma mark - KEY_NAME -> VALUE_NAME
  534. //%
  535. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  536. //% @package
  537. //% NSMutableDictionary *_dictionary;
  538. //% GPBEnumValidationFunc _validationFunc;
  539. //%}
  540. //%
  541. //%@synthesize validationFunc = _validationFunc;
  542. //%
  543. //%- (instancetype)init {
  544. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  545. //%}
  546. //%
  547. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  548. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  549. //%}
  550. //%
  551. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  552. //% rawValues:(const VALUE_TYPE [])rawValues
  553. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  554. //% count:(NSUInteger)count {
  555. //% self = [super init];
  556. //% if (self) {
  557. //% _dictionary = [[NSMutableDictionary alloc] init];
  558. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  559. //% if (count && rawValues && keys) {
  560. //% for (NSUInteger i = 0; i < count; ++i) {
  561. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  562. //% }
  563. //% }
  564. //% }
  565. //% return self;
  566. //%}
  567. //%
  568. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  569. //% self = [self initWithValidationFunction:dictionary.validationFunc
  570. //% rawValues:NULL
  571. //% forKeys:NULL
  572. //% count:0];
  573. //% if (self) {
  574. //% if (dictionary) {
  575. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  576. //% }
  577. //% }
  578. //% return self;
  579. //%}
  580. //%
  581. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  582. //% capacity:(__unused NSUInteger)numItems {
  583. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  584. //%}
  585. //%
  586. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
  587. //%
  588. //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  589. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  590. //% if (wrapped && value) {
  591. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  592. //% if (!_validationFunc(result)) {
  593. //% result = kGPBUnrecognizedEnumeratorValue;
  594. //% }
  595. //% *value = result;
  596. //% }
  597. //% return (wrapped != NULL);
  598. //%}
  599. //%
  600. //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
  601. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  602. //% if (wrapped && rawValue) {
  603. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  604. //% }
  605. //% return (wrapped != NULL);
  606. //%}
  607. //%
  608. //%- (void)enumerateKeysAndEnumsUsingBlock:
  609. //% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  610. //% GPBEnumValidationFunc func = _validationFunc;
  611. //% BOOL stop = NO;
  612. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  613. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  614. //% while ((aKey = [keys nextObject])) {
  615. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue = _dictionary[aKey];
  616. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  617. //% if (!func(unwrapped)) {
  618. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  619. //% }
  620. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, &stop);
  621. //% if (stop) {
  622. //% break;
  623. //% }
  624. //% }
  625. //%}
  626. //%
  627. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
  628. //%
  629. //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  630. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  631. //% [NSException raise:NSInvalidArgumentException
  632. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  633. //% value];
  634. //% }
  635. //%
  636. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  637. //% if (_autocreator) {
  638. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  639. //% }
  640. //%}
  641. //%
  642. //%@end
  643. //%
  644. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  645. //%- (void)dealloc {
  646. //% NSAssert(!_autocreator,
  647. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  648. //% [self class], _autocreator);
  649. //% [_dictionary release];
  650. //% [super dealloc];
  651. //%}
  652. //%
  653. //%- (instancetype)copyWithZone:(NSZone *)zone {
  654. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  655. //%}
  656. //%
  657. //%- (BOOL)isEqual:(id)other {
  658. //% if (self == other) {
  659. //% return YES;
  660. //% }
  661. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  662. //% return NO;
  663. //% }
  664. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  665. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  666. //%}
  667. //%
  668. //%- (NSUInteger)hash {
  669. //% return _dictionary.count;
  670. //%}
  671. //%
  672. //%- (NSString *)description {
  673. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  674. //%}
  675. //%
  676. //%- (NSUInteger)count {
  677. //% return _dictionary.count;
  678. //%}
  679. //%
  680. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
  681. //% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  682. //% BOOL stop = NO;
  683. //% NSDictionary *internal = _dictionary;
  684. //% NSEnumerator *keys = [internal keyEnumerator];
  685. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  686. //% while ((aKey = [keys nextObject])) {
  687. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  688. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), &stop);
  689. //% if (stop) {
  690. //% break;
  691. //% }
  692. //% }
  693. //%}
  694. //%
  695. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  696. //% NSDictionary *internal = _dictionary;
  697. //% NSUInteger count = internal.count;
  698. //% if (count == 0) {
  699. //% return 0;
  700. //% }
  701. //%
  702. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  703. //% GPBDataType keyDataType = field.mapKeyDataType;
  704. //% size_t result = 0;
  705. //% NSEnumerator *keys = [internal keyEnumerator];
  706. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  707. //% while ((aKey = [keys nextObject])) {
  708. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  709. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  710. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  711. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  712. //% }
  713. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  714. //% result += tagSize * count;
  715. //% return result;
  716. //%}
  717. //%
  718. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  719. //% asField:(GPBFieldDescriptor *)field {
  720. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  721. //% GPBDataType keyDataType = field.mapKeyDataType;
  722. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  723. //% NSDictionary *internal = _dictionary;
  724. //% NSEnumerator *keys = [internal keyEnumerator];
  725. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  726. //% while ((aKey = [keys nextObject])) {
  727. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  728. //% [outputStream writeInt32NoTag:tag];
  729. //% // Write the size of the message.
  730. //% KEY_TYPE KisP##unwrappedKey = UNWRAP##KEY_NAME(aKey);
  731. //% VALUE_TYPE unwrappedValue = UNWRAP##VALUE_NAME(a##VNAME_VAR$u);
  732. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  733. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  734. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  735. //% // Write the fields.
  736. //% WriteDict##KEY_NAME##Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  737. //% WriteDict##VALUE_NAME##Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  738. //% }
  739. //%}
  740. //%
  741. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  742. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  743. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  744. //%}
  745. //%
  746. //%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  747. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, __unused BOOL *stop) {
  748. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
  749. //% }];
  750. //%}
  751. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  752. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
  753. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
  754. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  755. //% if (otherDictionary) {
  756. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  757. //% if (_autocreator) {
  758. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  759. //% }
  760. //% }
  761. //%}
  762. //%
  763. //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
  764. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
  765. //% if (_autocreator) {
  766. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  767. //% }
  768. //%}
  769. //%
  770. //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  771. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  772. //%}
  773. //%
  774. //%- (void)removeAll {
  775. //% [_dictionary removeAllObjects];
  776. //%}
  777. //
  778. // Custom Generation for Bool keys
  779. //
  780. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  781. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
  782. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  783. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
  784. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
  785. //%#pragma mark - Bool -> VALUE_NAME
  786. //%
  787. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  788. //% @package
  789. //% VALUE_TYPE _values[2];
  790. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  791. //%
  792. //%- (instancetype)init {
  793. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  794. //%}
  795. //%
  796. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  797. //%
  798. //%- (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  799. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  800. //%}
  801. //%
  802. //%BOOL_DICT_DEALLOC##HELPER()
  803. //%
  804. //%- (instancetype)copyWithZone:(NSZone *)zone {
  805. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  806. //%}
  807. //%
  808. //%- (BOOL)isEqual:(id)other {
  809. //% if (self == other) {
  810. //% return YES;
  811. //% }
  812. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  813. //% return NO;
  814. //% }
  815. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  816. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  817. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  818. //% return NO;
  819. //% }
  820. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  821. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  822. //% return NO;
  823. //% }
  824. //% return YES;
  825. //%}
  826. //%
  827. //%- (NSUInteger)hash {
  828. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  829. //%}
  830. //%
  831. //%- (NSString *)description {
  832. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  833. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  834. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  835. //% }
  836. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  837. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  838. //% }
  839. //% [result appendString:@" }"];
  840. //% return result;
  841. //%}
  842. //%
  843. //%- (NSUInteger)count {
  844. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  845. //%}
  846. //%
  847. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
  848. //%
  849. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  850. //%
  851. //%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  852. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  853. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  854. //% }
  855. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  856. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  857. //% }
  858. //%}
  859. //%
  860. //%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
  861. //% (void (NS_NOESCAPE ^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  862. //% BOOL stop = NO;
  863. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  864. //% block(NO, _values[0], &stop);
  865. //% }
  866. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  867. //% block(YES, _values[1], &stop);
  868. //% }
  869. //%}
  870. //%
  871. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  872. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  873. //% NSUInteger count = 0;
  874. //% size_t result = 0;
  875. //% for (int i = 0; i < 2; ++i) {
  876. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  877. //% ++count;
  878. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  879. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  880. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  881. //% }
  882. //% }
  883. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  884. //% result += tagSize * count;
  885. //% return result;
  886. //%}
  887. //%
  888. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  889. //% asField:(GPBFieldDescriptor *)field {
  890. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  891. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  892. //% for (int i = 0; i < 2; ++i) {
  893. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  894. //% // Write the tag.
  895. //% [outputStream writeInt32NoTag:tag];
  896. //% // Write the size of the message.
  897. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  898. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  899. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  900. //% // Write the fields.
  901. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  902. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  903. //% }
  904. //% }
  905. //%}
  906. //%
  907. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  908. //%
  909. //%@end
  910. //%
  911. //
  912. // Helpers for PODs
  913. //
  914. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  915. //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
  916. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  917. //% if (wrapped && value) {
  918. //% *value = UNWRAP##VALUE_NAME(wrapped);
  919. //% }
  920. //% return (wrapped != NULL);
  921. //%}
  922. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  923. //%@(VALUE)
  924. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  925. //%[VALUE unsignedIntValue]
  926. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  927. //%[VALUE intValue]
  928. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  929. //%[VALUE unsignedLongLongValue]
  930. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  931. //%[VALUE longLongValue]
  932. //%PDDM-DEFINE UNWRAPBool(VALUE)
  933. //%[VALUE boolValue]
  934. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  935. //%[VALUE floatValue]
  936. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  937. //%[VALUE doubleValue]
  938. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  939. //%[VALUE intValue]
  940. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  941. //%[NSString stringWithFormat:@"%u", VALUE]
  942. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  943. //%[NSString stringWithFormat:@"%d", VALUE]
  944. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  945. //%[NSString stringWithFormat:@"%llu", VALUE]
  946. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  947. //%[NSString stringWithFormat:@"%lld", VALUE]
  948. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  949. //%(VALUE ? @"true" : @"false")
  950. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  951. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  952. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  953. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  954. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  955. //%@(VALUE)
  956. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  957. //%NSNumber *
  958. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  959. //%VAL1 != VAL2
  960. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  961. // Empty
  962. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  963. // Empty
  964. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  965. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  966. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  967. // Empty
  968. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  969. // Empty
  970. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  971. // Empty
  972. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  973. // Empty
  974. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  975. // Empty
  976. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  977. // Empty
  978. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  979. // Empty
  980. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  981. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  982. //% forKey:(GPBGenericValue *)key
  983. //% keyDataType:(GPBDataType)keyDataType {
  984. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  985. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  986. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  987. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  988. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  989. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  990. //% [outputStream flush];
  991. //% [outputStream release];
  992. //% return data;
  993. //%}
  994. //%
  995. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  996. //%value##VALUE_NAME
  997. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  998. // Empty
  999. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1000. // Empty
  1001. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1002. //% BOOL _valueSet[2];
  1003. //%
  1004. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1005. //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
  1006. //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys
  1007. //% ##VALUE_NAME$S## count:(NSUInteger)count {
  1008. //% self = [super init];
  1009. //% if (self) {
  1010. //% for (NSUInteger i = 0; i < count; ++i) {
  1011. //% int idx = keys[i] ? 1 : 0;
  1012. //% _values[idx] = values[i];
  1013. //% _valueSet[idx] = YES;
  1014. //% }
  1015. //% }
  1016. //% return self;
  1017. //%}
  1018. //%
  1019. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1020. //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
  1021. //% if (self) {
  1022. //% if (dictionary) {
  1023. //% for (int i = 0; i < 2; ++i) {
  1024. //% if (dictionary->_valueSet[i]) {
  1025. //% _values[i] = dictionary->_values[i];
  1026. //% _valueSet[i] = YES;
  1027. //% }
  1028. //% }
  1029. //% }
  1030. //% }
  1031. //% return self;
  1032. //%}
  1033. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1034. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1035. //%- (void)dealloc {
  1036. //% NSAssert(!_autocreator,
  1037. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1038. //% [self class], _autocreator);
  1039. //% [super dealloc];
  1040. //%}
  1041. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1042. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1043. //%BOOL_DICT_HASPOD(IDX, REF)
  1044. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1045. //%REF##_valueSet[IDX]
  1046. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
  1047. //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
  1048. //% int idx = (key ? 1 : 0);
  1049. //% if (_valueSet[idx]) {
  1050. //% if (value) {
  1051. //% *value = _values[idx];
  1052. //% }
  1053. //% return YES;
  1054. //% }
  1055. //% return NO;
  1056. //%}
  1057. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1058. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1059. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1060. //% int idx = (key->valueBool ? 1 : 0);
  1061. //% _values[idx] = value->value##VALUE_NAME;
  1062. //% _valueSet[idx] = YES;
  1063. //%}
  1064. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1065. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1066. //% if (otherDictionary) {
  1067. //% for (int i = 0; i < 2; ++i) {
  1068. //% if (otherDictionary->_valueSet[i]) {
  1069. //% _valueSet[i] = YES;
  1070. //% _values[i] = otherDictionary->_values[i];
  1071. //% }
  1072. //% }
  1073. //% if (_autocreator) {
  1074. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1075. //% }
  1076. //% }
  1077. //%}
  1078. //%
  1079. //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
  1080. //% int idx = (key ? 1 : 0);
  1081. //% _values[idx] = value;
  1082. //% _valueSet[idx] = YES;
  1083. //% if (_autocreator) {
  1084. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1085. //% }
  1086. //%}
  1087. //%
  1088. //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
  1089. //% _valueSet[aKey ? 1 : 0] = NO;
  1090. //%}
  1091. //%
  1092. //%- (void)removeAll {
  1093. //% _valueSet[0] = NO;
  1094. //% _valueSet[1] = NO;
  1095. //%}
  1096. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1097. //%STR_FORMAT_##VALUE_NAME()
  1098. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1099. //%%u
  1100. //%PDDM-DEFINE STR_FORMAT_Int32()
  1101. //%%d
  1102. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1103. //%%llu
  1104. //%PDDM-DEFINE STR_FORMAT_Int64()
  1105. //%%lld
  1106. //%PDDM-DEFINE STR_FORMAT_Bool()
  1107. //%%d
  1108. //%PDDM-DEFINE STR_FORMAT_Float()
  1109. //%%f
  1110. //%PDDM-DEFINE STR_FORMAT_Double()
  1111. //%%lf
  1112. //
  1113. // Helpers for Objects
  1114. //
  1115. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1116. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1117. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1118. //% return result;
  1119. //%}
  1120. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1121. //%VALUE
  1122. //%PDDM-DEFINE UNWRAPString(VALUE)
  1123. //%VALUE
  1124. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1125. //%VALUE
  1126. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1127. //%VALUE
  1128. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1129. //%VALUE
  1130. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1131. //%ENUM_TYPEOBJECT_##TYPE()
  1132. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1133. //%NSString *
  1134. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1135. //%id ##
  1136. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1137. //%![VAL1 isEqual:VAL2]
  1138. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1139. //%- (BOOL)isInitialized {
  1140. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1141. //% if (!msg.initialized) {
  1142. //% return NO;
  1143. //% }
  1144. //% }
  1145. //% return YES;
  1146. //%}
  1147. //%
  1148. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1149. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1150. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1151. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  1152. //% id aKey;
  1153. //% NSMutableDictionary *internalDict = newDict->_dictionary;
  1154. //% while ((aKey = [keys nextObject])) {
  1155. //% GPBMessage *msg = _dictionary[aKey];
  1156. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1157. //% [internalDict setObject:copiedMsg forKey:aKey];
  1158. //% [copiedMsg release];
  1159. //% }
  1160. //% return newDict;
  1161. //%}
  1162. //%
  1163. //%
  1164. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1165. //%- (BOOL)isInitialized {
  1166. //% if (_values[0] && ![_values[0] isInitialized]) {
  1167. //% return NO;
  1168. //% }
  1169. //% if (_values[1] && ![_values[1] isInitialized]) {
  1170. //% return NO;
  1171. //% }
  1172. //% return YES;
  1173. //%}
  1174. //%
  1175. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1176. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1177. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1178. //% for (int i = 0; i < 2; ++i) {
  1179. //% if (_values[i] != nil) {
  1180. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1181. //% }
  1182. //% }
  1183. //% return newDict;
  1184. //%}
  1185. //%
  1186. //%
  1187. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1188. // Empty
  1189. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1190. //%valueString
  1191. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1192. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1193. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1194. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1195. //%##EXTRA_INDENT$S## }
  1196. //%
  1197. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1198. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1199. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1200. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1201. //%##EXTRA_INDENT$S## }
  1202. //%
  1203. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1204. // Empty
  1205. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1206. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1207. //% forKeys:(const BOOL [])keys
  1208. //% count:(NSUInteger)count {
  1209. //% self = [super init];
  1210. //% if (self) {
  1211. //% for (NSUInteger i = 0; i < count; ++i) {
  1212. //% if (!objects[i]) {
  1213. //% [NSException raise:NSInvalidArgumentException
  1214. //% format:@"Attempting to add nil object to a Dictionary"];
  1215. //% }
  1216. //% int idx = keys[i] ? 1 : 0;
  1217. //% [_values[idx] release];
  1218. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1219. //% }
  1220. //% }
  1221. //% return self;
  1222. //%}
  1223. //%
  1224. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1225. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1226. //% if (self) {
  1227. //% if (dictionary) {
  1228. //% _values[0] = [dictionary->_values[0] retain];
  1229. //% _values[1] = [dictionary->_values[1] retain];
  1230. //% }
  1231. //% }
  1232. //% return self;
  1233. //%}
  1234. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1235. //%- (void)dealloc {
  1236. //% NSAssert(!_autocreator,
  1237. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1238. //% [self class], _autocreator);
  1239. //% [_values[0] release];
  1240. //% [_values[1] release];
  1241. //% [super dealloc];
  1242. //%}
  1243. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1244. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1245. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1246. //%REF##_values[IDX] != nil
  1247. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
  1248. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1249. //% return _values[key ? 1 : 0];
  1250. //%}
  1251. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1252. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1253. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1254. //% int idx = (key->valueBool ? 1 : 0);
  1255. //% [_values[idx] release];
  1256. //% _values[idx] = [value->valueString retain];
  1257. //%}
  1258. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1259. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1260. //% if (otherDictionary) {
  1261. //% for (int i = 0; i < 2; ++i) {
  1262. //% if (otherDictionary->_values[i] != nil) {
  1263. //% [_values[i] release];
  1264. //% _values[i] = [otherDictionary->_values[i] retain];
  1265. //% }
  1266. //% }
  1267. //% if (_autocreator) {
  1268. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1269. //% }
  1270. //% }
  1271. //%}
  1272. //%
  1273. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1274. //% if (!object) {
  1275. //% [NSException raise:NSInvalidArgumentException
  1276. //% format:@"Attempting to add nil object to a Dictionary"];
  1277. //% }
  1278. //% int idx = (key ? 1 : 0);
  1279. //% [_values[idx] release];
  1280. //% _values[idx] = [object retain];
  1281. //% if (_autocreator) {
  1282. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1283. //% }
  1284. //%}
  1285. //%
  1286. //%- (void)removeObjectForKey:(BOOL)aKey {
  1287. //% int idx = (aKey ? 1 : 0);
  1288. //% [_values[idx] release];
  1289. //% _values[idx] = nil;
  1290. //%}
  1291. //%
  1292. //%- (void)removeAll {
  1293. //% for (int i = 0; i < 2; ++i) {
  1294. //% [_values[i] release];
  1295. //% _values[i] = nil;
  1296. //% }
  1297. //%}
  1298. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1299. //%%@
  1300. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1301. // This block of code is generated, do not edit it directly.
  1302. #pragma mark - UInt32 -> UInt32
  1303. @implementation GPBUInt32UInt32Dictionary {
  1304. @package
  1305. NSMutableDictionary *_dictionary;
  1306. }
  1307. - (instancetype)init {
  1308. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1309. }
  1310. - (instancetype)initWithUInt32s:(const uint32_t [])values
  1311. forKeys:(const uint32_t [])keys
  1312. count:(NSUInteger)count {
  1313. self = [super init];
  1314. if (self) {
  1315. _dictionary = [[NSMutableDictionary alloc] init];
  1316. if (count && values && keys) {
  1317. for (NSUInteger i = 0; i < count; ++i) {
  1318. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1319. }
  1320. }
  1321. }
  1322. return self;
  1323. }
  1324. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1325. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  1326. if (self) {
  1327. if (dictionary) {
  1328. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1329. }
  1330. }
  1331. return self;
  1332. }
  1333. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1334. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1335. }
  1336. - (void)dealloc {
  1337. NSAssert(!_autocreator,
  1338. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1339. [self class], _autocreator);
  1340. [_dictionary release];
  1341. [super dealloc];
  1342. }
  1343. - (instancetype)copyWithZone:(NSZone *)zone {
  1344. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1345. }
  1346. - (BOOL)isEqual:(id)other {
  1347. if (self == other) {
  1348. return YES;
  1349. }
  1350. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1351. return NO;
  1352. }
  1353. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1354. return [_dictionary isEqual:otherDictionary->_dictionary];
  1355. }
  1356. - (NSUInteger)hash {
  1357. return _dictionary.count;
  1358. }
  1359. - (NSString *)description {
  1360. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1361. }
  1362. - (NSUInteger)count {
  1363. return _dictionary.count;
  1364. }
  1365. - (void)enumerateKeysAndUInt32sUsingBlock:
  1366. (void (NS_NOESCAPE ^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1367. BOOL stop = NO;
  1368. NSDictionary *internal = _dictionary;
  1369. NSEnumerator *keys = [internal keyEnumerator];
  1370. NSNumber *aKey;
  1371. while ((aKey = [keys nextObject])) {
  1372. NSNumber *aValue = internal[aKey];
  1373. block([aKey unsignedIntValue], [aValue unsignedIntValue], &stop);
  1374. if (stop) {
  1375. break;
  1376. }
  1377. }
  1378. }
  1379. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1380. NSDictionary *internal = _dictionary;
  1381. NSUInteger count = internal.count;
  1382. if (count == 0) {
  1383. return 0;
  1384. }
  1385. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1386. GPBDataType keyDataType = field.mapKeyDataType;
  1387. size_t result = 0;
  1388. NSEnumerator *keys = [internal keyEnumerator];
  1389. NSNumber *aKey;
  1390. while ((aKey = [keys nextObject])) {
  1391. NSNumber *aValue = internal[aKey];
  1392. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1393. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1394. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1395. }
  1396. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1397. result += tagSize * count;
  1398. return result;
  1399. }
  1400. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1401. asField:(GPBFieldDescriptor *)field {
  1402. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1403. GPBDataType keyDataType = field.mapKeyDataType;
  1404. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1405. NSDictionary *internal = _dictionary;
  1406. NSEnumerator *keys = [internal keyEnumerator];
  1407. NSNumber *aKey;
  1408. while ((aKey = [keys nextObject])) {
  1409. NSNumber *aValue = internal[aKey];
  1410. [outputStream writeInt32NoTag:tag];
  1411. // Write the size of the message.
  1412. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1413. uint32_t unwrappedValue = [aValue unsignedIntValue];
  1414. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1415. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1416. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1417. // Write the fields.
  1418. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1419. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1420. }
  1421. }
  1422. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1423. forGPBGenericValueKey:(GPBGenericValue *)key {
  1424. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1425. }
  1426. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1427. [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, __unused BOOL *stop) {
  1428. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1429. }];
  1430. }
  1431. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
  1432. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1433. if (wrapped && value) {
  1434. *value = [wrapped unsignedIntValue];
  1435. }
  1436. return (wrapped != NULL);
  1437. }
  1438. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1439. if (otherDictionary) {
  1440. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1441. if (_autocreator) {
  1442. GPBAutocreatedDictionaryModified(_autocreator, self);
  1443. }
  1444. }
  1445. }
  1446. - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
  1447. [_dictionary setObject:@(value) forKey:@(key)];
  1448. if (_autocreator) {
  1449. GPBAutocreatedDictionaryModified(_autocreator, self);
  1450. }
  1451. }
  1452. - (void)removeUInt32ForKey:(uint32_t)aKey {
  1453. [_dictionary removeObjectForKey:@(aKey)];
  1454. }
  1455. - (void)removeAll {
  1456. [_dictionary removeAllObjects];
  1457. }
  1458. @end
  1459. #pragma mark - UInt32 -> Int32
  1460. @implementation GPBUInt32Int32Dictionary {
  1461. @package
  1462. NSMutableDictionary *_dictionary;
  1463. }
  1464. - (instancetype)init {
  1465. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1466. }
  1467. - (instancetype)initWithInt32s:(const int32_t [])values
  1468. forKeys:(const uint32_t [])keys
  1469. count:(NSUInteger)count {
  1470. self = [super init];
  1471. if (self) {
  1472. _dictionary = [[NSMutableDictionary alloc] init];
  1473. if (count && values && keys) {
  1474. for (NSUInteger i = 0; i < count; ++i) {
  1475. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1476. }
  1477. }
  1478. }
  1479. return self;
  1480. }
  1481. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1482. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  1483. if (self) {
  1484. if (dictionary) {
  1485. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1486. }
  1487. }
  1488. return self;
  1489. }
  1490. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1491. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1492. }
  1493. - (void)dealloc {
  1494. NSAssert(!_autocreator,
  1495. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1496. [self class], _autocreator);
  1497. [_dictionary release];
  1498. [super dealloc];
  1499. }
  1500. - (instancetype)copyWithZone:(NSZone *)zone {
  1501. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1502. }
  1503. - (BOOL)isEqual:(id)other {
  1504. if (self == other) {
  1505. return YES;
  1506. }
  1507. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1508. return NO;
  1509. }
  1510. GPBUInt32Int32Dictionary *otherDictionary = other;
  1511. return [_dictionary isEqual:otherDictionary->_dictionary];
  1512. }
  1513. - (NSUInteger)hash {
  1514. return _dictionary.count;
  1515. }
  1516. - (NSString *)description {
  1517. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1518. }
  1519. - (NSUInteger)count {
  1520. return _dictionary.count;
  1521. }
  1522. - (void)enumerateKeysAndInt32sUsingBlock:
  1523. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  1524. BOOL stop = NO;
  1525. NSDictionary *internal = _dictionary;
  1526. NSEnumerator *keys = [internal keyEnumerator];
  1527. NSNumber *aKey;
  1528. while ((aKey = [keys nextObject])) {
  1529. NSNumber *aValue = internal[aKey];
  1530. block([aKey unsignedIntValue], [aValue intValue], &stop);
  1531. if (stop) {
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1537. NSDictionary *internal = _dictionary;
  1538. NSUInteger count = internal.count;
  1539. if (count == 0) {
  1540. return 0;
  1541. }
  1542. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1543. GPBDataType keyDataType = field.mapKeyDataType;
  1544. size_t result = 0;
  1545. NSEnumerator *keys = [internal keyEnumerator];
  1546. NSNumber *aKey;
  1547. while ((aKey = [keys nextObject])) {
  1548. NSNumber *aValue = internal[aKey];
  1549. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1550. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1551. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1552. }
  1553. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1554. result += tagSize * count;
  1555. return result;
  1556. }
  1557. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1558. asField:(GPBFieldDescriptor *)field {
  1559. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1560. GPBDataType keyDataType = field.mapKeyDataType;
  1561. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1562. NSDictionary *internal = _dictionary;
  1563. NSEnumerator *keys = [internal keyEnumerator];
  1564. NSNumber *aKey;
  1565. while ((aKey = [keys nextObject])) {
  1566. NSNumber *aValue = internal[aKey];
  1567. [outputStream writeInt32NoTag:tag];
  1568. // Write the size of the message.
  1569. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1570. int32_t unwrappedValue = [aValue intValue];
  1571. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1572. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1573. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1574. // Write the fields.
  1575. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1576. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1577. }
  1578. }
  1579. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1580. forGPBGenericValueKey:(GPBGenericValue *)key {
  1581. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1582. }
  1583. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1584. [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, __unused BOOL *stop) {
  1585. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1586. }];
  1587. }
  1588. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
  1589. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1590. if (wrapped && value) {
  1591. *value = [wrapped intValue];
  1592. }
  1593. return (wrapped != NULL);
  1594. }
  1595. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1596. if (otherDictionary) {
  1597. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1598. if (_autocreator) {
  1599. GPBAutocreatedDictionaryModified(_autocreator, self);
  1600. }
  1601. }
  1602. }
  1603. - (void)setInt32:(int32_t)value forKey:(uint32_t)key {
  1604. [_dictionary setObject:@(value) forKey:@(key)];
  1605. if (_autocreator) {
  1606. GPBAutocreatedDictionaryModified(_autocreator, self);
  1607. }
  1608. }
  1609. - (void)removeInt32ForKey:(uint32_t)aKey {
  1610. [_dictionary removeObjectForKey:@(aKey)];
  1611. }
  1612. - (void)removeAll {
  1613. [_dictionary removeAllObjects];
  1614. }
  1615. @end
  1616. #pragma mark - UInt32 -> UInt64
  1617. @implementation GPBUInt32UInt64Dictionary {
  1618. @package
  1619. NSMutableDictionary *_dictionary;
  1620. }
  1621. - (instancetype)init {
  1622. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1623. }
  1624. - (instancetype)initWithUInt64s:(const uint64_t [])values
  1625. forKeys:(const uint32_t [])keys
  1626. count:(NSUInteger)count {
  1627. self = [super init];
  1628. if (self) {
  1629. _dictionary = [[NSMutableDictionary alloc] init];
  1630. if (count && values && keys) {
  1631. for (NSUInteger i = 0; i < count; ++i) {
  1632. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1633. }
  1634. }
  1635. }
  1636. return self;
  1637. }
  1638. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1639. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  1640. if (self) {
  1641. if (dictionary) {
  1642. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1643. }
  1644. }
  1645. return self;
  1646. }
  1647. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1648. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1649. }
  1650. - (void)dealloc {
  1651. NSAssert(!_autocreator,
  1652. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1653. [self class], _autocreator);
  1654. [_dictionary release];
  1655. [super dealloc];
  1656. }
  1657. - (instancetype)copyWithZone:(NSZone *)zone {
  1658. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1659. }
  1660. - (BOOL)isEqual:(id)other {
  1661. if (self == other) {
  1662. return YES;
  1663. }
  1664. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1665. return NO;
  1666. }
  1667. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1668. return [_dictionary isEqual:otherDictionary->_dictionary];
  1669. }
  1670. - (NSUInteger)hash {
  1671. return _dictionary.count;
  1672. }
  1673. - (NSString *)description {
  1674. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1675. }
  1676. - (NSUInteger)count {
  1677. return _dictionary.count;
  1678. }
  1679. - (void)enumerateKeysAndUInt64sUsingBlock:
  1680. (void (NS_NOESCAPE ^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1681. BOOL stop = NO;
  1682. NSDictionary *internal = _dictionary;
  1683. NSEnumerator *keys = [internal keyEnumerator];
  1684. NSNumber *aKey;
  1685. while ((aKey = [keys nextObject])) {
  1686. NSNumber *aValue = internal[aKey];
  1687. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], &stop);
  1688. if (stop) {
  1689. break;
  1690. }
  1691. }
  1692. }
  1693. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1694. NSDictionary *internal = _dictionary;
  1695. NSUInteger count = internal.count;
  1696. if (count == 0) {
  1697. return 0;
  1698. }
  1699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1700. GPBDataType keyDataType = field.mapKeyDataType;
  1701. size_t result = 0;
  1702. NSEnumerator *keys = [internal keyEnumerator];
  1703. NSNumber *aKey;
  1704. while ((aKey = [keys nextObject])) {
  1705. NSNumber *aValue = internal[aKey];
  1706. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1707. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1708. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1709. }
  1710. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1711. result += tagSize * count;
  1712. return result;
  1713. }
  1714. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1715. asField:(GPBFieldDescriptor *)field {
  1716. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1717. GPBDataType keyDataType = field.mapKeyDataType;
  1718. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1719. NSDictionary *internal = _dictionary;
  1720. NSEnumerator *keys = [internal keyEnumerator];
  1721. NSNumber *aKey;
  1722. while ((aKey = [keys nextObject])) {
  1723. NSNumber *aValue = internal[aKey];
  1724. [outputStream writeInt32NoTag:tag];
  1725. // Write the size of the message.
  1726. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1727. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  1728. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1729. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1730. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1731. // Write the fields.
  1732. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1733. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1734. }
  1735. }
  1736. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1737. forGPBGenericValueKey:(GPBGenericValue *)key {
  1738. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1739. }
  1740. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1741. [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, __unused BOOL *stop) {
  1742. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1743. }];
  1744. }
  1745. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
  1746. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1747. if (wrapped && value) {
  1748. *value = [wrapped unsignedLongLongValue];
  1749. }
  1750. return (wrapped != NULL);
  1751. }
  1752. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1753. if (otherDictionary) {
  1754. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1755. if (_autocreator) {
  1756. GPBAutocreatedDictionaryModified(_autocreator, self);
  1757. }
  1758. }
  1759. }
  1760. - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
  1761. [_dictionary setObject:@(value) forKey:@(key)];
  1762. if (_autocreator) {
  1763. GPBAutocreatedDictionaryModified(_autocreator, self);
  1764. }
  1765. }
  1766. - (void)removeUInt64ForKey:(uint32_t)aKey {
  1767. [_dictionary removeObjectForKey:@(aKey)];
  1768. }
  1769. - (void)removeAll {
  1770. [_dictionary removeAllObjects];
  1771. }
  1772. @end
  1773. #pragma mark - UInt32 -> Int64
  1774. @implementation GPBUInt32Int64Dictionary {
  1775. @package
  1776. NSMutableDictionary *_dictionary;
  1777. }
  1778. - (instancetype)init {
  1779. return [self initWithInt64s:NULL forKeys:NULL count:0];
  1780. }
  1781. - (instancetype)initWithInt64s:(const int64_t [])values
  1782. forKeys:(const uint32_t [])keys
  1783. count:(NSUInteger)count {
  1784. self = [super init];
  1785. if (self) {
  1786. _dictionary = [[NSMutableDictionary alloc] init];
  1787. if (count && values && keys) {
  1788. for (NSUInteger i = 0; i < count; ++i) {
  1789. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1790. }
  1791. }
  1792. }
  1793. return self;
  1794. }
  1795. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1796. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  1797. if (self) {
  1798. if (dictionary) {
  1799. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1800. }
  1801. }
  1802. return self;
  1803. }
  1804. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1805. return [self initWithInt64s:NULL forKeys:NULL count:0];
  1806. }
  1807. - (void)dealloc {
  1808. NSAssert(!_autocreator,
  1809. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1810. [self class], _autocreator);
  1811. [_dictionary release];
  1812. [super dealloc];
  1813. }
  1814. - (instancetype)copyWithZone:(NSZone *)zone {
  1815. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  1816. }
  1817. - (BOOL)isEqual:(id)other {
  1818. if (self == other) {
  1819. return YES;
  1820. }
  1821. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  1822. return NO;
  1823. }
  1824. GPBUInt32Int64Dictionary *otherDictionary = other;
  1825. return [_dictionary isEqual:otherDictionary->_dictionary];
  1826. }
  1827. - (NSUInteger)hash {
  1828. return _dictionary.count;
  1829. }
  1830. - (NSString *)description {
  1831. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1832. }
  1833. - (NSUInteger)count {
  1834. return _dictionary.count;
  1835. }
  1836. - (void)enumerateKeysAndInt64sUsingBlock:
  1837. (void (NS_NOESCAPE ^)(uint32_t key, int64_t value, BOOL *stop))block {
  1838. BOOL stop = NO;
  1839. NSDictionary *internal = _dictionary;
  1840. NSEnumerator *keys = [internal keyEnumerator];
  1841. NSNumber *aKey;
  1842. while ((aKey = [keys nextObject])) {
  1843. NSNumber *aValue = internal[aKey];
  1844. block([aKey unsignedIntValue], [aValue longLongValue], &stop);
  1845. if (stop) {
  1846. break;
  1847. }
  1848. }
  1849. }
  1850. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1851. NSDictionary *internal = _dictionary;
  1852. NSUInteger count = internal.count;
  1853. if (count == 0) {
  1854. return 0;
  1855. }
  1856. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1857. GPBDataType keyDataType = field.mapKeyDataType;
  1858. size_t result = 0;
  1859. NSEnumerator *keys = [internal keyEnumerator];
  1860. NSNumber *aKey;
  1861. while ((aKey = [keys nextObject])) {
  1862. NSNumber *aValue = internal[aKey];
  1863. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1864. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  1865. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1866. }
  1867. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1868. result += tagSize * count;
  1869. return result;
  1870. }
  1871. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1872. asField:(GPBFieldDescriptor *)field {
  1873. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1874. GPBDataType keyDataType = field.mapKeyDataType;
  1875. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1876. NSDictionary *internal = _dictionary;
  1877. NSEnumerator *keys = [internal keyEnumerator];
  1878. NSNumber *aKey;
  1879. while ((aKey = [keys nextObject])) {
  1880. NSNumber *aValue = internal[aKey];
  1881. [outputStream writeInt32NoTag:tag];
  1882. // Write the size of the message.
  1883. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1884. int64_t unwrappedValue = [aValue longLongValue];
  1885. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1886. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1887. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1888. // Write the fields.
  1889. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1890. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1891. }
  1892. }
  1893. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1894. forGPBGenericValueKey:(GPBGenericValue *)key {
  1895. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  1896. }
  1897. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1898. [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, __unused BOOL *stop) {
  1899. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  1900. }];
  1901. }
  1902. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
  1903. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1904. if (wrapped && value) {
  1905. *value = [wrapped longLongValue];
  1906. }
  1907. return (wrapped != NULL);
  1908. }
  1909. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  1910. if (otherDictionary) {
  1911. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1912. if (_autocreator) {
  1913. GPBAutocreatedDictionaryModified(_autocreator, self);
  1914. }
  1915. }
  1916. }
  1917. - (void)setInt64:(int64_t)value forKey:(uint32_t)key {
  1918. [_dictionary setObject:@(value) forKey:@(key)];
  1919. if (_autocreator) {
  1920. GPBAutocreatedDictionaryModified(_autocreator, self);
  1921. }
  1922. }
  1923. - (void)removeInt64ForKey:(uint32_t)aKey {
  1924. [_dictionary removeObjectForKey:@(aKey)];
  1925. }
  1926. - (void)removeAll {
  1927. [_dictionary removeAllObjects];
  1928. }
  1929. @end
  1930. #pragma mark - UInt32 -> Bool
  1931. @implementation GPBUInt32BoolDictionary {
  1932. @package
  1933. NSMutableDictionary *_dictionary;
  1934. }
  1935. - (instancetype)init {
  1936. return [self initWithBools:NULL forKeys:NULL count:0];
  1937. }
  1938. - (instancetype)initWithBools:(const BOOL [])values
  1939. forKeys:(const uint32_t [])keys
  1940. count:(NSUInteger)count {
  1941. self = [super init];
  1942. if (self) {
  1943. _dictionary = [[NSMutableDictionary alloc] init];
  1944. if (count && values && keys) {
  1945. for (NSUInteger i = 0; i < count; ++i) {
  1946. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1947. }
  1948. }
  1949. }
  1950. return self;
  1951. }
  1952. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  1953. self = [self initWithBools:NULL forKeys:NULL count:0];
  1954. if (self) {
  1955. if (dictionary) {
  1956. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1957. }
  1958. }
  1959. return self;
  1960. }
  1961. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1962. return [self initWithBools:NULL forKeys:NULL count:0];
  1963. }
  1964. - (void)dealloc {
  1965. NSAssert(!_autocreator,
  1966. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1967. [self class], _autocreator);
  1968. [_dictionary release];
  1969. [super dealloc];
  1970. }
  1971. - (instancetype)copyWithZone:(NSZone *)zone {
  1972. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  1973. }
  1974. - (BOOL)isEqual:(id)other {
  1975. if (self == other) {
  1976. return YES;
  1977. }
  1978. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  1979. return NO;
  1980. }
  1981. GPBUInt32BoolDictionary *otherDictionary = other;
  1982. return [_dictionary isEqual:otherDictionary->_dictionary];
  1983. }
  1984. - (NSUInteger)hash {
  1985. return _dictionary.count;
  1986. }
  1987. - (NSString *)description {
  1988. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1989. }
  1990. - (NSUInteger)count {
  1991. return _dictionary.count;
  1992. }
  1993. - (void)enumerateKeysAndBoolsUsingBlock:
  1994. (void (NS_NOESCAPE ^)(uint32_t key, BOOL value, BOOL *stop))block {
  1995. BOOL stop = NO;
  1996. NSDictionary *internal = _dictionary;
  1997. NSEnumerator *keys = [internal keyEnumerator];
  1998. NSNumber *aKey;
  1999. while ((aKey = [keys nextObject])) {
  2000. NSNumber *aValue = internal[aKey];
  2001. block([aKey unsignedIntValue], [aValue boolValue], &stop);
  2002. if (stop) {
  2003. break;
  2004. }
  2005. }
  2006. }
  2007. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2008. NSDictionary *internal = _dictionary;
  2009. NSUInteger count = internal.count;
  2010. if (count == 0) {
  2011. return 0;
  2012. }
  2013. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2014. GPBDataType keyDataType = field.mapKeyDataType;
  2015. size_t result = 0;
  2016. NSEnumerator *keys = [internal keyEnumerator];
  2017. NSNumber *aKey;
  2018. while ((aKey = [keys nextObject])) {
  2019. NSNumber *aValue = internal[aKey];
  2020. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2021. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2022. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2023. }
  2024. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2025. result += tagSize * count;
  2026. return result;
  2027. }
  2028. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2029. asField:(GPBFieldDescriptor *)field {
  2030. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2031. GPBDataType keyDataType = field.mapKeyDataType;
  2032. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2033. NSDictionary *internal = _dictionary;
  2034. NSEnumerator *keys = [internal keyEnumerator];
  2035. NSNumber *aKey;
  2036. while ((aKey = [keys nextObject])) {
  2037. NSNumber *aValue = internal[aKey];
  2038. [outputStream writeInt32NoTag:tag];
  2039. // Write the size of the message.
  2040. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2041. BOOL unwrappedValue = [aValue boolValue];
  2042. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2043. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2044. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2045. // Write the fields.
  2046. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2047. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2048. }
  2049. }
  2050. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2051. forGPBGenericValueKey:(GPBGenericValue *)key {
  2052. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2053. }
  2054. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2055. [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, __unused BOOL *stop) {
  2056. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2057. }];
  2058. }
  2059. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
  2060. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2061. if (wrapped && value) {
  2062. *value = [wrapped boolValue];
  2063. }
  2064. return (wrapped != NULL);
  2065. }
  2066. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2067. if (otherDictionary) {
  2068. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2069. if (_autocreator) {
  2070. GPBAutocreatedDictionaryModified(_autocreator, self);
  2071. }
  2072. }
  2073. }
  2074. - (void)setBool:(BOOL)value forKey:(uint32_t)key {
  2075. [_dictionary setObject:@(value) forKey:@(key)];
  2076. if (_autocreator) {
  2077. GPBAutocreatedDictionaryModified(_autocreator, self);
  2078. }
  2079. }
  2080. - (void)removeBoolForKey:(uint32_t)aKey {
  2081. [_dictionary removeObjectForKey:@(aKey)];
  2082. }
  2083. - (void)removeAll {
  2084. [_dictionary removeAllObjects];
  2085. }
  2086. @end
  2087. #pragma mark - UInt32 -> Float
  2088. @implementation GPBUInt32FloatDictionary {
  2089. @package
  2090. NSMutableDictionary *_dictionary;
  2091. }
  2092. - (instancetype)init {
  2093. return [self initWithFloats:NULL forKeys:NULL count:0];
  2094. }
  2095. - (instancetype)initWithFloats:(const float [])values
  2096. forKeys:(const uint32_t [])keys
  2097. count:(NSUInteger)count {
  2098. self = [super init];
  2099. if (self) {
  2100. _dictionary = [[NSMutableDictionary alloc] init];
  2101. if (count && values && keys) {
  2102. for (NSUInteger i = 0; i < count; ++i) {
  2103. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2104. }
  2105. }
  2106. }
  2107. return self;
  2108. }
  2109. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2110. self = [self initWithFloats:NULL forKeys:NULL count:0];
  2111. if (self) {
  2112. if (dictionary) {
  2113. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2114. }
  2115. }
  2116. return self;
  2117. }
  2118. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2119. return [self initWithFloats:NULL forKeys:NULL count:0];
  2120. }
  2121. - (void)dealloc {
  2122. NSAssert(!_autocreator,
  2123. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2124. [self class], _autocreator);
  2125. [_dictionary release];
  2126. [super dealloc];
  2127. }
  2128. - (instancetype)copyWithZone:(NSZone *)zone {
  2129. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2130. }
  2131. - (BOOL)isEqual:(id)other {
  2132. if (self == other) {
  2133. return YES;
  2134. }
  2135. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2136. return NO;
  2137. }
  2138. GPBUInt32FloatDictionary *otherDictionary = other;
  2139. return [_dictionary isEqual:otherDictionary->_dictionary];
  2140. }
  2141. - (NSUInteger)hash {
  2142. return _dictionary.count;
  2143. }
  2144. - (NSString *)description {
  2145. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2146. }
  2147. - (NSUInteger)count {
  2148. return _dictionary.count;
  2149. }
  2150. - (void)enumerateKeysAndFloatsUsingBlock:
  2151. (void (NS_NOESCAPE ^)(uint32_t key, float value, BOOL *stop))block {
  2152. BOOL stop = NO;
  2153. NSDictionary *internal = _dictionary;
  2154. NSEnumerator *keys = [internal keyEnumerator];
  2155. NSNumber *aKey;
  2156. while ((aKey = [keys nextObject])) {
  2157. NSNumber *aValue = internal[aKey];
  2158. block([aKey unsignedIntValue], [aValue floatValue], &stop);
  2159. if (stop) {
  2160. break;
  2161. }
  2162. }
  2163. }
  2164. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2165. NSDictionary *internal = _dictionary;
  2166. NSUInteger count = internal.count;
  2167. if (count == 0) {
  2168. return 0;
  2169. }
  2170. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2171. GPBDataType keyDataType = field.mapKeyDataType;
  2172. size_t result = 0;
  2173. NSEnumerator *keys = [internal keyEnumerator];
  2174. NSNumber *aKey;
  2175. while ((aKey = [keys nextObject])) {
  2176. NSNumber *aValue = internal[aKey];
  2177. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2178. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2179. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2180. }
  2181. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2182. result += tagSize * count;
  2183. return result;
  2184. }
  2185. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2186. asField:(GPBFieldDescriptor *)field {
  2187. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2188. GPBDataType keyDataType = field.mapKeyDataType;
  2189. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2190. NSDictionary *internal = _dictionary;
  2191. NSEnumerator *keys = [internal keyEnumerator];
  2192. NSNumber *aKey;
  2193. while ((aKey = [keys nextObject])) {
  2194. NSNumber *aValue = internal[aKey];
  2195. [outputStream writeInt32NoTag:tag];
  2196. // Write the size of the message.
  2197. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2198. float unwrappedValue = [aValue floatValue];
  2199. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2200. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2201. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2202. // Write the fields.
  2203. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2204. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2205. }
  2206. }
  2207. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2208. forGPBGenericValueKey:(GPBGenericValue *)key {
  2209. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2210. }
  2211. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2212. [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, __unused BOOL *stop) {
  2213. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2214. }];
  2215. }
  2216. - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
  2217. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2218. if (wrapped && value) {
  2219. *value = [wrapped floatValue];
  2220. }
  2221. return (wrapped != NULL);
  2222. }
  2223. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2224. if (otherDictionary) {
  2225. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2226. if (_autocreator) {
  2227. GPBAutocreatedDictionaryModified(_autocreator, self);
  2228. }
  2229. }
  2230. }
  2231. - (void)setFloat:(float)value forKey:(uint32_t)key {
  2232. [_dictionary setObject:@(value) forKey:@(key)];
  2233. if (_autocreator) {
  2234. GPBAutocreatedDictionaryModified(_autocreator, self);
  2235. }
  2236. }
  2237. - (void)removeFloatForKey:(uint32_t)aKey {
  2238. [_dictionary removeObjectForKey:@(aKey)];
  2239. }
  2240. - (void)removeAll {
  2241. [_dictionary removeAllObjects];
  2242. }
  2243. @end
  2244. #pragma mark - UInt32 -> Double
  2245. @implementation GPBUInt32DoubleDictionary {
  2246. @package
  2247. NSMutableDictionary *_dictionary;
  2248. }
  2249. - (instancetype)init {
  2250. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2251. }
  2252. - (instancetype)initWithDoubles:(const double [])values
  2253. forKeys:(const uint32_t [])keys
  2254. count:(NSUInteger)count {
  2255. self = [super init];
  2256. if (self) {
  2257. _dictionary = [[NSMutableDictionary alloc] init];
  2258. if (count && values && keys) {
  2259. for (NSUInteger i = 0; i < count; ++i) {
  2260. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2261. }
  2262. }
  2263. }
  2264. return self;
  2265. }
  2266. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2267. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  2268. if (self) {
  2269. if (dictionary) {
  2270. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2271. }
  2272. }
  2273. return self;
  2274. }
  2275. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2276. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2277. }
  2278. - (void)dealloc {
  2279. NSAssert(!_autocreator,
  2280. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2281. [self class], _autocreator);
  2282. [_dictionary release];
  2283. [super dealloc];
  2284. }
  2285. - (instancetype)copyWithZone:(NSZone *)zone {
  2286. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2287. }
  2288. - (BOOL)isEqual:(id)other {
  2289. if (self == other) {
  2290. return YES;
  2291. }
  2292. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2293. return NO;
  2294. }
  2295. GPBUInt32DoubleDictionary *otherDictionary = other;
  2296. return [_dictionary isEqual:otherDictionary->_dictionary];
  2297. }
  2298. - (NSUInteger)hash {
  2299. return _dictionary.count;
  2300. }
  2301. - (NSString *)description {
  2302. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2303. }
  2304. - (NSUInteger)count {
  2305. return _dictionary.count;
  2306. }
  2307. - (void)enumerateKeysAndDoublesUsingBlock:
  2308. (void (NS_NOESCAPE ^)(uint32_t key, double value, BOOL *stop))block {
  2309. BOOL stop = NO;
  2310. NSDictionary *internal = _dictionary;
  2311. NSEnumerator *keys = [internal keyEnumerator];
  2312. NSNumber *aKey;
  2313. while ((aKey = [keys nextObject])) {
  2314. NSNumber *aValue = internal[aKey];
  2315. block([aKey unsignedIntValue], [aValue doubleValue], &stop);
  2316. if (stop) {
  2317. break;
  2318. }
  2319. }
  2320. }
  2321. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2322. NSDictionary *internal = _dictionary;
  2323. NSUInteger count = internal.count;
  2324. if (count == 0) {
  2325. return 0;
  2326. }
  2327. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2328. GPBDataType keyDataType = field.mapKeyDataType;
  2329. size_t result = 0;
  2330. NSEnumerator *keys = [internal keyEnumerator];
  2331. NSNumber *aKey;
  2332. while ((aKey = [keys nextObject])) {
  2333. NSNumber *aValue = internal[aKey];
  2334. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2335. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2336. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2337. }
  2338. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2339. result += tagSize * count;
  2340. return result;
  2341. }
  2342. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2343. asField:(GPBFieldDescriptor *)field {
  2344. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2345. GPBDataType keyDataType = field.mapKeyDataType;
  2346. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2347. NSDictionary *internal = _dictionary;
  2348. NSEnumerator *keys = [internal keyEnumerator];
  2349. NSNumber *aKey;
  2350. while ((aKey = [keys nextObject])) {
  2351. NSNumber *aValue = internal[aKey];
  2352. [outputStream writeInt32NoTag:tag];
  2353. // Write the size of the message.
  2354. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2355. double unwrappedValue = [aValue doubleValue];
  2356. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2357. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2358. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2359. // Write the fields.
  2360. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2361. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2362. }
  2363. }
  2364. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2365. forGPBGenericValueKey:(GPBGenericValue *)key {
  2366. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2367. }
  2368. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2369. [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, __unused BOOL *stop) {
  2370. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2371. }];
  2372. }
  2373. - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
  2374. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2375. if (wrapped && value) {
  2376. *value = [wrapped doubleValue];
  2377. }
  2378. return (wrapped != NULL);
  2379. }
  2380. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2381. if (otherDictionary) {
  2382. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2383. if (_autocreator) {
  2384. GPBAutocreatedDictionaryModified(_autocreator, self);
  2385. }
  2386. }
  2387. }
  2388. - (void)setDouble:(double)value forKey:(uint32_t)key {
  2389. [_dictionary setObject:@(value) forKey:@(key)];
  2390. if (_autocreator) {
  2391. GPBAutocreatedDictionaryModified(_autocreator, self);
  2392. }
  2393. }
  2394. - (void)removeDoubleForKey:(uint32_t)aKey {
  2395. [_dictionary removeObjectForKey:@(aKey)];
  2396. }
  2397. - (void)removeAll {
  2398. [_dictionary removeAllObjects];
  2399. }
  2400. @end
  2401. #pragma mark - UInt32 -> Enum
  2402. @implementation GPBUInt32EnumDictionary {
  2403. @package
  2404. NSMutableDictionary *_dictionary;
  2405. GPBEnumValidationFunc _validationFunc;
  2406. }
  2407. @synthesize validationFunc = _validationFunc;
  2408. - (instancetype)init {
  2409. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2410. }
  2411. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2412. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2413. }
  2414. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2415. rawValues:(const int32_t [])rawValues
  2416. forKeys:(const uint32_t [])keys
  2417. count:(NSUInteger)count {
  2418. self = [super init];
  2419. if (self) {
  2420. _dictionary = [[NSMutableDictionary alloc] init];
  2421. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2422. if (count && rawValues && keys) {
  2423. for (NSUInteger i = 0; i < count; ++i) {
  2424. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2425. }
  2426. }
  2427. }
  2428. return self;
  2429. }
  2430. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2431. self = [self initWithValidationFunction:dictionary.validationFunc
  2432. rawValues:NULL
  2433. forKeys:NULL
  2434. count:0];
  2435. if (self) {
  2436. if (dictionary) {
  2437. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2438. }
  2439. }
  2440. return self;
  2441. }
  2442. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2443. capacity:(__unused NSUInteger)numItems {
  2444. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2445. }
  2446. - (void)dealloc {
  2447. NSAssert(!_autocreator,
  2448. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2449. [self class], _autocreator);
  2450. [_dictionary release];
  2451. [super dealloc];
  2452. }
  2453. - (instancetype)copyWithZone:(NSZone *)zone {
  2454. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2455. }
  2456. - (BOOL)isEqual:(id)other {
  2457. if (self == other) {
  2458. return YES;
  2459. }
  2460. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2461. return NO;
  2462. }
  2463. GPBUInt32EnumDictionary *otherDictionary = other;
  2464. return [_dictionary isEqual:otherDictionary->_dictionary];
  2465. }
  2466. - (NSUInteger)hash {
  2467. return _dictionary.count;
  2468. }
  2469. - (NSString *)description {
  2470. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2471. }
  2472. - (NSUInteger)count {
  2473. return _dictionary.count;
  2474. }
  2475. - (void)enumerateKeysAndRawValuesUsingBlock:
  2476. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  2477. BOOL stop = NO;
  2478. NSDictionary *internal = _dictionary;
  2479. NSEnumerator *keys = [internal keyEnumerator];
  2480. NSNumber *aKey;
  2481. while ((aKey = [keys nextObject])) {
  2482. NSNumber *aValue = internal[aKey];
  2483. block([aKey unsignedIntValue], [aValue intValue], &stop);
  2484. if (stop) {
  2485. break;
  2486. }
  2487. }
  2488. }
  2489. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2490. NSDictionary *internal = _dictionary;
  2491. NSUInteger count = internal.count;
  2492. if (count == 0) {
  2493. return 0;
  2494. }
  2495. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2496. GPBDataType keyDataType = field.mapKeyDataType;
  2497. size_t result = 0;
  2498. NSEnumerator *keys = [internal keyEnumerator];
  2499. NSNumber *aKey;
  2500. while ((aKey = [keys nextObject])) {
  2501. NSNumber *aValue = internal[aKey];
  2502. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2503. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2504. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2505. }
  2506. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2507. result += tagSize * count;
  2508. return result;
  2509. }
  2510. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2511. asField:(GPBFieldDescriptor *)field {
  2512. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2513. GPBDataType keyDataType = field.mapKeyDataType;
  2514. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2515. NSDictionary *internal = _dictionary;
  2516. NSEnumerator *keys = [internal keyEnumerator];
  2517. NSNumber *aKey;
  2518. while ((aKey = [keys nextObject])) {
  2519. NSNumber *aValue = internal[aKey];
  2520. [outputStream writeInt32NoTag:tag];
  2521. // Write the size of the message.
  2522. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2523. int32_t unwrappedValue = [aValue intValue];
  2524. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2525. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2526. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2527. // Write the fields.
  2528. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2529. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2530. }
  2531. }
  2532. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2533. forKey:(GPBGenericValue *)key
  2534. keyDataType:(GPBDataType)keyDataType {
  2535. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2536. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2537. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2538. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2539. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2540. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2541. [outputStream flush];
  2542. [outputStream release];
  2543. return data;
  2544. }
  2545. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2546. forGPBGenericValueKey:(GPBGenericValue *)key {
  2547. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2548. }
  2549. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2550. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, __unused BOOL *stop) {
  2551. block([NSString stringWithFormat:@"%u", key], @(value));
  2552. }];
  2553. }
  2554. - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
  2555. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2556. if (wrapped && value) {
  2557. int32_t result = [wrapped intValue];
  2558. if (!_validationFunc(result)) {
  2559. result = kGPBUnrecognizedEnumeratorValue;
  2560. }
  2561. *value = result;
  2562. }
  2563. return (wrapped != NULL);
  2564. }
  2565. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
  2566. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2567. if (wrapped && rawValue) {
  2568. *rawValue = [wrapped intValue];
  2569. }
  2570. return (wrapped != NULL);
  2571. }
  2572. - (void)enumerateKeysAndEnumsUsingBlock:
  2573. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  2574. GPBEnumValidationFunc func = _validationFunc;
  2575. BOOL stop = NO;
  2576. NSEnumerator *keys = [_dictionary keyEnumerator];
  2577. NSNumber *aKey;
  2578. while ((aKey = [keys nextObject])) {
  2579. NSNumber *aValue = _dictionary[aKey];
  2580. int32_t unwrapped = [aValue intValue];
  2581. if (!func(unwrapped)) {
  2582. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2583. }
  2584. block([aKey unsignedIntValue], unwrapped, &stop);
  2585. if (stop) {
  2586. break;
  2587. }
  2588. }
  2589. }
  2590. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2591. if (otherDictionary) {
  2592. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2593. if (_autocreator) {
  2594. GPBAutocreatedDictionaryModified(_autocreator, self);
  2595. }
  2596. }
  2597. }
  2598. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2599. [_dictionary setObject:@(value) forKey:@(key)];
  2600. if (_autocreator) {
  2601. GPBAutocreatedDictionaryModified(_autocreator, self);
  2602. }
  2603. }
  2604. - (void)removeEnumForKey:(uint32_t)aKey {
  2605. [_dictionary removeObjectForKey:@(aKey)];
  2606. }
  2607. - (void)removeAll {
  2608. [_dictionary removeAllObjects];
  2609. }
  2610. - (void)setEnum:(int32_t)value forKey:(uint32_t)key {
  2611. if (!_validationFunc(value)) {
  2612. [NSException raise:NSInvalidArgumentException
  2613. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2614. value];
  2615. }
  2616. [_dictionary setObject:@(value) forKey:@(key)];
  2617. if (_autocreator) {
  2618. GPBAutocreatedDictionaryModified(_autocreator, self);
  2619. }
  2620. }
  2621. @end
  2622. #pragma mark - UInt32 -> Object
  2623. @implementation GPBUInt32ObjectDictionary {
  2624. @package
  2625. NSMutableDictionary *_dictionary;
  2626. }
  2627. - (instancetype)init {
  2628. return [self initWithObjects:NULL forKeys:NULL count:0];
  2629. }
  2630. - (instancetype)initWithObjects:(const id [])objects
  2631. forKeys:(const uint32_t [])keys
  2632. count:(NSUInteger)count {
  2633. self = [super init];
  2634. if (self) {
  2635. _dictionary = [[NSMutableDictionary alloc] init];
  2636. if (count && objects && keys) {
  2637. for (NSUInteger i = 0; i < count; ++i) {
  2638. if (!objects[i]) {
  2639. [NSException raise:NSInvalidArgumentException
  2640. format:@"Attempting to add nil object to a Dictionary"];
  2641. }
  2642. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  2643. }
  2644. }
  2645. }
  2646. return self;
  2647. }
  2648. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2649. self = [self initWithObjects:NULL forKeys:NULL count:0];
  2650. if (self) {
  2651. if (dictionary) {
  2652. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2653. }
  2654. }
  2655. return self;
  2656. }
  2657. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2658. return [self initWithObjects:NULL forKeys:NULL count:0];
  2659. }
  2660. - (void)dealloc {
  2661. NSAssert(!_autocreator,
  2662. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2663. [self class], _autocreator);
  2664. [_dictionary release];
  2665. [super dealloc];
  2666. }
  2667. - (instancetype)copyWithZone:(NSZone *)zone {
  2668. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2669. }
  2670. - (BOOL)isEqual:(id)other {
  2671. if (self == other) {
  2672. return YES;
  2673. }
  2674. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2675. return NO;
  2676. }
  2677. GPBUInt32ObjectDictionary *otherDictionary = other;
  2678. return [_dictionary isEqual:otherDictionary->_dictionary];
  2679. }
  2680. - (NSUInteger)hash {
  2681. return _dictionary.count;
  2682. }
  2683. - (NSString *)description {
  2684. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2685. }
  2686. - (NSUInteger)count {
  2687. return _dictionary.count;
  2688. }
  2689. - (void)enumerateKeysAndObjectsUsingBlock:
  2690. (void (NS_NOESCAPE ^)(uint32_t key, id object, BOOL *stop))block {
  2691. BOOL stop = NO;
  2692. NSDictionary *internal = _dictionary;
  2693. NSEnumerator *keys = [internal keyEnumerator];
  2694. NSNumber *aKey;
  2695. while ((aKey = [keys nextObject])) {
  2696. id aObject = internal[aKey];
  2697. block([aKey unsignedIntValue], aObject, &stop);
  2698. if (stop) {
  2699. break;
  2700. }
  2701. }
  2702. }
  2703. - (BOOL)isInitialized {
  2704. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2705. if (!msg.initialized) {
  2706. return NO;
  2707. }
  2708. }
  2709. return YES;
  2710. }
  2711. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2712. GPBUInt32ObjectDictionary *newDict =
  2713. [[GPBUInt32ObjectDictionary alloc] init];
  2714. NSEnumerator *keys = [_dictionary keyEnumerator];
  2715. id aKey;
  2716. NSMutableDictionary *internalDict = newDict->_dictionary;
  2717. while ((aKey = [keys nextObject])) {
  2718. GPBMessage *msg = _dictionary[aKey];
  2719. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  2720. [internalDict setObject:copiedMsg forKey:aKey];
  2721. [copiedMsg release];
  2722. }
  2723. return newDict;
  2724. }
  2725. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2726. NSDictionary *internal = _dictionary;
  2727. NSUInteger count = internal.count;
  2728. if (count == 0) {
  2729. return 0;
  2730. }
  2731. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2732. GPBDataType keyDataType = field.mapKeyDataType;
  2733. size_t result = 0;
  2734. NSEnumerator *keys = [internal keyEnumerator];
  2735. NSNumber *aKey;
  2736. while ((aKey = [keys nextObject])) {
  2737. id aObject = internal[aKey];
  2738. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2739. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  2740. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2741. }
  2742. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2743. result += tagSize * count;
  2744. return result;
  2745. }
  2746. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2747. asField:(GPBFieldDescriptor *)field {
  2748. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2749. GPBDataType keyDataType = field.mapKeyDataType;
  2750. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2751. NSDictionary *internal = _dictionary;
  2752. NSEnumerator *keys = [internal keyEnumerator];
  2753. NSNumber *aKey;
  2754. while ((aKey = [keys nextObject])) {
  2755. id aObject = internal[aKey];
  2756. [outputStream writeInt32NoTag:tag];
  2757. // Write the size of the message.
  2758. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2759. id unwrappedValue = aObject;
  2760. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2761. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2762. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2763. // Write the fields.
  2764. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2765. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2766. }
  2767. }
  2768. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2769. forGPBGenericValueKey:(GPBGenericValue *)key {
  2770. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  2771. }
  2772. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2773. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, __unused BOOL *stop) {
  2774. block([NSString stringWithFormat:@"%u", key], object);
  2775. }];
  2776. }
  2777. - (id)objectForKey:(uint32_t)key {
  2778. id result = [_dictionary objectForKey:@(key)];
  2779. return result;
  2780. }
  2781. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  2782. if (otherDictionary) {
  2783. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2784. if (_autocreator) {
  2785. GPBAutocreatedDictionaryModified(_autocreator, self);
  2786. }
  2787. }
  2788. }
  2789. - (void)setObject:(id)object forKey:(uint32_t)key {
  2790. if (!object) {
  2791. [NSException raise:NSInvalidArgumentException
  2792. format:@"Attempting to add nil object to a Dictionary"];
  2793. }
  2794. [_dictionary setObject:object forKey:@(key)];
  2795. if (_autocreator) {
  2796. GPBAutocreatedDictionaryModified(_autocreator, self);
  2797. }
  2798. }
  2799. - (void)removeObjectForKey:(uint32_t)aKey {
  2800. [_dictionary removeObjectForKey:@(aKey)];
  2801. }
  2802. - (void)removeAll {
  2803. [_dictionary removeAllObjects];
  2804. }
  2805. @end
  2806. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  2807. // This block of code is generated, do not edit it directly.
  2808. #pragma mark - Int32 -> UInt32
  2809. @implementation GPBInt32UInt32Dictionary {
  2810. @package
  2811. NSMutableDictionary *_dictionary;
  2812. }
  2813. - (instancetype)init {
  2814. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  2815. }
  2816. - (instancetype)initWithUInt32s:(const uint32_t [])values
  2817. forKeys:(const int32_t [])keys
  2818. count:(NSUInteger)count {
  2819. self = [super init];
  2820. if (self) {
  2821. _dictionary = [[NSMutableDictionary alloc] init];
  2822. if (count && values && keys) {
  2823. for (NSUInteger i = 0; i < count; ++i) {
  2824. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2825. }
  2826. }
  2827. }
  2828. return self;
  2829. }
  2830. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  2831. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  2832. if (self) {
  2833. if (dictionary) {
  2834. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2835. }
  2836. }
  2837. return self;
  2838. }
  2839. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2840. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  2841. }
  2842. - (void)dealloc {
  2843. NSAssert(!_autocreator,
  2844. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2845. [self class], _autocreator);
  2846. [_dictionary release];
  2847. [super dealloc];
  2848. }
  2849. - (instancetype)copyWithZone:(NSZone *)zone {
  2850. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  2851. }
  2852. - (BOOL)isEqual:(id)other {
  2853. if (self == other) {
  2854. return YES;
  2855. }
  2856. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  2857. return NO;
  2858. }
  2859. GPBInt32UInt32Dictionary *otherDictionary = other;
  2860. return [_dictionary isEqual:otherDictionary->_dictionary];
  2861. }
  2862. - (NSUInteger)hash {
  2863. return _dictionary.count;
  2864. }
  2865. - (NSString *)description {
  2866. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2867. }
  2868. - (NSUInteger)count {
  2869. return _dictionary.count;
  2870. }
  2871. - (void)enumerateKeysAndUInt32sUsingBlock:
  2872. (void (NS_NOESCAPE ^)(int32_t key, uint32_t value, BOOL *stop))block {
  2873. BOOL stop = NO;
  2874. NSDictionary *internal = _dictionary;
  2875. NSEnumerator *keys = [internal keyEnumerator];
  2876. NSNumber *aKey;
  2877. while ((aKey = [keys nextObject])) {
  2878. NSNumber *aValue = internal[aKey];
  2879. block([aKey intValue], [aValue unsignedIntValue], &stop);
  2880. if (stop) {
  2881. break;
  2882. }
  2883. }
  2884. }
  2885. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2886. NSDictionary *internal = _dictionary;
  2887. NSUInteger count = internal.count;
  2888. if (count == 0) {
  2889. return 0;
  2890. }
  2891. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2892. GPBDataType keyDataType = field.mapKeyDataType;
  2893. size_t result = 0;
  2894. NSEnumerator *keys = [internal keyEnumerator];
  2895. NSNumber *aKey;
  2896. while ((aKey = [keys nextObject])) {
  2897. NSNumber *aValue = internal[aKey];
  2898. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  2899. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  2900. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2901. }
  2902. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2903. result += tagSize * count;
  2904. return result;
  2905. }
  2906. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2907. asField:(GPBFieldDescriptor *)field {
  2908. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2909. GPBDataType keyDataType = field.mapKeyDataType;
  2910. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2911. NSDictionary *internal = _dictionary;
  2912. NSEnumerator *keys = [internal keyEnumerator];
  2913. NSNumber *aKey;
  2914. while ((aKey = [keys nextObject])) {
  2915. NSNumber *aValue = internal[aKey];
  2916. [outputStream writeInt32NoTag:tag];
  2917. // Write the size of the message.
  2918. int32_t unwrappedKey = [aKey intValue];
  2919. uint32_t unwrappedValue = [aValue unsignedIntValue];
  2920. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2921. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2922. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2923. // Write the fields.
  2924. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2925. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2926. }
  2927. }
  2928. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2929. forGPBGenericValueKey:(GPBGenericValue *)key {
  2930. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  2931. }
  2932. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2933. [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, __unused BOOL *stop) {
  2934. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  2935. }];
  2936. }
  2937. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
  2938. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2939. if (wrapped && value) {
  2940. *value = [wrapped unsignedIntValue];
  2941. }
  2942. return (wrapped != NULL);
  2943. }
  2944. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  2945. if (otherDictionary) {
  2946. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2947. if (_autocreator) {
  2948. GPBAutocreatedDictionaryModified(_autocreator, self);
  2949. }
  2950. }
  2951. }
  2952. - (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
  2953. [_dictionary setObject:@(value) forKey:@(key)];
  2954. if (_autocreator) {
  2955. GPBAutocreatedDictionaryModified(_autocreator, self);
  2956. }
  2957. }
  2958. - (void)removeUInt32ForKey:(int32_t)aKey {
  2959. [_dictionary removeObjectForKey:@(aKey)];
  2960. }
  2961. - (void)removeAll {
  2962. [_dictionary removeAllObjects];
  2963. }
  2964. @end
  2965. #pragma mark - Int32 -> Int32
  2966. @implementation GPBInt32Int32Dictionary {
  2967. @package
  2968. NSMutableDictionary *_dictionary;
  2969. }
  2970. - (instancetype)init {
  2971. return [self initWithInt32s:NULL forKeys:NULL count:0];
  2972. }
  2973. - (instancetype)initWithInt32s:(const int32_t [])values
  2974. forKeys:(const int32_t [])keys
  2975. count:(NSUInteger)count {
  2976. self = [super init];
  2977. if (self) {
  2978. _dictionary = [[NSMutableDictionary alloc] init];
  2979. if (count && values && keys) {
  2980. for (NSUInteger i = 0; i < count; ++i) {
  2981. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2982. }
  2983. }
  2984. }
  2985. return self;
  2986. }
  2987. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  2988. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  2989. if (self) {
  2990. if (dictionary) {
  2991. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2992. }
  2993. }
  2994. return self;
  2995. }
  2996. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2997. return [self initWithInt32s:NULL forKeys:NULL count:0];
  2998. }
  2999. - (void)dealloc {
  3000. NSAssert(!_autocreator,
  3001. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3002. [self class], _autocreator);
  3003. [_dictionary release];
  3004. [super dealloc];
  3005. }
  3006. - (instancetype)copyWithZone:(NSZone *)zone {
  3007. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3008. }
  3009. - (BOOL)isEqual:(id)other {
  3010. if (self == other) {
  3011. return YES;
  3012. }
  3013. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3014. return NO;
  3015. }
  3016. GPBInt32Int32Dictionary *otherDictionary = other;
  3017. return [_dictionary isEqual:otherDictionary->_dictionary];
  3018. }
  3019. - (NSUInteger)hash {
  3020. return _dictionary.count;
  3021. }
  3022. - (NSString *)description {
  3023. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3024. }
  3025. - (NSUInteger)count {
  3026. return _dictionary.count;
  3027. }
  3028. - (void)enumerateKeysAndInt32sUsingBlock:
  3029. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  3030. BOOL stop = NO;
  3031. NSDictionary *internal = _dictionary;
  3032. NSEnumerator *keys = [internal keyEnumerator];
  3033. NSNumber *aKey;
  3034. while ((aKey = [keys nextObject])) {
  3035. NSNumber *aValue = internal[aKey];
  3036. block([aKey intValue], [aValue intValue], &stop);
  3037. if (stop) {
  3038. break;
  3039. }
  3040. }
  3041. }
  3042. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3043. NSDictionary *internal = _dictionary;
  3044. NSUInteger count = internal.count;
  3045. if (count == 0) {
  3046. return 0;
  3047. }
  3048. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3049. GPBDataType keyDataType = field.mapKeyDataType;
  3050. size_t result = 0;
  3051. NSEnumerator *keys = [internal keyEnumerator];
  3052. NSNumber *aKey;
  3053. while ((aKey = [keys nextObject])) {
  3054. NSNumber *aValue = internal[aKey];
  3055. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3056. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3057. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3058. }
  3059. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3060. result += tagSize * count;
  3061. return result;
  3062. }
  3063. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3064. asField:(GPBFieldDescriptor *)field {
  3065. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3066. GPBDataType keyDataType = field.mapKeyDataType;
  3067. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3068. NSDictionary *internal = _dictionary;
  3069. NSEnumerator *keys = [internal keyEnumerator];
  3070. NSNumber *aKey;
  3071. while ((aKey = [keys nextObject])) {
  3072. NSNumber *aValue = internal[aKey];
  3073. [outputStream writeInt32NoTag:tag];
  3074. // Write the size of the message.
  3075. int32_t unwrappedKey = [aKey intValue];
  3076. int32_t unwrappedValue = [aValue intValue];
  3077. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3078. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3079. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3080. // Write the fields.
  3081. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3082. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3083. }
  3084. }
  3085. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3086. forGPBGenericValueKey:(GPBGenericValue *)key {
  3087. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3088. }
  3089. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3090. [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, __unused BOOL *stop) {
  3091. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3092. }];
  3093. }
  3094. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
  3095. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3096. if (wrapped && value) {
  3097. *value = [wrapped intValue];
  3098. }
  3099. return (wrapped != NULL);
  3100. }
  3101. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3102. if (otherDictionary) {
  3103. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3104. if (_autocreator) {
  3105. GPBAutocreatedDictionaryModified(_autocreator, self);
  3106. }
  3107. }
  3108. }
  3109. - (void)setInt32:(int32_t)value forKey:(int32_t)key {
  3110. [_dictionary setObject:@(value) forKey:@(key)];
  3111. if (_autocreator) {
  3112. GPBAutocreatedDictionaryModified(_autocreator, self);
  3113. }
  3114. }
  3115. - (void)removeInt32ForKey:(int32_t)aKey {
  3116. [_dictionary removeObjectForKey:@(aKey)];
  3117. }
  3118. - (void)removeAll {
  3119. [_dictionary removeAllObjects];
  3120. }
  3121. @end
  3122. #pragma mark - Int32 -> UInt64
  3123. @implementation GPBInt32UInt64Dictionary {
  3124. @package
  3125. NSMutableDictionary *_dictionary;
  3126. }
  3127. - (instancetype)init {
  3128. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3129. }
  3130. - (instancetype)initWithUInt64s:(const uint64_t [])values
  3131. forKeys:(const int32_t [])keys
  3132. count:(NSUInteger)count {
  3133. self = [super init];
  3134. if (self) {
  3135. _dictionary = [[NSMutableDictionary alloc] init];
  3136. if (count && values && keys) {
  3137. for (NSUInteger i = 0; i < count; ++i) {
  3138. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3139. }
  3140. }
  3141. }
  3142. return self;
  3143. }
  3144. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3145. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  3146. if (self) {
  3147. if (dictionary) {
  3148. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3149. }
  3150. }
  3151. return self;
  3152. }
  3153. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3154. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3155. }
  3156. - (void)dealloc {
  3157. NSAssert(!_autocreator,
  3158. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3159. [self class], _autocreator);
  3160. [_dictionary release];
  3161. [super dealloc];
  3162. }
  3163. - (instancetype)copyWithZone:(NSZone *)zone {
  3164. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3165. }
  3166. - (BOOL)isEqual:(id)other {
  3167. if (self == other) {
  3168. return YES;
  3169. }
  3170. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3171. return NO;
  3172. }
  3173. GPBInt32UInt64Dictionary *otherDictionary = other;
  3174. return [_dictionary isEqual:otherDictionary->_dictionary];
  3175. }
  3176. - (NSUInteger)hash {
  3177. return _dictionary.count;
  3178. }
  3179. - (NSString *)description {
  3180. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3181. }
  3182. - (NSUInteger)count {
  3183. return _dictionary.count;
  3184. }
  3185. - (void)enumerateKeysAndUInt64sUsingBlock:
  3186. (void (NS_NOESCAPE ^)(int32_t key, uint64_t value, BOOL *stop))block {
  3187. BOOL stop = NO;
  3188. NSDictionary *internal = _dictionary;
  3189. NSEnumerator *keys = [internal keyEnumerator];
  3190. NSNumber *aKey;
  3191. while ((aKey = [keys nextObject])) {
  3192. NSNumber *aValue = internal[aKey];
  3193. block([aKey intValue], [aValue unsignedLongLongValue], &stop);
  3194. if (stop) {
  3195. break;
  3196. }
  3197. }
  3198. }
  3199. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3200. NSDictionary *internal = _dictionary;
  3201. NSUInteger count = internal.count;
  3202. if (count == 0) {
  3203. return 0;
  3204. }
  3205. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3206. GPBDataType keyDataType = field.mapKeyDataType;
  3207. size_t result = 0;
  3208. NSEnumerator *keys = [internal keyEnumerator];
  3209. NSNumber *aKey;
  3210. while ((aKey = [keys nextObject])) {
  3211. NSNumber *aValue = internal[aKey];
  3212. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3213. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3214. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3215. }
  3216. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3217. result += tagSize * count;
  3218. return result;
  3219. }
  3220. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3221. asField:(GPBFieldDescriptor *)field {
  3222. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3223. GPBDataType keyDataType = field.mapKeyDataType;
  3224. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3225. NSDictionary *internal = _dictionary;
  3226. NSEnumerator *keys = [internal keyEnumerator];
  3227. NSNumber *aKey;
  3228. while ((aKey = [keys nextObject])) {
  3229. NSNumber *aValue = internal[aKey];
  3230. [outputStream writeInt32NoTag:tag];
  3231. // Write the size of the message.
  3232. int32_t unwrappedKey = [aKey intValue];
  3233. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  3234. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3235. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3236. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3237. // Write the fields.
  3238. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3239. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3240. }
  3241. }
  3242. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3243. forGPBGenericValueKey:(GPBGenericValue *)key {
  3244. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3245. }
  3246. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3247. [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, __unused BOOL *stop) {
  3248. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3249. }];
  3250. }
  3251. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
  3252. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3253. if (wrapped && value) {
  3254. *value = [wrapped unsignedLongLongValue];
  3255. }
  3256. return (wrapped != NULL);
  3257. }
  3258. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3259. if (otherDictionary) {
  3260. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3261. if (_autocreator) {
  3262. GPBAutocreatedDictionaryModified(_autocreator, self);
  3263. }
  3264. }
  3265. }
  3266. - (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
  3267. [_dictionary setObject:@(value) forKey:@(key)];
  3268. if (_autocreator) {
  3269. GPBAutocreatedDictionaryModified(_autocreator, self);
  3270. }
  3271. }
  3272. - (void)removeUInt64ForKey:(int32_t)aKey {
  3273. [_dictionary removeObjectForKey:@(aKey)];
  3274. }
  3275. - (void)removeAll {
  3276. [_dictionary removeAllObjects];
  3277. }
  3278. @end
  3279. #pragma mark - Int32 -> Int64
  3280. @implementation GPBInt32Int64Dictionary {
  3281. @package
  3282. NSMutableDictionary *_dictionary;
  3283. }
  3284. - (instancetype)init {
  3285. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3286. }
  3287. - (instancetype)initWithInt64s:(const int64_t [])values
  3288. forKeys:(const int32_t [])keys
  3289. count:(NSUInteger)count {
  3290. self = [super init];
  3291. if (self) {
  3292. _dictionary = [[NSMutableDictionary alloc] init];
  3293. if (count && values && keys) {
  3294. for (NSUInteger i = 0; i < count; ++i) {
  3295. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3296. }
  3297. }
  3298. }
  3299. return self;
  3300. }
  3301. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3302. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  3303. if (self) {
  3304. if (dictionary) {
  3305. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3306. }
  3307. }
  3308. return self;
  3309. }
  3310. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3311. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3312. }
  3313. - (void)dealloc {
  3314. NSAssert(!_autocreator,
  3315. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3316. [self class], _autocreator);
  3317. [_dictionary release];
  3318. [super dealloc];
  3319. }
  3320. - (instancetype)copyWithZone:(NSZone *)zone {
  3321. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3322. }
  3323. - (BOOL)isEqual:(id)other {
  3324. if (self == other) {
  3325. return YES;
  3326. }
  3327. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3328. return NO;
  3329. }
  3330. GPBInt32Int64Dictionary *otherDictionary = other;
  3331. return [_dictionary isEqual:otherDictionary->_dictionary];
  3332. }
  3333. - (NSUInteger)hash {
  3334. return _dictionary.count;
  3335. }
  3336. - (NSString *)description {
  3337. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3338. }
  3339. - (NSUInteger)count {
  3340. return _dictionary.count;
  3341. }
  3342. - (void)enumerateKeysAndInt64sUsingBlock:
  3343. (void (NS_NOESCAPE ^)(int32_t key, int64_t value, BOOL *stop))block {
  3344. BOOL stop = NO;
  3345. NSDictionary *internal = _dictionary;
  3346. NSEnumerator *keys = [internal keyEnumerator];
  3347. NSNumber *aKey;
  3348. while ((aKey = [keys nextObject])) {
  3349. NSNumber *aValue = internal[aKey];
  3350. block([aKey intValue], [aValue longLongValue], &stop);
  3351. if (stop) {
  3352. break;
  3353. }
  3354. }
  3355. }
  3356. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3357. NSDictionary *internal = _dictionary;
  3358. NSUInteger count = internal.count;
  3359. if (count == 0) {
  3360. return 0;
  3361. }
  3362. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3363. GPBDataType keyDataType = field.mapKeyDataType;
  3364. size_t result = 0;
  3365. NSEnumerator *keys = [internal keyEnumerator];
  3366. NSNumber *aKey;
  3367. while ((aKey = [keys nextObject])) {
  3368. NSNumber *aValue = internal[aKey];
  3369. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3370. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3371. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3372. }
  3373. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3374. result += tagSize * count;
  3375. return result;
  3376. }
  3377. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3378. asField:(GPBFieldDescriptor *)field {
  3379. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3380. GPBDataType keyDataType = field.mapKeyDataType;
  3381. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3382. NSDictionary *internal = _dictionary;
  3383. NSEnumerator *keys = [internal keyEnumerator];
  3384. NSNumber *aKey;
  3385. while ((aKey = [keys nextObject])) {
  3386. NSNumber *aValue = internal[aKey];
  3387. [outputStream writeInt32NoTag:tag];
  3388. // Write the size of the message.
  3389. int32_t unwrappedKey = [aKey intValue];
  3390. int64_t unwrappedValue = [aValue longLongValue];
  3391. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3392. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3393. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3394. // Write the fields.
  3395. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3396. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3397. }
  3398. }
  3399. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3400. forGPBGenericValueKey:(GPBGenericValue *)key {
  3401. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3402. }
  3403. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3404. [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, __unused BOOL *stop) {
  3405. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3406. }];
  3407. }
  3408. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
  3409. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3410. if (wrapped && value) {
  3411. *value = [wrapped longLongValue];
  3412. }
  3413. return (wrapped != NULL);
  3414. }
  3415. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3416. if (otherDictionary) {
  3417. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3418. if (_autocreator) {
  3419. GPBAutocreatedDictionaryModified(_autocreator, self);
  3420. }
  3421. }
  3422. }
  3423. - (void)setInt64:(int64_t)value forKey:(int32_t)key {
  3424. [_dictionary setObject:@(value) forKey:@(key)];
  3425. if (_autocreator) {
  3426. GPBAutocreatedDictionaryModified(_autocreator, self);
  3427. }
  3428. }
  3429. - (void)removeInt64ForKey:(int32_t)aKey {
  3430. [_dictionary removeObjectForKey:@(aKey)];
  3431. }
  3432. - (void)removeAll {
  3433. [_dictionary removeAllObjects];
  3434. }
  3435. @end
  3436. #pragma mark - Int32 -> Bool
  3437. @implementation GPBInt32BoolDictionary {
  3438. @package
  3439. NSMutableDictionary *_dictionary;
  3440. }
  3441. - (instancetype)init {
  3442. return [self initWithBools:NULL forKeys:NULL count:0];
  3443. }
  3444. - (instancetype)initWithBools:(const BOOL [])values
  3445. forKeys:(const int32_t [])keys
  3446. count:(NSUInteger)count {
  3447. self = [super init];
  3448. if (self) {
  3449. _dictionary = [[NSMutableDictionary alloc] init];
  3450. if (count && values && keys) {
  3451. for (NSUInteger i = 0; i < count; ++i) {
  3452. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3453. }
  3454. }
  3455. }
  3456. return self;
  3457. }
  3458. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3459. self = [self initWithBools:NULL forKeys:NULL count:0];
  3460. if (self) {
  3461. if (dictionary) {
  3462. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3463. }
  3464. }
  3465. return self;
  3466. }
  3467. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3468. return [self initWithBools:NULL forKeys:NULL count:0];
  3469. }
  3470. - (void)dealloc {
  3471. NSAssert(!_autocreator,
  3472. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3473. [self class], _autocreator);
  3474. [_dictionary release];
  3475. [super dealloc];
  3476. }
  3477. - (instancetype)copyWithZone:(NSZone *)zone {
  3478. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3479. }
  3480. - (BOOL)isEqual:(id)other {
  3481. if (self == other) {
  3482. return YES;
  3483. }
  3484. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3485. return NO;
  3486. }
  3487. GPBInt32BoolDictionary *otherDictionary = other;
  3488. return [_dictionary isEqual:otherDictionary->_dictionary];
  3489. }
  3490. - (NSUInteger)hash {
  3491. return _dictionary.count;
  3492. }
  3493. - (NSString *)description {
  3494. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3495. }
  3496. - (NSUInteger)count {
  3497. return _dictionary.count;
  3498. }
  3499. - (void)enumerateKeysAndBoolsUsingBlock:
  3500. (void (NS_NOESCAPE ^)(int32_t key, BOOL value, BOOL *stop))block {
  3501. BOOL stop = NO;
  3502. NSDictionary *internal = _dictionary;
  3503. NSEnumerator *keys = [internal keyEnumerator];
  3504. NSNumber *aKey;
  3505. while ((aKey = [keys nextObject])) {
  3506. NSNumber *aValue = internal[aKey];
  3507. block([aKey intValue], [aValue boolValue], &stop);
  3508. if (stop) {
  3509. break;
  3510. }
  3511. }
  3512. }
  3513. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3514. NSDictionary *internal = _dictionary;
  3515. NSUInteger count = internal.count;
  3516. if (count == 0) {
  3517. return 0;
  3518. }
  3519. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3520. GPBDataType keyDataType = field.mapKeyDataType;
  3521. size_t result = 0;
  3522. NSEnumerator *keys = [internal keyEnumerator];
  3523. NSNumber *aKey;
  3524. while ((aKey = [keys nextObject])) {
  3525. NSNumber *aValue = internal[aKey];
  3526. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3527. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3528. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3529. }
  3530. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3531. result += tagSize * count;
  3532. return result;
  3533. }
  3534. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3535. asField:(GPBFieldDescriptor *)field {
  3536. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3537. GPBDataType keyDataType = field.mapKeyDataType;
  3538. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3539. NSDictionary *internal = _dictionary;
  3540. NSEnumerator *keys = [internal keyEnumerator];
  3541. NSNumber *aKey;
  3542. while ((aKey = [keys nextObject])) {
  3543. NSNumber *aValue = internal[aKey];
  3544. [outputStream writeInt32NoTag:tag];
  3545. // Write the size of the message.
  3546. int32_t unwrappedKey = [aKey intValue];
  3547. BOOL unwrappedValue = [aValue boolValue];
  3548. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3549. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3550. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3551. // Write the fields.
  3552. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3553. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3554. }
  3555. }
  3556. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3557. forGPBGenericValueKey:(GPBGenericValue *)key {
  3558. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3559. }
  3560. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3561. [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, __unused BOOL *stop) {
  3562. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3563. }];
  3564. }
  3565. - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
  3566. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3567. if (wrapped && value) {
  3568. *value = [wrapped boolValue];
  3569. }
  3570. return (wrapped != NULL);
  3571. }
  3572. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3573. if (otherDictionary) {
  3574. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3575. if (_autocreator) {
  3576. GPBAutocreatedDictionaryModified(_autocreator, self);
  3577. }
  3578. }
  3579. }
  3580. - (void)setBool:(BOOL)value forKey:(int32_t)key {
  3581. [_dictionary setObject:@(value) forKey:@(key)];
  3582. if (_autocreator) {
  3583. GPBAutocreatedDictionaryModified(_autocreator, self);
  3584. }
  3585. }
  3586. - (void)removeBoolForKey:(int32_t)aKey {
  3587. [_dictionary removeObjectForKey:@(aKey)];
  3588. }
  3589. - (void)removeAll {
  3590. [_dictionary removeAllObjects];
  3591. }
  3592. @end
  3593. #pragma mark - Int32 -> Float
  3594. @implementation GPBInt32FloatDictionary {
  3595. @package
  3596. NSMutableDictionary *_dictionary;
  3597. }
  3598. - (instancetype)init {
  3599. return [self initWithFloats:NULL forKeys:NULL count:0];
  3600. }
  3601. - (instancetype)initWithFloats:(const float [])values
  3602. forKeys:(const int32_t [])keys
  3603. count:(NSUInteger)count {
  3604. self = [super init];
  3605. if (self) {
  3606. _dictionary = [[NSMutableDictionary alloc] init];
  3607. if (count && values && keys) {
  3608. for (NSUInteger i = 0; i < count; ++i) {
  3609. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3610. }
  3611. }
  3612. }
  3613. return self;
  3614. }
  3615. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3616. self = [self initWithFloats:NULL forKeys:NULL count:0];
  3617. if (self) {
  3618. if (dictionary) {
  3619. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3620. }
  3621. }
  3622. return self;
  3623. }
  3624. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3625. return [self initWithFloats:NULL forKeys:NULL count:0];
  3626. }
  3627. - (void)dealloc {
  3628. NSAssert(!_autocreator,
  3629. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3630. [self class], _autocreator);
  3631. [_dictionary release];
  3632. [super dealloc];
  3633. }
  3634. - (instancetype)copyWithZone:(NSZone *)zone {
  3635. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  3636. }
  3637. - (BOOL)isEqual:(id)other {
  3638. if (self == other) {
  3639. return YES;
  3640. }
  3641. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  3642. return NO;
  3643. }
  3644. GPBInt32FloatDictionary *otherDictionary = other;
  3645. return [_dictionary isEqual:otherDictionary->_dictionary];
  3646. }
  3647. - (NSUInteger)hash {
  3648. return _dictionary.count;
  3649. }
  3650. - (NSString *)description {
  3651. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3652. }
  3653. - (NSUInteger)count {
  3654. return _dictionary.count;
  3655. }
  3656. - (void)enumerateKeysAndFloatsUsingBlock:
  3657. (void (NS_NOESCAPE ^)(int32_t key, float value, BOOL *stop))block {
  3658. BOOL stop = NO;
  3659. NSDictionary *internal = _dictionary;
  3660. NSEnumerator *keys = [internal keyEnumerator];
  3661. NSNumber *aKey;
  3662. while ((aKey = [keys nextObject])) {
  3663. NSNumber *aValue = internal[aKey];
  3664. block([aKey intValue], [aValue floatValue], &stop);
  3665. if (stop) {
  3666. break;
  3667. }
  3668. }
  3669. }
  3670. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3671. NSDictionary *internal = _dictionary;
  3672. NSUInteger count = internal.count;
  3673. if (count == 0) {
  3674. return 0;
  3675. }
  3676. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3677. GPBDataType keyDataType = field.mapKeyDataType;
  3678. size_t result = 0;
  3679. NSEnumerator *keys = [internal keyEnumerator];
  3680. NSNumber *aKey;
  3681. while ((aKey = [keys nextObject])) {
  3682. NSNumber *aValue = internal[aKey];
  3683. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3684. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  3685. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3686. }
  3687. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3688. result += tagSize * count;
  3689. return result;
  3690. }
  3691. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3692. asField:(GPBFieldDescriptor *)field {
  3693. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3694. GPBDataType keyDataType = field.mapKeyDataType;
  3695. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3696. NSDictionary *internal = _dictionary;
  3697. NSEnumerator *keys = [internal keyEnumerator];
  3698. NSNumber *aKey;
  3699. while ((aKey = [keys nextObject])) {
  3700. NSNumber *aValue = internal[aKey];
  3701. [outputStream writeInt32NoTag:tag];
  3702. // Write the size of the message.
  3703. int32_t unwrappedKey = [aKey intValue];
  3704. float unwrappedValue = [aValue floatValue];
  3705. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3706. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3707. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3708. // Write the fields.
  3709. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3710. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3711. }
  3712. }
  3713. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3714. forGPBGenericValueKey:(GPBGenericValue *)key {
  3715. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  3716. }
  3717. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3718. [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, __unused BOOL *stop) {
  3719. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  3720. }];
  3721. }
  3722. - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
  3723. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3724. if (wrapped && value) {
  3725. *value = [wrapped floatValue];
  3726. }
  3727. return (wrapped != NULL);
  3728. }
  3729. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  3730. if (otherDictionary) {
  3731. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3732. if (_autocreator) {
  3733. GPBAutocreatedDictionaryModified(_autocreator, self);
  3734. }
  3735. }
  3736. }
  3737. - (void)setFloat:(float)value forKey:(int32_t)key {
  3738. [_dictionary setObject:@(value) forKey:@(key)];
  3739. if (_autocreator) {
  3740. GPBAutocreatedDictionaryModified(_autocreator, self);
  3741. }
  3742. }
  3743. - (void)removeFloatForKey:(int32_t)aKey {
  3744. [_dictionary removeObjectForKey:@(aKey)];
  3745. }
  3746. - (void)removeAll {
  3747. [_dictionary removeAllObjects];
  3748. }
  3749. @end
  3750. #pragma mark - Int32 -> Double
  3751. @implementation GPBInt32DoubleDictionary {
  3752. @package
  3753. NSMutableDictionary *_dictionary;
  3754. }
  3755. - (instancetype)init {
  3756. return [self initWithDoubles:NULL forKeys:NULL count:0];
  3757. }
  3758. - (instancetype)initWithDoubles:(const double [])values
  3759. forKeys:(const int32_t [])keys
  3760. count:(NSUInteger)count {
  3761. self = [super init];
  3762. if (self) {
  3763. _dictionary = [[NSMutableDictionary alloc] init];
  3764. if (count && values && keys) {
  3765. for (NSUInteger i = 0; i < count; ++i) {
  3766. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3767. }
  3768. }
  3769. }
  3770. return self;
  3771. }
  3772. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  3773. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  3774. if (self) {
  3775. if (dictionary) {
  3776. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3777. }
  3778. }
  3779. return self;
  3780. }
  3781. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3782. return [self initWithDoubles:NULL forKeys:NULL count:0];
  3783. }
  3784. - (void)dealloc {
  3785. NSAssert(!_autocreator,
  3786. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3787. [self class], _autocreator);
  3788. [_dictionary release];
  3789. [super dealloc];
  3790. }
  3791. - (instancetype)copyWithZone:(NSZone *)zone {
  3792. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  3793. }
  3794. - (BOOL)isEqual:(id)other {
  3795. if (self == other) {
  3796. return YES;
  3797. }
  3798. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  3799. return NO;
  3800. }
  3801. GPBInt32DoubleDictionary *otherDictionary = other;
  3802. return [_dictionary isEqual:otherDictionary->_dictionary];
  3803. }
  3804. - (NSUInteger)hash {
  3805. return _dictionary.count;
  3806. }
  3807. - (NSString *)description {
  3808. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3809. }
  3810. - (NSUInteger)count {
  3811. return _dictionary.count;
  3812. }
  3813. - (void)enumerateKeysAndDoublesUsingBlock:
  3814. (void (NS_NOESCAPE ^)(int32_t key, double value, BOOL *stop))block {
  3815. BOOL stop = NO;
  3816. NSDictionary *internal = _dictionary;
  3817. NSEnumerator *keys = [internal keyEnumerator];
  3818. NSNumber *aKey;
  3819. while ((aKey = [keys nextObject])) {
  3820. NSNumber *aValue = internal[aKey];
  3821. block([aKey intValue], [aValue doubleValue], &stop);
  3822. if (stop) {
  3823. break;
  3824. }
  3825. }
  3826. }
  3827. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3828. NSDictionary *internal = _dictionary;
  3829. NSUInteger count = internal.count;
  3830. if (count == 0) {
  3831. return 0;
  3832. }
  3833. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3834. GPBDataType keyDataType = field.mapKeyDataType;
  3835. size_t result = 0;
  3836. NSEnumerator *keys = [internal keyEnumerator];
  3837. NSNumber *aKey;
  3838. while ((aKey = [keys nextObject])) {
  3839. NSNumber *aValue = internal[aKey];
  3840. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3841. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  3842. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3843. }
  3844. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3845. result += tagSize * count;
  3846. return result;
  3847. }
  3848. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3849. asField:(GPBFieldDescriptor *)field {
  3850. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3851. GPBDataType keyDataType = field.mapKeyDataType;
  3852. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3853. NSDictionary *internal = _dictionary;
  3854. NSEnumerator *keys = [internal keyEnumerator];
  3855. NSNumber *aKey;
  3856. while ((aKey = [keys nextObject])) {
  3857. NSNumber *aValue = internal[aKey];
  3858. [outputStream writeInt32NoTag:tag];
  3859. // Write the size of the message.
  3860. int32_t unwrappedKey = [aKey intValue];
  3861. double unwrappedValue = [aValue doubleValue];
  3862. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3863. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3864. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3865. // Write the fields.
  3866. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3867. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3868. }
  3869. }
  3870. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3871. forGPBGenericValueKey:(GPBGenericValue *)key {
  3872. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  3873. }
  3874. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3875. [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, __unused BOOL *stop) {
  3876. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  3877. }];
  3878. }
  3879. - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
  3880. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3881. if (wrapped && value) {
  3882. *value = [wrapped doubleValue];
  3883. }
  3884. return (wrapped != NULL);
  3885. }
  3886. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  3887. if (otherDictionary) {
  3888. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3889. if (_autocreator) {
  3890. GPBAutocreatedDictionaryModified(_autocreator, self);
  3891. }
  3892. }
  3893. }
  3894. - (void)setDouble:(double)value forKey:(int32_t)key {
  3895. [_dictionary setObject:@(value) forKey:@(key)];
  3896. if (_autocreator) {
  3897. GPBAutocreatedDictionaryModified(_autocreator, self);
  3898. }
  3899. }
  3900. - (void)removeDoubleForKey:(int32_t)aKey {
  3901. [_dictionary removeObjectForKey:@(aKey)];
  3902. }
  3903. - (void)removeAll {
  3904. [_dictionary removeAllObjects];
  3905. }
  3906. @end
  3907. #pragma mark - Int32 -> Enum
  3908. @implementation GPBInt32EnumDictionary {
  3909. @package
  3910. NSMutableDictionary *_dictionary;
  3911. GPBEnumValidationFunc _validationFunc;
  3912. }
  3913. @synthesize validationFunc = _validationFunc;
  3914. - (instancetype)init {
  3915. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  3916. }
  3917. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  3918. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  3919. }
  3920. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  3921. rawValues:(const int32_t [])rawValues
  3922. forKeys:(const int32_t [])keys
  3923. count:(NSUInteger)count {
  3924. self = [super init];
  3925. if (self) {
  3926. _dictionary = [[NSMutableDictionary alloc] init];
  3927. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  3928. if (count && rawValues && keys) {
  3929. for (NSUInteger i = 0; i < count; ++i) {
  3930. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  3931. }
  3932. }
  3933. }
  3934. return self;
  3935. }
  3936. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  3937. self = [self initWithValidationFunction:dictionary.validationFunc
  3938. rawValues:NULL
  3939. forKeys:NULL
  3940. count:0];
  3941. if (self) {
  3942. if (dictionary) {
  3943. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3944. }
  3945. }
  3946. return self;
  3947. }
  3948. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  3949. capacity:(__unused NSUInteger)numItems {
  3950. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  3951. }
  3952. - (void)dealloc {
  3953. NSAssert(!_autocreator,
  3954. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3955. [self class], _autocreator);
  3956. [_dictionary release];
  3957. [super dealloc];
  3958. }
  3959. - (instancetype)copyWithZone:(NSZone *)zone {
  3960. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  3961. }
  3962. - (BOOL)isEqual:(id)other {
  3963. if (self == other) {
  3964. return YES;
  3965. }
  3966. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  3967. return NO;
  3968. }
  3969. GPBInt32EnumDictionary *otherDictionary = other;
  3970. return [_dictionary isEqual:otherDictionary->_dictionary];
  3971. }
  3972. - (NSUInteger)hash {
  3973. return _dictionary.count;
  3974. }
  3975. - (NSString *)description {
  3976. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3977. }
  3978. - (NSUInteger)count {
  3979. return _dictionary.count;
  3980. }
  3981. - (void)enumerateKeysAndRawValuesUsingBlock:
  3982. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  3983. BOOL stop = NO;
  3984. NSDictionary *internal = _dictionary;
  3985. NSEnumerator *keys = [internal keyEnumerator];
  3986. NSNumber *aKey;
  3987. while ((aKey = [keys nextObject])) {
  3988. NSNumber *aValue = internal[aKey];
  3989. block([aKey intValue], [aValue intValue], &stop);
  3990. if (stop) {
  3991. break;
  3992. }
  3993. }
  3994. }
  3995. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3996. NSDictionary *internal = _dictionary;
  3997. NSUInteger count = internal.count;
  3998. if (count == 0) {
  3999. return 0;
  4000. }
  4001. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4002. GPBDataType keyDataType = field.mapKeyDataType;
  4003. size_t result = 0;
  4004. NSEnumerator *keys = [internal keyEnumerator];
  4005. NSNumber *aKey;
  4006. while ((aKey = [keys nextObject])) {
  4007. NSNumber *aValue = internal[aKey];
  4008. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4009. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4010. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4011. }
  4012. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4013. result += tagSize * count;
  4014. return result;
  4015. }
  4016. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4017. asField:(GPBFieldDescriptor *)field {
  4018. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4019. GPBDataType keyDataType = field.mapKeyDataType;
  4020. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4021. NSDictionary *internal = _dictionary;
  4022. NSEnumerator *keys = [internal keyEnumerator];
  4023. NSNumber *aKey;
  4024. while ((aKey = [keys nextObject])) {
  4025. NSNumber *aValue = internal[aKey];
  4026. [outputStream writeInt32NoTag:tag];
  4027. // Write the size of the message.
  4028. int32_t unwrappedKey = [aKey intValue];
  4029. int32_t unwrappedValue = [aValue intValue];
  4030. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4031. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4032. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4033. // Write the fields.
  4034. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4035. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4036. }
  4037. }
  4038. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4039. forKey:(GPBGenericValue *)key
  4040. keyDataType:(GPBDataType)keyDataType {
  4041. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4042. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4043. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4044. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4045. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4046. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4047. [outputStream flush];
  4048. [outputStream release];
  4049. return data;
  4050. }
  4051. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4052. forGPBGenericValueKey:(GPBGenericValue *)key {
  4053. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4054. }
  4055. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4056. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, __unused BOOL *stop) {
  4057. block([NSString stringWithFormat:@"%d", key], @(value));
  4058. }];
  4059. }
  4060. - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
  4061. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4062. if (wrapped && value) {
  4063. int32_t result = [wrapped intValue];
  4064. if (!_validationFunc(result)) {
  4065. result = kGPBUnrecognizedEnumeratorValue;
  4066. }
  4067. *value = result;
  4068. }
  4069. return (wrapped != NULL);
  4070. }
  4071. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
  4072. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4073. if (wrapped && rawValue) {
  4074. *rawValue = [wrapped intValue];
  4075. }
  4076. return (wrapped != NULL);
  4077. }
  4078. - (void)enumerateKeysAndEnumsUsingBlock:
  4079. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  4080. GPBEnumValidationFunc func = _validationFunc;
  4081. BOOL stop = NO;
  4082. NSEnumerator *keys = [_dictionary keyEnumerator];
  4083. NSNumber *aKey;
  4084. while ((aKey = [keys nextObject])) {
  4085. NSNumber *aValue = _dictionary[aKey];
  4086. int32_t unwrapped = [aValue intValue];
  4087. if (!func(unwrapped)) {
  4088. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4089. }
  4090. block([aKey intValue], unwrapped, &stop);
  4091. if (stop) {
  4092. break;
  4093. }
  4094. }
  4095. }
  4096. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4097. if (otherDictionary) {
  4098. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4099. if (_autocreator) {
  4100. GPBAutocreatedDictionaryModified(_autocreator, self);
  4101. }
  4102. }
  4103. }
  4104. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4105. [_dictionary setObject:@(value) forKey:@(key)];
  4106. if (_autocreator) {
  4107. GPBAutocreatedDictionaryModified(_autocreator, self);
  4108. }
  4109. }
  4110. - (void)removeEnumForKey:(int32_t)aKey {
  4111. [_dictionary removeObjectForKey:@(aKey)];
  4112. }
  4113. - (void)removeAll {
  4114. [_dictionary removeAllObjects];
  4115. }
  4116. - (void)setEnum:(int32_t)value forKey:(int32_t)key {
  4117. if (!_validationFunc(value)) {
  4118. [NSException raise:NSInvalidArgumentException
  4119. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4120. value];
  4121. }
  4122. [_dictionary setObject:@(value) forKey:@(key)];
  4123. if (_autocreator) {
  4124. GPBAutocreatedDictionaryModified(_autocreator, self);
  4125. }
  4126. }
  4127. @end
  4128. #pragma mark - Int32 -> Object
  4129. @implementation GPBInt32ObjectDictionary {
  4130. @package
  4131. NSMutableDictionary *_dictionary;
  4132. }
  4133. - (instancetype)init {
  4134. return [self initWithObjects:NULL forKeys:NULL count:0];
  4135. }
  4136. - (instancetype)initWithObjects:(const id [])objects
  4137. forKeys:(const int32_t [])keys
  4138. count:(NSUInteger)count {
  4139. self = [super init];
  4140. if (self) {
  4141. _dictionary = [[NSMutableDictionary alloc] init];
  4142. if (count && objects && keys) {
  4143. for (NSUInteger i = 0; i < count; ++i) {
  4144. if (!objects[i]) {
  4145. [NSException raise:NSInvalidArgumentException
  4146. format:@"Attempting to add nil object to a Dictionary"];
  4147. }
  4148. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4149. }
  4150. }
  4151. }
  4152. return self;
  4153. }
  4154. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4155. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4156. if (self) {
  4157. if (dictionary) {
  4158. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4159. }
  4160. }
  4161. return self;
  4162. }
  4163. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4164. return [self initWithObjects:NULL forKeys:NULL count:0];
  4165. }
  4166. - (void)dealloc {
  4167. NSAssert(!_autocreator,
  4168. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4169. [self class], _autocreator);
  4170. [_dictionary release];
  4171. [super dealloc];
  4172. }
  4173. - (instancetype)copyWithZone:(NSZone *)zone {
  4174. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4175. }
  4176. - (BOOL)isEqual:(id)other {
  4177. if (self == other) {
  4178. return YES;
  4179. }
  4180. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4181. return NO;
  4182. }
  4183. GPBInt32ObjectDictionary *otherDictionary = other;
  4184. return [_dictionary isEqual:otherDictionary->_dictionary];
  4185. }
  4186. - (NSUInteger)hash {
  4187. return _dictionary.count;
  4188. }
  4189. - (NSString *)description {
  4190. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4191. }
  4192. - (NSUInteger)count {
  4193. return _dictionary.count;
  4194. }
  4195. - (void)enumerateKeysAndObjectsUsingBlock:
  4196. (void (NS_NOESCAPE ^)(int32_t key, id object, BOOL *stop))block {
  4197. BOOL stop = NO;
  4198. NSDictionary *internal = _dictionary;
  4199. NSEnumerator *keys = [internal keyEnumerator];
  4200. NSNumber *aKey;
  4201. while ((aKey = [keys nextObject])) {
  4202. id aObject = internal[aKey];
  4203. block([aKey intValue], aObject, &stop);
  4204. if (stop) {
  4205. break;
  4206. }
  4207. }
  4208. }
  4209. - (BOOL)isInitialized {
  4210. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4211. if (!msg.initialized) {
  4212. return NO;
  4213. }
  4214. }
  4215. return YES;
  4216. }
  4217. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4218. GPBInt32ObjectDictionary *newDict =
  4219. [[GPBInt32ObjectDictionary alloc] init];
  4220. NSEnumerator *keys = [_dictionary keyEnumerator];
  4221. id aKey;
  4222. NSMutableDictionary *internalDict = newDict->_dictionary;
  4223. while ((aKey = [keys nextObject])) {
  4224. GPBMessage *msg = _dictionary[aKey];
  4225. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4226. [internalDict setObject:copiedMsg forKey:aKey];
  4227. [copiedMsg release];
  4228. }
  4229. return newDict;
  4230. }
  4231. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4232. NSDictionary *internal = _dictionary;
  4233. NSUInteger count = internal.count;
  4234. if (count == 0) {
  4235. return 0;
  4236. }
  4237. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4238. GPBDataType keyDataType = field.mapKeyDataType;
  4239. size_t result = 0;
  4240. NSEnumerator *keys = [internal keyEnumerator];
  4241. NSNumber *aKey;
  4242. while ((aKey = [keys nextObject])) {
  4243. id aObject = internal[aKey];
  4244. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4245. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4246. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4247. }
  4248. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4249. result += tagSize * count;
  4250. return result;
  4251. }
  4252. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4253. asField:(GPBFieldDescriptor *)field {
  4254. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4255. GPBDataType keyDataType = field.mapKeyDataType;
  4256. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4257. NSDictionary *internal = _dictionary;
  4258. NSEnumerator *keys = [internal keyEnumerator];
  4259. NSNumber *aKey;
  4260. while ((aKey = [keys nextObject])) {
  4261. id aObject = internal[aKey];
  4262. [outputStream writeInt32NoTag:tag];
  4263. // Write the size of the message.
  4264. int32_t unwrappedKey = [aKey intValue];
  4265. id unwrappedValue = aObject;
  4266. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4267. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4268. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4269. // Write the fields.
  4270. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4271. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4272. }
  4273. }
  4274. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4275. forGPBGenericValueKey:(GPBGenericValue *)key {
  4276. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4277. }
  4278. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4279. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, __unused BOOL *stop) {
  4280. block([NSString stringWithFormat:@"%d", key], object);
  4281. }];
  4282. }
  4283. - (id)objectForKey:(int32_t)key {
  4284. id result = [_dictionary objectForKey:@(key)];
  4285. return result;
  4286. }
  4287. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4288. if (otherDictionary) {
  4289. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4290. if (_autocreator) {
  4291. GPBAutocreatedDictionaryModified(_autocreator, self);
  4292. }
  4293. }
  4294. }
  4295. - (void)setObject:(id)object forKey:(int32_t)key {
  4296. if (!object) {
  4297. [NSException raise:NSInvalidArgumentException
  4298. format:@"Attempting to add nil object to a Dictionary"];
  4299. }
  4300. [_dictionary setObject:object forKey:@(key)];
  4301. if (_autocreator) {
  4302. GPBAutocreatedDictionaryModified(_autocreator, self);
  4303. }
  4304. }
  4305. - (void)removeObjectForKey:(int32_t)aKey {
  4306. [_dictionary removeObjectForKey:@(aKey)];
  4307. }
  4308. - (void)removeAll {
  4309. [_dictionary removeAllObjects];
  4310. }
  4311. @end
  4312. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4313. // This block of code is generated, do not edit it directly.
  4314. #pragma mark - UInt64 -> UInt32
  4315. @implementation GPBUInt64UInt32Dictionary {
  4316. @package
  4317. NSMutableDictionary *_dictionary;
  4318. }
  4319. - (instancetype)init {
  4320. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4321. }
  4322. - (instancetype)initWithUInt32s:(const uint32_t [])values
  4323. forKeys:(const uint64_t [])keys
  4324. count:(NSUInteger)count {
  4325. self = [super init];
  4326. if (self) {
  4327. _dictionary = [[NSMutableDictionary alloc] init];
  4328. if (count && values && keys) {
  4329. for (NSUInteger i = 0; i < count; ++i) {
  4330. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4331. }
  4332. }
  4333. }
  4334. return self;
  4335. }
  4336. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4337. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  4338. if (self) {
  4339. if (dictionary) {
  4340. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4341. }
  4342. }
  4343. return self;
  4344. }
  4345. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4346. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4347. }
  4348. - (void)dealloc {
  4349. NSAssert(!_autocreator,
  4350. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4351. [self class], _autocreator);
  4352. [_dictionary release];
  4353. [super dealloc];
  4354. }
  4355. - (instancetype)copyWithZone:(NSZone *)zone {
  4356. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4357. }
  4358. - (BOOL)isEqual:(id)other {
  4359. if (self == other) {
  4360. return YES;
  4361. }
  4362. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4363. return NO;
  4364. }
  4365. GPBUInt64UInt32Dictionary *otherDictionary = other;
  4366. return [_dictionary isEqual:otherDictionary->_dictionary];
  4367. }
  4368. - (NSUInteger)hash {
  4369. return _dictionary.count;
  4370. }
  4371. - (NSString *)description {
  4372. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4373. }
  4374. - (NSUInteger)count {
  4375. return _dictionary.count;
  4376. }
  4377. - (void)enumerateKeysAndUInt32sUsingBlock:
  4378. (void (NS_NOESCAPE ^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4379. BOOL stop = NO;
  4380. NSDictionary *internal = _dictionary;
  4381. NSEnumerator *keys = [internal keyEnumerator];
  4382. NSNumber *aKey;
  4383. while ((aKey = [keys nextObject])) {
  4384. NSNumber *aValue = internal[aKey];
  4385. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], &stop);
  4386. if (stop) {
  4387. break;
  4388. }
  4389. }
  4390. }
  4391. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4392. NSDictionary *internal = _dictionary;
  4393. NSUInteger count = internal.count;
  4394. if (count == 0) {
  4395. return 0;
  4396. }
  4397. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4398. GPBDataType keyDataType = field.mapKeyDataType;
  4399. size_t result = 0;
  4400. NSEnumerator *keys = [internal keyEnumerator];
  4401. NSNumber *aKey;
  4402. while ((aKey = [keys nextObject])) {
  4403. NSNumber *aValue = internal[aKey];
  4404. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4405. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4406. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4407. }
  4408. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4409. result += tagSize * count;
  4410. return result;
  4411. }
  4412. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4413. asField:(GPBFieldDescriptor *)field {
  4414. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4415. GPBDataType keyDataType = field.mapKeyDataType;
  4416. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4417. NSDictionary *internal = _dictionary;
  4418. NSEnumerator *keys = [internal keyEnumerator];
  4419. NSNumber *aKey;
  4420. while ((aKey = [keys nextObject])) {
  4421. NSNumber *aValue = internal[aKey];
  4422. [outputStream writeInt32NoTag:tag];
  4423. // Write the size of the message.
  4424. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4425. uint32_t unwrappedValue = [aValue unsignedIntValue];
  4426. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4427. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4428. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4429. // Write the fields.
  4430. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4431. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4432. }
  4433. }
  4434. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4435. forGPBGenericValueKey:(GPBGenericValue *)key {
  4436. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4437. }
  4438. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4439. [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, __unused BOOL *stop) {
  4440. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4441. }];
  4442. }
  4443. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
  4444. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4445. if (wrapped && value) {
  4446. *value = [wrapped unsignedIntValue];
  4447. }
  4448. return (wrapped != NULL);
  4449. }
  4450. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4451. if (otherDictionary) {
  4452. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4453. if (_autocreator) {
  4454. GPBAutocreatedDictionaryModified(_autocreator, self);
  4455. }
  4456. }
  4457. }
  4458. - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
  4459. [_dictionary setObject:@(value) forKey:@(key)];
  4460. if (_autocreator) {
  4461. GPBAutocreatedDictionaryModified(_autocreator, self);
  4462. }
  4463. }
  4464. - (void)removeUInt32ForKey:(uint64_t)aKey {
  4465. [_dictionary removeObjectForKey:@(aKey)];
  4466. }
  4467. - (void)removeAll {
  4468. [_dictionary removeAllObjects];
  4469. }
  4470. @end
  4471. #pragma mark - UInt64 -> Int32
  4472. @implementation GPBUInt64Int32Dictionary {
  4473. @package
  4474. NSMutableDictionary *_dictionary;
  4475. }
  4476. - (instancetype)init {
  4477. return [self initWithInt32s:NULL forKeys:NULL count:0];
  4478. }
  4479. - (instancetype)initWithInt32s:(const int32_t [])values
  4480. forKeys:(const uint64_t [])keys
  4481. count:(NSUInteger)count {
  4482. self = [super init];
  4483. if (self) {
  4484. _dictionary = [[NSMutableDictionary alloc] init];
  4485. if (count && values && keys) {
  4486. for (NSUInteger i = 0; i < count; ++i) {
  4487. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4488. }
  4489. }
  4490. }
  4491. return self;
  4492. }
  4493. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4494. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  4495. if (self) {
  4496. if (dictionary) {
  4497. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4498. }
  4499. }
  4500. return self;
  4501. }
  4502. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4503. return [self initWithInt32s:NULL forKeys:NULL count:0];
  4504. }
  4505. - (void)dealloc {
  4506. NSAssert(!_autocreator,
  4507. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4508. [self class], _autocreator);
  4509. [_dictionary release];
  4510. [super dealloc];
  4511. }
  4512. - (instancetype)copyWithZone:(NSZone *)zone {
  4513. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  4514. }
  4515. - (BOOL)isEqual:(id)other {
  4516. if (self == other) {
  4517. return YES;
  4518. }
  4519. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  4520. return NO;
  4521. }
  4522. GPBUInt64Int32Dictionary *otherDictionary = other;
  4523. return [_dictionary isEqual:otherDictionary->_dictionary];
  4524. }
  4525. - (NSUInteger)hash {
  4526. return _dictionary.count;
  4527. }
  4528. - (NSString *)description {
  4529. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4530. }
  4531. - (NSUInteger)count {
  4532. return _dictionary.count;
  4533. }
  4534. - (void)enumerateKeysAndInt32sUsingBlock:
  4535. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  4536. BOOL stop = NO;
  4537. NSDictionary *internal = _dictionary;
  4538. NSEnumerator *keys = [internal keyEnumerator];
  4539. NSNumber *aKey;
  4540. while ((aKey = [keys nextObject])) {
  4541. NSNumber *aValue = internal[aKey];
  4542. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  4543. if (stop) {
  4544. break;
  4545. }
  4546. }
  4547. }
  4548. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4549. NSDictionary *internal = _dictionary;
  4550. NSUInteger count = internal.count;
  4551. if (count == 0) {
  4552. return 0;
  4553. }
  4554. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4555. GPBDataType keyDataType = field.mapKeyDataType;
  4556. size_t result = 0;
  4557. NSEnumerator *keys = [internal keyEnumerator];
  4558. NSNumber *aKey;
  4559. while ((aKey = [keys nextObject])) {
  4560. NSNumber *aValue = internal[aKey];
  4561. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4562. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4563. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4564. }
  4565. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4566. result += tagSize * count;
  4567. return result;
  4568. }
  4569. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4570. asField:(GPBFieldDescriptor *)field {
  4571. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4572. GPBDataType keyDataType = field.mapKeyDataType;
  4573. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4574. NSDictionary *internal = _dictionary;
  4575. NSEnumerator *keys = [internal keyEnumerator];
  4576. NSNumber *aKey;
  4577. while ((aKey = [keys nextObject])) {
  4578. NSNumber *aValue = internal[aKey];
  4579. [outputStream writeInt32NoTag:tag];
  4580. // Write the size of the message.
  4581. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4582. int32_t unwrappedValue = [aValue intValue];
  4583. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4584. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4585. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4586. // Write the fields.
  4587. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4588. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4589. }
  4590. }
  4591. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4592. forGPBGenericValueKey:(GPBGenericValue *)key {
  4593. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  4594. }
  4595. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4596. [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, __unused BOOL *stop) {
  4597. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  4598. }];
  4599. }
  4600. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
  4601. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4602. if (wrapped && value) {
  4603. *value = [wrapped intValue];
  4604. }
  4605. return (wrapped != NULL);
  4606. }
  4607. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  4608. if (otherDictionary) {
  4609. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4610. if (_autocreator) {
  4611. GPBAutocreatedDictionaryModified(_autocreator, self);
  4612. }
  4613. }
  4614. }
  4615. - (void)setInt32:(int32_t)value forKey:(uint64_t)key {
  4616. [_dictionary setObject:@(value) forKey:@(key)];
  4617. if (_autocreator) {
  4618. GPBAutocreatedDictionaryModified(_autocreator, self);
  4619. }
  4620. }
  4621. - (void)removeInt32ForKey:(uint64_t)aKey {
  4622. [_dictionary removeObjectForKey:@(aKey)];
  4623. }
  4624. - (void)removeAll {
  4625. [_dictionary removeAllObjects];
  4626. }
  4627. @end
  4628. #pragma mark - UInt64 -> UInt64
  4629. @implementation GPBUInt64UInt64Dictionary {
  4630. @package
  4631. NSMutableDictionary *_dictionary;
  4632. }
  4633. - (instancetype)init {
  4634. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  4635. }
  4636. - (instancetype)initWithUInt64s:(const uint64_t [])values
  4637. forKeys:(const uint64_t [])keys
  4638. count:(NSUInteger)count {
  4639. self = [super init];
  4640. if (self) {
  4641. _dictionary = [[NSMutableDictionary alloc] init];
  4642. if (count && values && keys) {
  4643. for (NSUInteger i = 0; i < count; ++i) {
  4644. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4645. }
  4646. }
  4647. }
  4648. return self;
  4649. }
  4650. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  4651. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  4652. if (self) {
  4653. if (dictionary) {
  4654. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4655. }
  4656. }
  4657. return self;
  4658. }
  4659. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4660. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  4661. }
  4662. - (void)dealloc {
  4663. NSAssert(!_autocreator,
  4664. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4665. [self class], _autocreator);
  4666. [_dictionary release];
  4667. [super dealloc];
  4668. }
  4669. - (instancetype)copyWithZone:(NSZone *)zone {
  4670. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  4671. }
  4672. - (BOOL)isEqual:(id)other {
  4673. if (self == other) {
  4674. return YES;
  4675. }
  4676. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  4677. return NO;
  4678. }
  4679. GPBUInt64UInt64Dictionary *otherDictionary = other;
  4680. return [_dictionary isEqual:otherDictionary->_dictionary];
  4681. }
  4682. - (NSUInteger)hash {
  4683. return _dictionary.count;
  4684. }
  4685. - (NSString *)description {
  4686. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4687. }
  4688. - (NSUInteger)count {
  4689. return _dictionary.count;
  4690. }
  4691. - (void)enumerateKeysAndUInt64sUsingBlock:
  4692. (void (NS_NOESCAPE ^)(uint64_t key, uint64_t value, BOOL *stop))block {
  4693. BOOL stop = NO;
  4694. NSDictionary *internal = _dictionary;
  4695. NSEnumerator *keys = [internal keyEnumerator];
  4696. NSNumber *aKey;
  4697. while ((aKey = [keys nextObject])) {
  4698. NSNumber *aValue = internal[aKey];
  4699. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], &stop);
  4700. if (stop) {
  4701. break;
  4702. }
  4703. }
  4704. }
  4705. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4706. NSDictionary *internal = _dictionary;
  4707. NSUInteger count = internal.count;
  4708. if (count == 0) {
  4709. return 0;
  4710. }
  4711. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4712. GPBDataType keyDataType = field.mapKeyDataType;
  4713. size_t result = 0;
  4714. NSEnumerator *keys = [internal keyEnumerator];
  4715. NSNumber *aKey;
  4716. while ((aKey = [keys nextObject])) {
  4717. NSNumber *aValue = internal[aKey];
  4718. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4719. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  4720. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4721. }
  4722. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4723. result += tagSize * count;
  4724. return result;
  4725. }
  4726. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4727. asField:(GPBFieldDescriptor *)field {
  4728. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4729. GPBDataType keyDataType = field.mapKeyDataType;
  4730. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4731. NSDictionary *internal = _dictionary;
  4732. NSEnumerator *keys = [internal keyEnumerator];
  4733. NSNumber *aKey;
  4734. while ((aKey = [keys nextObject])) {
  4735. NSNumber *aValue = internal[aKey];
  4736. [outputStream writeInt32NoTag:tag];
  4737. // Write the size of the message.
  4738. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4739. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  4740. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4741. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4742. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4743. // Write the fields.
  4744. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4745. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4746. }
  4747. }
  4748. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4749. forGPBGenericValueKey:(GPBGenericValue *)key {
  4750. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  4751. }
  4752. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4753. [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, __unused BOOL *stop) {
  4754. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  4755. }];
  4756. }
  4757. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
  4758. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4759. if (wrapped && value) {
  4760. *value = [wrapped unsignedLongLongValue];
  4761. }
  4762. return (wrapped != NULL);
  4763. }
  4764. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  4765. if (otherDictionary) {
  4766. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4767. if (_autocreator) {
  4768. GPBAutocreatedDictionaryModified(_autocreator, self);
  4769. }
  4770. }
  4771. }
  4772. - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
  4773. [_dictionary setObject:@(value) forKey:@(key)];
  4774. if (_autocreator) {
  4775. GPBAutocreatedDictionaryModified(_autocreator, self);
  4776. }
  4777. }
  4778. - (void)removeUInt64ForKey:(uint64_t)aKey {
  4779. [_dictionary removeObjectForKey:@(aKey)];
  4780. }
  4781. - (void)removeAll {
  4782. [_dictionary removeAllObjects];
  4783. }
  4784. @end
  4785. #pragma mark - UInt64 -> Int64
  4786. @implementation GPBUInt64Int64Dictionary {
  4787. @package
  4788. NSMutableDictionary *_dictionary;
  4789. }
  4790. - (instancetype)init {
  4791. return [self initWithInt64s:NULL forKeys:NULL count:0];
  4792. }
  4793. - (instancetype)initWithInt64s:(const int64_t [])values
  4794. forKeys:(const uint64_t [])keys
  4795. count:(NSUInteger)count {
  4796. self = [super init];
  4797. if (self) {
  4798. _dictionary = [[NSMutableDictionary alloc] init];
  4799. if (count && values && keys) {
  4800. for (NSUInteger i = 0; i < count; ++i) {
  4801. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4802. }
  4803. }
  4804. }
  4805. return self;
  4806. }
  4807. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  4808. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  4809. if (self) {
  4810. if (dictionary) {
  4811. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4812. }
  4813. }
  4814. return self;
  4815. }
  4816. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4817. return [self initWithInt64s:NULL forKeys:NULL count:0];
  4818. }
  4819. - (void)dealloc {
  4820. NSAssert(!_autocreator,
  4821. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4822. [self class], _autocreator);
  4823. [_dictionary release];
  4824. [super dealloc];
  4825. }
  4826. - (instancetype)copyWithZone:(NSZone *)zone {
  4827. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  4828. }
  4829. - (BOOL)isEqual:(id)other {
  4830. if (self == other) {
  4831. return YES;
  4832. }
  4833. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  4834. return NO;
  4835. }
  4836. GPBUInt64Int64Dictionary *otherDictionary = other;
  4837. return [_dictionary isEqual:otherDictionary->_dictionary];
  4838. }
  4839. - (NSUInteger)hash {
  4840. return _dictionary.count;
  4841. }
  4842. - (NSString *)description {
  4843. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4844. }
  4845. - (NSUInteger)count {
  4846. return _dictionary.count;
  4847. }
  4848. - (void)enumerateKeysAndInt64sUsingBlock:
  4849. (void (NS_NOESCAPE ^)(uint64_t key, int64_t value, BOOL *stop))block {
  4850. BOOL stop = NO;
  4851. NSDictionary *internal = _dictionary;
  4852. NSEnumerator *keys = [internal keyEnumerator];
  4853. NSNumber *aKey;
  4854. while ((aKey = [keys nextObject])) {
  4855. NSNumber *aValue = internal[aKey];
  4856. block([aKey unsignedLongLongValue], [aValue longLongValue], &stop);
  4857. if (stop) {
  4858. break;
  4859. }
  4860. }
  4861. }
  4862. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4863. NSDictionary *internal = _dictionary;
  4864. NSUInteger count = internal.count;
  4865. if (count == 0) {
  4866. return 0;
  4867. }
  4868. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4869. GPBDataType keyDataType = field.mapKeyDataType;
  4870. size_t result = 0;
  4871. NSEnumerator *keys = [internal keyEnumerator];
  4872. NSNumber *aKey;
  4873. while ((aKey = [keys nextObject])) {
  4874. NSNumber *aValue = internal[aKey];
  4875. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4876. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  4877. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4878. }
  4879. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4880. result += tagSize * count;
  4881. return result;
  4882. }
  4883. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4884. asField:(GPBFieldDescriptor *)field {
  4885. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4886. GPBDataType keyDataType = field.mapKeyDataType;
  4887. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4888. NSDictionary *internal = _dictionary;
  4889. NSEnumerator *keys = [internal keyEnumerator];
  4890. NSNumber *aKey;
  4891. while ((aKey = [keys nextObject])) {
  4892. NSNumber *aValue = internal[aKey];
  4893. [outputStream writeInt32NoTag:tag];
  4894. // Write the size of the message.
  4895. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4896. int64_t unwrappedValue = [aValue longLongValue];
  4897. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4898. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4899. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4900. // Write the fields.
  4901. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4902. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4903. }
  4904. }
  4905. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4906. forGPBGenericValueKey:(GPBGenericValue *)key {
  4907. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  4908. }
  4909. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4910. [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, __unused BOOL *stop) {
  4911. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  4912. }];
  4913. }
  4914. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
  4915. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4916. if (wrapped && value) {
  4917. *value = [wrapped longLongValue];
  4918. }
  4919. return (wrapped != NULL);
  4920. }
  4921. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  4922. if (otherDictionary) {
  4923. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4924. if (_autocreator) {
  4925. GPBAutocreatedDictionaryModified(_autocreator, self);
  4926. }
  4927. }
  4928. }
  4929. - (void)setInt64:(int64_t)value forKey:(uint64_t)key {
  4930. [_dictionary setObject:@(value) forKey:@(key)];
  4931. if (_autocreator) {
  4932. GPBAutocreatedDictionaryModified(_autocreator, self);
  4933. }
  4934. }
  4935. - (void)removeInt64ForKey:(uint64_t)aKey {
  4936. [_dictionary removeObjectForKey:@(aKey)];
  4937. }
  4938. - (void)removeAll {
  4939. [_dictionary removeAllObjects];
  4940. }
  4941. @end
  4942. #pragma mark - UInt64 -> Bool
  4943. @implementation GPBUInt64BoolDictionary {
  4944. @package
  4945. NSMutableDictionary *_dictionary;
  4946. }
  4947. - (instancetype)init {
  4948. return [self initWithBools:NULL forKeys:NULL count:0];
  4949. }
  4950. - (instancetype)initWithBools:(const BOOL [])values
  4951. forKeys:(const uint64_t [])keys
  4952. count:(NSUInteger)count {
  4953. self = [super init];
  4954. if (self) {
  4955. _dictionary = [[NSMutableDictionary alloc] init];
  4956. if (count && values && keys) {
  4957. for (NSUInteger i = 0; i < count; ++i) {
  4958. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4959. }
  4960. }
  4961. }
  4962. return self;
  4963. }
  4964. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  4965. self = [self initWithBools:NULL forKeys:NULL count:0];
  4966. if (self) {
  4967. if (dictionary) {
  4968. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4969. }
  4970. }
  4971. return self;
  4972. }
  4973. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4974. return [self initWithBools:NULL forKeys:NULL count:0];
  4975. }
  4976. - (void)dealloc {
  4977. NSAssert(!_autocreator,
  4978. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4979. [self class], _autocreator);
  4980. [_dictionary release];
  4981. [super dealloc];
  4982. }
  4983. - (instancetype)copyWithZone:(NSZone *)zone {
  4984. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  4985. }
  4986. - (BOOL)isEqual:(id)other {
  4987. if (self == other) {
  4988. return YES;
  4989. }
  4990. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  4991. return NO;
  4992. }
  4993. GPBUInt64BoolDictionary *otherDictionary = other;
  4994. return [_dictionary isEqual:otherDictionary->_dictionary];
  4995. }
  4996. - (NSUInteger)hash {
  4997. return _dictionary.count;
  4998. }
  4999. - (NSString *)description {
  5000. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5001. }
  5002. - (NSUInteger)count {
  5003. return _dictionary.count;
  5004. }
  5005. - (void)enumerateKeysAndBoolsUsingBlock:
  5006. (void (NS_NOESCAPE ^)(uint64_t key, BOOL value, BOOL *stop))block {
  5007. BOOL stop = NO;
  5008. NSDictionary *internal = _dictionary;
  5009. NSEnumerator *keys = [internal keyEnumerator];
  5010. NSNumber *aKey;
  5011. while ((aKey = [keys nextObject])) {
  5012. NSNumber *aValue = internal[aKey];
  5013. block([aKey unsignedLongLongValue], [aValue boolValue], &stop);
  5014. if (stop) {
  5015. break;
  5016. }
  5017. }
  5018. }
  5019. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5020. NSDictionary *internal = _dictionary;
  5021. NSUInteger count = internal.count;
  5022. if (count == 0) {
  5023. return 0;
  5024. }
  5025. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5026. GPBDataType keyDataType = field.mapKeyDataType;
  5027. size_t result = 0;
  5028. NSEnumerator *keys = [internal keyEnumerator];
  5029. NSNumber *aKey;
  5030. while ((aKey = [keys nextObject])) {
  5031. NSNumber *aValue = internal[aKey];
  5032. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5033. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5034. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5035. }
  5036. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5037. result += tagSize * count;
  5038. return result;
  5039. }
  5040. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5041. asField:(GPBFieldDescriptor *)field {
  5042. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5043. GPBDataType keyDataType = field.mapKeyDataType;
  5044. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5045. NSDictionary *internal = _dictionary;
  5046. NSEnumerator *keys = [internal keyEnumerator];
  5047. NSNumber *aKey;
  5048. while ((aKey = [keys nextObject])) {
  5049. NSNumber *aValue = internal[aKey];
  5050. [outputStream writeInt32NoTag:tag];
  5051. // Write the size of the message.
  5052. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5053. BOOL unwrappedValue = [aValue boolValue];
  5054. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5055. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5056. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5057. // Write the fields.
  5058. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5059. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5060. }
  5061. }
  5062. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5063. forGPBGenericValueKey:(GPBGenericValue *)key {
  5064. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5065. }
  5066. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5067. [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, __unused BOOL *stop) {
  5068. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5069. }];
  5070. }
  5071. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
  5072. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5073. if (wrapped && value) {
  5074. *value = [wrapped boolValue];
  5075. }
  5076. return (wrapped != NULL);
  5077. }
  5078. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5079. if (otherDictionary) {
  5080. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5081. if (_autocreator) {
  5082. GPBAutocreatedDictionaryModified(_autocreator, self);
  5083. }
  5084. }
  5085. }
  5086. - (void)setBool:(BOOL)value forKey:(uint64_t)key {
  5087. [_dictionary setObject:@(value) forKey:@(key)];
  5088. if (_autocreator) {
  5089. GPBAutocreatedDictionaryModified(_autocreator, self);
  5090. }
  5091. }
  5092. - (void)removeBoolForKey:(uint64_t)aKey {
  5093. [_dictionary removeObjectForKey:@(aKey)];
  5094. }
  5095. - (void)removeAll {
  5096. [_dictionary removeAllObjects];
  5097. }
  5098. @end
  5099. #pragma mark - UInt64 -> Float
  5100. @implementation GPBUInt64FloatDictionary {
  5101. @package
  5102. NSMutableDictionary *_dictionary;
  5103. }
  5104. - (instancetype)init {
  5105. return [self initWithFloats:NULL forKeys:NULL count:0];
  5106. }
  5107. - (instancetype)initWithFloats:(const float [])values
  5108. forKeys:(const uint64_t [])keys
  5109. count:(NSUInteger)count {
  5110. self = [super init];
  5111. if (self) {
  5112. _dictionary = [[NSMutableDictionary alloc] init];
  5113. if (count && values && keys) {
  5114. for (NSUInteger i = 0; i < count; ++i) {
  5115. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5116. }
  5117. }
  5118. }
  5119. return self;
  5120. }
  5121. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5122. self = [self initWithFloats:NULL forKeys:NULL count:0];
  5123. if (self) {
  5124. if (dictionary) {
  5125. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5126. }
  5127. }
  5128. return self;
  5129. }
  5130. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5131. return [self initWithFloats:NULL forKeys:NULL count:0];
  5132. }
  5133. - (void)dealloc {
  5134. NSAssert(!_autocreator,
  5135. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5136. [self class], _autocreator);
  5137. [_dictionary release];
  5138. [super dealloc];
  5139. }
  5140. - (instancetype)copyWithZone:(NSZone *)zone {
  5141. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5142. }
  5143. - (BOOL)isEqual:(id)other {
  5144. if (self == other) {
  5145. return YES;
  5146. }
  5147. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5148. return NO;
  5149. }
  5150. GPBUInt64FloatDictionary *otherDictionary = other;
  5151. return [_dictionary isEqual:otherDictionary->_dictionary];
  5152. }
  5153. - (NSUInteger)hash {
  5154. return _dictionary.count;
  5155. }
  5156. - (NSString *)description {
  5157. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5158. }
  5159. - (NSUInteger)count {
  5160. return _dictionary.count;
  5161. }
  5162. - (void)enumerateKeysAndFloatsUsingBlock:
  5163. (void (NS_NOESCAPE ^)(uint64_t key, float value, BOOL *stop))block {
  5164. BOOL stop = NO;
  5165. NSDictionary *internal = _dictionary;
  5166. NSEnumerator *keys = [internal keyEnumerator];
  5167. NSNumber *aKey;
  5168. while ((aKey = [keys nextObject])) {
  5169. NSNumber *aValue = internal[aKey];
  5170. block([aKey unsignedLongLongValue], [aValue floatValue], &stop);
  5171. if (stop) {
  5172. break;
  5173. }
  5174. }
  5175. }
  5176. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5177. NSDictionary *internal = _dictionary;
  5178. NSUInteger count = internal.count;
  5179. if (count == 0) {
  5180. return 0;
  5181. }
  5182. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5183. GPBDataType keyDataType = field.mapKeyDataType;
  5184. size_t result = 0;
  5185. NSEnumerator *keys = [internal keyEnumerator];
  5186. NSNumber *aKey;
  5187. while ((aKey = [keys nextObject])) {
  5188. NSNumber *aValue = internal[aKey];
  5189. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5190. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5191. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5192. }
  5193. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5194. result += tagSize * count;
  5195. return result;
  5196. }
  5197. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5198. asField:(GPBFieldDescriptor *)field {
  5199. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5200. GPBDataType keyDataType = field.mapKeyDataType;
  5201. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5202. NSDictionary *internal = _dictionary;
  5203. NSEnumerator *keys = [internal keyEnumerator];
  5204. NSNumber *aKey;
  5205. while ((aKey = [keys nextObject])) {
  5206. NSNumber *aValue = internal[aKey];
  5207. [outputStream writeInt32NoTag:tag];
  5208. // Write the size of the message.
  5209. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5210. float unwrappedValue = [aValue floatValue];
  5211. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5212. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5213. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5214. // Write the fields.
  5215. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5216. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5217. }
  5218. }
  5219. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5220. forGPBGenericValueKey:(GPBGenericValue *)key {
  5221. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5222. }
  5223. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5224. [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, __unused BOOL *stop) {
  5225. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5226. }];
  5227. }
  5228. - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
  5229. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5230. if (wrapped && value) {
  5231. *value = [wrapped floatValue];
  5232. }
  5233. return (wrapped != NULL);
  5234. }
  5235. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5236. if (otherDictionary) {
  5237. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5238. if (_autocreator) {
  5239. GPBAutocreatedDictionaryModified(_autocreator, self);
  5240. }
  5241. }
  5242. }
  5243. - (void)setFloat:(float)value forKey:(uint64_t)key {
  5244. [_dictionary setObject:@(value) forKey:@(key)];
  5245. if (_autocreator) {
  5246. GPBAutocreatedDictionaryModified(_autocreator, self);
  5247. }
  5248. }
  5249. - (void)removeFloatForKey:(uint64_t)aKey {
  5250. [_dictionary removeObjectForKey:@(aKey)];
  5251. }
  5252. - (void)removeAll {
  5253. [_dictionary removeAllObjects];
  5254. }
  5255. @end
  5256. #pragma mark - UInt64 -> Double
  5257. @implementation GPBUInt64DoubleDictionary {
  5258. @package
  5259. NSMutableDictionary *_dictionary;
  5260. }
  5261. - (instancetype)init {
  5262. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5263. }
  5264. - (instancetype)initWithDoubles:(const double [])values
  5265. forKeys:(const uint64_t [])keys
  5266. count:(NSUInteger)count {
  5267. self = [super init];
  5268. if (self) {
  5269. _dictionary = [[NSMutableDictionary alloc] init];
  5270. if (count && values && keys) {
  5271. for (NSUInteger i = 0; i < count; ++i) {
  5272. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5273. }
  5274. }
  5275. }
  5276. return self;
  5277. }
  5278. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5279. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  5280. if (self) {
  5281. if (dictionary) {
  5282. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5283. }
  5284. }
  5285. return self;
  5286. }
  5287. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5288. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5289. }
  5290. - (void)dealloc {
  5291. NSAssert(!_autocreator,
  5292. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5293. [self class], _autocreator);
  5294. [_dictionary release];
  5295. [super dealloc];
  5296. }
  5297. - (instancetype)copyWithZone:(NSZone *)zone {
  5298. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5299. }
  5300. - (BOOL)isEqual:(id)other {
  5301. if (self == other) {
  5302. return YES;
  5303. }
  5304. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5305. return NO;
  5306. }
  5307. GPBUInt64DoubleDictionary *otherDictionary = other;
  5308. return [_dictionary isEqual:otherDictionary->_dictionary];
  5309. }
  5310. - (NSUInteger)hash {
  5311. return _dictionary.count;
  5312. }
  5313. - (NSString *)description {
  5314. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5315. }
  5316. - (NSUInteger)count {
  5317. return _dictionary.count;
  5318. }
  5319. - (void)enumerateKeysAndDoublesUsingBlock:
  5320. (void (NS_NOESCAPE ^)(uint64_t key, double value, BOOL *stop))block {
  5321. BOOL stop = NO;
  5322. NSDictionary *internal = _dictionary;
  5323. NSEnumerator *keys = [internal keyEnumerator];
  5324. NSNumber *aKey;
  5325. while ((aKey = [keys nextObject])) {
  5326. NSNumber *aValue = internal[aKey];
  5327. block([aKey unsignedLongLongValue], [aValue doubleValue], &stop);
  5328. if (stop) {
  5329. break;
  5330. }
  5331. }
  5332. }
  5333. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5334. NSDictionary *internal = _dictionary;
  5335. NSUInteger count = internal.count;
  5336. if (count == 0) {
  5337. return 0;
  5338. }
  5339. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5340. GPBDataType keyDataType = field.mapKeyDataType;
  5341. size_t result = 0;
  5342. NSEnumerator *keys = [internal keyEnumerator];
  5343. NSNumber *aKey;
  5344. while ((aKey = [keys nextObject])) {
  5345. NSNumber *aValue = internal[aKey];
  5346. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5347. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5348. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5349. }
  5350. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5351. result += tagSize * count;
  5352. return result;
  5353. }
  5354. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5355. asField:(GPBFieldDescriptor *)field {
  5356. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5357. GPBDataType keyDataType = field.mapKeyDataType;
  5358. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5359. NSDictionary *internal = _dictionary;
  5360. NSEnumerator *keys = [internal keyEnumerator];
  5361. NSNumber *aKey;
  5362. while ((aKey = [keys nextObject])) {
  5363. NSNumber *aValue = internal[aKey];
  5364. [outputStream writeInt32NoTag:tag];
  5365. // Write the size of the message.
  5366. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5367. double unwrappedValue = [aValue doubleValue];
  5368. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5369. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5370. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5371. // Write the fields.
  5372. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5373. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5374. }
  5375. }
  5376. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5377. forGPBGenericValueKey:(GPBGenericValue *)key {
  5378. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5379. }
  5380. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5381. [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, __unused BOOL *stop) {
  5382. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5383. }];
  5384. }
  5385. - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
  5386. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5387. if (wrapped && value) {
  5388. *value = [wrapped doubleValue];
  5389. }
  5390. return (wrapped != NULL);
  5391. }
  5392. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5393. if (otherDictionary) {
  5394. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5395. if (_autocreator) {
  5396. GPBAutocreatedDictionaryModified(_autocreator, self);
  5397. }
  5398. }
  5399. }
  5400. - (void)setDouble:(double)value forKey:(uint64_t)key {
  5401. [_dictionary setObject:@(value) forKey:@(key)];
  5402. if (_autocreator) {
  5403. GPBAutocreatedDictionaryModified(_autocreator, self);
  5404. }
  5405. }
  5406. - (void)removeDoubleForKey:(uint64_t)aKey {
  5407. [_dictionary removeObjectForKey:@(aKey)];
  5408. }
  5409. - (void)removeAll {
  5410. [_dictionary removeAllObjects];
  5411. }
  5412. @end
  5413. #pragma mark - UInt64 -> Enum
  5414. @implementation GPBUInt64EnumDictionary {
  5415. @package
  5416. NSMutableDictionary *_dictionary;
  5417. GPBEnumValidationFunc _validationFunc;
  5418. }
  5419. @synthesize validationFunc = _validationFunc;
  5420. - (instancetype)init {
  5421. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  5422. }
  5423. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  5424. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5425. }
  5426. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5427. rawValues:(const int32_t [])rawValues
  5428. forKeys:(const uint64_t [])keys
  5429. count:(NSUInteger)count {
  5430. self = [super init];
  5431. if (self) {
  5432. _dictionary = [[NSMutableDictionary alloc] init];
  5433. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  5434. if (count && rawValues && keys) {
  5435. for (NSUInteger i = 0; i < count; ++i) {
  5436. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  5437. }
  5438. }
  5439. }
  5440. return self;
  5441. }
  5442. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  5443. self = [self initWithValidationFunction:dictionary.validationFunc
  5444. rawValues:NULL
  5445. forKeys:NULL
  5446. count:0];
  5447. if (self) {
  5448. if (dictionary) {
  5449. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5450. }
  5451. }
  5452. return self;
  5453. }
  5454. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5455. capacity:(__unused NSUInteger)numItems {
  5456. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5457. }
  5458. - (void)dealloc {
  5459. NSAssert(!_autocreator,
  5460. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5461. [self class], _autocreator);
  5462. [_dictionary release];
  5463. [super dealloc];
  5464. }
  5465. - (instancetype)copyWithZone:(NSZone *)zone {
  5466. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  5467. }
  5468. - (BOOL)isEqual:(id)other {
  5469. if (self == other) {
  5470. return YES;
  5471. }
  5472. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  5473. return NO;
  5474. }
  5475. GPBUInt64EnumDictionary *otherDictionary = other;
  5476. return [_dictionary isEqual:otherDictionary->_dictionary];
  5477. }
  5478. - (NSUInteger)hash {
  5479. return _dictionary.count;
  5480. }
  5481. - (NSString *)description {
  5482. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5483. }
  5484. - (NSUInteger)count {
  5485. return _dictionary.count;
  5486. }
  5487. - (void)enumerateKeysAndRawValuesUsingBlock:
  5488. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  5489. BOOL stop = NO;
  5490. NSDictionary *internal = _dictionary;
  5491. NSEnumerator *keys = [internal keyEnumerator];
  5492. NSNumber *aKey;
  5493. while ((aKey = [keys nextObject])) {
  5494. NSNumber *aValue = internal[aKey];
  5495. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  5496. if (stop) {
  5497. break;
  5498. }
  5499. }
  5500. }
  5501. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5502. NSDictionary *internal = _dictionary;
  5503. NSUInteger count = internal.count;
  5504. if (count == 0) {
  5505. return 0;
  5506. }
  5507. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5508. GPBDataType keyDataType = field.mapKeyDataType;
  5509. size_t result = 0;
  5510. NSEnumerator *keys = [internal keyEnumerator];
  5511. NSNumber *aKey;
  5512. while ((aKey = [keys nextObject])) {
  5513. NSNumber *aValue = internal[aKey];
  5514. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5515. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5516. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5517. }
  5518. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5519. result += tagSize * count;
  5520. return result;
  5521. }
  5522. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5523. asField:(GPBFieldDescriptor *)field {
  5524. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5525. GPBDataType keyDataType = field.mapKeyDataType;
  5526. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5527. NSDictionary *internal = _dictionary;
  5528. NSEnumerator *keys = [internal keyEnumerator];
  5529. NSNumber *aKey;
  5530. while ((aKey = [keys nextObject])) {
  5531. NSNumber *aValue = internal[aKey];
  5532. [outputStream writeInt32NoTag:tag];
  5533. // Write the size of the message.
  5534. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5535. int32_t unwrappedValue = [aValue intValue];
  5536. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5537. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5538. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5539. // Write the fields.
  5540. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5541. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5542. }
  5543. }
  5544. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  5545. forKey:(GPBGenericValue *)key
  5546. keyDataType:(GPBDataType)keyDataType {
  5547. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  5548. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  5549. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  5550. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  5551. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  5552. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  5553. [outputStream flush];
  5554. [outputStream release];
  5555. return data;
  5556. }
  5557. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5558. forGPBGenericValueKey:(GPBGenericValue *)key {
  5559. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  5560. }
  5561. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5562. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, __unused BOOL *stop) {
  5563. block([NSString stringWithFormat:@"%llu", key], @(value));
  5564. }];
  5565. }
  5566. - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
  5567. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5568. if (wrapped && value) {
  5569. int32_t result = [wrapped intValue];
  5570. if (!_validationFunc(result)) {
  5571. result = kGPBUnrecognizedEnumeratorValue;
  5572. }
  5573. *value = result;
  5574. }
  5575. return (wrapped != NULL);
  5576. }
  5577. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
  5578. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5579. if (wrapped && rawValue) {
  5580. *rawValue = [wrapped intValue];
  5581. }
  5582. return (wrapped != NULL);
  5583. }
  5584. - (void)enumerateKeysAndEnumsUsingBlock:
  5585. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  5586. GPBEnumValidationFunc func = _validationFunc;
  5587. BOOL stop = NO;
  5588. NSEnumerator *keys = [_dictionary keyEnumerator];
  5589. NSNumber *aKey;
  5590. while ((aKey = [keys nextObject])) {
  5591. NSNumber *aValue = _dictionary[aKey];
  5592. int32_t unwrapped = [aValue intValue];
  5593. if (!func(unwrapped)) {
  5594. unwrapped = kGPBUnrecognizedEnumeratorValue;
  5595. }
  5596. block([aKey unsignedLongLongValue], unwrapped, &stop);
  5597. if (stop) {
  5598. break;
  5599. }
  5600. }
  5601. }
  5602. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  5603. if (otherDictionary) {
  5604. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5605. if (_autocreator) {
  5606. GPBAutocreatedDictionaryModified(_autocreator, self);
  5607. }
  5608. }
  5609. }
  5610. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  5611. [_dictionary setObject:@(value) forKey:@(key)];
  5612. if (_autocreator) {
  5613. GPBAutocreatedDictionaryModified(_autocreator, self);
  5614. }
  5615. }
  5616. - (void)removeEnumForKey:(uint64_t)aKey {
  5617. [_dictionary removeObjectForKey:@(aKey)];
  5618. }
  5619. - (void)removeAll {
  5620. [_dictionary removeAllObjects];
  5621. }
  5622. - (void)setEnum:(int32_t)value forKey:(uint64_t)key {
  5623. if (!_validationFunc(value)) {
  5624. [NSException raise:NSInvalidArgumentException
  5625. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  5626. value];
  5627. }
  5628. [_dictionary setObject:@(value) forKey:@(key)];
  5629. if (_autocreator) {
  5630. GPBAutocreatedDictionaryModified(_autocreator, self);
  5631. }
  5632. }
  5633. @end
  5634. #pragma mark - UInt64 -> Object
  5635. @implementation GPBUInt64ObjectDictionary {
  5636. @package
  5637. NSMutableDictionary *_dictionary;
  5638. }
  5639. - (instancetype)init {
  5640. return [self initWithObjects:NULL forKeys:NULL count:0];
  5641. }
  5642. - (instancetype)initWithObjects:(const id [])objects
  5643. forKeys:(const uint64_t [])keys
  5644. count:(NSUInteger)count {
  5645. self = [super init];
  5646. if (self) {
  5647. _dictionary = [[NSMutableDictionary alloc] init];
  5648. if (count && objects && keys) {
  5649. for (NSUInteger i = 0; i < count; ++i) {
  5650. if (!objects[i]) {
  5651. [NSException raise:NSInvalidArgumentException
  5652. format:@"Attempting to add nil object to a Dictionary"];
  5653. }
  5654. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  5655. }
  5656. }
  5657. }
  5658. return self;
  5659. }
  5660. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  5661. self = [self initWithObjects:NULL forKeys:NULL count:0];
  5662. if (self) {
  5663. if (dictionary) {
  5664. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5665. }
  5666. }
  5667. return self;
  5668. }
  5669. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5670. return [self initWithObjects:NULL forKeys:NULL count:0];
  5671. }
  5672. - (void)dealloc {
  5673. NSAssert(!_autocreator,
  5674. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5675. [self class], _autocreator);
  5676. [_dictionary release];
  5677. [super dealloc];
  5678. }
  5679. - (instancetype)copyWithZone:(NSZone *)zone {
  5680. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  5681. }
  5682. - (BOOL)isEqual:(id)other {
  5683. if (self == other) {
  5684. return YES;
  5685. }
  5686. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  5687. return NO;
  5688. }
  5689. GPBUInt64ObjectDictionary *otherDictionary = other;
  5690. return [_dictionary isEqual:otherDictionary->_dictionary];
  5691. }
  5692. - (NSUInteger)hash {
  5693. return _dictionary.count;
  5694. }
  5695. - (NSString *)description {
  5696. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5697. }
  5698. - (NSUInteger)count {
  5699. return _dictionary.count;
  5700. }
  5701. - (void)enumerateKeysAndObjectsUsingBlock:
  5702. (void (NS_NOESCAPE ^)(uint64_t key, id object, BOOL *stop))block {
  5703. BOOL stop = NO;
  5704. NSDictionary *internal = _dictionary;
  5705. NSEnumerator *keys = [internal keyEnumerator];
  5706. NSNumber *aKey;
  5707. while ((aKey = [keys nextObject])) {
  5708. id aObject = internal[aKey];
  5709. block([aKey unsignedLongLongValue], aObject, &stop);
  5710. if (stop) {
  5711. break;
  5712. }
  5713. }
  5714. }
  5715. - (BOOL)isInitialized {
  5716. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  5717. if (!msg.initialized) {
  5718. return NO;
  5719. }
  5720. }
  5721. return YES;
  5722. }
  5723. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  5724. GPBUInt64ObjectDictionary *newDict =
  5725. [[GPBUInt64ObjectDictionary alloc] init];
  5726. NSEnumerator *keys = [_dictionary keyEnumerator];
  5727. id aKey;
  5728. NSMutableDictionary *internalDict = newDict->_dictionary;
  5729. while ((aKey = [keys nextObject])) {
  5730. GPBMessage *msg = _dictionary[aKey];
  5731. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  5732. [internalDict setObject:copiedMsg forKey:aKey];
  5733. [copiedMsg release];
  5734. }
  5735. return newDict;
  5736. }
  5737. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5738. NSDictionary *internal = _dictionary;
  5739. NSUInteger count = internal.count;
  5740. if (count == 0) {
  5741. return 0;
  5742. }
  5743. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5744. GPBDataType keyDataType = field.mapKeyDataType;
  5745. size_t result = 0;
  5746. NSEnumerator *keys = [internal keyEnumerator];
  5747. NSNumber *aKey;
  5748. while ((aKey = [keys nextObject])) {
  5749. id aObject = internal[aKey];
  5750. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5751. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  5752. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5753. }
  5754. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5755. result += tagSize * count;
  5756. return result;
  5757. }
  5758. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5759. asField:(GPBFieldDescriptor *)field {
  5760. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5761. GPBDataType keyDataType = field.mapKeyDataType;
  5762. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5763. NSDictionary *internal = _dictionary;
  5764. NSEnumerator *keys = [internal keyEnumerator];
  5765. NSNumber *aKey;
  5766. while ((aKey = [keys nextObject])) {
  5767. id aObject = internal[aKey];
  5768. [outputStream writeInt32NoTag:tag];
  5769. // Write the size of the message.
  5770. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5771. id unwrappedValue = aObject;
  5772. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5773. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5774. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5775. // Write the fields.
  5776. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5777. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5778. }
  5779. }
  5780. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5781. forGPBGenericValueKey:(GPBGenericValue *)key {
  5782. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  5783. }
  5784. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5785. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, __unused BOOL *stop) {
  5786. block([NSString stringWithFormat:@"%llu", key], object);
  5787. }];
  5788. }
  5789. - (id)objectForKey:(uint64_t)key {
  5790. id result = [_dictionary objectForKey:@(key)];
  5791. return result;
  5792. }
  5793. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  5794. if (otherDictionary) {
  5795. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5796. if (_autocreator) {
  5797. GPBAutocreatedDictionaryModified(_autocreator, self);
  5798. }
  5799. }
  5800. }
  5801. - (void)setObject:(id)object forKey:(uint64_t)key {
  5802. if (!object) {
  5803. [NSException raise:NSInvalidArgumentException
  5804. format:@"Attempting to add nil object to a Dictionary"];
  5805. }
  5806. [_dictionary setObject:object forKey:@(key)];
  5807. if (_autocreator) {
  5808. GPBAutocreatedDictionaryModified(_autocreator, self);
  5809. }
  5810. }
  5811. - (void)removeObjectForKey:(uint64_t)aKey {
  5812. [_dictionary removeObjectForKey:@(aKey)];
  5813. }
  5814. - (void)removeAll {
  5815. [_dictionary removeAllObjects];
  5816. }
  5817. @end
  5818. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  5819. // This block of code is generated, do not edit it directly.
  5820. #pragma mark - Int64 -> UInt32
  5821. @implementation GPBInt64UInt32Dictionary {
  5822. @package
  5823. NSMutableDictionary *_dictionary;
  5824. }
  5825. - (instancetype)init {
  5826. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5827. }
  5828. - (instancetype)initWithUInt32s:(const uint32_t [])values
  5829. forKeys:(const int64_t [])keys
  5830. count:(NSUInteger)count {
  5831. self = [super init];
  5832. if (self) {
  5833. _dictionary = [[NSMutableDictionary alloc] init];
  5834. if (count && values && keys) {
  5835. for (NSUInteger i = 0; i < count; ++i) {
  5836. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5837. }
  5838. }
  5839. }
  5840. return self;
  5841. }
  5842. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  5843. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  5844. if (self) {
  5845. if (dictionary) {
  5846. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5847. }
  5848. }
  5849. return self;
  5850. }
  5851. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5852. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5853. }
  5854. - (void)dealloc {
  5855. NSAssert(!_autocreator,
  5856. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5857. [self class], _autocreator);
  5858. [_dictionary release];
  5859. [super dealloc];
  5860. }
  5861. - (instancetype)copyWithZone:(NSZone *)zone {
  5862. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  5863. }
  5864. - (BOOL)isEqual:(id)other {
  5865. if (self == other) {
  5866. return YES;
  5867. }
  5868. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  5869. return NO;
  5870. }
  5871. GPBInt64UInt32Dictionary *otherDictionary = other;
  5872. return [_dictionary isEqual:otherDictionary->_dictionary];
  5873. }
  5874. - (NSUInteger)hash {
  5875. return _dictionary.count;
  5876. }
  5877. - (NSString *)description {
  5878. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5879. }
  5880. - (NSUInteger)count {
  5881. return _dictionary.count;
  5882. }
  5883. - (void)enumerateKeysAndUInt32sUsingBlock:
  5884. (void (NS_NOESCAPE ^)(int64_t key, uint32_t value, BOOL *stop))block {
  5885. BOOL stop = NO;
  5886. NSDictionary *internal = _dictionary;
  5887. NSEnumerator *keys = [internal keyEnumerator];
  5888. NSNumber *aKey;
  5889. while ((aKey = [keys nextObject])) {
  5890. NSNumber *aValue = internal[aKey];
  5891. block([aKey longLongValue], [aValue unsignedIntValue], &stop);
  5892. if (stop) {
  5893. break;
  5894. }
  5895. }
  5896. }
  5897. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5898. NSDictionary *internal = _dictionary;
  5899. NSUInteger count = internal.count;
  5900. if (count == 0) {
  5901. return 0;
  5902. }
  5903. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5904. GPBDataType keyDataType = field.mapKeyDataType;
  5905. size_t result = 0;
  5906. NSEnumerator *keys = [internal keyEnumerator];
  5907. NSNumber *aKey;
  5908. while ((aKey = [keys nextObject])) {
  5909. NSNumber *aValue = internal[aKey];
  5910. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  5911. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  5912. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5913. }
  5914. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5915. result += tagSize * count;
  5916. return result;
  5917. }
  5918. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5919. asField:(GPBFieldDescriptor *)field {
  5920. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5921. GPBDataType keyDataType = field.mapKeyDataType;
  5922. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5923. NSDictionary *internal = _dictionary;
  5924. NSEnumerator *keys = [internal keyEnumerator];
  5925. NSNumber *aKey;
  5926. while ((aKey = [keys nextObject])) {
  5927. NSNumber *aValue = internal[aKey];
  5928. [outputStream writeInt32NoTag:tag];
  5929. // Write the size of the message.
  5930. int64_t unwrappedKey = [aKey longLongValue];
  5931. uint32_t unwrappedValue = [aValue unsignedIntValue];
  5932. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5933. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5934. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5935. // Write the fields.
  5936. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5937. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5938. }
  5939. }
  5940. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5941. forGPBGenericValueKey:(GPBGenericValue *)key {
  5942. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  5943. }
  5944. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5945. [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, __unused BOOL *stop) {
  5946. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  5947. }];
  5948. }
  5949. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
  5950. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5951. if (wrapped && value) {
  5952. *value = [wrapped unsignedIntValue];
  5953. }
  5954. return (wrapped != NULL);
  5955. }
  5956. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  5957. if (otherDictionary) {
  5958. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5959. if (_autocreator) {
  5960. GPBAutocreatedDictionaryModified(_autocreator, self);
  5961. }
  5962. }
  5963. }
  5964. - (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
  5965. [_dictionary setObject:@(value) forKey:@(key)];
  5966. if (_autocreator) {
  5967. GPBAutocreatedDictionaryModified(_autocreator, self);
  5968. }
  5969. }
  5970. - (void)removeUInt32ForKey:(int64_t)aKey {
  5971. [_dictionary removeObjectForKey:@(aKey)];
  5972. }
  5973. - (void)removeAll {
  5974. [_dictionary removeAllObjects];
  5975. }
  5976. @end
  5977. #pragma mark - Int64 -> Int32
  5978. @implementation GPBInt64Int32Dictionary {
  5979. @package
  5980. NSMutableDictionary *_dictionary;
  5981. }
  5982. - (instancetype)init {
  5983. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5984. }
  5985. - (instancetype)initWithInt32s:(const int32_t [])values
  5986. forKeys:(const int64_t [])keys
  5987. count:(NSUInteger)count {
  5988. self = [super init];
  5989. if (self) {
  5990. _dictionary = [[NSMutableDictionary alloc] init];
  5991. if (count && values && keys) {
  5992. for (NSUInteger i = 0; i < count; ++i) {
  5993. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5994. }
  5995. }
  5996. }
  5997. return self;
  5998. }
  5999. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6000. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  6001. if (self) {
  6002. if (dictionary) {
  6003. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6004. }
  6005. }
  6006. return self;
  6007. }
  6008. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6009. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6010. }
  6011. - (void)dealloc {
  6012. NSAssert(!_autocreator,
  6013. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6014. [self class], _autocreator);
  6015. [_dictionary release];
  6016. [super dealloc];
  6017. }
  6018. - (instancetype)copyWithZone:(NSZone *)zone {
  6019. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6020. }
  6021. - (BOOL)isEqual:(id)other {
  6022. if (self == other) {
  6023. return YES;
  6024. }
  6025. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6026. return NO;
  6027. }
  6028. GPBInt64Int32Dictionary *otherDictionary = other;
  6029. return [_dictionary isEqual:otherDictionary->_dictionary];
  6030. }
  6031. - (NSUInteger)hash {
  6032. return _dictionary.count;
  6033. }
  6034. - (NSString *)description {
  6035. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6036. }
  6037. - (NSUInteger)count {
  6038. return _dictionary.count;
  6039. }
  6040. - (void)enumerateKeysAndInt32sUsingBlock:
  6041. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  6042. BOOL stop = NO;
  6043. NSDictionary *internal = _dictionary;
  6044. NSEnumerator *keys = [internal keyEnumerator];
  6045. NSNumber *aKey;
  6046. while ((aKey = [keys nextObject])) {
  6047. NSNumber *aValue = internal[aKey];
  6048. block([aKey longLongValue], [aValue intValue], &stop);
  6049. if (stop) {
  6050. break;
  6051. }
  6052. }
  6053. }
  6054. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6055. NSDictionary *internal = _dictionary;
  6056. NSUInteger count = internal.count;
  6057. if (count == 0) {
  6058. return 0;
  6059. }
  6060. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6061. GPBDataType keyDataType = field.mapKeyDataType;
  6062. size_t result = 0;
  6063. NSEnumerator *keys = [internal keyEnumerator];
  6064. NSNumber *aKey;
  6065. while ((aKey = [keys nextObject])) {
  6066. NSNumber *aValue = internal[aKey];
  6067. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6068. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6069. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6070. }
  6071. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6072. result += tagSize * count;
  6073. return result;
  6074. }
  6075. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6076. asField:(GPBFieldDescriptor *)field {
  6077. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6078. GPBDataType keyDataType = field.mapKeyDataType;
  6079. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6080. NSDictionary *internal = _dictionary;
  6081. NSEnumerator *keys = [internal keyEnumerator];
  6082. NSNumber *aKey;
  6083. while ((aKey = [keys nextObject])) {
  6084. NSNumber *aValue = internal[aKey];
  6085. [outputStream writeInt32NoTag:tag];
  6086. // Write the size of the message.
  6087. int64_t unwrappedKey = [aKey longLongValue];
  6088. int32_t unwrappedValue = [aValue intValue];
  6089. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6090. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6091. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6092. // Write the fields.
  6093. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6094. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6095. }
  6096. }
  6097. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6098. forGPBGenericValueKey:(GPBGenericValue *)key {
  6099. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6100. }
  6101. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6102. [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, __unused BOOL *stop) {
  6103. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6104. }];
  6105. }
  6106. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
  6107. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6108. if (wrapped && value) {
  6109. *value = [wrapped intValue];
  6110. }
  6111. return (wrapped != NULL);
  6112. }
  6113. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6114. if (otherDictionary) {
  6115. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6116. if (_autocreator) {
  6117. GPBAutocreatedDictionaryModified(_autocreator, self);
  6118. }
  6119. }
  6120. }
  6121. - (void)setInt32:(int32_t)value forKey:(int64_t)key {
  6122. [_dictionary setObject:@(value) forKey:@(key)];
  6123. if (_autocreator) {
  6124. GPBAutocreatedDictionaryModified(_autocreator, self);
  6125. }
  6126. }
  6127. - (void)removeInt32ForKey:(int64_t)aKey {
  6128. [_dictionary removeObjectForKey:@(aKey)];
  6129. }
  6130. - (void)removeAll {
  6131. [_dictionary removeAllObjects];
  6132. }
  6133. @end
  6134. #pragma mark - Int64 -> UInt64
  6135. @implementation GPBInt64UInt64Dictionary {
  6136. @package
  6137. NSMutableDictionary *_dictionary;
  6138. }
  6139. - (instancetype)init {
  6140. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6141. }
  6142. - (instancetype)initWithUInt64s:(const uint64_t [])values
  6143. forKeys:(const int64_t [])keys
  6144. count:(NSUInteger)count {
  6145. self = [super init];
  6146. if (self) {
  6147. _dictionary = [[NSMutableDictionary alloc] init];
  6148. if (count && values && keys) {
  6149. for (NSUInteger i = 0; i < count; ++i) {
  6150. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6151. }
  6152. }
  6153. }
  6154. return self;
  6155. }
  6156. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6157. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  6158. if (self) {
  6159. if (dictionary) {
  6160. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6161. }
  6162. }
  6163. return self;
  6164. }
  6165. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6166. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6167. }
  6168. - (void)dealloc {
  6169. NSAssert(!_autocreator,
  6170. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6171. [self class], _autocreator);
  6172. [_dictionary release];
  6173. [super dealloc];
  6174. }
  6175. - (instancetype)copyWithZone:(NSZone *)zone {
  6176. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6177. }
  6178. - (BOOL)isEqual:(id)other {
  6179. if (self == other) {
  6180. return YES;
  6181. }
  6182. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6183. return NO;
  6184. }
  6185. GPBInt64UInt64Dictionary *otherDictionary = other;
  6186. return [_dictionary isEqual:otherDictionary->_dictionary];
  6187. }
  6188. - (NSUInteger)hash {
  6189. return _dictionary.count;
  6190. }
  6191. - (NSString *)description {
  6192. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6193. }
  6194. - (NSUInteger)count {
  6195. return _dictionary.count;
  6196. }
  6197. - (void)enumerateKeysAndUInt64sUsingBlock:
  6198. (void (NS_NOESCAPE ^)(int64_t key, uint64_t value, BOOL *stop))block {
  6199. BOOL stop = NO;
  6200. NSDictionary *internal = _dictionary;
  6201. NSEnumerator *keys = [internal keyEnumerator];
  6202. NSNumber *aKey;
  6203. while ((aKey = [keys nextObject])) {
  6204. NSNumber *aValue = internal[aKey];
  6205. block([aKey longLongValue], [aValue unsignedLongLongValue], &stop);
  6206. if (stop) {
  6207. break;
  6208. }
  6209. }
  6210. }
  6211. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6212. NSDictionary *internal = _dictionary;
  6213. NSUInteger count = internal.count;
  6214. if (count == 0) {
  6215. return 0;
  6216. }
  6217. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6218. GPBDataType keyDataType = field.mapKeyDataType;
  6219. size_t result = 0;
  6220. NSEnumerator *keys = [internal keyEnumerator];
  6221. NSNumber *aKey;
  6222. while ((aKey = [keys nextObject])) {
  6223. NSNumber *aValue = internal[aKey];
  6224. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6225. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6226. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6227. }
  6228. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6229. result += tagSize * count;
  6230. return result;
  6231. }
  6232. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6233. asField:(GPBFieldDescriptor *)field {
  6234. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6235. GPBDataType keyDataType = field.mapKeyDataType;
  6236. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6237. NSDictionary *internal = _dictionary;
  6238. NSEnumerator *keys = [internal keyEnumerator];
  6239. NSNumber *aKey;
  6240. while ((aKey = [keys nextObject])) {
  6241. NSNumber *aValue = internal[aKey];
  6242. [outputStream writeInt32NoTag:tag];
  6243. // Write the size of the message.
  6244. int64_t unwrappedKey = [aKey longLongValue];
  6245. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  6246. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6247. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6248. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6249. // Write the fields.
  6250. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6251. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6252. }
  6253. }
  6254. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6255. forGPBGenericValueKey:(GPBGenericValue *)key {
  6256. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6257. }
  6258. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6259. [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, __unused BOOL *stop) {
  6260. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6261. }];
  6262. }
  6263. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
  6264. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6265. if (wrapped && value) {
  6266. *value = [wrapped unsignedLongLongValue];
  6267. }
  6268. return (wrapped != NULL);
  6269. }
  6270. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6271. if (otherDictionary) {
  6272. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6273. if (_autocreator) {
  6274. GPBAutocreatedDictionaryModified(_autocreator, self);
  6275. }
  6276. }
  6277. }
  6278. - (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
  6279. [_dictionary setObject:@(value) forKey:@(key)];
  6280. if (_autocreator) {
  6281. GPBAutocreatedDictionaryModified(_autocreator, self);
  6282. }
  6283. }
  6284. - (void)removeUInt64ForKey:(int64_t)aKey {
  6285. [_dictionary removeObjectForKey:@(aKey)];
  6286. }
  6287. - (void)removeAll {
  6288. [_dictionary removeAllObjects];
  6289. }
  6290. @end
  6291. #pragma mark - Int64 -> Int64
  6292. @implementation GPBInt64Int64Dictionary {
  6293. @package
  6294. NSMutableDictionary *_dictionary;
  6295. }
  6296. - (instancetype)init {
  6297. return [self initWithInt64s:NULL forKeys:NULL count:0];
  6298. }
  6299. - (instancetype)initWithInt64s:(const int64_t [])values
  6300. forKeys:(const int64_t [])keys
  6301. count:(NSUInteger)count {
  6302. self = [super init];
  6303. if (self) {
  6304. _dictionary = [[NSMutableDictionary alloc] init];
  6305. if (count && values && keys) {
  6306. for (NSUInteger i = 0; i < count; ++i) {
  6307. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6308. }
  6309. }
  6310. }
  6311. return self;
  6312. }
  6313. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6314. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  6315. if (self) {
  6316. if (dictionary) {
  6317. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6318. }
  6319. }
  6320. return self;
  6321. }
  6322. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6323. return [self initWithInt64s:NULL forKeys:NULL count:0];
  6324. }
  6325. - (void)dealloc {
  6326. NSAssert(!_autocreator,
  6327. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6328. [self class], _autocreator);
  6329. [_dictionary release];
  6330. [super dealloc];
  6331. }
  6332. - (instancetype)copyWithZone:(NSZone *)zone {
  6333. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  6334. }
  6335. - (BOOL)isEqual:(id)other {
  6336. if (self == other) {
  6337. return YES;
  6338. }
  6339. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  6340. return NO;
  6341. }
  6342. GPBInt64Int64Dictionary *otherDictionary = other;
  6343. return [_dictionary isEqual:otherDictionary->_dictionary];
  6344. }
  6345. - (NSUInteger)hash {
  6346. return _dictionary.count;
  6347. }
  6348. - (NSString *)description {
  6349. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6350. }
  6351. - (NSUInteger)count {
  6352. return _dictionary.count;
  6353. }
  6354. - (void)enumerateKeysAndInt64sUsingBlock:
  6355. (void (NS_NOESCAPE ^)(int64_t key, int64_t value, BOOL *stop))block {
  6356. BOOL stop = NO;
  6357. NSDictionary *internal = _dictionary;
  6358. NSEnumerator *keys = [internal keyEnumerator];
  6359. NSNumber *aKey;
  6360. while ((aKey = [keys nextObject])) {
  6361. NSNumber *aValue = internal[aKey];
  6362. block([aKey longLongValue], [aValue longLongValue], &stop);
  6363. if (stop) {
  6364. break;
  6365. }
  6366. }
  6367. }
  6368. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6369. NSDictionary *internal = _dictionary;
  6370. NSUInteger count = internal.count;
  6371. if (count == 0) {
  6372. return 0;
  6373. }
  6374. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6375. GPBDataType keyDataType = field.mapKeyDataType;
  6376. size_t result = 0;
  6377. NSEnumerator *keys = [internal keyEnumerator];
  6378. NSNumber *aKey;
  6379. while ((aKey = [keys nextObject])) {
  6380. NSNumber *aValue = internal[aKey];
  6381. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6382. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  6383. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6384. }
  6385. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6386. result += tagSize * count;
  6387. return result;
  6388. }
  6389. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6390. asField:(GPBFieldDescriptor *)field {
  6391. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6392. GPBDataType keyDataType = field.mapKeyDataType;
  6393. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6394. NSDictionary *internal = _dictionary;
  6395. NSEnumerator *keys = [internal keyEnumerator];
  6396. NSNumber *aKey;
  6397. while ((aKey = [keys nextObject])) {
  6398. NSNumber *aValue = internal[aKey];
  6399. [outputStream writeInt32NoTag:tag];
  6400. // Write the size of the message.
  6401. int64_t unwrappedKey = [aKey longLongValue];
  6402. int64_t unwrappedValue = [aValue longLongValue];
  6403. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6404. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6405. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6406. // Write the fields.
  6407. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6408. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6409. }
  6410. }
  6411. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6412. forGPBGenericValueKey:(GPBGenericValue *)key {
  6413. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  6414. }
  6415. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6416. [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, __unused BOOL *stop) {
  6417. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  6418. }];
  6419. }
  6420. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
  6421. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6422. if (wrapped && value) {
  6423. *value = [wrapped longLongValue];
  6424. }
  6425. return (wrapped != NULL);
  6426. }
  6427. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  6428. if (otherDictionary) {
  6429. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6430. if (_autocreator) {
  6431. GPBAutocreatedDictionaryModified(_autocreator, self);
  6432. }
  6433. }
  6434. }
  6435. - (void)setInt64:(int64_t)value forKey:(int64_t)key {
  6436. [_dictionary setObject:@(value) forKey:@(key)];
  6437. if (_autocreator) {
  6438. GPBAutocreatedDictionaryModified(_autocreator, self);
  6439. }
  6440. }
  6441. - (void)removeInt64ForKey:(int64_t)aKey {
  6442. [_dictionary removeObjectForKey:@(aKey)];
  6443. }
  6444. - (void)removeAll {
  6445. [_dictionary removeAllObjects];
  6446. }
  6447. @end
  6448. #pragma mark - Int64 -> Bool
  6449. @implementation GPBInt64BoolDictionary {
  6450. @package
  6451. NSMutableDictionary *_dictionary;
  6452. }
  6453. - (instancetype)init {
  6454. return [self initWithBools:NULL forKeys:NULL count:0];
  6455. }
  6456. - (instancetype)initWithBools:(const BOOL [])values
  6457. forKeys:(const int64_t [])keys
  6458. count:(NSUInteger)count {
  6459. self = [super init];
  6460. if (self) {
  6461. _dictionary = [[NSMutableDictionary alloc] init];
  6462. if (count && values && keys) {
  6463. for (NSUInteger i = 0; i < count; ++i) {
  6464. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6465. }
  6466. }
  6467. }
  6468. return self;
  6469. }
  6470. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  6471. self = [self initWithBools:NULL forKeys:NULL count:0];
  6472. if (self) {
  6473. if (dictionary) {
  6474. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6475. }
  6476. }
  6477. return self;
  6478. }
  6479. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6480. return [self initWithBools:NULL forKeys:NULL count:0];
  6481. }
  6482. - (void)dealloc {
  6483. NSAssert(!_autocreator,
  6484. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6485. [self class], _autocreator);
  6486. [_dictionary release];
  6487. [super dealloc];
  6488. }
  6489. - (instancetype)copyWithZone:(NSZone *)zone {
  6490. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  6491. }
  6492. - (BOOL)isEqual:(id)other {
  6493. if (self == other) {
  6494. return YES;
  6495. }
  6496. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  6497. return NO;
  6498. }
  6499. GPBInt64BoolDictionary *otherDictionary = other;
  6500. return [_dictionary isEqual:otherDictionary->_dictionary];
  6501. }
  6502. - (NSUInteger)hash {
  6503. return _dictionary.count;
  6504. }
  6505. - (NSString *)description {
  6506. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6507. }
  6508. - (NSUInteger)count {
  6509. return _dictionary.count;
  6510. }
  6511. - (void)enumerateKeysAndBoolsUsingBlock:
  6512. (void (NS_NOESCAPE ^)(int64_t key, BOOL value, BOOL *stop))block {
  6513. BOOL stop = NO;
  6514. NSDictionary *internal = _dictionary;
  6515. NSEnumerator *keys = [internal keyEnumerator];
  6516. NSNumber *aKey;
  6517. while ((aKey = [keys nextObject])) {
  6518. NSNumber *aValue = internal[aKey];
  6519. block([aKey longLongValue], [aValue boolValue], &stop);
  6520. if (stop) {
  6521. break;
  6522. }
  6523. }
  6524. }
  6525. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6526. NSDictionary *internal = _dictionary;
  6527. NSUInteger count = internal.count;
  6528. if (count == 0) {
  6529. return 0;
  6530. }
  6531. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6532. GPBDataType keyDataType = field.mapKeyDataType;
  6533. size_t result = 0;
  6534. NSEnumerator *keys = [internal keyEnumerator];
  6535. NSNumber *aKey;
  6536. while ((aKey = [keys nextObject])) {
  6537. NSNumber *aValue = internal[aKey];
  6538. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6539. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  6540. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6541. }
  6542. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6543. result += tagSize * count;
  6544. return result;
  6545. }
  6546. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6547. asField:(GPBFieldDescriptor *)field {
  6548. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6549. GPBDataType keyDataType = field.mapKeyDataType;
  6550. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6551. NSDictionary *internal = _dictionary;
  6552. NSEnumerator *keys = [internal keyEnumerator];
  6553. NSNumber *aKey;
  6554. while ((aKey = [keys nextObject])) {
  6555. NSNumber *aValue = internal[aKey];
  6556. [outputStream writeInt32NoTag:tag];
  6557. // Write the size of the message.
  6558. int64_t unwrappedKey = [aKey longLongValue];
  6559. BOOL unwrappedValue = [aValue boolValue];
  6560. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6561. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6562. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6563. // Write the fields.
  6564. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6565. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6566. }
  6567. }
  6568. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6569. forGPBGenericValueKey:(GPBGenericValue *)key {
  6570. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  6571. }
  6572. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6573. [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, __unused BOOL *stop) {
  6574. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  6575. }];
  6576. }
  6577. - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
  6578. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6579. if (wrapped && value) {
  6580. *value = [wrapped boolValue];
  6581. }
  6582. return (wrapped != NULL);
  6583. }
  6584. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  6585. if (otherDictionary) {
  6586. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6587. if (_autocreator) {
  6588. GPBAutocreatedDictionaryModified(_autocreator, self);
  6589. }
  6590. }
  6591. }
  6592. - (void)setBool:(BOOL)value forKey:(int64_t)key {
  6593. [_dictionary setObject:@(value) forKey:@(key)];
  6594. if (_autocreator) {
  6595. GPBAutocreatedDictionaryModified(_autocreator, self);
  6596. }
  6597. }
  6598. - (void)removeBoolForKey:(int64_t)aKey {
  6599. [_dictionary removeObjectForKey:@(aKey)];
  6600. }
  6601. - (void)removeAll {
  6602. [_dictionary removeAllObjects];
  6603. }
  6604. @end
  6605. #pragma mark - Int64 -> Float
  6606. @implementation GPBInt64FloatDictionary {
  6607. @package
  6608. NSMutableDictionary *_dictionary;
  6609. }
  6610. - (instancetype)init {
  6611. return [self initWithFloats:NULL forKeys:NULL count:0];
  6612. }
  6613. - (instancetype)initWithFloats:(const float [])values
  6614. forKeys:(const int64_t [])keys
  6615. count:(NSUInteger)count {
  6616. self = [super init];
  6617. if (self) {
  6618. _dictionary = [[NSMutableDictionary alloc] init];
  6619. if (count && values && keys) {
  6620. for (NSUInteger i = 0; i < count; ++i) {
  6621. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6622. }
  6623. }
  6624. }
  6625. return self;
  6626. }
  6627. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  6628. self = [self initWithFloats:NULL forKeys:NULL count:0];
  6629. if (self) {
  6630. if (dictionary) {
  6631. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6632. }
  6633. }
  6634. return self;
  6635. }
  6636. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6637. return [self initWithFloats:NULL forKeys:NULL count:0];
  6638. }
  6639. - (void)dealloc {
  6640. NSAssert(!_autocreator,
  6641. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6642. [self class], _autocreator);
  6643. [_dictionary release];
  6644. [super dealloc];
  6645. }
  6646. - (instancetype)copyWithZone:(NSZone *)zone {
  6647. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  6648. }
  6649. - (BOOL)isEqual:(id)other {
  6650. if (self == other) {
  6651. return YES;
  6652. }
  6653. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  6654. return NO;
  6655. }
  6656. GPBInt64FloatDictionary *otherDictionary = other;
  6657. return [_dictionary isEqual:otherDictionary->_dictionary];
  6658. }
  6659. - (NSUInteger)hash {
  6660. return _dictionary.count;
  6661. }
  6662. - (NSString *)description {
  6663. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6664. }
  6665. - (NSUInteger)count {
  6666. return _dictionary.count;
  6667. }
  6668. - (void)enumerateKeysAndFloatsUsingBlock:
  6669. (void (NS_NOESCAPE ^)(int64_t key, float value, BOOL *stop))block {
  6670. BOOL stop = NO;
  6671. NSDictionary *internal = _dictionary;
  6672. NSEnumerator *keys = [internal keyEnumerator];
  6673. NSNumber *aKey;
  6674. while ((aKey = [keys nextObject])) {
  6675. NSNumber *aValue = internal[aKey];
  6676. block([aKey longLongValue], [aValue floatValue], &stop);
  6677. if (stop) {
  6678. break;
  6679. }
  6680. }
  6681. }
  6682. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6683. NSDictionary *internal = _dictionary;
  6684. NSUInteger count = internal.count;
  6685. if (count == 0) {
  6686. return 0;
  6687. }
  6688. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6689. GPBDataType keyDataType = field.mapKeyDataType;
  6690. size_t result = 0;
  6691. NSEnumerator *keys = [internal keyEnumerator];
  6692. NSNumber *aKey;
  6693. while ((aKey = [keys nextObject])) {
  6694. NSNumber *aValue = internal[aKey];
  6695. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6696. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  6697. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6698. }
  6699. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6700. result += tagSize * count;
  6701. return result;
  6702. }
  6703. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6704. asField:(GPBFieldDescriptor *)field {
  6705. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6706. GPBDataType keyDataType = field.mapKeyDataType;
  6707. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6708. NSDictionary *internal = _dictionary;
  6709. NSEnumerator *keys = [internal keyEnumerator];
  6710. NSNumber *aKey;
  6711. while ((aKey = [keys nextObject])) {
  6712. NSNumber *aValue = internal[aKey];
  6713. [outputStream writeInt32NoTag:tag];
  6714. // Write the size of the message.
  6715. int64_t unwrappedKey = [aKey longLongValue];
  6716. float unwrappedValue = [aValue floatValue];
  6717. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6718. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6719. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6720. // Write the fields.
  6721. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6722. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6723. }
  6724. }
  6725. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6726. forGPBGenericValueKey:(GPBGenericValue *)key {
  6727. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  6728. }
  6729. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6730. [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, __unused BOOL *stop) {
  6731. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  6732. }];
  6733. }
  6734. - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
  6735. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6736. if (wrapped && value) {
  6737. *value = [wrapped floatValue];
  6738. }
  6739. return (wrapped != NULL);
  6740. }
  6741. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  6742. if (otherDictionary) {
  6743. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6744. if (_autocreator) {
  6745. GPBAutocreatedDictionaryModified(_autocreator, self);
  6746. }
  6747. }
  6748. }
  6749. - (void)setFloat:(float)value forKey:(int64_t)key {
  6750. [_dictionary setObject:@(value) forKey:@(key)];
  6751. if (_autocreator) {
  6752. GPBAutocreatedDictionaryModified(_autocreator, self);
  6753. }
  6754. }
  6755. - (void)removeFloatForKey:(int64_t)aKey {
  6756. [_dictionary removeObjectForKey:@(aKey)];
  6757. }
  6758. - (void)removeAll {
  6759. [_dictionary removeAllObjects];
  6760. }
  6761. @end
  6762. #pragma mark - Int64 -> Double
  6763. @implementation GPBInt64DoubleDictionary {
  6764. @package
  6765. NSMutableDictionary *_dictionary;
  6766. }
  6767. - (instancetype)init {
  6768. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6769. }
  6770. - (instancetype)initWithDoubles:(const double [])values
  6771. forKeys:(const int64_t [])keys
  6772. count:(NSUInteger)count {
  6773. self = [super init];
  6774. if (self) {
  6775. _dictionary = [[NSMutableDictionary alloc] init];
  6776. if (count && values && keys) {
  6777. for (NSUInteger i = 0; i < count; ++i) {
  6778. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6779. }
  6780. }
  6781. }
  6782. return self;
  6783. }
  6784. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  6785. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  6786. if (self) {
  6787. if (dictionary) {
  6788. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6789. }
  6790. }
  6791. return self;
  6792. }
  6793. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6794. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6795. }
  6796. - (void)dealloc {
  6797. NSAssert(!_autocreator,
  6798. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6799. [self class], _autocreator);
  6800. [_dictionary release];
  6801. [super dealloc];
  6802. }
  6803. - (instancetype)copyWithZone:(NSZone *)zone {
  6804. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6805. }
  6806. - (BOOL)isEqual:(id)other {
  6807. if (self == other) {
  6808. return YES;
  6809. }
  6810. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  6811. return NO;
  6812. }
  6813. GPBInt64DoubleDictionary *otherDictionary = other;
  6814. return [_dictionary isEqual:otherDictionary->_dictionary];
  6815. }
  6816. - (NSUInteger)hash {
  6817. return _dictionary.count;
  6818. }
  6819. - (NSString *)description {
  6820. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6821. }
  6822. - (NSUInteger)count {
  6823. return _dictionary.count;
  6824. }
  6825. - (void)enumerateKeysAndDoublesUsingBlock:
  6826. (void (NS_NOESCAPE ^)(int64_t key, double value, BOOL *stop))block {
  6827. BOOL stop = NO;
  6828. NSDictionary *internal = _dictionary;
  6829. NSEnumerator *keys = [internal keyEnumerator];
  6830. NSNumber *aKey;
  6831. while ((aKey = [keys nextObject])) {
  6832. NSNumber *aValue = internal[aKey];
  6833. block([aKey longLongValue], [aValue doubleValue], &stop);
  6834. if (stop) {
  6835. break;
  6836. }
  6837. }
  6838. }
  6839. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6840. NSDictionary *internal = _dictionary;
  6841. NSUInteger count = internal.count;
  6842. if (count == 0) {
  6843. return 0;
  6844. }
  6845. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6846. GPBDataType keyDataType = field.mapKeyDataType;
  6847. size_t result = 0;
  6848. NSEnumerator *keys = [internal keyEnumerator];
  6849. NSNumber *aKey;
  6850. while ((aKey = [keys nextObject])) {
  6851. NSNumber *aValue = internal[aKey];
  6852. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6853. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6854. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6855. }
  6856. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6857. result += tagSize * count;
  6858. return result;
  6859. }
  6860. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6861. asField:(GPBFieldDescriptor *)field {
  6862. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6863. GPBDataType keyDataType = field.mapKeyDataType;
  6864. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6865. NSDictionary *internal = _dictionary;
  6866. NSEnumerator *keys = [internal keyEnumerator];
  6867. NSNumber *aKey;
  6868. while ((aKey = [keys nextObject])) {
  6869. NSNumber *aValue = internal[aKey];
  6870. [outputStream writeInt32NoTag:tag];
  6871. // Write the size of the message.
  6872. int64_t unwrappedKey = [aKey longLongValue];
  6873. double unwrappedValue = [aValue doubleValue];
  6874. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6875. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6876. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6877. // Write the fields.
  6878. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6879. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6880. }
  6881. }
  6882. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6883. forGPBGenericValueKey:(GPBGenericValue *)key {
  6884. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  6885. }
  6886. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6887. [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, __unused BOOL *stop) {
  6888. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6889. }];
  6890. }
  6891. - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
  6892. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6893. if (wrapped && value) {
  6894. *value = [wrapped doubleValue];
  6895. }
  6896. return (wrapped != NULL);
  6897. }
  6898. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  6899. if (otherDictionary) {
  6900. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6901. if (_autocreator) {
  6902. GPBAutocreatedDictionaryModified(_autocreator, self);
  6903. }
  6904. }
  6905. }
  6906. - (void)setDouble:(double)value forKey:(int64_t)key {
  6907. [_dictionary setObject:@(value) forKey:@(key)];
  6908. if (_autocreator) {
  6909. GPBAutocreatedDictionaryModified(_autocreator, self);
  6910. }
  6911. }
  6912. - (void)removeDoubleForKey:(int64_t)aKey {
  6913. [_dictionary removeObjectForKey:@(aKey)];
  6914. }
  6915. - (void)removeAll {
  6916. [_dictionary removeAllObjects];
  6917. }
  6918. @end
  6919. #pragma mark - Int64 -> Enum
  6920. @implementation GPBInt64EnumDictionary {
  6921. @package
  6922. NSMutableDictionary *_dictionary;
  6923. GPBEnumValidationFunc _validationFunc;
  6924. }
  6925. @synthesize validationFunc = _validationFunc;
  6926. - (instancetype)init {
  6927. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6928. }
  6929. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6930. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6931. }
  6932. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6933. rawValues:(const int32_t [])rawValues
  6934. forKeys:(const int64_t [])keys
  6935. count:(NSUInteger)count {
  6936. self = [super init];
  6937. if (self) {
  6938. _dictionary = [[NSMutableDictionary alloc] init];
  6939. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6940. if (count && rawValues && keys) {
  6941. for (NSUInteger i = 0; i < count; ++i) {
  6942. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6943. }
  6944. }
  6945. }
  6946. return self;
  6947. }
  6948. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  6949. self = [self initWithValidationFunction:dictionary.validationFunc
  6950. rawValues:NULL
  6951. forKeys:NULL
  6952. count:0];
  6953. if (self) {
  6954. if (dictionary) {
  6955. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6956. }
  6957. }
  6958. return self;
  6959. }
  6960. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6961. capacity:(__unused NSUInteger)numItems {
  6962. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6963. }
  6964. - (void)dealloc {
  6965. NSAssert(!_autocreator,
  6966. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6967. [self class], _autocreator);
  6968. [_dictionary release];
  6969. [super dealloc];
  6970. }
  6971. - (instancetype)copyWithZone:(NSZone *)zone {
  6972. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6973. }
  6974. - (BOOL)isEqual:(id)other {
  6975. if (self == other) {
  6976. return YES;
  6977. }
  6978. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  6979. return NO;
  6980. }
  6981. GPBInt64EnumDictionary *otherDictionary = other;
  6982. return [_dictionary isEqual:otherDictionary->_dictionary];
  6983. }
  6984. - (NSUInteger)hash {
  6985. return _dictionary.count;
  6986. }
  6987. - (NSString *)description {
  6988. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6989. }
  6990. - (NSUInteger)count {
  6991. return _dictionary.count;
  6992. }
  6993. - (void)enumerateKeysAndRawValuesUsingBlock:
  6994. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  6995. BOOL stop = NO;
  6996. NSDictionary *internal = _dictionary;
  6997. NSEnumerator *keys = [internal keyEnumerator];
  6998. NSNumber *aKey;
  6999. while ((aKey = [keys nextObject])) {
  7000. NSNumber *aValue = internal[aKey];
  7001. block([aKey longLongValue], [aValue intValue], &stop);
  7002. if (stop) {
  7003. break;
  7004. }
  7005. }
  7006. }
  7007. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7008. NSDictionary *internal = _dictionary;
  7009. NSUInteger count = internal.count;
  7010. if (count == 0) {
  7011. return 0;
  7012. }
  7013. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7014. GPBDataType keyDataType = field.mapKeyDataType;
  7015. size_t result = 0;
  7016. NSEnumerator *keys = [internal keyEnumerator];
  7017. NSNumber *aKey;
  7018. while ((aKey = [keys nextObject])) {
  7019. NSNumber *aValue = internal[aKey];
  7020. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7021. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7022. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7023. }
  7024. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7025. result += tagSize * count;
  7026. return result;
  7027. }
  7028. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7029. asField:(GPBFieldDescriptor *)field {
  7030. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7031. GPBDataType keyDataType = field.mapKeyDataType;
  7032. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7033. NSDictionary *internal = _dictionary;
  7034. NSEnumerator *keys = [internal keyEnumerator];
  7035. NSNumber *aKey;
  7036. while ((aKey = [keys nextObject])) {
  7037. NSNumber *aValue = internal[aKey];
  7038. [outputStream writeInt32NoTag:tag];
  7039. // Write the size of the message.
  7040. int64_t unwrappedKey = [aKey longLongValue];
  7041. int32_t unwrappedValue = [aValue intValue];
  7042. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7043. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7044. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7045. // Write the fields.
  7046. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7047. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7048. }
  7049. }
  7050. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7051. forKey:(GPBGenericValue *)key
  7052. keyDataType:(GPBDataType)keyDataType {
  7053. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7054. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7055. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7056. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7057. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7058. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7059. [outputStream flush];
  7060. [outputStream release];
  7061. return data;
  7062. }
  7063. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7064. forGPBGenericValueKey:(GPBGenericValue *)key {
  7065. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7066. }
  7067. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7068. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, __unused BOOL *stop) {
  7069. block([NSString stringWithFormat:@"%lld", key], @(value));
  7070. }];
  7071. }
  7072. - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
  7073. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7074. if (wrapped && value) {
  7075. int32_t result = [wrapped intValue];
  7076. if (!_validationFunc(result)) {
  7077. result = kGPBUnrecognizedEnumeratorValue;
  7078. }
  7079. *value = result;
  7080. }
  7081. return (wrapped != NULL);
  7082. }
  7083. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
  7084. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7085. if (wrapped && rawValue) {
  7086. *rawValue = [wrapped intValue];
  7087. }
  7088. return (wrapped != NULL);
  7089. }
  7090. - (void)enumerateKeysAndEnumsUsingBlock:
  7091. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  7092. GPBEnumValidationFunc func = _validationFunc;
  7093. BOOL stop = NO;
  7094. NSEnumerator *keys = [_dictionary keyEnumerator];
  7095. NSNumber *aKey;
  7096. while ((aKey = [keys nextObject])) {
  7097. NSNumber *aValue = _dictionary[aKey];
  7098. int32_t unwrapped = [aValue intValue];
  7099. if (!func(unwrapped)) {
  7100. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7101. }
  7102. block([aKey longLongValue], unwrapped, &stop);
  7103. if (stop) {
  7104. break;
  7105. }
  7106. }
  7107. }
  7108. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7109. if (otherDictionary) {
  7110. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7111. if (_autocreator) {
  7112. GPBAutocreatedDictionaryModified(_autocreator, self);
  7113. }
  7114. }
  7115. }
  7116. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7117. [_dictionary setObject:@(value) forKey:@(key)];
  7118. if (_autocreator) {
  7119. GPBAutocreatedDictionaryModified(_autocreator, self);
  7120. }
  7121. }
  7122. - (void)removeEnumForKey:(int64_t)aKey {
  7123. [_dictionary removeObjectForKey:@(aKey)];
  7124. }
  7125. - (void)removeAll {
  7126. [_dictionary removeAllObjects];
  7127. }
  7128. - (void)setEnum:(int32_t)value forKey:(int64_t)key {
  7129. if (!_validationFunc(value)) {
  7130. [NSException raise:NSInvalidArgumentException
  7131. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7132. value];
  7133. }
  7134. [_dictionary setObject:@(value) forKey:@(key)];
  7135. if (_autocreator) {
  7136. GPBAutocreatedDictionaryModified(_autocreator, self);
  7137. }
  7138. }
  7139. @end
  7140. #pragma mark - Int64 -> Object
  7141. @implementation GPBInt64ObjectDictionary {
  7142. @package
  7143. NSMutableDictionary *_dictionary;
  7144. }
  7145. - (instancetype)init {
  7146. return [self initWithObjects:NULL forKeys:NULL count:0];
  7147. }
  7148. - (instancetype)initWithObjects:(const id [])objects
  7149. forKeys:(const int64_t [])keys
  7150. count:(NSUInteger)count {
  7151. self = [super init];
  7152. if (self) {
  7153. _dictionary = [[NSMutableDictionary alloc] init];
  7154. if (count && objects && keys) {
  7155. for (NSUInteger i = 0; i < count; ++i) {
  7156. if (!objects[i]) {
  7157. [NSException raise:NSInvalidArgumentException
  7158. format:@"Attempting to add nil object to a Dictionary"];
  7159. }
  7160. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  7161. }
  7162. }
  7163. }
  7164. return self;
  7165. }
  7166. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7167. self = [self initWithObjects:NULL forKeys:NULL count:0];
  7168. if (self) {
  7169. if (dictionary) {
  7170. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7171. }
  7172. }
  7173. return self;
  7174. }
  7175. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7176. return [self initWithObjects:NULL forKeys:NULL count:0];
  7177. }
  7178. - (void)dealloc {
  7179. NSAssert(!_autocreator,
  7180. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7181. [self class], _autocreator);
  7182. [_dictionary release];
  7183. [super dealloc];
  7184. }
  7185. - (instancetype)copyWithZone:(NSZone *)zone {
  7186. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  7187. }
  7188. - (BOOL)isEqual:(id)other {
  7189. if (self == other) {
  7190. return YES;
  7191. }
  7192. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  7193. return NO;
  7194. }
  7195. GPBInt64ObjectDictionary *otherDictionary = other;
  7196. return [_dictionary isEqual:otherDictionary->_dictionary];
  7197. }
  7198. - (NSUInteger)hash {
  7199. return _dictionary.count;
  7200. }
  7201. - (NSString *)description {
  7202. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7203. }
  7204. - (NSUInteger)count {
  7205. return _dictionary.count;
  7206. }
  7207. - (void)enumerateKeysAndObjectsUsingBlock:
  7208. (void (NS_NOESCAPE ^)(int64_t key, id object, BOOL *stop))block {
  7209. BOOL stop = NO;
  7210. NSDictionary *internal = _dictionary;
  7211. NSEnumerator *keys = [internal keyEnumerator];
  7212. NSNumber *aKey;
  7213. while ((aKey = [keys nextObject])) {
  7214. id aObject = internal[aKey];
  7215. block([aKey longLongValue], aObject, &stop);
  7216. if (stop) {
  7217. break;
  7218. }
  7219. }
  7220. }
  7221. - (BOOL)isInitialized {
  7222. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  7223. if (!msg.initialized) {
  7224. return NO;
  7225. }
  7226. }
  7227. return YES;
  7228. }
  7229. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  7230. GPBInt64ObjectDictionary *newDict =
  7231. [[GPBInt64ObjectDictionary alloc] init];
  7232. NSEnumerator *keys = [_dictionary keyEnumerator];
  7233. id aKey;
  7234. NSMutableDictionary *internalDict = newDict->_dictionary;
  7235. while ((aKey = [keys nextObject])) {
  7236. GPBMessage *msg = _dictionary[aKey];
  7237. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  7238. [internalDict setObject:copiedMsg forKey:aKey];
  7239. [copiedMsg release];
  7240. }
  7241. return newDict;
  7242. }
  7243. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7244. NSDictionary *internal = _dictionary;
  7245. NSUInteger count = internal.count;
  7246. if (count == 0) {
  7247. return 0;
  7248. }
  7249. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7250. GPBDataType keyDataType = field.mapKeyDataType;
  7251. size_t result = 0;
  7252. NSEnumerator *keys = [internal keyEnumerator];
  7253. NSNumber *aKey;
  7254. while ((aKey = [keys nextObject])) {
  7255. id aObject = internal[aKey];
  7256. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7257. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  7258. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7259. }
  7260. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7261. result += tagSize * count;
  7262. return result;
  7263. }
  7264. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7265. asField:(GPBFieldDescriptor *)field {
  7266. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7267. GPBDataType keyDataType = field.mapKeyDataType;
  7268. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7269. NSDictionary *internal = _dictionary;
  7270. NSEnumerator *keys = [internal keyEnumerator];
  7271. NSNumber *aKey;
  7272. while ((aKey = [keys nextObject])) {
  7273. id aObject = internal[aKey];
  7274. [outputStream writeInt32NoTag:tag];
  7275. // Write the size of the message.
  7276. int64_t unwrappedKey = [aKey longLongValue];
  7277. id unwrappedValue = aObject;
  7278. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7279. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7280. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7281. // Write the fields.
  7282. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7283. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7284. }
  7285. }
  7286. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7287. forGPBGenericValueKey:(GPBGenericValue *)key {
  7288. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  7289. }
  7290. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7291. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, __unused BOOL *stop) {
  7292. block([NSString stringWithFormat:@"%lld", key], object);
  7293. }];
  7294. }
  7295. - (id)objectForKey:(int64_t)key {
  7296. id result = [_dictionary objectForKey:@(key)];
  7297. return result;
  7298. }
  7299. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  7300. if (otherDictionary) {
  7301. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7302. if (_autocreator) {
  7303. GPBAutocreatedDictionaryModified(_autocreator, self);
  7304. }
  7305. }
  7306. }
  7307. - (void)setObject:(id)object forKey:(int64_t)key {
  7308. if (!object) {
  7309. [NSException raise:NSInvalidArgumentException
  7310. format:@"Attempting to add nil object to a Dictionary"];
  7311. }
  7312. [_dictionary setObject:object forKey:@(key)];
  7313. if (_autocreator) {
  7314. GPBAutocreatedDictionaryModified(_autocreator, self);
  7315. }
  7316. }
  7317. - (void)removeObjectForKey:(int64_t)aKey {
  7318. [_dictionary removeObjectForKey:@(aKey)];
  7319. }
  7320. - (void)removeAll {
  7321. [_dictionary removeAllObjects];
  7322. }
  7323. @end
  7324. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  7325. // This block of code is generated, do not edit it directly.
  7326. #pragma mark - String -> UInt32
  7327. @implementation GPBStringUInt32Dictionary {
  7328. @package
  7329. NSMutableDictionary *_dictionary;
  7330. }
  7331. - (instancetype)init {
  7332. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  7333. }
  7334. - (instancetype)initWithUInt32s:(const uint32_t [])values
  7335. forKeys:(const NSString * [])keys
  7336. count:(NSUInteger)count {
  7337. self = [super init];
  7338. if (self) {
  7339. _dictionary = [[NSMutableDictionary alloc] init];
  7340. if (count && values && keys) {
  7341. for (NSUInteger i = 0; i < count; ++i) {
  7342. if (!keys[i]) {
  7343. [NSException raise:NSInvalidArgumentException
  7344. format:@"Attempting to add nil key to a Dictionary"];
  7345. }
  7346. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7347. }
  7348. }
  7349. }
  7350. return self;
  7351. }
  7352. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  7353. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  7354. if (self) {
  7355. if (dictionary) {
  7356. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7357. }
  7358. }
  7359. return self;
  7360. }
  7361. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7362. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  7363. }
  7364. - (void)dealloc {
  7365. NSAssert(!_autocreator,
  7366. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7367. [self class], _autocreator);
  7368. [_dictionary release];
  7369. [super dealloc];
  7370. }
  7371. - (instancetype)copyWithZone:(NSZone *)zone {
  7372. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7373. }
  7374. - (BOOL)isEqual:(id)other {
  7375. if (self == other) {
  7376. return YES;
  7377. }
  7378. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  7379. return NO;
  7380. }
  7381. GPBStringUInt32Dictionary *otherDictionary = other;
  7382. return [_dictionary isEqual:otherDictionary->_dictionary];
  7383. }
  7384. - (NSUInteger)hash {
  7385. return _dictionary.count;
  7386. }
  7387. - (NSString *)description {
  7388. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7389. }
  7390. - (NSUInteger)count {
  7391. return _dictionary.count;
  7392. }
  7393. - (void)enumerateKeysAndUInt32sUsingBlock:
  7394. (void (NS_NOESCAPE ^)(NSString *key, uint32_t value, BOOL *stop))block {
  7395. BOOL stop = NO;
  7396. NSDictionary *internal = _dictionary;
  7397. NSEnumerator *keys = [internal keyEnumerator];
  7398. NSString *aKey;
  7399. while ((aKey = [keys nextObject])) {
  7400. NSNumber *aValue = internal[aKey];
  7401. block(aKey, [aValue unsignedIntValue], &stop);
  7402. if (stop) {
  7403. break;
  7404. }
  7405. }
  7406. }
  7407. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7408. NSDictionary *internal = _dictionary;
  7409. NSUInteger count = internal.count;
  7410. if (count == 0) {
  7411. return 0;
  7412. }
  7413. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7414. GPBDataType keyDataType = field.mapKeyDataType;
  7415. size_t result = 0;
  7416. NSEnumerator *keys = [internal keyEnumerator];
  7417. NSString *aKey;
  7418. while ((aKey = [keys nextObject])) {
  7419. NSNumber *aValue = internal[aKey];
  7420. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7421. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  7422. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7423. }
  7424. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7425. result += tagSize * count;
  7426. return result;
  7427. }
  7428. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7429. asField:(GPBFieldDescriptor *)field {
  7430. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7431. GPBDataType keyDataType = field.mapKeyDataType;
  7432. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7433. NSDictionary *internal = _dictionary;
  7434. NSEnumerator *keys = [internal keyEnumerator];
  7435. NSString *aKey;
  7436. while ((aKey = [keys nextObject])) {
  7437. NSNumber *aValue = internal[aKey];
  7438. [outputStream writeInt32NoTag:tag];
  7439. // Write the size of the message.
  7440. NSString *unwrappedKey = aKey;
  7441. uint32_t unwrappedValue = [aValue unsignedIntValue];
  7442. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7443. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7444. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7445. // Write the fields.
  7446. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7447. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7448. }
  7449. }
  7450. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7451. forGPBGenericValueKey:(GPBGenericValue *)key {
  7452. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  7453. }
  7454. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7455. [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, __unused BOOL *stop) {
  7456. block(key, [NSString stringWithFormat:@"%u", value]);
  7457. }];
  7458. }
  7459. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
  7460. NSNumber *wrapped = [_dictionary objectForKey:key];
  7461. if (wrapped && value) {
  7462. *value = [wrapped unsignedIntValue];
  7463. }
  7464. return (wrapped != NULL);
  7465. }
  7466. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  7467. if (otherDictionary) {
  7468. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7469. if (_autocreator) {
  7470. GPBAutocreatedDictionaryModified(_autocreator, self);
  7471. }
  7472. }
  7473. }
  7474. - (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
  7475. if (!key) {
  7476. [NSException raise:NSInvalidArgumentException
  7477. format:@"Attempting to add nil key to a Dictionary"];
  7478. }
  7479. [_dictionary setObject:@(value) forKey:key];
  7480. if (_autocreator) {
  7481. GPBAutocreatedDictionaryModified(_autocreator, self);
  7482. }
  7483. }
  7484. - (void)removeUInt32ForKey:(NSString *)aKey {
  7485. [_dictionary removeObjectForKey:aKey];
  7486. }
  7487. - (void)removeAll {
  7488. [_dictionary removeAllObjects];
  7489. }
  7490. @end
  7491. #pragma mark - String -> Int32
  7492. @implementation GPBStringInt32Dictionary {
  7493. @package
  7494. NSMutableDictionary *_dictionary;
  7495. }
  7496. - (instancetype)init {
  7497. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7498. }
  7499. - (instancetype)initWithInt32s:(const int32_t [])values
  7500. forKeys:(const NSString * [])keys
  7501. count:(NSUInteger)count {
  7502. self = [super init];
  7503. if (self) {
  7504. _dictionary = [[NSMutableDictionary alloc] init];
  7505. if (count && values && keys) {
  7506. for (NSUInteger i = 0; i < count; ++i) {
  7507. if (!keys[i]) {
  7508. [NSException raise:NSInvalidArgumentException
  7509. format:@"Attempting to add nil key to a Dictionary"];
  7510. }
  7511. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7512. }
  7513. }
  7514. }
  7515. return self;
  7516. }
  7517. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  7518. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  7519. if (self) {
  7520. if (dictionary) {
  7521. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7522. }
  7523. }
  7524. return self;
  7525. }
  7526. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7527. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7528. }
  7529. - (void)dealloc {
  7530. NSAssert(!_autocreator,
  7531. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7532. [self class], _autocreator);
  7533. [_dictionary release];
  7534. [super dealloc];
  7535. }
  7536. - (instancetype)copyWithZone:(NSZone *)zone {
  7537. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7538. }
  7539. - (BOOL)isEqual:(id)other {
  7540. if (self == other) {
  7541. return YES;
  7542. }
  7543. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  7544. return NO;
  7545. }
  7546. GPBStringInt32Dictionary *otherDictionary = other;
  7547. return [_dictionary isEqual:otherDictionary->_dictionary];
  7548. }
  7549. - (NSUInteger)hash {
  7550. return _dictionary.count;
  7551. }
  7552. - (NSString *)description {
  7553. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7554. }
  7555. - (NSUInteger)count {
  7556. return _dictionary.count;
  7557. }
  7558. - (void)enumerateKeysAndInt32sUsingBlock:
  7559. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  7560. BOOL stop = NO;
  7561. NSDictionary *internal = _dictionary;
  7562. NSEnumerator *keys = [internal keyEnumerator];
  7563. NSString *aKey;
  7564. while ((aKey = [keys nextObject])) {
  7565. NSNumber *aValue = internal[aKey];
  7566. block(aKey, [aValue intValue], &stop);
  7567. if (stop) {
  7568. break;
  7569. }
  7570. }
  7571. }
  7572. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7573. NSDictionary *internal = _dictionary;
  7574. NSUInteger count = internal.count;
  7575. if (count == 0) {
  7576. return 0;
  7577. }
  7578. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7579. GPBDataType keyDataType = field.mapKeyDataType;
  7580. size_t result = 0;
  7581. NSEnumerator *keys = [internal keyEnumerator];
  7582. NSString *aKey;
  7583. while ((aKey = [keys nextObject])) {
  7584. NSNumber *aValue = internal[aKey];
  7585. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7586. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7587. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7588. }
  7589. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7590. result += tagSize * count;
  7591. return result;
  7592. }
  7593. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7594. asField:(GPBFieldDescriptor *)field {
  7595. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7596. GPBDataType keyDataType = field.mapKeyDataType;
  7597. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7598. NSDictionary *internal = _dictionary;
  7599. NSEnumerator *keys = [internal keyEnumerator];
  7600. NSString *aKey;
  7601. while ((aKey = [keys nextObject])) {
  7602. NSNumber *aValue = internal[aKey];
  7603. [outputStream writeInt32NoTag:tag];
  7604. // Write the size of the message.
  7605. NSString *unwrappedKey = aKey;
  7606. int32_t unwrappedValue = [aValue intValue];
  7607. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7608. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7609. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7610. // Write the fields.
  7611. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7612. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7613. }
  7614. }
  7615. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7616. forGPBGenericValueKey:(GPBGenericValue *)key {
  7617. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  7618. }
  7619. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7620. [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, __unused BOOL *stop) {
  7621. block(key, [NSString stringWithFormat:@"%d", value]);
  7622. }];
  7623. }
  7624. - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
  7625. NSNumber *wrapped = [_dictionary objectForKey:key];
  7626. if (wrapped && value) {
  7627. *value = [wrapped intValue];
  7628. }
  7629. return (wrapped != NULL);
  7630. }
  7631. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  7632. if (otherDictionary) {
  7633. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7634. if (_autocreator) {
  7635. GPBAutocreatedDictionaryModified(_autocreator, self);
  7636. }
  7637. }
  7638. }
  7639. - (void)setInt32:(int32_t)value forKey:(NSString *)key {
  7640. if (!key) {
  7641. [NSException raise:NSInvalidArgumentException
  7642. format:@"Attempting to add nil key to a Dictionary"];
  7643. }
  7644. [_dictionary setObject:@(value) forKey:key];
  7645. if (_autocreator) {
  7646. GPBAutocreatedDictionaryModified(_autocreator, self);
  7647. }
  7648. }
  7649. - (void)removeInt32ForKey:(NSString *)aKey {
  7650. [_dictionary removeObjectForKey:aKey];
  7651. }
  7652. - (void)removeAll {
  7653. [_dictionary removeAllObjects];
  7654. }
  7655. @end
  7656. #pragma mark - String -> UInt64
  7657. @implementation GPBStringUInt64Dictionary {
  7658. @package
  7659. NSMutableDictionary *_dictionary;
  7660. }
  7661. - (instancetype)init {
  7662. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7663. }
  7664. - (instancetype)initWithUInt64s:(const uint64_t [])values
  7665. forKeys:(const NSString * [])keys
  7666. count:(NSUInteger)count {
  7667. self = [super init];
  7668. if (self) {
  7669. _dictionary = [[NSMutableDictionary alloc] init];
  7670. if (count && values && keys) {
  7671. for (NSUInteger i = 0; i < count; ++i) {
  7672. if (!keys[i]) {
  7673. [NSException raise:NSInvalidArgumentException
  7674. format:@"Attempting to add nil key to a Dictionary"];
  7675. }
  7676. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7677. }
  7678. }
  7679. }
  7680. return self;
  7681. }
  7682. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  7683. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  7684. if (self) {
  7685. if (dictionary) {
  7686. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7687. }
  7688. }
  7689. return self;
  7690. }
  7691. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7692. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7693. }
  7694. - (void)dealloc {
  7695. NSAssert(!_autocreator,
  7696. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7697. [self class], _autocreator);
  7698. [_dictionary release];
  7699. [super dealloc];
  7700. }
  7701. - (instancetype)copyWithZone:(NSZone *)zone {
  7702. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  7703. }
  7704. - (BOOL)isEqual:(id)other {
  7705. if (self == other) {
  7706. return YES;
  7707. }
  7708. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  7709. return NO;
  7710. }
  7711. GPBStringUInt64Dictionary *otherDictionary = other;
  7712. return [_dictionary isEqual:otherDictionary->_dictionary];
  7713. }
  7714. - (NSUInteger)hash {
  7715. return _dictionary.count;
  7716. }
  7717. - (NSString *)description {
  7718. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7719. }
  7720. - (NSUInteger)count {
  7721. return _dictionary.count;
  7722. }
  7723. - (void)enumerateKeysAndUInt64sUsingBlock:
  7724. (void (NS_NOESCAPE ^)(NSString *key, uint64_t value, BOOL *stop))block {
  7725. BOOL stop = NO;
  7726. NSDictionary *internal = _dictionary;
  7727. NSEnumerator *keys = [internal keyEnumerator];
  7728. NSString *aKey;
  7729. while ((aKey = [keys nextObject])) {
  7730. NSNumber *aValue = internal[aKey];
  7731. block(aKey, [aValue unsignedLongLongValue], &stop);
  7732. if (stop) {
  7733. break;
  7734. }
  7735. }
  7736. }
  7737. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7738. NSDictionary *internal = _dictionary;
  7739. NSUInteger count = internal.count;
  7740. if (count == 0) {
  7741. return 0;
  7742. }
  7743. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7744. GPBDataType keyDataType = field.mapKeyDataType;
  7745. size_t result = 0;
  7746. NSEnumerator *keys = [internal keyEnumerator];
  7747. NSString *aKey;
  7748. while ((aKey = [keys nextObject])) {
  7749. NSNumber *aValue = internal[aKey];
  7750. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7751. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7752. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7753. }
  7754. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7755. result += tagSize * count;
  7756. return result;
  7757. }
  7758. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7759. asField:(GPBFieldDescriptor *)field {
  7760. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7761. GPBDataType keyDataType = field.mapKeyDataType;
  7762. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7763. NSDictionary *internal = _dictionary;
  7764. NSEnumerator *keys = [internal keyEnumerator];
  7765. NSString *aKey;
  7766. while ((aKey = [keys nextObject])) {
  7767. NSNumber *aValue = internal[aKey];
  7768. [outputStream writeInt32NoTag:tag];
  7769. // Write the size of the message.
  7770. NSString *unwrappedKey = aKey;
  7771. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  7772. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7773. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7774. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7775. // Write the fields.
  7776. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7777. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7778. }
  7779. }
  7780. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7781. forGPBGenericValueKey:(GPBGenericValue *)key {
  7782. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  7783. }
  7784. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7785. [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, __unused BOOL *stop) {
  7786. block(key, [NSString stringWithFormat:@"%llu", value]);
  7787. }];
  7788. }
  7789. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
  7790. NSNumber *wrapped = [_dictionary objectForKey:key];
  7791. if (wrapped && value) {
  7792. *value = [wrapped unsignedLongLongValue];
  7793. }
  7794. return (wrapped != NULL);
  7795. }
  7796. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  7797. if (otherDictionary) {
  7798. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7799. if (_autocreator) {
  7800. GPBAutocreatedDictionaryModified(_autocreator, self);
  7801. }
  7802. }
  7803. }
  7804. - (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
  7805. if (!key) {
  7806. [NSException raise:NSInvalidArgumentException
  7807. format:@"Attempting to add nil key to a Dictionary"];
  7808. }
  7809. [_dictionary setObject:@(value) forKey:key];
  7810. if (_autocreator) {
  7811. GPBAutocreatedDictionaryModified(_autocreator, self);
  7812. }
  7813. }
  7814. - (void)removeUInt64ForKey:(NSString *)aKey {
  7815. [_dictionary removeObjectForKey:aKey];
  7816. }
  7817. - (void)removeAll {
  7818. [_dictionary removeAllObjects];
  7819. }
  7820. @end
  7821. #pragma mark - String -> Int64
  7822. @implementation GPBStringInt64Dictionary {
  7823. @package
  7824. NSMutableDictionary *_dictionary;
  7825. }
  7826. - (instancetype)init {
  7827. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7828. }
  7829. - (instancetype)initWithInt64s:(const int64_t [])values
  7830. forKeys:(const NSString * [])keys
  7831. count:(NSUInteger)count {
  7832. self = [super init];
  7833. if (self) {
  7834. _dictionary = [[NSMutableDictionary alloc] init];
  7835. if (count && values && keys) {
  7836. for (NSUInteger i = 0; i < count; ++i) {
  7837. if (!keys[i]) {
  7838. [NSException raise:NSInvalidArgumentException
  7839. format:@"Attempting to add nil key to a Dictionary"];
  7840. }
  7841. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7842. }
  7843. }
  7844. }
  7845. return self;
  7846. }
  7847. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  7848. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  7849. if (self) {
  7850. if (dictionary) {
  7851. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7852. }
  7853. }
  7854. return self;
  7855. }
  7856. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7857. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7858. }
  7859. - (void)dealloc {
  7860. NSAssert(!_autocreator,
  7861. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7862. [self class], _autocreator);
  7863. [_dictionary release];
  7864. [super dealloc];
  7865. }
  7866. - (instancetype)copyWithZone:(NSZone *)zone {
  7867. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  7868. }
  7869. - (BOOL)isEqual:(id)other {
  7870. if (self == other) {
  7871. return YES;
  7872. }
  7873. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  7874. return NO;
  7875. }
  7876. GPBStringInt64Dictionary *otherDictionary = other;
  7877. return [_dictionary isEqual:otherDictionary->_dictionary];
  7878. }
  7879. - (NSUInteger)hash {
  7880. return _dictionary.count;
  7881. }
  7882. - (NSString *)description {
  7883. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7884. }
  7885. - (NSUInteger)count {
  7886. return _dictionary.count;
  7887. }
  7888. - (void)enumerateKeysAndInt64sUsingBlock:
  7889. (void (NS_NOESCAPE ^)(NSString *key, int64_t value, BOOL *stop))block {
  7890. BOOL stop = NO;
  7891. NSDictionary *internal = _dictionary;
  7892. NSEnumerator *keys = [internal keyEnumerator];
  7893. NSString *aKey;
  7894. while ((aKey = [keys nextObject])) {
  7895. NSNumber *aValue = internal[aKey];
  7896. block(aKey, [aValue longLongValue], &stop);
  7897. if (stop) {
  7898. break;
  7899. }
  7900. }
  7901. }
  7902. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7903. NSDictionary *internal = _dictionary;
  7904. NSUInteger count = internal.count;
  7905. if (count == 0) {
  7906. return 0;
  7907. }
  7908. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7909. GPBDataType keyDataType = field.mapKeyDataType;
  7910. size_t result = 0;
  7911. NSEnumerator *keys = [internal keyEnumerator];
  7912. NSString *aKey;
  7913. while ((aKey = [keys nextObject])) {
  7914. NSNumber *aValue = internal[aKey];
  7915. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7916. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7917. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7918. }
  7919. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7920. result += tagSize * count;
  7921. return result;
  7922. }
  7923. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7924. asField:(GPBFieldDescriptor *)field {
  7925. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7926. GPBDataType keyDataType = field.mapKeyDataType;
  7927. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7928. NSDictionary *internal = _dictionary;
  7929. NSEnumerator *keys = [internal keyEnumerator];
  7930. NSString *aKey;
  7931. while ((aKey = [keys nextObject])) {
  7932. NSNumber *aValue = internal[aKey];
  7933. [outputStream writeInt32NoTag:tag];
  7934. // Write the size of the message.
  7935. NSString *unwrappedKey = aKey;
  7936. int64_t unwrappedValue = [aValue longLongValue];
  7937. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7938. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7939. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7940. // Write the fields.
  7941. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7942. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7943. }
  7944. }
  7945. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7946. forGPBGenericValueKey:(GPBGenericValue *)key {
  7947. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  7948. }
  7949. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7950. [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, __unused BOOL *stop) {
  7951. block(key, [NSString stringWithFormat:@"%lld", value]);
  7952. }];
  7953. }
  7954. - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
  7955. NSNumber *wrapped = [_dictionary objectForKey:key];
  7956. if (wrapped && value) {
  7957. *value = [wrapped longLongValue];
  7958. }
  7959. return (wrapped != NULL);
  7960. }
  7961. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  7962. if (otherDictionary) {
  7963. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7964. if (_autocreator) {
  7965. GPBAutocreatedDictionaryModified(_autocreator, self);
  7966. }
  7967. }
  7968. }
  7969. - (void)setInt64:(int64_t)value forKey:(NSString *)key {
  7970. if (!key) {
  7971. [NSException raise:NSInvalidArgumentException
  7972. format:@"Attempting to add nil key to a Dictionary"];
  7973. }
  7974. [_dictionary setObject:@(value) forKey:key];
  7975. if (_autocreator) {
  7976. GPBAutocreatedDictionaryModified(_autocreator, self);
  7977. }
  7978. }
  7979. - (void)removeInt64ForKey:(NSString *)aKey {
  7980. [_dictionary removeObjectForKey:aKey];
  7981. }
  7982. - (void)removeAll {
  7983. [_dictionary removeAllObjects];
  7984. }
  7985. @end
  7986. #pragma mark - String -> Bool
  7987. @implementation GPBStringBoolDictionary {
  7988. @package
  7989. NSMutableDictionary *_dictionary;
  7990. }
  7991. - (instancetype)init {
  7992. return [self initWithBools:NULL forKeys:NULL count:0];
  7993. }
  7994. - (instancetype)initWithBools:(const BOOL [])values
  7995. forKeys:(const NSString * [])keys
  7996. count:(NSUInteger)count {
  7997. self = [super init];
  7998. if (self) {
  7999. _dictionary = [[NSMutableDictionary alloc] init];
  8000. if (count && values && keys) {
  8001. for (NSUInteger i = 0; i < count; ++i) {
  8002. if (!keys[i]) {
  8003. [NSException raise:NSInvalidArgumentException
  8004. format:@"Attempting to add nil key to a Dictionary"];
  8005. }
  8006. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8007. }
  8008. }
  8009. }
  8010. return self;
  8011. }
  8012. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8013. self = [self initWithBools:NULL forKeys:NULL count:0];
  8014. if (self) {
  8015. if (dictionary) {
  8016. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8017. }
  8018. }
  8019. return self;
  8020. }
  8021. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8022. return [self initWithBools:NULL forKeys:NULL count:0];
  8023. }
  8024. - (void)dealloc {
  8025. NSAssert(!_autocreator,
  8026. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8027. [self class], _autocreator);
  8028. [_dictionary release];
  8029. [super dealloc];
  8030. }
  8031. - (instancetype)copyWithZone:(NSZone *)zone {
  8032. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8033. }
  8034. - (BOOL)isEqual:(id)other {
  8035. if (self == other) {
  8036. return YES;
  8037. }
  8038. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8039. return NO;
  8040. }
  8041. GPBStringBoolDictionary *otherDictionary = other;
  8042. return [_dictionary isEqual:otherDictionary->_dictionary];
  8043. }
  8044. - (NSUInteger)hash {
  8045. return _dictionary.count;
  8046. }
  8047. - (NSString *)description {
  8048. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8049. }
  8050. - (NSUInteger)count {
  8051. return _dictionary.count;
  8052. }
  8053. - (void)enumerateKeysAndBoolsUsingBlock:
  8054. (void (NS_NOESCAPE ^)(NSString *key, BOOL value, BOOL *stop))block {
  8055. BOOL stop = NO;
  8056. NSDictionary *internal = _dictionary;
  8057. NSEnumerator *keys = [internal keyEnumerator];
  8058. NSString *aKey;
  8059. while ((aKey = [keys nextObject])) {
  8060. NSNumber *aValue = internal[aKey];
  8061. block(aKey, [aValue boolValue], &stop);
  8062. if (stop) {
  8063. break;
  8064. }
  8065. }
  8066. }
  8067. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8068. NSDictionary *internal = _dictionary;
  8069. NSUInteger count = internal.count;
  8070. if (count == 0) {
  8071. return 0;
  8072. }
  8073. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8074. GPBDataType keyDataType = field.mapKeyDataType;
  8075. size_t result = 0;
  8076. NSEnumerator *keys = [internal keyEnumerator];
  8077. NSString *aKey;
  8078. while ((aKey = [keys nextObject])) {
  8079. NSNumber *aValue = internal[aKey];
  8080. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8081. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8082. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8083. }
  8084. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8085. result += tagSize * count;
  8086. return result;
  8087. }
  8088. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8089. asField:(GPBFieldDescriptor *)field {
  8090. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8091. GPBDataType keyDataType = field.mapKeyDataType;
  8092. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8093. NSDictionary *internal = _dictionary;
  8094. NSEnumerator *keys = [internal keyEnumerator];
  8095. NSString *aKey;
  8096. while ((aKey = [keys nextObject])) {
  8097. NSNumber *aValue = internal[aKey];
  8098. [outputStream writeInt32NoTag:tag];
  8099. // Write the size of the message.
  8100. NSString *unwrappedKey = aKey;
  8101. BOOL unwrappedValue = [aValue boolValue];
  8102. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8103. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8104. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8105. // Write the fields.
  8106. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8107. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8108. }
  8109. }
  8110. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8111. forGPBGenericValueKey:(GPBGenericValue *)key {
  8112. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8113. }
  8114. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8115. [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, __unused BOOL *stop) {
  8116. block(key, (value ? @"true" : @"false"));
  8117. }];
  8118. }
  8119. - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
  8120. NSNumber *wrapped = [_dictionary objectForKey:key];
  8121. if (wrapped && value) {
  8122. *value = [wrapped boolValue];
  8123. }
  8124. return (wrapped != NULL);
  8125. }
  8126. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  8127. if (otherDictionary) {
  8128. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8129. if (_autocreator) {
  8130. GPBAutocreatedDictionaryModified(_autocreator, self);
  8131. }
  8132. }
  8133. }
  8134. - (void)setBool:(BOOL)value forKey:(NSString *)key {
  8135. if (!key) {
  8136. [NSException raise:NSInvalidArgumentException
  8137. format:@"Attempting to add nil key to a Dictionary"];
  8138. }
  8139. [_dictionary setObject:@(value) forKey:key];
  8140. if (_autocreator) {
  8141. GPBAutocreatedDictionaryModified(_autocreator, self);
  8142. }
  8143. }
  8144. - (void)removeBoolForKey:(NSString *)aKey {
  8145. [_dictionary removeObjectForKey:aKey];
  8146. }
  8147. - (void)removeAll {
  8148. [_dictionary removeAllObjects];
  8149. }
  8150. @end
  8151. #pragma mark - String -> Float
  8152. @implementation GPBStringFloatDictionary {
  8153. @package
  8154. NSMutableDictionary *_dictionary;
  8155. }
  8156. - (instancetype)init {
  8157. return [self initWithFloats:NULL forKeys:NULL count:0];
  8158. }
  8159. - (instancetype)initWithFloats:(const float [])values
  8160. forKeys:(const NSString * [])keys
  8161. count:(NSUInteger)count {
  8162. self = [super init];
  8163. if (self) {
  8164. _dictionary = [[NSMutableDictionary alloc] init];
  8165. if (count && values && keys) {
  8166. for (NSUInteger i = 0; i < count; ++i) {
  8167. if (!keys[i]) {
  8168. [NSException raise:NSInvalidArgumentException
  8169. format:@"Attempting to add nil key to a Dictionary"];
  8170. }
  8171. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8172. }
  8173. }
  8174. }
  8175. return self;
  8176. }
  8177. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  8178. self = [self initWithFloats:NULL forKeys:NULL count:0];
  8179. if (self) {
  8180. if (dictionary) {
  8181. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8182. }
  8183. }
  8184. return self;
  8185. }
  8186. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8187. return [self initWithFloats:NULL forKeys:NULL count:0];
  8188. }
  8189. - (void)dealloc {
  8190. NSAssert(!_autocreator,
  8191. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8192. [self class], _autocreator);
  8193. [_dictionary release];
  8194. [super dealloc];
  8195. }
  8196. - (instancetype)copyWithZone:(NSZone *)zone {
  8197. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  8198. }
  8199. - (BOOL)isEqual:(id)other {
  8200. if (self == other) {
  8201. return YES;
  8202. }
  8203. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  8204. return NO;
  8205. }
  8206. GPBStringFloatDictionary *otherDictionary = other;
  8207. return [_dictionary isEqual:otherDictionary->_dictionary];
  8208. }
  8209. - (NSUInteger)hash {
  8210. return _dictionary.count;
  8211. }
  8212. - (NSString *)description {
  8213. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8214. }
  8215. - (NSUInteger)count {
  8216. return _dictionary.count;
  8217. }
  8218. - (void)enumerateKeysAndFloatsUsingBlock:
  8219. (void (NS_NOESCAPE ^)(NSString *key, float value, BOOL *stop))block {
  8220. BOOL stop = NO;
  8221. NSDictionary *internal = _dictionary;
  8222. NSEnumerator *keys = [internal keyEnumerator];
  8223. NSString *aKey;
  8224. while ((aKey = [keys nextObject])) {
  8225. NSNumber *aValue = internal[aKey];
  8226. block(aKey, [aValue floatValue], &stop);
  8227. if (stop) {
  8228. break;
  8229. }
  8230. }
  8231. }
  8232. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8233. NSDictionary *internal = _dictionary;
  8234. NSUInteger count = internal.count;
  8235. if (count == 0) {
  8236. return 0;
  8237. }
  8238. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8239. GPBDataType keyDataType = field.mapKeyDataType;
  8240. size_t result = 0;
  8241. NSEnumerator *keys = [internal keyEnumerator];
  8242. NSString *aKey;
  8243. while ((aKey = [keys nextObject])) {
  8244. NSNumber *aValue = internal[aKey];
  8245. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8246. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  8247. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8248. }
  8249. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8250. result += tagSize * count;
  8251. return result;
  8252. }
  8253. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8254. asField:(GPBFieldDescriptor *)field {
  8255. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8256. GPBDataType keyDataType = field.mapKeyDataType;
  8257. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8258. NSDictionary *internal = _dictionary;
  8259. NSEnumerator *keys = [internal keyEnumerator];
  8260. NSString *aKey;
  8261. while ((aKey = [keys nextObject])) {
  8262. NSNumber *aValue = internal[aKey];
  8263. [outputStream writeInt32NoTag:tag];
  8264. // Write the size of the message.
  8265. NSString *unwrappedKey = aKey;
  8266. float unwrappedValue = [aValue floatValue];
  8267. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8268. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8269. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8270. // Write the fields.
  8271. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8272. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8273. }
  8274. }
  8275. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8276. forGPBGenericValueKey:(GPBGenericValue *)key {
  8277. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  8278. }
  8279. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8280. [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, __unused BOOL *stop) {
  8281. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  8282. }];
  8283. }
  8284. - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
  8285. NSNumber *wrapped = [_dictionary objectForKey:key];
  8286. if (wrapped && value) {
  8287. *value = [wrapped floatValue];
  8288. }
  8289. return (wrapped != NULL);
  8290. }
  8291. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  8292. if (otherDictionary) {
  8293. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8294. if (_autocreator) {
  8295. GPBAutocreatedDictionaryModified(_autocreator, self);
  8296. }
  8297. }
  8298. }
  8299. - (void)setFloat:(float)value forKey:(NSString *)key {
  8300. if (!key) {
  8301. [NSException raise:NSInvalidArgumentException
  8302. format:@"Attempting to add nil key to a Dictionary"];
  8303. }
  8304. [_dictionary setObject:@(value) forKey:key];
  8305. if (_autocreator) {
  8306. GPBAutocreatedDictionaryModified(_autocreator, self);
  8307. }
  8308. }
  8309. - (void)removeFloatForKey:(NSString *)aKey {
  8310. [_dictionary removeObjectForKey:aKey];
  8311. }
  8312. - (void)removeAll {
  8313. [_dictionary removeAllObjects];
  8314. }
  8315. @end
  8316. #pragma mark - String -> Double
  8317. @implementation GPBStringDoubleDictionary {
  8318. @package
  8319. NSMutableDictionary *_dictionary;
  8320. }
  8321. - (instancetype)init {
  8322. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8323. }
  8324. - (instancetype)initWithDoubles:(const double [])values
  8325. forKeys:(const NSString * [])keys
  8326. count:(NSUInteger)count {
  8327. self = [super init];
  8328. if (self) {
  8329. _dictionary = [[NSMutableDictionary alloc] init];
  8330. if (count && values && keys) {
  8331. for (NSUInteger i = 0; i < count; ++i) {
  8332. if (!keys[i]) {
  8333. [NSException raise:NSInvalidArgumentException
  8334. format:@"Attempting to add nil key to a Dictionary"];
  8335. }
  8336. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8337. }
  8338. }
  8339. }
  8340. return self;
  8341. }
  8342. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  8343. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  8344. if (self) {
  8345. if (dictionary) {
  8346. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8347. }
  8348. }
  8349. return self;
  8350. }
  8351. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8352. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8353. }
  8354. - (void)dealloc {
  8355. NSAssert(!_autocreator,
  8356. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8357. [self class], _autocreator);
  8358. [_dictionary release];
  8359. [super dealloc];
  8360. }
  8361. - (instancetype)copyWithZone:(NSZone *)zone {
  8362. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  8363. }
  8364. - (BOOL)isEqual:(id)other {
  8365. if (self == other) {
  8366. return YES;
  8367. }
  8368. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  8369. return NO;
  8370. }
  8371. GPBStringDoubleDictionary *otherDictionary = other;
  8372. return [_dictionary isEqual:otherDictionary->_dictionary];
  8373. }
  8374. - (NSUInteger)hash {
  8375. return _dictionary.count;
  8376. }
  8377. - (NSString *)description {
  8378. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8379. }
  8380. - (NSUInteger)count {
  8381. return _dictionary.count;
  8382. }
  8383. - (void)enumerateKeysAndDoublesUsingBlock:
  8384. (void (NS_NOESCAPE ^)(NSString *key, double value, BOOL *stop))block {
  8385. BOOL stop = NO;
  8386. NSDictionary *internal = _dictionary;
  8387. NSEnumerator *keys = [internal keyEnumerator];
  8388. NSString *aKey;
  8389. while ((aKey = [keys nextObject])) {
  8390. NSNumber *aValue = internal[aKey];
  8391. block(aKey, [aValue doubleValue], &stop);
  8392. if (stop) {
  8393. break;
  8394. }
  8395. }
  8396. }
  8397. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8398. NSDictionary *internal = _dictionary;
  8399. NSUInteger count = internal.count;
  8400. if (count == 0) {
  8401. return 0;
  8402. }
  8403. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8404. GPBDataType keyDataType = field.mapKeyDataType;
  8405. size_t result = 0;
  8406. NSEnumerator *keys = [internal keyEnumerator];
  8407. NSString *aKey;
  8408. while ((aKey = [keys nextObject])) {
  8409. NSNumber *aValue = internal[aKey];
  8410. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8411. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  8412. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8413. }
  8414. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8415. result += tagSize * count;
  8416. return result;
  8417. }
  8418. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8419. asField:(GPBFieldDescriptor *)field {
  8420. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8421. GPBDataType keyDataType = field.mapKeyDataType;
  8422. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8423. NSDictionary *internal = _dictionary;
  8424. NSEnumerator *keys = [internal keyEnumerator];
  8425. NSString *aKey;
  8426. while ((aKey = [keys nextObject])) {
  8427. NSNumber *aValue = internal[aKey];
  8428. [outputStream writeInt32NoTag:tag];
  8429. // Write the size of the message.
  8430. NSString *unwrappedKey = aKey;
  8431. double unwrappedValue = [aValue doubleValue];
  8432. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8433. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8434. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8435. // Write the fields.
  8436. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8437. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8438. }
  8439. }
  8440. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8441. forGPBGenericValueKey:(GPBGenericValue *)key {
  8442. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  8443. }
  8444. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8445. [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, __unused BOOL *stop) {
  8446. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  8447. }];
  8448. }
  8449. - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
  8450. NSNumber *wrapped = [_dictionary objectForKey:key];
  8451. if (wrapped && value) {
  8452. *value = [wrapped doubleValue];
  8453. }
  8454. return (wrapped != NULL);
  8455. }
  8456. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  8457. if (otherDictionary) {
  8458. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8459. if (_autocreator) {
  8460. GPBAutocreatedDictionaryModified(_autocreator, self);
  8461. }
  8462. }
  8463. }
  8464. - (void)setDouble:(double)value forKey:(NSString *)key {
  8465. if (!key) {
  8466. [NSException raise:NSInvalidArgumentException
  8467. format:@"Attempting to add nil key to a Dictionary"];
  8468. }
  8469. [_dictionary setObject:@(value) forKey:key];
  8470. if (_autocreator) {
  8471. GPBAutocreatedDictionaryModified(_autocreator, self);
  8472. }
  8473. }
  8474. - (void)removeDoubleForKey:(NSString *)aKey {
  8475. [_dictionary removeObjectForKey:aKey];
  8476. }
  8477. - (void)removeAll {
  8478. [_dictionary removeAllObjects];
  8479. }
  8480. @end
  8481. #pragma mark - String -> Enum
  8482. @implementation GPBStringEnumDictionary {
  8483. @package
  8484. NSMutableDictionary *_dictionary;
  8485. GPBEnumValidationFunc _validationFunc;
  8486. }
  8487. @synthesize validationFunc = _validationFunc;
  8488. - (instancetype)init {
  8489. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  8490. }
  8491. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  8492. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8493. }
  8494. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8495. rawValues:(const int32_t [])rawValues
  8496. forKeys:(const NSString * [])keys
  8497. count:(NSUInteger)count {
  8498. self = [super init];
  8499. if (self) {
  8500. _dictionary = [[NSMutableDictionary alloc] init];
  8501. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  8502. if (count && rawValues && keys) {
  8503. for (NSUInteger i = 0; i < count; ++i) {
  8504. if (!keys[i]) {
  8505. [NSException raise:NSInvalidArgumentException
  8506. format:@"Attempting to add nil key to a Dictionary"];
  8507. }
  8508. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  8509. }
  8510. }
  8511. }
  8512. return self;
  8513. }
  8514. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  8515. self = [self initWithValidationFunction:dictionary.validationFunc
  8516. rawValues:NULL
  8517. forKeys:NULL
  8518. count:0];
  8519. if (self) {
  8520. if (dictionary) {
  8521. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8522. }
  8523. }
  8524. return self;
  8525. }
  8526. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8527. capacity:(__unused NSUInteger)numItems {
  8528. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8529. }
  8530. - (void)dealloc {
  8531. NSAssert(!_autocreator,
  8532. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8533. [self class], _autocreator);
  8534. [_dictionary release];
  8535. [super dealloc];
  8536. }
  8537. - (instancetype)copyWithZone:(NSZone *)zone {
  8538. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  8539. }
  8540. - (BOOL)isEqual:(id)other {
  8541. if (self == other) {
  8542. return YES;
  8543. }
  8544. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  8545. return NO;
  8546. }
  8547. GPBStringEnumDictionary *otherDictionary = other;
  8548. return [_dictionary isEqual:otherDictionary->_dictionary];
  8549. }
  8550. - (NSUInteger)hash {
  8551. return _dictionary.count;
  8552. }
  8553. - (NSString *)description {
  8554. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8555. }
  8556. - (NSUInteger)count {
  8557. return _dictionary.count;
  8558. }
  8559. - (void)enumerateKeysAndRawValuesUsingBlock:
  8560. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  8561. BOOL stop = NO;
  8562. NSDictionary *internal = _dictionary;
  8563. NSEnumerator *keys = [internal keyEnumerator];
  8564. NSString *aKey;
  8565. while ((aKey = [keys nextObject])) {
  8566. NSNumber *aValue = internal[aKey];
  8567. block(aKey, [aValue intValue], &stop);
  8568. if (stop) {
  8569. break;
  8570. }
  8571. }
  8572. }
  8573. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8574. NSDictionary *internal = _dictionary;
  8575. NSUInteger count = internal.count;
  8576. if (count == 0) {
  8577. return 0;
  8578. }
  8579. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8580. GPBDataType keyDataType = field.mapKeyDataType;
  8581. size_t result = 0;
  8582. NSEnumerator *keys = [internal keyEnumerator];
  8583. NSString *aKey;
  8584. while ((aKey = [keys nextObject])) {
  8585. NSNumber *aValue = internal[aKey];
  8586. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8587. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8588. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8589. }
  8590. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8591. result += tagSize * count;
  8592. return result;
  8593. }
  8594. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8595. asField:(GPBFieldDescriptor *)field {
  8596. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8597. GPBDataType keyDataType = field.mapKeyDataType;
  8598. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8599. NSDictionary *internal = _dictionary;
  8600. NSEnumerator *keys = [internal keyEnumerator];
  8601. NSString *aKey;
  8602. while ((aKey = [keys nextObject])) {
  8603. NSNumber *aValue = internal[aKey];
  8604. [outputStream writeInt32NoTag:tag];
  8605. // Write the size of the message.
  8606. NSString *unwrappedKey = aKey;
  8607. int32_t unwrappedValue = [aValue intValue];
  8608. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8609. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8610. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8611. // Write the fields.
  8612. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8613. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8614. }
  8615. }
  8616. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  8617. forKey:(GPBGenericValue *)key
  8618. keyDataType:(GPBDataType)keyDataType {
  8619. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  8620. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  8621. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  8622. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  8623. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  8624. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  8625. [outputStream flush];
  8626. [outputStream release];
  8627. return data;
  8628. }
  8629. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8630. forGPBGenericValueKey:(GPBGenericValue *)key {
  8631. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  8632. }
  8633. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8634. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, __unused BOOL *stop) {
  8635. block(key, @(value));
  8636. }];
  8637. }
  8638. - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
  8639. NSNumber *wrapped = [_dictionary objectForKey:key];
  8640. if (wrapped && value) {
  8641. int32_t result = [wrapped intValue];
  8642. if (!_validationFunc(result)) {
  8643. result = kGPBUnrecognizedEnumeratorValue;
  8644. }
  8645. *value = result;
  8646. }
  8647. return (wrapped != NULL);
  8648. }
  8649. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
  8650. NSNumber *wrapped = [_dictionary objectForKey:key];
  8651. if (wrapped && rawValue) {
  8652. *rawValue = [wrapped intValue];
  8653. }
  8654. return (wrapped != NULL);
  8655. }
  8656. - (void)enumerateKeysAndEnumsUsingBlock:
  8657. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  8658. GPBEnumValidationFunc func = _validationFunc;
  8659. BOOL stop = NO;
  8660. NSEnumerator *keys = [_dictionary keyEnumerator];
  8661. NSString *aKey;
  8662. while ((aKey = [keys nextObject])) {
  8663. NSNumber *aValue = _dictionary[aKey];
  8664. int32_t unwrapped = [aValue intValue];
  8665. if (!func(unwrapped)) {
  8666. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8667. }
  8668. block(aKey, unwrapped, &stop);
  8669. if (stop) {
  8670. break;
  8671. }
  8672. }
  8673. }
  8674. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  8675. if (otherDictionary) {
  8676. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8677. if (_autocreator) {
  8678. GPBAutocreatedDictionaryModified(_autocreator, self);
  8679. }
  8680. }
  8681. }
  8682. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  8683. if (!key) {
  8684. [NSException raise:NSInvalidArgumentException
  8685. format:@"Attempting to add nil key to a Dictionary"];
  8686. }
  8687. [_dictionary setObject:@(value) forKey:key];
  8688. if (_autocreator) {
  8689. GPBAutocreatedDictionaryModified(_autocreator, self);
  8690. }
  8691. }
  8692. - (void)removeEnumForKey:(NSString *)aKey {
  8693. [_dictionary removeObjectForKey:aKey];
  8694. }
  8695. - (void)removeAll {
  8696. [_dictionary removeAllObjects];
  8697. }
  8698. - (void)setEnum:(int32_t)value forKey:(NSString *)key {
  8699. if (!key) {
  8700. [NSException raise:NSInvalidArgumentException
  8701. format:@"Attempting to add nil key to a Dictionary"];
  8702. }
  8703. if (!_validationFunc(value)) {
  8704. [NSException raise:NSInvalidArgumentException
  8705. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  8706. value];
  8707. }
  8708. [_dictionary setObject:@(value) forKey:key];
  8709. if (_autocreator) {
  8710. GPBAutocreatedDictionaryModified(_autocreator, self);
  8711. }
  8712. }
  8713. @end
  8714. //%PDDM-EXPAND-END (5 expansions)
  8715. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  8716. // This block of code is generated, do not edit it directly.
  8717. #pragma mark - Bool -> UInt32
  8718. @implementation GPBBoolUInt32Dictionary {
  8719. @package
  8720. uint32_t _values[2];
  8721. BOOL _valueSet[2];
  8722. }
  8723. - (instancetype)init {
  8724. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8725. }
  8726. - (instancetype)initWithUInt32s:(const uint32_t [])values
  8727. forKeys:(const BOOL [])keys
  8728. count:(NSUInteger)count {
  8729. self = [super init];
  8730. if (self) {
  8731. for (NSUInteger i = 0; i < count; ++i) {
  8732. int idx = keys[i] ? 1 : 0;
  8733. _values[idx] = values[i];
  8734. _valueSet[idx] = YES;
  8735. }
  8736. }
  8737. return self;
  8738. }
  8739. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  8740. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  8741. if (self) {
  8742. if (dictionary) {
  8743. for (int i = 0; i < 2; ++i) {
  8744. if (dictionary->_valueSet[i]) {
  8745. _values[i] = dictionary->_values[i];
  8746. _valueSet[i] = YES;
  8747. }
  8748. }
  8749. }
  8750. }
  8751. return self;
  8752. }
  8753. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8754. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8755. }
  8756. #if !defined(NS_BLOCK_ASSERTIONS)
  8757. - (void)dealloc {
  8758. NSAssert(!_autocreator,
  8759. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8760. [self class], _autocreator);
  8761. [super dealloc];
  8762. }
  8763. #endif // !defined(NS_BLOCK_ASSERTIONS)
  8764. - (instancetype)copyWithZone:(NSZone *)zone {
  8765. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8766. }
  8767. - (BOOL)isEqual:(id)other {
  8768. if (self == other) {
  8769. return YES;
  8770. }
  8771. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  8772. return NO;
  8773. }
  8774. GPBBoolUInt32Dictionary *otherDictionary = other;
  8775. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  8776. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  8777. return NO;
  8778. }
  8779. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  8780. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  8781. return NO;
  8782. }
  8783. return YES;
  8784. }
  8785. - (NSUInteger)hash {
  8786. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8787. }
  8788. - (NSString *)description {
  8789. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  8790. if (_valueSet[0]) {
  8791. [result appendFormat:@"NO: %u", _values[0]];
  8792. }
  8793. if (_valueSet[1]) {
  8794. [result appendFormat:@"YES: %u", _values[1]];
  8795. }
  8796. [result appendString:@" }"];
  8797. return result;
  8798. }
  8799. - (NSUInteger)count {
  8800. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8801. }
  8802. - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
  8803. int idx = (key ? 1 : 0);
  8804. if (_valueSet[idx]) {
  8805. if (value) {
  8806. *value = _values[idx];
  8807. }
  8808. return YES;
  8809. }
  8810. return NO;
  8811. }
  8812. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8813. forGPBGenericValueKey:(GPBGenericValue *)key {
  8814. int idx = (key->valueBool ? 1 : 0);
  8815. _values[idx] = value->valueUInt32;
  8816. _valueSet[idx] = YES;
  8817. }
  8818. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8819. if (_valueSet[0]) {
  8820. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  8821. }
  8822. if (_valueSet[1]) {
  8823. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  8824. }
  8825. }
  8826. - (void)enumerateKeysAndUInt32sUsingBlock:
  8827. (void (NS_NOESCAPE ^)(BOOL key, uint32_t value, BOOL *stop))block {
  8828. BOOL stop = NO;
  8829. if (_valueSet[0]) {
  8830. block(NO, _values[0], &stop);
  8831. }
  8832. if (!stop && _valueSet[1]) {
  8833. block(YES, _values[1], &stop);
  8834. }
  8835. }
  8836. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8837. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8838. NSUInteger count = 0;
  8839. size_t result = 0;
  8840. for (int i = 0; i < 2; ++i) {
  8841. if (_valueSet[i]) {
  8842. ++count;
  8843. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  8844. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  8845. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8846. }
  8847. }
  8848. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8849. result += tagSize * count;
  8850. return result;
  8851. }
  8852. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8853. asField:(GPBFieldDescriptor *)field {
  8854. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8855. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8856. for (int i = 0; i < 2; ++i) {
  8857. if (_valueSet[i]) {
  8858. // Write the tag.
  8859. [outputStream writeInt32NoTag:tag];
  8860. // Write the size of the message.
  8861. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  8862. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  8863. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8864. // Write the fields.
  8865. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  8866. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  8867. }
  8868. }
  8869. }
  8870. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  8871. if (otherDictionary) {
  8872. for (int i = 0; i < 2; ++i) {
  8873. if (otherDictionary->_valueSet[i]) {
  8874. _valueSet[i] = YES;
  8875. _values[i] = otherDictionary->_values[i];
  8876. }
  8877. }
  8878. if (_autocreator) {
  8879. GPBAutocreatedDictionaryModified(_autocreator, self);
  8880. }
  8881. }
  8882. }
  8883. - (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
  8884. int idx = (key ? 1 : 0);
  8885. _values[idx] = value;
  8886. _valueSet[idx] = YES;
  8887. if (_autocreator) {
  8888. GPBAutocreatedDictionaryModified(_autocreator, self);
  8889. }
  8890. }
  8891. - (void)removeUInt32ForKey:(BOOL)aKey {
  8892. _valueSet[aKey ? 1 : 0] = NO;
  8893. }
  8894. - (void)removeAll {
  8895. _valueSet[0] = NO;
  8896. _valueSet[1] = NO;
  8897. }
  8898. @end
  8899. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  8900. // This block of code is generated, do not edit it directly.
  8901. #pragma mark - Bool -> Int32
  8902. @implementation GPBBoolInt32Dictionary {
  8903. @package
  8904. int32_t _values[2];
  8905. BOOL _valueSet[2];
  8906. }
  8907. - (instancetype)init {
  8908. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8909. }
  8910. - (instancetype)initWithInt32s:(const int32_t [])values
  8911. forKeys:(const BOOL [])keys
  8912. count:(NSUInteger)count {
  8913. self = [super init];
  8914. if (self) {
  8915. for (NSUInteger i = 0; i < count; ++i) {
  8916. int idx = keys[i] ? 1 : 0;
  8917. _values[idx] = values[i];
  8918. _valueSet[idx] = YES;
  8919. }
  8920. }
  8921. return self;
  8922. }
  8923. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  8924. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  8925. if (self) {
  8926. if (dictionary) {
  8927. for (int i = 0; i < 2; ++i) {
  8928. if (dictionary->_valueSet[i]) {
  8929. _values[i] = dictionary->_values[i];
  8930. _valueSet[i] = YES;
  8931. }
  8932. }
  8933. }
  8934. }
  8935. return self;
  8936. }
  8937. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8938. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8939. }
  8940. #if !defined(NS_BLOCK_ASSERTIONS)
  8941. - (void)dealloc {
  8942. NSAssert(!_autocreator,
  8943. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8944. [self class], _autocreator);
  8945. [super dealloc];
  8946. }
  8947. #endif // !defined(NS_BLOCK_ASSERTIONS)
  8948. - (instancetype)copyWithZone:(NSZone *)zone {
  8949. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8950. }
  8951. - (BOOL)isEqual:(id)other {
  8952. if (self == other) {
  8953. return YES;
  8954. }
  8955. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  8956. return NO;
  8957. }
  8958. GPBBoolInt32Dictionary *otherDictionary = other;
  8959. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  8960. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  8961. return NO;
  8962. }
  8963. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  8964. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  8965. return NO;
  8966. }
  8967. return YES;
  8968. }
  8969. - (NSUInteger)hash {
  8970. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8971. }
  8972. - (NSString *)description {
  8973. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  8974. if (_valueSet[0]) {
  8975. [result appendFormat:@"NO: %d", _values[0]];
  8976. }
  8977. if (_valueSet[1]) {
  8978. [result appendFormat:@"YES: %d", _values[1]];
  8979. }
  8980. [result appendString:@" }"];
  8981. return result;
  8982. }
  8983. - (NSUInteger)count {
  8984. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8985. }
  8986. - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
  8987. int idx = (key ? 1 : 0);
  8988. if (_valueSet[idx]) {
  8989. if (value) {
  8990. *value = _values[idx];
  8991. }
  8992. return YES;
  8993. }
  8994. return NO;
  8995. }
  8996. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8997. forGPBGenericValueKey:(GPBGenericValue *)key {
  8998. int idx = (key->valueBool ? 1 : 0);
  8999. _values[idx] = value->valueInt32;
  9000. _valueSet[idx] = YES;
  9001. }
  9002. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9003. if (_valueSet[0]) {
  9004. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9005. }
  9006. if (_valueSet[1]) {
  9007. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9008. }
  9009. }
  9010. - (void)enumerateKeysAndInt32sUsingBlock:
  9011. (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block {
  9012. BOOL stop = NO;
  9013. if (_valueSet[0]) {
  9014. block(NO, _values[0], &stop);
  9015. }
  9016. if (!stop && _valueSet[1]) {
  9017. block(YES, _values[1], &stop);
  9018. }
  9019. }
  9020. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9021. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9022. NSUInteger count = 0;
  9023. size_t result = 0;
  9024. for (int i = 0; i < 2; ++i) {
  9025. if (_valueSet[i]) {
  9026. ++count;
  9027. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9028. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9029. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9030. }
  9031. }
  9032. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9033. result += tagSize * count;
  9034. return result;
  9035. }
  9036. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9037. asField:(GPBFieldDescriptor *)field {
  9038. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9039. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9040. for (int i = 0; i < 2; ++i) {
  9041. if (_valueSet[i]) {
  9042. // Write the tag.
  9043. [outputStream writeInt32NoTag:tag];
  9044. // Write the size of the message.
  9045. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9046. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9047. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9048. // Write the fields.
  9049. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9050. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9051. }
  9052. }
  9053. }
  9054. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  9055. if (otherDictionary) {
  9056. for (int i = 0; i < 2; ++i) {
  9057. if (otherDictionary->_valueSet[i]) {
  9058. _valueSet[i] = YES;
  9059. _values[i] = otherDictionary->_values[i];
  9060. }
  9061. }
  9062. if (_autocreator) {
  9063. GPBAutocreatedDictionaryModified(_autocreator, self);
  9064. }
  9065. }
  9066. }
  9067. - (void)setInt32:(int32_t)value forKey:(BOOL)key {
  9068. int idx = (key ? 1 : 0);
  9069. _values[idx] = value;
  9070. _valueSet[idx] = YES;
  9071. if (_autocreator) {
  9072. GPBAutocreatedDictionaryModified(_autocreator, self);
  9073. }
  9074. }
  9075. - (void)removeInt32ForKey:(BOOL)aKey {
  9076. _valueSet[aKey ? 1 : 0] = NO;
  9077. }
  9078. - (void)removeAll {
  9079. _valueSet[0] = NO;
  9080. _valueSet[1] = NO;
  9081. }
  9082. @end
  9083. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  9084. // This block of code is generated, do not edit it directly.
  9085. #pragma mark - Bool -> UInt64
  9086. @implementation GPBBoolUInt64Dictionary {
  9087. @package
  9088. uint64_t _values[2];
  9089. BOOL _valueSet[2];
  9090. }
  9091. - (instancetype)init {
  9092. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9093. }
  9094. - (instancetype)initWithUInt64s:(const uint64_t [])values
  9095. forKeys:(const BOOL [])keys
  9096. count:(NSUInteger)count {
  9097. self = [super init];
  9098. if (self) {
  9099. for (NSUInteger i = 0; i < count; ++i) {
  9100. int idx = keys[i] ? 1 : 0;
  9101. _values[idx] = values[i];
  9102. _valueSet[idx] = YES;
  9103. }
  9104. }
  9105. return self;
  9106. }
  9107. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  9108. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  9109. if (self) {
  9110. if (dictionary) {
  9111. for (int i = 0; i < 2; ++i) {
  9112. if (dictionary->_valueSet[i]) {
  9113. _values[i] = dictionary->_values[i];
  9114. _valueSet[i] = YES;
  9115. }
  9116. }
  9117. }
  9118. }
  9119. return self;
  9120. }
  9121. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9122. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9123. }
  9124. #if !defined(NS_BLOCK_ASSERTIONS)
  9125. - (void)dealloc {
  9126. NSAssert(!_autocreator,
  9127. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9128. [self class], _autocreator);
  9129. [super dealloc];
  9130. }
  9131. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9132. - (instancetype)copyWithZone:(NSZone *)zone {
  9133. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9134. }
  9135. - (BOOL)isEqual:(id)other {
  9136. if (self == other) {
  9137. return YES;
  9138. }
  9139. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  9140. return NO;
  9141. }
  9142. GPBBoolUInt64Dictionary *otherDictionary = other;
  9143. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9144. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9145. return NO;
  9146. }
  9147. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9148. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9149. return NO;
  9150. }
  9151. return YES;
  9152. }
  9153. - (NSUInteger)hash {
  9154. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9155. }
  9156. - (NSString *)description {
  9157. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9158. if (_valueSet[0]) {
  9159. [result appendFormat:@"NO: %llu", _values[0]];
  9160. }
  9161. if (_valueSet[1]) {
  9162. [result appendFormat:@"YES: %llu", _values[1]];
  9163. }
  9164. [result appendString:@" }"];
  9165. return result;
  9166. }
  9167. - (NSUInteger)count {
  9168. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9169. }
  9170. - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
  9171. int idx = (key ? 1 : 0);
  9172. if (_valueSet[idx]) {
  9173. if (value) {
  9174. *value = _values[idx];
  9175. }
  9176. return YES;
  9177. }
  9178. return NO;
  9179. }
  9180. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9181. forGPBGenericValueKey:(GPBGenericValue *)key {
  9182. int idx = (key->valueBool ? 1 : 0);
  9183. _values[idx] = value->valueUInt64;
  9184. _valueSet[idx] = YES;
  9185. }
  9186. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9187. if (_valueSet[0]) {
  9188. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  9189. }
  9190. if (_valueSet[1]) {
  9191. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  9192. }
  9193. }
  9194. - (void)enumerateKeysAndUInt64sUsingBlock:
  9195. (void (NS_NOESCAPE ^)(BOOL key, uint64_t value, BOOL *stop))block {
  9196. BOOL stop = NO;
  9197. if (_valueSet[0]) {
  9198. block(NO, _values[0], &stop);
  9199. }
  9200. if (!stop && _valueSet[1]) {
  9201. block(YES, _values[1], &stop);
  9202. }
  9203. }
  9204. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9205. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9206. NSUInteger count = 0;
  9207. size_t result = 0;
  9208. for (int i = 0; i < 2; ++i) {
  9209. if (_valueSet[i]) {
  9210. ++count;
  9211. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9212. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9213. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9214. }
  9215. }
  9216. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9217. result += tagSize * count;
  9218. return result;
  9219. }
  9220. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9221. asField:(GPBFieldDescriptor *)field {
  9222. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9223. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9224. for (int i = 0; i < 2; ++i) {
  9225. if (_valueSet[i]) {
  9226. // Write the tag.
  9227. [outputStream writeInt32NoTag:tag];
  9228. // Write the size of the message.
  9229. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9230. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9231. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9232. // Write the fields.
  9233. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9234. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9235. }
  9236. }
  9237. }
  9238. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  9239. if (otherDictionary) {
  9240. for (int i = 0; i < 2; ++i) {
  9241. if (otherDictionary->_valueSet[i]) {
  9242. _valueSet[i] = YES;
  9243. _values[i] = otherDictionary->_values[i];
  9244. }
  9245. }
  9246. if (_autocreator) {
  9247. GPBAutocreatedDictionaryModified(_autocreator, self);
  9248. }
  9249. }
  9250. }
  9251. - (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
  9252. int idx = (key ? 1 : 0);
  9253. _values[idx] = value;
  9254. _valueSet[idx] = YES;
  9255. if (_autocreator) {
  9256. GPBAutocreatedDictionaryModified(_autocreator, self);
  9257. }
  9258. }
  9259. - (void)removeUInt64ForKey:(BOOL)aKey {
  9260. _valueSet[aKey ? 1 : 0] = NO;
  9261. }
  9262. - (void)removeAll {
  9263. _valueSet[0] = NO;
  9264. _valueSet[1] = NO;
  9265. }
  9266. @end
  9267. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  9268. // This block of code is generated, do not edit it directly.
  9269. #pragma mark - Bool -> Int64
  9270. @implementation GPBBoolInt64Dictionary {
  9271. @package
  9272. int64_t _values[2];
  9273. BOOL _valueSet[2];
  9274. }
  9275. - (instancetype)init {
  9276. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9277. }
  9278. - (instancetype)initWithInt64s:(const int64_t [])values
  9279. forKeys:(const BOOL [])keys
  9280. count:(NSUInteger)count {
  9281. self = [super init];
  9282. if (self) {
  9283. for (NSUInteger i = 0; i < count; ++i) {
  9284. int idx = keys[i] ? 1 : 0;
  9285. _values[idx] = values[i];
  9286. _valueSet[idx] = YES;
  9287. }
  9288. }
  9289. return self;
  9290. }
  9291. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  9292. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  9293. if (self) {
  9294. if (dictionary) {
  9295. for (int i = 0; i < 2; ++i) {
  9296. if (dictionary->_valueSet[i]) {
  9297. _values[i] = dictionary->_values[i];
  9298. _valueSet[i] = YES;
  9299. }
  9300. }
  9301. }
  9302. }
  9303. return self;
  9304. }
  9305. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9306. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9307. }
  9308. #if !defined(NS_BLOCK_ASSERTIONS)
  9309. - (void)dealloc {
  9310. NSAssert(!_autocreator,
  9311. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9312. [self class], _autocreator);
  9313. [super dealloc];
  9314. }
  9315. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9316. - (instancetype)copyWithZone:(NSZone *)zone {
  9317. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9318. }
  9319. - (BOOL)isEqual:(id)other {
  9320. if (self == other) {
  9321. return YES;
  9322. }
  9323. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  9324. return NO;
  9325. }
  9326. GPBBoolInt64Dictionary *otherDictionary = other;
  9327. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9328. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9329. return NO;
  9330. }
  9331. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9332. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9333. return NO;
  9334. }
  9335. return YES;
  9336. }
  9337. - (NSUInteger)hash {
  9338. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9339. }
  9340. - (NSString *)description {
  9341. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9342. if (_valueSet[0]) {
  9343. [result appendFormat:@"NO: %lld", _values[0]];
  9344. }
  9345. if (_valueSet[1]) {
  9346. [result appendFormat:@"YES: %lld", _values[1]];
  9347. }
  9348. [result appendString:@" }"];
  9349. return result;
  9350. }
  9351. - (NSUInteger)count {
  9352. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9353. }
  9354. - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
  9355. int idx = (key ? 1 : 0);
  9356. if (_valueSet[idx]) {
  9357. if (value) {
  9358. *value = _values[idx];
  9359. }
  9360. return YES;
  9361. }
  9362. return NO;
  9363. }
  9364. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9365. forGPBGenericValueKey:(GPBGenericValue *)key {
  9366. int idx = (key->valueBool ? 1 : 0);
  9367. _values[idx] = value->valueInt64;
  9368. _valueSet[idx] = YES;
  9369. }
  9370. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9371. if (_valueSet[0]) {
  9372. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  9373. }
  9374. if (_valueSet[1]) {
  9375. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  9376. }
  9377. }
  9378. - (void)enumerateKeysAndInt64sUsingBlock:
  9379. (void (NS_NOESCAPE ^)(BOOL key, int64_t value, BOOL *stop))block {
  9380. BOOL stop = NO;
  9381. if (_valueSet[0]) {
  9382. block(NO, _values[0], &stop);
  9383. }
  9384. if (!stop && _valueSet[1]) {
  9385. block(YES, _values[1], &stop);
  9386. }
  9387. }
  9388. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9389. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9390. NSUInteger count = 0;
  9391. size_t result = 0;
  9392. for (int i = 0; i < 2; ++i) {
  9393. if (_valueSet[i]) {
  9394. ++count;
  9395. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9396. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9397. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9398. }
  9399. }
  9400. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9401. result += tagSize * count;
  9402. return result;
  9403. }
  9404. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9405. asField:(GPBFieldDescriptor *)field {
  9406. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9407. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9408. for (int i = 0; i < 2; ++i) {
  9409. if (_valueSet[i]) {
  9410. // Write the tag.
  9411. [outputStream writeInt32NoTag:tag];
  9412. // Write the size of the message.
  9413. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9414. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9415. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9416. // Write the fields.
  9417. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9418. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9419. }
  9420. }
  9421. }
  9422. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  9423. if (otherDictionary) {
  9424. for (int i = 0; i < 2; ++i) {
  9425. if (otherDictionary->_valueSet[i]) {
  9426. _valueSet[i] = YES;
  9427. _values[i] = otherDictionary->_values[i];
  9428. }
  9429. }
  9430. if (_autocreator) {
  9431. GPBAutocreatedDictionaryModified(_autocreator, self);
  9432. }
  9433. }
  9434. }
  9435. - (void)setInt64:(int64_t)value forKey:(BOOL)key {
  9436. int idx = (key ? 1 : 0);
  9437. _values[idx] = value;
  9438. _valueSet[idx] = YES;
  9439. if (_autocreator) {
  9440. GPBAutocreatedDictionaryModified(_autocreator, self);
  9441. }
  9442. }
  9443. - (void)removeInt64ForKey:(BOOL)aKey {
  9444. _valueSet[aKey ? 1 : 0] = NO;
  9445. }
  9446. - (void)removeAll {
  9447. _valueSet[0] = NO;
  9448. _valueSet[1] = NO;
  9449. }
  9450. @end
  9451. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  9452. // This block of code is generated, do not edit it directly.
  9453. #pragma mark - Bool -> Bool
  9454. @implementation GPBBoolBoolDictionary {
  9455. @package
  9456. BOOL _values[2];
  9457. BOOL _valueSet[2];
  9458. }
  9459. - (instancetype)init {
  9460. return [self initWithBools:NULL forKeys:NULL count:0];
  9461. }
  9462. - (instancetype)initWithBools:(const BOOL [])values
  9463. forKeys:(const BOOL [])keys
  9464. count:(NSUInteger)count {
  9465. self = [super init];
  9466. if (self) {
  9467. for (NSUInteger i = 0; i < count; ++i) {
  9468. int idx = keys[i] ? 1 : 0;
  9469. _values[idx] = values[i];
  9470. _valueSet[idx] = YES;
  9471. }
  9472. }
  9473. return self;
  9474. }
  9475. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  9476. self = [self initWithBools:NULL forKeys:NULL count:0];
  9477. if (self) {
  9478. if (dictionary) {
  9479. for (int i = 0; i < 2; ++i) {
  9480. if (dictionary->_valueSet[i]) {
  9481. _values[i] = dictionary->_values[i];
  9482. _valueSet[i] = YES;
  9483. }
  9484. }
  9485. }
  9486. }
  9487. return self;
  9488. }
  9489. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9490. return [self initWithBools:NULL forKeys:NULL count:0];
  9491. }
  9492. #if !defined(NS_BLOCK_ASSERTIONS)
  9493. - (void)dealloc {
  9494. NSAssert(!_autocreator,
  9495. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9496. [self class], _autocreator);
  9497. [super dealloc];
  9498. }
  9499. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9500. - (instancetype)copyWithZone:(NSZone *)zone {
  9501. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9502. }
  9503. - (BOOL)isEqual:(id)other {
  9504. if (self == other) {
  9505. return YES;
  9506. }
  9507. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  9508. return NO;
  9509. }
  9510. GPBBoolBoolDictionary *otherDictionary = other;
  9511. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9512. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9513. return NO;
  9514. }
  9515. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9516. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9517. return NO;
  9518. }
  9519. return YES;
  9520. }
  9521. - (NSUInteger)hash {
  9522. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9523. }
  9524. - (NSString *)description {
  9525. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9526. if (_valueSet[0]) {
  9527. [result appendFormat:@"NO: %d", _values[0]];
  9528. }
  9529. if (_valueSet[1]) {
  9530. [result appendFormat:@"YES: %d", _values[1]];
  9531. }
  9532. [result appendString:@" }"];
  9533. return result;
  9534. }
  9535. - (NSUInteger)count {
  9536. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9537. }
  9538. - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
  9539. int idx = (key ? 1 : 0);
  9540. if (_valueSet[idx]) {
  9541. if (value) {
  9542. *value = _values[idx];
  9543. }
  9544. return YES;
  9545. }
  9546. return NO;
  9547. }
  9548. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9549. forGPBGenericValueKey:(GPBGenericValue *)key {
  9550. int idx = (key->valueBool ? 1 : 0);
  9551. _values[idx] = value->valueBool;
  9552. _valueSet[idx] = YES;
  9553. }
  9554. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9555. if (_valueSet[0]) {
  9556. block(@"false", (_values[0] ? @"true" : @"false"));
  9557. }
  9558. if (_valueSet[1]) {
  9559. block(@"true", (_values[1] ? @"true" : @"false"));
  9560. }
  9561. }
  9562. - (void)enumerateKeysAndBoolsUsingBlock:
  9563. (void (NS_NOESCAPE ^)(BOOL key, BOOL value, BOOL *stop))block {
  9564. BOOL stop = NO;
  9565. if (_valueSet[0]) {
  9566. block(NO, _values[0], &stop);
  9567. }
  9568. if (!stop && _valueSet[1]) {
  9569. block(YES, _values[1], &stop);
  9570. }
  9571. }
  9572. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9573. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9574. NSUInteger count = 0;
  9575. size_t result = 0;
  9576. for (int i = 0; i < 2; ++i) {
  9577. if (_valueSet[i]) {
  9578. ++count;
  9579. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9580. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9581. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9582. }
  9583. }
  9584. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9585. result += tagSize * count;
  9586. return result;
  9587. }
  9588. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9589. asField:(GPBFieldDescriptor *)field {
  9590. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9591. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9592. for (int i = 0; i < 2; ++i) {
  9593. if (_valueSet[i]) {
  9594. // Write the tag.
  9595. [outputStream writeInt32NoTag:tag];
  9596. // Write the size of the message.
  9597. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9598. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9599. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9600. // Write the fields.
  9601. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9602. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9603. }
  9604. }
  9605. }
  9606. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  9607. if (otherDictionary) {
  9608. for (int i = 0; i < 2; ++i) {
  9609. if (otherDictionary->_valueSet[i]) {
  9610. _valueSet[i] = YES;
  9611. _values[i] = otherDictionary->_values[i];
  9612. }
  9613. }
  9614. if (_autocreator) {
  9615. GPBAutocreatedDictionaryModified(_autocreator, self);
  9616. }
  9617. }
  9618. }
  9619. - (void)setBool:(BOOL)value forKey:(BOOL)key {
  9620. int idx = (key ? 1 : 0);
  9621. _values[idx] = value;
  9622. _valueSet[idx] = YES;
  9623. if (_autocreator) {
  9624. GPBAutocreatedDictionaryModified(_autocreator, self);
  9625. }
  9626. }
  9627. - (void)removeBoolForKey:(BOOL)aKey {
  9628. _valueSet[aKey ? 1 : 0] = NO;
  9629. }
  9630. - (void)removeAll {
  9631. _valueSet[0] = NO;
  9632. _valueSet[1] = NO;
  9633. }
  9634. @end
  9635. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  9636. // This block of code is generated, do not edit it directly.
  9637. #pragma mark - Bool -> Float
  9638. @implementation GPBBoolFloatDictionary {
  9639. @package
  9640. float _values[2];
  9641. BOOL _valueSet[2];
  9642. }
  9643. - (instancetype)init {
  9644. return [self initWithFloats:NULL forKeys:NULL count:0];
  9645. }
  9646. - (instancetype)initWithFloats:(const float [])values
  9647. forKeys:(const BOOL [])keys
  9648. count:(NSUInteger)count {
  9649. self = [super init];
  9650. if (self) {
  9651. for (NSUInteger i = 0; i < count; ++i) {
  9652. int idx = keys[i] ? 1 : 0;
  9653. _values[idx] = values[i];
  9654. _valueSet[idx] = YES;
  9655. }
  9656. }
  9657. return self;
  9658. }
  9659. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  9660. self = [self initWithFloats:NULL forKeys:NULL count:0];
  9661. if (self) {
  9662. if (dictionary) {
  9663. for (int i = 0; i < 2; ++i) {
  9664. if (dictionary->_valueSet[i]) {
  9665. _values[i] = dictionary->_values[i];
  9666. _valueSet[i] = YES;
  9667. }
  9668. }
  9669. }
  9670. }
  9671. return self;
  9672. }
  9673. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9674. return [self initWithFloats:NULL forKeys:NULL count:0];
  9675. }
  9676. #if !defined(NS_BLOCK_ASSERTIONS)
  9677. - (void)dealloc {
  9678. NSAssert(!_autocreator,
  9679. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9680. [self class], _autocreator);
  9681. [super dealloc];
  9682. }
  9683. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9684. - (instancetype)copyWithZone:(NSZone *)zone {
  9685. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9686. }
  9687. - (BOOL)isEqual:(id)other {
  9688. if (self == other) {
  9689. return YES;
  9690. }
  9691. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  9692. return NO;
  9693. }
  9694. GPBBoolFloatDictionary *otherDictionary = other;
  9695. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9696. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9697. return NO;
  9698. }
  9699. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9700. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9701. return NO;
  9702. }
  9703. return YES;
  9704. }
  9705. - (NSUInteger)hash {
  9706. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9707. }
  9708. - (NSString *)description {
  9709. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9710. if (_valueSet[0]) {
  9711. [result appendFormat:@"NO: %f", _values[0]];
  9712. }
  9713. if (_valueSet[1]) {
  9714. [result appendFormat:@"YES: %f", _values[1]];
  9715. }
  9716. [result appendString:@" }"];
  9717. return result;
  9718. }
  9719. - (NSUInteger)count {
  9720. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9721. }
  9722. - (BOOL)getFloat:(float *)value forKey:(BOOL)key {
  9723. int idx = (key ? 1 : 0);
  9724. if (_valueSet[idx]) {
  9725. if (value) {
  9726. *value = _values[idx];
  9727. }
  9728. return YES;
  9729. }
  9730. return NO;
  9731. }
  9732. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9733. forGPBGenericValueKey:(GPBGenericValue *)key {
  9734. int idx = (key->valueBool ? 1 : 0);
  9735. _values[idx] = value->valueFloat;
  9736. _valueSet[idx] = YES;
  9737. }
  9738. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9739. if (_valueSet[0]) {
  9740. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  9741. }
  9742. if (_valueSet[1]) {
  9743. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  9744. }
  9745. }
  9746. - (void)enumerateKeysAndFloatsUsingBlock:
  9747. (void (NS_NOESCAPE ^)(BOOL key, float value, BOOL *stop))block {
  9748. BOOL stop = NO;
  9749. if (_valueSet[0]) {
  9750. block(NO, _values[0], &stop);
  9751. }
  9752. if (!stop && _valueSet[1]) {
  9753. block(YES, _values[1], &stop);
  9754. }
  9755. }
  9756. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9757. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9758. NSUInteger count = 0;
  9759. size_t result = 0;
  9760. for (int i = 0; i < 2; ++i) {
  9761. if (_valueSet[i]) {
  9762. ++count;
  9763. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9764. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9765. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9766. }
  9767. }
  9768. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9769. result += tagSize * count;
  9770. return result;
  9771. }
  9772. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9773. asField:(GPBFieldDescriptor *)field {
  9774. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9775. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9776. for (int i = 0; i < 2; ++i) {
  9777. if (_valueSet[i]) {
  9778. // Write the tag.
  9779. [outputStream writeInt32NoTag:tag];
  9780. // Write the size of the message.
  9781. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9782. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9783. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9784. // Write the fields.
  9785. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9786. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9787. }
  9788. }
  9789. }
  9790. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  9791. if (otherDictionary) {
  9792. for (int i = 0; i < 2; ++i) {
  9793. if (otherDictionary->_valueSet[i]) {
  9794. _valueSet[i] = YES;
  9795. _values[i] = otherDictionary->_values[i];
  9796. }
  9797. }
  9798. if (_autocreator) {
  9799. GPBAutocreatedDictionaryModified(_autocreator, self);
  9800. }
  9801. }
  9802. }
  9803. - (void)setFloat:(float)value forKey:(BOOL)key {
  9804. int idx = (key ? 1 : 0);
  9805. _values[idx] = value;
  9806. _valueSet[idx] = YES;
  9807. if (_autocreator) {
  9808. GPBAutocreatedDictionaryModified(_autocreator, self);
  9809. }
  9810. }
  9811. - (void)removeFloatForKey:(BOOL)aKey {
  9812. _valueSet[aKey ? 1 : 0] = NO;
  9813. }
  9814. - (void)removeAll {
  9815. _valueSet[0] = NO;
  9816. _valueSet[1] = NO;
  9817. }
  9818. @end
  9819. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  9820. // This block of code is generated, do not edit it directly.
  9821. #pragma mark - Bool -> Double
  9822. @implementation GPBBoolDoubleDictionary {
  9823. @package
  9824. double _values[2];
  9825. BOOL _valueSet[2];
  9826. }
  9827. - (instancetype)init {
  9828. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9829. }
  9830. - (instancetype)initWithDoubles:(const double [])values
  9831. forKeys:(const BOOL [])keys
  9832. count:(NSUInteger)count {
  9833. self = [super init];
  9834. if (self) {
  9835. for (NSUInteger i = 0; i < count; ++i) {
  9836. int idx = keys[i] ? 1 : 0;
  9837. _values[idx] = values[i];
  9838. _valueSet[idx] = YES;
  9839. }
  9840. }
  9841. return self;
  9842. }
  9843. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  9844. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  9845. if (self) {
  9846. if (dictionary) {
  9847. for (int i = 0; i < 2; ++i) {
  9848. if (dictionary->_valueSet[i]) {
  9849. _values[i] = dictionary->_values[i];
  9850. _valueSet[i] = YES;
  9851. }
  9852. }
  9853. }
  9854. }
  9855. return self;
  9856. }
  9857. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9858. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9859. }
  9860. #if !defined(NS_BLOCK_ASSERTIONS)
  9861. - (void)dealloc {
  9862. NSAssert(!_autocreator,
  9863. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9864. [self class], _autocreator);
  9865. [super dealloc];
  9866. }
  9867. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9868. - (instancetype)copyWithZone:(NSZone *)zone {
  9869. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9870. }
  9871. - (BOOL)isEqual:(id)other {
  9872. if (self == other) {
  9873. return YES;
  9874. }
  9875. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  9876. return NO;
  9877. }
  9878. GPBBoolDoubleDictionary *otherDictionary = other;
  9879. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9880. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9881. return NO;
  9882. }
  9883. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9884. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9885. return NO;
  9886. }
  9887. return YES;
  9888. }
  9889. - (NSUInteger)hash {
  9890. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9891. }
  9892. - (NSString *)description {
  9893. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9894. if (_valueSet[0]) {
  9895. [result appendFormat:@"NO: %lf", _values[0]];
  9896. }
  9897. if (_valueSet[1]) {
  9898. [result appendFormat:@"YES: %lf", _values[1]];
  9899. }
  9900. [result appendString:@" }"];
  9901. return result;
  9902. }
  9903. - (NSUInteger)count {
  9904. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9905. }
  9906. - (BOOL)getDouble:(double *)value forKey:(BOOL)key {
  9907. int idx = (key ? 1 : 0);
  9908. if (_valueSet[idx]) {
  9909. if (value) {
  9910. *value = _values[idx];
  9911. }
  9912. return YES;
  9913. }
  9914. return NO;
  9915. }
  9916. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9917. forGPBGenericValueKey:(GPBGenericValue *)key {
  9918. int idx = (key->valueBool ? 1 : 0);
  9919. _values[idx] = value->valueDouble;
  9920. _valueSet[idx] = YES;
  9921. }
  9922. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9923. if (_valueSet[0]) {
  9924. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  9925. }
  9926. if (_valueSet[1]) {
  9927. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  9928. }
  9929. }
  9930. - (void)enumerateKeysAndDoublesUsingBlock:
  9931. (void (NS_NOESCAPE ^)(BOOL key, double value, BOOL *stop))block {
  9932. BOOL stop = NO;
  9933. if (_valueSet[0]) {
  9934. block(NO, _values[0], &stop);
  9935. }
  9936. if (!stop && _valueSet[1]) {
  9937. block(YES, _values[1], &stop);
  9938. }
  9939. }
  9940. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9941. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9942. NSUInteger count = 0;
  9943. size_t result = 0;
  9944. for (int i = 0; i < 2; ++i) {
  9945. if (_valueSet[i]) {
  9946. ++count;
  9947. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9948. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9949. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9950. }
  9951. }
  9952. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9953. result += tagSize * count;
  9954. return result;
  9955. }
  9956. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9957. asField:(GPBFieldDescriptor *)field {
  9958. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9959. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9960. for (int i = 0; i < 2; ++i) {
  9961. if (_valueSet[i]) {
  9962. // Write the tag.
  9963. [outputStream writeInt32NoTag:tag];
  9964. // Write the size of the message.
  9965. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9966. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9967. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9968. // Write the fields.
  9969. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9970. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9971. }
  9972. }
  9973. }
  9974. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  9975. if (otherDictionary) {
  9976. for (int i = 0; i < 2; ++i) {
  9977. if (otherDictionary->_valueSet[i]) {
  9978. _valueSet[i] = YES;
  9979. _values[i] = otherDictionary->_values[i];
  9980. }
  9981. }
  9982. if (_autocreator) {
  9983. GPBAutocreatedDictionaryModified(_autocreator, self);
  9984. }
  9985. }
  9986. }
  9987. - (void)setDouble:(double)value forKey:(BOOL)key {
  9988. int idx = (key ? 1 : 0);
  9989. _values[idx] = value;
  9990. _valueSet[idx] = YES;
  9991. if (_autocreator) {
  9992. GPBAutocreatedDictionaryModified(_autocreator, self);
  9993. }
  9994. }
  9995. - (void)removeDoubleForKey:(BOOL)aKey {
  9996. _valueSet[aKey ? 1 : 0] = NO;
  9997. }
  9998. - (void)removeAll {
  9999. _valueSet[0] = NO;
  10000. _valueSet[1] = NO;
  10001. }
  10002. @end
  10003. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  10004. // This block of code is generated, do not edit it directly.
  10005. #pragma mark - Bool -> Object
  10006. @implementation GPBBoolObjectDictionary {
  10007. @package
  10008. id _values[2];
  10009. }
  10010. - (instancetype)init {
  10011. return [self initWithObjects:NULL forKeys:NULL count:0];
  10012. }
  10013. - (instancetype)initWithObjects:(const id [])objects
  10014. forKeys:(const BOOL [])keys
  10015. count:(NSUInteger)count {
  10016. self = [super init];
  10017. if (self) {
  10018. for (NSUInteger i = 0; i < count; ++i) {
  10019. if (!objects[i]) {
  10020. [NSException raise:NSInvalidArgumentException
  10021. format:@"Attempting to add nil object to a Dictionary"];
  10022. }
  10023. int idx = keys[i] ? 1 : 0;
  10024. [_values[idx] release];
  10025. _values[idx] = (id)[objects[i] retain];
  10026. }
  10027. }
  10028. return self;
  10029. }
  10030. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  10031. self = [self initWithObjects:NULL forKeys:NULL count:0];
  10032. if (self) {
  10033. if (dictionary) {
  10034. _values[0] = [dictionary->_values[0] retain];
  10035. _values[1] = [dictionary->_values[1] retain];
  10036. }
  10037. }
  10038. return self;
  10039. }
  10040. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  10041. return [self initWithObjects:NULL forKeys:NULL count:0];
  10042. }
  10043. - (void)dealloc {
  10044. NSAssert(!_autocreator,
  10045. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10046. [self class], _autocreator);
  10047. [_values[0] release];
  10048. [_values[1] release];
  10049. [super dealloc];
  10050. }
  10051. - (instancetype)copyWithZone:(NSZone *)zone {
  10052. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  10053. }
  10054. - (BOOL)isEqual:(id)other {
  10055. if (self == other) {
  10056. return YES;
  10057. }
  10058. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  10059. return NO;
  10060. }
  10061. GPBBoolObjectDictionary *otherDictionary = other;
  10062. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  10063. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  10064. return NO;
  10065. }
  10066. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  10067. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  10068. return NO;
  10069. }
  10070. return YES;
  10071. }
  10072. - (NSUInteger)hash {
  10073. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10074. }
  10075. - (NSString *)description {
  10076. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10077. if ((_values[0] != nil)) {
  10078. [result appendFormat:@"NO: %@", _values[0]];
  10079. }
  10080. if ((_values[1] != nil)) {
  10081. [result appendFormat:@"YES: %@", _values[1]];
  10082. }
  10083. [result appendString:@" }"];
  10084. return result;
  10085. }
  10086. - (NSUInteger)count {
  10087. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10088. }
  10089. - (id)objectForKey:(BOOL)key {
  10090. return _values[key ? 1 : 0];
  10091. }
  10092. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10093. forGPBGenericValueKey:(GPBGenericValue *)key {
  10094. int idx = (key->valueBool ? 1 : 0);
  10095. [_values[idx] release];
  10096. _values[idx] = [value->valueString retain];
  10097. }
  10098. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10099. if (_values[0] != nil) {
  10100. block(@"false", _values[0]);
  10101. }
  10102. if ((_values[1] != nil)) {
  10103. block(@"true", _values[1]);
  10104. }
  10105. }
  10106. - (void)enumerateKeysAndObjectsUsingBlock:
  10107. (void (NS_NOESCAPE ^)(BOOL key, id object, BOOL *stop))block {
  10108. BOOL stop = NO;
  10109. if (_values[0] != nil) {
  10110. block(NO, _values[0], &stop);
  10111. }
  10112. if (!stop && (_values[1] != nil)) {
  10113. block(YES, _values[1], &stop);
  10114. }
  10115. }
  10116. - (BOOL)isInitialized {
  10117. if (_values[0] && ![_values[0] isInitialized]) {
  10118. return NO;
  10119. }
  10120. if (_values[1] && ![_values[1] isInitialized]) {
  10121. return NO;
  10122. }
  10123. return YES;
  10124. }
  10125. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  10126. GPBBoolObjectDictionary *newDict =
  10127. [[GPBBoolObjectDictionary alloc] init];
  10128. for (int i = 0; i < 2; ++i) {
  10129. if (_values[i] != nil) {
  10130. newDict->_values[i] = [_values[i] copyWithZone:zone];
  10131. }
  10132. }
  10133. return newDict;
  10134. }
  10135. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10136. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10137. NSUInteger count = 0;
  10138. size_t result = 0;
  10139. for (int i = 0; i < 2; ++i) {
  10140. if (_values[i] != nil) {
  10141. ++count;
  10142. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10143. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10144. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10145. }
  10146. }
  10147. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10148. result += tagSize * count;
  10149. return result;
  10150. }
  10151. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10152. asField:(GPBFieldDescriptor *)field {
  10153. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10154. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10155. for (int i = 0; i < 2; ++i) {
  10156. if (_values[i] != nil) {
  10157. // Write the tag.
  10158. [outputStream writeInt32NoTag:tag];
  10159. // Write the size of the message.
  10160. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10161. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10162. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10163. // Write the fields.
  10164. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10165. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10166. }
  10167. }
  10168. }
  10169. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  10170. if (otherDictionary) {
  10171. for (int i = 0; i < 2; ++i) {
  10172. if (otherDictionary->_values[i] != nil) {
  10173. [_values[i] release];
  10174. _values[i] = [otherDictionary->_values[i] retain];
  10175. }
  10176. }
  10177. if (_autocreator) {
  10178. GPBAutocreatedDictionaryModified(_autocreator, self);
  10179. }
  10180. }
  10181. }
  10182. - (void)setObject:(id)object forKey:(BOOL)key {
  10183. if (!object) {
  10184. [NSException raise:NSInvalidArgumentException
  10185. format:@"Attempting to add nil object to a Dictionary"];
  10186. }
  10187. int idx = (key ? 1 : 0);
  10188. [_values[idx] release];
  10189. _values[idx] = [object retain];
  10190. if (_autocreator) {
  10191. GPBAutocreatedDictionaryModified(_autocreator, self);
  10192. }
  10193. }
  10194. - (void)removeObjectForKey:(BOOL)aKey {
  10195. int idx = (aKey ? 1 : 0);
  10196. [_values[idx] release];
  10197. _values[idx] = nil;
  10198. }
  10199. - (void)removeAll {
  10200. for (int i = 0; i < 2; ++i) {
  10201. [_values[i] release];
  10202. _values[i] = nil;
  10203. }
  10204. }
  10205. @end
  10206. //%PDDM-EXPAND-END (8 expansions)
  10207. // clang-format on
  10208. #pragma mark - Bool -> Enum
  10209. @implementation GPBBoolEnumDictionary {
  10210. @package
  10211. GPBEnumValidationFunc _validationFunc;
  10212. int32_t _values[2];
  10213. BOOL _valueSet[2];
  10214. }
  10215. @synthesize validationFunc = _validationFunc;
  10216. - (instancetype)init {
  10217. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  10218. }
  10219. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  10220. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10221. }
  10222. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10223. rawValues:(const int32_t[])rawValues
  10224. forKeys:(const BOOL[])keys
  10225. count:(NSUInteger)count {
  10226. self = [super init];
  10227. if (self) {
  10228. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  10229. for (NSUInteger i = 0; i < count; ++i) {
  10230. int idx = keys[i] ? 1 : 0;
  10231. _values[idx] = rawValues[i];
  10232. _valueSet[idx] = YES;
  10233. }
  10234. }
  10235. return self;
  10236. }
  10237. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  10238. self = [self initWithValidationFunction:dictionary.validationFunc
  10239. rawValues:NULL
  10240. forKeys:NULL
  10241. count:0];
  10242. if (self) {
  10243. if (dictionary) {
  10244. for (int i = 0; i < 2; ++i) {
  10245. if (dictionary->_valueSet[i]) {
  10246. _values[i] = dictionary->_values[i];
  10247. _valueSet[i] = YES;
  10248. }
  10249. }
  10250. }
  10251. }
  10252. return self;
  10253. }
  10254. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10255. capacity:(__unused NSUInteger)numItems {
  10256. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10257. }
  10258. #if !defined(NS_BLOCK_ASSERTIONS)
  10259. - (void)dealloc {
  10260. NSAssert(!_autocreator, @"%@: Autocreator must be cleared before release, autocreator: %@",
  10261. [self class], _autocreator);
  10262. [super dealloc];
  10263. }
  10264. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10265. - (instancetype)copyWithZone:(NSZone *)zone {
  10266. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  10267. }
  10268. - (BOOL)isEqual:(id)other {
  10269. if (self == other) {
  10270. return YES;
  10271. }
  10272. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  10273. return NO;
  10274. }
  10275. GPBBoolEnumDictionary *otherDictionary = other;
  10276. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10277. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10278. return NO;
  10279. }
  10280. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10281. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10282. return NO;
  10283. }
  10284. return YES;
  10285. }
  10286. - (NSUInteger)hash {
  10287. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10288. }
  10289. - (NSString *)description {
  10290. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10291. if (_valueSet[0]) {
  10292. [result appendFormat:@"NO: %d", _values[0]];
  10293. }
  10294. if (_valueSet[1]) {
  10295. [result appendFormat:@"YES: %d", _values[1]];
  10296. }
  10297. [result appendString:@" }"];
  10298. return result;
  10299. }
  10300. - (NSUInteger)count {
  10301. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10302. }
  10303. - (BOOL)getEnum:(int32_t *)value forKey:(BOOL)key {
  10304. int idx = (key ? 1 : 0);
  10305. if (_valueSet[idx]) {
  10306. if (value) {
  10307. int32_t result = _values[idx];
  10308. if (!_validationFunc(result)) {
  10309. result = kGPBUnrecognizedEnumeratorValue;
  10310. }
  10311. *value = result;
  10312. }
  10313. return YES;
  10314. }
  10315. return NO;
  10316. }
  10317. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(BOOL)key {
  10318. int idx = (key ? 1 : 0);
  10319. if (_valueSet[idx]) {
  10320. if (rawValue) {
  10321. *rawValue = _values[idx];
  10322. }
  10323. return YES;
  10324. }
  10325. return NO;
  10326. }
  10327. - (void)enumerateKeysAndRawValuesUsingBlock:(void(NS_NOESCAPE ^)(BOOL key, int32_t value,
  10328. BOOL *stop))block {
  10329. BOOL stop = NO;
  10330. if (_valueSet[0]) {
  10331. block(NO, _values[0], &stop);
  10332. }
  10333. if (!stop && _valueSet[1]) {
  10334. block(YES, _values[1], &stop);
  10335. }
  10336. }
  10337. - (void)enumerateKeysAndEnumsUsingBlock:(void(NS_NOESCAPE ^)(BOOL key, int32_t rawValue,
  10338. BOOL *stop))block {
  10339. BOOL stop = NO;
  10340. GPBEnumValidationFunc func = _validationFunc;
  10341. int32_t validatedValue;
  10342. if (_valueSet[0]) {
  10343. validatedValue = _values[0];
  10344. if (!func(validatedValue)) {
  10345. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10346. }
  10347. block(NO, validatedValue, &stop);
  10348. }
  10349. if (!stop && _valueSet[1]) {
  10350. validatedValue = _values[1];
  10351. if (!func(validatedValue)) {
  10352. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10353. }
  10354. block(YES, validatedValue, &stop);
  10355. }
  10356. }
  10357. // clang-format off
  10358. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10359. // This block of code is generated, do not edit it directly.
  10360. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  10361. forKey:(GPBGenericValue *)key
  10362. keyDataType:(GPBDataType)keyDataType {
  10363. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  10364. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  10365. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  10366. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  10367. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  10368. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  10369. [outputStream flush];
  10370. [outputStream release];
  10371. return data;
  10372. }
  10373. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10374. // clang-format on
  10375. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10376. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10377. NSUInteger count = 0;
  10378. size_t result = 0;
  10379. for (int i = 0; i < 2; ++i) {
  10380. if (_valueSet[i]) {
  10381. ++count;
  10382. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10383. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10384. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10385. }
  10386. }
  10387. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10388. result += tagSize * count;
  10389. return result;
  10390. }
  10391. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10392. asField:(GPBFieldDescriptor *)field {
  10393. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10394. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10395. for (int i = 0; i < 2; ++i) {
  10396. if (_valueSet[i]) {
  10397. // Write the tag.
  10398. [outputStream writeInt32NoTag:tag];
  10399. // Write the size of the message.
  10400. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10401. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10402. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10403. // Write the fields.
  10404. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10405. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10406. }
  10407. }
  10408. }
  10409. - (void)enumerateForTextFormat:(void(NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10410. if (_valueSet[0]) {
  10411. block(@"false", @(_values[0]));
  10412. }
  10413. if (_valueSet[1]) {
  10414. block(@"true", @(_values[1]));
  10415. }
  10416. }
  10417. - (void)setGPBGenericValue:(GPBGenericValue *)value forGPBGenericValueKey:(GPBGenericValue *)key {
  10418. int idx = (key->valueBool ? 1 : 0);
  10419. _values[idx] = value->valueInt32;
  10420. _valueSet[idx] = YES;
  10421. }
  10422. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  10423. if (otherDictionary) {
  10424. for (int i = 0; i < 2; ++i) {
  10425. if (otherDictionary->_valueSet[i]) {
  10426. _valueSet[i] = YES;
  10427. _values[i] = otherDictionary->_values[i];
  10428. }
  10429. }
  10430. if (_autocreator) {
  10431. GPBAutocreatedDictionaryModified(_autocreator, self);
  10432. }
  10433. }
  10434. }
  10435. - (void)setEnum:(int32_t)value forKey:(BOOL)key {
  10436. if (!_validationFunc(value)) {
  10437. [NSException raise:NSInvalidArgumentException
  10438. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)", value];
  10439. }
  10440. int idx = (key ? 1 : 0);
  10441. _values[idx] = value;
  10442. _valueSet[idx] = YES;
  10443. if (_autocreator) {
  10444. GPBAutocreatedDictionaryModified(_autocreator, self);
  10445. }
  10446. }
  10447. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  10448. int idx = (key ? 1 : 0);
  10449. _values[idx] = rawValue;
  10450. _valueSet[idx] = YES;
  10451. if (_autocreator) {
  10452. GPBAutocreatedDictionaryModified(_autocreator, self);
  10453. }
  10454. }
  10455. - (void)removeEnumForKey:(BOOL)aKey {
  10456. _valueSet[aKey ? 1 : 0] = NO;
  10457. }
  10458. - (void)removeAll {
  10459. _valueSet[0] = NO;
  10460. _valueSet[1] = NO;
  10461. }
  10462. @end
  10463. #pragma mark - NSDictionary Subclass
  10464. @implementation GPBAutocreatedDictionary {
  10465. NSMutableDictionary *_dictionary;
  10466. }
  10467. - (void)dealloc {
  10468. NSAssert(!_autocreator, @"%@: Autocreator must be cleared before release, autocreator: %@",
  10469. [self class], _autocreator);
  10470. [_dictionary release];
  10471. [super dealloc];
  10472. }
  10473. #pragma mark Required NSDictionary overrides
  10474. - (instancetype)initWithObjects:(const id[])objects
  10475. forKeys:(const id<NSCopying>[])keys
  10476. count:(NSUInteger)count {
  10477. self = [super init];
  10478. if (self) {
  10479. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys count:count];
  10480. }
  10481. return self;
  10482. }
  10483. - (NSUInteger)count {
  10484. return [_dictionary count];
  10485. }
  10486. - (id)objectForKey:(id)aKey {
  10487. return [_dictionary objectForKey:aKey];
  10488. }
  10489. - (NSEnumerator *)keyEnumerator {
  10490. if (_dictionary == nil) {
  10491. _dictionary = [[NSMutableDictionary alloc] init];
  10492. }
  10493. return [_dictionary keyEnumerator];
  10494. }
  10495. #pragma mark Required NSMutableDictionary overrides
  10496. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  10497. // since we only autocreate empty dictionaries.
  10498. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  10499. if (_dictionary == nil) {
  10500. _dictionary = [[NSMutableDictionary alloc] init];
  10501. }
  10502. [_dictionary setObject:anObject forKey:aKey];
  10503. if (_autocreator) {
  10504. GPBAutocreatedDictionaryModified(_autocreator, self);
  10505. }
  10506. }
  10507. - (void)removeObjectForKey:(id)aKey {
  10508. [_dictionary removeObjectForKey:aKey];
  10509. }
  10510. #pragma mark Extra things hooked
  10511. - (id)copyWithZone:(NSZone *)zone {
  10512. if (_dictionary == nil) {
  10513. return [[NSMutableDictionary allocWithZone:zone] init];
  10514. }
  10515. return [_dictionary copyWithZone:zone];
  10516. }
  10517. - (id)mutableCopyWithZone:(NSZone *)zone {
  10518. if (_dictionary == nil) {
  10519. return [[NSMutableDictionary allocWithZone:zone] init];
  10520. }
  10521. return [_dictionary mutableCopyWithZone:zone];
  10522. }
  10523. // Not really needed, but subscripting is likely common enough it doesn't hurt
  10524. // to ensure it goes directly to the real NSMutableDictionary.
  10525. - (id)objectForKeyedSubscript:(id)key {
  10526. return [_dictionary objectForKeyedSubscript:key];
  10527. }
  10528. // Not really needed, but subscripting is likely common enough it doesn't hurt
  10529. // to ensure it goes directly to the real NSMutableDictionary.
  10530. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  10531. if (_dictionary == nil) {
  10532. _dictionary = [[NSMutableDictionary alloc] init];
  10533. }
  10534. [_dictionary setObject:obj forKeyedSubscript:key];
  10535. if (_autocreator) {
  10536. GPBAutocreatedDictionaryModified(_autocreator, self);
  10537. }
  10538. }
  10539. - (void)enumerateKeysAndObjectsUsingBlock:(void(NS_NOESCAPE ^)(id key, id obj, BOOL *stop))block {
  10540. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  10541. }
  10542. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  10543. usingBlock:(void(NS_NOESCAPE ^)(id key, id obj, BOOL *stop))block {
  10544. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  10545. }
  10546. @end
  10547. #pragma clang diagnostic pop