GPBDictionary.m 370 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967
  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.h"
  8. #import "GPBDictionary_PackagePrivate.h"
  9. #import "GPBCodedInputStream.h"
  10. #import "GPBCodedInputStream_PackagePrivate.h"
  11. #import "GPBCodedOutputStream.h"
  12. #import "GPBCodedOutputStream_PackagePrivate.h"
  13. #import "GPBDescriptor.h"
  14. #import "GPBDescriptor_PackagePrivate.h"
  15. #import "GPBMessage.h"
  16. #import "GPBMessage_PackagePrivate.h"
  17. #import "GPBUtilities.h"
  18. #import "GPBUtilities_PackagePrivate.h"
  19. // ------------------------------ NOTE ------------------------------
  20. // At the moment, this is all using NSNumbers in NSDictionaries under
  21. // the hood, but it is all hidden so we can come back and optimize
  22. // with direct CFDictionary usage later. The reason that wasn't
  23. // done yet is needing to support 32bit iOS builds. Otherwise
  24. // it would be pretty simple to store all this data in CFDictionaries
  25. // directly.
  26. // ------------------------------------------------------------------
  27. // Direct access is use for speed, to avoid even internally declaring things
  28. // read/write, etc. The warning is enabled in the project to ensure code calling
  29. // protos can turn on -Wdirect-ivar-access without issues.
  30. #pragma clang diagnostic push
  31. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  32. enum {
  33. kMapKeyFieldNumber = 1,
  34. kMapValueFieldNumber = 2,
  35. };
  36. static BOOL DictDefault_IsValidValue(int32_t value) {
  37. // Anything but the bad value marker is allowed.
  38. return (value != kGPBUnrecognizedEnumeratorValue);
  39. }
  40. // Disable clang-format for the macros.
  41. // clang-format off
  42. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  43. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  44. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  45. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  46. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  47. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  48. //% } else {
  49. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  50. //% return 0;
  51. //% }
  52. //%}
  53. //%
  54. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  55. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  56. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  57. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  58. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  59. //% } else {
  60. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  61. //% }
  62. //%}
  63. //%
  64. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  65. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  66. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  67. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  68. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  69. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  70. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  71. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  72. //% } else {
  73. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  74. //% return 0;
  75. //% }
  76. //%}
  77. //%
  78. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  79. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  80. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  81. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  82. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  83. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  84. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  85. //% } else {
  86. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  87. //% }
  88. //%}
  89. //%
  90. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  91. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, __unused GPBDataType dataType) {
  92. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  93. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  94. //%}
  95. //%
  96. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, __unused GPBDataType dataType) {
  97. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  98. //% [stream write##VALUE_NAME##:fieldNum value:value];
  99. //%}
  100. //%
  101. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  102. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  103. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  104. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  105. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  106. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  107. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  108. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  109. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  110. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  111. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  112. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  113. // This block of code is generated, do not edit it directly.
  114. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  115. if (dataType == GPBDataTypeInt32) {
  116. return GPBComputeInt32Size(fieldNum, value);
  117. } else if (dataType == GPBDataTypeSInt32) {
  118. return GPBComputeSInt32Size(fieldNum, value);
  119. } else if (dataType == GPBDataTypeSFixed32) {
  120. return GPBComputeSFixed32Size(fieldNum, value);
  121. } else {
  122. NSCAssert(NO, @"Unexpected type %d", dataType);
  123. return 0;
  124. }
  125. }
  126. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  127. if (dataType == GPBDataTypeInt32) {
  128. [stream writeInt32:fieldNum value:value];
  129. } else if (dataType == GPBDataTypeSInt32) {
  130. [stream writeSInt32:fieldNum value:value];
  131. } else if (dataType == GPBDataTypeSFixed32) {
  132. [stream writeSFixed32:fieldNum value:value];
  133. } else {
  134. NSCAssert(NO, @"Unexpected type %d", dataType);
  135. }
  136. }
  137. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  138. if (dataType == GPBDataTypeUInt32) {
  139. return GPBComputeUInt32Size(fieldNum, value);
  140. } else if (dataType == GPBDataTypeFixed32) {
  141. return GPBComputeFixed32Size(fieldNum, value);
  142. } else {
  143. NSCAssert(NO, @"Unexpected type %d", dataType);
  144. return 0;
  145. }
  146. }
  147. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  148. if (dataType == GPBDataTypeUInt32) {
  149. [stream writeUInt32:fieldNum value:value];
  150. } else if (dataType == GPBDataTypeFixed32) {
  151. [stream writeFixed32:fieldNum value:value];
  152. } else {
  153. NSCAssert(NO, @"Unexpected type %d", dataType);
  154. }
  155. }
  156. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  157. if (dataType == GPBDataTypeInt64) {
  158. return GPBComputeInt64Size(fieldNum, value);
  159. } else if (dataType == GPBDataTypeSInt64) {
  160. return GPBComputeSInt64Size(fieldNum, value);
  161. } else if (dataType == GPBDataTypeSFixed64) {
  162. return GPBComputeSFixed64Size(fieldNum, value);
  163. } else {
  164. NSCAssert(NO, @"Unexpected type %d", dataType);
  165. return 0;
  166. }
  167. }
  168. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  169. if (dataType == GPBDataTypeInt64) {
  170. [stream writeInt64:fieldNum value:value];
  171. } else if (dataType == GPBDataTypeSInt64) {
  172. [stream writeSInt64:fieldNum value:value];
  173. } else if (dataType == GPBDataTypeSFixed64) {
  174. [stream writeSFixed64:fieldNum value:value];
  175. } else {
  176. NSCAssert(NO, @"Unexpected type %d", dataType);
  177. }
  178. }
  179. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  180. if (dataType == GPBDataTypeUInt64) {
  181. return GPBComputeUInt64Size(fieldNum, value);
  182. } else if (dataType == GPBDataTypeFixed64) {
  183. return GPBComputeFixed64Size(fieldNum, value);
  184. } else {
  185. NSCAssert(NO, @"Unexpected type %d", dataType);
  186. return 0;
  187. }
  188. }
  189. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  190. if (dataType == GPBDataTypeUInt64) {
  191. [stream writeUInt64:fieldNum value:value];
  192. } else if (dataType == GPBDataTypeFixed64) {
  193. [stream writeFixed64:fieldNum value:value];
  194. } else {
  195. NSCAssert(NO, @"Unexpected type %d", dataType);
  196. }
  197. }
  198. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, __unused GPBDataType dataType) {
  199. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  200. return GPBComputeBoolSize(fieldNum, value);
  201. }
  202. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, __unused GPBDataType dataType) {
  203. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  204. [stream writeBool:fieldNum value:value];
  205. }
  206. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, __unused GPBDataType dataType) {
  207. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  208. return GPBComputeEnumSize(fieldNum, value);
  209. }
  210. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, __unused GPBDataType dataType) {
  211. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  212. [stream writeEnum:fieldNum value:value];
  213. }
  214. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, __unused GPBDataType dataType) {
  215. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  216. return GPBComputeFloatSize(fieldNum, value);
  217. }
  218. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, __unused GPBDataType dataType) {
  219. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  220. [stream writeFloat:fieldNum value:value];
  221. }
  222. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, __unused GPBDataType dataType) {
  223. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  224. return GPBComputeDoubleSize(fieldNum, value);
  225. }
  226. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, __unused GPBDataType dataType) {
  227. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  228. [stream writeDouble:fieldNum value:value];
  229. }
  230. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, __unused GPBDataType dataType) {
  231. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  232. return GPBComputeStringSize(fieldNum, value);
  233. }
  234. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, __unused GPBDataType dataType) {
  235. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  236. [stream writeString:fieldNum value:value];
  237. }
  238. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  239. if (dataType == GPBDataTypeMessage) {
  240. return GPBComputeMessageSize(fieldNum, value);
  241. } else if (dataType == GPBDataTypeString) {
  242. return GPBComputeStringSize(fieldNum, value);
  243. } else if (dataType == GPBDataTypeBytes) {
  244. return GPBComputeBytesSize(fieldNum, value);
  245. } else {
  246. NSCAssert(NO, @"Unexpected type %d", dataType);
  247. return 0;
  248. }
  249. }
  250. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  251. if (dataType == GPBDataTypeMessage) {
  252. [stream writeMessage:fieldNum value:value];
  253. } else if (dataType == GPBDataTypeString) {
  254. [stream writeString:fieldNum value:value];
  255. } else if (dataType == GPBDataTypeBytes) {
  256. [stream writeBytes:fieldNum value:value];
  257. } else {
  258. NSCAssert(NO, @"Unexpected type %d", dataType);
  259. }
  260. }
  261. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  262. // clang-format on
  263. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  264. GPBDataType mapValueType = GPBGetFieldDataType(field);
  265. size_t result = 0;
  266. NSString *key;
  267. NSEnumerator *keys = [dict keyEnumerator];
  268. while ((key = [keys nextObject])) {
  269. id obj = dict[key];
  270. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  271. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  272. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  273. }
  274. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  275. result += tagSize * dict.count;
  276. return result;
  277. }
  278. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  279. NSDictionary *dict, GPBFieldDescriptor *field) {
  280. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  281. GPBDataType mapValueType = GPBGetFieldDataType(field);
  282. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  283. NSString *key;
  284. NSEnumerator *keys = [dict keyEnumerator];
  285. while ((key = [keys nextObject])) {
  286. id obj = dict[key];
  287. // Write the tag.
  288. [outputStream writeInt32NoTag:tag];
  289. // Write the size of the message.
  290. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  291. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  292. // Write the size and fields.
  293. [outputStream writeInt32NoTag:(int32_t)msgSize];
  294. [outputStream writeString:kMapKeyFieldNumber value:key];
  295. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  296. }
  297. }
  298. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict,
  299. __unused GPBFieldDescriptor *field) {
  300. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  301. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  302. GPBMessage *msg;
  303. NSEnumerator *objects = [dict objectEnumerator];
  304. while ((msg = [objects nextObject])) {
  305. if (!msg.initialized) {
  306. return NO;
  307. }
  308. }
  309. return YES;
  310. }
  311. // Note: if the type is an object, it the retain pass back to the caller.
  312. static void ReadValue(GPBCodedInputStream *stream, GPBGenericValue *valueToFill, GPBDataType type,
  313. id<GPBExtensionRegistry> registry, GPBFieldDescriptor *field) {
  314. switch (type) {
  315. case GPBDataTypeBool:
  316. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  317. break;
  318. case GPBDataTypeFixed32:
  319. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  320. break;
  321. case GPBDataTypeSFixed32:
  322. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  323. break;
  324. case GPBDataTypeFloat:
  325. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  326. break;
  327. case GPBDataTypeFixed64:
  328. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  329. break;
  330. case GPBDataTypeSFixed64:
  331. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  332. break;
  333. case GPBDataTypeDouble:
  334. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  335. break;
  336. case GPBDataTypeInt32:
  337. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  338. break;
  339. case GPBDataTypeInt64:
  340. valueToFill->valueInt64 = GPBCodedInputStreamReadInt64(&stream->state_);
  341. break;
  342. case GPBDataTypeSInt32:
  343. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  344. break;
  345. case GPBDataTypeSInt64:
  346. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  347. break;
  348. case GPBDataTypeUInt32:
  349. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  350. break;
  351. case GPBDataTypeUInt64:
  352. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  353. break;
  354. case GPBDataTypeBytes:
  355. [valueToFill->valueData release];
  356. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  357. break;
  358. case GPBDataTypeString:
  359. [valueToFill->valueString release];
  360. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  361. break;
  362. case GPBDataTypeMessage: {
  363. GPBMessage *message = [[field.msgClass alloc] init];
  364. [stream readMessage:message extensionRegistry:registry];
  365. [valueToFill->valueMessage release];
  366. valueToFill->valueMessage = message;
  367. break;
  368. }
  369. case GPBDataTypeGroup:
  370. NSCAssert(NO, @"Can't happen");
  371. break;
  372. case GPBDataTypeEnum:
  373. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  374. break;
  375. }
  376. }
  377. void GPBDictionaryReadEntry(id mapDictionary, GPBCodedInputStream *stream,
  378. id<GPBExtensionRegistry> registry, GPBFieldDescriptor *field,
  379. GPBMessage *parentMessage) {
  380. GPBDataType keyDataType = field.mapKeyDataType;
  381. GPBDataType valueDataType = GPBGetFieldDataType(field);
  382. GPBGenericValue key;
  383. GPBGenericValue value;
  384. // Zero them (but pick up any enum default for proto2).
  385. key.valueString = value.valueString = nil;
  386. if (valueDataType == GPBDataTypeEnum) {
  387. value = field.defaultValue;
  388. }
  389. GPBCodedInputStreamState *state = &stream->state_;
  390. uint32_t keyTag = GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  391. uint32_t valueTag =
  392. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  393. BOOL hitError = NO;
  394. while (YES) {
  395. uint32_t tag = GPBCodedInputStreamReadTag(state);
  396. if (tag == keyTag) {
  397. ReadValue(stream, &key, keyDataType, registry, field);
  398. } else if (tag == valueTag) {
  399. ReadValue(stream, &value, valueDataType, registry, field);
  400. } else if (tag == 0) {
  401. // zero signals EOF / limit reached
  402. break;
  403. } else { // Unknown
  404. if (![stream skipField:tag]) {
  405. hitError = YES;
  406. break;
  407. }
  408. }
  409. }
  410. if (!hitError) {
  411. // Handle the special defaults and/or missing key/value.
  412. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  413. key.valueString = [@"" retain];
  414. }
  415. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  416. #pragma clang diagnostic push
  417. #pragma clang diagnostic ignored "-Wswitch-enum"
  418. switch (valueDataType) {
  419. case GPBDataTypeString:
  420. value.valueString = [@"" retain];
  421. break;
  422. case GPBDataTypeBytes:
  423. value.valueData = [GPBEmptyNSData() retain];
  424. break;
  425. #if defined(__clang_analyzer__)
  426. case GPBDataTypeGroup:
  427. // Maps can't really have Groups as the value type, but this case is needed
  428. // so the analyzer won't report the possibility of send nil in for the value
  429. // in the NSMutableDictionary case below.
  430. #endif
  431. case GPBDataTypeMessage: {
  432. value.valueMessage = [[field.msgClass alloc] init];
  433. break;
  434. }
  435. default:
  436. // Nothing
  437. break;
  438. }
  439. #pragma clang diagnostic pop
  440. }
  441. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  442. // mapDictionary is an NSMutableDictionary
  443. [(NSMutableDictionary *)mapDictionary setObject:value.valueString forKey:key.valueString];
  444. } else {
  445. if (valueDataType == GPBDataTypeEnum) {
  446. if (!GPBFieldIsClosedEnum(field) || [field isValidEnumValue:value.valueEnum]) {
  447. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  448. } else {
  449. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  450. forKey:&key
  451. keyDataType:keyDataType];
  452. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  453. }
  454. } else {
  455. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  456. }
  457. }
  458. }
  459. if (GPBDataTypeIsObject(keyDataType)) {
  460. [key.valueString release];
  461. }
  462. if (GPBDataTypeIsObject(valueDataType)) {
  463. [value.valueString release];
  464. }
  465. }
  466. //
  467. // Macros for the common basic cases.
  468. //
  469. // Disable clang-format for the macros.
  470. // clang-format off
  471. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  472. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  473. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  474. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  475. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  476. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  477. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  478. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  479. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  480. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  481. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  482. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  483. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  484. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
  485. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  486. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
  487. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
  488. //%#pragma mark - KEY_NAME -> VALUE_NAME
  489. //%
  490. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  491. //% @package
  492. //% NSMutableDictionary *_dictionary;
  493. //%}
  494. //%
  495. //%- (instancetype)init {
  496. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  497. //%}
  498. //%
  499. //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  500. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  501. //% ##VNAME$S## count:(NSUInteger)count {
  502. //% self = [super init];
  503. //% if (self) {
  504. //% _dictionary = [[NSMutableDictionary alloc] init];
  505. //% if (count && VNAME_VAR##s && keys) {
  506. //% for (NSUInteger i = 0; i < count; ++i) {
  507. //%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])];
  508. //% }
  509. //% }
  510. //% }
  511. //% return self;
  512. //%}
  513. //%
  514. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  515. //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  516. //% if (self) {
  517. //% if (dictionary) {
  518. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  519. //% }
  520. //% }
  521. //% return self;
  522. //%}
  523. //%
  524. //%- (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  525. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  526. //%}
  527. //%
  528. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  529. //%
  530. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  531. //%
  532. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  533. //%
  534. //%@end
  535. //%
  536. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  537. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  538. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  539. //%#pragma mark - KEY_NAME -> VALUE_NAME
  540. //%
  541. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  542. //% @package
  543. //% NSMutableDictionary *_dictionary;
  544. //% GPBEnumValidationFunc _validationFunc;
  545. //%}
  546. //%
  547. //%@synthesize validationFunc = _validationFunc;
  548. //%
  549. //%- (instancetype)init {
  550. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  551. //%}
  552. //%
  553. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  554. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  555. //%}
  556. //%
  557. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  558. //% rawValues:(const VALUE_TYPE [])rawValues
  559. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  560. //% count:(NSUInteger)count {
  561. //% self = [super init];
  562. //% if (self) {
  563. //% _dictionary = [[NSMutableDictionary alloc] init];
  564. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  565. //% if (count && rawValues && keys) {
  566. //% for (NSUInteger i = 0; i < count; ++i) {
  567. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  568. //% }
  569. //% }
  570. //% }
  571. //% return self;
  572. //%}
  573. //%
  574. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  575. //% self = [self initWithValidationFunction:dictionary.validationFunc
  576. //% rawValues:NULL
  577. //% forKeys:NULL
  578. //% count:0];
  579. //% if (self) {
  580. //% if (dictionary) {
  581. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  582. //% }
  583. //% }
  584. //% return self;
  585. //%}
  586. //%
  587. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  588. //% capacity:(__unused NSUInteger)numItems {
  589. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  590. //%}
  591. //%
  592. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
  593. //%
  594. //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  595. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  596. //% if (wrapped && value) {
  597. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  598. //% if (!_validationFunc(result)) {
  599. //% result = kGPBUnrecognizedEnumeratorValue;
  600. //% }
  601. //% *value = result;
  602. //% }
  603. //% return (wrapped != NULL);
  604. //%}
  605. //%
  606. //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
  607. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  608. //% if (wrapped && rawValue) {
  609. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  610. //% }
  611. //% return (wrapped != NULL);
  612. //%}
  613. //%
  614. //%- (void)enumerateKeysAndEnumsUsingBlock:
  615. //% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  616. //% GPBEnumValidationFunc func = _validationFunc;
  617. //% BOOL stop = NO;
  618. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  619. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  620. //% while ((aKey = [keys nextObject])) {
  621. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue = _dictionary[aKey];
  622. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  623. //% if (!func(unwrapped)) {
  624. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  625. //% }
  626. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, &stop);
  627. //% if (stop) {
  628. //% break;
  629. //% }
  630. //% }
  631. //%}
  632. //%
  633. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
  634. //%
  635. //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  636. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  637. //% [NSException raise:NSInvalidArgumentException
  638. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  639. //% value];
  640. //% }
  641. //%
  642. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  643. //% if (_autocreator) {
  644. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  645. //% }
  646. //%}
  647. //%
  648. //%@end
  649. //%
  650. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  651. //%- (void)dealloc {
  652. //% NSAssert(!_autocreator,
  653. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  654. //% [self class], _autocreator);
  655. //% [_dictionary release];
  656. //% [super dealloc];
  657. //%}
  658. //%
  659. //%- (instancetype)copyWithZone:(NSZone *)zone {
  660. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  661. //%}
  662. //%
  663. //%- (BOOL)isEqual:(id)other {
  664. //% if (self == other) {
  665. //% return YES;
  666. //% }
  667. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  668. //% return NO;
  669. //% }
  670. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  671. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  672. //%}
  673. //%
  674. //%- (NSUInteger)hash {
  675. //% return _dictionary.count;
  676. //%}
  677. //%
  678. //%- (NSString *)description {
  679. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  680. //%}
  681. //%
  682. //%- (NSUInteger)count {
  683. //% return _dictionary.count;
  684. //%}
  685. //%
  686. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
  687. //% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  688. //% BOOL stop = NO;
  689. //% NSDictionary *internal = _dictionary;
  690. //% NSEnumerator *keys = [internal keyEnumerator];
  691. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  692. //% while ((aKey = [keys nextObject])) {
  693. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  694. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), &stop);
  695. //% if (stop) {
  696. //% break;
  697. //% }
  698. //% }
  699. //%}
  700. //%
  701. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  702. //% NSDictionary *internal = _dictionary;
  703. //% NSUInteger count = internal.count;
  704. //% if (count == 0) {
  705. //% return 0;
  706. //% }
  707. //%
  708. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  709. //% GPBDataType keyDataType = field.mapKeyDataType;
  710. //% size_t result = 0;
  711. //% NSEnumerator *keys = [internal keyEnumerator];
  712. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  713. //% while ((aKey = [keys nextObject])) {
  714. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  715. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  716. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  717. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  718. //% }
  719. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  720. //% result += tagSize * count;
  721. //% return result;
  722. //%}
  723. //%
  724. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  725. //% asField:(GPBFieldDescriptor *)field {
  726. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  727. //% GPBDataType keyDataType = field.mapKeyDataType;
  728. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  729. //% NSDictionary *internal = _dictionary;
  730. //% NSEnumerator *keys = [internal keyEnumerator];
  731. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  732. //% while ((aKey = [keys nextObject])) {
  733. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  734. //% [outputStream writeInt32NoTag:tag];
  735. //% // Write the size of the message.
  736. //% KEY_TYPE KisP##unwrappedKey = UNWRAP##KEY_NAME(aKey);
  737. //% VALUE_TYPE unwrappedValue = UNWRAP##VALUE_NAME(a##VNAME_VAR$u);
  738. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  739. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  740. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  741. //% // Write the fields.
  742. //% WriteDict##KEY_NAME##Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  743. //% WriteDict##VALUE_NAME##Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  744. //% }
  745. //%}
  746. //%
  747. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  748. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  749. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  750. //%}
  751. //%
  752. //%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  753. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, __unused BOOL *stop) {
  754. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
  755. //% }];
  756. //%}
  757. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  758. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
  759. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
  760. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  761. //% if (otherDictionary) {
  762. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  763. //% if (_autocreator) {
  764. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  765. //% }
  766. //% }
  767. //%}
  768. //%
  769. //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
  770. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
  771. //% if (_autocreator) {
  772. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  773. //% }
  774. //%}
  775. //%
  776. //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  777. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  778. //%}
  779. //%
  780. //%- (void)removeAll {
  781. //% [_dictionary removeAllObjects];
  782. //%}
  783. //
  784. // Custom Generation for Bool keys
  785. //
  786. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  787. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
  788. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  789. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
  790. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
  791. //%#pragma mark - Bool -> VALUE_NAME
  792. //%
  793. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  794. //% @package
  795. //% VALUE_TYPE _values[2];
  796. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  797. //%
  798. //%- (instancetype)init {
  799. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  800. //%}
  801. //%
  802. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  803. //%
  804. //%- (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  805. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  806. //%}
  807. //%
  808. //%BOOL_DICT_DEALLOC##HELPER()
  809. //%
  810. //%- (instancetype)copyWithZone:(NSZone *)zone {
  811. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  812. //%}
  813. //%
  814. //%- (BOOL)isEqual:(id)other {
  815. //% if (self == other) {
  816. //% return YES;
  817. //% }
  818. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  819. //% return NO;
  820. //% }
  821. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  822. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  823. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  824. //% return NO;
  825. //% }
  826. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  827. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  828. //% return NO;
  829. //% }
  830. //% return YES;
  831. //%}
  832. //%
  833. //%- (NSUInteger)hash {
  834. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  835. //%}
  836. //%
  837. //%- (NSString *)description {
  838. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  839. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  840. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  841. //% }
  842. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  843. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  844. //% }
  845. //% [result appendString:@" }"];
  846. //% return result;
  847. //%}
  848. //%
  849. //%- (NSUInteger)count {
  850. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  851. //%}
  852. //%
  853. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
  854. //%
  855. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  856. //%
  857. //%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  858. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  859. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  860. //% }
  861. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  862. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  863. //% }
  864. //%}
  865. //%
  866. //%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
  867. //% (void (NS_NOESCAPE ^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  868. //% BOOL stop = NO;
  869. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  870. //% block(NO, _values[0], &stop);
  871. //% }
  872. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  873. //% block(YES, _values[1], &stop);
  874. //% }
  875. //%}
  876. //%
  877. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  878. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  879. //% NSUInteger count = 0;
  880. //% size_t result = 0;
  881. //% for (int i = 0; i < 2; ++i) {
  882. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  883. //% ++count;
  884. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  885. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  886. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  887. //% }
  888. //% }
  889. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  890. //% result += tagSize * count;
  891. //% return result;
  892. //%}
  893. //%
  894. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  895. //% asField:(GPBFieldDescriptor *)field {
  896. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  897. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  898. //% for (int i = 0; i < 2; ++i) {
  899. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  900. //% // Write the tag.
  901. //% [outputStream writeInt32NoTag:tag];
  902. //% // Write the size of the message.
  903. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  904. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  905. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  906. //% // Write the fields.
  907. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  908. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  909. //% }
  910. //% }
  911. //%}
  912. //%
  913. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  914. //%
  915. //%@end
  916. //%
  917. //
  918. // Helpers for PODs
  919. //
  920. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  921. //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
  922. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  923. //% if (wrapped && value) {
  924. //% *value = UNWRAP##VALUE_NAME(wrapped);
  925. //% }
  926. //% return (wrapped != NULL);
  927. //%}
  928. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  929. //%@(VALUE)
  930. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  931. //%[VALUE unsignedIntValue]
  932. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  933. //%[VALUE intValue]
  934. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  935. //%[VALUE unsignedLongLongValue]
  936. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  937. //%[VALUE longLongValue]
  938. //%PDDM-DEFINE UNWRAPBool(VALUE)
  939. //%[VALUE boolValue]
  940. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  941. //%[VALUE floatValue]
  942. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  943. //%[VALUE doubleValue]
  944. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  945. //%[VALUE intValue]
  946. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  947. //%[NSString stringWithFormat:@"%u", VALUE]
  948. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  949. //%[NSString stringWithFormat:@"%d", VALUE]
  950. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  951. //%[NSString stringWithFormat:@"%llu", VALUE]
  952. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  953. //%[NSString stringWithFormat:@"%lld", VALUE]
  954. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  955. //%(VALUE ? @"true" : @"false")
  956. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  957. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  958. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  959. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  960. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  961. //%@(VALUE)
  962. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  963. //%NSNumber *
  964. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  965. //%VAL1 != VAL2
  966. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  967. // Empty
  968. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  969. // Empty
  970. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  971. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  972. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  973. // Empty
  974. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  975. // Empty
  976. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  977. // Empty
  978. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  979. // Empty
  980. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  981. // Empty
  982. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  983. // Empty
  984. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  985. // Empty
  986. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  987. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  988. //% forKey:(GPBGenericValue *)key
  989. //% keyDataType:(GPBDataType)keyDataType {
  990. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  991. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  992. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  993. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  994. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  995. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  996. //% [outputStream flush];
  997. //% [outputStream release];
  998. //% return data;
  999. //%}
  1000. //%
  1001. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1002. //%value##VALUE_NAME
  1003. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1004. // Empty
  1005. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1006. // Empty
  1007. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1008. //% BOOL _valueSet[2];
  1009. //%
  1010. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1011. //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
  1012. //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys
  1013. //% ##VALUE_NAME$S## count:(NSUInteger)count {
  1014. //% self = [super init];
  1015. //% if (self) {
  1016. //% for (NSUInteger i = 0; i < count; ++i) {
  1017. //% int idx = keys[i] ? 1 : 0;
  1018. //% _values[idx] = values[i];
  1019. //% _valueSet[idx] = YES;
  1020. //% }
  1021. //% }
  1022. //% return self;
  1023. //%}
  1024. //%
  1025. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1026. //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
  1027. //% if (self) {
  1028. //% if (dictionary) {
  1029. //% for (int i = 0; i < 2; ++i) {
  1030. //% if (dictionary->_valueSet[i]) {
  1031. //% _values[i] = dictionary->_values[i];
  1032. //% _valueSet[i] = YES;
  1033. //% }
  1034. //% }
  1035. //% }
  1036. //% }
  1037. //% return self;
  1038. //%}
  1039. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1040. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1041. //%- (void)dealloc {
  1042. //% NSAssert(!_autocreator,
  1043. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1044. //% [self class], _autocreator);
  1045. //% [super dealloc];
  1046. //%}
  1047. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1048. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1049. //%BOOL_DICT_HASPOD(IDX, REF)
  1050. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1051. //%REF##_valueSet[IDX]
  1052. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
  1053. //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
  1054. //% int idx = (key ? 1 : 0);
  1055. //% if (_valueSet[idx]) {
  1056. //% if (value) {
  1057. //% *value = _values[idx];
  1058. //% }
  1059. //% return YES;
  1060. //% }
  1061. //% return NO;
  1062. //%}
  1063. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1064. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1065. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1066. //% int idx = (key->valueBool ? 1 : 0);
  1067. //% _values[idx] = value->value##VALUE_NAME;
  1068. //% _valueSet[idx] = YES;
  1069. //%}
  1070. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1071. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1072. //% if (otherDictionary) {
  1073. //% for (int i = 0; i < 2; ++i) {
  1074. //% if (otherDictionary->_valueSet[i]) {
  1075. //% _valueSet[i] = YES;
  1076. //% _values[i] = otherDictionary->_values[i];
  1077. //% }
  1078. //% }
  1079. //% if (_autocreator) {
  1080. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1081. //% }
  1082. //% }
  1083. //%}
  1084. //%
  1085. //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
  1086. //% int idx = (key ? 1 : 0);
  1087. //% _values[idx] = value;
  1088. //% _valueSet[idx] = YES;
  1089. //% if (_autocreator) {
  1090. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1091. //% }
  1092. //%}
  1093. //%
  1094. //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
  1095. //% _valueSet[aKey ? 1 : 0] = NO;
  1096. //%}
  1097. //%
  1098. //%- (void)removeAll {
  1099. //% _valueSet[0] = NO;
  1100. //% _valueSet[1] = NO;
  1101. //%}
  1102. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1103. //%STR_FORMAT_##VALUE_NAME()
  1104. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1105. //%%u
  1106. //%PDDM-DEFINE STR_FORMAT_Int32()
  1107. //%%d
  1108. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1109. //%%llu
  1110. //%PDDM-DEFINE STR_FORMAT_Int64()
  1111. //%%lld
  1112. //%PDDM-DEFINE STR_FORMAT_Bool()
  1113. //%%d
  1114. //%PDDM-DEFINE STR_FORMAT_Float()
  1115. //%%f
  1116. //%PDDM-DEFINE STR_FORMAT_Double()
  1117. //%%lf
  1118. //
  1119. // Helpers for Objects
  1120. //
  1121. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1122. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1123. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1124. //% return result;
  1125. //%}
  1126. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1127. //%VALUE
  1128. //%PDDM-DEFINE UNWRAPString(VALUE)
  1129. //%VALUE
  1130. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1131. //%VALUE
  1132. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1133. //%VALUE
  1134. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1135. //%VALUE
  1136. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1137. //%ENUM_TYPEOBJECT_##TYPE()
  1138. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1139. //%NSString *
  1140. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1141. //%id ##
  1142. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1143. //%![VAL1 isEqual:VAL2]
  1144. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1145. //%- (BOOL)isInitialized {
  1146. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1147. //% if (!msg.initialized) {
  1148. //% return NO;
  1149. //% }
  1150. //% }
  1151. //% return YES;
  1152. //%}
  1153. //%
  1154. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1155. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1156. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1157. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  1158. //% id aKey;
  1159. //% NSMutableDictionary *internalDict = newDict->_dictionary;
  1160. //% while ((aKey = [keys nextObject])) {
  1161. //% GPBMessage *msg = _dictionary[aKey];
  1162. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1163. //% [internalDict setObject:copiedMsg forKey:aKey];
  1164. //% [copiedMsg release];
  1165. //% }
  1166. //% return newDict;
  1167. //%}
  1168. //%
  1169. //%
  1170. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1171. //%- (BOOL)isInitialized {
  1172. //% if (_values[0] && ![_values[0] isInitialized]) {
  1173. //% return NO;
  1174. //% }
  1175. //% if (_values[1] && ![_values[1] isInitialized]) {
  1176. //% return NO;
  1177. //% }
  1178. //% return YES;
  1179. //%}
  1180. //%
  1181. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1182. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1183. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1184. //% for (int i = 0; i < 2; ++i) {
  1185. //% if (_values[i] != nil) {
  1186. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1187. //% }
  1188. //% }
  1189. //% return newDict;
  1190. //%}
  1191. //%
  1192. //%
  1193. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1194. // Empty
  1195. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1196. //%valueString
  1197. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1198. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1199. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1200. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1201. //%##EXTRA_INDENT$S## }
  1202. //%
  1203. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1204. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1205. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1206. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1207. //%##EXTRA_INDENT$S## }
  1208. //%
  1209. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1210. // Empty
  1211. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1212. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1213. //% forKeys:(const BOOL [])keys
  1214. //% count:(NSUInteger)count {
  1215. //% self = [super init];
  1216. //% if (self) {
  1217. //% for (NSUInteger i = 0; i < count; ++i) {
  1218. //% if (!objects[i]) {
  1219. //% [NSException raise:NSInvalidArgumentException
  1220. //% format:@"Attempting to add nil object to a Dictionary"];
  1221. //% }
  1222. //% int idx = keys[i] ? 1 : 0;
  1223. //% [_values[idx] release];
  1224. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1225. //% }
  1226. //% }
  1227. //% return self;
  1228. //%}
  1229. //%
  1230. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1231. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1232. //% if (self) {
  1233. //% if (dictionary) {
  1234. //% _values[0] = [dictionary->_values[0] retain];
  1235. //% _values[1] = [dictionary->_values[1] retain];
  1236. //% }
  1237. //% }
  1238. //% return self;
  1239. //%}
  1240. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1241. //%- (void)dealloc {
  1242. //% NSAssert(!_autocreator,
  1243. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1244. //% [self class], _autocreator);
  1245. //% [_values[0] release];
  1246. //% [_values[1] release];
  1247. //% [super dealloc];
  1248. //%}
  1249. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1250. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1251. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1252. //%REF##_values[IDX] != nil
  1253. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
  1254. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1255. //% return _values[key ? 1 : 0];
  1256. //%}
  1257. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1258. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1259. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1260. //% int idx = (key->valueBool ? 1 : 0);
  1261. //% [_values[idx] release];
  1262. //% _values[idx] = [value->valueString retain];
  1263. //%}
  1264. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1265. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1266. //% if (otherDictionary) {
  1267. //% for (int i = 0; i < 2; ++i) {
  1268. //% if (otherDictionary->_values[i] != nil) {
  1269. //% [_values[i] release];
  1270. //% _values[i] = [otherDictionary->_values[i] retain];
  1271. //% }
  1272. //% }
  1273. //% if (_autocreator) {
  1274. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1275. //% }
  1276. //% }
  1277. //%}
  1278. //%
  1279. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1280. //% if (!object) {
  1281. //% [NSException raise:NSInvalidArgumentException
  1282. //% format:@"Attempting to add nil object to a Dictionary"];
  1283. //% }
  1284. //% int idx = (key ? 1 : 0);
  1285. //% [_values[idx] release];
  1286. //% _values[idx] = [object retain];
  1287. //% if (_autocreator) {
  1288. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1289. //% }
  1290. //%}
  1291. //%
  1292. //%- (void)removeObjectForKey:(BOOL)aKey {
  1293. //% int idx = (aKey ? 1 : 0);
  1294. //% [_values[idx] release];
  1295. //% _values[idx] = nil;
  1296. //%}
  1297. //%
  1298. //%- (void)removeAll {
  1299. //% for (int i = 0; i < 2; ++i) {
  1300. //% [_values[i] release];
  1301. //% _values[i] = nil;
  1302. //% }
  1303. //%}
  1304. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1305. //%%@
  1306. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1307. // This block of code is generated, do not edit it directly.
  1308. #pragma mark - UInt32 -> UInt32
  1309. @implementation GPBUInt32UInt32Dictionary {
  1310. @package
  1311. NSMutableDictionary *_dictionary;
  1312. }
  1313. - (instancetype)init {
  1314. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1315. }
  1316. - (instancetype)initWithUInt32s:(const uint32_t [])values
  1317. forKeys:(const uint32_t [])keys
  1318. count:(NSUInteger)count {
  1319. self = [super init];
  1320. if (self) {
  1321. _dictionary = [[NSMutableDictionary alloc] init];
  1322. if (count && values && keys) {
  1323. for (NSUInteger i = 0; i < count; ++i) {
  1324. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1325. }
  1326. }
  1327. }
  1328. return self;
  1329. }
  1330. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1331. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  1332. if (self) {
  1333. if (dictionary) {
  1334. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1335. }
  1336. }
  1337. return self;
  1338. }
  1339. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1340. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1341. }
  1342. - (void)dealloc {
  1343. NSAssert(!_autocreator,
  1344. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1345. [self class], _autocreator);
  1346. [_dictionary release];
  1347. [super dealloc];
  1348. }
  1349. - (instancetype)copyWithZone:(NSZone *)zone {
  1350. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1351. }
  1352. - (BOOL)isEqual:(id)other {
  1353. if (self == other) {
  1354. return YES;
  1355. }
  1356. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1357. return NO;
  1358. }
  1359. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1360. return [_dictionary isEqual:otherDictionary->_dictionary];
  1361. }
  1362. - (NSUInteger)hash {
  1363. return _dictionary.count;
  1364. }
  1365. - (NSString *)description {
  1366. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1367. }
  1368. - (NSUInteger)count {
  1369. return _dictionary.count;
  1370. }
  1371. - (void)enumerateKeysAndUInt32sUsingBlock:
  1372. (void (NS_NOESCAPE ^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1373. BOOL stop = NO;
  1374. NSDictionary *internal = _dictionary;
  1375. NSEnumerator *keys = [internal keyEnumerator];
  1376. NSNumber *aKey;
  1377. while ((aKey = [keys nextObject])) {
  1378. NSNumber *aValue = internal[aKey];
  1379. block([aKey unsignedIntValue], [aValue unsignedIntValue], &stop);
  1380. if (stop) {
  1381. break;
  1382. }
  1383. }
  1384. }
  1385. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1386. NSDictionary *internal = _dictionary;
  1387. NSUInteger count = internal.count;
  1388. if (count == 0) {
  1389. return 0;
  1390. }
  1391. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1392. GPBDataType keyDataType = field.mapKeyDataType;
  1393. size_t result = 0;
  1394. NSEnumerator *keys = [internal keyEnumerator];
  1395. NSNumber *aKey;
  1396. while ((aKey = [keys nextObject])) {
  1397. NSNumber *aValue = internal[aKey];
  1398. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1399. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1400. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1401. }
  1402. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1403. result += tagSize * count;
  1404. return result;
  1405. }
  1406. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1407. asField:(GPBFieldDescriptor *)field {
  1408. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1409. GPBDataType keyDataType = field.mapKeyDataType;
  1410. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1411. NSDictionary *internal = _dictionary;
  1412. NSEnumerator *keys = [internal keyEnumerator];
  1413. NSNumber *aKey;
  1414. while ((aKey = [keys nextObject])) {
  1415. NSNumber *aValue = internal[aKey];
  1416. [outputStream writeInt32NoTag:tag];
  1417. // Write the size of the message.
  1418. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1419. uint32_t unwrappedValue = [aValue unsignedIntValue];
  1420. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1421. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1422. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1423. // Write the fields.
  1424. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1425. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1426. }
  1427. }
  1428. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1429. forGPBGenericValueKey:(GPBGenericValue *)key {
  1430. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1431. }
  1432. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1433. [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, __unused BOOL *stop) {
  1434. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1435. }];
  1436. }
  1437. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
  1438. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1439. if (wrapped && value) {
  1440. *value = [wrapped unsignedIntValue];
  1441. }
  1442. return (wrapped != NULL);
  1443. }
  1444. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1445. if (otherDictionary) {
  1446. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1447. if (_autocreator) {
  1448. GPBAutocreatedDictionaryModified(_autocreator, self);
  1449. }
  1450. }
  1451. }
  1452. - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
  1453. [_dictionary setObject:@(value) forKey:@(key)];
  1454. if (_autocreator) {
  1455. GPBAutocreatedDictionaryModified(_autocreator, self);
  1456. }
  1457. }
  1458. - (void)removeUInt32ForKey:(uint32_t)aKey {
  1459. [_dictionary removeObjectForKey:@(aKey)];
  1460. }
  1461. - (void)removeAll {
  1462. [_dictionary removeAllObjects];
  1463. }
  1464. @end
  1465. #pragma mark - UInt32 -> Int32
  1466. @implementation GPBUInt32Int32Dictionary {
  1467. @package
  1468. NSMutableDictionary *_dictionary;
  1469. }
  1470. - (instancetype)init {
  1471. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1472. }
  1473. - (instancetype)initWithInt32s:(const int32_t [])values
  1474. forKeys:(const uint32_t [])keys
  1475. count:(NSUInteger)count {
  1476. self = [super init];
  1477. if (self) {
  1478. _dictionary = [[NSMutableDictionary alloc] init];
  1479. if (count && values && keys) {
  1480. for (NSUInteger i = 0; i < count; ++i) {
  1481. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1482. }
  1483. }
  1484. }
  1485. return self;
  1486. }
  1487. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1488. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  1489. if (self) {
  1490. if (dictionary) {
  1491. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1492. }
  1493. }
  1494. return self;
  1495. }
  1496. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1497. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1498. }
  1499. - (void)dealloc {
  1500. NSAssert(!_autocreator,
  1501. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1502. [self class], _autocreator);
  1503. [_dictionary release];
  1504. [super dealloc];
  1505. }
  1506. - (instancetype)copyWithZone:(NSZone *)zone {
  1507. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1508. }
  1509. - (BOOL)isEqual:(id)other {
  1510. if (self == other) {
  1511. return YES;
  1512. }
  1513. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1514. return NO;
  1515. }
  1516. GPBUInt32Int32Dictionary *otherDictionary = other;
  1517. return [_dictionary isEqual:otherDictionary->_dictionary];
  1518. }
  1519. - (NSUInteger)hash {
  1520. return _dictionary.count;
  1521. }
  1522. - (NSString *)description {
  1523. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1524. }
  1525. - (NSUInteger)count {
  1526. return _dictionary.count;
  1527. }
  1528. - (void)enumerateKeysAndInt32sUsingBlock:
  1529. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  1530. BOOL stop = NO;
  1531. NSDictionary *internal = _dictionary;
  1532. NSEnumerator *keys = [internal keyEnumerator];
  1533. NSNumber *aKey;
  1534. while ((aKey = [keys nextObject])) {
  1535. NSNumber *aValue = internal[aKey];
  1536. block([aKey unsignedIntValue], [aValue intValue], &stop);
  1537. if (stop) {
  1538. break;
  1539. }
  1540. }
  1541. }
  1542. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1543. NSDictionary *internal = _dictionary;
  1544. NSUInteger count = internal.count;
  1545. if (count == 0) {
  1546. return 0;
  1547. }
  1548. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1549. GPBDataType keyDataType = field.mapKeyDataType;
  1550. size_t result = 0;
  1551. NSEnumerator *keys = [internal keyEnumerator];
  1552. NSNumber *aKey;
  1553. while ((aKey = [keys nextObject])) {
  1554. NSNumber *aValue = internal[aKey];
  1555. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1556. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1557. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1558. }
  1559. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1560. result += tagSize * count;
  1561. return result;
  1562. }
  1563. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1564. asField:(GPBFieldDescriptor *)field {
  1565. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1566. GPBDataType keyDataType = field.mapKeyDataType;
  1567. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1568. NSDictionary *internal = _dictionary;
  1569. NSEnumerator *keys = [internal keyEnumerator];
  1570. NSNumber *aKey;
  1571. while ((aKey = [keys nextObject])) {
  1572. NSNumber *aValue = internal[aKey];
  1573. [outputStream writeInt32NoTag:tag];
  1574. // Write the size of the message.
  1575. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1576. int32_t unwrappedValue = [aValue intValue];
  1577. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1578. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1579. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1580. // Write the fields.
  1581. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1582. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1583. }
  1584. }
  1585. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1586. forGPBGenericValueKey:(GPBGenericValue *)key {
  1587. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1588. }
  1589. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1590. [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, __unused BOOL *stop) {
  1591. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1592. }];
  1593. }
  1594. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
  1595. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1596. if (wrapped && value) {
  1597. *value = [wrapped intValue];
  1598. }
  1599. return (wrapped != NULL);
  1600. }
  1601. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1602. if (otherDictionary) {
  1603. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1604. if (_autocreator) {
  1605. GPBAutocreatedDictionaryModified(_autocreator, self);
  1606. }
  1607. }
  1608. }
  1609. - (void)setInt32:(int32_t)value forKey:(uint32_t)key {
  1610. [_dictionary setObject:@(value) forKey:@(key)];
  1611. if (_autocreator) {
  1612. GPBAutocreatedDictionaryModified(_autocreator, self);
  1613. }
  1614. }
  1615. - (void)removeInt32ForKey:(uint32_t)aKey {
  1616. [_dictionary removeObjectForKey:@(aKey)];
  1617. }
  1618. - (void)removeAll {
  1619. [_dictionary removeAllObjects];
  1620. }
  1621. @end
  1622. #pragma mark - UInt32 -> UInt64
  1623. @implementation GPBUInt32UInt64Dictionary {
  1624. @package
  1625. NSMutableDictionary *_dictionary;
  1626. }
  1627. - (instancetype)init {
  1628. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1629. }
  1630. - (instancetype)initWithUInt64s:(const uint64_t [])values
  1631. forKeys:(const uint32_t [])keys
  1632. count:(NSUInteger)count {
  1633. self = [super init];
  1634. if (self) {
  1635. _dictionary = [[NSMutableDictionary alloc] init];
  1636. if (count && values && keys) {
  1637. for (NSUInteger i = 0; i < count; ++i) {
  1638. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1639. }
  1640. }
  1641. }
  1642. return self;
  1643. }
  1644. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1645. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  1646. if (self) {
  1647. if (dictionary) {
  1648. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1649. }
  1650. }
  1651. return self;
  1652. }
  1653. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1654. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1655. }
  1656. - (void)dealloc {
  1657. NSAssert(!_autocreator,
  1658. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1659. [self class], _autocreator);
  1660. [_dictionary release];
  1661. [super dealloc];
  1662. }
  1663. - (instancetype)copyWithZone:(NSZone *)zone {
  1664. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1665. }
  1666. - (BOOL)isEqual:(id)other {
  1667. if (self == other) {
  1668. return YES;
  1669. }
  1670. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1671. return NO;
  1672. }
  1673. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1674. return [_dictionary isEqual:otherDictionary->_dictionary];
  1675. }
  1676. - (NSUInteger)hash {
  1677. return _dictionary.count;
  1678. }
  1679. - (NSString *)description {
  1680. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1681. }
  1682. - (NSUInteger)count {
  1683. return _dictionary.count;
  1684. }
  1685. - (void)enumerateKeysAndUInt64sUsingBlock:
  1686. (void (NS_NOESCAPE ^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1687. BOOL stop = NO;
  1688. NSDictionary *internal = _dictionary;
  1689. NSEnumerator *keys = [internal keyEnumerator];
  1690. NSNumber *aKey;
  1691. while ((aKey = [keys nextObject])) {
  1692. NSNumber *aValue = internal[aKey];
  1693. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], &stop);
  1694. if (stop) {
  1695. break;
  1696. }
  1697. }
  1698. }
  1699. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1700. NSDictionary *internal = _dictionary;
  1701. NSUInteger count = internal.count;
  1702. if (count == 0) {
  1703. return 0;
  1704. }
  1705. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1706. GPBDataType keyDataType = field.mapKeyDataType;
  1707. size_t result = 0;
  1708. NSEnumerator *keys = [internal keyEnumerator];
  1709. NSNumber *aKey;
  1710. while ((aKey = [keys nextObject])) {
  1711. NSNumber *aValue = internal[aKey];
  1712. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1713. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1714. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1715. }
  1716. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1717. result += tagSize * count;
  1718. return result;
  1719. }
  1720. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1721. asField:(GPBFieldDescriptor *)field {
  1722. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1723. GPBDataType keyDataType = field.mapKeyDataType;
  1724. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1725. NSDictionary *internal = _dictionary;
  1726. NSEnumerator *keys = [internal keyEnumerator];
  1727. NSNumber *aKey;
  1728. while ((aKey = [keys nextObject])) {
  1729. NSNumber *aValue = internal[aKey];
  1730. [outputStream writeInt32NoTag:tag];
  1731. // Write the size of the message.
  1732. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1733. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  1734. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1735. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1736. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1737. // Write the fields.
  1738. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1739. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1740. }
  1741. }
  1742. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1743. forGPBGenericValueKey:(GPBGenericValue *)key {
  1744. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1745. }
  1746. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1747. [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, __unused BOOL *stop) {
  1748. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1749. }];
  1750. }
  1751. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
  1752. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1753. if (wrapped && value) {
  1754. *value = [wrapped unsignedLongLongValue];
  1755. }
  1756. return (wrapped != NULL);
  1757. }
  1758. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1759. if (otherDictionary) {
  1760. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1761. if (_autocreator) {
  1762. GPBAutocreatedDictionaryModified(_autocreator, self);
  1763. }
  1764. }
  1765. }
  1766. - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
  1767. [_dictionary setObject:@(value) forKey:@(key)];
  1768. if (_autocreator) {
  1769. GPBAutocreatedDictionaryModified(_autocreator, self);
  1770. }
  1771. }
  1772. - (void)removeUInt64ForKey:(uint32_t)aKey {
  1773. [_dictionary removeObjectForKey:@(aKey)];
  1774. }
  1775. - (void)removeAll {
  1776. [_dictionary removeAllObjects];
  1777. }
  1778. @end
  1779. #pragma mark - UInt32 -> Int64
  1780. @implementation GPBUInt32Int64Dictionary {
  1781. @package
  1782. NSMutableDictionary *_dictionary;
  1783. }
  1784. - (instancetype)init {
  1785. return [self initWithInt64s:NULL forKeys:NULL count:0];
  1786. }
  1787. - (instancetype)initWithInt64s:(const int64_t [])values
  1788. forKeys:(const uint32_t [])keys
  1789. count:(NSUInteger)count {
  1790. self = [super init];
  1791. if (self) {
  1792. _dictionary = [[NSMutableDictionary alloc] init];
  1793. if (count && values && keys) {
  1794. for (NSUInteger i = 0; i < count; ++i) {
  1795. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1796. }
  1797. }
  1798. }
  1799. return self;
  1800. }
  1801. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1802. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  1803. if (self) {
  1804. if (dictionary) {
  1805. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1806. }
  1807. }
  1808. return self;
  1809. }
  1810. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1811. return [self initWithInt64s:NULL forKeys:NULL count:0];
  1812. }
  1813. - (void)dealloc {
  1814. NSAssert(!_autocreator,
  1815. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1816. [self class], _autocreator);
  1817. [_dictionary release];
  1818. [super dealloc];
  1819. }
  1820. - (instancetype)copyWithZone:(NSZone *)zone {
  1821. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  1822. }
  1823. - (BOOL)isEqual:(id)other {
  1824. if (self == other) {
  1825. return YES;
  1826. }
  1827. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  1828. return NO;
  1829. }
  1830. GPBUInt32Int64Dictionary *otherDictionary = other;
  1831. return [_dictionary isEqual:otherDictionary->_dictionary];
  1832. }
  1833. - (NSUInteger)hash {
  1834. return _dictionary.count;
  1835. }
  1836. - (NSString *)description {
  1837. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1838. }
  1839. - (NSUInteger)count {
  1840. return _dictionary.count;
  1841. }
  1842. - (void)enumerateKeysAndInt64sUsingBlock:
  1843. (void (NS_NOESCAPE ^)(uint32_t key, int64_t value, BOOL *stop))block {
  1844. BOOL stop = NO;
  1845. NSDictionary *internal = _dictionary;
  1846. NSEnumerator *keys = [internal keyEnumerator];
  1847. NSNumber *aKey;
  1848. while ((aKey = [keys nextObject])) {
  1849. NSNumber *aValue = internal[aKey];
  1850. block([aKey unsignedIntValue], [aValue longLongValue], &stop);
  1851. if (stop) {
  1852. break;
  1853. }
  1854. }
  1855. }
  1856. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1857. NSDictionary *internal = _dictionary;
  1858. NSUInteger count = internal.count;
  1859. if (count == 0) {
  1860. return 0;
  1861. }
  1862. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1863. GPBDataType keyDataType = field.mapKeyDataType;
  1864. size_t result = 0;
  1865. NSEnumerator *keys = [internal keyEnumerator];
  1866. NSNumber *aKey;
  1867. while ((aKey = [keys nextObject])) {
  1868. NSNumber *aValue = internal[aKey];
  1869. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1870. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  1871. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1872. }
  1873. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1874. result += tagSize * count;
  1875. return result;
  1876. }
  1877. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1878. asField:(GPBFieldDescriptor *)field {
  1879. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1880. GPBDataType keyDataType = field.mapKeyDataType;
  1881. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1882. NSDictionary *internal = _dictionary;
  1883. NSEnumerator *keys = [internal keyEnumerator];
  1884. NSNumber *aKey;
  1885. while ((aKey = [keys nextObject])) {
  1886. NSNumber *aValue = internal[aKey];
  1887. [outputStream writeInt32NoTag:tag];
  1888. // Write the size of the message.
  1889. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1890. int64_t unwrappedValue = [aValue longLongValue];
  1891. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1892. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1893. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1894. // Write the fields.
  1895. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1896. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1897. }
  1898. }
  1899. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1900. forGPBGenericValueKey:(GPBGenericValue *)key {
  1901. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  1902. }
  1903. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1904. [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, __unused BOOL *stop) {
  1905. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  1906. }];
  1907. }
  1908. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
  1909. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1910. if (wrapped && value) {
  1911. *value = [wrapped longLongValue];
  1912. }
  1913. return (wrapped != NULL);
  1914. }
  1915. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  1916. if (otherDictionary) {
  1917. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1918. if (_autocreator) {
  1919. GPBAutocreatedDictionaryModified(_autocreator, self);
  1920. }
  1921. }
  1922. }
  1923. - (void)setInt64:(int64_t)value forKey:(uint32_t)key {
  1924. [_dictionary setObject:@(value) forKey:@(key)];
  1925. if (_autocreator) {
  1926. GPBAutocreatedDictionaryModified(_autocreator, self);
  1927. }
  1928. }
  1929. - (void)removeInt64ForKey:(uint32_t)aKey {
  1930. [_dictionary removeObjectForKey:@(aKey)];
  1931. }
  1932. - (void)removeAll {
  1933. [_dictionary removeAllObjects];
  1934. }
  1935. @end
  1936. #pragma mark - UInt32 -> Bool
  1937. @implementation GPBUInt32BoolDictionary {
  1938. @package
  1939. NSMutableDictionary *_dictionary;
  1940. }
  1941. - (instancetype)init {
  1942. return [self initWithBools:NULL forKeys:NULL count:0];
  1943. }
  1944. - (instancetype)initWithBools:(const BOOL [])values
  1945. forKeys:(const uint32_t [])keys
  1946. count:(NSUInteger)count {
  1947. self = [super init];
  1948. if (self) {
  1949. _dictionary = [[NSMutableDictionary alloc] init];
  1950. if (count && values && keys) {
  1951. for (NSUInteger i = 0; i < count; ++i) {
  1952. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1953. }
  1954. }
  1955. }
  1956. return self;
  1957. }
  1958. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  1959. self = [self initWithBools:NULL forKeys:NULL count:0];
  1960. if (self) {
  1961. if (dictionary) {
  1962. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1963. }
  1964. }
  1965. return self;
  1966. }
  1967. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  1968. return [self initWithBools:NULL forKeys:NULL count:0];
  1969. }
  1970. - (void)dealloc {
  1971. NSAssert(!_autocreator,
  1972. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1973. [self class], _autocreator);
  1974. [_dictionary release];
  1975. [super dealloc];
  1976. }
  1977. - (instancetype)copyWithZone:(NSZone *)zone {
  1978. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  1979. }
  1980. - (BOOL)isEqual:(id)other {
  1981. if (self == other) {
  1982. return YES;
  1983. }
  1984. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  1985. return NO;
  1986. }
  1987. GPBUInt32BoolDictionary *otherDictionary = other;
  1988. return [_dictionary isEqual:otherDictionary->_dictionary];
  1989. }
  1990. - (NSUInteger)hash {
  1991. return _dictionary.count;
  1992. }
  1993. - (NSString *)description {
  1994. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1995. }
  1996. - (NSUInteger)count {
  1997. return _dictionary.count;
  1998. }
  1999. - (void)enumerateKeysAndBoolsUsingBlock:
  2000. (void (NS_NOESCAPE ^)(uint32_t key, BOOL value, BOOL *stop))block {
  2001. BOOL stop = NO;
  2002. NSDictionary *internal = _dictionary;
  2003. NSEnumerator *keys = [internal keyEnumerator];
  2004. NSNumber *aKey;
  2005. while ((aKey = [keys nextObject])) {
  2006. NSNumber *aValue = internal[aKey];
  2007. block([aKey unsignedIntValue], [aValue boolValue], &stop);
  2008. if (stop) {
  2009. break;
  2010. }
  2011. }
  2012. }
  2013. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2014. NSDictionary *internal = _dictionary;
  2015. NSUInteger count = internal.count;
  2016. if (count == 0) {
  2017. return 0;
  2018. }
  2019. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2020. GPBDataType keyDataType = field.mapKeyDataType;
  2021. size_t result = 0;
  2022. NSEnumerator *keys = [internal keyEnumerator];
  2023. NSNumber *aKey;
  2024. while ((aKey = [keys nextObject])) {
  2025. NSNumber *aValue = internal[aKey];
  2026. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2027. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2028. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2029. }
  2030. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2031. result += tagSize * count;
  2032. return result;
  2033. }
  2034. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2035. asField:(GPBFieldDescriptor *)field {
  2036. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2037. GPBDataType keyDataType = field.mapKeyDataType;
  2038. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2039. NSDictionary *internal = _dictionary;
  2040. NSEnumerator *keys = [internal keyEnumerator];
  2041. NSNumber *aKey;
  2042. while ((aKey = [keys nextObject])) {
  2043. NSNumber *aValue = internal[aKey];
  2044. [outputStream writeInt32NoTag:tag];
  2045. // Write the size of the message.
  2046. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2047. BOOL unwrappedValue = [aValue boolValue];
  2048. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2049. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2050. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2051. // Write the fields.
  2052. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2053. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2054. }
  2055. }
  2056. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2057. forGPBGenericValueKey:(GPBGenericValue *)key {
  2058. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2059. }
  2060. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2061. [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, __unused BOOL *stop) {
  2062. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2063. }];
  2064. }
  2065. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
  2066. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2067. if (wrapped && value) {
  2068. *value = [wrapped boolValue];
  2069. }
  2070. return (wrapped != NULL);
  2071. }
  2072. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2073. if (otherDictionary) {
  2074. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2075. if (_autocreator) {
  2076. GPBAutocreatedDictionaryModified(_autocreator, self);
  2077. }
  2078. }
  2079. }
  2080. - (void)setBool:(BOOL)value forKey:(uint32_t)key {
  2081. [_dictionary setObject:@(value) forKey:@(key)];
  2082. if (_autocreator) {
  2083. GPBAutocreatedDictionaryModified(_autocreator, self);
  2084. }
  2085. }
  2086. - (void)removeBoolForKey:(uint32_t)aKey {
  2087. [_dictionary removeObjectForKey:@(aKey)];
  2088. }
  2089. - (void)removeAll {
  2090. [_dictionary removeAllObjects];
  2091. }
  2092. @end
  2093. #pragma mark - UInt32 -> Float
  2094. @implementation GPBUInt32FloatDictionary {
  2095. @package
  2096. NSMutableDictionary *_dictionary;
  2097. }
  2098. - (instancetype)init {
  2099. return [self initWithFloats:NULL forKeys:NULL count:0];
  2100. }
  2101. - (instancetype)initWithFloats:(const float [])values
  2102. forKeys:(const uint32_t [])keys
  2103. count:(NSUInteger)count {
  2104. self = [super init];
  2105. if (self) {
  2106. _dictionary = [[NSMutableDictionary alloc] init];
  2107. if (count && values && keys) {
  2108. for (NSUInteger i = 0; i < count; ++i) {
  2109. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2110. }
  2111. }
  2112. }
  2113. return self;
  2114. }
  2115. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2116. self = [self initWithFloats:NULL forKeys:NULL count:0];
  2117. if (self) {
  2118. if (dictionary) {
  2119. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2120. }
  2121. }
  2122. return self;
  2123. }
  2124. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2125. return [self initWithFloats:NULL forKeys:NULL count:0];
  2126. }
  2127. - (void)dealloc {
  2128. NSAssert(!_autocreator,
  2129. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2130. [self class], _autocreator);
  2131. [_dictionary release];
  2132. [super dealloc];
  2133. }
  2134. - (instancetype)copyWithZone:(NSZone *)zone {
  2135. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2136. }
  2137. - (BOOL)isEqual:(id)other {
  2138. if (self == other) {
  2139. return YES;
  2140. }
  2141. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2142. return NO;
  2143. }
  2144. GPBUInt32FloatDictionary *otherDictionary = other;
  2145. return [_dictionary isEqual:otherDictionary->_dictionary];
  2146. }
  2147. - (NSUInteger)hash {
  2148. return _dictionary.count;
  2149. }
  2150. - (NSString *)description {
  2151. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2152. }
  2153. - (NSUInteger)count {
  2154. return _dictionary.count;
  2155. }
  2156. - (void)enumerateKeysAndFloatsUsingBlock:
  2157. (void (NS_NOESCAPE ^)(uint32_t key, float value, BOOL *stop))block {
  2158. BOOL stop = NO;
  2159. NSDictionary *internal = _dictionary;
  2160. NSEnumerator *keys = [internal keyEnumerator];
  2161. NSNumber *aKey;
  2162. while ((aKey = [keys nextObject])) {
  2163. NSNumber *aValue = internal[aKey];
  2164. block([aKey unsignedIntValue], [aValue floatValue], &stop);
  2165. if (stop) {
  2166. break;
  2167. }
  2168. }
  2169. }
  2170. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2171. NSDictionary *internal = _dictionary;
  2172. NSUInteger count = internal.count;
  2173. if (count == 0) {
  2174. return 0;
  2175. }
  2176. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2177. GPBDataType keyDataType = field.mapKeyDataType;
  2178. size_t result = 0;
  2179. NSEnumerator *keys = [internal keyEnumerator];
  2180. NSNumber *aKey;
  2181. while ((aKey = [keys nextObject])) {
  2182. NSNumber *aValue = internal[aKey];
  2183. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2184. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2185. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2186. }
  2187. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2188. result += tagSize * count;
  2189. return result;
  2190. }
  2191. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2192. asField:(GPBFieldDescriptor *)field {
  2193. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2194. GPBDataType keyDataType = field.mapKeyDataType;
  2195. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2196. NSDictionary *internal = _dictionary;
  2197. NSEnumerator *keys = [internal keyEnumerator];
  2198. NSNumber *aKey;
  2199. while ((aKey = [keys nextObject])) {
  2200. NSNumber *aValue = internal[aKey];
  2201. [outputStream writeInt32NoTag:tag];
  2202. // Write the size of the message.
  2203. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2204. float unwrappedValue = [aValue floatValue];
  2205. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2206. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2207. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2208. // Write the fields.
  2209. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2210. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2211. }
  2212. }
  2213. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2214. forGPBGenericValueKey:(GPBGenericValue *)key {
  2215. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2216. }
  2217. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2218. [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, __unused BOOL *stop) {
  2219. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2220. }];
  2221. }
  2222. - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
  2223. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2224. if (wrapped && value) {
  2225. *value = [wrapped floatValue];
  2226. }
  2227. return (wrapped != NULL);
  2228. }
  2229. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2230. if (otherDictionary) {
  2231. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2232. if (_autocreator) {
  2233. GPBAutocreatedDictionaryModified(_autocreator, self);
  2234. }
  2235. }
  2236. }
  2237. - (void)setFloat:(float)value forKey:(uint32_t)key {
  2238. [_dictionary setObject:@(value) forKey:@(key)];
  2239. if (_autocreator) {
  2240. GPBAutocreatedDictionaryModified(_autocreator, self);
  2241. }
  2242. }
  2243. - (void)removeFloatForKey:(uint32_t)aKey {
  2244. [_dictionary removeObjectForKey:@(aKey)];
  2245. }
  2246. - (void)removeAll {
  2247. [_dictionary removeAllObjects];
  2248. }
  2249. @end
  2250. #pragma mark - UInt32 -> Double
  2251. @implementation GPBUInt32DoubleDictionary {
  2252. @package
  2253. NSMutableDictionary *_dictionary;
  2254. }
  2255. - (instancetype)init {
  2256. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2257. }
  2258. - (instancetype)initWithDoubles:(const double [])values
  2259. forKeys:(const uint32_t [])keys
  2260. count:(NSUInteger)count {
  2261. self = [super init];
  2262. if (self) {
  2263. _dictionary = [[NSMutableDictionary alloc] init];
  2264. if (count && values && keys) {
  2265. for (NSUInteger i = 0; i < count; ++i) {
  2266. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2267. }
  2268. }
  2269. }
  2270. return self;
  2271. }
  2272. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2273. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  2274. if (self) {
  2275. if (dictionary) {
  2276. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2277. }
  2278. }
  2279. return self;
  2280. }
  2281. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2282. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2283. }
  2284. - (void)dealloc {
  2285. NSAssert(!_autocreator,
  2286. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2287. [self class], _autocreator);
  2288. [_dictionary release];
  2289. [super dealloc];
  2290. }
  2291. - (instancetype)copyWithZone:(NSZone *)zone {
  2292. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2293. }
  2294. - (BOOL)isEqual:(id)other {
  2295. if (self == other) {
  2296. return YES;
  2297. }
  2298. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2299. return NO;
  2300. }
  2301. GPBUInt32DoubleDictionary *otherDictionary = other;
  2302. return [_dictionary isEqual:otherDictionary->_dictionary];
  2303. }
  2304. - (NSUInteger)hash {
  2305. return _dictionary.count;
  2306. }
  2307. - (NSString *)description {
  2308. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2309. }
  2310. - (NSUInteger)count {
  2311. return _dictionary.count;
  2312. }
  2313. - (void)enumerateKeysAndDoublesUsingBlock:
  2314. (void (NS_NOESCAPE ^)(uint32_t key, double value, BOOL *stop))block {
  2315. BOOL stop = NO;
  2316. NSDictionary *internal = _dictionary;
  2317. NSEnumerator *keys = [internal keyEnumerator];
  2318. NSNumber *aKey;
  2319. while ((aKey = [keys nextObject])) {
  2320. NSNumber *aValue = internal[aKey];
  2321. block([aKey unsignedIntValue], [aValue doubleValue], &stop);
  2322. if (stop) {
  2323. break;
  2324. }
  2325. }
  2326. }
  2327. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2328. NSDictionary *internal = _dictionary;
  2329. NSUInteger count = internal.count;
  2330. if (count == 0) {
  2331. return 0;
  2332. }
  2333. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2334. GPBDataType keyDataType = field.mapKeyDataType;
  2335. size_t result = 0;
  2336. NSEnumerator *keys = [internal keyEnumerator];
  2337. NSNumber *aKey;
  2338. while ((aKey = [keys nextObject])) {
  2339. NSNumber *aValue = internal[aKey];
  2340. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2341. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2342. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2343. }
  2344. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2345. result += tagSize * count;
  2346. return result;
  2347. }
  2348. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2349. asField:(GPBFieldDescriptor *)field {
  2350. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2351. GPBDataType keyDataType = field.mapKeyDataType;
  2352. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2353. NSDictionary *internal = _dictionary;
  2354. NSEnumerator *keys = [internal keyEnumerator];
  2355. NSNumber *aKey;
  2356. while ((aKey = [keys nextObject])) {
  2357. NSNumber *aValue = internal[aKey];
  2358. [outputStream writeInt32NoTag:tag];
  2359. // Write the size of the message.
  2360. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2361. double unwrappedValue = [aValue doubleValue];
  2362. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2363. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2364. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2365. // Write the fields.
  2366. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2367. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2368. }
  2369. }
  2370. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2371. forGPBGenericValueKey:(GPBGenericValue *)key {
  2372. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2373. }
  2374. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2375. [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, __unused BOOL *stop) {
  2376. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2377. }];
  2378. }
  2379. - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
  2380. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2381. if (wrapped && value) {
  2382. *value = [wrapped doubleValue];
  2383. }
  2384. return (wrapped != NULL);
  2385. }
  2386. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2387. if (otherDictionary) {
  2388. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2389. if (_autocreator) {
  2390. GPBAutocreatedDictionaryModified(_autocreator, self);
  2391. }
  2392. }
  2393. }
  2394. - (void)setDouble:(double)value forKey:(uint32_t)key {
  2395. [_dictionary setObject:@(value) forKey:@(key)];
  2396. if (_autocreator) {
  2397. GPBAutocreatedDictionaryModified(_autocreator, self);
  2398. }
  2399. }
  2400. - (void)removeDoubleForKey:(uint32_t)aKey {
  2401. [_dictionary removeObjectForKey:@(aKey)];
  2402. }
  2403. - (void)removeAll {
  2404. [_dictionary removeAllObjects];
  2405. }
  2406. @end
  2407. #pragma mark - UInt32 -> Enum
  2408. @implementation GPBUInt32EnumDictionary {
  2409. @package
  2410. NSMutableDictionary *_dictionary;
  2411. GPBEnumValidationFunc _validationFunc;
  2412. }
  2413. @synthesize validationFunc = _validationFunc;
  2414. - (instancetype)init {
  2415. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2416. }
  2417. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2418. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2419. }
  2420. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2421. rawValues:(const int32_t [])rawValues
  2422. forKeys:(const uint32_t [])keys
  2423. count:(NSUInteger)count {
  2424. self = [super init];
  2425. if (self) {
  2426. _dictionary = [[NSMutableDictionary alloc] init];
  2427. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2428. if (count && rawValues && keys) {
  2429. for (NSUInteger i = 0; i < count; ++i) {
  2430. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2431. }
  2432. }
  2433. }
  2434. return self;
  2435. }
  2436. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2437. self = [self initWithValidationFunction:dictionary.validationFunc
  2438. rawValues:NULL
  2439. forKeys:NULL
  2440. count:0];
  2441. if (self) {
  2442. if (dictionary) {
  2443. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2444. }
  2445. }
  2446. return self;
  2447. }
  2448. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2449. capacity:(__unused NSUInteger)numItems {
  2450. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2451. }
  2452. - (void)dealloc {
  2453. NSAssert(!_autocreator,
  2454. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2455. [self class], _autocreator);
  2456. [_dictionary release];
  2457. [super dealloc];
  2458. }
  2459. - (instancetype)copyWithZone:(NSZone *)zone {
  2460. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2461. }
  2462. - (BOOL)isEqual:(id)other {
  2463. if (self == other) {
  2464. return YES;
  2465. }
  2466. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2467. return NO;
  2468. }
  2469. GPBUInt32EnumDictionary *otherDictionary = other;
  2470. return [_dictionary isEqual:otherDictionary->_dictionary];
  2471. }
  2472. - (NSUInteger)hash {
  2473. return _dictionary.count;
  2474. }
  2475. - (NSString *)description {
  2476. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2477. }
  2478. - (NSUInteger)count {
  2479. return _dictionary.count;
  2480. }
  2481. - (void)enumerateKeysAndRawValuesUsingBlock:
  2482. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  2483. BOOL stop = NO;
  2484. NSDictionary *internal = _dictionary;
  2485. NSEnumerator *keys = [internal keyEnumerator];
  2486. NSNumber *aKey;
  2487. while ((aKey = [keys nextObject])) {
  2488. NSNumber *aValue = internal[aKey];
  2489. block([aKey unsignedIntValue], [aValue intValue], &stop);
  2490. if (stop) {
  2491. break;
  2492. }
  2493. }
  2494. }
  2495. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2496. NSDictionary *internal = _dictionary;
  2497. NSUInteger count = internal.count;
  2498. if (count == 0) {
  2499. return 0;
  2500. }
  2501. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2502. GPBDataType keyDataType = field.mapKeyDataType;
  2503. size_t result = 0;
  2504. NSEnumerator *keys = [internal keyEnumerator];
  2505. NSNumber *aKey;
  2506. while ((aKey = [keys nextObject])) {
  2507. NSNumber *aValue = internal[aKey];
  2508. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2509. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2510. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2511. }
  2512. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2513. result += tagSize * count;
  2514. return result;
  2515. }
  2516. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2517. asField:(GPBFieldDescriptor *)field {
  2518. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2519. GPBDataType keyDataType = field.mapKeyDataType;
  2520. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2521. NSDictionary *internal = _dictionary;
  2522. NSEnumerator *keys = [internal keyEnumerator];
  2523. NSNumber *aKey;
  2524. while ((aKey = [keys nextObject])) {
  2525. NSNumber *aValue = internal[aKey];
  2526. [outputStream writeInt32NoTag:tag];
  2527. // Write the size of the message.
  2528. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2529. int32_t unwrappedValue = [aValue intValue];
  2530. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2531. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2532. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2533. // Write the fields.
  2534. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2535. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2536. }
  2537. }
  2538. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2539. forKey:(GPBGenericValue *)key
  2540. keyDataType:(GPBDataType)keyDataType {
  2541. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2542. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2543. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2544. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2545. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2546. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2547. [outputStream flush];
  2548. [outputStream release];
  2549. return data;
  2550. }
  2551. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2552. forGPBGenericValueKey:(GPBGenericValue *)key {
  2553. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2554. }
  2555. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2556. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, __unused BOOL *stop) {
  2557. block([NSString stringWithFormat:@"%u", key], @(value));
  2558. }];
  2559. }
  2560. - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
  2561. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2562. if (wrapped && value) {
  2563. int32_t result = [wrapped intValue];
  2564. if (!_validationFunc(result)) {
  2565. result = kGPBUnrecognizedEnumeratorValue;
  2566. }
  2567. *value = result;
  2568. }
  2569. return (wrapped != NULL);
  2570. }
  2571. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
  2572. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2573. if (wrapped && rawValue) {
  2574. *rawValue = [wrapped intValue];
  2575. }
  2576. return (wrapped != NULL);
  2577. }
  2578. - (void)enumerateKeysAndEnumsUsingBlock:
  2579. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  2580. GPBEnumValidationFunc func = _validationFunc;
  2581. BOOL stop = NO;
  2582. NSEnumerator *keys = [_dictionary keyEnumerator];
  2583. NSNumber *aKey;
  2584. while ((aKey = [keys nextObject])) {
  2585. NSNumber *aValue = _dictionary[aKey];
  2586. int32_t unwrapped = [aValue intValue];
  2587. if (!func(unwrapped)) {
  2588. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2589. }
  2590. block([aKey unsignedIntValue], unwrapped, &stop);
  2591. if (stop) {
  2592. break;
  2593. }
  2594. }
  2595. }
  2596. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2597. if (otherDictionary) {
  2598. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2599. if (_autocreator) {
  2600. GPBAutocreatedDictionaryModified(_autocreator, self);
  2601. }
  2602. }
  2603. }
  2604. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2605. [_dictionary setObject:@(value) forKey:@(key)];
  2606. if (_autocreator) {
  2607. GPBAutocreatedDictionaryModified(_autocreator, self);
  2608. }
  2609. }
  2610. - (void)removeEnumForKey:(uint32_t)aKey {
  2611. [_dictionary removeObjectForKey:@(aKey)];
  2612. }
  2613. - (void)removeAll {
  2614. [_dictionary removeAllObjects];
  2615. }
  2616. - (void)setEnum:(int32_t)value forKey:(uint32_t)key {
  2617. if (!_validationFunc(value)) {
  2618. [NSException raise:NSInvalidArgumentException
  2619. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2620. value];
  2621. }
  2622. [_dictionary setObject:@(value) forKey:@(key)];
  2623. if (_autocreator) {
  2624. GPBAutocreatedDictionaryModified(_autocreator, self);
  2625. }
  2626. }
  2627. @end
  2628. #pragma mark - UInt32 -> Object
  2629. @implementation GPBUInt32ObjectDictionary {
  2630. @package
  2631. NSMutableDictionary *_dictionary;
  2632. }
  2633. - (instancetype)init {
  2634. return [self initWithObjects:NULL forKeys:NULL count:0];
  2635. }
  2636. - (instancetype)initWithObjects:(const id [])objects
  2637. forKeys:(const uint32_t [])keys
  2638. count:(NSUInteger)count {
  2639. self = [super init];
  2640. if (self) {
  2641. _dictionary = [[NSMutableDictionary alloc] init];
  2642. if (count && objects && keys) {
  2643. for (NSUInteger i = 0; i < count; ++i) {
  2644. if (!objects[i]) {
  2645. [NSException raise:NSInvalidArgumentException
  2646. format:@"Attempting to add nil object to a Dictionary"];
  2647. }
  2648. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  2649. }
  2650. }
  2651. }
  2652. return self;
  2653. }
  2654. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2655. self = [self initWithObjects:NULL forKeys:NULL count:0];
  2656. if (self) {
  2657. if (dictionary) {
  2658. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2659. }
  2660. }
  2661. return self;
  2662. }
  2663. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2664. return [self initWithObjects:NULL forKeys:NULL count:0];
  2665. }
  2666. - (void)dealloc {
  2667. NSAssert(!_autocreator,
  2668. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2669. [self class], _autocreator);
  2670. [_dictionary release];
  2671. [super dealloc];
  2672. }
  2673. - (instancetype)copyWithZone:(NSZone *)zone {
  2674. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2675. }
  2676. - (BOOL)isEqual:(id)other {
  2677. if (self == other) {
  2678. return YES;
  2679. }
  2680. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2681. return NO;
  2682. }
  2683. GPBUInt32ObjectDictionary *otherDictionary = other;
  2684. return [_dictionary isEqual:otherDictionary->_dictionary];
  2685. }
  2686. - (NSUInteger)hash {
  2687. return _dictionary.count;
  2688. }
  2689. - (NSString *)description {
  2690. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2691. }
  2692. - (NSUInteger)count {
  2693. return _dictionary.count;
  2694. }
  2695. - (void)enumerateKeysAndObjectsUsingBlock:
  2696. (void (NS_NOESCAPE ^)(uint32_t key, id object, BOOL *stop))block {
  2697. BOOL stop = NO;
  2698. NSDictionary *internal = _dictionary;
  2699. NSEnumerator *keys = [internal keyEnumerator];
  2700. NSNumber *aKey;
  2701. while ((aKey = [keys nextObject])) {
  2702. id aObject = internal[aKey];
  2703. block([aKey unsignedIntValue], aObject, &stop);
  2704. if (stop) {
  2705. break;
  2706. }
  2707. }
  2708. }
  2709. - (BOOL)isInitialized {
  2710. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2711. if (!msg.initialized) {
  2712. return NO;
  2713. }
  2714. }
  2715. return YES;
  2716. }
  2717. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2718. GPBUInt32ObjectDictionary *newDict =
  2719. [[GPBUInt32ObjectDictionary alloc] init];
  2720. NSEnumerator *keys = [_dictionary keyEnumerator];
  2721. id aKey;
  2722. NSMutableDictionary *internalDict = newDict->_dictionary;
  2723. while ((aKey = [keys nextObject])) {
  2724. GPBMessage *msg = _dictionary[aKey];
  2725. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  2726. [internalDict setObject:copiedMsg forKey:aKey];
  2727. [copiedMsg release];
  2728. }
  2729. return newDict;
  2730. }
  2731. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2732. NSDictionary *internal = _dictionary;
  2733. NSUInteger count = internal.count;
  2734. if (count == 0) {
  2735. return 0;
  2736. }
  2737. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2738. GPBDataType keyDataType = field.mapKeyDataType;
  2739. size_t result = 0;
  2740. NSEnumerator *keys = [internal keyEnumerator];
  2741. NSNumber *aKey;
  2742. while ((aKey = [keys nextObject])) {
  2743. id aObject = internal[aKey];
  2744. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2745. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  2746. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2747. }
  2748. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2749. result += tagSize * count;
  2750. return result;
  2751. }
  2752. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2753. asField:(GPBFieldDescriptor *)field {
  2754. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2755. GPBDataType keyDataType = field.mapKeyDataType;
  2756. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2757. NSDictionary *internal = _dictionary;
  2758. NSEnumerator *keys = [internal keyEnumerator];
  2759. NSNumber *aKey;
  2760. while ((aKey = [keys nextObject])) {
  2761. id aObject = internal[aKey];
  2762. [outputStream writeInt32NoTag:tag];
  2763. // Write the size of the message.
  2764. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2765. id unwrappedValue = aObject;
  2766. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2767. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2768. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2769. // Write the fields.
  2770. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2771. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2772. }
  2773. }
  2774. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2775. forGPBGenericValueKey:(GPBGenericValue *)key {
  2776. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  2777. }
  2778. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2779. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, __unused BOOL *stop) {
  2780. block([NSString stringWithFormat:@"%u", key], object);
  2781. }];
  2782. }
  2783. - (id)objectForKey:(uint32_t)key {
  2784. id result = [_dictionary objectForKey:@(key)];
  2785. return result;
  2786. }
  2787. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  2788. if (otherDictionary) {
  2789. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2790. if (_autocreator) {
  2791. GPBAutocreatedDictionaryModified(_autocreator, self);
  2792. }
  2793. }
  2794. }
  2795. - (void)setObject:(id)object forKey:(uint32_t)key {
  2796. if (!object) {
  2797. [NSException raise:NSInvalidArgumentException
  2798. format:@"Attempting to add nil object to a Dictionary"];
  2799. }
  2800. [_dictionary setObject:object forKey:@(key)];
  2801. if (_autocreator) {
  2802. GPBAutocreatedDictionaryModified(_autocreator, self);
  2803. }
  2804. }
  2805. - (void)removeObjectForKey:(uint32_t)aKey {
  2806. [_dictionary removeObjectForKey:@(aKey)];
  2807. }
  2808. - (void)removeAll {
  2809. [_dictionary removeAllObjects];
  2810. }
  2811. @end
  2812. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  2813. // This block of code is generated, do not edit it directly.
  2814. #pragma mark - Int32 -> UInt32
  2815. @implementation GPBInt32UInt32Dictionary {
  2816. @package
  2817. NSMutableDictionary *_dictionary;
  2818. }
  2819. - (instancetype)init {
  2820. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  2821. }
  2822. - (instancetype)initWithUInt32s:(const uint32_t [])values
  2823. forKeys:(const int32_t [])keys
  2824. count:(NSUInteger)count {
  2825. self = [super init];
  2826. if (self) {
  2827. _dictionary = [[NSMutableDictionary alloc] init];
  2828. if (count && values && keys) {
  2829. for (NSUInteger i = 0; i < count; ++i) {
  2830. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2831. }
  2832. }
  2833. }
  2834. return self;
  2835. }
  2836. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  2837. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  2838. if (self) {
  2839. if (dictionary) {
  2840. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2841. }
  2842. }
  2843. return self;
  2844. }
  2845. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  2846. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  2847. }
  2848. - (void)dealloc {
  2849. NSAssert(!_autocreator,
  2850. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2851. [self class], _autocreator);
  2852. [_dictionary release];
  2853. [super dealloc];
  2854. }
  2855. - (instancetype)copyWithZone:(NSZone *)zone {
  2856. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  2857. }
  2858. - (BOOL)isEqual:(id)other {
  2859. if (self == other) {
  2860. return YES;
  2861. }
  2862. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  2863. return NO;
  2864. }
  2865. GPBInt32UInt32Dictionary *otherDictionary = other;
  2866. return [_dictionary isEqual:otherDictionary->_dictionary];
  2867. }
  2868. - (NSUInteger)hash {
  2869. return _dictionary.count;
  2870. }
  2871. - (NSString *)description {
  2872. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2873. }
  2874. - (NSUInteger)count {
  2875. return _dictionary.count;
  2876. }
  2877. - (void)enumerateKeysAndUInt32sUsingBlock:
  2878. (void (NS_NOESCAPE ^)(int32_t key, uint32_t value, BOOL *stop))block {
  2879. BOOL stop = NO;
  2880. NSDictionary *internal = _dictionary;
  2881. NSEnumerator *keys = [internal keyEnumerator];
  2882. NSNumber *aKey;
  2883. while ((aKey = [keys nextObject])) {
  2884. NSNumber *aValue = internal[aKey];
  2885. block([aKey intValue], [aValue unsignedIntValue], &stop);
  2886. if (stop) {
  2887. break;
  2888. }
  2889. }
  2890. }
  2891. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2892. NSDictionary *internal = _dictionary;
  2893. NSUInteger count = internal.count;
  2894. if (count == 0) {
  2895. return 0;
  2896. }
  2897. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2898. GPBDataType keyDataType = field.mapKeyDataType;
  2899. size_t result = 0;
  2900. NSEnumerator *keys = [internal keyEnumerator];
  2901. NSNumber *aKey;
  2902. while ((aKey = [keys nextObject])) {
  2903. NSNumber *aValue = internal[aKey];
  2904. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  2905. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  2906. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2907. }
  2908. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2909. result += tagSize * count;
  2910. return result;
  2911. }
  2912. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2913. asField:(GPBFieldDescriptor *)field {
  2914. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2915. GPBDataType keyDataType = field.mapKeyDataType;
  2916. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2917. NSDictionary *internal = _dictionary;
  2918. NSEnumerator *keys = [internal keyEnumerator];
  2919. NSNumber *aKey;
  2920. while ((aKey = [keys nextObject])) {
  2921. NSNumber *aValue = internal[aKey];
  2922. [outputStream writeInt32NoTag:tag];
  2923. // Write the size of the message.
  2924. int32_t unwrappedKey = [aKey intValue];
  2925. uint32_t unwrappedValue = [aValue unsignedIntValue];
  2926. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2927. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2928. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2929. // Write the fields.
  2930. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2931. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2932. }
  2933. }
  2934. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2935. forGPBGenericValueKey:(GPBGenericValue *)key {
  2936. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  2937. }
  2938. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2939. [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, __unused BOOL *stop) {
  2940. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  2941. }];
  2942. }
  2943. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
  2944. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2945. if (wrapped && value) {
  2946. *value = [wrapped unsignedIntValue];
  2947. }
  2948. return (wrapped != NULL);
  2949. }
  2950. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  2951. if (otherDictionary) {
  2952. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2953. if (_autocreator) {
  2954. GPBAutocreatedDictionaryModified(_autocreator, self);
  2955. }
  2956. }
  2957. }
  2958. - (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
  2959. [_dictionary setObject:@(value) forKey:@(key)];
  2960. if (_autocreator) {
  2961. GPBAutocreatedDictionaryModified(_autocreator, self);
  2962. }
  2963. }
  2964. - (void)removeUInt32ForKey:(int32_t)aKey {
  2965. [_dictionary removeObjectForKey:@(aKey)];
  2966. }
  2967. - (void)removeAll {
  2968. [_dictionary removeAllObjects];
  2969. }
  2970. @end
  2971. #pragma mark - Int32 -> Int32
  2972. @implementation GPBInt32Int32Dictionary {
  2973. @package
  2974. NSMutableDictionary *_dictionary;
  2975. }
  2976. - (instancetype)init {
  2977. return [self initWithInt32s:NULL forKeys:NULL count:0];
  2978. }
  2979. - (instancetype)initWithInt32s:(const int32_t [])values
  2980. forKeys:(const int32_t [])keys
  2981. count:(NSUInteger)count {
  2982. self = [super init];
  2983. if (self) {
  2984. _dictionary = [[NSMutableDictionary alloc] init];
  2985. if (count && values && keys) {
  2986. for (NSUInteger i = 0; i < count; ++i) {
  2987. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2988. }
  2989. }
  2990. }
  2991. return self;
  2992. }
  2993. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  2994. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  2995. if (self) {
  2996. if (dictionary) {
  2997. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2998. }
  2999. }
  3000. return self;
  3001. }
  3002. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3003. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3004. }
  3005. - (void)dealloc {
  3006. NSAssert(!_autocreator,
  3007. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3008. [self class], _autocreator);
  3009. [_dictionary release];
  3010. [super dealloc];
  3011. }
  3012. - (instancetype)copyWithZone:(NSZone *)zone {
  3013. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3014. }
  3015. - (BOOL)isEqual:(id)other {
  3016. if (self == other) {
  3017. return YES;
  3018. }
  3019. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3020. return NO;
  3021. }
  3022. GPBInt32Int32Dictionary *otherDictionary = other;
  3023. return [_dictionary isEqual:otherDictionary->_dictionary];
  3024. }
  3025. - (NSUInteger)hash {
  3026. return _dictionary.count;
  3027. }
  3028. - (NSString *)description {
  3029. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3030. }
  3031. - (NSUInteger)count {
  3032. return _dictionary.count;
  3033. }
  3034. - (void)enumerateKeysAndInt32sUsingBlock:
  3035. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  3036. BOOL stop = NO;
  3037. NSDictionary *internal = _dictionary;
  3038. NSEnumerator *keys = [internal keyEnumerator];
  3039. NSNumber *aKey;
  3040. while ((aKey = [keys nextObject])) {
  3041. NSNumber *aValue = internal[aKey];
  3042. block([aKey intValue], [aValue intValue], &stop);
  3043. if (stop) {
  3044. break;
  3045. }
  3046. }
  3047. }
  3048. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3049. NSDictionary *internal = _dictionary;
  3050. NSUInteger count = internal.count;
  3051. if (count == 0) {
  3052. return 0;
  3053. }
  3054. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3055. GPBDataType keyDataType = field.mapKeyDataType;
  3056. size_t result = 0;
  3057. NSEnumerator *keys = [internal keyEnumerator];
  3058. NSNumber *aKey;
  3059. while ((aKey = [keys nextObject])) {
  3060. NSNumber *aValue = internal[aKey];
  3061. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3062. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3063. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3064. }
  3065. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3066. result += tagSize * count;
  3067. return result;
  3068. }
  3069. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3070. asField:(GPBFieldDescriptor *)field {
  3071. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3072. GPBDataType keyDataType = field.mapKeyDataType;
  3073. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3074. NSDictionary *internal = _dictionary;
  3075. NSEnumerator *keys = [internal keyEnumerator];
  3076. NSNumber *aKey;
  3077. while ((aKey = [keys nextObject])) {
  3078. NSNumber *aValue = internal[aKey];
  3079. [outputStream writeInt32NoTag:tag];
  3080. // Write the size of the message.
  3081. int32_t unwrappedKey = [aKey intValue];
  3082. int32_t unwrappedValue = [aValue intValue];
  3083. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3084. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3085. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3086. // Write the fields.
  3087. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3088. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3089. }
  3090. }
  3091. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3092. forGPBGenericValueKey:(GPBGenericValue *)key {
  3093. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3094. }
  3095. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3096. [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, __unused BOOL *stop) {
  3097. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3098. }];
  3099. }
  3100. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
  3101. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3102. if (wrapped && value) {
  3103. *value = [wrapped intValue];
  3104. }
  3105. return (wrapped != NULL);
  3106. }
  3107. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3108. if (otherDictionary) {
  3109. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3110. if (_autocreator) {
  3111. GPBAutocreatedDictionaryModified(_autocreator, self);
  3112. }
  3113. }
  3114. }
  3115. - (void)setInt32:(int32_t)value forKey:(int32_t)key {
  3116. [_dictionary setObject:@(value) forKey:@(key)];
  3117. if (_autocreator) {
  3118. GPBAutocreatedDictionaryModified(_autocreator, self);
  3119. }
  3120. }
  3121. - (void)removeInt32ForKey:(int32_t)aKey {
  3122. [_dictionary removeObjectForKey:@(aKey)];
  3123. }
  3124. - (void)removeAll {
  3125. [_dictionary removeAllObjects];
  3126. }
  3127. @end
  3128. #pragma mark - Int32 -> UInt64
  3129. @implementation GPBInt32UInt64Dictionary {
  3130. @package
  3131. NSMutableDictionary *_dictionary;
  3132. }
  3133. - (instancetype)init {
  3134. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3135. }
  3136. - (instancetype)initWithUInt64s:(const uint64_t [])values
  3137. forKeys:(const int32_t [])keys
  3138. count:(NSUInteger)count {
  3139. self = [super init];
  3140. if (self) {
  3141. _dictionary = [[NSMutableDictionary alloc] init];
  3142. if (count && values && keys) {
  3143. for (NSUInteger i = 0; i < count; ++i) {
  3144. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3145. }
  3146. }
  3147. }
  3148. return self;
  3149. }
  3150. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3151. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  3152. if (self) {
  3153. if (dictionary) {
  3154. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3155. }
  3156. }
  3157. return self;
  3158. }
  3159. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3160. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3161. }
  3162. - (void)dealloc {
  3163. NSAssert(!_autocreator,
  3164. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3165. [self class], _autocreator);
  3166. [_dictionary release];
  3167. [super dealloc];
  3168. }
  3169. - (instancetype)copyWithZone:(NSZone *)zone {
  3170. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3171. }
  3172. - (BOOL)isEqual:(id)other {
  3173. if (self == other) {
  3174. return YES;
  3175. }
  3176. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3177. return NO;
  3178. }
  3179. GPBInt32UInt64Dictionary *otherDictionary = other;
  3180. return [_dictionary isEqual:otherDictionary->_dictionary];
  3181. }
  3182. - (NSUInteger)hash {
  3183. return _dictionary.count;
  3184. }
  3185. - (NSString *)description {
  3186. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3187. }
  3188. - (NSUInteger)count {
  3189. return _dictionary.count;
  3190. }
  3191. - (void)enumerateKeysAndUInt64sUsingBlock:
  3192. (void (NS_NOESCAPE ^)(int32_t key, uint64_t value, BOOL *stop))block {
  3193. BOOL stop = NO;
  3194. NSDictionary *internal = _dictionary;
  3195. NSEnumerator *keys = [internal keyEnumerator];
  3196. NSNumber *aKey;
  3197. while ((aKey = [keys nextObject])) {
  3198. NSNumber *aValue = internal[aKey];
  3199. block([aKey intValue], [aValue unsignedLongLongValue], &stop);
  3200. if (stop) {
  3201. break;
  3202. }
  3203. }
  3204. }
  3205. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3206. NSDictionary *internal = _dictionary;
  3207. NSUInteger count = internal.count;
  3208. if (count == 0) {
  3209. return 0;
  3210. }
  3211. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3212. GPBDataType keyDataType = field.mapKeyDataType;
  3213. size_t result = 0;
  3214. NSEnumerator *keys = [internal keyEnumerator];
  3215. NSNumber *aKey;
  3216. while ((aKey = [keys nextObject])) {
  3217. NSNumber *aValue = internal[aKey];
  3218. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3219. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3220. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3221. }
  3222. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3223. result += tagSize * count;
  3224. return result;
  3225. }
  3226. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3227. asField:(GPBFieldDescriptor *)field {
  3228. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3229. GPBDataType keyDataType = field.mapKeyDataType;
  3230. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3231. NSDictionary *internal = _dictionary;
  3232. NSEnumerator *keys = [internal keyEnumerator];
  3233. NSNumber *aKey;
  3234. while ((aKey = [keys nextObject])) {
  3235. NSNumber *aValue = internal[aKey];
  3236. [outputStream writeInt32NoTag:tag];
  3237. // Write the size of the message.
  3238. int32_t unwrappedKey = [aKey intValue];
  3239. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  3240. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3241. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3242. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3243. // Write the fields.
  3244. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3245. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3246. }
  3247. }
  3248. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3249. forGPBGenericValueKey:(GPBGenericValue *)key {
  3250. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3251. }
  3252. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3253. [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, __unused BOOL *stop) {
  3254. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3255. }];
  3256. }
  3257. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
  3258. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3259. if (wrapped && value) {
  3260. *value = [wrapped unsignedLongLongValue];
  3261. }
  3262. return (wrapped != NULL);
  3263. }
  3264. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3265. if (otherDictionary) {
  3266. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3267. if (_autocreator) {
  3268. GPBAutocreatedDictionaryModified(_autocreator, self);
  3269. }
  3270. }
  3271. }
  3272. - (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
  3273. [_dictionary setObject:@(value) forKey:@(key)];
  3274. if (_autocreator) {
  3275. GPBAutocreatedDictionaryModified(_autocreator, self);
  3276. }
  3277. }
  3278. - (void)removeUInt64ForKey:(int32_t)aKey {
  3279. [_dictionary removeObjectForKey:@(aKey)];
  3280. }
  3281. - (void)removeAll {
  3282. [_dictionary removeAllObjects];
  3283. }
  3284. @end
  3285. #pragma mark - Int32 -> Int64
  3286. @implementation GPBInt32Int64Dictionary {
  3287. @package
  3288. NSMutableDictionary *_dictionary;
  3289. }
  3290. - (instancetype)init {
  3291. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3292. }
  3293. - (instancetype)initWithInt64s:(const int64_t [])values
  3294. forKeys:(const int32_t [])keys
  3295. count:(NSUInteger)count {
  3296. self = [super init];
  3297. if (self) {
  3298. _dictionary = [[NSMutableDictionary alloc] init];
  3299. if (count && values && keys) {
  3300. for (NSUInteger i = 0; i < count; ++i) {
  3301. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3302. }
  3303. }
  3304. }
  3305. return self;
  3306. }
  3307. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3308. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  3309. if (self) {
  3310. if (dictionary) {
  3311. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3312. }
  3313. }
  3314. return self;
  3315. }
  3316. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3317. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3318. }
  3319. - (void)dealloc {
  3320. NSAssert(!_autocreator,
  3321. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3322. [self class], _autocreator);
  3323. [_dictionary release];
  3324. [super dealloc];
  3325. }
  3326. - (instancetype)copyWithZone:(NSZone *)zone {
  3327. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3328. }
  3329. - (BOOL)isEqual:(id)other {
  3330. if (self == other) {
  3331. return YES;
  3332. }
  3333. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3334. return NO;
  3335. }
  3336. GPBInt32Int64Dictionary *otherDictionary = other;
  3337. return [_dictionary isEqual:otherDictionary->_dictionary];
  3338. }
  3339. - (NSUInteger)hash {
  3340. return _dictionary.count;
  3341. }
  3342. - (NSString *)description {
  3343. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3344. }
  3345. - (NSUInteger)count {
  3346. return _dictionary.count;
  3347. }
  3348. - (void)enumerateKeysAndInt64sUsingBlock:
  3349. (void (NS_NOESCAPE ^)(int32_t key, int64_t value, BOOL *stop))block {
  3350. BOOL stop = NO;
  3351. NSDictionary *internal = _dictionary;
  3352. NSEnumerator *keys = [internal keyEnumerator];
  3353. NSNumber *aKey;
  3354. while ((aKey = [keys nextObject])) {
  3355. NSNumber *aValue = internal[aKey];
  3356. block([aKey intValue], [aValue longLongValue], &stop);
  3357. if (stop) {
  3358. break;
  3359. }
  3360. }
  3361. }
  3362. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3363. NSDictionary *internal = _dictionary;
  3364. NSUInteger count = internal.count;
  3365. if (count == 0) {
  3366. return 0;
  3367. }
  3368. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3369. GPBDataType keyDataType = field.mapKeyDataType;
  3370. size_t result = 0;
  3371. NSEnumerator *keys = [internal keyEnumerator];
  3372. NSNumber *aKey;
  3373. while ((aKey = [keys nextObject])) {
  3374. NSNumber *aValue = internal[aKey];
  3375. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3376. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3377. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3378. }
  3379. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3380. result += tagSize * count;
  3381. return result;
  3382. }
  3383. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3384. asField:(GPBFieldDescriptor *)field {
  3385. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3386. GPBDataType keyDataType = field.mapKeyDataType;
  3387. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3388. NSDictionary *internal = _dictionary;
  3389. NSEnumerator *keys = [internal keyEnumerator];
  3390. NSNumber *aKey;
  3391. while ((aKey = [keys nextObject])) {
  3392. NSNumber *aValue = internal[aKey];
  3393. [outputStream writeInt32NoTag:tag];
  3394. // Write the size of the message.
  3395. int32_t unwrappedKey = [aKey intValue];
  3396. int64_t unwrappedValue = [aValue longLongValue];
  3397. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3398. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3399. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3400. // Write the fields.
  3401. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3402. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3403. }
  3404. }
  3405. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3406. forGPBGenericValueKey:(GPBGenericValue *)key {
  3407. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3408. }
  3409. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3410. [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, __unused BOOL *stop) {
  3411. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3412. }];
  3413. }
  3414. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
  3415. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3416. if (wrapped && value) {
  3417. *value = [wrapped longLongValue];
  3418. }
  3419. return (wrapped != NULL);
  3420. }
  3421. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3422. if (otherDictionary) {
  3423. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3424. if (_autocreator) {
  3425. GPBAutocreatedDictionaryModified(_autocreator, self);
  3426. }
  3427. }
  3428. }
  3429. - (void)setInt64:(int64_t)value forKey:(int32_t)key {
  3430. [_dictionary setObject:@(value) forKey:@(key)];
  3431. if (_autocreator) {
  3432. GPBAutocreatedDictionaryModified(_autocreator, self);
  3433. }
  3434. }
  3435. - (void)removeInt64ForKey:(int32_t)aKey {
  3436. [_dictionary removeObjectForKey:@(aKey)];
  3437. }
  3438. - (void)removeAll {
  3439. [_dictionary removeAllObjects];
  3440. }
  3441. @end
  3442. #pragma mark - Int32 -> Bool
  3443. @implementation GPBInt32BoolDictionary {
  3444. @package
  3445. NSMutableDictionary *_dictionary;
  3446. }
  3447. - (instancetype)init {
  3448. return [self initWithBools:NULL forKeys:NULL count:0];
  3449. }
  3450. - (instancetype)initWithBools:(const BOOL [])values
  3451. forKeys:(const int32_t [])keys
  3452. count:(NSUInteger)count {
  3453. self = [super init];
  3454. if (self) {
  3455. _dictionary = [[NSMutableDictionary alloc] init];
  3456. if (count && values && keys) {
  3457. for (NSUInteger i = 0; i < count; ++i) {
  3458. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3459. }
  3460. }
  3461. }
  3462. return self;
  3463. }
  3464. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3465. self = [self initWithBools:NULL forKeys:NULL count:0];
  3466. if (self) {
  3467. if (dictionary) {
  3468. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3469. }
  3470. }
  3471. return self;
  3472. }
  3473. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3474. return [self initWithBools:NULL forKeys:NULL count:0];
  3475. }
  3476. - (void)dealloc {
  3477. NSAssert(!_autocreator,
  3478. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3479. [self class], _autocreator);
  3480. [_dictionary release];
  3481. [super dealloc];
  3482. }
  3483. - (instancetype)copyWithZone:(NSZone *)zone {
  3484. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3485. }
  3486. - (BOOL)isEqual:(id)other {
  3487. if (self == other) {
  3488. return YES;
  3489. }
  3490. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3491. return NO;
  3492. }
  3493. GPBInt32BoolDictionary *otherDictionary = other;
  3494. return [_dictionary isEqual:otherDictionary->_dictionary];
  3495. }
  3496. - (NSUInteger)hash {
  3497. return _dictionary.count;
  3498. }
  3499. - (NSString *)description {
  3500. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3501. }
  3502. - (NSUInteger)count {
  3503. return _dictionary.count;
  3504. }
  3505. - (void)enumerateKeysAndBoolsUsingBlock:
  3506. (void (NS_NOESCAPE ^)(int32_t key, BOOL value, BOOL *stop))block {
  3507. BOOL stop = NO;
  3508. NSDictionary *internal = _dictionary;
  3509. NSEnumerator *keys = [internal keyEnumerator];
  3510. NSNumber *aKey;
  3511. while ((aKey = [keys nextObject])) {
  3512. NSNumber *aValue = internal[aKey];
  3513. block([aKey intValue], [aValue boolValue], &stop);
  3514. if (stop) {
  3515. break;
  3516. }
  3517. }
  3518. }
  3519. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3520. NSDictionary *internal = _dictionary;
  3521. NSUInteger count = internal.count;
  3522. if (count == 0) {
  3523. return 0;
  3524. }
  3525. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3526. GPBDataType keyDataType = field.mapKeyDataType;
  3527. size_t result = 0;
  3528. NSEnumerator *keys = [internal keyEnumerator];
  3529. NSNumber *aKey;
  3530. while ((aKey = [keys nextObject])) {
  3531. NSNumber *aValue = internal[aKey];
  3532. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3533. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3534. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3535. }
  3536. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3537. result += tagSize * count;
  3538. return result;
  3539. }
  3540. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3541. asField:(GPBFieldDescriptor *)field {
  3542. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3543. GPBDataType keyDataType = field.mapKeyDataType;
  3544. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3545. NSDictionary *internal = _dictionary;
  3546. NSEnumerator *keys = [internal keyEnumerator];
  3547. NSNumber *aKey;
  3548. while ((aKey = [keys nextObject])) {
  3549. NSNumber *aValue = internal[aKey];
  3550. [outputStream writeInt32NoTag:tag];
  3551. // Write the size of the message.
  3552. int32_t unwrappedKey = [aKey intValue];
  3553. BOOL unwrappedValue = [aValue boolValue];
  3554. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3555. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3556. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3557. // Write the fields.
  3558. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3559. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3560. }
  3561. }
  3562. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3563. forGPBGenericValueKey:(GPBGenericValue *)key {
  3564. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3565. }
  3566. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3567. [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, __unused BOOL *stop) {
  3568. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3569. }];
  3570. }
  3571. - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
  3572. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3573. if (wrapped && value) {
  3574. *value = [wrapped boolValue];
  3575. }
  3576. return (wrapped != NULL);
  3577. }
  3578. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3579. if (otherDictionary) {
  3580. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3581. if (_autocreator) {
  3582. GPBAutocreatedDictionaryModified(_autocreator, self);
  3583. }
  3584. }
  3585. }
  3586. - (void)setBool:(BOOL)value forKey:(int32_t)key {
  3587. [_dictionary setObject:@(value) forKey:@(key)];
  3588. if (_autocreator) {
  3589. GPBAutocreatedDictionaryModified(_autocreator, self);
  3590. }
  3591. }
  3592. - (void)removeBoolForKey:(int32_t)aKey {
  3593. [_dictionary removeObjectForKey:@(aKey)];
  3594. }
  3595. - (void)removeAll {
  3596. [_dictionary removeAllObjects];
  3597. }
  3598. @end
  3599. #pragma mark - Int32 -> Float
  3600. @implementation GPBInt32FloatDictionary {
  3601. @package
  3602. NSMutableDictionary *_dictionary;
  3603. }
  3604. - (instancetype)init {
  3605. return [self initWithFloats:NULL forKeys:NULL count:0];
  3606. }
  3607. - (instancetype)initWithFloats:(const float [])values
  3608. forKeys:(const int32_t [])keys
  3609. count:(NSUInteger)count {
  3610. self = [super init];
  3611. if (self) {
  3612. _dictionary = [[NSMutableDictionary alloc] init];
  3613. if (count && values && keys) {
  3614. for (NSUInteger i = 0; i < count; ++i) {
  3615. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3616. }
  3617. }
  3618. }
  3619. return self;
  3620. }
  3621. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3622. self = [self initWithFloats:NULL forKeys:NULL count:0];
  3623. if (self) {
  3624. if (dictionary) {
  3625. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3626. }
  3627. }
  3628. return self;
  3629. }
  3630. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3631. return [self initWithFloats:NULL forKeys:NULL count:0];
  3632. }
  3633. - (void)dealloc {
  3634. NSAssert(!_autocreator,
  3635. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3636. [self class], _autocreator);
  3637. [_dictionary release];
  3638. [super dealloc];
  3639. }
  3640. - (instancetype)copyWithZone:(NSZone *)zone {
  3641. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  3642. }
  3643. - (BOOL)isEqual:(id)other {
  3644. if (self == other) {
  3645. return YES;
  3646. }
  3647. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  3648. return NO;
  3649. }
  3650. GPBInt32FloatDictionary *otherDictionary = other;
  3651. return [_dictionary isEqual:otherDictionary->_dictionary];
  3652. }
  3653. - (NSUInteger)hash {
  3654. return _dictionary.count;
  3655. }
  3656. - (NSString *)description {
  3657. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3658. }
  3659. - (NSUInteger)count {
  3660. return _dictionary.count;
  3661. }
  3662. - (void)enumerateKeysAndFloatsUsingBlock:
  3663. (void (NS_NOESCAPE ^)(int32_t key, float value, BOOL *stop))block {
  3664. BOOL stop = NO;
  3665. NSDictionary *internal = _dictionary;
  3666. NSEnumerator *keys = [internal keyEnumerator];
  3667. NSNumber *aKey;
  3668. while ((aKey = [keys nextObject])) {
  3669. NSNumber *aValue = internal[aKey];
  3670. block([aKey intValue], [aValue floatValue], &stop);
  3671. if (stop) {
  3672. break;
  3673. }
  3674. }
  3675. }
  3676. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3677. NSDictionary *internal = _dictionary;
  3678. NSUInteger count = internal.count;
  3679. if (count == 0) {
  3680. return 0;
  3681. }
  3682. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3683. GPBDataType keyDataType = field.mapKeyDataType;
  3684. size_t result = 0;
  3685. NSEnumerator *keys = [internal keyEnumerator];
  3686. NSNumber *aKey;
  3687. while ((aKey = [keys nextObject])) {
  3688. NSNumber *aValue = internal[aKey];
  3689. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3690. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  3691. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3692. }
  3693. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3694. result += tagSize * count;
  3695. return result;
  3696. }
  3697. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3698. asField:(GPBFieldDescriptor *)field {
  3699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3700. GPBDataType keyDataType = field.mapKeyDataType;
  3701. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3702. NSDictionary *internal = _dictionary;
  3703. NSEnumerator *keys = [internal keyEnumerator];
  3704. NSNumber *aKey;
  3705. while ((aKey = [keys nextObject])) {
  3706. NSNumber *aValue = internal[aKey];
  3707. [outputStream writeInt32NoTag:tag];
  3708. // Write the size of the message.
  3709. int32_t unwrappedKey = [aKey intValue];
  3710. float unwrappedValue = [aValue floatValue];
  3711. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3712. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3713. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3714. // Write the fields.
  3715. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3716. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3717. }
  3718. }
  3719. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3720. forGPBGenericValueKey:(GPBGenericValue *)key {
  3721. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  3722. }
  3723. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3724. [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, __unused BOOL *stop) {
  3725. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  3726. }];
  3727. }
  3728. - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
  3729. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3730. if (wrapped && value) {
  3731. *value = [wrapped floatValue];
  3732. }
  3733. return (wrapped != NULL);
  3734. }
  3735. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  3736. if (otherDictionary) {
  3737. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3738. if (_autocreator) {
  3739. GPBAutocreatedDictionaryModified(_autocreator, self);
  3740. }
  3741. }
  3742. }
  3743. - (void)setFloat:(float)value forKey:(int32_t)key {
  3744. [_dictionary setObject:@(value) forKey:@(key)];
  3745. if (_autocreator) {
  3746. GPBAutocreatedDictionaryModified(_autocreator, self);
  3747. }
  3748. }
  3749. - (void)removeFloatForKey:(int32_t)aKey {
  3750. [_dictionary removeObjectForKey:@(aKey)];
  3751. }
  3752. - (void)removeAll {
  3753. [_dictionary removeAllObjects];
  3754. }
  3755. @end
  3756. #pragma mark - Int32 -> Double
  3757. @implementation GPBInt32DoubleDictionary {
  3758. @package
  3759. NSMutableDictionary *_dictionary;
  3760. }
  3761. - (instancetype)init {
  3762. return [self initWithDoubles:NULL forKeys:NULL count:0];
  3763. }
  3764. - (instancetype)initWithDoubles:(const double [])values
  3765. forKeys:(const int32_t [])keys
  3766. count:(NSUInteger)count {
  3767. self = [super init];
  3768. if (self) {
  3769. _dictionary = [[NSMutableDictionary alloc] init];
  3770. if (count && values && keys) {
  3771. for (NSUInteger i = 0; i < count; ++i) {
  3772. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3773. }
  3774. }
  3775. }
  3776. return self;
  3777. }
  3778. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  3779. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  3780. if (self) {
  3781. if (dictionary) {
  3782. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3783. }
  3784. }
  3785. return self;
  3786. }
  3787. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  3788. return [self initWithDoubles:NULL forKeys:NULL count:0];
  3789. }
  3790. - (void)dealloc {
  3791. NSAssert(!_autocreator,
  3792. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3793. [self class], _autocreator);
  3794. [_dictionary release];
  3795. [super dealloc];
  3796. }
  3797. - (instancetype)copyWithZone:(NSZone *)zone {
  3798. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  3799. }
  3800. - (BOOL)isEqual:(id)other {
  3801. if (self == other) {
  3802. return YES;
  3803. }
  3804. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  3805. return NO;
  3806. }
  3807. GPBInt32DoubleDictionary *otherDictionary = other;
  3808. return [_dictionary isEqual:otherDictionary->_dictionary];
  3809. }
  3810. - (NSUInteger)hash {
  3811. return _dictionary.count;
  3812. }
  3813. - (NSString *)description {
  3814. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3815. }
  3816. - (NSUInteger)count {
  3817. return _dictionary.count;
  3818. }
  3819. - (void)enumerateKeysAndDoublesUsingBlock:
  3820. (void (NS_NOESCAPE ^)(int32_t key, double value, BOOL *stop))block {
  3821. BOOL stop = NO;
  3822. NSDictionary *internal = _dictionary;
  3823. NSEnumerator *keys = [internal keyEnumerator];
  3824. NSNumber *aKey;
  3825. while ((aKey = [keys nextObject])) {
  3826. NSNumber *aValue = internal[aKey];
  3827. block([aKey intValue], [aValue doubleValue], &stop);
  3828. if (stop) {
  3829. break;
  3830. }
  3831. }
  3832. }
  3833. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3834. NSDictionary *internal = _dictionary;
  3835. NSUInteger count = internal.count;
  3836. if (count == 0) {
  3837. return 0;
  3838. }
  3839. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3840. GPBDataType keyDataType = field.mapKeyDataType;
  3841. size_t result = 0;
  3842. NSEnumerator *keys = [internal keyEnumerator];
  3843. NSNumber *aKey;
  3844. while ((aKey = [keys nextObject])) {
  3845. NSNumber *aValue = internal[aKey];
  3846. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3847. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  3848. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3849. }
  3850. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3851. result += tagSize * count;
  3852. return result;
  3853. }
  3854. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3855. asField:(GPBFieldDescriptor *)field {
  3856. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3857. GPBDataType keyDataType = field.mapKeyDataType;
  3858. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3859. NSDictionary *internal = _dictionary;
  3860. NSEnumerator *keys = [internal keyEnumerator];
  3861. NSNumber *aKey;
  3862. while ((aKey = [keys nextObject])) {
  3863. NSNumber *aValue = internal[aKey];
  3864. [outputStream writeInt32NoTag:tag];
  3865. // Write the size of the message.
  3866. int32_t unwrappedKey = [aKey intValue];
  3867. double unwrappedValue = [aValue doubleValue];
  3868. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3869. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3870. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3871. // Write the fields.
  3872. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3873. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3874. }
  3875. }
  3876. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3877. forGPBGenericValueKey:(GPBGenericValue *)key {
  3878. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  3879. }
  3880. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3881. [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, __unused BOOL *stop) {
  3882. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  3883. }];
  3884. }
  3885. - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
  3886. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3887. if (wrapped && value) {
  3888. *value = [wrapped doubleValue];
  3889. }
  3890. return (wrapped != NULL);
  3891. }
  3892. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  3893. if (otherDictionary) {
  3894. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3895. if (_autocreator) {
  3896. GPBAutocreatedDictionaryModified(_autocreator, self);
  3897. }
  3898. }
  3899. }
  3900. - (void)setDouble:(double)value forKey:(int32_t)key {
  3901. [_dictionary setObject:@(value) forKey:@(key)];
  3902. if (_autocreator) {
  3903. GPBAutocreatedDictionaryModified(_autocreator, self);
  3904. }
  3905. }
  3906. - (void)removeDoubleForKey:(int32_t)aKey {
  3907. [_dictionary removeObjectForKey:@(aKey)];
  3908. }
  3909. - (void)removeAll {
  3910. [_dictionary removeAllObjects];
  3911. }
  3912. @end
  3913. #pragma mark - Int32 -> Enum
  3914. @implementation GPBInt32EnumDictionary {
  3915. @package
  3916. NSMutableDictionary *_dictionary;
  3917. GPBEnumValidationFunc _validationFunc;
  3918. }
  3919. @synthesize validationFunc = _validationFunc;
  3920. - (instancetype)init {
  3921. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  3922. }
  3923. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  3924. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  3925. }
  3926. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  3927. rawValues:(const int32_t [])rawValues
  3928. forKeys:(const int32_t [])keys
  3929. count:(NSUInteger)count {
  3930. self = [super init];
  3931. if (self) {
  3932. _dictionary = [[NSMutableDictionary alloc] init];
  3933. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  3934. if (count && rawValues && keys) {
  3935. for (NSUInteger i = 0; i < count; ++i) {
  3936. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  3937. }
  3938. }
  3939. }
  3940. return self;
  3941. }
  3942. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  3943. self = [self initWithValidationFunction:dictionary.validationFunc
  3944. rawValues:NULL
  3945. forKeys:NULL
  3946. count:0];
  3947. if (self) {
  3948. if (dictionary) {
  3949. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3950. }
  3951. }
  3952. return self;
  3953. }
  3954. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  3955. capacity:(__unused NSUInteger)numItems {
  3956. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  3957. }
  3958. - (void)dealloc {
  3959. NSAssert(!_autocreator,
  3960. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3961. [self class], _autocreator);
  3962. [_dictionary release];
  3963. [super dealloc];
  3964. }
  3965. - (instancetype)copyWithZone:(NSZone *)zone {
  3966. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  3967. }
  3968. - (BOOL)isEqual:(id)other {
  3969. if (self == other) {
  3970. return YES;
  3971. }
  3972. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  3973. return NO;
  3974. }
  3975. GPBInt32EnumDictionary *otherDictionary = other;
  3976. return [_dictionary isEqual:otherDictionary->_dictionary];
  3977. }
  3978. - (NSUInteger)hash {
  3979. return _dictionary.count;
  3980. }
  3981. - (NSString *)description {
  3982. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3983. }
  3984. - (NSUInteger)count {
  3985. return _dictionary.count;
  3986. }
  3987. - (void)enumerateKeysAndRawValuesUsingBlock:
  3988. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  3989. BOOL stop = NO;
  3990. NSDictionary *internal = _dictionary;
  3991. NSEnumerator *keys = [internal keyEnumerator];
  3992. NSNumber *aKey;
  3993. while ((aKey = [keys nextObject])) {
  3994. NSNumber *aValue = internal[aKey];
  3995. block([aKey intValue], [aValue intValue], &stop);
  3996. if (stop) {
  3997. break;
  3998. }
  3999. }
  4000. }
  4001. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4002. NSDictionary *internal = _dictionary;
  4003. NSUInteger count = internal.count;
  4004. if (count == 0) {
  4005. return 0;
  4006. }
  4007. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4008. GPBDataType keyDataType = field.mapKeyDataType;
  4009. size_t result = 0;
  4010. NSEnumerator *keys = [internal keyEnumerator];
  4011. NSNumber *aKey;
  4012. while ((aKey = [keys nextObject])) {
  4013. NSNumber *aValue = internal[aKey];
  4014. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4015. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4016. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4017. }
  4018. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4019. result += tagSize * count;
  4020. return result;
  4021. }
  4022. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4023. asField:(GPBFieldDescriptor *)field {
  4024. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4025. GPBDataType keyDataType = field.mapKeyDataType;
  4026. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4027. NSDictionary *internal = _dictionary;
  4028. NSEnumerator *keys = [internal keyEnumerator];
  4029. NSNumber *aKey;
  4030. while ((aKey = [keys nextObject])) {
  4031. NSNumber *aValue = internal[aKey];
  4032. [outputStream writeInt32NoTag:tag];
  4033. // Write the size of the message.
  4034. int32_t unwrappedKey = [aKey intValue];
  4035. int32_t unwrappedValue = [aValue intValue];
  4036. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4037. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4038. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4039. // Write the fields.
  4040. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4041. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4042. }
  4043. }
  4044. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4045. forKey:(GPBGenericValue *)key
  4046. keyDataType:(GPBDataType)keyDataType {
  4047. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4048. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4049. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4050. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4051. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4052. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4053. [outputStream flush];
  4054. [outputStream release];
  4055. return data;
  4056. }
  4057. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4058. forGPBGenericValueKey:(GPBGenericValue *)key {
  4059. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4060. }
  4061. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4062. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, __unused BOOL *stop) {
  4063. block([NSString stringWithFormat:@"%d", key], @(value));
  4064. }];
  4065. }
  4066. - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
  4067. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4068. if (wrapped && value) {
  4069. int32_t result = [wrapped intValue];
  4070. if (!_validationFunc(result)) {
  4071. result = kGPBUnrecognizedEnumeratorValue;
  4072. }
  4073. *value = result;
  4074. }
  4075. return (wrapped != NULL);
  4076. }
  4077. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
  4078. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4079. if (wrapped && rawValue) {
  4080. *rawValue = [wrapped intValue];
  4081. }
  4082. return (wrapped != NULL);
  4083. }
  4084. - (void)enumerateKeysAndEnumsUsingBlock:
  4085. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  4086. GPBEnumValidationFunc func = _validationFunc;
  4087. BOOL stop = NO;
  4088. NSEnumerator *keys = [_dictionary keyEnumerator];
  4089. NSNumber *aKey;
  4090. while ((aKey = [keys nextObject])) {
  4091. NSNumber *aValue = _dictionary[aKey];
  4092. int32_t unwrapped = [aValue intValue];
  4093. if (!func(unwrapped)) {
  4094. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4095. }
  4096. block([aKey intValue], unwrapped, &stop);
  4097. if (stop) {
  4098. break;
  4099. }
  4100. }
  4101. }
  4102. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4103. if (otherDictionary) {
  4104. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4105. if (_autocreator) {
  4106. GPBAutocreatedDictionaryModified(_autocreator, self);
  4107. }
  4108. }
  4109. }
  4110. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4111. [_dictionary setObject:@(value) forKey:@(key)];
  4112. if (_autocreator) {
  4113. GPBAutocreatedDictionaryModified(_autocreator, self);
  4114. }
  4115. }
  4116. - (void)removeEnumForKey:(int32_t)aKey {
  4117. [_dictionary removeObjectForKey:@(aKey)];
  4118. }
  4119. - (void)removeAll {
  4120. [_dictionary removeAllObjects];
  4121. }
  4122. - (void)setEnum:(int32_t)value forKey:(int32_t)key {
  4123. if (!_validationFunc(value)) {
  4124. [NSException raise:NSInvalidArgumentException
  4125. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4126. value];
  4127. }
  4128. [_dictionary setObject:@(value) forKey:@(key)];
  4129. if (_autocreator) {
  4130. GPBAutocreatedDictionaryModified(_autocreator, self);
  4131. }
  4132. }
  4133. @end
  4134. #pragma mark - Int32 -> Object
  4135. @implementation GPBInt32ObjectDictionary {
  4136. @package
  4137. NSMutableDictionary *_dictionary;
  4138. }
  4139. - (instancetype)init {
  4140. return [self initWithObjects:NULL forKeys:NULL count:0];
  4141. }
  4142. - (instancetype)initWithObjects:(const id [])objects
  4143. forKeys:(const int32_t [])keys
  4144. count:(NSUInteger)count {
  4145. self = [super init];
  4146. if (self) {
  4147. _dictionary = [[NSMutableDictionary alloc] init];
  4148. if (count && objects && keys) {
  4149. for (NSUInteger i = 0; i < count; ++i) {
  4150. if (!objects[i]) {
  4151. [NSException raise:NSInvalidArgumentException
  4152. format:@"Attempting to add nil object to a Dictionary"];
  4153. }
  4154. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4155. }
  4156. }
  4157. }
  4158. return self;
  4159. }
  4160. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4161. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4162. if (self) {
  4163. if (dictionary) {
  4164. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4165. }
  4166. }
  4167. return self;
  4168. }
  4169. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4170. return [self initWithObjects:NULL forKeys:NULL count:0];
  4171. }
  4172. - (void)dealloc {
  4173. NSAssert(!_autocreator,
  4174. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4175. [self class], _autocreator);
  4176. [_dictionary release];
  4177. [super dealloc];
  4178. }
  4179. - (instancetype)copyWithZone:(NSZone *)zone {
  4180. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4181. }
  4182. - (BOOL)isEqual:(id)other {
  4183. if (self == other) {
  4184. return YES;
  4185. }
  4186. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4187. return NO;
  4188. }
  4189. GPBInt32ObjectDictionary *otherDictionary = other;
  4190. return [_dictionary isEqual:otherDictionary->_dictionary];
  4191. }
  4192. - (NSUInteger)hash {
  4193. return _dictionary.count;
  4194. }
  4195. - (NSString *)description {
  4196. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4197. }
  4198. - (NSUInteger)count {
  4199. return _dictionary.count;
  4200. }
  4201. - (void)enumerateKeysAndObjectsUsingBlock:
  4202. (void (NS_NOESCAPE ^)(int32_t key, id object, BOOL *stop))block {
  4203. BOOL stop = NO;
  4204. NSDictionary *internal = _dictionary;
  4205. NSEnumerator *keys = [internal keyEnumerator];
  4206. NSNumber *aKey;
  4207. while ((aKey = [keys nextObject])) {
  4208. id aObject = internal[aKey];
  4209. block([aKey intValue], aObject, &stop);
  4210. if (stop) {
  4211. break;
  4212. }
  4213. }
  4214. }
  4215. - (BOOL)isInitialized {
  4216. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4217. if (!msg.initialized) {
  4218. return NO;
  4219. }
  4220. }
  4221. return YES;
  4222. }
  4223. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4224. GPBInt32ObjectDictionary *newDict =
  4225. [[GPBInt32ObjectDictionary alloc] init];
  4226. NSEnumerator *keys = [_dictionary keyEnumerator];
  4227. id aKey;
  4228. NSMutableDictionary *internalDict = newDict->_dictionary;
  4229. while ((aKey = [keys nextObject])) {
  4230. GPBMessage *msg = _dictionary[aKey];
  4231. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4232. [internalDict setObject:copiedMsg forKey:aKey];
  4233. [copiedMsg release];
  4234. }
  4235. return newDict;
  4236. }
  4237. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4238. NSDictionary *internal = _dictionary;
  4239. NSUInteger count = internal.count;
  4240. if (count == 0) {
  4241. return 0;
  4242. }
  4243. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4244. GPBDataType keyDataType = field.mapKeyDataType;
  4245. size_t result = 0;
  4246. NSEnumerator *keys = [internal keyEnumerator];
  4247. NSNumber *aKey;
  4248. while ((aKey = [keys nextObject])) {
  4249. id aObject = internal[aKey];
  4250. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4251. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4252. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4253. }
  4254. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4255. result += tagSize * count;
  4256. return result;
  4257. }
  4258. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4259. asField:(GPBFieldDescriptor *)field {
  4260. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4261. GPBDataType keyDataType = field.mapKeyDataType;
  4262. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4263. NSDictionary *internal = _dictionary;
  4264. NSEnumerator *keys = [internal keyEnumerator];
  4265. NSNumber *aKey;
  4266. while ((aKey = [keys nextObject])) {
  4267. id aObject = internal[aKey];
  4268. [outputStream writeInt32NoTag:tag];
  4269. // Write the size of the message.
  4270. int32_t unwrappedKey = [aKey intValue];
  4271. id unwrappedValue = aObject;
  4272. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4273. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4274. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4275. // Write the fields.
  4276. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4277. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4278. }
  4279. }
  4280. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4281. forGPBGenericValueKey:(GPBGenericValue *)key {
  4282. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4283. }
  4284. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4285. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, __unused BOOL *stop) {
  4286. block([NSString stringWithFormat:@"%d", key], object);
  4287. }];
  4288. }
  4289. - (id)objectForKey:(int32_t)key {
  4290. id result = [_dictionary objectForKey:@(key)];
  4291. return result;
  4292. }
  4293. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4294. if (otherDictionary) {
  4295. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4296. if (_autocreator) {
  4297. GPBAutocreatedDictionaryModified(_autocreator, self);
  4298. }
  4299. }
  4300. }
  4301. - (void)setObject:(id)object forKey:(int32_t)key {
  4302. if (!object) {
  4303. [NSException raise:NSInvalidArgumentException
  4304. format:@"Attempting to add nil object to a Dictionary"];
  4305. }
  4306. [_dictionary setObject:object forKey:@(key)];
  4307. if (_autocreator) {
  4308. GPBAutocreatedDictionaryModified(_autocreator, self);
  4309. }
  4310. }
  4311. - (void)removeObjectForKey:(int32_t)aKey {
  4312. [_dictionary removeObjectForKey:@(aKey)];
  4313. }
  4314. - (void)removeAll {
  4315. [_dictionary removeAllObjects];
  4316. }
  4317. @end
  4318. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4319. // This block of code is generated, do not edit it directly.
  4320. #pragma mark - UInt64 -> UInt32
  4321. @implementation GPBUInt64UInt32Dictionary {
  4322. @package
  4323. NSMutableDictionary *_dictionary;
  4324. }
  4325. - (instancetype)init {
  4326. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4327. }
  4328. - (instancetype)initWithUInt32s:(const uint32_t [])values
  4329. forKeys:(const uint64_t [])keys
  4330. count:(NSUInteger)count {
  4331. self = [super init];
  4332. if (self) {
  4333. _dictionary = [[NSMutableDictionary alloc] init];
  4334. if (count && values && keys) {
  4335. for (NSUInteger i = 0; i < count; ++i) {
  4336. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4337. }
  4338. }
  4339. }
  4340. return self;
  4341. }
  4342. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4343. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  4344. if (self) {
  4345. if (dictionary) {
  4346. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4347. }
  4348. }
  4349. return self;
  4350. }
  4351. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4352. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4353. }
  4354. - (void)dealloc {
  4355. NSAssert(!_autocreator,
  4356. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4357. [self class], _autocreator);
  4358. [_dictionary release];
  4359. [super dealloc];
  4360. }
  4361. - (instancetype)copyWithZone:(NSZone *)zone {
  4362. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4363. }
  4364. - (BOOL)isEqual:(id)other {
  4365. if (self == other) {
  4366. return YES;
  4367. }
  4368. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4369. return NO;
  4370. }
  4371. GPBUInt64UInt32Dictionary *otherDictionary = other;
  4372. return [_dictionary isEqual:otherDictionary->_dictionary];
  4373. }
  4374. - (NSUInteger)hash {
  4375. return _dictionary.count;
  4376. }
  4377. - (NSString *)description {
  4378. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4379. }
  4380. - (NSUInteger)count {
  4381. return _dictionary.count;
  4382. }
  4383. - (void)enumerateKeysAndUInt32sUsingBlock:
  4384. (void (NS_NOESCAPE ^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4385. BOOL stop = NO;
  4386. NSDictionary *internal = _dictionary;
  4387. NSEnumerator *keys = [internal keyEnumerator];
  4388. NSNumber *aKey;
  4389. while ((aKey = [keys nextObject])) {
  4390. NSNumber *aValue = internal[aKey];
  4391. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], &stop);
  4392. if (stop) {
  4393. break;
  4394. }
  4395. }
  4396. }
  4397. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4398. NSDictionary *internal = _dictionary;
  4399. NSUInteger count = internal.count;
  4400. if (count == 0) {
  4401. return 0;
  4402. }
  4403. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4404. GPBDataType keyDataType = field.mapKeyDataType;
  4405. size_t result = 0;
  4406. NSEnumerator *keys = [internal keyEnumerator];
  4407. NSNumber *aKey;
  4408. while ((aKey = [keys nextObject])) {
  4409. NSNumber *aValue = internal[aKey];
  4410. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4411. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4412. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4413. }
  4414. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4415. result += tagSize * count;
  4416. return result;
  4417. }
  4418. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4419. asField:(GPBFieldDescriptor *)field {
  4420. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4421. GPBDataType keyDataType = field.mapKeyDataType;
  4422. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4423. NSDictionary *internal = _dictionary;
  4424. NSEnumerator *keys = [internal keyEnumerator];
  4425. NSNumber *aKey;
  4426. while ((aKey = [keys nextObject])) {
  4427. NSNumber *aValue = internal[aKey];
  4428. [outputStream writeInt32NoTag:tag];
  4429. // Write the size of the message.
  4430. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4431. uint32_t unwrappedValue = [aValue unsignedIntValue];
  4432. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4433. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4434. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4435. // Write the fields.
  4436. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4437. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4438. }
  4439. }
  4440. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4441. forGPBGenericValueKey:(GPBGenericValue *)key {
  4442. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4443. }
  4444. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4445. [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, __unused BOOL *stop) {
  4446. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4447. }];
  4448. }
  4449. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
  4450. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4451. if (wrapped && value) {
  4452. *value = [wrapped unsignedIntValue];
  4453. }
  4454. return (wrapped != NULL);
  4455. }
  4456. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4457. if (otherDictionary) {
  4458. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4459. if (_autocreator) {
  4460. GPBAutocreatedDictionaryModified(_autocreator, self);
  4461. }
  4462. }
  4463. }
  4464. - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
  4465. [_dictionary setObject:@(value) forKey:@(key)];
  4466. if (_autocreator) {
  4467. GPBAutocreatedDictionaryModified(_autocreator, self);
  4468. }
  4469. }
  4470. - (void)removeUInt32ForKey:(uint64_t)aKey {
  4471. [_dictionary removeObjectForKey:@(aKey)];
  4472. }
  4473. - (void)removeAll {
  4474. [_dictionary removeAllObjects];
  4475. }
  4476. @end
  4477. #pragma mark - UInt64 -> Int32
  4478. @implementation GPBUInt64Int32Dictionary {
  4479. @package
  4480. NSMutableDictionary *_dictionary;
  4481. }
  4482. - (instancetype)init {
  4483. return [self initWithInt32s:NULL forKeys:NULL count:0];
  4484. }
  4485. - (instancetype)initWithInt32s:(const int32_t [])values
  4486. forKeys:(const uint64_t [])keys
  4487. count:(NSUInteger)count {
  4488. self = [super init];
  4489. if (self) {
  4490. _dictionary = [[NSMutableDictionary alloc] init];
  4491. if (count && values && keys) {
  4492. for (NSUInteger i = 0; i < count; ++i) {
  4493. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4494. }
  4495. }
  4496. }
  4497. return self;
  4498. }
  4499. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4500. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  4501. if (self) {
  4502. if (dictionary) {
  4503. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4504. }
  4505. }
  4506. return self;
  4507. }
  4508. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4509. return [self initWithInt32s:NULL forKeys:NULL count:0];
  4510. }
  4511. - (void)dealloc {
  4512. NSAssert(!_autocreator,
  4513. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4514. [self class], _autocreator);
  4515. [_dictionary release];
  4516. [super dealloc];
  4517. }
  4518. - (instancetype)copyWithZone:(NSZone *)zone {
  4519. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  4520. }
  4521. - (BOOL)isEqual:(id)other {
  4522. if (self == other) {
  4523. return YES;
  4524. }
  4525. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  4526. return NO;
  4527. }
  4528. GPBUInt64Int32Dictionary *otherDictionary = other;
  4529. return [_dictionary isEqual:otherDictionary->_dictionary];
  4530. }
  4531. - (NSUInteger)hash {
  4532. return _dictionary.count;
  4533. }
  4534. - (NSString *)description {
  4535. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4536. }
  4537. - (NSUInteger)count {
  4538. return _dictionary.count;
  4539. }
  4540. - (void)enumerateKeysAndInt32sUsingBlock:
  4541. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  4542. BOOL stop = NO;
  4543. NSDictionary *internal = _dictionary;
  4544. NSEnumerator *keys = [internal keyEnumerator];
  4545. NSNumber *aKey;
  4546. while ((aKey = [keys nextObject])) {
  4547. NSNumber *aValue = internal[aKey];
  4548. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  4549. if (stop) {
  4550. break;
  4551. }
  4552. }
  4553. }
  4554. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4555. NSDictionary *internal = _dictionary;
  4556. NSUInteger count = internal.count;
  4557. if (count == 0) {
  4558. return 0;
  4559. }
  4560. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4561. GPBDataType keyDataType = field.mapKeyDataType;
  4562. size_t result = 0;
  4563. NSEnumerator *keys = [internal keyEnumerator];
  4564. NSNumber *aKey;
  4565. while ((aKey = [keys nextObject])) {
  4566. NSNumber *aValue = internal[aKey];
  4567. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4568. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4570. }
  4571. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4572. result += tagSize * count;
  4573. return result;
  4574. }
  4575. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4576. asField:(GPBFieldDescriptor *)field {
  4577. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4578. GPBDataType keyDataType = field.mapKeyDataType;
  4579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4580. NSDictionary *internal = _dictionary;
  4581. NSEnumerator *keys = [internal keyEnumerator];
  4582. NSNumber *aKey;
  4583. while ((aKey = [keys nextObject])) {
  4584. NSNumber *aValue = internal[aKey];
  4585. [outputStream writeInt32NoTag:tag];
  4586. // Write the size of the message.
  4587. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4588. int32_t unwrappedValue = [aValue intValue];
  4589. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4590. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4591. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4592. // Write the fields.
  4593. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4594. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4595. }
  4596. }
  4597. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4598. forGPBGenericValueKey:(GPBGenericValue *)key {
  4599. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  4600. }
  4601. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4602. [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, __unused BOOL *stop) {
  4603. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  4604. }];
  4605. }
  4606. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
  4607. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4608. if (wrapped && value) {
  4609. *value = [wrapped intValue];
  4610. }
  4611. return (wrapped != NULL);
  4612. }
  4613. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  4614. if (otherDictionary) {
  4615. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4616. if (_autocreator) {
  4617. GPBAutocreatedDictionaryModified(_autocreator, self);
  4618. }
  4619. }
  4620. }
  4621. - (void)setInt32:(int32_t)value forKey:(uint64_t)key {
  4622. [_dictionary setObject:@(value) forKey:@(key)];
  4623. if (_autocreator) {
  4624. GPBAutocreatedDictionaryModified(_autocreator, self);
  4625. }
  4626. }
  4627. - (void)removeInt32ForKey:(uint64_t)aKey {
  4628. [_dictionary removeObjectForKey:@(aKey)];
  4629. }
  4630. - (void)removeAll {
  4631. [_dictionary removeAllObjects];
  4632. }
  4633. @end
  4634. #pragma mark - UInt64 -> UInt64
  4635. @implementation GPBUInt64UInt64Dictionary {
  4636. @package
  4637. NSMutableDictionary *_dictionary;
  4638. }
  4639. - (instancetype)init {
  4640. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  4641. }
  4642. - (instancetype)initWithUInt64s:(const uint64_t [])values
  4643. forKeys:(const uint64_t [])keys
  4644. count:(NSUInteger)count {
  4645. self = [super init];
  4646. if (self) {
  4647. _dictionary = [[NSMutableDictionary alloc] init];
  4648. if (count && values && keys) {
  4649. for (NSUInteger i = 0; i < count; ++i) {
  4650. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4651. }
  4652. }
  4653. }
  4654. return self;
  4655. }
  4656. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  4657. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  4658. if (self) {
  4659. if (dictionary) {
  4660. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4661. }
  4662. }
  4663. return self;
  4664. }
  4665. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4666. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  4667. }
  4668. - (void)dealloc {
  4669. NSAssert(!_autocreator,
  4670. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4671. [self class], _autocreator);
  4672. [_dictionary release];
  4673. [super dealloc];
  4674. }
  4675. - (instancetype)copyWithZone:(NSZone *)zone {
  4676. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  4677. }
  4678. - (BOOL)isEqual:(id)other {
  4679. if (self == other) {
  4680. return YES;
  4681. }
  4682. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  4683. return NO;
  4684. }
  4685. GPBUInt64UInt64Dictionary *otherDictionary = other;
  4686. return [_dictionary isEqual:otherDictionary->_dictionary];
  4687. }
  4688. - (NSUInteger)hash {
  4689. return _dictionary.count;
  4690. }
  4691. - (NSString *)description {
  4692. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4693. }
  4694. - (NSUInteger)count {
  4695. return _dictionary.count;
  4696. }
  4697. - (void)enumerateKeysAndUInt64sUsingBlock:
  4698. (void (NS_NOESCAPE ^)(uint64_t key, uint64_t value, BOOL *stop))block {
  4699. BOOL stop = NO;
  4700. NSDictionary *internal = _dictionary;
  4701. NSEnumerator *keys = [internal keyEnumerator];
  4702. NSNumber *aKey;
  4703. while ((aKey = [keys nextObject])) {
  4704. NSNumber *aValue = internal[aKey];
  4705. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], &stop);
  4706. if (stop) {
  4707. break;
  4708. }
  4709. }
  4710. }
  4711. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4712. NSDictionary *internal = _dictionary;
  4713. NSUInteger count = internal.count;
  4714. if (count == 0) {
  4715. return 0;
  4716. }
  4717. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4718. GPBDataType keyDataType = field.mapKeyDataType;
  4719. size_t result = 0;
  4720. NSEnumerator *keys = [internal keyEnumerator];
  4721. NSNumber *aKey;
  4722. while ((aKey = [keys nextObject])) {
  4723. NSNumber *aValue = internal[aKey];
  4724. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4725. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  4726. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4727. }
  4728. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4729. result += tagSize * count;
  4730. return result;
  4731. }
  4732. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4733. asField:(GPBFieldDescriptor *)field {
  4734. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4735. GPBDataType keyDataType = field.mapKeyDataType;
  4736. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4737. NSDictionary *internal = _dictionary;
  4738. NSEnumerator *keys = [internal keyEnumerator];
  4739. NSNumber *aKey;
  4740. while ((aKey = [keys nextObject])) {
  4741. NSNumber *aValue = internal[aKey];
  4742. [outputStream writeInt32NoTag:tag];
  4743. // Write the size of the message.
  4744. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4745. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  4746. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4747. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4748. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4749. // Write the fields.
  4750. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4751. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4752. }
  4753. }
  4754. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4755. forGPBGenericValueKey:(GPBGenericValue *)key {
  4756. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  4757. }
  4758. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4759. [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, __unused BOOL *stop) {
  4760. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  4761. }];
  4762. }
  4763. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
  4764. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4765. if (wrapped && value) {
  4766. *value = [wrapped unsignedLongLongValue];
  4767. }
  4768. return (wrapped != NULL);
  4769. }
  4770. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  4771. if (otherDictionary) {
  4772. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4773. if (_autocreator) {
  4774. GPBAutocreatedDictionaryModified(_autocreator, self);
  4775. }
  4776. }
  4777. }
  4778. - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
  4779. [_dictionary setObject:@(value) forKey:@(key)];
  4780. if (_autocreator) {
  4781. GPBAutocreatedDictionaryModified(_autocreator, self);
  4782. }
  4783. }
  4784. - (void)removeUInt64ForKey:(uint64_t)aKey {
  4785. [_dictionary removeObjectForKey:@(aKey)];
  4786. }
  4787. - (void)removeAll {
  4788. [_dictionary removeAllObjects];
  4789. }
  4790. @end
  4791. #pragma mark - UInt64 -> Int64
  4792. @implementation GPBUInt64Int64Dictionary {
  4793. @package
  4794. NSMutableDictionary *_dictionary;
  4795. }
  4796. - (instancetype)init {
  4797. return [self initWithInt64s:NULL forKeys:NULL count:0];
  4798. }
  4799. - (instancetype)initWithInt64s:(const int64_t [])values
  4800. forKeys:(const uint64_t [])keys
  4801. count:(NSUInteger)count {
  4802. self = [super init];
  4803. if (self) {
  4804. _dictionary = [[NSMutableDictionary alloc] init];
  4805. if (count && values && keys) {
  4806. for (NSUInteger i = 0; i < count; ++i) {
  4807. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4808. }
  4809. }
  4810. }
  4811. return self;
  4812. }
  4813. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  4814. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  4815. if (self) {
  4816. if (dictionary) {
  4817. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4818. }
  4819. }
  4820. return self;
  4821. }
  4822. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4823. return [self initWithInt64s:NULL forKeys:NULL count:0];
  4824. }
  4825. - (void)dealloc {
  4826. NSAssert(!_autocreator,
  4827. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4828. [self class], _autocreator);
  4829. [_dictionary release];
  4830. [super dealloc];
  4831. }
  4832. - (instancetype)copyWithZone:(NSZone *)zone {
  4833. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  4834. }
  4835. - (BOOL)isEqual:(id)other {
  4836. if (self == other) {
  4837. return YES;
  4838. }
  4839. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  4840. return NO;
  4841. }
  4842. GPBUInt64Int64Dictionary *otherDictionary = other;
  4843. return [_dictionary isEqual:otherDictionary->_dictionary];
  4844. }
  4845. - (NSUInteger)hash {
  4846. return _dictionary.count;
  4847. }
  4848. - (NSString *)description {
  4849. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4850. }
  4851. - (NSUInteger)count {
  4852. return _dictionary.count;
  4853. }
  4854. - (void)enumerateKeysAndInt64sUsingBlock:
  4855. (void (NS_NOESCAPE ^)(uint64_t key, int64_t value, BOOL *stop))block {
  4856. BOOL stop = NO;
  4857. NSDictionary *internal = _dictionary;
  4858. NSEnumerator *keys = [internal keyEnumerator];
  4859. NSNumber *aKey;
  4860. while ((aKey = [keys nextObject])) {
  4861. NSNumber *aValue = internal[aKey];
  4862. block([aKey unsignedLongLongValue], [aValue longLongValue], &stop);
  4863. if (stop) {
  4864. break;
  4865. }
  4866. }
  4867. }
  4868. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4869. NSDictionary *internal = _dictionary;
  4870. NSUInteger count = internal.count;
  4871. if (count == 0) {
  4872. return 0;
  4873. }
  4874. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4875. GPBDataType keyDataType = field.mapKeyDataType;
  4876. size_t result = 0;
  4877. NSEnumerator *keys = [internal keyEnumerator];
  4878. NSNumber *aKey;
  4879. while ((aKey = [keys nextObject])) {
  4880. NSNumber *aValue = internal[aKey];
  4881. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4882. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  4883. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4884. }
  4885. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4886. result += tagSize * count;
  4887. return result;
  4888. }
  4889. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4890. asField:(GPBFieldDescriptor *)field {
  4891. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4892. GPBDataType keyDataType = field.mapKeyDataType;
  4893. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4894. NSDictionary *internal = _dictionary;
  4895. NSEnumerator *keys = [internal keyEnumerator];
  4896. NSNumber *aKey;
  4897. while ((aKey = [keys nextObject])) {
  4898. NSNumber *aValue = internal[aKey];
  4899. [outputStream writeInt32NoTag:tag];
  4900. // Write the size of the message.
  4901. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4902. int64_t unwrappedValue = [aValue longLongValue];
  4903. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4904. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4905. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4906. // Write the fields.
  4907. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4908. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4909. }
  4910. }
  4911. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4912. forGPBGenericValueKey:(GPBGenericValue *)key {
  4913. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  4914. }
  4915. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4916. [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, __unused BOOL *stop) {
  4917. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  4918. }];
  4919. }
  4920. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
  4921. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4922. if (wrapped && value) {
  4923. *value = [wrapped longLongValue];
  4924. }
  4925. return (wrapped != NULL);
  4926. }
  4927. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  4928. if (otherDictionary) {
  4929. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4930. if (_autocreator) {
  4931. GPBAutocreatedDictionaryModified(_autocreator, self);
  4932. }
  4933. }
  4934. }
  4935. - (void)setInt64:(int64_t)value forKey:(uint64_t)key {
  4936. [_dictionary setObject:@(value) forKey:@(key)];
  4937. if (_autocreator) {
  4938. GPBAutocreatedDictionaryModified(_autocreator, self);
  4939. }
  4940. }
  4941. - (void)removeInt64ForKey:(uint64_t)aKey {
  4942. [_dictionary removeObjectForKey:@(aKey)];
  4943. }
  4944. - (void)removeAll {
  4945. [_dictionary removeAllObjects];
  4946. }
  4947. @end
  4948. #pragma mark - UInt64 -> Bool
  4949. @implementation GPBUInt64BoolDictionary {
  4950. @package
  4951. NSMutableDictionary *_dictionary;
  4952. }
  4953. - (instancetype)init {
  4954. return [self initWithBools:NULL forKeys:NULL count:0];
  4955. }
  4956. - (instancetype)initWithBools:(const BOOL [])values
  4957. forKeys:(const uint64_t [])keys
  4958. count:(NSUInteger)count {
  4959. self = [super init];
  4960. if (self) {
  4961. _dictionary = [[NSMutableDictionary alloc] init];
  4962. if (count && values && keys) {
  4963. for (NSUInteger i = 0; i < count; ++i) {
  4964. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4965. }
  4966. }
  4967. }
  4968. return self;
  4969. }
  4970. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  4971. self = [self initWithBools:NULL forKeys:NULL count:0];
  4972. if (self) {
  4973. if (dictionary) {
  4974. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4975. }
  4976. }
  4977. return self;
  4978. }
  4979. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  4980. return [self initWithBools:NULL forKeys:NULL count:0];
  4981. }
  4982. - (void)dealloc {
  4983. NSAssert(!_autocreator,
  4984. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4985. [self class], _autocreator);
  4986. [_dictionary release];
  4987. [super dealloc];
  4988. }
  4989. - (instancetype)copyWithZone:(NSZone *)zone {
  4990. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  4991. }
  4992. - (BOOL)isEqual:(id)other {
  4993. if (self == other) {
  4994. return YES;
  4995. }
  4996. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  4997. return NO;
  4998. }
  4999. GPBUInt64BoolDictionary *otherDictionary = other;
  5000. return [_dictionary isEqual:otherDictionary->_dictionary];
  5001. }
  5002. - (NSUInteger)hash {
  5003. return _dictionary.count;
  5004. }
  5005. - (NSString *)description {
  5006. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5007. }
  5008. - (NSUInteger)count {
  5009. return _dictionary.count;
  5010. }
  5011. - (void)enumerateKeysAndBoolsUsingBlock:
  5012. (void (NS_NOESCAPE ^)(uint64_t key, BOOL value, BOOL *stop))block {
  5013. BOOL stop = NO;
  5014. NSDictionary *internal = _dictionary;
  5015. NSEnumerator *keys = [internal keyEnumerator];
  5016. NSNumber *aKey;
  5017. while ((aKey = [keys nextObject])) {
  5018. NSNumber *aValue = internal[aKey];
  5019. block([aKey unsignedLongLongValue], [aValue boolValue], &stop);
  5020. if (stop) {
  5021. break;
  5022. }
  5023. }
  5024. }
  5025. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5026. NSDictionary *internal = _dictionary;
  5027. NSUInteger count = internal.count;
  5028. if (count == 0) {
  5029. return 0;
  5030. }
  5031. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5032. GPBDataType keyDataType = field.mapKeyDataType;
  5033. size_t result = 0;
  5034. NSEnumerator *keys = [internal keyEnumerator];
  5035. NSNumber *aKey;
  5036. while ((aKey = [keys nextObject])) {
  5037. NSNumber *aValue = internal[aKey];
  5038. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5039. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5040. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5041. }
  5042. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5043. result += tagSize * count;
  5044. return result;
  5045. }
  5046. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5047. asField:(GPBFieldDescriptor *)field {
  5048. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5049. GPBDataType keyDataType = field.mapKeyDataType;
  5050. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5051. NSDictionary *internal = _dictionary;
  5052. NSEnumerator *keys = [internal keyEnumerator];
  5053. NSNumber *aKey;
  5054. while ((aKey = [keys nextObject])) {
  5055. NSNumber *aValue = internal[aKey];
  5056. [outputStream writeInt32NoTag:tag];
  5057. // Write the size of the message.
  5058. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5059. BOOL unwrappedValue = [aValue boolValue];
  5060. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5061. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5062. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5063. // Write the fields.
  5064. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5065. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5066. }
  5067. }
  5068. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5069. forGPBGenericValueKey:(GPBGenericValue *)key {
  5070. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5071. }
  5072. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5073. [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, __unused BOOL *stop) {
  5074. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5075. }];
  5076. }
  5077. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
  5078. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5079. if (wrapped && value) {
  5080. *value = [wrapped boolValue];
  5081. }
  5082. return (wrapped != NULL);
  5083. }
  5084. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5085. if (otherDictionary) {
  5086. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5087. if (_autocreator) {
  5088. GPBAutocreatedDictionaryModified(_autocreator, self);
  5089. }
  5090. }
  5091. }
  5092. - (void)setBool:(BOOL)value forKey:(uint64_t)key {
  5093. [_dictionary setObject:@(value) forKey:@(key)];
  5094. if (_autocreator) {
  5095. GPBAutocreatedDictionaryModified(_autocreator, self);
  5096. }
  5097. }
  5098. - (void)removeBoolForKey:(uint64_t)aKey {
  5099. [_dictionary removeObjectForKey:@(aKey)];
  5100. }
  5101. - (void)removeAll {
  5102. [_dictionary removeAllObjects];
  5103. }
  5104. @end
  5105. #pragma mark - UInt64 -> Float
  5106. @implementation GPBUInt64FloatDictionary {
  5107. @package
  5108. NSMutableDictionary *_dictionary;
  5109. }
  5110. - (instancetype)init {
  5111. return [self initWithFloats:NULL forKeys:NULL count:0];
  5112. }
  5113. - (instancetype)initWithFloats:(const float [])values
  5114. forKeys:(const uint64_t [])keys
  5115. count:(NSUInteger)count {
  5116. self = [super init];
  5117. if (self) {
  5118. _dictionary = [[NSMutableDictionary alloc] init];
  5119. if (count && values && keys) {
  5120. for (NSUInteger i = 0; i < count; ++i) {
  5121. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5122. }
  5123. }
  5124. }
  5125. return self;
  5126. }
  5127. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5128. self = [self initWithFloats:NULL forKeys:NULL count:0];
  5129. if (self) {
  5130. if (dictionary) {
  5131. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5132. }
  5133. }
  5134. return self;
  5135. }
  5136. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5137. return [self initWithFloats:NULL forKeys:NULL count:0];
  5138. }
  5139. - (void)dealloc {
  5140. NSAssert(!_autocreator,
  5141. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5142. [self class], _autocreator);
  5143. [_dictionary release];
  5144. [super dealloc];
  5145. }
  5146. - (instancetype)copyWithZone:(NSZone *)zone {
  5147. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5148. }
  5149. - (BOOL)isEqual:(id)other {
  5150. if (self == other) {
  5151. return YES;
  5152. }
  5153. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5154. return NO;
  5155. }
  5156. GPBUInt64FloatDictionary *otherDictionary = other;
  5157. return [_dictionary isEqual:otherDictionary->_dictionary];
  5158. }
  5159. - (NSUInteger)hash {
  5160. return _dictionary.count;
  5161. }
  5162. - (NSString *)description {
  5163. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5164. }
  5165. - (NSUInteger)count {
  5166. return _dictionary.count;
  5167. }
  5168. - (void)enumerateKeysAndFloatsUsingBlock:
  5169. (void (NS_NOESCAPE ^)(uint64_t key, float value, BOOL *stop))block {
  5170. BOOL stop = NO;
  5171. NSDictionary *internal = _dictionary;
  5172. NSEnumerator *keys = [internal keyEnumerator];
  5173. NSNumber *aKey;
  5174. while ((aKey = [keys nextObject])) {
  5175. NSNumber *aValue = internal[aKey];
  5176. block([aKey unsignedLongLongValue], [aValue floatValue], &stop);
  5177. if (stop) {
  5178. break;
  5179. }
  5180. }
  5181. }
  5182. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5183. NSDictionary *internal = _dictionary;
  5184. NSUInteger count = internal.count;
  5185. if (count == 0) {
  5186. return 0;
  5187. }
  5188. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5189. GPBDataType keyDataType = field.mapKeyDataType;
  5190. size_t result = 0;
  5191. NSEnumerator *keys = [internal keyEnumerator];
  5192. NSNumber *aKey;
  5193. while ((aKey = [keys nextObject])) {
  5194. NSNumber *aValue = internal[aKey];
  5195. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5196. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5197. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5198. }
  5199. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5200. result += tagSize * count;
  5201. return result;
  5202. }
  5203. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5204. asField:(GPBFieldDescriptor *)field {
  5205. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5206. GPBDataType keyDataType = field.mapKeyDataType;
  5207. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5208. NSDictionary *internal = _dictionary;
  5209. NSEnumerator *keys = [internal keyEnumerator];
  5210. NSNumber *aKey;
  5211. while ((aKey = [keys nextObject])) {
  5212. NSNumber *aValue = internal[aKey];
  5213. [outputStream writeInt32NoTag:tag];
  5214. // Write the size of the message.
  5215. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5216. float unwrappedValue = [aValue floatValue];
  5217. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5218. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5219. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5220. // Write the fields.
  5221. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5222. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5223. }
  5224. }
  5225. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5226. forGPBGenericValueKey:(GPBGenericValue *)key {
  5227. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5228. }
  5229. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5230. [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, __unused BOOL *stop) {
  5231. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5232. }];
  5233. }
  5234. - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
  5235. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5236. if (wrapped && value) {
  5237. *value = [wrapped floatValue];
  5238. }
  5239. return (wrapped != NULL);
  5240. }
  5241. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5242. if (otherDictionary) {
  5243. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5244. if (_autocreator) {
  5245. GPBAutocreatedDictionaryModified(_autocreator, self);
  5246. }
  5247. }
  5248. }
  5249. - (void)setFloat:(float)value forKey:(uint64_t)key {
  5250. [_dictionary setObject:@(value) forKey:@(key)];
  5251. if (_autocreator) {
  5252. GPBAutocreatedDictionaryModified(_autocreator, self);
  5253. }
  5254. }
  5255. - (void)removeFloatForKey:(uint64_t)aKey {
  5256. [_dictionary removeObjectForKey:@(aKey)];
  5257. }
  5258. - (void)removeAll {
  5259. [_dictionary removeAllObjects];
  5260. }
  5261. @end
  5262. #pragma mark - UInt64 -> Double
  5263. @implementation GPBUInt64DoubleDictionary {
  5264. @package
  5265. NSMutableDictionary *_dictionary;
  5266. }
  5267. - (instancetype)init {
  5268. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5269. }
  5270. - (instancetype)initWithDoubles:(const double [])values
  5271. forKeys:(const uint64_t [])keys
  5272. count:(NSUInteger)count {
  5273. self = [super init];
  5274. if (self) {
  5275. _dictionary = [[NSMutableDictionary alloc] init];
  5276. if (count && values && keys) {
  5277. for (NSUInteger i = 0; i < count; ++i) {
  5278. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5279. }
  5280. }
  5281. }
  5282. return self;
  5283. }
  5284. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5285. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  5286. if (self) {
  5287. if (dictionary) {
  5288. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5289. }
  5290. }
  5291. return self;
  5292. }
  5293. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5294. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5295. }
  5296. - (void)dealloc {
  5297. NSAssert(!_autocreator,
  5298. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5299. [self class], _autocreator);
  5300. [_dictionary release];
  5301. [super dealloc];
  5302. }
  5303. - (instancetype)copyWithZone:(NSZone *)zone {
  5304. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5305. }
  5306. - (BOOL)isEqual:(id)other {
  5307. if (self == other) {
  5308. return YES;
  5309. }
  5310. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5311. return NO;
  5312. }
  5313. GPBUInt64DoubleDictionary *otherDictionary = other;
  5314. return [_dictionary isEqual:otherDictionary->_dictionary];
  5315. }
  5316. - (NSUInteger)hash {
  5317. return _dictionary.count;
  5318. }
  5319. - (NSString *)description {
  5320. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5321. }
  5322. - (NSUInteger)count {
  5323. return _dictionary.count;
  5324. }
  5325. - (void)enumerateKeysAndDoublesUsingBlock:
  5326. (void (NS_NOESCAPE ^)(uint64_t key, double value, BOOL *stop))block {
  5327. BOOL stop = NO;
  5328. NSDictionary *internal = _dictionary;
  5329. NSEnumerator *keys = [internal keyEnumerator];
  5330. NSNumber *aKey;
  5331. while ((aKey = [keys nextObject])) {
  5332. NSNumber *aValue = internal[aKey];
  5333. block([aKey unsignedLongLongValue], [aValue doubleValue], &stop);
  5334. if (stop) {
  5335. break;
  5336. }
  5337. }
  5338. }
  5339. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5340. NSDictionary *internal = _dictionary;
  5341. NSUInteger count = internal.count;
  5342. if (count == 0) {
  5343. return 0;
  5344. }
  5345. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5346. GPBDataType keyDataType = field.mapKeyDataType;
  5347. size_t result = 0;
  5348. NSEnumerator *keys = [internal keyEnumerator];
  5349. NSNumber *aKey;
  5350. while ((aKey = [keys nextObject])) {
  5351. NSNumber *aValue = internal[aKey];
  5352. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5353. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5354. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5355. }
  5356. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5357. result += tagSize * count;
  5358. return result;
  5359. }
  5360. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5361. asField:(GPBFieldDescriptor *)field {
  5362. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5363. GPBDataType keyDataType = field.mapKeyDataType;
  5364. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5365. NSDictionary *internal = _dictionary;
  5366. NSEnumerator *keys = [internal keyEnumerator];
  5367. NSNumber *aKey;
  5368. while ((aKey = [keys nextObject])) {
  5369. NSNumber *aValue = internal[aKey];
  5370. [outputStream writeInt32NoTag:tag];
  5371. // Write the size of the message.
  5372. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5373. double unwrappedValue = [aValue doubleValue];
  5374. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5375. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5376. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5377. // Write the fields.
  5378. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5379. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5380. }
  5381. }
  5382. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5383. forGPBGenericValueKey:(GPBGenericValue *)key {
  5384. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5385. }
  5386. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5387. [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, __unused BOOL *stop) {
  5388. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5389. }];
  5390. }
  5391. - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
  5392. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5393. if (wrapped && value) {
  5394. *value = [wrapped doubleValue];
  5395. }
  5396. return (wrapped != NULL);
  5397. }
  5398. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5399. if (otherDictionary) {
  5400. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5401. if (_autocreator) {
  5402. GPBAutocreatedDictionaryModified(_autocreator, self);
  5403. }
  5404. }
  5405. }
  5406. - (void)setDouble:(double)value forKey:(uint64_t)key {
  5407. [_dictionary setObject:@(value) forKey:@(key)];
  5408. if (_autocreator) {
  5409. GPBAutocreatedDictionaryModified(_autocreator, self);
  5410. }
  5411. }
  5412. - (void)removeDoubleForKey:(uint64_t)aKey {
  5413. [_dictionary removeObjectForKey:@(aKey)];
  5414. }
  5415. - (void)removeAll {
  5416. [_dictionary removeAllObjects];
  5417. }
  5418. @end
  5419. #pragma mark - UInt64 -> Enum
  5420. @implementation GPBUInt64EnumDictionary {
  5421. @package
  5422. NSMutableDictionary *_dictionary;
  5423. GPBEnumValidationFunc _validationFunc;
  5424. }
  5425. @synthesize validationFunc = _validationFunc;
  5426. - (instancetype)init {
  5427. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  5428. }
  5429. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  5430. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5431. }
  5432. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5433. rawValues:(const int32_t [])rawValues
  5434. forKeys:(const uint64_t [])keys
  5435. count:(NSUInteger)count {
  5436. self = [super init];
  5437. if (self) {
  5438. _dictionary = [[NSMutableDictionary alloc] init];
  5439. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  5440. if (count && rawValues && keys) {
  5441. for (NSUInteger i = 0; i < count; ++i) {
  5442. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  5443. }
  5444. }
  5445. }
  5446. return self;
  5447. }
  5448. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  5449. self = [self initWithValidationFunction:dictionary.validationFunc
  5450. rawValues:NULL
  5451. forKeys:NULL
  5452. count:0];
  5453. if (self) {
  5454. if (dictionary) {
  5455. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5456. }
  5457. }
  5458. return self;
  5459. }
  5460. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5461. capacity:(__unused NSUInteger)numItems {
  5462. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5463. }
  5464. - (void)dealloc {
  5465. NSAssert(!_autocreator,
  5466. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5467. [self class], _autocreator);
  5468. [_dictionary release];
  5469. [super dealloc];
  5470. }
  5471. - (instancetype)copyWithZone:(NSZone *)zone {
  5472. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  5473. }
  5474. - (BOOL)isEqual:(id)other {
  5475. if (self == other) {
  5476. return YES;
  5477. }
  5478. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  5479. return NO;
  5480. }
  5481. GPBUInt64EnumDictionary *otherDictionary = other;
  5482. return [_dictionary isEqual:otherDictionary->_dictionary];
  5483. }
  5484. - (NSUInteger)hash {
  5485. return _dictionary.count;
  5486. }
  5487. - (NSString *)description {
  5488. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5489. }
  5490. - (NSUInteger)count {
  5491. return _dictionary.count;
  5492. }
  5493. - (void)enumerateKeysAndRawValuesUsingBlock:
  5494. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  5495. BOOL stop = NO;
  5496. NSDictionary *internal = _dictionary;
  5497. NSEnumerator *keys = [internal keyEnumerator];
  5498. NSNumber *aKey;
  5499. while ((aKey = [keys nextObject])) {
  5500. NSNumber *aValue = internal[aKey];
  5501. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  5502. if (stop) {
  5503. break;
  5504. }
  5505. }
  5506. }
  5507. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5508. NSDictionary *internal = _dictionary;
  5509. NSUInteger count = internal.count;
  5510. if (count == 0) {
  5511. return 0;
  5512. }
  5513. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5514. GPBDataType keyDataType = field.mapKeyDataType;
  5515. size_t result = 0;
  5516. NSEnumerator *keys = [internal keyEnumerator];
  5517. NSNumber *aKey;
  5518. while ((aKey = [keys nextObject])) {
  5519. NSNumber *aValue = internal[aKey];
  5520. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5521. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5522. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5523. }
  5524. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5525. result += tagSize * count;
  5526. return result;
  5527. }
  5528. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5529. asField:(GPBFieldDescriptor *)field {
  5530. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5531. GPBDataType keyDataType = field.mapKeyDataType;
  5532. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5533. NSDictionary *internal = _dictionary;
  5534. NSEnumerator *keys = [internal keyEnumerator];
  5535. NSNumber *aKey;
  5536. while ((aKey = [keys nextObject])) {
  5537. NSNumber *aValue = internal[aKey];
  5538. [outputStream writeInt32NoTag:tag];
  5539. // Write the size of the message.
  5540. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5541. int32_t unwrappedValue = [aValue intValue];
  5542. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5543. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5544. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5545. // Write the fields.
  5546. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5547. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5548. }
  5549. }
  5550. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  5551. forKey:(GPBGenericValue *)key
  5552. keyDataType:(GPBDataType)keyDataType {
  5553. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  5554. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  5555. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  5556. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  5557. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  5558. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  5559. [outputStream flush];
  5560. [outputStream release];
  5561. return data;
  5562. }
  5563. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5564. forGPBGenericValueKey:(GPBGenericValue *)key {
  5565. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  5566. }
  5567. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5568. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, __unused BOOL *stop) {
  5569. block([NSString stringWithFormat:@"%llu", key], @(value));
  5570. }];
  5571. }
  5572. - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
  5573. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5574. if (wrapped && value) {
  5575. int32_t result = [wrapped intValue];
  5576. if (!_validationFunc(result)) {
  5577. result = kGPBUnrecognizedEnumeratorValue;
  5578. }
  5579. *value = result;
  5580. }
  5581. return (wrapped != NULL);
  5582. }
  5583. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
  5584. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5585. if (wrapped && rawValue) {
  5586. *rawValue = [wrapped intValue];
  5587. }
  5588. return (wrapped != NULL);
  5589. }
  5590. - (void)enumerateKeysAndEnumsUsingBlock:
  5591. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  5592. GPBEnumValidationFunc func = _validationFunc;
  5593. BOOL stop = NO;
  5594. NSEnumerator *keys = [_dictionary keyEnumerator];
  5595. NSNumber *aKey;
  5596. while ((aKey = [keys nextObject])) {
  5597. NSNumber *aValue = _dictionary[aKey];
  5598. int32_t unwrapped = [aValue intValue];
  5599. if (!func(unwrapped)) {
  5600. unwrapped = kGPBUnrecognizedEnumeratorValue;
  5601. }
  5602. block([aKey unsignedLongLongValue], unwrapped, &stop);
  5603. if (stop) {
  5604. break;
  5605. }
  5606. }
  5607. }
  5608. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  5609. if (otherDictionary) {
  5610. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5611. if (_autocreator) {
  5612. GPBAutocreatedDictionaryModified(_autocreator, self);
  5613. }
  5614. }
  5615. }
  5616. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  5617. [_dictionary setObject:@(value) forKey:@(key)];
  5618. if (_autocreator) {
  5619. GPBAutocreatedDictionaryModified(_autocreator, self);
  5620. }
  5621. }
  5622. - (void)removeEnumForKey:(uint64_t)aKey {
  5623. [_dictionary removeObjectForKey:@(aKey)];
  5624. }
  5625. - (void)removeAll {
  5626. [_dictionary removeAllObjects];
  5627. }
  5628. - (void)setEnum:(int32_t)value forKey:(uint64_t)key {
  5629. if (!_validationFunc(value)) {
  5630. [NSException raise:NSInvalidArgumentException
  5631. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  5632. value];
  5633. }
  5634. [_dictionary setObject:@(value) forKey:@(key)];
  5635. if (_autocreator) {
  5636. GPBAutocreatedDictionaryModified(_autocreator, self);
  5637. }
  5638. }
  5639. @end
  5640. #pragma mark - UInt64 -> Object
  5641. @implementation GPBUInt64ObjectDictionary {
  5642. @package
  5643. NSMutableDictionary *_dictionary;
  5644. }
  5645. - (instancetype)init {
  5646. return [self initWithObjects:NULL forKeys:NULL count:0];
  5647. }
  5648. - (instancetype)initWithObjects:(const id [])objects
  5649. forKeys:(const uint64_t [])keys
  5650. count:(NSUInteger)count {
  5651. self = [super init];
  5652. if (self) {
  5653. _dictionary = [[NSMutableDictionary alloc] init];
  5654. if (count && objects && keys) {
  5655. for (NSUInteger i = 0; i < count; ++i) {
  5656. if (!objects[i]) {
  5657. [NSException raise:NSInvalidArgumentException
  5658. format:@"Attempting to add nil object to a Dictionary"];
  5659. }
  5660. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  5661. }
  5662. }
  5663. }
  5664. return self;
  5665. }
  5666. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  5667. self = [self initWithObjects:NULL forKeys:NULL count:0];
  5668. if (self) {
  5669. if (dictionary) {
  5670. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5671. }
  5672. }
  5673. return self;
  5674. }
  5675. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5676. return [self initWithObjects:NULL forKeys:NULL count:0];
  5677. }
  5678. - (void)dealloc {
  5679. NSAssert(!_autocreator,
  5680. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5681. [self class], _autocreator);
  5682. [_dictionary release];
  5683. [super dealloc];
  5684. }
  5685. - (instancetype)copyWithZone:(NSZone *)zone {
  5686. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  5687. }
  5688. - (BOOL)isEqual:(id)other {
  5689. if (self == other) {
  5690. return YES;
  5691. }
  5692. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  5693. return NO;
  5694. }
  5695. GPBUInt64ObjectDictionary *otherDictionary = other;
  5696. return [_dictionary isEqual:otherDictionary->_dictionary];
  5697. }
  5698. - (NSUInteger)hash {
  5699. return _dictionary.count;
  5700. }
  5701. - (NSString *)description {
  5702. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5703. }
  5704. - (NSUInteger)count {
  5705. return _dictionary.count;
  5706. }
  5707. - (void)enumerateKeysAndObjectsUsingBlock:
  5708. (void (NS_NOESCAPE ^)(uint64_t key, id object, BOOL *stop))block {
  5709. BOOL stop = NO;
  5710. NSDictionary *internal = _dictionary;
  5711. NSEnumerator *keys = [internal keyEnumerator];
  5712. NSNumber *aKey;
  5713. while ((aKey = [keys nextObject])) {
  5714. id aObject = internal[aKey];
  5715. block([aKey unsignedLongLongValue], aObject, &stop);
  5716. if (stop) {
  5717. break;
  5718. }
  5719. }
  5720. }
  5721. - (BOOL)isInitialized {
  5722. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  5723. if (!msg.initialized) {
  5724. return NO;
  5725. }
  5726. }
  5727. return YES;
  5728. }
  5729. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  5730. GPBUInt64ObjectDictionary *newDict =
  5731. [[GPBUInt64ObjectDictionary alloc] init];
  5732. NSEnumerator *keys = [_dictionary keyEnumerator];
  5733. id aKey;
  5734. NSMutableDictionary *internalDict = newDict->_dictionary;
  5735. while ((aKey = [keys nextObject])) {
  5736. GPBMessage *msg = _dictionary[aKey];
  5737. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  5738. [internalDict setObject:copiedMsg forKey:aKey];
  5739. [copiedMsg release];
  5740. }
  5741. return newDict;
  5742. }
  5743. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5744. NSDictionary *internal = _dictionary;
  5745. NSUInteger count = internal.count;
  5746. if (count == 0) {
  5747. return 0;
  5748. }
  5749. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5750. GPBDataType keyDataType = field.mapKeyDataType;
  5751. size_t result = 0;
  5752. NSEnumerator *keys = [internal keyEnumerator];
  5753. NSNumber *aKey;
  5754. while ((aKey = [keys nextObject])) {
  5755. id aObject = internal[aKey];
  5756. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5757. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  5758. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5759. }
  5760. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5761. result += tagSize * count;
  5762. return result;
  5763. }
  5764. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5765. asField:(GPBFieldDescriptor *)field {
  5766. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5767. GPBDataType keyDataType = field.mapKeyDataType;
  5768. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5769. NSDictionary *internal = _dictionary;
  5770. NSEnumerator *keys = [internal keyEnumerator];
  5771. NSNumber *aKey;
  5772. while ((aKey = [keys nextObject])) {
  5773. id aObject = internal[aKey];
  5774. [outputStream writeInt32NoTag:tag];
  5775. // Write the size of the message.
  5776. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5777. id unwrappedValue = aObject;
  5778. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5779. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5780. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5781. // Write the fields.
  5782. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5783. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5784. }
  5785. }
  5786. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5787. forGPBGenericValueKey:(GPBGenericValue *)key {
  5788. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  5789. }
  5790. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5791. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, __unused BOOL *stop) {
  5792. block([NSString stringWithFormat:@"%llu", key], object);
  5793. }];
  5794. }
  5795. - (id)objectForKey:(uint64_t)key {
  5796. id result = [_dictionary objectForKey:@(key)];
  5797. return result;
  5798. }
  5799. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  5800. if (otherDictionary) {
  5801. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5802. if (_autocreator) {
  5803. GPBAutocreatedDictionaryModified(_autocreator, self);
  5804. }
  5805. }
  5806. }
  5807. - (void)setObject:(id)object forKey:(uint64_t)key {
  5808. if (!object) {
  5809. [NSException raise:NSInvalidArgumentException
  5810. format:@"Attempting to add nil object to a Dictionary"];
  5811. }
  5812. [_dictionary setObject:object forKey:@(key)];
  5813. if (_autocreator) {
  5814. GPBAutocreatedDictionaryModified(_autocreator, self);
  5815. }
  5816. }
  5817. - (void)removeObjectForKey:(uint64_t)aKey {
  5818. [_dictionary removeObjectForKey:@(aKey)];
  5819. }
  5820. - (void)removeAll {
  5821. [_dictionary removeAllObjects];
  5822. }
  5823. @end
  5824. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  5825. // This block of code is generated, do not edit it directly.
  5826. #pragma mark - Int64 -> UInt32
  5827. @implementation GPBInt64UInt32Dictionary {
  5828. @package
  5829. NSMutableDictionary *_dictionary;
  5830. }
  5831. - (instancetype)init {
  5832. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5833. }
  5834. - (instancetype)initWithUInt32s:(const uint32_t [])values
  5835. forKeys:(const int64_t [])keys
  5836. count:(NSUInteger)count {
  5837. self = [super init];
  5838. if (self) {
  5839. _dictionary = [[NSMutableDictionary alloc] init];
  5840. if (count && values && keys) {
  5841. for (NSUInteger i = 0; i < count; ++i) {
  5842. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5843. }
  5844. }
  5845. }
  5846. return self;
  5847. }
  5848. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  5849. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  5850. if (self) {
  5851. if (dictionary) {
  5852. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5853. }
  5854. }
  5855. return self;
  5856. }
  5857. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  5858. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5859. }
  5860. - (void)dealloc {
  5861. NSAssert(!_autocreator,
  5862. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5863. [self class], _autocreator);
  5864. [_dictionary release];
  5865. [super dealloc];
  5866. }
  5867. - (instancetype)copyWithZone:(NSZone *)zone {
  5868. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  5869. }
  5870. - (BOOL)isEqual:(id)other {
  5871. if (self == other) {
  5872. return YES;
  5873. }
  5874. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  5875. return NO;
  5876. }
  5877. GPBInt64UInt32Dictionary *otherDictionary = other;
  5878. return [_dictionary isEqual:otherDictionary->_dictionary];
  5879. }
  5880. - (NSUInteger)hash {
  5881. return _dictionary.count;
  5882. }
  5883. - (NSString *)description {
  5884. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5885. }
  5886. - (NSUInteger)count {
  5887. return _dictionary.count;
  5888. }
  5889. - (void)enumerateKeysAndUInt32sUsingBlock:
  5890. (void (NS_NOESCAPE ^)(int64_t key, uint32_t value, BOOL *stop))block {
  5891. BOOL stop = NO;
  5892. NSDictionary *internal = _dictionary;
  5893. NSEnumerator *keys = [internal keyEnumerator];
  5894. NSNumber *aKey;
  5895. while ((aKey = [keys nextObject])) {
  5896. NSNumber *aValue = internal[aKey];
  5897. block([aKey longLongValue], [aValue unsignedIntValue], &stop);
  5898. if (stop) {
  5899. break;
  5900. }
  5901. }
  5902. }
  5903. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5904. NSDictionary *internal = _dictionary;
  5905. NSUInteger count = internal.count;
  5906. if (count == 0) {
  5907. return 0;
  5908. }
  5909. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5910. GPBDataType keyDataType = field.mapKeyDataType;
  5911. size_t result = 0;
  5912. NSEnumerator *keys = [internal keyEnumerator];
  5913. NSNumber *aKey;
  5914. while ((aKey = [keys nextObject])) {
  5915. NSNumber *aValue = internal[aKey];
  5916. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  5917. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  5918. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5919. }
  5920. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5921. result += tagSize * count;
  5922. return result;
  5923. }
  5924. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5925. asField:(GPBFieldDescriptor *)field {
  5926. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5927. GPBDataType keyDataType = field.mapKeyDataType;
  5928. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5929. NSDictionary *internal = _dictionary;
  5930. NSEnumerator *keys = [internal keyEnumerator];
  5931. NSNumber *aKey;
  5932. while ((aKey = [keys nextObject])) {
  5933. NSNumber *aValue = internal[aKey];
  5934. [outputStream writeInt32NoTag:tag];
  5935. // Write the size of the message.
  5936. int64_t unwrappedKey = [aKey longLongValue];
  5937. uint32_t unwrappedValue = [aValue unsignedIntValue];
  5938. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5939. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5940. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5941. // Write the fields.
  5942. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5943. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5944. }
  5945. }
  5946. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5947. forGPBGenericValueKey:(GPBGenericValue *)key {
  5948. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  5949. }
  5950. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5951. [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, __unused BOOL *stop) {
  5952. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  5953. }];
  5954. }
  5955. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
  5956. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5957. if (wrapped && value) {
  5958. *value = [wrapped unsignedIntValue];
  5959. }
  5960. return (wrapped != NULL);
  5961. }
  5962. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  5963. if (otherDictionary) {
  5964. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5965. if (_autocreator) {
  5966. GPBAutocreatedDictionaryModified(_autocreator, self);
  5967. }
  5968. }
  5969. }
  5970. - (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
  5971. [_dictionary setObject:@(value) forKey:@(key)];
  5972. if (_autocreator) {
  5973. GPBAutocreatedDictionaryModified(_autocreator, self);
  5974. }
  5975. }
  5976. - (void)removeUInt32ForKey:(int64_t)aKey {
  5977. [_dictionary removeObjectForKey:@(aKey)];
  5978. }
  5979. - (void)removeAll {
  5980. [_dictionary removeAllObjects];
  5981. }
  5982. @end
  5983. #pragma mark - Int64 -> Int32
  5984. @implementation GPBInt64Int32Dictionary {
  5985. @package
  5986. NSMutableDictionary *_dictionary;
  5987. }
  5988. - (instancetype)init {
  5989. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5990. }
  5991. - (instancetype)initWithInt32s:(const int32_t [])values
  5992. forKeys:(const int64_t [])keys
  5993. count:(NSUInteger)count {
  5994. self = [super init];
  5995. if (self) {
  5996. _dictionary = [[NSMutableDictionary alloc] init];
  5997. if (count && values && keys) {
  5998. for (NSUInteger i = 0; i < count; ++i) {
  5999. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6000. }
  6001. }
  6002. }
  6003. return self;
  6004. }
  6005. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6006. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  6007. if (self) {
  6008. if (dictionary) {
  6009. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6010. }
  6011. }
  6012. return self;
  6013. }
  6014. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6015. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6016. }
  6017. - (void)dealloc {
  6018. NSAssert(!_autocreator,
  6019. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6020. [self class], _autocreator);
  6021. [_dictionary release];
  6022. [super dealloc];
  6023. }
  6024. - (instancetype)copyWithZone:(NSZone *)zone {
  6025. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6026. }
  6027. - (BOOL)isEqual:(id)other {
  6028. if (self == other) {
  6029. return YES;
  6030. }
  6031. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6032. return NO;
  6033. }
  6034. GPBInt64Int32Dictionary *otherDictionary = other;
  6035. return [_dictionary isEqual:otherDictionary->_dictionary];
  6036. }
  6037. - (NSUInteger)hash {
  6038. return _dictionary.count;
  6039. }
  6040. - (NSString *)description {
  6041. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6042. }
  6043. - (NSUInteger)count {
  6044. return _dictionary.count;
  6045. }
  6046. - (void)enumerateKeysAndInt32sUsingBlock:
  6047. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  6048. BOOL stop = NO;
  6049. NSDictionary *internal = _dictionary;
  6050. NSEnumerator *keys = [internal keyEnumerator];
  6051. NSNumber *aKey;
  6052. while ((aKey = [keys nextObject])) {
  6053. NSNumber *aValue = internal[aKey];
  6054. block([aKey longLongValue], [aValue intValue], &stop);
  6055. if (stop) {
  6056. break;
  6057. }
  6058. }
  6059. }
  6060. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6061. NSDictionary *internal = _dictionary;
  6062. NSUInteger count = internal.count;
  6063. if (count == 0) {
  6064. return 0;
  6065. }
  6066. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6067. GPBDataType keyDataType = field.mapKeyDataType;
  6068. size_t result = 0;
  6069. NSEnumerator *keys = [internal keyEnumerator];
  6070. NSNumber *aKey;
  6071. while ((aKey = [keys nextObject])) {
  6072. NSNumber *aValue = internal[aKey];
  6073. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6074. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6075. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6076. }
  6077. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6078. result += tagSize * count;
  6079. return result;
  6080. }
  6081. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6082. asField:(GPBFieldDescriptor *)field {
  6083. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6084. GPBDataType keyDataType = field.mapKeyDataType;
  6085. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6086. NSDictionary *internal = _dictionary;
  6087. NSEnumerator *keys = [internal keyEnumerator];
  6088. NSNumber *aKey;
  6089. while ((aKey = [keys nextObject])) {
  6090. NSNumber *aValue = internal[aKey];
  6091. [outputStream writeInt32NoTag:tag];
  6092. // Write the size of the message.
  6093. int64_t unwrappedKey = [aKey longLongValue];
  6094. int32_t unwrappedValue = [aValue intValue];
  6095. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6096. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6097. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6098. // Write the fields.
  6099. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6100. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6101. }
  6102. }
  6103. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6104. forGPBGenericValueKey:(GPBGenericValue *)key {
  6105. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6106. }
  6107. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6108. [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, __unused BOOL *stop) {
  6109. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6110. }];
  6111. }
  6112. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
  6113. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6114. if (wrapped && value) {
  6115. *value = [wrapped intValue];
  6116. }
  6117. return (wrapped != NULL);
  6118. }
  6119. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6120. if (otherDictionary) {
  6121. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6122. if (_autocreator) {
  6123. GPBAutocreatedDictionaryModified(_autocreator, self);
  6124. }
  6125. }
  6126. }
  6127. - (void)setInt32:(int32_t)value forKey:(int64_t)key {
  6128. [_dictionary setObject:@(value) forKey:@(key)];
  6129. if (_autocreator) {
  6130. GPBAutocreatedDictionaryModified(_autocreator, self);
  6131. }
  6132. }
  6133. - (void)removeInt32ForKey:(int64_t)aKey {
  6134. [_dictionary removeObjectForKey:@(aKey)];
  6135. }
  6136. - (void)removeAll {
  6137. [_dictionary removeAllObjects];
  6138. }
  6139. @end
  6140. #pragma mark - Int64 -> UInt64
  6141. @implementation GPBInt64UInt64Dictionary {
  6142. @package
  6143. NSMutableDictionary *_dictionary;
  6144. }
  6145. - (instancetype)init {
  6146. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6147. }
  6148. - (instancetype)initWithUInt64s:(const uint64_t [])values
  6149. forKeys:(const int64_t [])keys
  6150. count:(NSUInteger)count {
  6151. self = [super init];
  6152. if (self) {
  6153. _dictionary = [[NSMutableDictionary alloc] init];
  6154. if (count && values && keys) {
  6155. for (NSUInteger i = 0; i < count; ++i) {
  6156. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6157. }
  6158. }
  6159. }
  6160. return self;
  6161. }
  6162. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6163. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  6164. if (self) {
  6165. if (dictionary) {
  6166. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6167. }
  6168. }
  6169. return self;
  6170. }
  6171. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6172. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6173. }
  6174. - (void)dealloc {
  6175. NSAssert(!_autocreator,
  6176. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6177. [self class], _autocreator);
  6178. [_dictionary release];
  6179. [super dealloc];
  6180. }
  6181. - (instancetype)copyWithZone:(NSZone *)zone {
  6182. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6183. }
  6184. - (BOOL)isEqual:(id)other {
  6185. if (self == other) {
  6186. return YES;
  6187. }
  6188. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6189. return NO;
  6190. }
  6191. GPBInt64UInt64Dictionary *otherDictionary = other;
  6192. return [_dictionary isEqual:otherDictionary->_dictionary];
  6193. }
  6194. - (NSUInteger)hash {
  6195. return _dictionary.count;
  6196. }
  6197. - (NSString *)description {
  6198. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6199. }
  6200. - (NSUInteger)count {
  6201. return _dictionary.count;
  6202. }
  6203. - (void)enumerateKeysAndUInt64sUsingBlock:
  6204. (void (NS_NOESCAPE ^)(int64_t key, uint64_t value, BOOL *stop))block {
  6205. BOOL stop = NO;
  6206. NSDictionary *internal = _dictionary;
  6207. NSEnumerator *keys = [internal keyEnumerator];
  6208. NSNumber *aKey;
  6209. while ((aKey = [keys nextObject])) {
  6210. NSNumber *aValue = internal[aKey];
  6211. block([aKey longLongValue], [aValue unsignedLongLongValue], &stop);
  6212. if (stop) {
  6213. break;
  6214. }
  6215. }
  6216. }
  6217. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6218. NSDictionary *internal = _dictionary;
  6219. NSUInteger count = internal.count;
  6220. if (count == 0) {
  6221. return 0;
  6222. }
  6223. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6224. GPBDataType keyDataType = field.mapKeyDataType;
  6225. size_t result = 0;
  6226. NSEnumerator *keys = [internal keyEnumerator];
  6227. NSNumber *aKey;
  6228. while ((aKey = [keys nextObject])) {
  6229. NSNumber *aValue = internal[aKey];
  6230. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6231. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6232. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6233. }
  6234. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6235. result += tagSize * count;
  6236. return result;
  6237. }
  6238. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6239. asField:(GPBFieldDescriptor *)field {
  6240. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6241. GPBDataType keyDataType = field.mapKeyDataType;
  6242. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6243. NSDictionary *internal = _dictionary;
  6244. NSEnumerator *keys = [internal keyEnumerator];
  6245. NSNumber *aKey;
  6246. while ((aKey = [keys nextObject])) {
  6247. NSNumber *aValue = internal[aKey];
  6248. [outputStream writeInt32NoTag:tag];
  6249. // Write the size of the message.
  6250. int64_t unwrappedKey = [aKey longLongValue];
  6251. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  6252. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6253. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6254. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6255. // Write the fields.
  6256. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6257. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6258. }
  6259. }
  6260. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6261. forGPBGenericValueKey:(GPBGenericValue *)key {
  6262. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6263. }
  6264. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6265. [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, __unused BOOL *stop) {
  6266. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6267. }];
  6268. }
  6269. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
  6270. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6271. if (wrapped && value) {
  6272. *value = [wrapped unsignedLongLongValue];
  6273. }
  6274. return (wrapped != NULL);
  6275. }
  6276. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6277. if (otherDictionary) {
  6278. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6279. if (_autocreator) {
  6280. GPBAutocreatedDictionaryModified(_autocreator, self);
  6281. }
  6282. }
  6283. }
  6284. - (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
  6285. [_dictionary setObject:@(value) forKey:@(key)];
  6286. if (_autocreator) {
  6287. GPBAutocreatedDictionaryModified(_autocreator, self);
  6288. }
  6289. }
  6290. - (void)removeUInt64ForKey:(int64_t)aKey {
  6291. [_dictionary removeObjectForKey:@(aKey)];
  6292. }
  6293. - (void)removeAll {
  6294. [_dictionary removeAllObjects];
  6295. }
  6296. @end
  6297. #pragma mark - Int64 -> Int64
  6298. @implementation GPBInt64Int64Dictionary {
  6299. @package
  6300. NSMutableDictionary *_dictionary;
  6301. }
  6302. - (instancetype)init {
  6303. return [self initWithInt64s:NULL forKeys:NULL count:0];
  6304. }
  6305. - (instancetype)initWithInt64s:(const int64_t [])values
  6306. forKeys:(const int64_t [])keys
  6307. count:(NSUInteger)count {
  6308. self = [super init];
  6309. if (self) {
  6310. _dictionary = [[NSMutableDictionary alloc] init];
  6311. if (count && values && keys) {
  6312. for (NSUInteger i = 0; i < count; ++i) {
  6313. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6314. }
  6315. }
  6316. }
  6317. return self;
  6318. }
  6319. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6320. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  6321. if (self) {
  6322. if (dictionary) {
  6323. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6324. }
  6325. }
  6326. return self;
  6327. }
  6328. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6329. return [self initWithInt64s:NULL forKeys:NULL count:0];
  6330. }
  6331. - (void)dealloc {
  6332. NSAssert(!_autocreator,
  6333. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6334. [self class], _autocreator);
  6335. [_dictionary release];
  6336. [super dealloc];
  6337. }
  6338. - (instancetype)copyWithZone:(NSZone *)zone {
  6339. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  6340. }
  6341. - (BOOL)isEqual:(id)other {
  6342. if (self == other) {
  6343. return YES;
  6344. }
  6345. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  6346. return NO;
  6347. }
  6348. GPBInt64Int64Dictionary *otherDictionary = other;
  6349. return [_dictionary isEqual:otherDictionary->_dictionary];
  6350. }
  6351. - (NSUInteger)hash {
  6352. return _dictionary.count;
  6353. }
  6354. - (NSString *)description {
  6355. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6356. }
  6357. - (NSUInteger)count {
  6358. return _dictionary.count;
  6359. }
  6360. - (void)enumerateKeysAndInt64sUsingBlock:
  6361. (void (NS_NOESCAPE ^)(int64_t key, int64_t value, BOOL *stop))block {
  6362. BOOL stop = NO;
  6363. NSDictionary *internal = _dictionary;
  6364. NSEnumerator *keys = [internal keyEnumerator];
  6365. NSNumber *aKey;
  6366. while ((aKey = [keys nextObject])) {
  6367. NSNumber *aValue = internal[aKey];
  6368. block([aKey longLongValue], [aValue longLongValue], &stop);
  6369. if (stop) {
  6370. break;
  6371. }
  6372. }
  6373. }
  6374. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6375. NSDictionary *internal = _dictionary;
  6376. NSUInteger count = internal.count;
  6377. if (count == 0) {
  6378. return 0;
  6379. }
  6380. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6381. GPBDataType keyDataType = field.mapKeyDataType;
  6382. size_t result = 0;
  6383. NSEnumerator *keys = [internal keyEnumerator];
  6384. NSNumber *aKey;
  6385. while ((aKey = [keys nextObject])) {
  6386. NSNumber *aValue = internal[aKey];
  6387. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6388. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  6389. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6390. }
  6391. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6392. result += tagSize * count;
  6393. return result;
  6394. }
  6395. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6396. asField:(GPBFieldDescriptor *)field {
  6397. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6398. GPBDataType keyDataType = field.mapKeyDataType;
  6399. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6400. NSDictionary *internal = _dictionary;
  6401. NSEnumerator *keys = [internal keyEnumerator];
  6402. NSNumber *aKey;
  6403. while ((aKey = [keys nextObject])) {
  6404. NSNumber *aValue = internal[aKey];
  6405. [outputStream writeInt32NoTag:tag];
  6406. // Write the size of the message.
  6407. int64_t unwrappedKey = [aKey longLongValue];
  6408. int64_t unwrappedValue = [aValue longLongValue];
  6409. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6410. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6411. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6412. // Write the fields.
  6413. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6414. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6415. }
  6416. }
  6417. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6418. forGPBGenericValueKey:(GPBGenericValue *)key {
  6419. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  6420. }
  6421. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6422. [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, __unused BOOL *stop) {
  6423. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  6424. }];
  6425. }
  6426. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
  6427. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6428. if (wrapped && value) {
  6429. *value = [wrapped longLongValue];
  6430. }
  6431. return (wrapped != NULL);
  6432. }
  6433. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  6434. if (otherDictionary) {
  6435. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6436. if (_autocreator) {
  6437. GPBAutocreatedDictionaryModified(_autocreator, self);
  6438. }
  6439. }
  6440. }
  6441. - (void)setInt64:(int64_t)value forKey:(int64_t)key {
  6442. [_dictionary setObject:@(value) forKey:@(key)];
  6443. if (_autocreator) {
  6444. GPBAutocreatedDictionaryModified(_autocreator, self);
  6445. }
  6446. }
  6447. - (void)removeInt64ForKey:(int64_t)aKey {
  6448. [_dictionary removeObjectForKey:@(aKey)];
  6449. }
  6450. - (void)removeAll {
  6451. [_dictionary removeAllObjects];
  6452. }
  6453. @end
  6454. #pragma mark - Int64 -> Bool
  6455. @implementation GPBInt64BoolDictionary {
  6456. @package
  6457. NSMutableDictionary *_dictionary;
  6458. }
  6459. - (instancetype)init {
  6460. return [self initWithBools:NULL forKeys:NULL count:0];
  6461. }
  6462. - (instancetype)initWithBools:(const BOOL [])values
  6463. forKeys:(const int64_t [])keys
  6464. count:(NSUInteger)count {
  6465. self = [super init];
  6466. if (self) {
  6467. _dictionary = [[NSMutableDictionary alloc] init];
  6468. if (count && values && keys) {
  6469. for (NSUInteger i = 0; i < count; ++i) {
  6470. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6471. }
  6472. }
  6473. }
  6474. return self;
  6475. }
  6476. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  6477. self = [self initWithBools:NULL forKeys:NULL count:0];
  6478. if (self) {
  6479. if (dictionary) {
  6480. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6481. }
  6482. }
  6483. return self;
  6484. }
  6485. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6486. return [self initWithBools:NULL forKeys:NULL count:0];
  6487. }
  6488. - (void)dealloc {
  6489. NSAssert(!_autocreator,
  6490. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6491. [self class], _autocreator);
  6492. [_dictionary release];
  6493. [super dealloc];
  6494. }
  6495. - (instancetype)copyWithZone:(NSZone *)zone {
  6496. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  6497. }
  6498. - (BOOL)isEqual:(id)other {
  6499. if (self == other) {
  6500. return YES;
  6501. }
  6502. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  6503. return NO;
  6504. }
  6505. GPBInt64BoolDictionary *otherDictionary = other;
  6506. return [_dictionary isEqual:otherDictionary->_dictionary];
  6507. }
  6508. - (NSUInteger)hash {
  6509. return _dictionary.count;
  6510. }
  6511. - (NSString *)description {
  6512. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6513. }
  6514. - (NSUInteger)count {
  6515. return _dictionary.count;
  6516. }
  6517. - (void)enumerateKeysAndBoolsUsingBlock:
  6518. (void (NS_NOESCAPE ^)(int64_t key, BOOL value, BOOL *stop))block {
  6519. BOOL stop = NO;
  6520. NSDictionary *internal = _dictionary;
  6521. NSEnumerator *keys = [internal keyEnumerator];
  6522. NSNumber *aKey;
  6523. while ((aKey = [keys nextObject])) {
  6524. NSNumber *aValue = internal[aKey];
  6525. block([aKey longLongValue], [aValue boolValue], &stop);
  6526. if (stop) {
  6527. break;
  6528. }
  6529. }
  6530. }
  6531. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6532. NSDictionary *internal = _dictionary;
  6533. NSUInteger count = internal.count;
  6534. if (count == 0) {
  6535. return 0;
  6536. }
  6537. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6538. GPBDataType keyDataType = field.mapKeyDataType;
  6539. size_t result = 0;
  6540. NSEnumerator *keys = [internal keyEnumerator];
  6541. NSNumber *aKey;
  6542. while ((aKey = [keys nextObject])) {
  6543. NSNumber *aValue = internal[aKey];
  6544. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6545. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  6546. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6547. }
  6548. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6549. result += tagSize * count;
  6550. return result;
  6551. }
  6552. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6553. asField:(GPBFieldDescriptor *)field {
  6554. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6555. GPBDataType keyDataType = field.mapKeyDataType;
  6556. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6557. NSDictionary *internal = _dictionary;
  6558. NSEnumerator *keys = [internal keyEnumerator];
  6559. NSNumber *aKey;
  6560. while ((aKey = [keys nextObject])) {
  6561. NSNumber *aValue = internal[aKey];
  6562. [outputStream writeInt32NoTag:tag];
  6563. // Write the size of the message.
  6564. int64_t unwrappedKey = [aKey longLongValue];
  6565. BOOL unwrappedValue = [aValue boolValue];
  6566. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6567. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6568. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6569. // Write the fields.
  6570. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6571. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6572. }
  6573. }
  6574. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6575. forGPBGenericValueKey:(GPBGenericValue *)key {
  6576. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  6577. }
  6578. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6579. [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, __unused BOOL *stop) {
  6580. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  6581. }];
  6582. }
  6583. - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
  6584. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6585. if (wrapped && value) {
  6586. *value = [wrapped boolValue];
  6587. }
  6588. return (wrapped != NULL);
  6589. }
  6590. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  6591. if (otherDictionary) {
  6592. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6593. if (_autocreator) {
  6594. GPBAutocreatedDictionaryModified(_autocreator, self);
  6595. }
  6596. }
  6597. }
  6598. - (void)setBool:(BOOL)value forKey:(int64_t)key {
  6599. [_dictionary setObject:@(value) forKey:@(key)];
  6600. if (_autocreator) {
  6601. GPBAutocreatedDictionaryModified(_autocreator, self);
  6602. }
  6603. }
  6604. - (void)removeBoolForKey:(int64_t)aKey {
  6605. [_dictionary removeObjectForKey:@(aKey)];
  6606. }
  6607. - (void)removeAll {
  6608. [_dictionary removeAllObjects];
  6609. }
  6610. @end
  6611. #pragma mark - Int64 -> Float
  6612. @implementation GPBInt64FloatDictionary {
  6613. @package
  6614. NSMutableDictionary *_dictionary;
  6615. }
  6616. - (instancetype)init {
  6617. return [self initWithFloats:NULL forKeys:NULL count:0];
  6618. }
  6619. - (instancetype)initWithFloats:(const float [])values
  6620. forKeys:(const int64_t [])keys
  6621. count:(NSUInteger)count {
  6622. self = [super init];
  6623. if (self) {
  6624. _dictionary = [[NSMutableDictionary alloc] init];
  6625. if (count && values && keys) {
  6626. for (NSUInteger i = 0; i < count; ++i) {
  6627. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6628. }
  6629. }
  6630. }
  6631. return self;
  6632. }
  6633. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  6634. self = [self initWithFloats:NULL forKeys:NULL count:0];
  6635. if (self) {
  6636. if (dictionary) {
  6637. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6638. }
  6639. }
  6640. return self;
  6641. }
  6642. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6643. return [self initWithFloats:NULL forKeys:NULL count:0];
  6644. }
  6645. - (void)dealloc {
  6646. NSAssert(!_autocreator,
  6647. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6648. [self class], _autocreator);
  6649. [_dictionary release];
  6650. [super dealloc];
  6651. }
  6652. - (instancetype)copyWithZone:(NSZone *)zone {
  6653. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  6654. }
  6655. - (BOOL)isEqual:(id)other {
  6656. if (self == other) {
  6657. return YES;
  6658. }
  6659. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  6660. return NO;
  6661. }
  6662. GPBInt64FloatDictionary *otherDictionary = other;
  6663. return [_dictionary isEqual:otherDictionary->_dictionary];
  6664. }
  6665. - (NSUInteger)hash {
  6666. return _dictionary.count;
  6667. }
  6668. - (NSString *)description {
  6669. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6670. }
  6671. - (NSUInteger)count {
  6672. return _dictionary.count;
  6673. }
  6674. - (void)enumerateKeysAndFloatsUsingBlock:
  6675. (void (NS_NOESCAPE ^)(int64_t key, float value, BOOL *stop))block {
  6676. BOOL stop = NO;
  6677. NSDictionary *internal = _dictionary;
  6678. NSEnumerator *keys = [internal keyEnumerator];
  6679. NSNumber *aKey;
  6680. while ((aKey = [keys nextObject])) {
  6681. NSNumber *aValue = internal[aKey];
  6682. block([aKey longLongValue], [aValue floatValue], &stop);
  6683. if (stop) {
  6684. break;
  6685. }
  6686. }
  6687. }
  6688. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6689. NSDictionary *internal = _dictionary;
  6690. NSUInteger count = internal.count;
  6691. if (count == 0) {
  6692. return 0;
  6693. }
  6694. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6695. GPBDataType keyDataType = field.mapKeyDataType;
  6696. size_t result = 0;
  6697. NSEnumerator *keys = [internal keyEnumerator];
  6698. NSNumber *aKey;
  6699. while ((aKey = [keys nextObject])) {
  6700. NSNumber *aValue = internal[aKey];
  6701. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6702. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  6703. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6704. }
  6705. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6706. result += tagSize * count;
  6707. return result;
  6708. }
  6709. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6710. asField:(GPBFieldDescriptor *)field {
  6711. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6712. GPBDataType keyDataType = field.mapKeyDataType;
  6713. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6714. NSDictionary *internal = _dictionary;
  6715. NSEnumerator *keys = [internal keyEnumerator];
  6716. NSNumber *aKey;
  6717. while ((aKey = [keys nextObject])) {
  6718. NSNumber *aValue = internal[aKey];
  6719. [outputStream writeInt32NoTag:tag];
  6720. // Write the size of the message.
  6721. int64_t unwrappedKey = [aKey longLongValue];
  6722. float unwrappedValue = [aValue floatValue];
  6723. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6724. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6725. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6726. // Write the fields.
  6727. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6728. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6729. }
  6730. }
  6731. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6732. forGPBGenericValueKey:(GPBGenericValue *)key {
  6733. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  6734. }
  6735. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6736. [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, __unused BOOL *stop) {
  6737. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  6738. }];
  6739. }
  6740. - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
  6741. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6742. if (wrapped && value) {
  6743. *value = [wrapped floatValue];
  6744. }
  6745. return (wrapped != NULL);
  6746. }
  6747. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  6748. if (otherDictionary) {
  6749. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6750. if (_autocreator) {
  6751. GPBAutocreatedDictionaryModified(_autocreator, self);
  6752. }
  6753. }
  6754. }
  6755. - (void)setFloat:(float)value forKey:(int64_t)key {
  6756. [_dictionary setObject:@(value) forKey:@(key)];
  6757. if (_autocreator) {
  6758. GPBAutocreatedDictionaryModified(_autocreator, self);
  6759. }
  6760. }
  6761. - (void)removeFloatForKey:(int64_t)aKey {
  6762. [_dictionary removeObjectForKey:@(aKey)];
  6763. }
  6764. - (void)removeAll {
  6765. [_dictionary removeAllObjects];
  6766. }
  6767. @end
  6768. #pragma mark - Int64 -> Double
  6769. @implementation GPBInt64DoubleDictionary {
  6770. @package
  6771. NSMutableDictionary *_dictionary;
  6772. }
  6773. - (instancetype)init {
  6774. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6775. }
  6776. - (instancetype)initWithDoubles:(const double [])values
  6777. forKeys:(const int64_t [])keys
  6778. count:(NSUInteger)count {
  6779. self = [super init];
  6780. if (self) {
  6781. _dictionary = [[NSMutableDictionary alloc] init];
  6782. if (count && values && keys) {
  6783. for (NSUInteger i = 0; i < count; ++i) {
  6784. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6785. }
  6786. }
  6787. }
  6788. return self;
  6789. }
  6790. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  6791. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  6792. if (self) {
  6793. if (dictionary) {
  6794. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6795. }
  6796. }
  6797. return self;
  6798. }
  6799. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  6800. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6801. }
  6802. - (void)dealloc {
  6803. NSAssert(!_autocreator,
  6804. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6805. [self class], _autocreator);
  6806. [_dictionary release];
  6807. [super dealloc];
  6808. }
  6809. - (instancetype)copyWithZone:(NSZone *)zone {
  6810. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6811. }
  6812. - (BOOL)isEqual:(id)other {
  6813. if (self == other) {
  6814. return YES;
  6815. }
  6816. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  6817. return NO;
  6818. }
  6819. GPBInt64DoubleDictionary *otherDictionary = other;
  6820. return [_dictionary isEqual:otherDictionary->_dictionary];
  6821. }
  6822. - (NSUInteger)hash {
  6823. return _dictionary.count;
  6824. }
  6825. - (NSString *)description {
  6826. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6827. }
  6828. - (NSUInteger)count {
  6829. return _dictionary.count;
  6830. }
  6831. - (void)enumerateKeysAndDoublesUsingBlock:
  6832. (void (NS_NOESCAPE ^)(int64_t key, double value, BOOL *stop))block {
  6833. BOOL stop = NO;
  6834. NSDictionary *internal = _dictionary;
  6835. NSEnumerator *keys = [internal keyEnumerator];
  6836. NSNumber *aKey;
  6837. while ((aKey = [keys nextObject])) {
  6838. NSNumber *aValue = internal[aKey];
  6839. block([aKey longLongValue], [aValue doubleValue], &stop);
  6840. if (stop) {
  6841. break;
  6842. }
  6843. }
  6844. }
  6845. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6846. NSDictionary *internal = _dictionary;
  6847. NSUInteger count = internal.count;
  6848. if (count == 0) {
  6849. return 0;
  6850. }
  6851. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6852. GPBDataType keyDataType = field.mapKeyDataType;
  6853. size_t result = 0;
  6854. NSEnumerator *keys = [internal keyEnumerator];
  6855. NSNumber *aKey;
  6856. while ((aKey = [keys nextObject])) {
  6857. NSNumber *aValue = internal[aKey];
  6858. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6859. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6860. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6861. }
  6862. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6863. result += tagSize * count;
  6864. return result;
  6865. }
  6866. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6867. asField:(GPBFieldDescriptor *)field {
  6868. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6869. GPBDataType keyDataType = field.mapKeyDataType;
  6870. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6871. NSDictionary *internal = _dictionary;
  6872. NSEnumerator *keys = [internal keyEnumerator];
  6873. NSNumber *aKey;
  6874. while ((aKey = [keys nextObject])) {
  6875. NSNumber *aValue = internal[aKey];
  6876. [outputStream writeInt32NoTag:tag];
  6877. // Write the size of the message.
  6878. int64_t unwrappedKey = [aKey longLongValue];
  6879. double unwrappedValue = [aValue doubleValue];
  6880. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6881. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6882. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6883. // Write the fields.
  6884. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6885. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6886. }
  6887. }
  6888. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6889. forGPBGenericValueKey:(GPBGenericValue *)key {
  6890. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  6891. }
  6892. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6893. [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, __unused BOOL *stop) {
  6894. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6895. }];
  6896. }
  6897. - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
  6898. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6899. if (wrapped && value) {
  6900. *value = [wrapped doubleValue];
  6901. }
  6902. return (wrapped != NULL);
  6903. }
  6904. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  6905. if (otherDictionary) {
  6906. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6907. if (_autocreator) {
  6908. GPBAutocreatedDictionaryModified(_autocreator, self);
  6909. }
  6910. }
  6911. }
  6912. - (void)setDouble:(double)value forKey:(int64_t)key {
  6913. [_dictionary setObject:@(value) forKey:@(key)];
  6914. if (_autocreator) {
  6915. GPBAutocreatedDictionaryModified(_autocreator, self);
  6916. }
  6917. }
  6918. - (void)removeDoubleForKey:(int64_t)aKey {
  6919. [_dictionary removeObjectForKey:@(aKey)];
  6920. }
  6921. - (void)removeAll {
  6922. [_dictionary removeAllObjects];
  6923. }
  6924. @end
  6925. #pragma mark - Int64 -> Enum
  6926. @implementation GPBInt64EnumDictionary {
  6927. @package
  6928. NSMutableDictionary *_dictionary;
  6929. GPBEnumValidationFunc _validationFunc;
  6930. }
  6931. @synthesize validationFunc = _validationFunc;
  6932. - (instancetype)init {
  6933. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6934. }
  6935. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6936. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6937. }
  6938. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6939. rawValues:(const int32_t [])rawValues
  6940. forKeys:(const int64_t [])keys
  6941. count:(NSUInteger)count {
  6942. self = [super init];
  6943. if (self) {
  6944. _dictionary = [[NSMutableDictionary alloc] init];
  6945. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6946. if (count && rawValues && keys) {
  6947. for (NSUInteger i = 0; i < count; ++i) {
  6948. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6949. }
  6950. }
  6951. }
  6952. return self;
  6953. }
  6954. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  6955. self = [self initWithValidationFunction:dictionary.validationFunc
  6956. rawValues:NULL
  6957. forKeys:NULL
  6958. count:0];
  6959. if (self) {
  6960. if (dictionary) {
  6961. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6962. }
  6963. }
  6964. return self;
  6965. }
  6966. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6967. capacity:(__unused NSUInteger)numItems {
  6968. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6969. }
  6970. - (void)dealloc {
  6971. NSAssert(!_autocreator,
  6972. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6973. [self class], _autocreator);
  6974. [_dictionary release];
  6975. [super dealloc];
  6976. }
  6977. - (instancetype)copyWithZone:(NSZone *)zone {
  6978. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6979. }
  6980. - (BOOL)isEqual:(id)other {
  6981. if (self == other) {
  6982. return YES;
  6983. }
  6984. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  6985. return NO;
  6986. }
  6987. GPBInt64EnumDictionary *otherDictionary = other;
  6988. return [_dictionary isEqual:otherDictionary->_dictionary];
  6989. }
  6990. - (NSUInteger)hash {
  6991. return _dictionary.count;
  6992. }
  6993. - (NSString *)description {
  6994. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6995. }
  6996. - (NSUInteger)count {
  6997. return _dictionary.count;
  6998. }
  6999. - (void)enumerateKeysAndRawValuesUsingBlock:
  7000. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  7001. BOOL stop = NO;
  7002. NSDictionary *internal = _dictionary;
  7003. NSEnumerator *keys = [internal keyEnumerator];
  7004. NSNumber *aKey;
  7005. while ((aKey = [keys nextObject])) {
  7006. NSNumber *aValue = internal[aKey];
  7007. block([aKey longLongValue], [aValue intValue], &stop);
  7008. if (stop) {
  7009. break;
  7010. }
  7011. }
  7012. }
  7013. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7014. NSDictionary *internal = _dictionary;
  7015. NSUInteger count = internal.count;
  7016. if (count == 0) {
  7017. return 0;
  7018. }
  7019. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7020. GPBDataType keyDataType = field.mapKeyDataType;
  7021. size_t result = 0;
  7022. NSEnumerator *keys = [internal keyEnumerator];
  7023. NSNumber *aKey;
  7024. while ((aKey = [keys nextObject])) {
  7025. NSNumber *aValue = internal[aKey];
  7026. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7027. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7028. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7029. }
  7030. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7031. result += tagSize * count;
  7032. return result;
  7033. }
  7034. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7035. asField:(GPBFieldDescriptor *)field {
  7036. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7037. GPBDataType keyDataType = field.mapKeyDataType;
  7038. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7039. NSDictionary *internal = _dictionary;
  7040. NSEnumerator *keys = [internal keyEnumerator];
  7041. NSNumber *aKey;
  7042. while ((aKey = [keys nextObject])) {
  7043. NSNumber *aValue = internal[aKey];
  7044. [outputStream writeInt32NoTag:tag];
  7045. // Write the size of the message.
  7046. int64_t unwrappedKey = [aKey longLongValue];
  7047. int32_t unwrappedValue = [aValue intValue];
  7048. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7049. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7050. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7051. // Write the fields.
  7052. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7053. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7054. }
  7055. }
  7056. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7057. forKey:(GPBGenericValue *)key
  7058. keyDataType:(GPBDataType)keyDataType {
  7059. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7060. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7061. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7062. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7063. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7064. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7065. [outputStream flush];
  7066. [outputStream release];
  7067. return data;
  7068. }
  7069. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7070. forGPBGenericValueKey:(GPBGenericValue *)key {
  7071. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7072. }
  7073. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7074. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, __unused BOOL *stop) {
  7075. block([NSString stringWithFormat:@"%lld", key], @(value));
  7076. }];
  7077. }
  7078. - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
  7079. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7080. if (wrapped && value) {
  7081. int32_t result = [wrapped intValue];
  7082. if (!_validationFunc(result)) {
  7083. result = kGPBUnrecognizedEnumeratorValue;
  7084. }
  7085. *value = result;
  7086. }
  7087. return (wrapped != NULL);
  7088. }
  7089. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
  7090. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7091. if (wrapped && rawValue) {
  7092. *rawValue = [wrapped intValue];
  7093. }
  7094. return (wrapped != NULL);
  7095. }
  7096. - (void)enumerateKeysAndEnumsUsingBlock:
  7097. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  7098. GPBEnumValidationFunc func = _validationFunc;
  7099. BOOL stop = NO;
  7100. NSEnumerator *keys = [_dictionary keyEnumerator];
  7101. NSNumber *aKey;
  7102. while ((aKey = [keys nextObject])) {
  7103. NSNumber *aValue = _dictionary[aKey];
  7104. int32_t unwrapped = [aValue intValue];
  7105. if (!func(unwrapped)) {
  7106. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7107. }
  7108. block([aKey longLongValue], unwrapped, &stop);
  7109. if (stop) {
  7110. break;
  7111. }
  7112. }
  7113. }
  7114. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7115. if (otherDictionary) {
  7116. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7117. if (_autocreator) {
  7118. GPBAutocreatedDictionaryModified(_autocreator, self);
  7119. }
  7120. }
  7121. }
  7122. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7123. [_dictionary setObject:@(value) forKey:@(key)];
  7124. if (_autocreator) {
  7125. GPBAutocreatedDictionaryModified(_autocreator, self);
  7126. }
  7127. }
  7128. - (void)removeEnumForKey:(int64_t)aKey {
  7129. [_dictionary removeObjectForKey:@(aKey)];
  7130. }
  7131. - (void)removeAll {
  7132. [_dictionary removeAllObjects];
  7133. }
  7134. - (void)setEnum:(int32_t)value forKey:(int64_t)key {
  7135. if (!_validationFunc(value)) {
  7136. [NSException raise:NSInvalidArgumentException
  7137. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7138. value];
  7139. }
  7140. [_dictionary setObject:@(value) forKey:@(key)];
  7141. if (_autocreator) {
  7142. GPBAutocreatedDictionaryModified(_autocreator, self);
  7143. }
  7144. }
  7145. @end
  7146. #pragma mark - Int64 -> Object
  7147. @implementation GPBInt64ObjectDictionary {
  7148. @package
  7149. NSMutableDictionary *_dictionary;
  7150. }
  7151. - (instancetype)init {
  7152. return [self initWithObjects:NULL forKeys:NULL count:0];
  7153. }
  7154. - (instancetype)initWithObjects:(const id [])objects
  7155. forKeys:(const int64_t [])keys
  7156. count:(NSUInteger)count {
  7157. self = [super init];
  7158. if (self) {
  7159. _dictionary = [[NSMutableDictionary alloc] init];
  7160. if (count && objects && keys) {
  7161. for (NSUInteger i = 0; i < count; ++i) {
  7162. if (!objects[i]) {
  7163. [NSException raise:NSInvalidArgumentException
  7164. format:@"Attempting to add nil object to a Dictionary"];
  7165. }
  7166. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  7167. }
  7168. }
  7169. }
  7170. return self;
  7171. }
  7172. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7173. self = [self initWithObjects:NULL forKeys:NULL count:0];
  7174. if (self) {
  7175. if (dictionary) {
  7176. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7177. }
  7178. }
  7179. return self;
  7180. }
  7181. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7182. return [self initWithObjects:NULL forKeys:NULL count:0];
  7183. }
  7184. - (void)dealloc {
  7185. NSAssert(!_autocreator,
  7186. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7187. [self class], _autocreator);
  7188. [_dictionary release];
  7189. [super dealloc];
  7190. }
  7191. - (instancetype)copyWithZone:(NSZone *)zone {
  7192. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  7193. }
  7194. - (BOOL)isEqual:(id)other {
  7195. if (self == other) {
  7196. return YES;
  7197. }
  7198. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  7199. return NO;
  7200. }
  7201. GPBInt64ObjectDictionary *otherDictionary = other;
  7202. return [_dictionary isEqual:otherDictionary->_dictionary];
  7203. }
  7204. - (NSUInteger)hash {
  7205. return _dictionary.count;
  7206. }
  7207. - (NSString *)description {
  7208. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7209. }
  7210. - (NSUInteger)count {
  7211. return _dictionary.count;
  7212. }
  7213. - (void)enumerateKeysAndObjectsUsingBlock:
  7214. (void (NS_NOESCAPE ^)(int64_t key, id object, BOOL *stop))block {
  7215. BOOL stop = NO;
  7216. NSDictionary *internal = _dictionary;
  7217. NSEnumerator *keys = [internal keyEnumerator];
  7218. NSNumber *aKey;
  7219. while ((aKey = [keys nextObject])) {
  7220. id aObject = internal[aKey];
  7221. block([aKey longLongValue], aObject, &stop);
  7222. if (stop) {
  7223. break;
  7224. }
  7225. }
  7226. }
  7227. - (BOOL)isInitialized {
  7228. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  7229. if (!msg.initialized) {
  7230. return NO;
  7231. }
  7232. }
  7233. return YES;
  7234. }
  7235. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  7236. GPBInt64ObjectDictionary *newDict =
  7237. [[GPBInt64ObjectDictionary alloc] init];
  7238. NSEnumerator *keys = [_dictionary keyEnumerator];
  7239. id aKey;
  7240. NSMutableDictionary *internalDict = newDict->_dictionary;
  7241. while ((aKey = [keys nextObject])) {
  7242. GPBMessage *msg = _dictionary[aKey];
  7243. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  7244. [internalDict setObject:copiedMsg forKey:aKey];
  7245. [copiedMsg release];
  7246. }
  7247. return newDict;
  7248. }
  7249. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7250. NSDictionary *internal = _dictionary;
  7251. NSUInteger count = internal.count;
  7252. if (count == 0) {
  7253. return 0;
  7254. }
  7255. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7256. GPBDataType keyDataType = field.mapKeyDataType;
  7257. size_t result = 0;
  7258. NSEnumerator *keys = [internal keyEnumerator];
  7259. NSNumber *aKey;
  7260. while ((aKey = [keys nextObject])) {
  7261. id aObject = internal[aKey];
  7262. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7263. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  7264. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7265. }
  7266. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7267. result += tagSize * count;
  7268. return result;
  7269. }
  7270. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7271. asField:(GPBFieldDescriptor *)field {
  7272. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7273. GPBDataType keyDataType = field.mapKeyDataType;
  7274. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7275. NSDictionary *internal = _dictionary;
  7276. NSEnumerator *keys = [internal keyEnumerator];
  7277. NSNumber *aKey;
  7278. while ((aKey = [keys nextObject])) {
  7279. id aObject = internal[aKey];
  7280. [outputStream writeInt32NoTag:tag];
  7281. // Write the size of the message.
  7282. int64_t unwrappedKey = [aKey longLongValue];
  7283. id unwrappedValue = aObject;
  7284. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7285. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7286. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7287. // Write the fields.
  7288. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7289. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7290. }
  7291. }
  7292. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7293. forGPBGenericValueKey:(GPBGenericValue *)key {
  7294. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  7295. }
  7296. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7297. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, __unused BOOL *stop) {
  7298. block([NSString stringWithFormat:@"%lld", key], object);
  7299. }];
  7300. }
  7301. - (id)objectForKey:(int64_t)key {
  7302. id result = [_dictionary objectForKey:@(key)];
  7303. return result;
  7304. }
  7305. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  7306. if (otherDictionary) {
  7307. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7308. if (_autocreator) {
  7309. GPBAutocreatedDictionaryModified(_autocreator, self);
  7310. }
  7311. }
  7312. }
  7313. - (void)setObject:(id)object forKey:(int64_t)key {
  7314. if (!object) {
  7315. [NSException raise:NSInvalidArgumentException
  7316. format:@"Attempting to add nil object to a Dictionary"];
  7317. }
  7318. [_dictionary setObject:object forKey:@(key)];
  7319. if (_autocreator) {
  7320. GPBAutocreatedDictionaryModified(_autocreator, self);
  7321. }
  7322. }
  7323. - (void)removeObjectForKey:(int64_t)aKey {
  7324. [_dictionary removeObjectForKey:@(aKey)];
  7325. }
  7326. - (void)removeAll {
  7327. [_dictionary removeAllObjects];
  7328. }
  7329. @end
  7330. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  7331. // This block of code is generated, do not edit it directly.
  7332. #pragma mark - String -> UInt32
  7333. @implementation GPBStringUInt32Dictionary {
  7334. @package
  7335. NSMutableDictionary *_dictionary;
  7336. }
  7337. - (instancetype)init {
  7338. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  7339. }
  7340. - (instancetype)initWithUInt32s:(const uint32_t [])values
  7341. forKeys:(const NSString * [])keys
  7342. count:(NSUInteger)count {
  7343. self = [super init];
  7344. if (self) {
  7345. _dictionary = [[NSMutableDictionary alloc] init];
  7346. if (count && values && keys) {
  7347. for (NSUInteger i = 0; i < count; ++i) {
  7348. if (!keys[i]) {
  7349. [NSException raise:NSInvalidArgumentException
  7350. format:@"Attempting to add nil key to a Dictionary"];
  7351. }
  7352. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7353. }
  7354. }
  7355. }
  7356. return self;
  7357. }
  7358. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  7359. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  7360. if (self) {
  7361. if (dictionary) {
  7362. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7363. }
  7364. }
  7365. return self;
  7366. }
  7367. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7368. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  7369. }
  7370. - (void)dealloc {
  7371. NSAssert(!_autocreator,
  7372. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7373. [self class], _autocreator);
  7374. [_dictionary release];
  7375. [super dealloc];
  7376. }
  7377. - (instancetype)copyWithZone:(NSZone *)zone {
  7378. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7379. }
  7380. - (BOOL)isEqual:(id)other {
  7381. if (self == other) {
  7382. return YES;
  7383. }
  7384. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  7385. return NO;
  7386. }
  7387. GPBStringUInt32Dictionary *otherDictionary = other;
  7388. return [_dictionary isEqual:otherDictionary->_dictionary];
  7389. }
  7390. - (NSUInteger)hash {
  7391. return _dictionary.count;
  7392. }
  7393. - (NSString *)description {
  7394. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7395. }
  7396. - (NSUInteger)count {
  7397. return _dictionary.count;
  7398. }
  7399. - (void)enumerateKeysAndUInt32sUsingBlock:
  7400. (void (NS_NOESCAPE ^)(NSString *key, uint32_t value, BOOL *stop))block {
  7401. BOOL stop = NO;
  7402. NSDictionary *internal = _dictionary;
  7403. NSEnumerator *keys = [internal keyEnumerator];
  7404. NSString *aKey;
  7405. while ((aKey = [keys nextObject])) {
  7406. NSNumber *aValue = internal[aKey];
  7407. block(aKey, [aValue unsignedIntValue], &stop);
  7408. if (stop) {
  7409. break;
  7410. }
  7411. }
  7412. }
  7413. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7414. NSDictionary *internal = _dictionary;
  7415. NSUInteger count = internal.count;
  7416. if (count == 0) {
  7417. return 0;
  7418. }
  7419. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7420. GPBDataType keyDataType = field.mapKeyDataType;
  7421. size_t result = 0;
  7422. NSEnumerator *keys = [internal keyEnumerator];
  7423. NSString *aKey;
  7424. while ((aKey = [keys nextObject])) {
  7425. NSNumber *aValue = internal[aKey];
  7426. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7427. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  7428. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7429. }
  7430. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7431. result += tagSize * count;
  7432. return result;
  7433. }
  7434. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7435. asField:(GPBFieldDescriptor *)field {
  7436. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7437. GPBDataType keyDataType = field.mapKeyDataType;
  7438. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7439. NSDictionary *internal = _dictionary;
  7440. NSEnumerator *keys = [internal keyEnumerator];
  7441. NSString *aKey;
  7442. while ((aKey = [keys nextObject])) {
  7443. NSNumber *aValue = internal[aKey];
  7444. [outputStream writeInt32NoTag:tag];
  7445. // Write the size of the message.
  7446. NSString *unwrappedKey = aKey;
  7447. uint32_t unwrappedValue = [aValue unsignedIntValue];
  7448. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7449. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7450. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7451. // Write the fields.
  7452. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7453. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7454. }
  7455. }
  7456. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7457. forGPBGenericValueKey:(GPBGenericValue *)key {
  7458. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  7459. }
  7460. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7461. [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, __unused BOOL *stop) {
  7462. block(key, [NSString stringWithFormat:@"%u", value]);
  7463. }];
  7464. }
  7465. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
  7466. NSNumber *wrapped = [_dictionary objectForKey:key];
  7467. if (wrapped && value) {
  7468. *value = [wrapped unsignedIntValue];
  7469. }
  7470. return (wrapped != NULL);
  7471. }
  7472. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  7473. if (otherDictionary) {
  7474. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7475. if (_autocreator) {
  7476. GPBAutocreatedDictionaryModified(_autocreator, self);
  7477. }
  7478. }
  7479. }
  7480. - (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
  7481. if (!key) {
  7482. [NSException raise:NSInvalidArgumentException
  7483. format:@"Attempting to add nil key to a Dictionary"];
  7484. }
  7485. [_dictionary setObject:@(value) forKey:key];
  7486. if (_autocreator) {
  7487. GPBAutocreatedDictionaryModified(_autocreator, self);
  7488. }
  7489. }
  7490. - (void)removeUInt32ForKey:(NSString *)aKey {
  7491. [_dictionary removeObjectForKey:aKey];
  7492. }
  7493. - (void)removeAll {
  7494. [_dictionary removeAllObjects];
  7495. }
  7496. @end
  7497. #pragma mark - String -> Int32
  7498. @implementation GPBStringInt32Dictionary {
  7499. @package
  7500. NSMutableDictionary *_dictionary;
  7501. }
  7502. - (instancetype)init {
  7503. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7504. }
  7505. - (instancetype)initWithInt32s:(const int32_t [])values
  7506. forKeys:(const NSString * [])keys
  7507. count:(NSUInteger)count {
  7508. self = [super init];
  7509. if (self) {
  7510. _dictionary = [[NSMutableDictionary alloc] init];
  7511. if (count && values && keys) {
  7512. for (NSUInteger i = 0; i < count; ++i) {
  7513. if (!keys[i]) {
  7514. [NSException raise:NSInvalidArgumentException
  7515. format:@"Attempting to add nil key to a Dictionary"];
  7516. }
  7517. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7518. }
  7519. }
  7520. }
  7521. return self;
  7522. }
  7523. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  7524. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  7525. if (self) {
  7526. if (dictionary) {
  7527. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7528. }
  7529. }
  7530. return self;
  7531. }
  7532. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7533. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7534. }
  7535. - (void)dealloc {
  7536. NSAssert(!_autocreator,
  7537. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7538. [self class], _autocreator);
  7539. [_dictionary release];
  7540. [super dealloc];
  7541. }
  7542. - (instancetype)copyWithZone:(NSZone *)zone {
  7543. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7544. }
  7545. - (BOOL)isEqual:(id)other {
  7546. if (self == other) {
  7547. return YES;
  7548. }
  7549. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  7550. return NO;
  7551. }
  7552. GPBStringInt32Dictionary *otherDictionary = other;
  7553. return [_dictionary isEqual:otherDictionary->_dictionary];
  7554. }
  7555. - (NSUInteger)hash {
  7556. return _dictionary.count;
  7557. }
  7558. - (NSString *)description {
  7559. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7560. }
  7561. - (NSUInteger)count {
  7562. return _dictionary.count;
  7563. }
  7564. - (void)enumerateKeysAndInt32sUsingBlock:
  7565. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  7566. BOOL stop = NO;
  7567. NSDictionary *internal = _dictionary;
  7568. NSEnumerator *keys = [internal keyEnumerator];
  7569. NSString *aKey;
  7570. while ((aKey = [keys nextObject])) {
  7571. NSNumber *aValue = internal[aKey];
  7572. block(aKey, [aValue intValue], &stop);
  7573. if (stop) {
  7574. break;
  7575. }
  7576. }
  7577. }
  7578. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7579. NSDictionary *internal = _dictionary;
  7580. NSUInteger count = internal.count;
  7581. if (count == 0) {
  7582. return 0;
  7583. }
  7584. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7585. GPBDataType keyDataType = field.mapKeyDataType;
  7586. size_t result = 0;
  7587. NSEnumerator *keys = [internal keyEnumerator];
  7588. NSString *aKey;
  7589. while ((aKey = [keys nextObject])) {
  7590. NSNumber *aValue = internal[aKey];
  7591. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7592. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7593. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7594. }
  7595. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7596. result += tagSize * count;
  7597. return result;
  7598. }
  7599. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7600. asField:(GPBFieldDescriptor *)field {
  7601. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7602. GPBDataType keyDataType = field.mapKeyDataType;
  7603. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7604. NSDictionary *internal = _dictionary;
  7605. NSEnumerator *keys = [internal keyEnumerator];
  7606. NSString *aKey;
  7607. while ((aKey = [keys nextObject])) {
  7608. NSNumber *aValue = internal[aKey];
  7609. [outputStream writeInt32NoTag:tag];
  7610. // Write the size of the message.
  7611. NSString *unwrappedKey = aKey;
  7612. int32_t unwrappedValue = [aValue intValue];
  7613. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7614. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7615. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7616. // Write the fields.
  7617. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7618. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7619. }
  7620. }
  7621. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7622. forGPBGenericValueKey:(GPBGenericValue *)key {
  7623. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  7624. }
  7625. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7626. [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, __unused BOOL *stop) {
  7627. block(key, [NSString stringWithFormat:@"%d", value]);
  7628. }];
  7629. }
  7630. - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
  7631. NSNumber *wrapped = [_dictionary objectForKey:key];
  7632. if (wrapped && value) {
  7633. *value = [wrapped intValue];
  7634. }
  7635. return (wrapped != NULL);
  7636. }
  7637. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  7638. if (otherDictionary) {
  7639. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7640. if (_autocreator) {
  7641. GPBAutocreatedDictionaryModified(_autocreator, self);
  7642. }
  7643. }
  7644. }
  7645. - (void)setInt32:(int32_t)value forKey:(NSString *)key {
  7646. if (!key) {
  7647. [NSException raise:NSInvalidArgumentException
  7648. format:@"Attempting to add nil key to a Dictionary"];
  7649. }
  7650. [_dictionary setObject:@(value) forKey:key];
  7651. if (_autocreator) {
  7652. GPBAutocreatedDictionaryModified(_autocreator, self);
  7653. }
  7654. }
  7655. - (void)removeInt32ForKey:(NSString *)aKey {
  7656. [_dictionary removeObjectForKey:aKey];
  7657. }
  7658. - (void)removeAll {
  7659. [_dictionary removeAllObjects];
  7660. }
  7661. @end
  7662. #pragma mark - String -> UInt64
  7663. @implementation GPBStringUInt64Dictionary {
  7664. @package
  7665. NSMutableDictionary *_dictionary;
  7666. }
  7667. - (instancetype)init {
  7668. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7669. }
  7670. - (instancetype)initWithUInt64s:(const uint64_t [])values
  7671. forKeys:(const NSString * [])keys
  7672. count:(NSUInteger)count {
  7673. self = [super init];
  7674. if (self) {
  7675. _dictionary = [[NSMutableDictionary alloc] init];
  7676. if (count && values && keys) {
  7677. for (NSUInteger i = 0; i < count; ++i) {
  7678. if (!keys[i]) {
  7679. [NSException raise:NSInvalidArgumentException
  7680. format:@"Attempting to add nil key to a Dictionary"];
  7681. }
  7682. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7683. }
  7684. }
  7685. }
  7686. return self;
  7687. }
  7688. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  7689. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  7690. if (self) {
  7691. if (dictionary) {
  7692. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7693. }
  7694. }
  7695. return self;
  7696. }
  7697. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7698. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7699. }
  7700. - (void)dealloc {
  7701. NSAssert(!_autocreator,
  7702. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7703. [self class], _autocreator);
  7704. [_dictionary release];
  7705. [super dealloc];
  7706. }
  7707. - (instancetype)copyWithZone:(NSZone *)zone {
  7708. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  7709. }
  7710. - (BOOL)isEqual:(id)other {
  7711. if (self == other) {
  7712. return YES;
  7713. }
  7714. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  7715. return NO;
  7716. }
  7717. GPBStringUInt64Dictionary *otherDictionary = other;
  7718. return [_dictionary isEqual:otherDictionary->_dictionary];
  7719. }
  7720. - (NSUInteger)hash {
  7721. return _dictionary.count;
  7722. }
  7723. - (NSString *)description {
  7724. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7725. }
  7726. - (NSUInteger)count {
  7727. return _dictionary.count;
  7728. }
  7729. - (void)enumerateKeysAndUInt64sUsingBlock:
  7730. (void (NS_NOESCAPE ^)(NSString *key, uint64_t value, BOOL *stop))block {
  7731. BOOL stop = NO;
  7732. NSDictionary *internal = _dictionary;
  7733. NSEnumerator *keys = [internal keyEnumerator];
  7734. NSString *aKey;
  7735. while ((aKey = [keys nextObject])) {
  7736. NSNumber *aValue = internal[aKey];
  7737. block(aKey, [aValue unsignedLongLongValue], &stop);
  7738. if (stop) {
  7739. break;
  7740. }
  7741. }
  7742. }
  7743. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7744. NSDictionary *internal = _dictionary;
  7745. NSUInteger count = internal.count;
  7746. if (count == 0) {
  7747. return 0;
  7748. }
  7749. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7750. GPBDataType keyDataType = field.mapKeyDataType;
  7751. size_t result = 0;
  7752. NSEnumerator *keys = [internal keyEnumerator];
  7753. NSString *aKey;
  7754. while ((aKey = [keys nextObject])) {
  7755. NSNumber *aValue = internal[aKey];
  7756. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7757. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7758. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7759. }
  7760. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7761. result += tagSize * count;
  7762. return result;
  7763. }
  7764. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7765. asField:(GPBFieldDescriptor *)field {
  7766. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7767. GPBDataType keyDataType = field.mapKeyDataType;
  7768. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7769. NSDictionary *internal = _dictionary;
  7770. NSEnumerator *keys = [internal keyEnumerator];
  7771. NSString *aKey;
  7772. while ((aKey = [keys nextObject])) {
  7773. NSNumber *aValue = internal[aKey];
  7774. [outputStream writeInt32NoTag:tag];
  7775. // Write the size of the message.
  7776. NSString *unwrappedKey = aKey;
  7777. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  7778. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7779. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7780. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7781. // Write the fields.
  7782. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7783. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7784. }
  7785. }
  7786. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7787. forGPBGenericValueKey:(GPBGenericValue *)key {
  7788. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  7789. }
  7790. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7791. [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, __unused BOOL *stop) {
  7792. block(key, [NSString stringWithFormat:@"%llu", value]);
  7793. }];
  7794. }
  7795. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
  7796. NSNumber *wrapped = [_dictionary objectForKey:key];
  7797. if (wrapped && value) {
  7798. *value = [wrapped unsignedLongLongValue];
  7799. }
  7800. return (wrapped != NULL);
  7801. }
  7802. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  7803. if (otherDictionary) {
  7804. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7805. if (_autocreator) {
  7806. GPBAutocreatedDictionaryModified(_autocreator, self);
  7807. }
  7808. }
  7809. }
  7810. - (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
  7811. if (!key) {
  7812. [NSException raise:NSInvalidArgumentException
  7813. format:@"Attempting to add nil key to a Dictionary"];
  7814. }
  7815. [_dictionary setObject:@(value) forKey:key];
  7816. if (_autocreator) {
  7817. GPBAutocreatedDictionaryModified(_autocreator, self);
  7818. }
  7819. }
  7820. - (void)removeUInt64ForKey:(NSString *)aKey {
  7821. [_dictionary removeObjectForKey:aKey];
  7822. }
  7823. - (void)removeAll {
  7824. [_dictionary removeAllObjects];
  7825. }
  7826. @end
  7827. #pragma mark - String -> Int64
  7828. @implementation GPBStringInt64Dictionary {
  7829. @package
  7830. NSMutableDictionary *_dictionary;
  7831. }
  7832. - (instancetype)init {
  7833. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7834. }
  7835. - (instancetype)initWithInt64s:(const int64_t [])values
  7836. forKeys:(const NSString * [])keys
  7837. count:(NSUInteger)count {
  7838. self = [super init];
  7839. if (self) {
  7840. _dictionary = [[NSMutableDictionary alloc] init];
  7841. if (count && values && keys) {
  7842. for (NSUInteger i = 0; i < count; ++i) {
  7843. if (!keys[i]) {
  7844. [NSException raise:NSInvalidArgumentException
  7845. format:@"Attempting to add nil key to a Dictionary"];
  7846. }
  7847. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7848. }
  7849. }
  7850. }
  7851. return self;
  7852. }
  7853. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  7854. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  7855. if (self) {
  7856. if (dictionary) {
  7857. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7858. }
  7859. }
  7860. return self;
  7861. }
  7862. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  7863. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7864. }
  7865. - (void)dealloc {
  7866. NSAssert(!_autocreator,
  7867. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7868. [self class], _autocreator);
  7869. [_dictionary release];
  7870. [super dealloc];
  7871. }
  7872. - (instancetype)copyWithZone:(NSZone *)zone {
  7873. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  7874. }
  7875. - (BOOL)isEqual:(id)other {
  7876. if (self == other) {
  7877. return YES;
  7878. }
  7879. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  7880. return NO;
  7881. }
  7882. GPBStringInt64Dictionary *otherDictionary = other;
  7883. return [_dictionary isEqual:otherDictionary->_dictionary];
  7884. }
  7885. - (NSUInteger)hash {
  7886. return _dictionary.count;
  7887. }
  7888. - (NSString *)description {
  7889. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7890. }
  7891. - (NSUInteger)count {
  7892. return _dictionary.count;
  7893. }
  7894. - (void)enumerateKeysAndInt64sUsingBlock:
  7895. (void (NS_NOESCAPE ^)(NSString *key, int64_t value, BOOL *stop))block {
  7896. BOOL stop = NO;
  7897. NSDictionary *internal = _dictionary;
  7898. NSEnumerator *keys = [internal keyEnumerator];
  7899. NSString *aKey;
  7900. while ((aKey = [keys nextObject])) {
  7901. NSNumber *aValue = internal[aKey];
  7902. block(aKey, [aValue longLongValue], &stop);
  7903. if (stop) {
  7904. break;
  7905. }
  7906. }
  7907. }
  7908. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7909. NSDictionary *internal = _dictionary;
  7910. NSUInteger count = internal.count;
  7911. if (count == 0) {
  7912. return 0;
  7913. }
  7914. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7915. GPBDataType keyDataType = field.mapKeyDataType;
  7916. size_t result = 0;
  7917. NSEnumerator *keys = [internal keyEnumerator];
  7918. NSString *aKey;
  7919. while ((aKey = [keys nextObject])) {
  7920. NSNumber *aValue = internal[aKey];
  7921. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7922. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7923. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7924. }
  7925. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7926. result += tagSize * count;
  7927. return result;
  7928. }
  7929. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7930. asField:(GPBFieldDescriptor *)field {
  7931. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7932. GPBDataType keyDataType = field.mapKeyDataType;
  7933. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7934. NSDictionary *internal = _dictionary;
  7935. NSEnumerator *keys = [internal keyEnumerator];
  7936. NSString *aKey;
  7937. while ((aKey = [keys nextObject])) {
  7938. NSNumber *aValue = internal[aKey];
  7939. [outputStream writeInt32NoTag:tag];
  7940. // Write the size of the message.
  7941. NSString *unwrappedKey = aKey;
  7942. int64_t unwrappedValue = [aValue longLongValue];
  7943. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7944. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7945. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7946. // Write the fields.
  7947. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7948. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7949. }
  7950. }
  7951. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7952. forGPBGenericValueKey:(GPBGenericValue *)key {
  7953. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  7954. }
  7955. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7956. [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, __unused BOOL *stop) {
  7957. block(key, [NSString stringWithFormat:@"%lld", value]);
  7958. }];
  7959. }
  7960. - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
  7961. NSNumber *wrapped = [_dictionary objectForKey:key];
  7962. if (wrapped && value) {
  7963. *value = [wrapped longLongValue];
  7964. }
  7965. return (wrapped != NULL);
  7966. }
  7967. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  7968. if (otherDictionary) {
  7969. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7970. if (_autocreator) {
  7971. GPBAutocreatedDictionaryModified(_autocreator, self);
  7972. }
  7973. }
  7974. }
  7975. - (void)setInt64:(int64_t)value forKey:(NSString *)key {
  7976. if (!key) {
  7977. [NSException raise:NSInvalidArgumentException
  7978. format:@"Attempting to add nil key to a Dictionary"];
  7979. }
  7980. [_dictionary setObject:@(value) forKey:key];
  7981. if (_autocreator) {
  7982. GPBAutocreatedDictionaryModified(_autocreator, self);
  7983. }
  7984. }
  7985. - (void)removeInt64ForKey:(NSString *)aKey {
  7986. [_dictionary removeObjectForKey:aKey];
  7987. }
  7988. - (void)removeAll {
  7989. [_dictionary removeAllObjects];
  7990. }
  7991. @end
  7992. #pragma mark - String -> Bool
  7993. @implementation GPBStringBoolDictionary {
  7994. @package
  7995. NSMutableDictionary *_dictionary;
  7996. }
  7997. - (instancetype)init {
  7998. return [self initWithBools:NULL forKeys:NULL count:0];
  7999. }
  8000. - (instancetype)initWithBools:(const BOOL [])values
  8001. forKeys:(const NSString * [])keys
  8002. count:(NSUInteger)count {
  8003. self = [super init];
  8004. if (self) {
  8005. _dictionary = [[NSMutableDictionary alloc] init];
  8006. if (count && values && keys) {
  8007. for (NSUInteger i = 0; i < count; ++i) {
  8008. if (!keys[i]) {
  8009. [NSException raise:NSInvalidArgumentException
  8010. format:@"Attempting to add nil key to a Dictionary"];
  8011. }
  8012. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8013. }
  8014. }
  8015. }
  8016. return self;
  8017. }
  8018. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8019. self = [self initWithBools:NULL forKeys:NULL count:0];
  8020. if (self) {
  8021. if (dictionary) {
  8022. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8023. }
  8024. }
  8025. return self;
  8026. }
  8027. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8028. return [self initWithBools:NULL forKeys:NULL count:0];
  8029. }
  8030. - (void)dealloc {
  8031. NSAssert(!_autocreator,
  8032. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8033. [self class], _autocreator);
  8034. [_dictionary release];
  8035. [super dealloc];
  8036. }
  8037. - (instancetype)copyWithZone:(NSZone *)zone {
  8038. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8039. }
  8040. - (BOOL)isEqual:(id)other {
  8041. if (self == other) {
  8042. return YES;
  8043. }
  8044. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8045. return NO;
  8046. }
  8047. GPBStringBoolDictionary *otherDictionary = other;
  8048. return [_dictionary isEqual:otherDictionary->_dictionary];
  8049. }
  8050. - (NSUInteger)hash {
  8051. return _dictionary.count;
  8052. }
  8053. - (NSString *)description {
  8054. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8055. }
  8056. - (NSUInteger)count {
  8057. return _dictionary.count;
  8058. }
  8059. - (void)enumerateKeysAndBoolsUsingBlock:
  8060. (void (NS_NOESCAPE ^)(NSString *key, BOOL value, BOOL *stop))block {
  8061. BOOL stop = NO;
  8062. NSDictionary *internal = _dictionary;
  8063. NSEnumerator *keys = [internal keyEnumerator];
  8064. NSString *aKey;
  8065. while ((aKey = [keys nextObject])) {
  8066. NSNumber *aValue = internal[aKey];
  8067. block(aKey, [aValue boolValue], &stop);
  8068. if (stop) {
  8069. break;
  8070. }
  8071. }
  8072. }
  8073. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8074. NSDictionary *internal = _dictionary;
  8075. NSUInteger count = internal.count;
  8076. if (count == 0) {
  8077. return 0;
  8078. }
  8079. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8080. GPBDataType keyDataType = field.mapKeyDataType;
  8081. size_t result = 0;
  8082. NSEnumerator *keys = [internal keyEnumerator];
  8083. NSString *aKey;
  8084. while ((aKey = [keys nextObject])) {
  8085. NSNumber *aValue = internal[aKey];
  8086. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8087. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8088. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8089. }
  8090. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8091. result += tagSize * count;
  8092. return result;
  8093. }
  8094. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8095. asField:(GPBFieldDescriptor *)field {
  8096. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8097. GPBDataType keyDataType = field.mapKeyDataType;
  8098. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8099. NSDictionary *internal = _dictionary;
  8100. NSEnumerator *keys = [internal keyEnumerator];
  8101. NSString *aKey;
  8102. while ((aKey = [keys nextObject])) {
  8103. NSNumber *aValue = internal[aKey];
  8104. [outputStream writeInt32NoTag:tag];
  8105. // Write the size of the message.
  8106. NSString *unwrappedKey = aKey;
  8107. BOOL unwrappedValue = [aValue boolValue];
  8108. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8109. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8110. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8111. // Write the fields.
  8112. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8113. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8114. }
  8115. }
  8116. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8117. forGPBGenericValueKey:(GPBGenericValue *)key {
  8118. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8119. }
  8120. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8121. [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, __unused BOOL *stop) {
  8122. block(key, (value ? @"true" : @"false"));
  8123. }];
  8124. }
  8125. - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
  8126. NSNumber *wrapped = [_dictionary objectForKey:key];
  8127. if (wrapped && value) {
  8128. *value = [wrapped boolValue];
  8129. }
  8130. return (wrapped != NULL);
  8131. }
  8132. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  8133. if (otherDictionary) {
  8134. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8135. if (_autocreator) {
  8136. GPBAutocreatedDictionaryModified(_autocreator, self);
  8137. }
  8138. }
  8139. }
  8140. - (void)setBool:(BOOL)value forKey:(NSString *)key {
  8141. if (!key) {
  8142. [NSException raise:NSInvalidArgumentException
  8143. format:@"Attempting to add nil key to a Dictionary"];
  8144. }
  8145. [_dictionary setObject:@(value) forKey:key];
  8146. if (_autocreator) {
  8147. GPBAutocreatedDictionaryModified(_autocreator, self);
  8148. }
  8149. }
  8150. - (void)removeBoolForKey:(NSString *)aKey {
  8151. [_dictionary removeObjectForKey:aKey];
  8152. }
  8153. - (void)removeAll {
  8154. [_dictionary removeAllObjects];
  8155. }
  8156. @end
  8157. #pragma mark - String -> Float
  8158. @implementation GPBStringFloatDictionary {
  8159. @package
  8160. NSMutableDictionary *_dictionary;
  8161. }
  8162. - (instancetype)init {
  8163. return [self initWithFloats:NULL forKeys:NULL count:0];
  8164. }
  8165. - (instancetype)initWithFloats:(const float [])values
  8166. forKeys:(const NSString * [])keys
  8167. count:(NSUInteger)count {
  8168. self = [super init];
  8169. if (self) {
  8170. _dictionary = [[NSMutableDictionary alloc] init];
  8171. if (count && values && keys) {
  8172. for (NSUInteger i = 0; i < count; ++i) {
  8173. if (!keys[i]) {
  8174. [NSException raise:NSInvalidArgumentException
  8175. format:@"Attempting to add nil key to a Dictionary"];
  8176. }
  8177. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8178. }
  8179. }
  8180. }
  8181. return self;
  8182. }
  8183. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  8184. self = [self initWithFloats:NULL forKeys:NULL count:0];
  8185. if (self) {
  8186. if (dictionary) {
  8187. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8188. }
  8189. }
  8190. return self;
  8191. }
  8192. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8193. return [self initWithFloats:NULL forKeys:NULL count:0];
  8194. }
  8195. - (void)dealloc {
  8196. NSAssert(!_autocreator,
  8197. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8198. [self class], _autocreator);
  8199. [_dictionary release];
  8200. [super dealloc];
  8201. }
  8202. - (instancetype)copyWithZone:(NSZone *)zone {
  8203. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  8204. }
  8205. - (BOOL)isEqual:(id)other {
  8206. if (self == other) {
  8207. return YES;
  8208. }
  8209. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  8210. return NO;
  8211. }
  8212. GPBStringFloatDictionary *otherDictionary = other;
  8213. return [_dictionary isEqual:otherDictionary->_dictionary];
  8214. }
  8215. - (NSUInteger)hash {
  8216. return _dictionary.count;
  8217. }
  8218. - (NSString *)description {
  8219. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8220. }
  8221. - (NSUInteger)count {
  8222. return _dictionary.count;
  8223. }
  8224. - (void)enumerateKeysAndFloatsUsingBlock:
  8225. (void (NS_NOESCAPE ^)(NSString *key, float value, BOOL *stop))block {
  8226. BOOL stop = NO;
  8227. NSDictionary *internal = _dictionary;
  8228. NSEnumerator *keys = [internal keyEnumerator];
  8229. NSString *aKey;
  8230. while ((aKey = [keys nextObject])) {
  8231. NSNumber *aValue = internal[aKey];
  8232. block(aKey, [aValue floatValue], &stop);
  8233. if (stop) {
  8234. break;
  8235. }
  8236. }
  8237. }
  8238. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8239. NSDictionary *internal = _dictionary;
  8240. NSUInteger count = internal.count;
  8241. if (count == 0) {
  8242. return 0;
  8243. }
  8244. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8245. GPBDataType keyDataType = field.mapKeyDataType;
  8246. size_t result = 0;
  8247. NSEnumerator *keys = [internal keyEnumerator];
  8248. NSString *aKey;
  8249. while ((aKey = [keys nextObject])) {
  8250. NSNumber *aValue = internal[aKey];
  8251. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8252. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  8253. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8254. }
  8255. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8256. result += tagSize * count;
  8257. return result;
  8258. }
  8259. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8260. asField:(GPBFieldDescriptor *)field {
  8261. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8262. GPBDataType keyDataType = field.mapKeyDataType;
  8263. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8264. NSDictionary *internal = _dictionary;
  8265. NSEnumerator *keys = [internal keyEnumerator];
  8266. NSString *aKey;
  8267. while ((aKey = [keys nextObject])) {
  8268. NSNumber *aValue = internal[aKey];
  8269. [outputStream writeInt32NoTag:tag];
  8270. // Write the size of the message.
  8271. NSString *unwrappedKey = aKey;
  8272. float unwrappedValue = [aValue floatValue];
  8273. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8274. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8275. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8276. // Write the fields.
  8277. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8278. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8279. }
  8280. }
  8281. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8282. forGPBGenericValueKey:(GPBGenericValue *)key {
  8283. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  8284. }
  8285. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8286. [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, __unused BOOL *stop) {
  8287. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  8288. }];
  8289. }
  8290. - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
  8291. NSNumber *wrapped = [_dictionary objectForKey:key];
  8292. if (wrapped && value) {
  8293. *value = [wrapped floatValue];
  8294. }
  8295. return (wrapped != NULL);
  8296. }
  8297. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  8298. if (otherDictionary) {
  8299. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8300. if (_autocreator) {
  8301. GPBAutocreatedDictionaryModified(_autocreator, self);
  8302. }
  8303. }
  8304. }
  8305. - (void)setFloat:(float)value forKey:(NSString *)key {
  8306. if (!key) {
  8307. [NSException raise:NSInvalidArgumentException
  8308. format:@"Attempting to add nil key to a Dictionary"];
  8309. }
  8310. [_dictionary setObject:@(value) forKey:key];
  8311. if (_autocreator) {
  8312. GPBAutocreatedDictionaryModified(_autocreator, self);
  8313. }
  8314. }
  8315. - (void)removeFloatForKey:(NSString *)aKey {
  8316. [_dictionary removeObjectForKey:aKey];
  8317. }
  8318. - (void)removeAll {
  8319. [_dictionary removeAllObjects];
  8320. }
  8321. @end
  8322. #pragma mark - String -> Double
  8323. @implementation GPBStringDoubleDictionary {
  8324. @package
  8325. NSMutableDictionary *_dictionary;
  8326. }
  8327. - (instancetype)init {
  8328. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8329. }
  8330. - (instancetype)initWithDoubles:(const double [])values
  8331. forKeys:(const NSString * [])keys
  8332. count:(NSUInteger)count {
  8333. self = [super init];
  8334. if (self) {
  8335. _dictionary = [[NSMutableDictionary alloc] init];
  8336. if (count && values && keys) {
  8337. for (NSUInteger i = 0; i < count; ++i) {
  8338. if (!keys[i]) {
  8339. [NSException raise:NSInvalidArgumentException
  8340. format:@"Attempting to add nil key to a Dictionary"];
  8341. }
  8342. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8343. }
  8344. }
  8345. }
  8346. return self;
  8347. }
  8348. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  8349. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  8350. if (self) {
  8351. if (dictionary) {
  8352. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8353. }
  8354. }
  8355. return self;
  8356. }
  8357. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8358. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8359. }
  8360. - (void)dealloc {
  8361. NSAssert(!_autocreator,
  8362. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8363. [self class], _autocreator);
  8364. [_dictionary release];
  8365. [super dealloc];
  8366. }
  8367. - (instancetype)copyWithZone:(NSZone *)zone {
  8368. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  8369. }
  8370. - (BOOL)isEqual:(id)other {
  8371. if (self == other) {
  8372. return YES;
  8373. }
  8374. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  8375. return NO;
  8376. }
  8377. GPBStringDoubleDictionary *otherDictionary = other;
  8378. return [_dictionary isEqual:otherDictionary->_dictionary];
  8379. }
  8380. - (NSUInteger)hash {
  8381. return _dictionary.count;
  8382. }
  8383. - (NSString *)description {
  8384. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8385. }
  8386. - (NSUInteger)count {
  8387. return _dictionary.count;
  8388. }
  8389. - (void)enumerateKeysAndDoublesUsingBlock:
  8390. (void (NS_NOESCAPE ^)(NSString *key, double value, BOOL *stop))block {
  8391. BOOL stop = NO;
  8392. NSDictionary *internal = _dictionary;
  8393. NSEnumerator *keys = [internal keyEnumerator];
  8394. NSString *aKey;
  8395. while ((aKey = [keys nextObject])) {
  8396. NSNumber *aValue = internal[aKey];
  8397. block(aKey, [aValue doubleValue], &stop);
  8398. if (stop) {
  8399. break;
  8400. }
  8401. }
  8402. }
  8403. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8404. NSDictionary *internal = _dictionary;
  8405. NSUInteger count = internal.count;
  8406. if (count == 0) {
  8407. return 0;
  8408. }
  8409. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8410. GPBDataType keyDataType = field.mapKeyDataType;
  8411. size_t result = 0;
  8412. NSEnumerator *keys = [internal keyEnumerator];
  8413. NSString *aKey;
  8414. while ((aKey = [keys nextObject])) {
  8415. NSNumber *aValue = internal[aKey];
  8416. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8417. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  8418. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8419. }
  8420. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8421. result += tagSize * count;
  8422. return result;
  8423. }
  8424. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8425. asField:(GPBFieldDescriptor *)field {
  8426. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8427. GPBDataType keyDataType = field.mapKeyDataType;
  8428. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8429. NSDictionary *internal = _dictionary;
  8430. NSEnumerator *keys = [internal keyEnumerator];
  8431. NSString *aKey;
  8432. while ((aKey = [keys nextObject])) {
  8433. NSNumber *aValue = internal[aKey];
  8434. [outputStream writeInt32NoTag:tag];
  8435. // Write the size of the message.
  8436. NSString *unwrappedKey = aKey;
  8437. double unwrappedValue = [aValue doubleValue];
  8438. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8439. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8440. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8441. // Write the fields.
  8442. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8443. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8444. }
  8445. }
  8446. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8447. forGPBGenericValueKey:(GPBGenericValue *)key {
  8448. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  8449. }
  8450. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8451. [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, __unused BOOL *stop) {
  8452. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  8453. }];
  8454. }
  8455. - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
  8456. NSNumber *wrapped = [_dictionary objectForKey:key];
  8457. if (wrapped && value) {
  8458. *value = [wrapped doubleValue];
  8459. }
  8460. return (wrapped != NULL);
  8461. }
  8462. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  8463. if (otherDictionary) {
  8464. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8465. if (_autocreator) {
  8466. GPBAutocreatedDictionaryModified(_autocreator, self);
  8467. }
  8468. }
  8469. }
  8470. - (void)setDouble:(double)value forKey:(NSString *)key {
  8471. if (!key) {
  8472. [NSException raise:NSInvalidArgumentException
  8473. format:@"Attempting to add nil key to a Dictionary"];
  8474. }
  8475. [_dictionary setObject:@(value) forKey:key];
  8476. if (_autocreator) {
  8477. GPBAutocreatedDictionaryModified(_autocreator, self);
  8478. }
  8479. }
  8480. - (void)removeDoubleForKey:(NSString *)aKey {
  8481. [_dictionary removeObjectForKey:aKey];
  8482. }
  8483. - (void)removeAll {
  8484. [_dictionary removeAllObjects];
  8485. }
  8486. @end
  8487. #pragma mark - String -> Enum
  8488. @implementation GPBStringEnumDictionary {
  8489. @package
  8490. NSMutableDictionary *_dictionary;
  8491. GPBEnumValidationFunc _validationFunc;
  8492. }
  8493. @synthesize validationFunc = _validationFunc;
  8494. - (instancetype)init {
  8495. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  8496. }
  8497. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  8498. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8499. }
  8500. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8501. rawValues:(const int32_t [])rawValues
  8502. forKeys:(const NSString * [])keys
  8503. count:(NSUInteger)count {
  8504. self = [super init];
  8505. if (self) {
  8506. _dictionary = [[NSMutableDictionary alloc] init];
  8507. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  8508. if (count && rawValues && keys) {
  8509. for (NSUInteger i = 0; i < count; ++i) {
  8510. if (!keys[i]) {
  8511. [NSException raise:NSInvalidArgumentException
  8512. format:@"Attempting to add nil key to a Dictionary"];
  8513. }
  8514. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  8515. }
  8516. }
  8517. }
  8518. return self;
  8519. }
  8520. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  8521. self = [self initWithValidationFunction:dictionary.validationFunc
  8522. rawValues:NULL
  8523. forKeys:NULL
  8524. count:0];
  8525. if (self) {
  8526. if (dictionary) {
  8527. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8528. }
  8529. }
  8530. return self;
  8531. }
  8532. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8533. capacity:(__unused NSUInteger)numItems {
  8534. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8535. }
  8536. - (void)dealloc {
  8537. NSAssert(!_autocreator,
  8538. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8539. [self class], _autocreator);
  8540. [_dictionary release];
  8541. [super dealloc];
  8542. }
  8543. - (instancetype)copyWithZone:(NSZone *)zone {
  8544. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  8545. }
  8546. - (BOOL)isEqual:(id)other {
  8547. if (self == other) {
  8548. return YES;
  8549. }
  8550. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  8551. return NO;
  8552. }
  8553. GPBStringEnumDictionary *otherDictionary = other;
  8554. return [_dictionary isEqual:otherDictionary->_dictionary];
  8555. }
  8556. - (NSUInteger)hash {
  8557. return _dictionary.count;
  8558. }
  8559. - (NSString *)description {
  8560. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8561. }
  8562. - (NSUInteger)count {
  8563. return _dictionary.count;
  8564. }
  8565. - (void)enumerateKeysAndRawValuesUsingBlock:
  8566. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  8567. BOOL stop = NO;
  8568. NSDictionary *internal = _dictionary;
  8569. NSEnumerator *keys = [internal keyEnumerator];
  8570. NSString *aKey;
  8571. while ((aKey = [keys nextObject])) {
  8572. NSNumber *aValue = internal[aKey];
  8573. block(aKey, [aValue intValue], &stop);
  8574. if (stop) {
  8575. break;
  8576. }
  8577. }
  8578. }
  8579. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8580. NSDictionary *internal = _dictionary;
  8581. NSUInteger count = internal.count;
  8582. if (count == 0) {
  8583. return 0;
  8584. }
  8585. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8586. GPBDataType keyDataType = field.mapKeyDataType;
  8587. size_t result = 0;
  8588. NSEnumerator *keys = [internal keyEnumerator];
  8589. NSString *aKey;
  8590. while ((aKey = [keys nextObject])) {
  8591. NSNumber *aValue = internal[aKey];
  8592. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8593. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8594. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8595. }
  8596. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8597. result += tagSize * count;
  8598. return result;
  8599. }
  8600. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8601. asField:(GPBFieldDescriptor *)field {
  8602. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8603. GPBDataType keyDataType = field.mapKeyDataType;
  8604. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8605. NSDictionary *internal = _dictionary;
  8606. NSEnumerator *keys = [internal keyEnumerator];
  8607. NSString *aKey;
  8608. while ((aKey = [keys nextObject])) {
  8609. NSNumber *aValue = internal[aKey];
  8610. [outputStream writeInt32NoTag:tag];
  8611. // Write the size of the message.
  8612. NSString *unwrappedKey = aKey;
  8613. int32_t unwrappedValue = [aValue intValue];
  8614. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8615. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8616. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8617. // Write the fields.
  8618. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8619. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8620. }
  8621. }
  8622. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  8623. forKey:(GPBGenericValue *)key
  8624. keyDataType:(GPBDataType)keyDataType {
  8625. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  8626. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  8627. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  8628. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  8629. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  8630. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  8631. [outputStream flush];
  8632. [outputStream release];
  8633. return data;
  8634. }
  8635. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8636. forGPBGenericValueKey:(GPBGenericValue *)key {
  8637. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  8638. }
  8639. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8640. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, __unused BOOL *stop) {
  8641. block(key, @(value));
  8642. }];
  8643. }
  8644. - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
  8645. NSNumber *wrapped = [_dictionary objectForKey:key];
  8646. if (wrapped && value) {
  8647. int32_t result = [wrapped intValue];
  8648. if (!_validationFunc(result)) {
  8649. result = kGPBUnrecognizedEnumeratorValue;
  8650. }
  8651. *value = result;
  8652. }
  8653. return (wrapped != NULL);
  8654. }
  8655. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
  8656. NSNumber *wrapped = [_dictionary objectForKey:key];
  8657. if (wrapped && rawValue) {
  8658. *rawValue = [wrapped intValue];
  8659. }
  8660. return (wrapped != NULL);
  8661. }
  8662. - (void)enumerateKeysAndEnumsUsingBlock:
  8663. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  8664. GPBEnumValidationFunc func = _validationFunc;
  8665. BOOL stop = NO;
  8666. NSEnumerator *keys = [_dictionary keyEnumerator];
  8667. NSString *aKey;
  8668. while ((aKey = [keys nextObject])) {
  8669. NSNumber *aValue = _dictionary[aKey];
  8670. int32_t unwrapped = [aValue intValue];
  8671. if (!func(unwrapped)) {
  8672. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8673. }
  8674. block(aKey, unwrapped, &stop);
  8675. if (stop) {
  8676. break;
  8677. }
  8678. }
  8679. }
  8680. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  8681. if (otherDictionary) {
  8682. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8683. if (_autocreator) {
  8684. GPBAutocreatedDictionaryModified(_autocreator, self);
  8685. }
  8686. }
  8687. }
  8688. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  8689. if (!key) {
  8690. [NSException raise:NSInvalidArgumentException
  8691. format:@"Attempting to add nil key to a Dictionary"];
  8692. }
  8693. [_dictionary setObject:@(value) forKey:key];
  8694. if (_autocreator) {
  8695. GPBAutocreatedDictionaryModified(_autocreator, self);
  8696. }
  8697. }
  8698. - (void)removeEnumForKey:(NSString *)aKey {
  8699. [_dictionary removeObjectForKey:aKey];
  8700. }
  8701. - (void)removeAll {
  8702. [_dictionary removeAllObjects];
  8703. }
  8704. - (void)setEnum:(int32_t)value forKey:(NSString *)key {
  8705. if (!key) {
  8706. [NSException raise:NSInvalidArgumentException
  8707. format:@"Attempting to add nil key to a Dictionary"];
  8708. }
  8709. if (!_validationFunc(value)) {
  8710. [NSException raise:NSInvalidArgumentException
  8711. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  8712. value];
  8713. }
  8714. [_dictionary setObject:@(value) forKey:key];
  8715. if (_autocreator) {
  8716. GPBAutocreatedDictionaryModified(_autocreator, self);
  8717. }
  8718. }
  8719. @end
  8720. //%PDDM-EXPAND-END (5 expansions)
  8721. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  8722. // This block of code is generated, do not edit it directly.
  8723. #pragma mark - Bool -> UInt32
  8724. @implementation GPBBoolUInt32Dictionary {
  8725. @package
  8726. uint32_t _values[2];
  8727. BOOL _valueSet[2];
  8728. }
  8729. - (instancetype)init {
  8730. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8731. }
  8732. - (instancetype)initWithUInt32s:(const uint32_t [])values
  8733. forKeys:(const BOOL [])keys
  8734. count:(NSUInteger)count {
  8735. self = [super init];
  8736. if (self) {
  8737. for (NSUInteger i = 0; i < count; ++i) {
  8738. int idx = keys[i] ? 1 : 0;
  8739. _values[idx] = values[i];
  8740. _valueSet[idx] = YES;
  8741. }
  8742. }
  8743. return self;
  8744. }
  8745. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  8746. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  8747. if (self) {
  8748. if (dictionary) {
  8749. for (int i = 0; i < 2; ++i) {
  8750. if (dictionary->_valueSet[i]) {
  8751. _values[i] = dictionary->_values[i];
  8752. _valueSet[i] = YES;
  8753. }
  8754. }
  8755. }
  8756. }
  8757. return self;
  8758. }
  8759. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8760. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8761. }
  8762. #if !defined(NS_BLOCK_ASSERTIONS)
  8763. - (void)dealloc {
  8764. NSAssert(!_autocreator,
  8765. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8766. [self class], _autocreator);
  8767. [super dealloc];
  8768. }
  8769. #endif // !defined(NS_BLOCK_ASSERTIONS)
  8770. - (instancetype)copyWithZone:(NSZone *)zone {
  8771. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8772. }
  8773. - (BOOL)isEqual:(id)other {
  8774. if (self == other) {
  8775. return YES;
  8776. }
  8777. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  8778. return NO;
  8779. }
  8780. GPBBoolUInt32Dictionary *otherDictionary = other;
  8781. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  8782. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  8783. return NO;
  8784. }
  8785. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  8786. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  8787. return NO;
  8788. }
  8789. return YES;
  8790. }
  8791. - (NSUInteger)hash {
  8792. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8793. }
  8794. - (NSString *)description {
  8795. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  8796. if (_valueSet[0]) {
  8797. [result appendFormat:@"NO: %u", _values[0]];
  8798. }
  8799. if (_valueSet[1]) {
  8800. [result appendFormat:@"YES: %u", _values[1]];
  8801. }
  8802. [result appendString:@" }"];
  8803. return result;
  8804. }
  8805. - (NSUInteger)count {
  8806. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8807. }
  8808. - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
  8809. int idx = (key ? 1 : 0);
  8810. if (_valueSet[idx]) {
  8811. if (value) {
  8812. *value = _values[idx];
  8813. }
  8814. return YES;
  8815. }
  8816. return NO;
  8817. }
  8818. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8819. forGPBGenericValueKey:(GPBGenericValue *)key {
  8820. int idx = (key->valueBool ? 1 : 0);
  8821. _values[idx] = value->valueUInt32;
  8822. _valueSet[idx] = YES;
  8823. }
  8824. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8825. if (_valueSet[0]) {
  8826. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  8827. }
  8828. if (_valueSet[1]) {
  8829. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  8830. }
  8831. }
  8832. - (void)enumerateKeysAndUInt32sUsingBlock:
  8833. (void (NS_NOESCAPE ^)(BOOL key, uint32_t value, BOOL *stop))block {
  8834. BOOL stop = NO;
  8835. if (_valueSet[0]) {
  8836. block(NO, _values[0], &stop);
  8837. }
  8838. if (!stop && _valueSet[1]) {
  8839. block(YES, _values[1], &stop);
  8840. }
  8841. }
  8842. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8843. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8844. NSUInteger count = 0;
  8845. size_t result = 0;
  8846. for (int i = 0; i < 2; ++i) {
  8847. if (_valueSet[i]) {
  8848. ++count;
  8849. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  8850. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  8851. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8852. }
  8853. }
  8854. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8855. result += tagSize * count;
  8856. return result;
  8857. }
  8858. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8859. asField:(GPBFieldDescriptor *)field {
  8860. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8861. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8862. for (int i = 0; i < 2; ++i) {
  8863. if (_valueSet[i]) {
  8864. // Write the tag.
  8865. [outputStream writeInt32NoTag:tag];
  8866. // Write the size of the message.
  8867. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  8868. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  8869. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8870. // Write the fields.
  8871. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  8872. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  8873. }
  8874. }
  8875. }
  8876. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  8877. if (otherDictionary) {
  8878. for (int i = 0; i < 2; ++i) {
  8879. if (otherDictionary->_valueSet[i]) {
  8880. _valueSet[i] = YES;
  8881. _values[i] = otherDictionary->_values[i];
  8882. }
  8883. }
  8884. if (_autocreator) {
  8885. GPBAutocreatedDictionaryModified(_autocreator, self);
  8886. }
  8887. }
  8888. }
  8889. - (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
  8890. int idx = (key ? 1 : 0);
  8891. _values[idx] = value;
  8892. _valueSet[idx] = YES;
  8893. if (_autocreator) {
  8894. GPBAutocreatedDictionaryModified(_autocreator, self);
  8895. }
  8896. }
  8897. - (void)removeUInt32ForKey:(BOOL)aKey {
  8898. _valueSet[aKey ? 1 : 0] = NO;
  8899. }
  8900. - (void)removeAll {
  8901. _valueSet[0] = NO;
  8902. _valueSet[1] = NO;
  8903. }
  8904. @end
  8905. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  8906. // This block of code is generated, do not edit it directly.
  8907. #pragma mark - Bool -> Int32
  8908. @implementation GPBBoolInt32Dictionary {
  8909. @package
  8910. int32_t _values[2];
  8911. BOOL _valueSet[2];
  8912. }
  8913. - (instancetype)init {
  8914. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8915. }
  8916. - (instancetype)initWithInt32s:(const int32_t [])values
  8917. forKeys:(const BOOL [])keys
  8918. count:(NSUInteger)count {
  8919. self = [super init];
  8920. if (self) {
  8921. for (NSUInteger i = 0; i < count; ++i) {
  8922. int idx = keys[i] ? 1 : 0;
  8923. _values[idx] = values[i];
  8924. _valueSet[idx] = YES;
  8925. }
  8926. }
  8927. return self;
  8928. }
  8929. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  8930. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  8931. if (self) {
  8932. if (dictionary) {
  8933. for (int i = 0; i < 2; ++i) {
  8934. if (dictionary->_valueSet[i]) {
  8935. _values[i] = dictionary->_values[i];
  8936. _valueSet[i] = YES;
  8937. }
  8938. }
  8939. }
  8940. }
  8941. return self;
  8942. }
  8943. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  8944. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8945. }
  8946. #if !defined(NS_BLOCK_ASSERTIONS)
  8947. - (void)dealloc {
  8948. NSAssert(!_autocreator,
  8949. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8950. [self class], _autocreator);
  8951. [super dealloc];
  8952. }
  8953. #endif // !defined(NS_BLOCK_ASSERTIONS)
  8954. - (instancetype)copyWithZone:(NSZone *)zone {
  8955. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8956. }
  8957. - (BOOL)isEqual:(id)other {
  8958. if (self == other) {
  8959. return YES;
  8960. }
  8961. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  8962. return NO;
  8963. }
  8964. GPBBoolInt32Dictionary *otherDictionary = other;
  8965. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  8966. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  8967. return NO;
  8968. }
  8969. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  8970. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  8971. return NO;
  8972. }
  8973. return YES;
  8974. }
  8975. - (NSUInteger)hash {
  8976. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8977. }
  8978. - (NSString *)description {
  8979. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  8980. if (_valueSet[0]) {
  8981. [result appendFormat:@"NO: %d", _values[0]];
  8982. }
  8983. if (_valueSet[1]) {
  8984. [result appendFormat:@"YES: %d", _values[1]];
  8985. }
  8986. [result appendString:@" }"];
  8987. return result;
  8988. }
  8989. - (NSUInteger)count {
  8990. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8991. }
  8992. - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
  8993. int idx = (key ? 1 : 0);
  8994. if (_valueSet[idx]) {
  8995. if (value) {
  8996. *value = _values[idx];
  8997. }
  8998. return YES;
  8999. }
  9000. return NO;
  9001. }
  9002. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9003. forGPBGenericValueKey:(GPBGenericValue *)key {
  9004. int idx = (key->valueBool ? 1 : 0);
  9005. _values[idx] = value->valueInt32;
  9006. _valueSet[idx] = YES;
  9007. }
  9008. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9009. if (_valueSet[0]) {
  9010. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9011. }
  9012. if (_valueSet[1]) {
  9013. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9014. }
  9015. }
  9016. - (void)enumerateKeysAndInt32sUsingBlock:
  9017. (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block {
  9018. BOOL stop = NO;
  9019. if (_valueSet[0]) {
  9020. block(NO, _values[0], &stop);
  9021. }
  9022. if (!stop && _valueSet[1]) {
  9023. block(YES, _values[1], &stop);
  9024. }
  9025. }
  9026. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9027. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9028. NSUInteger count = 0;
  9029. size_t result = 0;
  9030. for (int i = 0; i < 2; ++i) {
  9031. if (_valueSet[i]) {
  9032. ++count;
  9033. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9034. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9035. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9036. }
  9037. }
  9038. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9039. result += tagSize * count;
  9040. return result;
  9041. }
  9042. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9043. asField:(GPBFieldDescriptor *)field {
  9044. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9045. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9046. for (int i = 0; i < 2; ++i) {
  9047. if (_valueSet[i]) {
  9048. // Write the tag.
  9049. [outputStream writeInt32NoTag:tag];
  9050. // Write the size of the message.
  9051. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9052. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9053. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9054. // Write the fields.
  9055. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9056. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9057. }
  9058. }
  9059. }
  9060. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  9061. if (otherDictionary) {
  9062. for (int i = 0; i < 2; ++i) {
  9063. if (otherDictionary->_valueSet[i]) {
  9064. _valueSet[i] = YES;
  9065. _values[i] = otherDictionary->_values[i];
  9066. }
  9067. }
  9068. if (_autocreator) {
  9069. GPBAutocreatedDictionaryModified(_autocreator, self);
  9070. }
  9071. }
  9072. }
  9073. - (void)setInt32:(int32_t)value forKey:(BOOL)key {
  9074. int idx = (key ? 1 : 0);
  9075. _values[idx] = value;
  9076. _valueSet[idx] = YES;
  9077. if (_autocreator) {
  9078. GPBAutocreatedDictionaryModified(_autocreator, self);
  9079. }
  9080. }
  9081. - (void)removeInt32ForKey:(BOOL)aKey {
  9082. _valueSet[aKey ? 1 : 0] = NO;
  9083. }
  9084. - (void)removeAll {
  9085. _valueSet[0] = NO;
  9086. _valueSet[1] = NO;
  9087. }
  9088. @end
  9089. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  9090. // This block of code is generated, do not edit it directly.
  9091. #pragma mark - Bool -> UInt64
  9092. @implementation GPBBoolUInt64Dictionary {
  9093. @package
  9094. uint64_t _values[2];
  9095. BOOL _valueSet[2];
  9096. }
  9097. - (instancetype)init {
  9098. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9099. }
  9100. - (instancetype)initWithUInt64s:(const uint64_t [])values
  9101. forKeys:(const BOOL [])keys
  9102. count:(NSUInteger)count {
  9103. self = [super init];
  9104. if (self) {
  9105. for (NSUInteger i = 0; i < count; ++i) {
  9106. int idx = keys[i] ? 1 : 0;
  9107. _values[idx] = values[i];
  9108. _valueSet[idx] = YES;
  9109. }
  9110. }
  9111. return self;
  9112. }
  9113. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  9114. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  9115. if (self) {
  9116. if (dictionary) {
  9117. for (int i = 0; i < 2; ++i) {
  9118. if (dictionary->_valueSet[i]) {
  9119. _values[i] = dictionary->_values[i];
  9120. _valueSet[i] = YES;
  9121. }
  9122. }
  9123. }
  9124. }
  9125. return self;
  9126. }
  9127. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9128. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9129. }
  9130. #if !defined(NS_BLOCK_ASSERTIONS)
  9131. - (void)dealloc {
  9132. NSAssert(!_autocreator,
  9133. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9134. [self class], _autocreator);
  9135. [super dealloc];
  9136. }
  9137. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9138. - (instancetype)copyWithZone:(NSZone *)zone {
  9139. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9140. }
  9141. - (BOOL)isEqual:(id)other {
  9142. if (self == other) {
  9143. return YES;
  9144. }
  9145. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  9146. return NO;
  9147. }
  9148. GPBBoolUInt64Dictionary *otherDictionary = other;
  9149. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9150. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9151. return NO;
  9152. }
  9153. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9154. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9155. return NO;
  9156. }
  9157. return YES;
  9158. }
  9159. - (NSUInteger)hash {
  9160. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9161. }
  9162. - (NSString *)description {
  9163. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9164. if (_valueSet[0]) {
  9165. [result appendFormat:@"NO: %llu", _values[0]];
  9166. }
  9167. if (_valueSet[1]) {
  9168. [result appendFormat:@"YES: %llu", _values[1]];
  9169. }
  9170. [result appendString:@" }"];
  9171. return result;
  9172. }
  9173. - (NSUInteger)count {
  9174. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9175. }
  9176. - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
  9177. int idx = (key ? 1 : 0);
  9178. if (_valueSet[idx]) {
  9179. if (value) {
  9180. *value = _values[idx];
  9181. }
  9182. return YES;
  9183. }
  9184. return NO;
  9185. }
  9186. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9187. forGPBGenericValueKey:(GPBGenericValue *)key {
  9188. int idx = (key->valueBool ? 1 : 0);
  9189. _values[idx] = value->valueUInt64;
  9190. _valueSet[idx] = YES;
  9191. }
  9192. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9193. if (_valueSet[0]) {
  9194. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  9195. }
  9196. if (_valueSet[1]) {
  9197. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  9198. }
  9199. }
  9200. - (void)enumerateKeysAndUInt64sUsingBlock:
  9201. (void (NS_NOESCAPE ^)(BOOL key, uint64_t value, BOOL *stop))block {
  9202. BOOL stop = NO;
  9203. if (_valueSet[0]) {
  9204. block(NO, _values[0], &stop);
  9205. }
  9206. if (!stop && _valueSet[1]) {
  9207. block(YES, _values[1], &stop);
  9208. }
  9209. }
  9210. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9211. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9212. NSUInteger count = 0;
  9213. size_t result = 0;
  9214. for (int i = 0; i < 2; ++i) {
  9215. if (_valueSet[i]) {
  9216. ++count;
  9217. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9218. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9219. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9220. }
  9221. }
  9222. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9223. result += tagSize * count;
  9224. return result;
  9225. }
  9226. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9227. asField:(GPBFieldDescriptor *)field {
  9228. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9229. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9230. for (int i = 0; i < 2; ++i) {
  9231. if (_valueSet[i]) {
  9232. // Write the tag.
  9233. [outputStream writeInt32NoTag:tag];
  9234. // Write the size of the message.
  9235. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9236. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9237. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9238. // Write the fields.
  9239. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9240. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9241. }
  9242. }
  9243. }
  9244. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  9245. if (otherDictionary) {
  9246. for (int i = 0; i < 2; ++i) {
  9247. if (otherDictionary->_valueSet[i]) {
  9248. _valueSet[i] = YES;
  9249. _values[i] = otherDictionary->_values[i];
  9250. }
  9251. }
  9252. if (_autocreator) {
  9253. GPBAutocreatedDictionaryModified(_autocreator, self);
  9254. }
  9255. }
  9256. }
  9257. - (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
  9258. int idx = (key ? 1 : 0);
  9259. _values[idx] = value;
  9260. _valueSet[idx] = YES;
  9261. if (_autocreator) {
  9262. GPBAutocreatedDictionaryModified(_autocreator, self);
  9263. }
  9264. }
  9265. - (void)removeUInt64ForKey:(BOOL)aKey {
  9266. _valueSet[aKey ? 1 : 0] = NO;
  9267. }
  9268. - (void)removeAll {
  9269. _valueSet[0] = NO;
  9270. _valueSet[1] = NO;
  9271. }
  9272. @end
  9273. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  9274. // This block of code is generated, do not edit it directly.
  9275. #pragma mark - Bool -> Int64
  9276. @implementation GPBBoolInt64Dictionary {
  9277. @package
  9278. int64_t _values[2];
  9279. BOOL _valueSet[2];
  9280. }
  9281. - (instancetype)init {
  9282. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9283. }
  9284. - (instancetype)initWithInt64s:(const int64_t [])values
  9285. forKeys:(const BOOL [])keys
  9286. count:(NSUInteger)count {
  9287. self = [super init];
  9288. if (self) {
  9289. for (NSUInteger i = 0; i < count; ++i) {
  9290. int idx = keys[i] ? 1 : 0;
  9291. _values[idx] = values[i];
  9292. _valueSet[idx] = YES;
  9293. }
  9294. }
  9295. return self;
  9296. }
  9297. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  9298. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  9299. if (self) {
  9300. if (dictionary) {
  9301. for (int i = 0; i < 2; ++i) {
  9302. if (dictionary->_valueSet[i]) {
  9303. _values[i] = dictionary->_values[i];
  9304. _valueSet[i] = YES;
  9305. }
  9306. }
  9307. }
  9308. }
  9309. return self;
  9310. }
  9311. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9312. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9313. }
  9314. #if !defined(NS_BLOCK_ASSERTIONS)
  9315. - (void)dealloc {
  9316. NSAssert(!_autocreator,
  9317. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9318. [self class], _autocreator);
  9319. [super dealloc];
  9320. }
  9321. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9322. - (instancetype)copyWithZone:(NSZone *)zone {
  9323. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9324. }
  9325. - (BOOL)isEqual:(id)other {
  9326. if (self == other) {
  9327. return YES;
  9328. }
  9329. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  9330. return NO;
  9331. }
  9332. GPBBoolInt64Dictionary *otherDictionary = other;
  9333. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9334. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9335. return NO;
  9336. }
  9337. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9338. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9339. return NO;
  9340. }
  9341. return YES;
  9342. }
  9343. - (NSUInteger)hash {
  9344. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9345. }
  9346. - (NSString *)description {
  9347. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9348. if (_valueSet[0]) {
  9349. [result appendFormat:@"NO: %lld", _values[0]];
  9350. }
  9351. if (_valueSet[1]) {
  9352. [result appendFormat:@"YES: %lld", _values[1]];
  9353. }
  9354. [result appendString:@" }"];
  9355. return result;
  9356. }
  9357. - (NSUInteger)count {
  9358. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9359. }
  9360. - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
  9361. int idx = (key ? 1 : 0);
  9362. if (_valueSet[idx]) {
  9363. if (value) {
  9364. *value = _values[idx];
  9365. }
  9366. return YES;
  9367. }
  9368. return NO;
  9369. }
  9370. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9371. forGPBGenericValueKey:(GPBGenericValue *)key {
  9372. int idx = (key->valueBool ? 1 : 0);
  9373. _values[idx] = value->valueInt64;
  9374. _valueSet[idx] = YES;
  9375. }
  9376. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9377. if (_valueSet[0]) {
  9378. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  9379. }
  9380. if (_valueSet[1]) {
  9381. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  9382. }
  9383. }
  9384. - (void)enumerateKeysAndInt64sUsingBlock:
  9385. (void (NS_NOESCAPE ^)(BOOL key, int64_t value, BOOL *stop))block {
  9386. BOOL stop = NO;
  9387. if (_valueSet[0]) {
  9388. block(NO, _values[0], &stop);
  9389. }
  9390. if (!stop && _valueSet[1]) {
  9391. block(YES, _values[1], &stop);
  9392. }
  9393. }
  9394. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9395. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9396. NSUInteger count = 0;
  9397. size_t result = 0;
  9398. for (int i = 0; i < 2; ++i) {
  9399. if (_valueSet[i]) {
  9400. ++count;
  9401. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9402. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9403. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9404. }
  9405. }
  9406. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9407. result += tagSize * count;
  9408. return result;
  9409. }
  9410. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9411. asField:(GPBFieldDescriptor *)field {
  9412. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9413. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9414. for (int i = 0; i < 2; ++i) {
  9415. if (_valueSet[i]) {
  9416. // Write the tag.
  9417. [outputStream writeInt32NoTag:tag];
  9418. // Write the size of the message.
  9419. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9420. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9421. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9422. // Write the fields.
  9423. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9424. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9425. }
  9426. }
  9427. }
  9428. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  9429. if (otherDictionary) {
  9430. for (int i = 0; i < 2; ++i) {
  9431. if (otherDictionary->_valueSet[i]) {
  9432. _valueSet[i] = YES;
  9433. _values[i] = otherDictionary->_values[i];
  9434. }
  9435. }
  9436. if (_autocreator) {
  9437. GPBAutocreatedDictionaryModified(_autocreator, self);
  9438. }
  9439. }
  9440. }
  9441. - (void)setInt64:(int64_t)value forKey:(BOOL)key {
  9442. int idx = (key ? 1 : 0);
  9443. _values[idx] = value;
  9444. _valueSet[idx] = YES;
  9445. if (_autocreator) {
  9446. GPBAutocreatedDictionaryModified(_autocreator, self);
  9447. }
  9448. }
  9449. - (void)removeInt64ForKey:(BOOL)aKey {
  9450. _valueSet[aKey ? 1 : 0] = NO;
  9451. }
  9452. - (void)removeAll {
  9453. _valueSet[0] = NO;
  9454. _valueSet[1] = NO;
  9455. }
  9456. @end
  9457. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  9458. // This block of code is generated, do not edit it directly.
  9459. #pragma mark - Bool -> Bool
  9460. @implementation GPBBoolBoolDictionary {
  9461. @package
  9462. BOOL _values[2];
  9463. BOOL _valueSet[2];
  9464. }
  9465. - (instancetype)init {
  9466. return [self initWithBools:NULL forKeys:NULL count:0];
  9467. }
  9468. - (instancetype)initWithBools:(const BOOL [])values
  9469. forKeys:(const BOOL [])keys
  9470. count:(NSUInteger)count {
  9471. self = [super init];
  9472. if (self) {
  9473. for (NSUInteger i = 0; i < count; ++i) {
  9474. int idx = keys[i] ? 1 : 0;
  9475. _values[idx] = values[i];
  9476. _valueSet[idx] = YES;
  9477. }
  9478. }
  9479. return self;
  9480. }
  9481. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  9482. self = [self initWithBools:NULL forKeys:NULL count:0];
  9483. if (self) {
  9484. if (dictionary) {
  9485. for (int i = 0; i < 2; ++i) {
  9486. if (dictionary->_valueSet[i]) {
  9487. _values[i] = dictionary->_values[i];
  9488. _valueSet[i] = YES;
  9489. }
  9490. }
  9491. }
  9492. }
  9493. return self;
  9494. }
  9495. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9496. return [self initWithBools:NULL forKeys:NULL count:0];
  9497. }
  9498. #if !defined(NS_BLOCK_ASSERTIONS)
  9499. - (void)dealloc {
  9500. NSAssert(!_autocreator,
  9501. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9502. [self class], _autocreator);
  9503. [super dealloc];
  9504. }
  9505. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9506. - (instancetype)copyWithZone:(NSZone *)zone {
  9507. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9508. }
  9509. - (BOOL)isEqual:(id)other {
  9510. if (self == other) {
  9511. return YES;
  9512. }
  9513. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  9514. return NO;
  9515. }
  9516. GPBBoolBoolDictionary *otherDictionary = other;
  9517. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9518. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9519. return NO;
  9520. }
  9521. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9522. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9523. return NO;
  9524. }
  9525. return YES;
  9526. }
  9527. - (NSUInteger)hash {
  9528. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9529. }
  9530. - (NSString *)description {
  9531. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9532. if (_valueSet[0]) {
  9533. [result appendFormat:@"NO: %d", _values[0]];
  9534. }
  9535. if (_valueSet[1]) {
  9536. [result appendFormat:@"YES: %d", _values[1]];
  9537. }
  9538. [result appendString:@" }"];
  9539. return result;
  9540. }
  9541. - (NSUInteger)count {
  9542. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9543. }
  9544. - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
  9545. int idx = (key ? 1 : 0);
  9546. if (_valueSet[idx]) {
  9547. if (value) {
  9548. *value = _values[idx];
  9549. }
  9550. return YES;
  9551. }
  9552. return NO;
  9553. }
  9554. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9555. forGPBGenericValueKey:(GPBGenericValue *)key {
  9556. int idx = (key->valueBool ? 1 : 0);
  9557. _values[idx] = value->valueBool;
  9558. _valueSet[idx] = YES;
  9559. }
  9560. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9561. if (_valueSet[0]) {
  9562. block(@"false", (_values[0] ? @"true" : @"false"));
  9563. }
  9564. if (_valueSet[1]) {
  9565. block(@"true", (_values[1] ? @"true" : @"false"));
  9566. }
  9567. }
  9568. - (void)enumerateKeysAndBoolsUsingBlock:
  9569. (void (NS_NOESCAPE ^)(BOOL key, BOOL value, BOOL *stop))block {
  9570. BOOL stop = NO;
  9571. if (_valueSet[0]) {
  9572. block(NO, _values[0], &stop);
  9573. }
  9574. if (!stop && _valueSet[1]) {
  9575. block(YES, _values[1], &stop);
  9576. }
  9577. }
  9578. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9579. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9580. NSUInteger count = 0;
  9581. size_t result = 0;
  9582. for (int i = 0; i < 2; ++i) {
  9583. if (_valueSet[i]) {
  9584. ++count;
  9585. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9586. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9587. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9588. }
  9589. }
  9590. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9591. result += tagSize * count;
  9592. return result;
  9593. }
  9594. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9595. asField:(GPBFieldDescriptor *)field {
  9596. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9597. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9598. for (int i = 0; i < 2; ++i) {
  9599. if (_valueSet[i]) {
  9600. // Write the tag.
  9601. [outputStream writeInt32NoTag:tag];
  9602. // Write the size of the message.
  9603. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9604. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9605. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9606. // Write the fields.
  9607. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9608. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9609. }
  9610. }
  9611. }
  9612. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  9613. if (otherDictionary) {
  9614. for (int i = 0; i < 2; ++i) {
  9615. if (otherDictionary->_valueSet[i]) {
  9616. _valueSet[i] = YES;
  9617. _values[i] = otherDictionary->_values[i];
  9618. }
  9619. }
  9620. if (_autocreator) {
  9621. GPBAutocreatedDictionaryModified(_autocreator, self);
  9622. }
  9623. }
  9624. }
  9625. - (void)setBool:(BOOL)value forKey:(BOOL)key {
  9626. int idx = (key ? 1 : 0);
  9627. _values[idx] = value;
  9628. _valueSet[idx] = YES;
  9629. if (_autocreator) {
  9630. GPBAutocreatedDictionaryModified(_autocreator, self);
  9631. }
  9632. }
  9633. - (void)removeBoolForKey:(BOOL)aKey {
  9634. _valueSet[aKey ? 1 : 0] = NO;
  9635. }
  9636. - (void)removeAll {
  9637. _valueSet[0] = NO;
  9638. _valueSet[1] = NO;
  9639. }
  9640. @end
  9641. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  9642. // This block of code is generated, do not edit it directly.
  9643. #pragma mark - Bool -> Float
  9644. @implementation GPBBoolFloatDictionary {
  9645. @package
  9646. float _values[2];
  9647. BOOL _valueSet[2];
  9648. }
  9649. - (instancetype)init {
  9650. return [self initWithFloats:NULL forKeys:NULL count:0];
  9651. }
  9652. - (instancetype)initWithFloats:(const float [])values
  9653. forKeys:(const BOOL [])keys
  9654. count:(NSUInteger)count {
  9655. self = [super init];
  9656. if (self) {
  9657. for (NSUInteger i = 0; i < count; ++i) {
  9658. int idx = keys[i] ? 1 : 0;
  9659. _values[idx] = values[i];
  9660. _valueSet[idx] = YES;
  9661. }
  9662. }
  9663. return self;
  9664. }
  9665. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  9666. self = [self initWithFloats:NULL forKeys:NULL count:0];
  9667. if (self) {
  9668. if (dictionary) {
  9669. for (int i = 0; i < 2; ++i) {
  9670. if (dictionary->_valueSet[i]) {
  9671. _values[i] = dictionary->_values[i];
  9672. _valueSet[i] = YES;
  9673. }
  9674. }
  9675. }
  9676. }
  9677. return self;
  9678. }
  9679. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9680. return [self initWithFloats:NULL forKeys:NULL count:0];
  9681. }
  9682. #if !defined(NS_BLOCK_ASSERTIONS)
  9683. - (void)dealloc {
  9684. NSAssert(!_autocreator,
  9685. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9686. [self class], _autocreator);
  9687. [super dealloc];
  9688. }
  9689. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9690. - (instancetype)copyWithZone:(NSZone *)zone {
  9691. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9692. }
  9693. - (BOOL)isEqual:(id)other {
  9694. if (self == other) {
  9695. return YES;
  9696. }
  9697. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  9698. return NO;
  9699. }
  9700. GPBBoolFloatDictionary *otherDictionary = other;
  9701. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9702. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9703. return NO;
  9704. }
  9705. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9706. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9707. return NO;
  9708. }
  9709. return YES;
  9710. }
  9711. - (NSUInteger)hash {
  9712. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9713. }
  9714. - (NSString *)description {
  9715. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9716. if (_valueSet[0]) {
  9717. [result appendFormat:@"NO: %f", _values[0]];
  9718. }
  9719. if (_valueSet[1]) {
  9720. [result appendFormat:@"YES: %f", _values[1]];
  9721. }
  9722. [result appendString:@" }"];
  9723. return result;
  9724. }
  9725. - (NSUInteger)count {
  9726. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9727. }
  9728. - (BOOL)getFloat:(float *)value forKey:(BOOL)key {
  9729. int idx = (key ? 1 : 0);
  9730. if (_valueSet[idx]) {
  9731. if (value) {
  9732. *value = _values[idx];
  9733. }
  9734. return YES;
  9735. }
  9736. return NO;
  9737. }
  9738. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9739. forGPBGenericValueKey:(GPBGenericValue *)key {
  9740. int idx = (key->valueBool ? 1 : 0);
  9741. _values[idx] = value->valueFloat;
  9742. _valueSet[idx] = YES;
  9743. }
  9744. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9745. if (_valueSet[0]) {
  9746. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  9747. }
  9748. if (_valueSet[1]) {
  9749. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  9750. }
  9751. }
  9752. - (void)enumerateKeysAndFloatsUsingBlock:
  9753. (void (NS_NOESCAPE ^)(BOOL key, float value, BOOL *stop))block {
  9754. BOOL stop = NO;
  9755. if (_valueSet[0]) {
  9756. block(NO, _values[0], &stop);
  9757. }
  9758. if (!stop && _valueSet[1]) {
  9759. block(YES, _values[1], &stop);
  9760. }
  9761. }
  9762. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9763. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9764. NSUInteger count = 0;
  9765. size_t result = 0;
  9766. for (int i = 0; i < 2; ++i) {
  9767. if (_valueSet[i]) {
  9768. ++count;
  9769. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9770. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9771. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9772. }
  9773. }
  9774. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9775. result += tagSize * count;
  9776. return result;
  9777. }
  9778. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9779. asField:(GPBFieldDescriptor *)field {
  9780. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9781. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9782. for (int i = 0; i < 2; ++i) {
  9783. if (_valueSet[i]) {
  9784. // Write the tag.
  9785. [outputStream writeInt32NoTag:tag];
  9786. // Write the size of the message.
  9787. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9788. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9789. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9790. // Write the fields.
  9791. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9792. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9793. }
  9794. }
  9795. }
  9796. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  9797. if (otherDictionary) {
  9798. for (int i = 0; i < 2; ++i) {
  9799. if (otherDictionary->_valueSet[i]) {
  9800. _valueSet[i] = YES;
  9801. _values[i] = otherDictionary->_values[i];
  9802. }
  9803. }
  9804. if (_autocreator) {
  9805. GPBAutocreatedDictionaryModified(_autocreator, self);
  9806. }
  9807. }
  9808. }
  9809. - (void)setFloat:(float)value forKey:(BOOL)key {
  9810. int idx = (key ? 1 : 0);
  9811. _values[idx] = value;
  9812. _valueSet[idx] = YES;
  9813. if (_autocreator) {
  9814. GPBAutocreatedDictionaryModified(_autocreator, self);
  9815. }
  9816. }
  9817. - (void)removeFloatForKey:(BOOL)aKey {
  9818. _valueSet[aKey ? 1 : 0] = NO;
  9819. }
  9820. - (void)removeAll {
  9821. _valueSet[0] = NO;
  9822. _valueSet[1] = NO;
  9823. }
  9824. @end
  9825. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  9826. // This block of code is generated, do not edit it directly.
  9827. #pragma mark - Bool -> Double
  9828. @implementation GPBBoolDoubleDictionary {
  9829. @package
  9830. double _values[2];
  9831. BOOL _valueSet[2];
  9832. }
  9833. - (instancetype)init {
  9834. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9835. }
  9836. - (instancetype)initWithDoubles:(const double [])values
  9837. forKeys:(const BOOL [])keys
  9838. count:(NSUInteger)count {
  9839. self = [super init];
  9840. if (self) {
  9841. for (NSUInteger i = 0; i < count; ++i) {
  9842. int idx = keys[i] ? 1 : 0;
  9843. _values[idx] = values[i];
  9844. _valueSet[idx] = YES;
  9845. }
  9846. }
  9847. return self;
  9848. }
  9849. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  9850. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  9851. if (self) {
  9852. if (dictionary) {
  9853. for (int i = 0; i < 2; ++i) {
  9854. if (dictionary->_valueSet[i]) {
  9855. _values[i] = dictionary->_values[i];
  9856. _valueSet[i] = YES;
  9857. }
  9858. }
  9859. }
  9860. }
  9861. return self;
  9862. }
  9863. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  9864. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9865. }
  9866. #if !defined(NS_BLOCK_ASSERTIONS)
  9867. - (void)dealloc {
  9868. NSAssert(!_autocreator,
  9869. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9870. [self class], _autocreator);
  9871. [super dealloc];
  9872. }
  9873. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9874. - (instancetype)copyWithZone:(NSZone *)zone {
  9875. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9876. }
  9877. - (BOOL)isEqual:(id)other {
  9878. if (self == other) {
  9879. return YES;
  9880. }
  9881. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  9882. return NO;
  9883. }
  9884. GPBBoolDoubleDictionary *otherDictionary = other;
  9885. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9886. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9887. return NO;
  9888. }
  9889. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9890. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9891. return NO;
  9892. }
  9893. return YES;
  9894. }
  9895. - (NSUInteger)hash {
  9896. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9897. }
  9898. - (NSString *)description {
  9899. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9900. if (_valueSet[0]) {
  9901. [result appendFormat:@"NO: %lf", _values[0]];
  9902. }
  9903. if (_valueSet[1]) {
  9904. [result appendFormat:@"YES: %lf", _values[1]];
  9905. }
  9906. [result appendString:@" }"];
  9907. return result;
  9908. }
  9909. - (NSUInteger)count {
  9910. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9911. }
  9912. - (BOOL)getDouble:(double *)value forKey:(BOOL)key {
  9913. int idx = (key ? 1 : 0);
  9914. if (_valueSet[idx]) {
  9915. if (value) {
  9916. *value = _values[idx];
  9917. }
  9918. return YES;
  9919. }
  9920. return NO;
  9921. }
  9922. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9923. forGPBGenericValueKey:(GPBGenericValue *)key {
  9924. int idx = (key->valueBool ? 1 : 0);
  9925. _values[idx] = value->valueDouble;
  9926. _valueSet[idx] = YES;
  9927. }
  9928. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9929. if (_valueSet[0]) {
  9930. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  9931. }
  9932. if (_valueSet[1]) {
  9933. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  9934. }
  9935. }
  9936. - (void)enumerateKeysAndDoublesUsingBlock:
  9937. (void (NS_NOESCAPE ^)(BOOL key, double value, BOOL *stop))block {
  9938. BOOL stop = NO;
  9939. if (_valueSet[0]) {
  9940. block(NO, _values[0], &stop);
  9941. }
  9942. if (!stop && _valueSet[1]) {
  9943. block(YES, _values[1], &stop);
  9944. }
  9945. }
  9946. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9947. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9948. NSUInteger count = 0;
  9949. size_t result = 0;
  9950. for (int i = 0; i < 2; ++i) {
  9951. if (_valueSet[i]) {
  9952. ++count;
  9953. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9954. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9955. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9956. }
  9957. }
  9958. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9959. result += tagSize * count;
  9960. return result;
  9961. }
  9962. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9963. asField:(GPBFieldDescriptor *)field {
  9964. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9965. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9966. for (int i = 0; i < 2; ++i) {
  9967. if (_valueSet[i]) {
  9968. // Write the tag.
  9969. [outputStream writeInt32NoTag:tag];
  9970. // Write the size of the message.
  9971. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9972. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9973. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9974. // Write the fields.
  9975. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9976. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9977. }
  9978. }
  9979. }
  9980. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  9981. if (otherDictionary) {
  9982. for (int i = 0; i < 2; ++i) {
  9983. if (otherDictionary->_valueSet[i]) {
  9984. _valueSet[i] = YES;
  9985. _values[i] = otherDictionary->_values[i];
  9986. }
  9987. }
  9988. if (_autocreator) {
  9989. GPBAutocreatedDictionaryModified(_autocreator, self);
  9990. }
  9991. }
  9992. }
  9993. - (void)setDouble:(double)value forKey:(BOOL)key {
  9994. int idx = (key ? 1 : 0);
  9995. _values[idx] = value;
  9996. _valueSet[idx] = YES;
  9997. if (_autocreator) {
  9998. GPBAutocreatedDictionaryModified(_autocreator, self);
  9999. }
  10000. }
  10001. - (void)removeDoubleForKey:(BOOL)aKey {
  10002. _valueSet[aKey ? 1 : 0] = NO;
  10003. }
  10004. - (void)removeAll {
  10005. _valueSet[0] = NO;
  10006. _valueSet[1] = NO;
  10007. }
  10008. @end
  10009. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  10010. // This block of code is generated, do not edit it directly.
  10011. #pragma mark - Bool -> Object
  10012. @implementation GPBBoolObjectDictionary {
  10013. @package
  10014. id _values[2];
  10015. }
  10016. - (instancetype)init {
  10017. return [self initWithObjects:NULL forKeys:NULL count:0];
  10018. }
  10019. - (instancetype)initWithObjects:(const id [])objects
  10020. forKeys:(const BOOL [])keys
  10021. count:(NSUInteger)count {
  10022. self = [super init];
  10023. if (self) {
  10024. for (NSUInteger i = 0; i < count; ++i) {
  10025. if (!objects[i]) {
  10026. [NSException raise:NSInvalidArgumentException
  10027. format:@"Attempting to add nil object to a Dictionary"];
  10028. }
  10029. int idx = keys[i] ? 1 : 0;
  10030. [_values[idx] release];
  10031. _values[idx] = (id)[objects[i] retain];
  10032. }
  10033. }
  10034. return self;
  10035. }
  10036. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  10037. self = [self initWithObjects:NULL forKeys:NULL count:0];
  10038. if (self) {
  10039. if (dictionary) {
  10040. _values[0] = [dictionary->_values[0] retain];
  10041. _values[1] = [dictionary->_values[1] retain];
  10042. }
  10043. }
  10044. return self;
  10045. }
  10046. - (instancetype)initWithCapacity:(__unused NSUInteger)numItems {
  10047. return [self initWithObjects:NULL forKeys:NULL count:0];
  10048. }
  10049. - (void)dealloc {
  10050. NSAssert(!_autocreator,
  10051. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10052. [self class], _autocreator);
  10053. [_values[0] release];
  10054. [_values[1] release];
  10055. [super dealloc];
  10056. }
  10057. - (instancetype)copyWithZone:(NSZone *)zone {
  10058. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  10059. }
  10060. - (BOOL)isEqual:(id)other {
  10061. if (self == other) {
  10062. return YES;
  10063. }
  10064. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  10065. return NO;
  10066. }
  10067. GPBBoolObjectDictionary *otherDictionary = other;
  10068. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  10069. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  10070. return NO;
  10071. }
  10072. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  10073. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  10074. return NO;
  10075. }
  10076. return YES;
  10077. }
  10078. - (NSUInteger)hash {
  10079. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10080. }
  10081. - (NSString *)description {
  10082. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10083. if ((_values[0] != nil)) {
  10084. [result appendFormat:@"NO: %@", _values[0]];
  10085. }
  10086. if ((_values[1] != nil)) {
  10087. [result appendFormat:@"YES: %@", _values[1]];
  10088. }
  10089. [result appendString:@" }"];
  10090. return result;
  10091. }
  10092. - (NSUInteger)count {
  10093. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10094. }
  10095. - (id)objectForKey:(BOOL)key {
  10096. return _values[key ? 1 : 0];
  10097. }
  10098. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10099. forGPBGenericValueKey:(GPBGenericValue *)key {
  10100. int idx = (key->valueBool ? 1 : 0);
  10101. [_values[idx] release];
  10102. _values[idx] = [value->valueString retain];
  10103. }
  10104. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10105. if (_values[0] != nil) {
  10106. block(@"false", _values[0]);
  10107. }
  10108. if ((_values[1] != nil)) {
  10109. block(@"true", _values[1]);
  10110. }
  10111. }
  10112. - (void)enumerateKeysAndObjectsUsingBlock:
  10113. (void (NS_NOESCAPE ^)(BOOL key, id object, BOOL *stop))block {
  10114. BOOL stop = NO;
  10115. if (_values[0] != nil) {
  10116. block(NO, _values[0], &stop);
  10117. }
  10118. if (!stop && (_values[1] != nil)) {
  10119. block(YES, _values[1], &stop);
  10120. }
  10121. }
  10122. - (BOOL)isInitialized {
  10123. if (_values[0] && ![_values[0] isInitialized]) {
  10124. return NO;
  10125. }
  10126. if (_values[1] && ![_values[1] isInitialized]) {
  10127. return NO;
  10128. }
  10129. return YES;
  10130. }
  10131. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  10132. GPBBoolObjectDictionary *newDict =
  10133. [[GPBBoolObjectDictionary alloc] init];
  10134. for (int i = 0; i < 2; ++i) {
  10135. if (_values[i] != nil) {
  10136. newDict->_values[i] = [_values[i] copyWithZone:zone];
  10137. }
  10138. }
  10139. return newDict;
  10140. }
  10141. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10142. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10143. NSUInteger count = 0;
  10144. size_t result = 0;
  10145. for (int i = 0; i < 2; ++i) {
  10146. if (_values[i] != nil) {
  10147. ++count;
  10148. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10149. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10150. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10151. }
  10152. }
  10153. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10154. result += tagSize * count;
  10155. return result;
  10156. }
  10157. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10158. asField:(GPBFieldDescriptor *)field {
  10159. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10160. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10161. for (int i = 0; i < 2; ++i) {
  10162. if (_values[i] != nil) {
  10163. // Write the tag.
  10164. [outputStream writeInt32NoTag:tag];
  10165. // Write the size of the message.
  10166. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10167. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10168. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10169. // Write the fields.
  10170. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10171. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10172. }
  10173. }
  10174. }
  10175. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  10176. if (otherDictionary) {
  10177. for (int i = 0; i < 2; ++i) {
  10178. if (otherDictionary->_values[i] != nil) {
  10179. [_values[i] release];
  10180. _values[i] = [otherDictionary->_values[i] retain];
  10181. }
  10182. }
  10183. if (_autocreator) {
  10184. GPBAutocreatedDictionaryModified(_autocreator, self);
  10185. }
  10186. }
  10187. }
  10188. - (void)setObject:(id)object forKey:(BOOL)key {
  10189. if (!object) {
  10190. [NSException raise:NSInvalidArgumentException
  10191. format:@"Attempting to add nil object to a Dictionary"];
  10192. }
  10193. int idx = (key ? 1 : 0);
  10194. [_values[idx] release];
  10195. _values[idx] = [object retain];
  10196. if (_autocreator) {
  10197. GPBAutocreatedDictionaryModified(_autocreator, self);
  10198. }
  10199. }
  10200. - (void)removeObjectForKey:(BOOL)aKey {
  10201. int idx = (aKey ? 1 : 0);
  10202. [_values[idx] release];
  10203. _values[idx] = nil;
  10204. }
  10205. - (void)removeAll {
  10206. for (int i = 0; i < 2; ++i) {
  10207. [_values[i] release];
  10208. _values[i] = nil;
  10209. }
  10210. }
  10211. @end
  10212. //%PDDM-EXPAND-END (8 expansions)
  10213. // clang-format on
  10214. #pragma mark - Bool -> Enum
  10215. @implementation GPBBoolEnumDictionary {
  10216. @package
  10217. GPBEnumValidationFunc _validationFunc;
  10218. int32_t _values[2];
  10219. BOOL _valueSet[2];
  10220. }
  10221. @synthesize validationFunc = _validationFunc;
  10222. - (instancetype)init {
  10223. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  10224. }
  10225. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  10226. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10227. }
  10228. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10229. rawValues:(const int32_t[])rawValues
  10230. forKeys:(const BOOL[])keys
  10231. count:(NSUInteger)count {
  10232. self = [super init];
  10233. if (self) {
  10234. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  10235. for (NSUInteger i = 0; i < count; ++i) {
  10236. int idx = keys[i] ? 1 : 0;
  10237. _values[idx] = rawValues[i];
  10238. _valueSet[idx] = YES;
  10239. }
  10240. }
  10241. return self;
  10242. }
  10243. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  10244. self = [self initWithValidationFunction:dictionary.validationFunc
  10245. rawValues:NULL
  10246. forKeys:NULL
  10247. count:0];
  10248. if (self) {
  10249. if (dictionary) {
  10250. for (int i = 0; i < 2; ++i) {
  10251. if (dictionary->_valueSet[i]) {
  10252. _values[i] = dictionary->_values[i];
  10253. _valueSet[i] = YES;
  10254. }
  10255. }
  10256. }
  10257. }
  10258. return self;
  10259. }
  10260. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10261. capacity:(__unused NSUInteger)numItems {
  10262. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10263. }
  10264. #if !defined(NS_BLOCK_ASSERTIONS)
  10265. - (void)dealloc {
  10266. NSAssert(!_autocreator, @"%@: Autocreator must be cleared before release, autocreator: %@",
  10267. [self class], _autocreator);
  10268. [super dealloc];
  10269. }
  10270. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10271. - (instancetype)copyWithZone:(NSZone *)zone {
  10272. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  10273. }
  10274. - (BOOL)isEqual:(id)other {
  10275. if (self == other) {
  10276. return YES;
  10277. }
  10278. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  10279. return NO;
  10280. }
  10281. GPBBoolEnumDictionary *otherDictionary = other;
  10282. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10283. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10284. return NO;
  10285. }
  10286. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10287. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10288. return NO;
  10289. }
  10290. return YES;
  10291. }
  10292. - (NSUInteger)hash {
  10293. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10294. }
  10295. - (NSString *)description {
  10296. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10297. if (_valueSet[0]) {
  10298. [result appendFormat:@"NO: %d", _values[0]];
  10299. }
  10300. if (_valueSet[1]) {
  10301. [result appendFormat:@"YES: %d", _values[1]];
  10302. }
  10303. [result appendString:@" }"];
  10304. return result;
  10305. }
  10306. - (NSUInteger)count {
  10307. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10308. }
  10309. - (BOOL)getEnum:(int32_t *)value forKey:(BOOL)key {
  10310. int idx = (key ? 1 : 0);
  10311. if (_valueSet[idx]) {
  10312. if (value) {
  10313. int32_t result = _values[idx];
  10314. if (!_validationFunc(result)) {
  10315. result = kGPBUnrecognizedEnumeratorValue;
  10316. }
  10317. *value = result;
  10318. }
  10319. return YES;
  10320. }
  10321. return NO;
  10322. }
  10323. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(BOOL)key {
  10324. int idx = (key ? 1 : 0);
  10325. if (_valueSet[idx]) {
  10326. if (rawValue) {
  10327. *rawValue = _values[idx];
  10328. }
  10329. return YES;
  10330. }
  10331. return NO;
  10332. }
  10333. - (void)enumerateKeysAndRawValuesUsingBlock:(void(NS_NOESCAPE ^)(BOOL key, int32_t value,
  10334. BOOL *stop))block {
  10335. BOOL stop = NO;
  10336. if (_valueSet[0]) {
  10337. block(NO, _values[0], &stop);
  10338. }
  10339. if (!stop && _valueSet[1]) {
  10340. block(YES, _values[1], &stop);
  10341. }
  10342. }
  10343. - (void)enumerateKeysAndEnumsUsingBlock:(void(NS_NOESCAPE ^)(BOOL key, int32_t rawValue,
  10344. BOOL *stop))block {
  10345. BOOL stop = NO;
  10346. GPBEnumValidationFunc func = _validationFunc;
  10347. int32_t validatedValue;
  10348. if (_valueSet[0]) {
  10349. validatedValue = _values[0];
  10350. if (!func(validatedValue)) {
  10351. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10352. }
  10353. block(NO, validatedValue, &stop);
  10354. }
  10355. if (!stop && _valueSet[1]) {
  10356. validatedValue = _values[1];
  10357. if (!func(validatedValue)) {
  10358. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10359. }
  10360. block(YES, validatedValue, &stop);
  10361. }
  10362. }
  10363. // clang-format off
  10364. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10365. // This block of code is generated, do not edit it directly.
  10366. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  10367. forKey:(GPBGenericValue *)key
  10368. keyDataType:(GPBDataType)keyDataType {
  10369. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  10370. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  10371. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  10372. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  10373. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  10374. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  10375. [outputStream flush];
  10376. [outputStream release];
  10377. return data;
  10378. }
  10379. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10380. // clang-format on
  10381. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10382. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10383. NSUInteger count = 0;
  10384. size_t result = 0;
  10385. for (int i = 0; i < 2; ++i) {
  10386. if (_valueSet[i]) {
  10387. ++count;
  10388. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10389. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10390. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10391. }
  10392. }
  10393. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10394. result += tagSize * count;
  10395. return result;
  10396. }
  10397. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10398. asField:(GPBFieldDescriptor *)field {
  10399. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10400. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10401. for (int i = 0; i < 2; ++i) {
  10402. if (_valueSet[i]) {
  10403. // Write the tag.
  10404. [outputStream writeInt32NoTag:tag];
  10405. // Write the size of the message.
  10406. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10407. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10408. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10409. // Write the fields.
  10410. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10411. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10412. }
  10413. }
  10414. }
  10415. - (void)enumerateForTextFormat:(void(NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10416. if (_valueSet[0]) {
  10417. block(@"false", @(_values[0]));
  10418. }
  10419. if (_valueSet[1]) {
  10420. block(@"true", @(_values[1]));
  10421. }
  10422. }
  10423. - (void)setGPBGenericValue:(GPBGenericValue *)value forGPBGenericValueKey:(GPBGenericValue *)key {
  10424. int idx = (key->valueBool ? 1 : 0);
  10425. _values[idx] = value->valueInt32;
  10426. _valueSet[idx] = YES;
  10427. }
  10428. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  10429. if (otherDictionary) {
  10430. for (int i = 0; i < 2; ++i) {
  10431. if (otherDictionary->_valueSet[i]) {
  10432. _valueSet[i] = YES;
  10433. _values[i] = otherDictionary->_values[i];
  10434. }
  10435. }
  10436. if (_autocreator) {
  10437. GPBAutocreatedDictionaryModified(_autocreator, self);
  10438. }
  10439. }
  10440. }
  10441. - (void)setEnum:(int32_t)value forKey:(BOOL)key {
  10442. if (!_validationFunc(value)) {
  10443. [NSException raise:NSInvalidArgumentException
  10444. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)", value];
  10445. }
  10446. int idx = (key ? 1 : 0);
  10447. _values[idx] = value;
  10448. _valueSet[idx] = YES;
  10449. if (_autocreator) {
  10450. GPBAutocreatedDictionaryModified(_autocreator, self);
  10451. }
  10452. }
  10453. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  10454. int idx = (key ? 1 : 0);
  10455. _values[idx] = rawValue;
  10456. _valueSet[idx] = YES;
  10457. if (_autocreator) {
  10458. GPBAutocreatedDictionaryModified(_autocreator, self);
  10459. }
  10460. }
  10461. - (void)removeEnumForKey:(BOOL)aKey {
  10462. _valueSet[aKey ? 1 : 0] = NO;
  10463. }
  10464. - (void)removeAll {
  10465. _valueSet[0] = NO;
  10466. _valueSet[1] = NO;
  10467. }
  10468. @end
  10469. #pragma mark - NSDictionary Subclass
  10470. @implementation GPBAutocreatedDictionary {
  10471. NSMutableDictionary *_dictionary;
  10472. }
  10473. - (void)dealloc {
  10474. NSAssert(!_autocreator, @"%@: Autocreator must be cleared before release, autocreator: %@",
  10475. [self class], _autocreator);
  10476. [_dictionary release];
  10477. [super dealloc];
  10478. }
  10479. #pragma mark Required NSDictionary overrides
  10480. - (instancetype)initWithObjects:(const id[])objects
  10481. forKeys:(const id<NSCopying>[])keys
  10482. count:(NSUInteger)count {
  10483. self = [super init];
  10484. if (self) {
  10485. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys count:count];
  10486. }
  10487. return self;
  10488. }
  10489. - (NSUInteger)count {
  10490. return [_dictionary count];
  10491. }
  10492. - (id)objectForKey:(id)aKey {
  10493. return [_dictionary objectForKey:aKey];
  10494. }
  10495. - (NSEnumerator *)keyEnumerator {
  10496. if (_dictionary == nil) {
  10497. _dictionary = [[NSMutableDictionary alloc] init];
  10498. }
  10499. return [_dictionary keyEnumerator];
  10500. }
  10501. #pragma mark Required NSMutableDictionary overrides
  10502. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  10503. // since we only autocreate empty dictionaries.
  10504. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  10505. if (_dictionary == nil) {
  10506. _dictionary = [[NSMutableDictionary alloc] init];
  10507. }
  10508. [_dictionary setObject:anObject forKey:aKey];
  10509. if (_autocreator) {
  10510. GPBAutocreatedDictionaryModified(_autocreator, self);
  10511. }
  10512. }
  10513. - (void)removeObjectForKey:(id)aKey {
  10514. [_dictionary removeObjectForKey:aKey];
  10515. }
  10516. #pragma mark Extra things hooked
  10517. - (id)copyWithZone:(NSZone *)zone {
  10518. if (_dictionary == nil) {
  10519. return [[NSMutableDictionary allocWithZone:zone] init];
  10520. }
  10521. return [_dictionary copyWithZone:zone];
  10522. }
  10523. - (id)mutableCopyWithZone:(NSZone *)zone {
  10524. if (_dictionary == nil) {
  10525. return [[NSMutableDictionary allocWithZone:zone] init];
  10526. }
  10527. return [_dictionary mutableCopyWithZone:zone];
  10528. }
  10529. // Not really needed, but subscripting is likely common enough it doesn't hurt
  10530. // to ensure it goes directly to the real NSMutableDictionary.
  10531. - (id)objectForKeyedSubscript:(id)key {
  10532. return [_dictionary objectForKeyedSubscript:key];
  10533. }
  10534. // Not really needed, but subscripting is likely common enough it doesn't hurt
  10535. // to ensure it goes directly to the real NSMutableDictionary.
  10536. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  10537. if (_dictionary == nil) {
  10538. _dictionary = [[NSMutableDictionary alloc] init];
  10539. }
  10540. [_dictionary setObject:obj forKeyedSubscript:key];
  10541. if (_autocreator) {
  10542. GPBAutocreatedDictionaryModified(_autocreator, self);
  10543. }
  10544. }
  10545. - (void)enumerateKeysAndObjectsUsingBlock:(void(NS_NOESCAPE ^)(id key, id obj, BOOL *stop))block {
  10546. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  10547. }
  10548. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  10549. usingBlock:(void(NS_NOESCAPE ^)(id key, id obj, BOOL *stop))block {
  10550. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  10551. }
  10552. @end
  10553. #pragma clang diagnostic pop