GPBDictionary.m 428 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBDictionary_PackagePrivate.h"
  31. #import "GPBCodedInputStream_PackagePrivate.h"
  32. #import "GPBCodedOutputStream.h"
  33. #import "GPBDescriptor_PackagePrivate.h"
  34. #import "GPBMessage_PackagePrivate.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. // ------------------------------ NOTE ------------------------------
  37. // At the moment, this is all using NSNumbers in NSDictionaries under
  38. // the hood, but it is all hidden so we can come back and optimize
  39. // with direct CFDictionary usage later. The reason that wasn't
  40. // done yet is needing to support 32bit iOS builds. Otherwise
  41. // it would be pretty simple to store all this data in CFDictionaries
  42. // directly.
  43. // ------------------------------------------------------------------
  44. #define kMapKeyFieldNumber 1
  45. #define kMapValueFieldNumber 2
  46. static BOOL DictDefault_IsValidValue(int32_t value) {
  47. // Anything but the bad value marker is allowed.
  48. return (value != kGPBUnrecognizedEnumeratorValue);
  49. }
  50. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBTYPE_NAME1, GPBTYPE_NAME2)
  51. //%GPB_INLINE size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  52. //% NSCAssert((wireType == GPBType##GPBTYPE_NAME1) || (wireType == GPBType##GPBTYPE_NAME2),
  53. //% @"bad type: %d", wireType);
  54. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  55. //% return GPBCompute##GPBTYPE_NAME1##Size(fieldNum, value);
  56. //% } else { // wireType == GPBType##GPBTYPE_NAME2
  57. //% return GPBCompute##GPBTYPE_NAME2##Size(fieldNum, value);
  58. //% }
  59. //%}
  60. //%
  61. //%GPB_INLINE void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  62. //% NSCAssert((wireType == GPBType##GPBTYPE_NAME1) || (wireType == GPBType##GPBTYPE_NAME2),
  63. //% @"bad type: %d", wireType);
  64. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  65. //% [stream write##GPBTYPE_NAME1##:fieldNum value:value];
  66. //% } else { // wireType == GPBType##GPBTYPE_NAME2
  67. //% [stream write##GPBTYPE_NAME2##:fieldNum value:value];
  68. //% }
  69. //%}
  70. //%
  71. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBTYPE_NAME1, GPBTYPE_NAME2, GPBTYPE_NAME3)
  72. //%GPB_INLINE size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  73. //% NSCAssert((wireType == GPBType##GPBTYPE_NAME1) || (wireType == GPBType##GPBTYPE_NAME2) || (wireType == GPBType##GPBTYPE_NAME3),
  74. //% @"bad type: %d", wireType);
  75. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  76. //% return GPBCompute##GPBTYPE_NAME1##Size(fieldNum, value);
  77. //% } else if (wireType == GPBType##GPBTYPE_NAME2) {
  78. //% return GPBCompute##GPBTYPE_NAME2##Size(fieldNum, value);
  79. //% } else { // wireType == GPBType##GPBTYPE_NAME3
  80. //% return GPBCompute##GPBTYPE_NAME3##Size(fieldNum, value);
  81. //% }
  82. //%}
  83. //%
  84. //%GPB_INLINE void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  85. //% NSCAssert((wireType == GPBType##GPBTYPE_NAME1) || (wireType == GPBType##GPBTYPE_NAME2) || (wireType == GPBType##GPBTYPE_NAME3),
  86. //% @"bad type: %d", wireType);
  87. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  88. //% [stream write##GPBTYPE_NAME1##:fieldNum value:value];
  89. //% } else if (wireType == GPBType##GPBTYPE_NAME2) {
  90. //% [stream write##GPBTYPE_NAME2##:fieldNum value:value];
  91. //% } else { // wireType == GPBType##GPBTYPE_NAME3
  92. //% [stream write##GPBTYPE_NAME3##:fieldNum value:value];
  93. //% }
  94. //%}
  95. //%
  96. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  97. //%GPB_INLINE size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBType wireType) {
  98. //% NSCAssert(wireType == GPBType##VALUE_NAME, @"bad type: %d", wireType);
  99. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  100. //%}
  101. //%
  102. //%GPB_INLINE void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBType wireType) {
  103. //% NSCAssert(wireType == GPBType##VALUE_NAME, @"bad type: %d", wireType);
  104. //% [stream write##VALUE_NAME##:fieldNum value:value];
  105. //%}
  106. //%
  107. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  108. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  109. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  110. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  111. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  112. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  113. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  114. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  115. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  116. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  117. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Data)
  118. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  119. // This block of code is generated, do not edit it directly.
  120. GPB_INLINE size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBType wireType) {
  121. NSCAssert((wireType == GPBTypeInt32) || (wireType == GPBTypeSInt32) || (wireType == GPBTypeSFixed32),
  122. @"bad type: %d", wireType);
  123. if (wireType == GPBTypeInt32) {
  124. return GPBComputeInt32Size(fieldNum, value);
  125. } else if (wireType == GPBTypeSInt32) {
  126. return GPBComputeSInt32Size(fieldNum, value);
  127. } else { // wireType == GPBTypeSFixed32
  128. return GPBComputeSFixed32Size(fieldNum, value);
  129. }
  130. }
  131. GPB_INLINE void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBType wireType) {
  132. NSCAssert((wireType == GPBTypeInt32) || (wireType == GPBTypeSInt32) || (wireType == GPBTypeSFixed32),
  133. @"bad type: %d", wireType);
  134. if (wireType == GPBTypeInt32) {
  135. [stream writeInt32:fieldNum value:value];
  136. } else if (wireType == GPBTypeSInt32) {
  137. [stream writeSInt32:fieldNum value:value];
  138. } else { // wireType == GPBTypeSFixed32
  139. [stream writeSFixed32:fieldNum value:value];
  140. }
  141. }
  142. GPB_INLINE size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBType wireType) {
  143. NSCAssert((wireType == GPBTypeUInt32) || (wireType == GPBTypeFixed32),
  144. @"bad type: %d", wireType);
  145. if (wireType == GPBTypeUInt32) {
  146. return GPBComputeUInt32Size(fieldNum, value);
  147. } else { // wireType == GPBTypeFixed32
  148. return GPBComputeFixed32Size(fieldNum, value);
  149. }
  150. }
  151. GPB_INLINE void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBType wireType) {
  152. NSCAssert((wireType == GPBTypeUInt32) || (wireType == GPBTypeFixed32),
  153. @"bad type: %d", wireType);
  154. if (wireType == GPBTypeUInt32) {
  155. [stream writeUInt32:fieldNum value:value];
  156. } else { // wireType == GPBTypeFixed32
  157. [stream writeFixed32:fieldNum value:value];
  158. }
  159. }
  160. GPB_INLINE size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBType wireType) {
  161. NSCAssert((wireType == GPBTypeInt64) || (wireType == GPBTypeSInt64) || (wireType == GPBTypeSFixed64),
  162. @"bad type: %d", wireType);
  163. if (wireType == GPBTypeInt64) {
  164. return GPBComputeInt64Size(fieldNum, value);
  165. } else if (wireType == GPBTypeSInt64) {
  166. return GPBComputeSInt64Size(fieldNum, value);
  167. } else { // wireType == GPBTypeSFixed64
  168. return GPBComputeSFixed64Size(fieldNum, value);
  169. }
  170. }
  171. GPB_INLINE void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBType wireType) {
  172. NSCAssert((wireType == GPBTypeInt64) || (wireType == GPBTypeSInt64) || (wireType == GPBTypeSFixed64),
  173. @"bad type: %d", wireType);
  174. if (wireType == GPBTypeInt64) {
  175. [stream writeInt64:fieldNum value:value];
  176. } else if (wireType == GPBTypeSInt64) {
  177. [stream writeSInt64:fieldNum value:value];
  178. } else { // wireType == GPBTypeSFixed64
  179. [stream writeSFixed64:fieldNum value:value];
  180. }
  181. }
  182. GPB_INLINE size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBType wireType) {
  183. NSCAssert((wireType == GPBTypeUInt64) || (wireType == GPBTypeFixed64),
  184. @"bad type: %d", wireType);
  185. if (wireType == GPBTypeUInt64) {
  186. return GPBComputeUInt64Size(fieldNum, value);
  187. } else { // wireType == GPBTypeFixed64
  188. return GPBComputeFixed64Size(fieldNum, value);
  189. }
  190. }
  191. GPB_INLINE void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBType wireType) {
  192. NSCAssert((wireType == GPBTypeUInt64) || (wireType == GPBTypeFixed64),
  193. @"bad type: %d", wireType);
  194. if (wireType == GPBTypeUInt64) {
  195. [stream writeUInt64:fieldNum value:value];
  196. } else { // wireType == GPBTypeFixed64
  197. [stream writeFixed64:fieldNum value:value];
  198. }
  199. }
  200. GPB_INLINE size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBType wireType) {
  201. NSCAssert(wireType == GPBTypeBool, @"bad type: %d", wireType);
  202. return GPBComputeBoolSize(fieldNum, value);
  203. }
  204. GPB_INLINE void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBType wireType) {
  205. NSCAssert(wireType == GPBTypeBool, @"bad type: %d", wireType);
  206. [stream writeBool:fieldNum value:value];
  207. }
  208. GPB_INLINE size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBType wireType) {
  209. NSCAssert(wireType == GPBTypeEnum, @"bad type: %d", wireType);
  210. return GPBComputeEnumSize(fieldNum, value);
  211. }
  212. GPB_INLINE void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBType wireType) {
  213. NSCAssert(wireType == GPBTypeEnum, @"bad type: %d", wireType);
  214. [stream writeEnum:fieldNum value:value];
  215. }
  216. GPB_INLINE size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBType wireType) {
  217. NSCAssert(wireType == GPBTypeFloat, @"bad type: %d", wireType);
  218. return GPBComputeFloatSize(fieldNum, value);
  219. }
  220. GPB_INLINE void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBType wireType) {
  221. NSCAssert(wireType == GPBTypeFloat, @"bad type: %d", wireType);
  222. [stream writeFloat:fieldNum value:value];
  223. }
  224. GPB_INLINE size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBType wireType) {
  225. NSCAssert(wireType == GPBTypeDouble, @"bad type: %d", wireType);
  226. return GPBComputeDoubleSize(fieldNum, value);
  227. }
  228. GPB_INLINE void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBType wireType) {
  229. NSCAssert(wireType == GPBTypeDouble, @"bad type: %d", wireType);
  230. [stream writeDouble:fieldNum value:value];
  231. }
  232. GPB_INLINE size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBType wireType) {
  233. NSCAssert(wireType == GPBTypeString, @"bad type: %d", wireType);
  234. return GPBComputeStringSize(fieldNum, value);
  235. }
  236. GPB_INLINE void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBType wireType) {
  237. NSCAssert(wireType == GPBTypeString, @"bad type: %d", wireType);
  238. [stream writeString:fieldNum value:value];
  239. }
  240. GPB_INLINE size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBType wireType) {
  241. NSCAssert((wireType == GPBTypeMessage) || (wireType == GPBTypeString) || (wireType == GPBTypeData),
  242. @"bad type: %d", wireType);
  243. if (wireType == GPBTypeMessage) {
  244. return GPBComputeMessageSize(fieldNum, value);
  245. } else if (wireType == GPBTypeString) {
  246. return GPBComputeStringSize(fieldNum, value);
  247. } else { // wireType == GPBTypeData
  248. return GPBComputeDataSize(fieldNum, value);
  249. }
  250. }
  251. GPB_INLINE void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBType wireType) {
  252. NSCAssert((wireType == GPBTypeMessage) || (wireType == GPBTypeString) || (wireType == GPBTypeData),
  253. @"bad type: %d", wireType);
  254. if (wireType == GPBTypeMessage) {
  255. [stream writeMessage:fieldNum value:value];
  256. } else if (wireType == GPBTypeString) {
  257. [stream writeString:fieldNum value:value];
  258. } else { // wireType == GPBTypeData
  259. [stream writeData:fieldNum value:value];
  260. }
  261. }
  262. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  263. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  264. NSCAssert(field.mapKeyType == GPBTypeString, @"Unexpected key type");
  265. GPBType mapValueType = GPBGetFieldType(field);
  266. __block size_t result = 0;
  267. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  268. #pragma unused(stop)
  269. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  270. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  271. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  272. }];
  273. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  274. result += tagSize * dict.count;
  275. return result;
  276. }
  277. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  278. NSDictionary *dict,
  279. GPBFieldDescriptor *field) {
  280. NSCAssert(field.mapKeyType == GPBTypeString, @"Unexpected key type");
  281. GPBType mapValueType = GPBGetFieldType(field);
  282. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  283. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  284. #pragma unused(stop)
  285. // Write the tag.
  286. [outputStream writeInt32NoTag:tag];
  287. // Write the size of the message.
  288. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  289. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  290. // Write the size and fields.
  291. [outputStream writeInt32NoTag:(int32_t)msgSize];
  292. [outputStream writeString:kMapValueFieldNumber value:obj];
  293. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  294. }];
  295. }
  296. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  297. NSCAssert(field.mapKeyType == GPBTypeString, @"Unexpected key type");
  298. NSCAssert(GPBGetFieldType(field) == GPBTypeMessage, @"Unexpected value type");
  299. for (GPBMessage *msg in [dict objectEnumerator]) {
  300. if (!msg.initialized) {
  301. return NO;
  302. }
  303. }
  304. return YES;
  305. }
  306. // Note: if the type is an object, it the retain pass back to the caller.
  307. static void ReadValue(GPBCodedInputStream *stream,
  308. GPBValue *valueToFill,
  309. GPBType type,
  310. GPBExtensionRegistry *registry,
  311. GPBFieldDescriptor *field) {
  312. switch (type) {
  313. case GPBTypeBool:
  314. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  315. break;
  316. case GPBTypeFixed32:
  317. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  318. break;
  319. case GPBTypeSFixed32:
  320. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  321. break;
  322. case GPBTypeFloat:
  323. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  324. break;
  325. case GPBTypeFixed64:
  326. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  327. break;
  328. case GPBTypeSFixed64:
  329. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  330. break;
  331. case GPBTypeDouble:
  332. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  333. break;
  334. case GPBTypeInt32:
  335. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  336. break;
  337. case GPBTypeInt64:
  338. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  339. break;
  340. case GPBTypeSInt32:
  341. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  342. break;
  343. case GPBTypeSInt64:
  344. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  345. break;
  346. case GPBTypeUInt32:
  347. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  348. break;
  349. case GPBTypeUInt64:
  350. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  351. break;
  352. case GPBTypeData:
  353. [valueToFill->valueData release];
  354. valueToFill->valueData = GPBCodedInputStreamReadRetainedData(&stream->state_);
  355. break;
  356. case GPBTypeString:
  357. [valueToFill->valueString release];
  358. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  359. break;
  360. case GPBTypeMessage: {
  361. GPBMessage *message = [[field.msgClass alloc] init];
  362. [stream readMessage:message extensionRegistry:registry];
  363. [valueToFill->valueMessage release];
  364. valueToFill->valueMessage = message;
  365. break;
  366. }
  367. case GPBTypeGroup:
  368. NSCAssert(NO, @"Can't happen");
  369. break;
  370. case GPBTypeEnum:
  371. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  372. break;
  373. }
  374. }
  375. void GPBDictionaryReadEntry(id mapDictionary,
  376. GPBCodedInputStream *stream,
  377. GPBExtensionRegistry *registry,
  378. GPBFieldDescriptor *field,
  379. GPBMessage *parentMessage) {
  380. GPBType keyType = field.mapKeyType;
  381. GPBType valueType = GPBGetFieldType(field);
  382. GPBValue key;
  383. GPBValue value;
  384. // Zero them (but pick up any enum default for proto2).
  385. key.valueString = value.valueString = nil;
  386. if (valueType == GPBTypeEnum) {
  387. value = field.defaultValue;
  388. }
  389. GPBCodedInputStreamState *state = &stream->state_;
  390. uint32_t keyTag =
  391. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyType, NO));
  392. uint32_t valueTag =
  393. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueType, NO));
  394. BOOL hitError = NO;
  395. while (YES) {
  396. uint32_t tag = GPBCodedInputStreamReadTag(state);
  397. if (tag == keyTag) {
  398. ReadValue(stream, &key, keyType, registry, field);
  399. } else if (tag == valueTag) {
  400. ReadValue(stream, &value, valueType, registry, field);
  401. } else if (tag == 0) {
  402. // zero signals EOF / limit reached
  403. break;
  404. } else { // Unknown
  405. if (![stream skipField:tag]){
  406. hitError = YES;
  407. break;
  408. }
  409. }
  410. }
  411. if (!hitError) {
  412. // Handle the special defaults and/or missing key/value.
  413. if ((keyType == GPBTypeString) && (key.valueString == nil)) {
  414. key.valueString = [@"" retain];
  415. }
  416. if (GPBTypeIsObject(valueType) && value.valueString == nil) {
  417. switch (valueType) {
  418. case GPBTypeString:
  419. value.valueString = [@"" retain];
  420. break;
  421. case GPBTypeData:
  422. value.valueData = [GPBEmptyNSData() retain];
  423. break;
  424. case GPBTypeMessage: {
  425. value.valueMessage = [[field.msgClass alloc] init];
  426. break;
  427. }
  428. default:
  429. // Nothing
  430. break;
  431. }
  432. }
  433. if ((keyType == GPBTypeString) && GPBTypeIsObject(valueType)) {
  434. // mapDictionary is an NSMutableDictionary
  435. [mapDictionary setObject:value.valueString forKey:key.valueString];
  436. } else {
  437. if (valueType == GPBTypeEnum) {
  438. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  439. [field isValidEnumValue:value.valueEnum]) {
  440. [mapDictionary setGPBValue:&value forGPBValueKey:&key];
  441. } else {
  442. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  443. forKey:&key
  444. keyType:keyType];
  445. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  446. }
  447. } else {
  448. [mapDictionary setGPBValue:&value forGPBValueKey:&key];
  449. }
  450. }
  451. }
  452. if (GPBTypeIsObject(keyType)) [key.valueString release];
  453. if (GPBTypeIsObject(valueType)) [value.valueString release];
  454. }
  455. //
  456. // Macros for the common basic cases.
  457. //
  458. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  459. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  460. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  461. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  462. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  463. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  464. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  465. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  466. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  467. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  468. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  469. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  470. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  471. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  472. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  473. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT)
  474. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  475. //%#pragma mark - KEY_NAME -> VALUE_NAME
  476. //%
  477. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  478. //% @package
  479. //% NSMutableDictionary *_dictionary;
  480. //%}
  481. //%
  482. //%+ (instancetype)dictionary {
  483. //% return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  484. //%}
  485. //%
  486. //%+ (instancetype)dictionaryWithValue:(VALUE_TYPE)value
  487. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  488. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  489. //% // on to get the type correct.
  490. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValues:&value
  491. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  492. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  493. //%}
  494. //%
  495. //%+ (instancetype)dictionaryWithValues:(const VALUE_TYPE [])values
  496. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  497. //% count:(NSUInteger)count {
  498. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  499. //% // on to get the type correct.
  500. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValues:values
  501. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  502. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  503. //%}
  504. //%
  505. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  506. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  507. //% // on to get the type correct.
  508. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  509. //%}
  510. //%
  511. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  512. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  513. //%}
  514. //%
  515. //%- (instancetype)init {
  516. //% return [self initWithValues:NULL forKeys:NULL count:0];
  517. //%}
  518. //%
  519. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  520. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  521. //% count:(NSUInteger)count {
  522. //% self = [super init];
  523. //% if (self) {
  524. //% _dictionary = [[NSMutableDictionary alloc] init];
  525. //% if (count && values && keys) {
  526. //% for (NSUInteger i = 0; i < count; ++i) {
  527. //% [_dictionary setObject:WRAPPED##VHELPER(values[i]) forKey:WRAPPED##KHELPER(keys[i])];
  528. //% }
  529. //% }
  530. //% }
  531. //% return self;
  532. //%}
  533. //%
  534. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  535. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  536. //% if (self) {
  537. //% if (dictionary) {
  538. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  539. //% }
  540. //% }
  541. //% return self;
  542. //%}
  543. //%
  544. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  545. //% #pragma unused(numItems)
  546. //% return [self initWithValues:NULL forKeys:NULL count:0];
  547. //%}
  548. //%
  549. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, )
  550. //%
  551. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  552. //%
  553. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, )
  554. //%
  555. //%@end
  556. //%
  557. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  558. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  559. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  560. //%#pragma mark - KEY_NAME -> VALUE_NAME
  561. //%
  562. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  563. //% @package
  564. //% NSMutableDictionary *_dictionary;
  565. //% GPBEnumValidationFunc _validationFunc;
  566. //%}
  567. //%
  568. //%@synthesize validationFunc = _validationFunc;
  569. //%
  570. //%+ (instancetype)dictionary {
  571. //% return [[[self alloc] initWithValidationFunction:NULL
  572. //% rawValues:NULL
  573. //% forKeys:NULL
  574. //% count:0] autorelease];
  575. //%}
  576. //%
  577. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  578. //% return [[[self alloc] initWithValidationFunction:func
  579. //% rawValues:NULL
  580. //% forKeys:NULL
  581. //% count:0] autorelease];
  582. //%}
  583. //%
  584. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  585. //% rawValue:(VALUE_TYPE)rawValue
  586. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  587. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  588. //% // on to get the type correct.
  589. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  590. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  591. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  592. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  593. //%}
  594. //%
  595. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  596. //% rawValues:(const VALUE_TYPE [])rawValues
  597. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  598. //% count:(NSUInteger)count {
  599. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  600. //% // on to get the type correct.
  601. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  602. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  603. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  604. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  605. //%}
  606. //%
  607. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  608. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  609. //% // on to get the type correct.
  610. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  611. //%}
  612. //%
  613. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  614. //% capacity:(NSUInteger)numItems {
  615. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  616. //%}
  617. //%
  618. //%- (instancetype)init {
  619. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  620. //%}
  621. //%
  622. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  623. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  624. //%}
  625. //%
  626. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  627. //% rawValues:(const VALUE_TYPE [])rawValues
  628. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  629. //% count:(NSUInteger)count {
  630. //% self = [super init];
  631. //% if (self) {
  632. //% _dictionary = [[NSMutableDictionary alloc] init];
  633. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  634. //% if (count && rawValues && keys) {
  635. //% for (NSUInteger i = 0; i < count; ++i) {
  636. //% [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  637. //% }
  638. //% }
  639. //% }
  640. //% return self;
  641. //%}
  642. //%
  643. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  644. //% self = [self initWithValidationFunction:dictionary.validationFunc
  645. //% rawValues:NULL
  646. //% forKeys:NULL
  647. //% count:0];
  648. //% if (self) {
  649. //% if (dictionary) {
  650. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  651. //% }
  652. //% }
  653. //% return self;
  654. //%}
  655. //%
  656. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  657. //% capacity:(NSUInteger)numItems {
  658. //% #pragma unused(numItems)
  659. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  660. //%}
  661. //%
  662. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Raw)
  663. //%
  664. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  665. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  666. //% if (wrapped && value) {
  667. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  668. //% if (!_validationFunc(result)) {
  669. //% result = kGPBUnrecognizedEnumeratorValue;
  670. //% }
  671. //% *value = result;
  672. //% }
  673. //% return (wrapped != NULL);
  674. //%}
  675. //%
  676. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  677. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  678. //% if (wrapped && rawValue) {
  679. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  680. //% }
  681. //% return (wrapped != NULL);
  682. //%}
  683. //%
  684. //%- (void)enumerateKeysAndValuesUsingBlock:
  685. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  686. //% GPBEnumValidationFunc func = _validationFunc;
  687. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  688. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  689. //% BOOL *stop) {
  690. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  691. //% if (!func(unwrapped)) {
  692. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  693. //% }
  694. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  695. //% }];
  696. //%}
  697. //%
  698. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Raw)
  699. //%
  700. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  701. //% if (!_validationFunc(value)) {
  702. //% [NSException raise:NSInvalidArgumentException
  703. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  704. //% value];
  705. //% }
  706. //%
  707. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  708. //%}
  709. //%
  710. //%@end
  711. //%
  712. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, ACCESSOR_NAME)
  713. //%- (void)dealloc {
  714. //% [_dictionary release];
  715. //% [super dealloc];
  716. //%}
  717. //%
  718. //%- (instancetype)copyWithZone:(NSZone *)zone {
  719. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  720. //%}
  721. //%
  722. //%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
  723. //% if (self == other) {
  724. //% return YES;
  725. //% }
  726. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  727. //% return NO;
  728. //% }
  729. //% return [_dictionary isEqual:other->_dictionary];
  730. //%}
  731. //%
  732. //%- (NSUInteger)hash {
  733. //% return _dictionary.count;
  734. //%}
  735. //%
  736. //%- (NSString *)description {
  737. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  738. //%}
  739. //%
  740. //%- (NSUInteger)count {
  741. //% return _dictionary.count;
  742. //%}
  743. //%
  744. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##ValuesUsingBlock:
  745. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  746. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  747. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  748. //% BOOL *stop) {
  749. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(aValue), stop);
  750. //% }];
  751. //%}
  752. //%
  753. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  754. //% NSUInteger count = _dictionary.count;
  755. //% if (count == 0) {
  756. //% return 0;
  757. //% }
  758. //%
  759. //% GPBType valueType = GPBGetFieldType(field);
  760. //% GPBType keyType = field.mapKeyType;
  761. //% __block size_t result = 0;
  762. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  763. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  764. //% BOOL *stop) {
  765. //% #pragma unused(stop)
  766. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyType);
  767. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueType);
  768. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  769. //% }];
  770. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  771. //% result += tagSize * count;
  772. //% return result;
  773. //%}
  774. //%
  775. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  776. //% asField:(GPBFieldDescriptor *)field {
  777. //% GPBType valueType = GPBGetFieldType(field);
  778. //% GPBType keyType = field.mapKeyType;
  779. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  780. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  781. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  782. //% BOOL *stop) {
  783. //% #pragma unused(stop)
  784. //% // Write the tag.
  785. //% [outputStream writeInt32NoTag:tag];
  786. //% // Write the size of the message.
  787. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyType);
  788. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueType);
  789. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  790. //% // Write the fields.
  791. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyType);
  792. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueType);
  793. //% }];
  794. //%}
  795. //%
  796. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  797. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  798. //%}
  799. //%
  800. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  801. //% [self enumerateKeysAnd##ACCESSOR_NAME##ValuesUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop) {
  802. //% #pragma unused(stop)
  803. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(value));
  804. //% }];
  805. //%}
  806. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, ACCESSOR_NAME)
  807. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  808. //% if (otherDictionary) {
  809. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  810. //% }
  811. //%}
  812. //%
  813. //%- (void)set##ACCESSOR_NAME##Value:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  814. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  815. //%}
  816. //%
  817. //%- (void)removeValueForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  818. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  819. //%}
  820. //%
  821. //%- (void)removeAll {
  822. //% [_dictionary removeAllObjects];
  823. //%}
  824. //
  825. // Custom Generation for Bool keys
  826. //
  827. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  828. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD)
  829. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  830. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT)
  831. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER)
  832. //%#pragma mark - Bool -> VALUE_NAME
  833. //%
  834. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  835. //% @package
  836. //% VALUE_TYPE _values[2];
  837. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  838. //%
  839. //%+ (instancetype)dictionary {
  840. //% return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  841. //%}
  842. //%
  843. //%+ (instancetype)dictionaryWithValue:(VALUE_TYPE)value
  844. //% forKey:(BOOL)key {
  845. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  846. //% // on to get the type correct.
  847. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithValues:&value
  848. //% VALUE_NAME$S forKeys:&key
  849. //% VALUE_NAME$S count:1] autorelease];
  850. //%}
  851. //%
  852. //%+ (instancetype)dictionaryWithValues:(const VALUE_TYPE [])values
  853. //% forKeys:(const BOOL [])keys
  854. //% count:(NSUInteger)count {
  855. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  856. //% // on to get the type correct.
  857. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithValues:values
  858. //% VALUE_NAME$S forKeys:keys
  859. //% VALUE_NAME$S count:count] autorelease];
  860. //%}
  861. //%
  862. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  863. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  864. //% // on to get the type correct.
  865. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  866. //%}
  867. //%
  868. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  869. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  870. //%}
  871. //%
  872. //%- (instancetype)init {
  873. //% return [self initWithValues:NULL forKeys:NULL count:0];
  874. //%}
  875. //%
  876. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  877. //%
  878. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  879. //% #pragma unused(numItems)
  880. //% return [self initWithValues:NULL forKeys:NULL count:0];
  881. //%}
  882. //%
  883. //%BOOL_DICT_DEALLOC##HELPER()- (instancetype)copyWithZone:(NSZone *)zone {
  884. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  885. //%}
  886. //%
  887. //%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
  888. //% if (self == other) {
  889. //% return YES;
  890. //% }
  891. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  892. //% return NO;
  893. //% }
  894. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
  895. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
  896. //% return NO;
  897. //% }
  898. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
  899. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
  900. //% return NO;
  901. //% }
  902. //% return YES;
  903. //%}
  904. //%
  905. //%- (NSUInteger)hash {
  906. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  907. //%}
  908. //%
  909. //%- (NSString *)description {
  910. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  911. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  912. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  913. //% }
  914. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  915. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  916. //% }
  917. //% [result appendString:@" }"];
  918. //% return result;
  919. //%}
  920. //%
  921. //%- (NSUInteger)count {
  922. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  923. //%}
  924. //%
  925. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  926. //%
  927. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  928. //%
  929. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  930. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  931. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  932. //% }
  933. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  934. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  935. //% }
  936. //%}
  937. //%
  938. //%- (void)enumerateKeysAndValuesUsingBlock:
  939. //% (void (^)(BOOL key, VALUE_TYPE value, BOOL *stop))block {
  940. //% BOOL stop = NO;
  941. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  942. //% block(NO, _values[0], &stop);
  943. //% }
  944. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  945. //% block(YES, _values[1], &stop);
  946. //% }
  947. //%}
  948. //%
  949. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  950. //% GPBType valueType = GPBGetFieldType(field);
  951. //% NSUInteger count = 0;
  952. //% size_t result = 0;
  953. //% for (int i = 0; i < 2; ++i) {
  954. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  955. //% ++count;
  956. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  957. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueType);
  958. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  959. //% }
  960. //% }
  961. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  962. //% result += tagSize * count;
  963. //% return result;
  964. //%}
  965. //%
  966. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  967. //% asField:(GPBFieldDescriptor *)field {
  968. //% GPBType valueType = GPBGetFieldType(field);
  969. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  970. //% for (int i = 0; i < 2; ++i) {
  971. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  972. //% // Write the tag.
  973. //% [outputStream writeInt32NoTag:tag];
  974. //% // Write the size of the message.
  975. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  976. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueType);
  977. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  978. //% // Write the fields.
  979. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  980. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  981. //% }
  982. //% }
  983. //%}
  984. //%
  985. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  986. //%
  987. //%@end
  988. //%
  989. //
  990. // Helpers for PODs
  991. //
  992. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  993. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  994. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  995. //% if (wrapped && value) {
  996. //% *value = UNWRAP##VALUE_NAME(wrapped);
  997. //% }
  998. //% return (wrapped != NULL);
  999. //%}
  1000. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1001. //%@(VALUE)
  1002. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1003. //%[VALUE unsignedIntValue]
  1004. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1005. //%[VALUE intValue]
  1006. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1007. //%[VALUE unsignedLongLongValue]
  1008. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1009. //%[VALUE longLongValue]
  1010. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1011. //%[VALUE boolValue]
  1012. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1013. //%[VALUE floatValue]
  1014. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1015. //%[VALUE doubleValue]
  1016. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1017. //%[VALUE intValue]
  1018. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1019. //%[NSString stringWithFormat:@"%u", VALUE]
  1020. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1021. //%[NSString stringWithFormat:@"%d", VALUE]
  1022. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1023. //%[NSString stringWithFormat:@"%llu", VALUE]
  1024. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1025. //%[NSString stringWithFormat:@"%lld", VALUE]
  1026. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1027. //%(VALUE ? @"true" : @"false")
  1028. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1029. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1030. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1031. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1032. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1033. //%@(VALUE)
  1034. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1035. //%NSNumber *
  1036. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1037. //%VAL1 != VAL2
  1038. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1039. // Empty
  1040. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1041. // Empty
  1042. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1043. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1044. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1045. // Empty
  1046. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1047. // Empty
  1048. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1049. // Empty
  1050. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1051. // Empty
  1052. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1053. // Empty
  1054. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1055. // Empty
  1056. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1057. // Empty
  1058. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1059. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1060. //% forKey:(GPBValue *)key
  1061. //% keyType:(GPBType)keyType {
  1062. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyType);
  1063. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  1064. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1065. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1066. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyType);
  1067. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  1068. //% [outputStream release];
  1069. //% return data;
  1070. //%}
  1071. //%
  1072. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1073. //%value##VALUE_NAME
  1074. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1075. //% BOOL _valueSet[2];
  1076. //%
  1077. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1078. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1079. //% forKeys:(const BOOL [])keys
  1080. //% count:(NSUInteger)count {
  1081. //% self = [super init];
  1082. //% if (self) {
  1083. //% for (NSUInteger i = 0; i < count; ++i) {
  1084. //% int idx = keys[i] ? 1 : 0;
  1085. //% _values[idx] = values[i];
  1086. //% _valueSet[idx] = YES;
  1087. //% }
  1088. //% }
  1089. //% return self;
  1090. //%}
  1091. //%
  1092. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1093. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1094. //% if (self) {
  1095. //% if (dictionary) {
  1096. //% for (int i = 0; i < 2; ++i) {
  1097. //% if (dictionary->_valueSet[i]) {
  1098. //% _values[i] = dictionary->_values[i];
  1099. //% _valueSet[i] = YES;
  1100. //% }
  1101. //% }
  1102. //% }
  1103. //% }
  1104. //% return self;
  1105. //%}
  1106. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1107. // Empty
  1108. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1109. //%BOOL_DICT_HASPOD(IDX, REF)
  1110. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1111. //%REF##_valueSet[IDX]
  1112. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1113. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1114. //% int idx = (key ? 1 : 0);
  1115. //% if (_valueSet[idx]) {
  1116. //% if (value) {
  1117. //% *value = _values[idx];
  1118. //% }
  1119. //% return YES;
  1120. //% }
  1121. //% return NO;
  1122. //%}
  1123. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1124. //%- (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1125. //% int idx = (key->valueBool ? 1 : 0);
  1126. //% _values[idx] = value->value##VALUE_NAME;
  1127. //% _valueSet[idx] = YES;
  1128. //%}
  1129. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1130. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1131. //% if (otherDictionary) {
  1132. //% for (int i = 0; i < 2; ++i) {
  1133. //% if (otherDictionary->_valueSet[i]) {
  1134. //% _valueSet[i] = YES;
  1135. //% _values[i] = otherDictionary->_values[i];
  1136. //% }
  1137. //% }
  1138. //% }
  1139. //%}
  1140. //%
  1141. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1142. //% int idx = (key ? 1 : 0);
  1143. //% _values[idx] = value;
  1144. //% _valueSet[idx] = YES;
  1145. //%}
  1146. //%
  1147. //%- (void)removeValueForKey:(BOOL)aKey {
  1148. //% _valueSet[aKey ? 1 : 0] = NO;
  1149. //%}
  1150. //%
  1151. //%- (void)removeAll {
  1152. //% _valueSet[0] = NO;
  1153. //% _valueSet[1] = NO;
  1154. //%}
  1155. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1156. //%STR_FORMAT_##VALUE_NAME()
  1157. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1158. //%%u
  1159. //%PDDM-DEFINE STR_FORMAT_Int32()
  1160. //%%d
  1161. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1162. //%%llu
  1163. //%PDDM-DEFINE STR_FORMAT_Int64()
  1164. //%%lld
  1165. //%PDDM-DEFINE STR_FORMAT_Bool()
  1166. //%%d
  1167. //%PDDM-DEFINE STR_FORMAT_Float()
  1168. //%%f
  1169. //%PDDM-DEFINE STR_FORMAT_Double()
  1170. //%%lf
  1171. //
  1172. // Helpers for Objects
  1173. //
  1174. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1175. //%- (VALUE_TYPE)valueForKey:(KEY_TYPE)key {
  1176. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1177. //% return result;
  1178. //%}
  1179. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1180. //%VALUE
  1181. //%PDDM-DEFINE UNWRAPString(VALUE)
  1182. //%VALUE
  1183. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1184. //%VALUE
  1185. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1186. //%VALUE
  1187. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1188. //%VALUE
  1189. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1190. //%ENUM_TYPEOBJECT_##TYPE()
  1191. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1192. //%NSString *
  1193. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1194. //%id ##
  1195. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1196. //%![VAL1 isEqual:VAL2]
  1197. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1198. //%- (BOOL)isInitialized {
  1199. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1200. //% if (!msg.initialized) {
  1201. //% return NO;
  1202. //% }
  1203. //% }
  1204. //% return YES;
  1205. //%}
  1206. //%
  1207. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1208. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1209. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1210. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1211. //% GPBMessage *msg,
  1212. //% BOOL *stop) {
  1213. //% #pragma unused(stop)
  1214. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1215. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1216. //% [copiedMsg release];
  1217. //% }];
  1218. //% return newDict;
  1219. //%}
  1220. //%
  1221. //%
  1222. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1223. //%- (BOOL)isInitialized {
  1224. //% if (_values[0] && ![_values[0] isInitialized]) {
  1225. //% return NO;
  1226. //% }
  1227. //% if (_values[1] && ![_values[1] isInitialized]) {
  1228. //% return NO;
  1229. //% }
  1230. //% return YES;
  1231. //%}
  1232. //%
  1233. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1234. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1235. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1236. //% for (int i = 0; i < 2; ++i) {
  1237. //% if (_values[i] != nil) {
  1238. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1239. //% }
  1240. //% }
  1241. //% return newDict;
  1242. //%}
  1243. //%
  1244. //%
  1245. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1246. // Empty
  1247. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1248. //%valueString
  1249. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1250. // Empty
  1251. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1252. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1253. //% forKeys:(const BOOL [])keys
  1254. //% count:(NSUInteger)count {
  1255. //% self = [super init];
  1256. //% if (self) {
  1257. //% for (NSUInteger i = 0; i < count; ++i) {
  1258. //% int idx = keys[i] ? 1 : 0;
  1259. //% [_values[idx] release];
  1260. //% _values[idx] = (VALUE_TYPE)[values[i] retain];
  1261. //% }
  1262. //% }
  1263. //% return self;
  1264. //%}
  1265. //%
  1266. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1267. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1268. //% if (self) {
  1269. //% if (dictionary) {
  1270. //% _values[0] = [dictionary->_values[0] retain];
  1271. //% _values[1] = [dictionary->_values[1] retain];
  1272. //% }
  1273. //% }
  1274. //% return self;
  1275. //%}
  1276. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1277. //%- (void)dealloc {
  1278. //% [_values[0] release];
  1279. //% [_values[1] release];
  1280. //% [super dealloc];
  1281. //%}
  1282. //%
  1283. //%
  1284. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1285. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1286. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1287. //%REF##_values[IDX] != nil
  1288. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1289. //%- (VALUE_TYPE)valueForKey:(BOOL)key {
  1290. //% return _values[key ? 1 : 0];
  1291. //%}
  1292. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1293. //%- (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1294. //% int idx = (key->valueBool ? 1 : 0);
  1295. //% [_values[idx] release];
  1296. //% _values[idx] = [value->valueString retain];
  1297. //%}
  1298. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1299. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1300. //% if (otherDictionary) {
  1301. //% for (int i = 0; i < 2; ++i) {
  1302. //% if (otherDictionary->_values[i] != nil) {
  1303. //% [_values[i] release];
  1304. //% _values[i] = [otherDictionary->_values[i] retain];
  1305. //% }
  1306. //% }
  1307. //% }
  1308. //%}
  1309. //%
  1310. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1311. //% int idx = (key ? 1 : 0);
  1312. //% [_values[idx] release];
  1313. //% _values[idx] = [value retain];
  1314. //%}
  1315. //%
  1316. //%- (void)removeValueForKey:(BOOL)aKey {
  1317. //% int idx = (aKey ? 1 : 0);
  1318. //% [_values[idx] release];
  1319. //% _values[idx] = nil;
  1320. //%}
  1321. //%
  1322. //%- (void)removeAll {
  1323. //% for (int i = 0; i < 2; ++i) {
  1324. //% [_values[i] release];
  1325. //% _values[i] = nil;
  1326. //% }
  1327. //%}
  1328. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1329. //%%@
  1330. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1331. // This block of code is generated, do not edit it directly.
  1332. #pragma mark - UInt32 -> UInt32
  1333. @implementation GPBUInt32UInt32Dictionary {
  1334. @package
  1335. NSMutableDictionary *_dictionary;
  1336. }
  1337. + (instancetype)dictionary {
  1338. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1339. }
  1340. + (instancetype)dictionaryWithValue:(uint32_t)value
  1341. forKey:(uint32_t)key {
  1342. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1343. // on to get the type correct.
  1344. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1345. forKeys:&key
  1346. count:1] autorelease];
  1347. }
  1348. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1349. forKeys:(const uint32_t [])keys
  1350. count:(NSUInteger)count {
  1351. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1352. // on to get the type correct.
  1353. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1354. forKeys:keys
  1355. count:count] autorelease];
  1356. }
  1357. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1358. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1359. // on to get the type correct.
  1360. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1361. }
  1362. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1363. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1364. }
  1365. - (instancetype)init {
  1366. return [self initWithValues:NULL forKeys:NULL count:0];
  1367. }
  1368. - (instancetype)initWithValues:(const uint32_t [])values
  1369. forKeys:(const uint32_t [])keys
  1370. count:(NSUInteger)count {
  1371. self = [super init];
  1372. if (self) {
  1373. _dictionary = [[NSMutableDictionary alloc] init];
  1374. if (count && values && keys) {
  1375. for (NSUInteger i = 0; i < count; ++i) {
  1376. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1377. }
  1378. }
  1379. }
  1380. return self;
  1381. }
  1382. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1383. self = [self initWithValues:NULL forKeys:NULL count:0];
  1384. if (self) {
  1385. if (dictionary) {
  1386. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1387. }
  1388. }
  1389. return self;
  1390. }
  1391. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1392. #pragma unused(numItems)
  1393. return [self initWithValues:NULL forKeys:NULL count:0];
  1394. }
  1395. - (void)dealloc {
  1396. [_dictionary release];
  1397. [super dealloc];
  1398. }
  1399. - (instancetype)copyWithZone:(NSZone *)zone {
  1400. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1401. }
  1402. - (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
  1403. if (self == other) {
  1404. return YES;
  1405. }
  1406. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1407. return NO;
  1408. }
  1409. return [_dictionary isEqual:other->_dictionary];
  1410. }
  1411. - (NSUInteger)hash {
  1412. return _dictionary.count;
  1413. }
  1414. - (NSString *)description {
  1415. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1416. }
  1417. - (NSUInteger)count {
  1418. return _dictionary.count;
  1419. }
  1420. - (void)enumerateKeysAndValuesUsingBlock:
  1421. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1422. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1423. NSNumber *aValue,
  1424. BOOL *stop) {
  1425. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1426. }];
  1427. }
  1428. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1429. NSUInteger count = _dictionary.count;
  1430. if (count == 0) {
  1431. return 0;
  1432. }
  1433. GPBType valueType = GPBGetFieldType(field);
  1434. GPBType keyType = field.mapKeyType;
  1435. __block size_t result = 0;
  1436. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1437. NSNumber *aValue,
  1438. BOOL *stop) {
  1439. #pragma unused(stop)
  1440. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1441. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  1442. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1443. }];
  1444. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1445. result += tagSize * count;
  1446. return result;
  1447. }
  1448. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1449. asField:(GPBFieldDescriptor *)field {
  1450. GPBType valueType = GPBGetFieldType(field);
  1451. GPBType keyType = field.mapKeyType;
  1452. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1453. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1454. NSNumber *aValue,
  1455. BOOL *stop) {
  1456. #pragma unused(stop)
  1457. // Write the tag.
  1458. [outputStream writeInt32NoTag:tag];
  1459. // Write the size of the message.
  1460. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1461. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  1462. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1463. // Write the fields.
  1464. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1465. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  1466. }];
  1467. }
  1468. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1469. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1470. }
  1471. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1472. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1473. #pragma unused(stop)
  1474. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1475. }];
  1476. }
  1477. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1478. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1479. if (wrapped && value) {
  1480. *value = [wrapped unsignedIntValue];
  1481. }
  1482. return (wrapped != NULL);
  1483. }
  1484. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1485. if (otherDictionary) {
  1486. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1487. }
  1488. }
  1489. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1490. [_dictionary setObject:@(value) forKey:@(key)];
  1491. }
  1492. - (void)removeValueForKey:(uint32_t)aKey {
  1493. [_dictionary removeObjectForKey:@(aKey)];
  1494. }
  1495. - (void)removeAll {
  1496. [_dictionary removeAllObjects];
  1497. }
  1498. @end
  1499. #pragma mark - UInt32 -> Int32
  1500. @implementation GPBUInt32Int32Dictionary {
  1501. @package
  1502. NSMutableDictionary *_dictionary;
  1503. }
  1504. + (instancetype)dictionary {
  1505. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1506. }
  1507. + (instancetype)dictionaryWithValue:(int32_t)value
  1508. forKey:(uint32_t)key {
  1509. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1510. // on to get the type correct.
  1511. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1512. forKeys:&key
  1513. count:1] autorelease];
  1514. }
  1515. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1516. forKeys:(const uint32_t [])keys
  1517. count:(NSUInteger)count {
  1518. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1519. // on to get the type correct.
  1520. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1521. forKeys:keys
  1522. count:count] autorelease];
  1523. }
  1524. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1525. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1526. // on to get the type correct.
  1527. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1528. }
  1529. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1530. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1531. }
  1532. - (instancetype)init {
  1533. return [self initWithValues:NULL forKeys:NULL count:0];
  1534. }
  1535. - (instancetype)initWithValues:(const int32_t [])values
  1536. forKeys:(const uint32_t [])keys
  1537. count:(NSUInteger)count {
  1538. self = [super init];
  1539. if (self) {
  1540. _dictionary = [[NSMutableDictionary alloc] init];
  1541. if (count && values && keys) {
  1542. for (NSUInteger i = 0; i < count; ++i) {
  1543. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1544. }
  1545. }
  1546. }
  1547. return self;
  1548. }
  1549. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1550. self = [self initWithValues:NULL forKeys:NULL count:0];
  1551. if (self) {
  1552. if (dictionary) {
  1553. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1554. }
  1555. }
  1556. return self;
  1557. }
  1558. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1559. #pragma unused(numItems)
  1560. return [self initWithValues:NULL forKeys:NULL count:0];
  1561. }
  1562. - (void)dealloc {
  1563. [_dictionary release];
  1564. [super dealloc];
  1565. }
  1566. - (instancetype)copyWithZone:(NSZone *)zone {
  1567. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1568. }
  1569. - (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
  1570. if (self == other) {
  1571. return YES;
  1572. }
  1573. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1574. return NO;
  1575. }
  1576. return [_dictionary isEqual:other->_dictionary];
  1577. }
  1578. - (NSUInteger)hash {
  1579. return _dictionary.count;
  1580. }
  1581. - (NSString *)description {
  1582. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1583. }
  1584. - (NSUInteger)count {
  1585. return _dictionary.count;
  1586. }
  1587. - (void)enumerateKeysAndValuesUsingBlock:
  1588. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1589. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1590. NSNumber *aValue,
  1591. BOOL *stop) {
  1592. block([aKey unsignedIntValue], [aValue intValue], stop);
  1593. }];
  1594. }
  1595. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1596. NSUInteger count = _dictionary.count;
  1597. if (count == 0) {
  1598. return 0;
  1599. }
  1600. GPBType valueType = GPBGetFieldType(field);
  1601. GPBType keyType = field.mapKeyType;
  1602. __block size_t result = 0;
  1603. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1604. NSNumber *aValue,
  1605. BOOL *stop) {
  1606. #pragma unused(stop)
  1607. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1608. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  1609. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1610. }];
  1611. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1612. result += tagSize * count;
  1613. return result;
  1614. }
  1615. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1616. asField:(GPBFieldDescriptor *)field {
  1617. GPBType valueType = GPBGetFieldType(field);
  1618. GPBType keyType = field.mapKeyType;
  1619. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1620. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1621. NSNumber *aValue,
  1622. BOOL *stop) {
  1623. #pragma unused(stop)
  1624. // Write the tag.
  1625. [outputStream writeInt32NoTag:tag];
  1626. // Write the size of the message.
  1627. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1628. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  1629. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1630. // Write the fields.
  1631. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1632. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  1633. }];
  1634. }
  1635. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1636. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1637. }
  1638. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1639. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1640. #pragma unused(stop)
  1641. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1642. }];
  1643. }
  1644. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1645. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1646. if (wrapped && value) {
  1647. *value = [wrapped intValue];
  1648. }
  1649. return (wrapped != NULL);
  1650. }
  1651. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1652. if (otherDictionary) {
  1653. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1654. }
  1655. }
  1656. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1657. [_dictionary setObject:@(value) forKey:@(key)];
  1658. }
  1659. - (void)removeValueForKey:(uint32_t)aKey {
  1660. [_dictionary removeObjectForKey:@(aKey)];
  1661. }
  1662. - (void)removeAll {
  1663. [_dictionary removeAllObjects];
  1664. }
  1665. @end
  1666. #pragma mark - UInt32 -> UInt64
  1667. @implementation GPBUInt32UInt64Dictionary {
  1668. @package
  1669. NSMutableDictionary *_dictionary;
  1670. }
  1671. + (instancetype)dictionary {
  1672. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1673. }
  1674. + (instancetype)dictionaryWithValue:(uint64_t)value
  1675. forKey:(uint32_t)key {
  1676. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1677. // on to get the type correct.
  1678. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1679. forKeys:&key
  1680. count:1] autorelease];
  1681. }
  1682. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1683. forKeys:(const uint32_t [])keys
  1684. count:(NSUInteger)count {
  1685. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1686. // on to get the type correct.
  1687. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1688. forKeys:keys
  1689. count:count] autorelease];
  1690. }
  1691. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1692. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1693. // on to get the type correct.
  1694. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1695. }
  1696. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1697. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1698. }
  1699. - (instancetype)init {
  1700. return [self initWithValues:NULL forKeys:NULL count:0];
  1701. }
  1702. - (instancetype)initWithValues:(const uint64_t [])values
  1703. forKeys:(const uint32_t [])keys
  1704. count:(NSUInteger)count {
  1705. self = [super init];
  1706. if (self) {
  1707. _dictionary = [[NSMutableDictionary alloc] init];
  1708. if (count && values && keys) {
  1709. for (NSUInteger i = 0; i < count; ++i) {
  1710. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1711. }
  1712. }
  1713. }
  1714. return self;
  1715. }
  1716. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1717. self = [self initWithValues:NULL forKeys:NULL count:0];
  1718. if (self) {
  1719. if (dictionary) {
  1720. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1721. }
  1722. }
  1723. return self;
  1724. }
  1725. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1726. #pragma unused(numItems)
  1727. return [self initWithValues:NULL forKeys:NULL count:0];
  1728. }
  1729. - (void)dealloc {
  1730. [_dictionary release];
  1731. [super dealloc];
  1732. }
  1733. - (instancetype)copyWithZone:(NSZone *)zone {
  1734. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1735. }
  1736. - (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
  1737. if (self == other) {
  1738. return YES;
  1739. }
  1740. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1741. return NO;
  1742. }
  1743. return [_dictionary isEqual:other->_dictionary];
  1744. }
  1745. - (NSUInteger)hash {
  1746. return _dictionary.count;
  1747. }
  1748. - (NSString *)description {
  1749. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1750. }
  1751. - (NSUInteger)count {
  1752. return _dictionary.count;
  1753. }
  1754. - (void)enumerateKeysAndValuesUsingBlock:
  1755. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1756. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1757. NSNumber *aValue,
  1758. BOOL *stop) {
  1759. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1760. }];
  1761. }
  1762. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1763. NSUInteger count = _dictionary.count;
  1764. if (count == 0) {
  1765. return 0;
  1766. }
  1767. GPBType valueType = GPBGetFieldType(field);
  1768. GPBType keyType = field.mapKeyType;
  1769. __block size_t result = 0;
  1770. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1771. NSNumber *aValue,
  1772. BOOL *stop) {
  1773. #pragma unused(stop)
  1774. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1775. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  1776. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1777. }];
  1778. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1779. result += tagSize * count;
  1780. return result;
  1781. }
  1782. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1783. asField:(GPBFieldDescriptor *)field {
  1784. GPBType valueType = GPBGetFieldType(field);
  1785. GPBType keyType = field.mapKeyType;
  1786. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1787. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1788. NSNumber *aValue,
  1789. BOOL *stop) {
  1790. #pragma unused(stop)
  1791. // Write the tag.
  1792. [outputStream writeInt32NoTag:tag];
  1793. // Write the size of the message.
  1794. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1795. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  1796. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1797. // Write the fields.
  1798. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1799. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  1800. }];
  1801. }
  1802. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1803. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1804. }
  1805. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1806. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1807. #pragma unused(stop)
  1808. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1809. }];
  1810. }
  1811. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1812. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1813. if (wrapped && value) {
  1814. *value = [wrapped unsignedLongLongValue];
  1815. }
  1816. return (wrapped != NULL);
  1817. }
  1818. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1819. if (otherDictionary) {
  1820. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1821. }
  1822. }
  1823. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1824. [_dictionary setObject:@(value) forKey:@(key)];
  1825. }
  1826. - (void)removeValueForKey:(uint32_t)aKey {
  1827. [_dictionary removeObjectForKey:@(aKey)];
  1828. }
  1829. - (void)removeAll {
  1830. [_dictionary removeAllObjects];
  1831. }
  1832. @end
  1833. #pragma mark - UInt32 -> Int64
  1834. @implementation GPBUInt32Int64Dictionary {
  1835. @package
  1836. NSMutableDictionary *_dictionary;
  1837. }
  1838. + (instancetype)dictionary {
  1839. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1840. }
  1841. + (instancetype)dictionaryWithValue:(int64_t)value
  1842. forKey:(uint32_t)key {
  1843. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1844. // on to get the type correct.
  1845. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  1846. forKeys:&key
  1847. count:1] autorelease];
  1848. }
  1849. + (instancetype)dictionaryWithValues:(const int64_t [])values
  1850. forKeys:(const uint32_t [])keys
  1851. count:(NSUInteger)count {
  1852. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1853. // on to get the type correct.
  1854. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  1855. forKeys:keys
  1856. count:count] autorelease];
  1857. }
  1858. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1859. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1860. // on to get the type correct.
  1861. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1862. }
  1863. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1864. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1865. }
  1866. - (instancetype)init {
  1867. return [self initWithValues:NULL forKeys:NULL count:0];
  1868. }
  1869. - (instancetype)initWithValues:(const int64_t [])values
  1870. forKeys:(const uint32_t [])keys
  1871. count:(NSUInteger)count {
  1872. self = [super init];
  1873. if (self) {
  1874. _dictionary = [[NSMutableDictionary alloc] init];
  1875. if (count && values && keys) {
  1876. for (NSUInteger i = 0; i < count; ++i) {
  1877. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1878. }
  1879. }
  1880. }
  1881. return self;
  1882. }
  1883. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1884. self = [self initWithValues:NULL forKeys:NULL count:0];
  1885. if (self) {
  1886. if (dictionary) {
  1887. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1888. }
  1889. }
  1890. return self;
  1891. }
  1892. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1893. #pragma unused(numItems)
  1894. return [self initWithValues:NULL forKeys:NULL count:0];
  1895. }
  1896. - (void)dealloc {
  1897. [_dictionary release];
  1898. [super dealloc];
  1899. }
  1900. - (instancetype)copyWithZone:(NSZone *)zone {
  1901. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  1902. }
  1903. - (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
  1904. if (self == other) {
  1905. return YES;
  1906. }
  1907. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  1908. return NO;
  1909. }
  1910. return [_dictionary isEqual:other->_dictionary];
  1911. }
  1912. - (NSUInteger)hash {
  1913. return _dictionary.count;
  1914. }
  1915. - (NSString *)description {
  1916. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1917. }
  1918. - (NSUInteger)count {
  1919. return _dictionary.count;
  1920. }
  1921. - (void)enumerateKeysAndValuesUsingBlock:
  1922. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  1923. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1924. NSNumber *aValue,
  1925. BOOL *stop) {
  1926. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  1927. }];
  1928. }
  1929. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1930. NSUInteger count = _dictionary.count;
  1931. if (count == 0) {
  1932. return 0;
  1933. }
  1934. GPBType valueType = GPBGetFieldType(field);
  1935. GPBType keyType = field.mapKeyType;
  1936. __block size_t result = 0;
  1937. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1938. NSNumber *aValue,
  1939. BOOL *stop) {
  1940. #pragma unused(stop)
  1941. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1942. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  1943. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1944. }];
  1945. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1946. result += tagSize * count;
  1947. return result;
  1948. }
  1949. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1950. asField:(GPBFieldDescriptor *)field {
  1951. GPBType valueType = GPBGetFieldType(field);
  1952. GPBType keyType = field.mapKeyType;
  1953. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1954. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1955. NSNumber *aValue,
  1956. BOOL *stop) {
  1957. #pragma unused(stop)
  1958. // Write the tag.
  1959. [outputStream writeInt32NoTag:tag];
  1960. // Write the size of the message.
  1961. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1962. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  1963. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1964. // Write the fields.
  1965. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1966. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  1967. }];
  1968. }
  1969. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1970. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  1971. }
  1972. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1973. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  1974. #pragma unused(stop)
  1975. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  1976. }];
  1977. }
  1978. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  1979. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1980. if (wrapped && value) {
  1981. *value = [wrapped longLongValue];
  1982. }
  1983. return (wrapped != NULL);
  1984. }
  1985. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  1986. if (otherDictionary) {
  1987. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1988. }
  1989. }
  1990. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  1991. [_dictionary setObject:@(value) forKey:@(key)];
  1992. }
  1993. - (void)removeValueForKey:(uint32_t)aKey {
  1994. [_dictionary removeObjectForKey:@(aKey)];
  1995. }
  1996. - (void)removeAll {
  1997. [_dictionary removeAllObjects];
  1998. }
  1999. @end
  2000. #pragma mark - UInt32 -> Bool
  2001. @implementation GPBUInt32BoolDictionary {
  2002. @package
  2003. NSMutableDictionary *_dictionary;
  2004. }
  2005. + (instancetype)dictionary {
  2006. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2007. }
  2008. + (instancetype)dictionaryWithValue:(BOOL)value
  2009. forKey:(uint32_t)key {
  2010. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2011. // on to get the type correct.
  2012. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2013. forKeys:&key
  2014. count:1] autorelease];
  2015. }
  2016. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2017. forKeys:(const uint32_t [])keys
  2018. count:(NSUInteger)count {
  2019. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2020. // on to get the type correct.
  2021. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2022. forKeys:keys
  2023. count:count] autorelease];
  2024. }
  2025. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2026. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2027. // on to get the type correct.
  2028. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2029. }
  2030. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2031. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2032. }
  2033. - (instancetype)init {
  2034. return [self initWithValues:NULL forKeys:NULL count:0];
  2035. }
  2036. - (instancetype)initWithValues:(const BOOL [])values
  2037. forKeys:(const uint32_t [])keys
  2038. count:(NSUInteger)count {
  2039. self = [super init];
  2040. if (self) {
  2041. _dictionary = [[NSMutableDictionary alloc] init];
  2042. if (count && values && keys) {
  2043. for (NSUInteger i = 0; i < count; ++i) {
  2044. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2045. }
  2046. }
  2047. }
  2048. return self;
  2049. }
  2050. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2051. self = [self initWithValues:NULL forKeys:NULL count:0];
  2052. if (self) {
  2053. if (dictionary) {
  2054. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2055. }
  2056. }
  2057. return self;
  2058. }
  2059. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2060. #pragma unused(numItems)
  2061. return [self initWithValues:NULL forKeys:NULL count:0];
  2062. }
  2063. - (void)dealloc {
  2064. [_dictionary release];
  2065. [super dealloc];
  2066. }
  2067. - (instancetype)copyWithZone:(NSZone *)zone {
  2068. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2069. }
  2070. - (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
  2071. if (self == other) {
  2072. return YES;
  2073. }
  2074. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2075. return NO;
  2076. }
  2077. return [_dictionary isEqual:other->_dictionary];
  2078. }
  2079. - (NSUInteger)hash {
  2080. return _dictionary.count;
  2081. }
  2082. - (NSString *)description {
  2083. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2084. }
  2085. - (NSUInteger)count {
  2086. return _dictionary.count;
  2087. }
  2088. - (void)enumerateKeysAndValuesUsingBlock:
  2089. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2090. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2091. NSNumber *aValue,
  2092. BOOL *stop) {
  2093. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2094. }];
  2095. }
  2096. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2097. NSUInteger count = _dictionary.count;
  2098. if (count == 0) {
  2099. return 0;
  2100. }
  2101. GPBType valueType = GPBGetFieldType(field);
  2102. GPBType keyType = field.mapKeyType;
  2103. __block size_t result = 0;
  2104. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2105. NSNumber *aValue,
  2106. BOOL *stop) {
  2107. #pragma unused(stop)
  2108. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2109. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  2110. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2111. }];
  2112. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2113. result += tagSize * count;
  2114. return result;
  2115. }
  2116. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2117. asField:(GPBFieldDescriptor *)field {
  2118. GPBType valueType = GPBGetFieldType(field);
  2119. GPBType keyType = field.mapKeyType;
  2120. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2121. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2122. NSNumber *aValue,
  2123. BOOL *stop) {
  2124. #pragma unused(stop)
  2125. // Write the tag.
  2126. [outputStream writeInt32NoTag:tag];
  2127. // Write the size of the message.
  2128. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2129. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  2130. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2131. // Write the fields.
  2132. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2133. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  2134. }];
  2135. }
  2136. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2137. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2138. }
  2139. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2140. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2141. #pragma unused(stop)
  2142. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2143. }];
  2144. }
  2145. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2146. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2147. if (wrapped && value) {
  2148. *value = [wrapped boolValue];
  2149. }
  2150. return (wrapped != NULL);
  2151. }
  2152. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2153. if (otherDictionary) {
  2154. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2155. }
  2156. }
  2157. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2158. [_dictionary setObject:@(value) forKey:@(key)];
  2159. }
  2160. - (void)removeValueForKey:(uint32_t)aKey {
  2161. [_dictionary removeObjectForKey:@(aKey)];
  2162. }
  2163. - (void)removeAll {
  2164. [_dictionary removeAllObjects];
  2165. }
  2166. @end
  2167. #pragma mark - UInt32 -> Float
  2168. @implementation GPBUInt32FloatDictionary {
  2169. @package
  2170. NSMutableDictionary *_dictionary;
  2171. }
  2172. + (instancetype)dictionary {
  2173. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2174. }
  2175. + (instancetype)dictionaryWithValue:(float)value
  2176. forKey:(uint32_t)key {
  2177. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2178. // on to get the type correct.
  2179. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2180. forKeys:&key
  2181. count:1] autorelease];
  2182. }
  2183. + (instancetype)dictionaryWithValues:(const float [])values
  2184. forKeys:(const uint32_t [])keys
  2185. count:(NSUInteger)count {
  2186. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2187. // on to get the type correct.
  2188. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2189. forKeys:keys
  2190. count:count] autorelease];
  2191. }
  2192. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2193. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2194. // on to get the type correct.
  2195. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2196. }
  2197. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2198. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2199. }
  2200. - (instancetype)init {
  2201. return [self initWithValues:NULL forKeys:NULL count:0];
  2202. }
  2203. - (instancetype)initWithValues:(const float [])values
  2204. forKeys:(const uint32_t [])keys
  2205. count:(NSUInteger)count {
  2206. self = [super init];
  2207. if (self) {
  2208. _dictionary = [[NSMutableDictionary alloc] init];
  2209. if (count && values && keys) {
  2210. for (NSUInteger i = 0; i < count; ++i) {
  2211. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2212. }
  2213. }
  2214. }
  2215. return self;
  2216. }
  2217. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2218. self = [self initWithValues:NULL forKeys:NULL count:0];
  2219. if (self) {
  2220. if (dictionary) {
  2221. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2222. }
  2223. }
  2224. return self;
  2225. }
  2226. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2227. #pragma unused(numItems)
  2228. return [self initWithValues:NULL forKeys:NULL count:0];
  2229. }
  2230. - (void)dealloc {
  2231. [_dictionary release];
  2232. [super dealloc];
  2233. }
  2234. - (instancetype)copyWithZone:(NSZone *)zone {
  2235. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2236. }
  2237. - (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
  2238. if (self == other) {
  2239. return YES;
  2240. }
  2241. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2242. return NO;
  2243. }
  2244. return [_dictionary isEqual:other->_dictionary];
  2245. }
  2246. - (NSUInteger)hash {
  2247. return _dictionary.count;
  2248. }
  2249. - (NSString *)description {
  2250. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2251. }
  2252. - (NSUInteger)count {
  2253. return _dictionary.count;
  2254. }
  2255. - (void)enumerateKeysAndValuesUsingBlock:
  2256. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2258. NSNumber *aValue,
  2259. BOOL *stop) {
  2260. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2261. }];
  2262. }
  2263. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2264. NSUInteger count = _dictionary.count;
  2265. if (count == 0) {
  2266. return 0;
  2267. }
  2268. GPBType valueType = GPBGetFieldType(field);
  2269. GPBType keyType = field.mapKeyType;
  2270. __block size_t result = 0;
  2271. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2272. NSNumber *aValue,
  2273. BOOL *stop) {
  2274. #pragma unused(stop)
  2275. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2276. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  2277. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2278. }];
  2279. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2280. result += tagSize * count;
  2281. return result;
  2282. }
  2283. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2284. asField:(GPBFieldDescriptor *)field {
  2285. GPBType valueType = GPBGetFieldType(field);
  2286. GPBType keyType = field.mapKeyType;
  2287. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2288. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2289. NSNumber *aValue,
  2290. BOOL *stop) {
  2291. #pragma unused(stop)
  2292. // Write the tag.
  2293. [outputStream writeInt32NoTag:tag];
  2294. // Write the size of the message.
  2295. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2296. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  2297. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2298. // Write the fields.
  2299. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2300. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  2301. }];
  2302. }
  2303. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2304. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2305. }
  2306. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2307. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2308. #pragma unused(stop)
  2309. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2310. }];
  2311. }
  2312. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2313. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2314. if (wrapped && value) {
  2315. *value = [wrapped floatValue];
  2316. }
  2317. return (wrapped != NULL);
  2318. }
  2319. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2320. if (otherDictionary) {
  2321. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2322. }
  2323. }
  2324. - (void)setValue:(float)value forKey:(uint32_t)key {
  2325. [_dictionary setObject:@(value) forKey:@(key)];
  2326. }
  2327. - (void)removeValueForKey:(uint32_t)aKey {
  2328. [_dictionary removeObjectForKey:@(aKey)];
  2329. }
  2330. - (void)removeAll {
  2331. [_dictionary removeAllObjects];
  2332. }
  2333. @end
  2334. #pragma mark - UInt32 -> Double
  2335. @implementation GPBUInt32DoubleDictionary {
  2336. @package
  2337. NSMutableDictionary *_dictionary;
  2338. }
  2339. + (instancetype)dictionary {
  2340. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2341. }
  2342. + (instancetype)dictionaryWithValue:(double)value
  2343. forKey:(uint32_t)key {
  2344. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2345. // on to get the type correct.
  2346. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2347. forKeys:&key
  2348. count:1] autorelease];
  2349. }
  2350. + (instancetype)dictionaryWithValues:(const double [])values
  2351. forKeys:(const uint32_t [])keys
  2352. count:(NSUInteger)count {
  2353. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2354. // on to get the type correct.
  2355. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2356. forKeys:keys
  2357. count:count] autorelease];
  2358. }
  2359. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2360. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2361. // on to get the type correct.
  2362. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2363. }
  2364. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2365. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2366. }
  2367. - (instancetype)init {
  2368. return [self initWithValues:NULL forKeys:NULL count:0];
  2369. }
  2370. - (instancetype)initWithValues:(const double [])values
  2371. forKeys:(const uint32_t [])keys
  2372. count:(NSUInteger)count {
  2373. self = [super init];
  2374. if (self) {
  2375. _dictionary = [[NSMutableDictionary alloc] init];
  2376. if (count && values && keys) {
  2377. for (NSUInteger i = 0; i < count; ++i) {
  2378. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2379. }
  2380. }
  2381. }
  2382. return self;
  2383. }
  2384. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2385. self = [self initWithValues:NULL forKeys:NULL count:0];
  2386. if (self) {
  2387. if (dictionary) {
  2388. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2389. }
  2390. }
  2391. return self;
  2392. }
  2393. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2394. #pragma unused(numItems)
  2395. return [self initWithValues:NULL forKeys:NULL count:0];
  2396. }
  2397. - (void)dealloc {
  2398. [_dictionary release];
  2399. [super dealloc];
  2400. }
  2401. - (instancetype)copyWithZone:(NSZone *)zone {
  2402. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2403. }
  2404. - (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
  2405. if (self == other) {
  2406. return YES;
  2407. }
  2408. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2409. return NO;
  2410. }
  2411. return [_dictionary isEqual:other->_dictionary];
  2412. }
  2413. - (NSUInteger)hash {
  2414. return _dictionary.count;
  2415. }
  2416. - (NSString *)description {
  2417. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2418. }
  2419. - (NSUInteger)count {
  2420. return _dictionary.count;
  2421. }
  2422. - (void)enumerateKeysAndValuesUsingBlock:
  2423. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2424. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2425. NSNumber *aValue,
  2426. BOOL *stop) {
  2427. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2428. }];
  2429. }
  2430. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2431. NSUInteger count = _dictionary.count;
  2432. if (count == 0) {
  2433. return 0;
  2434. }
  2435. GPBType valueType = GPBGetFieldType(field);
  2436. GPBType keyType = field.mapKeyType;
  2437. __block size_t result = 0;
  2438. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2439. NSNumber *aValue,
  2440. BOOL *stop) {
  2441. #pragma unused(stop)
  2442. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2443. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  2444. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2445. }];
  2446. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2447. result += tagSize * count;
  2448. return result;
  2449. }
  2450. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2451. asField:(GPBFieldDescriptor *)field {
  2452. GPBType valueType = GPBGetFieldType(field);
  2453. GPBType keyType = field.mapKeyType;
  2454. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2455. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2456. NSNumber *aValue,
  2457. BOOL *stop) {
  2458. #pragma unused(stop)
  2459. // Write the tag.
  2460. [outputStream writeInt32NoTag:tag];
  2461. // Write the size of the message.
  2462. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2463. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  2464. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2465. // Write the fields.
  2466. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2467. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  2468. }];
  2469. }
  2470. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2471. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2472. }
  2473. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2474. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2475. #pragma unused(stop)
  2476. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2477. }];
  2478. }
  2479. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2480. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2481. if (wrapped && value) {
  2482. *value = [wrapped doubleValue];
  2483. }
  2484. return (wrapped != NULL);
  2485. }
  2486. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2487. if (otherDictionary) {
  2488. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2489. }
  2490. }
  2491. - (void)setValue:(double)value forKey:(uint32_t)key {
  2492. [_dictionary setObject:@(value) forKey:@(key)];
  2493. }
  2494. - (void)removeValueForKey:(uint32_t)aKey {
  2495. [_dictionary removeObjectForKey:@(aKey)];
  2496. }
  2497. - (void)removeAll {
  2498. [_dictionary removeAllObjects];
  2499. }
  2500. @end
  2501. #pragma mark - UInt32 -> Enum
  2502. @implementation GPBUInt32EnumDictionary {
  2503. @package
  2504. NSMutableDictionary *_dictionary;
  2505. GPBEnumValidationFunc _validationFunc;
  2506. }
  2507. @synthesize validationFunc = _validationFunc;
  2508. + (instancetype)dictionary {
  2509. return [[[self alloc] initWithValidationFunction:NULL
  2510. rawValues:NULL
  2511. forKeys:NULL
  2512. count:0] autorelease];
  2513. }
  2514. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2515. return [[[self alloc] initWithValidationFunction:func
  2516. rawValues:NULL
  2517. forKeys:NULL
  2518. count:0] autorelease];
  2519. }
  2520. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2521. rawValue:(int32_t)rawValue
  2522. forKey:(uint32_t)key {
  2523. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2524. // on to get the type correct.
  2525. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2526. rawValues:&rawValue
  2527. forKeys:&key
  2528. count:1] autorelease];
  2529. }
  2530. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2531. rawValues:(const int32_t [])rawValues
  2532. forKeys:(const uint32_t [])keys
  2533. count:(NSUInteger)count {
  2534. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2535. // on to get the type correct.
  2536. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2537. rawValues:rawValues
  2538. forKeys:keys
  2539. count:count] autorelease];
  2540. }
  2541. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2542. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2543. // on to get the type correct.
  2544. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2545. }
  2546. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2547. capacity:(NSUInteger)numItems {
  2548. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2549. }
  2550. - (instancetype)init {
  2551. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2552. }
  2553. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2554. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2555. }
  2556. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2557. rawValues:(const int32_t [])rawValues
  2558. forKeys:(const uint32_t [])keys
  2559. count:(NSUInteger)count {
  2560. self = [super init];
  2561. if (self) {
  2562. _dictionary = [[NSMutableDictionary alloc] init];
  2563. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2564. if (count && rawValues && keys) {
  2565. for (NSUInteger i = 0; i < count; ++i) {
  2566. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2567. }
  2568. }
  2569. }
  2570. return self;
  2571. }
  2572. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2573. self = [self initWithValidationFunction:dictionary.validationFunc
  2574. rawValues:NULL
  2575. forKeys:NULL
  2576. count:0];
  2577. if (self) {
  2578. if (dictionary) {
  2579. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2580. }
  2581. }
  2582. return self;
  2583. }
  2584. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2585. capacity:(NSUInteger)numItems {
  2586. #pragma unused(numItems)
  2587. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2588. }
  2589. - (void)dealloc {
  2590. [_dictionary release];
  2591. [super dealloc];
  2592. }
  2593. - (instancetype)copyWithZone:(NSZone *)zone {
  2594. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2595. }
  2596. - (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
  2597. if (self == other) {
  2598. return YES;
  2599. }
  2600. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2601. return NO;
  2602. }
  2603. return [_dictionary isEqual:other->_dictionary];
  2604. }
  2605. - (NSUInteger)hash {
  2606. return _dictionary.count;
  2607. }
  2608. - (NSString *)description {
  2609. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2610. }
  2611. - (NSUInteger)count {
  2612. return _dictionary.count;
  2613. }
  2614. - (void)enumerateKeysAndRawValuesUsingBlock:
  2615. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2616. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2617. NSNumber *aValue,
  2618. BOOL *stop) {
  2619. block([aKey unsignedIntValue], [aValue intValue], stop);
  2620. }];
  2621. }
  2622. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2623. NSUInteger count = _dictionary.count;
  2624. if (count == 0) {
  2625. return 0;
  2626. }
  2627. GPBType valueType = GPBGetFieldType(field);
  2628. GPBType keyType = field.mapKeyType;
  2629. __block size_t result = 0;
  2630. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2631. NSNumber *aValue,
  2632. BOOL *stop) {
  2633. #pragma unused(stop)
  2634. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2635. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  2636. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2637. }];
  2638. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2639. result += tagSize * count;
  2640. return result;
  2641. }
  2642. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2643. asField:(GPBFieldDescriptor *)field {
  2644. GPBType valueType = GPBGetFieldType(field);
  2645. GPBType keyType = field.mapKeyType;
  2646. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2647. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2648. NSNumber *aValue,
  2649. BOOL *stop) {
  2650. #pragma unused(stop)
  2651. // Write the tag.
  2652. [outputStream writeInt32NoTag:tag];
  2653. // Write the size of the message.
  2654. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2655. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  2656. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2657. // Write the fields.
  2658. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2659. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  2660. }];
  2661. }
  2662. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2663. forKey:(GPBValue *)key
  2664. keyType:(GPBType)keyType {
  2665. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyType);
  2666. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  2667. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2668. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2669. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyType);
  2670. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  2671. [outputStream release];
  2672. return data;
  2673. }
  2674. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2675. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2676. }
  2677. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2678. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2679. #pragma unused(stop)
  2680. block([NSString stringWithFormat:@"%u", key], @(value));
  2681. }];
  2682. }
  2683. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2684. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2685. if (wrapped && value) {
  2686. int32_t result = [wrapped intValue];
  2687. if (!_validationFunc(result)) {
  2688. result = kGPBUnrecognizedEnumeratorValue;
  2689. }
  2690. *value = result;
  2691. }
  2692. return (wrapped != NULL);
  2693. }
  2694. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2695. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2696. if (wrapped && rawValue) {
  2697. *rawValue = [wrapped intValue];
  2698. }
  2699. return (wrapped != NULL);
  2700. }
  2701. - (void)enumerateKeysAndValuesUsingBlock:
  2702. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2703. GPBEnumValidationFunc func = _validationFunc;
  2704. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2705. NSNumber *aValue,
  2706. BOOL *stop) {
  2707. int32_t unwrapped = [aValue intValue];
  2708. if (!func(unwrapped)) {
  2709. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2710. }
  2711. block([aKey unsignedIntValue], unwrapped, stop);
  2712. }];
  2713. }
  2714. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2715. if (otherDictionary) {
  2716. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2717. }
  2718. }
  2719. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2720. [_dictionary setObject:@(value) forKey:@(key)];
  2721. }
  2722. - (void)removeValueForKey:(uint32_t)aKey {
  2723. [_dictionary removeObjectForKey:@(aKey)];
  2724. }
  2725. - (void)removeAll {
  2726. [_dictionary removeAllObjects];
  2727. }
  2728. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2729. if (!_validationFunc(value)) {
  2730. [NSException raise:NSInvalidArgumentException
  2731. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2732. value];
  2733. }
  2734. [_dictionary setObject:@(value) forKey:@(key)];
  2735. }
  2736. @end
  2737. #pragma mark - UInt32 -> Object
  2738. @implementation GPBUInt32ObjectDictionary {
  2739. @package
  2740. NSMutableDictionary *_dictionary;
  2741. }
  2742. + (instancetype)dictionary {
  2743. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2744. }
  2745. + (instancetype)dictionaryWithValue:(id)value
  2746. forKey:(uint32_t)key {
  2747. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2748. // on to get the type correct.
  2749. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithValues:&value
  2750. forKeys:&key
  2751. count:1] autorelease];
  2752. }
  2753. + (instancetype)dictionaryWithValues:(const id [])values
  2754. forKeys:(const uint32_t [])keys
  2755. count:(NSUInteger)count {
  2756. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2757. // on to get the type correct.
  2758. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithValues:values
  2759. forKeys:keys
  2760. count:count] autorelease];
  2761. }
  2762. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2763. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2764. // on to get the type correct.
  2765. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2766. }
  2767. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2768. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2769. }
  2770. - (instancetype)init {
  2771. return [self initWithValues:NULL forKeys:NULL count:0];
  2772. }
  2773. - (instancetype)initWithValues:(const id [])values
  2774. forKeys:(const uint32_t [])keys
  2775. count:(NSUInteger)count {
  2776. self = [super init];
  2777. if (self) {
  2778. _dictionary = [[NSMutableDictionary alloc] init];
  2779. if (count && values && keys) {
  2780. for (NSUInteger i = 0; i < count; ++i) {
  2781. [_dictionary setObject:values[i] forKey:@(keys[i])];
  2782. }
  2783. }
  2784. }
  2785. return self;
  2786. }
  2787. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2788. self = [self initWithValues:NULL forKeys:NULL count:0];
  2789. if (self) {
  2790. if (dictionary) {
  2791. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2792. }
  2793. }
  2794. return self;
  2795. }
  2796. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2797. #pragma unused(numItems)
  2798. return [self initWithValues:NULL forKeys:NULL count:0];
  2799. }
  2800. - (void)dealloc {
  2801. [_dictionary release];
  2802. [super dealloc];
  2803. }
  2804. - (instancetype)copyWithZone:(NSZone *)zone {
  2805. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2806. }
  2807. - (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
  2808. if (self == other) {
  2809. return YES;
  2810. }
  2811. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2812. return NO;
  2813. }
  2814. return [_dictionary isEqual:other->_dictionary];
  2815. }
  2816. - (NSUInteger)hash {
  2817. return _dictionary.count;
  2818. }
  2819. - (NSString *)description {
  2820. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2821. }
  2822. - (NSUInteger)count {
  2823. return _dictionary.count;
  2824. }
  2825. - (void)enumerateKeysAndValuesUsingBlock:
  2826. (void (^)(uint32_t key, id value, BOOL *stop))block {
  2827. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2828. id aValue,
  2829. BOOL *stop) {
  2830. block([aKey unsignedIntValue], aValue, stop);
  2831. }];
  2832. }
  2833. - (BOOL)isInitialized {
  2834. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2835. if (!msg.initialized) {
  2836. return NO;
  2837. }
  2838. }
  2839. return YES;
  2840. }
  2841. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2842. GPBUInt32ObjectDictionary *newDict =
  2843. [[GPBUInt32ObjectDictionary alloc] init];
  2844. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  2845. GPBMessage *msg,
  2846. BOOL *stop) {
  2847. #pragma unused(stop)
  2848. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  2849. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  2850. [copiedMsg release];
  2851. }];
  2852. return newDict;
  2853. }
  2854. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2855. NSUInteger count = _dictionary.count;
  2856. if (count == 0) {
  2857. return 0;
  2858. }
  2859. GPBType valueType = GPBGetFieldType(field);
  2860. GPBType keyType = field.mapKeyType;
  2861. __block size_t result = 0;
  2862. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2863. id aValue,
  2864. BOOL *stop) {
  2865. #pragma unused(stop)
  2866. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2867. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  2868. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2869. }];
  2870. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2871. result += tagSize * count;
  2872. return result;
  2873. }
  2874. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2875. asField:(GPBFieldDescriptor *)field {
  2876. GPBType valueType = GPBGetFieldType(field);
  2877. GPBType keyType = field.mapKeyType;
  2878. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2879. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2880. id aValue,
  2881. BOOL *stop) {
  2882. #pragma unused(stop)
  2883. // Write the tag.
  2884. [outputStream writeInt32NoTag:tag];
  2885. // Write the size of the message.
  2886. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2887. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  2888. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2889. // Write the fields.
  2890. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2891. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  2892. }];
  2893. }
  2894. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2895. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  2896. }
  2897. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2898. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, id value, BOOL *stop) {
  2899. #pragma unused(stop)
  2900. block([NSString stringWithFormat:@"%u", key], value);
  2901. }];
  2902. }
  2903. - (id)valueForKey:(uint32_t)key {
  2904. id result = [_dictionary objectForKey:@(key)];
  2905. return result;
  2906. }
  2907. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  2908. if (otherDictionary) {
  2909. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2910. }
  2911. }
  2912. - (void)setValue:(id)value forKey:(uint32_t)key {
  2913. [_dictionary setObject:value forKey:@(key)];
  2914. }
  2915. - (void)removeValueForKey:(uint32_t)aKey {
  2916. [_dictionary removeObjectForKey:@(aKey)];
  2917. }
  2918. - (void)removeAll {
  2919. [_dictionary removeAllObjects];
  2920. }
  2921. @end
  2922. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  2923. // This block of code is generated, do not edit it directly.
  2924. #pragma mark - Int32 -> UInt32
  2925. @implementation GPBInt32UInt32Dictionary {
  2926. @package
  2927. NSMutableDictionary *_dictionary;
  2928. }
  2929. + (instancetype)dictionary {
  2930. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2931. }
  2932. + (instancetype)dictionaryWithValue:(uint32_t)value
  2933. forKey:(int32_t)key {
  2934. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2935. // on to get the type correct.
  2936. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  2937. forKeys:&key
  2938. count:1] autorelease];
  2939. }
  2940. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  2941. forKeys:(const int32_t [])keys
  2942. count:(NSUInteger)count {
  2943. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2944. // on to get the type correct.
  2945. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  2946. forKeys:keys
  2947. count:count] autorelease];
  2948. }
  2949. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  2950. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2951. // on to get the type correct.
  2952. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2953. }
  2954. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2955. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2956. }
  2957. - (instancetype)init {
  2958. return [self initWithValues:NULL forKeys:NULL count:0];
  2959. }
  2960. - (instancetype)initWithValues:(const uint32_t [])values
  2961. forKeys:(const int32_t [])keys
  2962. count:(NSUInteger)count {
  2963. self = [super init];
  2964. if (self) {
  2965. _dictionary = [[NSMutableDictionary alloc] init];
  2966. if (count && values && keys) {
  2967. for (NSUInteger i = 0; i < count; ++i) {
  2968. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2969. }
  2970. }
  2971. }
  2972. return self;
  2973. }
  2974. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  2975. self = [self initWithValues:NULL forKeys:NULL count:0];
  2976. if (self) {
  2977. if (dictionary) {
  2978. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2979. }
  2980. }
  2981. return self;
  2982. }
  2983. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2984. #pragma unused(numItems)
  2985. return [self initWithValues:NULL forKeys:NULL count:0];
  2986. }
  2987. - (void)dealloc {
  2988. [_dictionary release];
  2989. [super dealloc];
  2990. }
  2991. - (instancetype)copyWithZone:(NSZone *)zone {
  2992. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  2993. }
  2994. - (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
  2995. if (self == other) {
  2996. return YES;
  2997. }
  2998. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  2999. return NO;
  3000. }
  3001. return [_dictionary isEqual:other->_dictionary];
  3002. }
  3003. - (NSUInteger)hash {
  3004. return _dictionary.count;
  3005. }
  3006. - (NSString *)description {
  3007. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3008. }
  3009. - (NSUInteger)count {
  3010. return _dictionary.count;
  3011. }
  3012. - (void)enumerateKeysAndValuesUsingBlock:
  3013. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3014. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3015. NSNumber *aValue,
  3016. BOOL *stop) {
  3017. block([aKey intValue], [aValue unsignedIntValue], stop);
  3018. }];
  3019. }
  3020. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3021. NSUInteger count = _dictionary.count;
  3022. if (count == 0) {
  3023. return 0;
  3024. }
  3025. GPBType valueType = GPBGetFieldType(field);
  3026. GPBType keyType = field.mapKeyType;
  3027. __block size_t result = 0;
  3028. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3029. NSNumber *aValue,
  3030. BOOL *stop) {
  3031. #pragma unused(stop)
  3032. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3033. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  3034. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3035. }];
  3036. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3037. result += tagSize * count;
  3038. return result;
  3039. }
  3040. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3041. asField:(GPBFieldDescriptor *)field {
  3042. GPBType valueType = GPBGetFieldType(field);
  3043. GPBType keyType = field.mapKeyType;
  3044. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3045. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3046. NSNumber *aValue,
  3047. BOOL *stop) {
  3048. #pragma unused(stop)
  3049. // Write the tag.
  3050. [outputStream writeInt32NoTag:tag];
  3051. // Write the size of the message.
  3052. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3053. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  3054. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3055. // Write the fields.
  3056. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3057. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  3058. }];
  3059. }
  3060. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3061. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3062. }
  3063. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3064. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3065. #pragma unused(stop)
  3066. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3067. }];
  3068. }
  3069. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3070. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3071. if (wrapped && value) {
  3072. *value = [wrapped unsignedIntValue];
  3073. }
  3074. return (wrapped != NULL);
  3075. }
  3076. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3077. if (otherDictionary) {
  3078. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3079. }
  3080. }
  3081. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3082. [_dictionary setObject:@(value) forKey:@(key)];
  3083. }
  3084. - (void)removeValueForKey:(int32_t)aKey {
  3085. [_dictionary removeObjectForKey:@(aKey)];
  3086. }
  3087. - (void)removeAll {
  3088. [_dictionary removeAllObjects];
  3089. }
  3090. @end
  3091. #pragma mark - Int32 -> Int32
  3092. @implementation GPBInt32Int32Dictionary {
  3093. @package
  3094. NSMutableDictionary *_dictionary;
  3095. }
  3096. + (instancetype)dictionary {
  3097. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3098. }
  3099. + (instancetype)dictionaryWithValue:(int32_t)value
  3100. forKey:(int32_t)key {
  3101. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3102. // on to get the type correct.
  3103. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3104. forKeys:&key
  3105. count:1] autorelease];
  3106. }
  3107. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3108. forKeys:(const int32_t [])keys
  3109. count:(NSUInteger)count {
  3110. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3111. // on to get the type correct.
  3112. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3113. forKeys:keys
  3114. count:count] autorelease];
  3115. }
  3116. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3117. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3118. // on to get the type correct.
  3119. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3120. }
  3121. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3122. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3123. }
  3124. - (instancetype)init {
  3125. return [self initWithValues:NULL forKeys:NULL count:0];
  3126. }
  3127. - (instancetype)initWithValues:(const int32_t [])values
  3128. forKeys:(const int32_t [])keys
  3129. count:(NSUInteger)count {
  3130. self = [super init];
  3131. if (self) {
  3132. _dictionary = [[NSMutableDictionary alloc] init];
  3133. if (count && values && keys) {
  3134. for (NSUInteger i = 0; i < count; ++i) {
  3135. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3136. }
  3137. }
  3138. }
  3139. return self;
  3140. }
  3141. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3142. self = [self initWithValues:NULL forKeys:NULL count:0];
  3143. if (self) {
  3144. if (dictionary) {
  3145. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3146. }
  3147. }
  3148. return self;
  3149. }
  3150. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3151. #pragma unused(numItems)
  3152. return [self initWithValues:NULL forKeys:NULL count:0];
  3153. }
  3154. - (void)dealloc {
  3155. [_dictionary release];
  3156. [super dealloc];
  3157. }
  3158. - (instancetype)copyWithZone:(NSZone *)zone {
  3159. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3160. }
  3161. - (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
  3162. if (self == other) {
  3163. return YES;
  3164. }
  3165. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3166. return NO;
  3167. }
  3168. return [_dictionary isEqual:other->_dictionary];
  3169. }
  3170. - (NSUInteger)hash {
  3171. return _dictionary.count;
  3172. }
  3173. - (NSString *)description {
  3174. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3175. }
  3176. - (NSUInteger)count {
  3177. return _dictionary.count;
  3178. }
  3179. - (void)enumerateKeysAndValuesUsingBlock:
  3180. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3181. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3182. NSNumber *aValue,
  3183. BOOL *stop) {
  3184. block([aKey intValue], [aValue intValue], stop);
  3185. }];
  3186. }
  3187. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3188. NSUInteger count = _dictionary.count;
  3189. if (count == 0) {
  3190. return 0;
  3191. }
  3192. GPBType valueType = GPBGetFieldType(field);
  3193. GPBType keyType = field.mapKeyType;
  3194. __block size_t result = 0;
  3195. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3196. NSNumber *aValue,
  3197. BOOL *stop) {
  3198. #pragma unused(stop)
  3199. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3200. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  3201. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3202. }];
  3203. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3204. result += tagSize * count;
  3205. return result;
  3206. }
  3207. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3208. asField:(GPBFieldDescriptor *)field {
  3209. GPBType valueType = GPBGetFieldType(field);
  3210. GPBType keyType = field.mapKeyType;
  3211. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3212. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3213. NSNumber *aValue,
  3214. BOOL *stop) {
  3215. #pragma unused(stop)
  3216. // Write the tag.
  3217. [outputStream writeInt32NoTag:tag];
  3218. // Write the size of the message.
  3219. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3220. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  3221. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3222. // Write the fields.
  3223. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3224. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  3225. }];
  3226. }
  3227. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3228. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3229. }
  3230. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3231. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3232. #pragma unused(stop)
  3233. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3234. }];
  3235. }
  3236. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3237. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3238. if (wrapped && value) {
  3239. *value = [wrapped intValue];
  3240. }
  3241. return (wrapped != NULL);
  3242. }
  3243. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3244. if (otherDictionary) {
  3245. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3246. }
  3247. }
  3248. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3249. [_dictionary setObject:@(value) forKey:@(key)];
  3250. }
  3251. - (void)removeValueForKey:(int32_t)aKey {
  3252. [_dictionary removeObjectForKey:@(aKey)];
  3253. }
  3254. - (void)removeAll {
  3255. [_dictionary removeAllObjects];
  3256. }
  3257. @end
  3258. #pragma mark - Int32 -> UInt64
  3259. @implementation GPBInt32UInt64Dictionary {
  3260. @package
  3261. NSMutableDictionary *_dictionary;
  3262. }
  3263. + (instancetype)dictionary {
  3264. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3265. }
  3266. + (instancetype)dictionaryWithValue:(uint64_t)value
  3267. forKey:(int32_t)key {
  3268. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3269. // on to get the type correct.
  3270. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3271. forKeys:&key
  3272. count:1] autorelease];
  3273. }
  3274. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3275. forKeys:(const int32_t [])keys
  3276. count:(NSUInteger)count {
  3277. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3278. // on to get the type correct.
  3279. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3280. forKeys:keys
  3281. count:count] autorelease];
  3282. }
  3283. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3284. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3285. // on to get the type correct.
  3286. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3287. }
  3288. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3289. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3290. }
  3291. - (instancetype)init {
  3292. return [self initWithValues:NULL forKeys:NULL count:0];
  3293. }
  3294. - (instancetype)initWithValues:(const uint64_t [])values
  3295. forKeys:(const int32_t [])keys
  3296. count:(NSUInteger)count {
  3297. self = [super init];
  3298. if (self) {
  3299. _dictionary = [[NSMutableDictionary alloc] init];
  3300. if (count && values && keys) {
  3301. for (NSUInteger i = 0; i < count; ++i) {
  3302. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3303. }
  3304. }
  3305. }
  3306. return self;
  3307. }
  3308. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3309. self = [self initWithValues:NULL forKeys:NULL count:0];
  3310. if (self) {
  3311. if (dictionary) {
  3312. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3313. }
  3314. }
  3315. return self;
  3316. }
  3317. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3318. #pragma unused(numItems)
  3319. return [self initWithValues:NULL forKeys:NULL count:0];
  3320. }
  3321. - (void)dealloc {
  3322. [_dictionary release];
  3323. [super dealloc];
  3324. }
  3325. - (instancetype)copyWithZone:(NSZone *)zone {
  3326. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3327. }
  3328. - (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
  3329. if (self == other) {
  3330. return YES;
  3331. }
  3332. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3333. return NO;
  3334. }
  3335. return [_dictionary isEqual:other->_dictionary];
  3336. }
  3337. - (NSUInteger)hash {
  3338. return _dictionary.count;
  3339. }
  3340. - (NSString *)description {
  3341. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3342. }
  3343. - (NSUInteger)count {
  3344. return _dictionary.count;
  3345. }
  3346. - (void)enumerateKeysAndValuesUsingBlock:
  3347. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3348. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3349. NSNumber *aValue,
  3350. BOOL *stop) {
  3351. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3352. }];
  3353. }
  3354. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3355. NSUInteger count = _dictionary.count;
  3356. if (count == 0) {
  3357. return 0;
  3358. }
  3359. GPBType valueType = GPBGetFieldType(field);
  3360. GPBType keyType = field.mapKeyType;
  3361. __block size_t result = 0;
  3362. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3363. NSNumber *aValue,
  3364. BOOL *stop) {
  3365. #pragma unused(stop)
  3366. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3367. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  3368. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3369. }];
  3370. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3371. result += tagSize * count;
  3372. return result;
  3373. }
  3374. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3375. asField:(GPBFieldDescriptor *)field {
  3376. GPBType valueType = GPBGetFieldType(field);
  3377. GPBType keyType = field.mapKeyType;
  3378. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3379. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3380. NSNumber *aValue,
  3381. BOOL *stop) {
  3382. #pragma unused(stop)
  3383. // Write the tag.
  3384. [outputStream writeInt32NoTag:tag];
  3385. // Write the size of the message.
  3386. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3387. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  3388. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3389. // Write the fields.
  3390. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3391. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  3392. }];
  3393. }
  3394. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3395. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3396. }
  3397. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3398. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3399. #pragma unused(stop)
  3400. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3401. }];
  3402. }
  3403. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3404. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3405. if (wrapped && value) {
  3406. *value = [wrapped unsignedLongLongValue];
  3407. }
  3408. return (wrapped != NULL);
  3409. }
  3410. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3411. if (otherDictionary) {
  3412. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3413. }
  3414. }
  3415. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3416. [_dictionary setObject:@(value) forKey:@(key)];
  3417. }
  3418. - (void)removeValueForKey:(int32_t)aKey {
  3419. [_dictionary removeObjectForKey:@(aKey)];
  3420. }
  3421. - (void)removeAll {
  3422. [_dictionary removeAllObjects];
  3423. }
  3424. @end
  3425. #pragma mark - Int32 -> Int64
  3426. @implementation GPBInt32Int64Dictionary {
  3427. @package
  3428. NSMutableDictionary *_dictionary;
  3429. }
  3430. + (instancetype)dictionary {
  3431. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3432. }
  3433. + (instancetype)dictionaryWithValue:(int64_t)value
  3434. forKey:(int32_t)key {
  3435. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3436. // on to get the type correct.
  3437. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3438. forKeys:&key
  3439. count:1] autorelease];
  3440. }
  3441. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3442. forKeys:(const int32_t [])keys
  3443. count:(NSUInteger)count {
  3444. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3445. // on to get the type correct.
  3446. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3447. forKeys:keys
  3448. count:count] autorelease];
  3449. }
  3450. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3451. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3452. // on to get the type correct.
  3453. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3454. }
  3455. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3456. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3457. }
  3458. - (instancetype)init {
  3459. return [self initWithValues:NULL forKeys:NULL count:0];
  3460. }
  3461. - (instancetype)initWithValues:(const int64_t [])values
  3462. forKeys:(const int32_t [])keys
  3463. count:(NSUInteger)count {
  3464. self = [super init];
  3465. if (self) {
  3466. _dictionary = [[NSMutableDictionary alloc] init];
  3467. if (count && values && keys) {
  3468. for (NSUInteger i = 0; i < count; ++i) {
  3469. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3470. }
  3471. }
  3472. }
  3473. return self;
  3474. }
  3475. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3476. self = [self initWithValues:NULL forKeys:NULL count:0];
  3477. if (self) {
  3478. if (dictionary) {
  3479. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3480. }
  3481. }
  3482. return self;
  3483. }
  3484. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3485. #pragma unused(numItems)
  3486. return [self initWithValues:NULL forKeys:NULL count:0];
  3487. }
  3488. - (void)dealloc {
  3489. [_dictionary release];
  3490. [super dealloc];
  3491. }
  3492. - (instancetype)copyWithZone:(NSZone *)zone {
  3493. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3494. }
  3495. - (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
  3496. if (self == other) {
  3497. return YES;
  3498. }
  3499. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3500. return NO;
  3501. }
  3502. return [_dictionary isEqual:other->_dictionary];
  3503. }
  3504. - (NSUInteger)hash {
  3505. return _dictionary.count;
  3506. }
  3507. - (NSString *)description {
  3508. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3509. }
  3510. - (NSUInteger)count {
  3511. return _dictionary.count;
  3512. }
  3513. - (void)enumerateKeysAndValuesUsingBlock:
  3514. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3515. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3516. NSNumber *aValue,
  3517. BOOL *stop) {
  3518. block([aKey intValue], [aValue longLongValue], stop);
  3519. }];
  3520. }
  3521. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3522. NSUInteger count = _dictionary.count;
  3523. if (count == 0) {
  3524. return 0;
  3525. }
  3526. GPBType valueType = GPBGetFieldType(field);
  3527. GPBType keyType = field.mapKeyType;
  3528. __block size_t result = 0;
  3529. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3530. NSNumber *aValue,
  3531. BOOL *stop) {
  3532. #pragma unused(stop)
  3533. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3534. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  3535. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3536. }];
  3537. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3538. result += tagSize * count;
  3539. return result;
  3540. }
  3541. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3542. asField:(GPBFieldDescriptor *)field {
  3543. GPBType valueType = GPBGetFieldType(field);
  3544. GPBType keyType = field.mapKeyType;
  3545. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3546. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3547. NSNumber *aValue,
  3548. BOOL *stop) {
  3549. #pragma unused(stop)
  3550. // Write the tag.
  3551. [outputStream writeInt32NoTag:tag];
  3552. // Write the size of the message.
  3553. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3554. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  3555. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3556. // Write the fields.
  3557. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3558. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  3559. }];
  3560. }
  3561. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3562. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3563. }
  3564. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3565. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3566. #pragma unused(stop)
  3567. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3568. }];
  3569. }
  3570. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3571. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3572. if (wrapped && value) {
  3573. *value = [wrapped longLongValue];
  3574. }
  3575. return (wrapped != NULL);
  3576. }
  3577. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3578. if (otherDictionary) {
  3579. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3580. }
  3581. }
  3582. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3583. [_dictionary setObject:@(value) forKey:@(key)];
  3584. }
  3585. - (void)removeValueForKey:(int32_t)aKey {
  3586. [_dictionary removeObjectForKey:@(aKey)];
  3587. }
  3588. - (void)removeAll {
  3589. [_dictionary removeAllObjects];
  3590. }
  3591. @end
  3592. #pragma mark - Int32 -> Bool
  3593. @implementation GPBInt32BoolDictionary {
  3594. @package
  3595. NSMutableDictionary *_dictionary;
  3596. }
  3597. + (instancetype)dictionary {
  3598. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3599. }
  3600. + (instancetype)dictionaryWithValue:(BOOL)value
  3601. forKey:(int32_t)key {
  3602. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3603. // on to get the type correct.
  3604. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3605. forKeys:&key
  3606. count:1] autorelease];
  3607. }
  3608. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3609. forKeys:(const int32_t [])keys
  3610. count:(NSUInteger)count {
  3611. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3612. // on to get the type correct.
  3613. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3614. forKeys:keys
  3615. count:count] autorelease];
  3616. }
  3617. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3618. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3619. // on to get the type correct.
  3620. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3621. }
  3622. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3623. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3624. }
  3625. - (instancetype)init {
  3626. return [self initWithValues:NULL forKeys:NULL count:0];
  3627. }
  3628. - (instancetype)initWithValues:(const BOOL [])values
  3629. forKeys:(const int32_t [])keys
  3630. count:(NSUInteger)count {
  3631. self = [super init];
  3632. if (self) {
  3633. _dictionary = [[NSMutableDictionary alloc] init];
  3634. if (count && values && keys) {
  3635. for (NSUInteger i = 0; i < count; ++i) {
  3636. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3637. }
  3638. }
  3639. }
  3640. return self;
  3641. }
  3642. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3643. self = [self initWithValues:NULL forKeys:NULL count:0];
  3644. if (self) {
  3645. if (dictionary) {
  3646. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3647. }
  3648. }
  3649. return self;
  3650. }
  3651. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3652. #pragma unused(numItems)
  3653. return [self initWithValues:NULL forKeys:NULL count:0];
  3654. }
  3655. - (void)dealloc {
  3656. [_dictionary release];
  3657. [super dealloc];
  3658. }
  3659. - (instancetype)copyWithZone:(NSZone *)zone {
  3660. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3661. }
  3662. - (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
  3663. if (self == other) {
  3664. return YES;
  3665. }
  3666. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3667. return NO;
  3668. }
  3669. return [_dictionary isEqual:other->_dictionary];
  3670. }
  3671. - (NSUInteger)hash {
  3672. return _dictionary.count;
  3673. }
  3674. - (NSString *)description {
  3675. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3676. }
  3677. - (NSUInteger)count {
  3678. return _dictionary.count;
  3679. }
  3680. - (void)enumerateKeysAndValuesUsingBlock:
  3681. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3682. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3683. NSNumber *aValue,
  3684. BOOL *stop) {
  3685. block([aKey intValue], [aValue boolValue], stop);
  3686. }];
  3687. }
  3688. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3689. NSUInteger count = _dictionary.count;
  3690. if (count == 0) {
  3691. return 0;
  3692. }
  3693. GPBType valueType = GPBGetFieldType(field);
  3694. GPBType keyType = field.mapKeyType;
  3695. __block size_t result = 0;
  3696. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3697. NSNumber *aValue,
  3698. BOOL *stop) {
  3699. #pragma unused(stop)
  3700. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3701. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  3702. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3703. }];
  3704. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3705. result += tagSize * count;
  3706. return result;
  3707. }
  3708. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3709. asField:(GPBFieldDescriptor *)field {
  3710. GPBType valueType = GPBGetFieldType(field);
  3711. GPBType keyType = field.mapKeyType;
  3712. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3713. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3714. NSNumber *aValue,
  3715. BOOL *stop) {
  3716. #pragma unused(stop)
  3717. // Write the tag.
  3718. [outputStream writeInt32NoTag:tag];
  3719. // Write the size of the message.
  3720. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3721. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  3722. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3723. // Write the fields.
  3724. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3725. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  3726. }];
  3727. }
  3728. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3729. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3730. }
  3731. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3732. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3733. #pragma unused(stop)
  3734. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3735. }];
  3736. }
  3737. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  3738. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3739. if (wrapped && value) {
  3740. *value = [wrapped boolValue];
  3741. }
  3742. return (wrapped != NULL);
  3743. }
  3744. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3745. if (otherDictionary) {
  3746. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3747. }
  3748. }
  3749. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  3750. [_dictionary setObject:@(value) forKey:@(key)];
  3751. }
  3752. - (void)removeValueForKey:(int32_t)aKey {
  3753. [_dictionary removeObjectForKey:@(aKey)];
  3754. }
  3755. - (void)removeAll {
  3756. [_dictionary removeAllObjects];
  3757. }
  3758. @end
  3759. #pragma mark - Int32 -> Float
  3760. @implementation GPBInt32FloatDictionary {
  3761. @package
  3762. NSMutableDictionary *_dictionary;
  3763. }
  3764. + (instancetype)dictionary {
  3765. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3766. }
  3767. + (instancetype)dictionaryWithValue:(float)value
  3768. forKey:(int32_t)key {
  3769. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3770. // on to get the type correct.
  3771. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  3772. forKeys:&key
  3773. count:1] autorelease];
  3774. }
  3775. + (instancetype)dictionaryWithValues:(const float [])values
  3776. forKeys:(const int32_t [])keys
  3777. count:(NSUInteger)count {
  3778. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3779. // on to get the type correct.
  3780. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  3781. forKeys:keys
  3782. count:count] autorelease];
  3783. }
  3784. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3785. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3786. // on to get the type correct.
  3787. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3788. }
  3789. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3790. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3791. }
  3792. - (instancetype)init {
  3793. return [self initWithValues:NULL forKeys:NULL count:0];
  3794. }
  3795. - (instancetype)initWithValues:(const float [])values
  3796. forKeys:(const int32_t [])keys
  3797. count:(NSUInteger)count {
  3798. self = [super init];
  3799. if (self) {
  3800. _dictionary = [[NSMutableDictionary alloc] init];
  3801. if (count && values && keys) {
  3802. for (NSUInteger i = 0; i < count; ++i) {
  3803. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3804. }
  3805. }
  3806. }
  3807. return self;
  3808. }
  3809. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3810. self = [self initWithValues:NULL forKeys:NULL count:0];
  3811. if (self) {
  3812. if (dictionary) {
  3813. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3814. }
  3815. }
  3816. return self;
  3817. }
  3818. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3819. #pragma unused(numItems)
  3820. return [self initWithValues:NULL forKeys:NULL count:0];
  3821. }
  3822. - (void)dealloc {
  3823. [_dictionary release];
  3824. [super dealloc];
  3825. }
  3826. - (instancetype)copyWithZone:(NSZone *)zone {
  3827. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  3828. }
  3829. - (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
  3830. if (self == other) {
  3831. return YES;
  3832. }
  3833. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  3834. return NO;
  3835. }
  3836. return [_dictionary isEqual:other->_dictionary];
  3837. }
  3838. - (NSUInteger)hash {
  3839. return _dictionary.count;
  3840. }
  3841. - (NSString *)description {
  3842. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3843. }
  3844. - (NSUInteger)count {
  3845. return _dictionary.count;
  3846. }
  3847. - (void)enumerateKeysAndValuesUsingBlock:
  3848. (void (^)(int32_t key, float value, BOOL *stop))block {
  3849. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3850. NSNumber *aValue,
  3851. BOOL *stop) {
  3852. block([aKey intValue], [aValue floatValue], stop);
  3853. }];
  3854. }
  3855. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3856. NSUInteger count = _dictionary.count;
  3857. if (count == 0) {
  3858. return 0;
  3859. }
  3860. GPBType valueType = GPBGetFieldType(field);
  3861. GPBType keyType = field.mapKeyType;
  3862. __block size_t result = 0;
  3863. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3864. NSNumber *aValue,
  3865. BOOL *stop) {
  3866. #pragma unused(stop)
  3867. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3868. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  3869. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3870. }];
  3871. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3872. result += tagSize * count;
  3873. return result;
  3874. }
  3875. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3876. asField:(GPBFieldDescriptor *)field {
  3877. GPBType valueType = GPBGetFieldType(field);
  3878. GPBType keyType = field.mapKeyType;
  3879. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3880. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3881. NSNumber *aValue,
  3882. BOOL *stop) {
  3883. #pragma unused(stop)
  3884. // Write the tag.
  3885. [outputStream writeInt32NoTag:tag];
  3886. // Write the size of the message.
  3887. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3888. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  3889. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3890. // Write the fields.
  3891. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3892. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  3893. }];
  3894. }
  3895. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3896. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  3897. }
  3898. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3899. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  3900. #pragma unused(stop)
  3901. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  3902. }];
  3903. }
  3904. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  3905. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3906. if (wrapped && value) {
  3907. *value = [wrapped floatValue];
  3908. }
  3909. return (wrapped != NULL);
  3910. }
  3911. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  3912. if (otherDictionary) {
  3913. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3914. }
  3915. }
  3916. - (void)setValue:(float)value forKey:(int32_t)key {
  3917. [_dictionary setObject:@(value) forKey:@(key)];
  3918. }
  3919. - (void)removeValueForKey:(int32_t)aKey {
  3920. [_dictionary removeObjectForKey:@(aKey)];
  3921. }
  3922. - (void)removeAll {
  3923. [_dictionary removeAllObjects];
  3924. }
  3925. @end
  3926. #pragma mark - Int32 -> Double
  3927. @implementation GPBInt32DoubleDictionary {
  3928. @package
  3929. NSMutableDictionary *_dictionary;
  3930. }
  3931. + (instancetype)dictionary {
  3932. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3933. }
  3934. + (instancetype)dictionaryWithValue:(double)value
  3935. forKey:(int32_t)key {
  3936. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3937. // on to get the type correct.
  3938. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  3939. forKeys:&key
  3940. count:1] autorelease];
  3941. }
  3942. + (instancetype)dictionaryWithValues:(const double [])values
  3943. forKeys:(const int32_t [])keys
  3944. count:(NSUInteger)count {
  3945. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3946. // on to get the type correct.
  3947. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  3948. forKeys:keys
  3949. count:count] autorelease];
  3950. }
  3951. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  3952. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3953. // on to get the type correct.
  3954. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3955. }
  3956. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3957. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3958. }
  3959. - (instancetype)init {
  3960. return [self initWithValues:NULL forKeys:NULL count:0];
  3961. }
  3962. - (instancetype)initWithValues:(const double [])values
  3963. forKeys:(const int32_t [])keys
  3964. count:(NSUInteger)count {
  3965. self = [super init];
  3966. if (self) {
  3967. _dictionary = [[NSMutableDictionary alloc] init];
  3968. if (count && values && keys) {
  3969. for (NSUInteger i = 0; i < count; ++i) {
  3970. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3971. }
  3972. }
  3973. }
  3974. return self;
  3975. }
  3976. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  3977. self = [self initWithValues:NULL forKeys:NULL count:0];
  3978. if (self) {
  3979. if (dictionary) {
  3980. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3981. }
  3982. }
  3983. return self;
  3984. }
  3985. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3986. #pragma unused(numItems)
  3987. return [self initWithValues:NULL forKeys:NULL count:0];
  3988. }
  3989. - (void)dealloc {
  3990. [_dictionary release];
  3991. [super dealloc];
  3992. }
  3993. - (instancetype)copyWithZone:(NSZone *)zone {
  3994. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  3995. }
  3996. - (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
  3997. if (self == other) {
  3998. return YES;
  3999. }
  4000. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4001. return NO;
  4002. }
  4003. return [_dictionary isEqual:other->_dictionary];
  4004. }
  4005. - (NSUInteger)hash {
  4006. return _dictionary.count;
  4007. }
  4008. - (NSString *)description {
  4009. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4010. }
  4011. - (NSUInteger)count {
  4012. return _dictionary.count;
  4013. }
  4014. - (void)enumerateKeysAndValuesUsingBlock:
  4015. (void (^)(int32_t key, double value, BOOL *stop))block {
  4016. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4017. NSNumber *aValue,
  4018. BOOL *stop) {
  4019. block([aKey intValue], [aValue doubleValue], stop);
  4020. }];
  4021. }
  4022. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4023. NSUInteger count = _dictionary.count;
  4024. if (count == 0) {
  4025. return 0;
  4026. }
  4027. GPBType valueType = GPBGetFieldType(field);
  4028. GPBType keyType = field.mapKeyType;
  4029. __block size_t result = 0;
  4030. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4031. NSNumber *aValue,
  4032. BOOL *stop) {
  4033. #pragma unused(stop)
  4034. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4035. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  4036. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4037. }];
  4038. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4039. result += tagSize * count;
  4040. return result;
  4041. }
  4042. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4043. asField:(GPBFieldDescriptor *)field {
  4044. GPBType valueType = GPBGetFieldType(field);
  4045. GPBType keyType = field.mapKeyType;
  4046. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4047. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4048. NSNumber *aValue,
  4049. BOOL *stop) {
  4050. #pragma unused(stop)
  4051. // Write the tag.
  4052. [outputStream writeInt32NoTag:tag];
  4053. // Write the size of the message.
  4054. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4055. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  4056. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4057. // Write the fields.
  4058. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4059. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  4060. }];
  4061. }
  4062. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4063. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4064. }
  4065. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4066. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4067. #pragma unused(stop)
  4068. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4069. }];
  4070. }
  4071. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4072. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4073. if (wrapped && value) {
  4074. *value = [wrapped doubleValue];
  4075. }
  4076. return (wrapped != NULL);
  4077. }
  4078. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4079. if (otherDictionary) {
  4080. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4081. }
  4082. }
  4083. - (void)setValue:(double)value forKey:(int32_t)key {
  4084. [_dictionary setObject:@(value) forKey:@(key)];
  4085. }
  4086. - (void)removeValueForKey:(int32_t)aKey {
  4087. [_dictionary removeObjectForKey:@(aKey)];
  4088. }
  4089. - (void)removeAll {
  4090. [_dictionary removeAllObjects];
  4091. }
  4092. @end
  4093. #pragma mark - Int32 -> Enum
  4094. @implementation GPBInt32EnumDictionary {
  4095. @package
  4096. NSMutableDictionary *_dictionary;
  4097. GPBEnumValidationFunc _validationFunc;
  4098. }
  4099. @synthesize validationFunc = _validationFunc;
  4100. + (instancetype)dictionary {
  4101. return [[[self alloc] initWithValidationFunction:NULL
  4102. rawValues:NULL
  4103. forKeys:NULL
  4104. count:0] autorelease];
  4105. }
  4106. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4107. return [[[self alloc] initWithValidationFunction:func
  4108. rawValues:NULL
  4109. forKeys:NULL
  4110. count:0] autorelease];
  4111. }
  4112. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4113. rawValue:(int32_t)rawValue
  4114. forKey:(int32_t)key {
  4115. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4116. // on to get the type correct.
  4117. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4118. rawValues:&rawValue
  4119. forKeys:&key
  4120. count:1] autorelease];
  4121. }
  4122. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4123. rawValues:(const int32_t [])rawValues
  4124. forKeys:(const int32_t [])keys
  4125. count:(NSUInteger)count {
  4126. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4127. // on to get the type correct.
  4128. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4129. rawValues:rawValues
  4130. forKeys:keys
  4131. count:count] autorelease];
  4132. }
  4133. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4134. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4135. // on to get the type correct.
  4136. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4137. }
  4138. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4139. capacity:(NSUInteger)numItems {
  4140. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4141. }
  4142. - (instancetype)init {
  4143. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4144. }
  4145. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4146. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4147. }
  4148. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4149. rawValues:(const int32_t [])rawValues
  4150. forKeys:(const int32_t [])keys
  4151. count:(NSUInteger)count {
  4152. self = [super init];
  4153. if (self) {
  4154. _dictionary = [[NSMutableDictionary alloc] init];
  4155. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4156. if (count && rawValues && keys) {
  4157. for (NSUInteger i = 0; i < count; ++i) {
  4158. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4159. }
  4160. }
  4161. }
  4162. return self;
  4163. }
  4164. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4165. self = [self initWithValidationFunction:dictionary.validationFunc
  4166. rawValues:NULL
  4167. forKeys:NULL
  4168. count:0];
  4169. if (self) {
  4170. if (dictionary) {
  4171. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4172. }
  4173. }
  4174. return self;
  4175. }
  4176. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4177. capacity:(NSUInteger)numItems {
  4178. #pragma unused(numItems)
  4179. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4180. }
  4181. - (void)dealloc {
  4182. [_dictionary release];
  4183. [super dealloc];
  4184. }
  4185. - (instancetype)copyWithZone:(NSZone *)zone {
  4186. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4187. }
  4188. - (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
  4189. if (self == other) {
  4190. return YES;
  4191. }
  4192. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4193. return NO;
  4194. }
  4195. return [_dictionary isEqual:other->_dictionary];
  4196. }
  4197. - (NSUInteger)hash {
  4198. return _dictionary.count;
  4199. }
  4200. - (NSString *)description {
  4201. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4202. }
  4203. - (NSUInteger)count {
  4204. return _dictionary.count;
  4205. }
  4206. - (void)enumerateKeysAndRawValuesUsingBlock:
  4207. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4208. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4209. NSNumber *aValue,
  4210. BOOL *stop) {
  4211. block([aKey intValue], [aValue intValue], stop);
  4212. }];
  4213. }
  4214. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4215. NSUInteger count = _dictionary.count;
  4216. if (count == 0) {
  4217. return 0;
  4218. }
  4219. GPBType valueType = GPBGetFieldType(field);
  4220. GPBType keyType = field.mapKeyType;
  4221. __block size_t result = 0;
  4222. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4223. NSNumber *aValue,
  4224. BOOL *stop) {
  4225. #pragma unused(stop)
  4226. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4227. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  4228. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4229. }];
  4230. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4231. result += tagSize * count;
  4232. return result;
  4233. }
  4234. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4235. asField:(GPBFieldDescriptor *)field {
  4236. GPBType valueType = GPBGetFieldType(field);
  4237. GPBType keyType = field.mapKeyType;
  4238. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4239. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4240. NSNumber *aValue,
  4241. BOOL *stop) {
  4242. #pragma unused(stop)
  4243. // Write the tag.
  4244. [outputStream writeInt32NoTag:tag];
  4245. // Write the size of the message.
  4246. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4247. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  4248. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4249. // Write the fields.
  4250. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4251. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  4252. }];
  4253. }
  4254. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4255. forKey:(GPBValue *)key
  4256. keyType:(GPBType)keyType {
  4257. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyType);
  4258. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  4259. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4260. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4261. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyType);
  4262. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  4263. [outputStream release];
  4264. return data;
  4265. }
  4266. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4267. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4268. }
  4269. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4270. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4271. #pragma unused(stop)
  4272. block([NSString stringWithFormat:@"%d", key], @(value));
  4273. }];
  4274. }
  4275. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4276. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4277. if (wrapped && value) {
  4278. int32_t result = [wrapped intValue];
  4279. if (!_validationFunc(result)) {
  4280. result = kGPBUnrecognizedEnumeratorValue;
  4281. }
  4282. *value = result;
  4283. }
  4284. return (wrapped != NULL);
  4285. }
  4286. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4287. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4288. if (wrapped && rawValue) {
  4289. *rawValue = [wrapped intValue];
  4290. }
  4291. return (wrapped != NULL);
  4292. }
  4293. - (void)enumerateKeysAndValuesUsingBlock:
  4294. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4295. GPBEnumValidationFunc func = _validationFunc;
  4296. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4297. NSNumber *aValue,
  4298. BOOL *stop) {
  4299. int32_t unwrapped = [aValue intValue];
  4300. if (!func(unwrapped)) {
  4301. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4302. }
  4303. block([aKey intValue], unwrapped, stop);
  4304. }];
  4305. }
  4306. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4307. if (otherDictionary) {
  4308. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4309. }
  4310. }
  4311. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4312. [_dictionary setObject:@(value) forKey:@(key)];
  4313. }
  4314. - (void)removeValueForKey:(int32_t)aKey {
  4315. [_dictionary removeObjectForKey:@(aKey)];
  4316. }
  4317. - (void)removeAll {
  4318. [_dictionary removeAllObjects];
  4319. }
  4320. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4321. if (!_validationFunc(value)) {
  4322. [NSException raise:NSInvalidArgumentException
  4323. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4324. value];
  4325. }
  4326. [_dictionary setObject:@(value) forKey:@(key)];
  4327. }
  4328. @end
  4329. #pragma mark - Int32 -> Object
  4330. @implementation GPBInt32ObjectDictionary {
  4331. @package
  4332. NSMutableDictionary *_dictionary;
  4333. }
  4334. + (instancetype)dictionary {
  4335. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4336. }
  4337. + (instancetype)dictionaryWithValue:(id)value
  4338. forKey:(int32_t)key {
  4339. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4340. // on to get the type correct.
  4341. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithValues:&value
  4342. forKeys:&key
  4343. count:1] autorelease];
  4344. }
  4345. + (instancetype)dictionaryWithValues:(const id [])values
  4346. forKeys:(const int32_t [])keys
  4347. count:(NSUInteger)count {
  4348. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4349. // on to get the type correct.
  4350. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithValues:values
  4351. forKeys:keys
  4352. count:count] autorelease];
  4353. }
  4354. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4355. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4356. // on to get the type correct.
  4357. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4358. }
  4359. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4360. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4361. }
  4362. - (instancetype)init {
  4363. return [self initWithValues:NULL forKeys:NULL count:0];
  4364. }
  4365. - (instancetype)initWithValues:(const id [])values
  4366. forKeys:(const int32_t [])keys
  4367. count:(NSUInteger)count {
  4368. self = [super init];
  4369. if (self) {
  4370. _dictionary = [[NSMutableDictionary alloc] init];
  4371. if (count && values && keys) {
  4372. for (NSUInteger i = 0; i < count; ++i) {
  4373. [_dictionary setObject:values[i] forKey:@(keys[i])];
  4374. }
  4375. }
  4376. }
  4377. return self;
  4378. }
  4379. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4380. self = [self initWithValues:NULL forKeys:NULL count:0];
  4381. if (self) {
  4382. if (dictionary) {
  4383. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4384. }
  4385. }
  4386. return self;
  4387. }
  4388. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4389. #pragma unused(numItems)
  4390. return [self initWithValues:NULL forKeys:NULL count:0];
  4391. }
  4392. - (void)dealloc {
  4393. [_dictionary release];
  4394. [super dealloc];
  4395. }
  4396. - (instancetype)copyWithZone:(NSZone *)zone {
  4397. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4398. }
  4399. - (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
  4400. if (self == other) {
  4401. return YES;
  4402. }
  4403. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4404. return NO;
  4405. }
  4406. return [_dictionary isEqual:other->_dictionary];
  4407. }
  4408. - (NSUInteger)hash {
  4409. return _dictionary.count;
  4410. }
  4411. - (NSString *)description {
  4412. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4413. }
  4414. - (NSUInteger)count {
  4415. return _dictionary.count;
  4416. }
  4417. - (void)enumerateKeysAndValuesUsingBlock:
  4418. (void (^)(int32_t key, id value, BOOL *stop))block {
  4419. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4420. id aValue,
  4421. BOOL *stop) {
  4422. block([aKey intValue], aValue, stop);
  4423. }];
  4424. }
  4425. - (BOOL)isInitialized {
  4426. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4427. if (!msg.initialized) {
  4428. return NO;
  4429. }
  4430. }
  4431. return YES;
  4432. }
  4433. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4434. GPBInt32ObjectDictionary *newDict =
  4435. [[GPBInt32ObjectDictionary alloc] init];
  4436. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4437. GPBMessage *msg,
  4438. BOOL *stop) {
  4439. #pragma unused(stop)
  4440. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4441. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4442. [copiedMsg release];
  4443. }];
  4444. return newDict;
  4445. }
  4446. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4447. NSUInteger count = _dictionary.count;
  4448. if (count == 0) {
  4449. return 0;
  4450. }
  4451. GPBType valueType = GPBGetFieldType(field);
  4452. GPBType keyType = field.mapKeyType;
  4453. __block size_t result = 0;
  4454. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4455. id aValue,
  4456. BOOL *stop) {
  4457. #pragma unused(stop)
  4458. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4459. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  4460. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4461. }];
  4462. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4463. result += tagSize * count;
  4464. return result;
  4465. }
  4466. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4467. asField:(GPBFieldDescriptor *)field {
  4468. GPBType valueType = GPBGetFieldType(field);
  4469. GPBType keyType = field.mapKeyType;
  4470. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4471. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4472. id aValue,
  4473. BOOL *stop) {
  4474. #pragma unused(stop)
  4475. // Write the tag.
  4476. [outputStream writeInt32NoTag:tag];
  4477. // Write the size of the message.
  4478. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4479. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  4480. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4481. // Write the fields.
  4482. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4483. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  4484. }];
  4485. }
  4486. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4487. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4488. }
  4489. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4490. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, id value, BOOL *stop) {
  4491. #pragma unused(stop)
  4492. block([NSString stringWithFormat:@"%d", key], value);
  4493. }];
  4494. }
  4495. - (id)valueForKey:(int32_t)key {
  4496. id result = [_dictionary objectForKey:@(key)];
  4497. return result;
  4498. }
  4499. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4500. if (otherDictionary) {
  4501. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4502. }
  4503. }
  4504. - (void)setValue:(id)value forKey:(int32_t)key {
  4505. [_dictionary setObject:value forKey:@(key)];
  4506. }
  4507. - (void)removeValueForKey:(int32_t)aKey {
  4508. [_dictionary removeObjectForKey:@(aKey)];
  4509. }
  4510. - (void)removeAll {
  4511. [_dictionary removeAllObjects];
  4512. }
  4513. @end
  4514. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4515. // This block of code is generated, do not edit it directly.
  4516. #pragma mark - UInt64 -> UInt32
  4517. @implementation GPBUInt64UInt32Dictionary {
  4518. @package
  4519. NSMutableDictionary *_dictionary;
  4520. }
  4521. + (instancetype)dictionary {
  4522. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4523. }
  4524. + (instancetype)dictionaryWithValue:(uint32_t)value
  4525. forKey:(uint64_t)key {
  4526. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4527. // on to get the type correct.
  4528. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4529. forKeys:&key
  4530. count:1] autorelease];
  4531. }
  4532. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4533. forKeys:(const uint64_t [])keys
  4534. count:(NSUInteger)count {
  4535. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4536. // on to get the type correct.
  4537. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4538. forKeys:keys
  4539. count:count] autorelease];
  4540. }
  4541. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4542. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4543. // on to get the type correct.
  4544. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4545. }
  4546. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4547. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4548. }
  4549. - (instancetype)init {
  4550. return [self initWithValues:NULL forKeys:NULL count:0];
  4551. }
  4552. - (instancetype)initWithValues:(const uint32_t [])values
  4553. forKeys:(const uint64_t [])keys
  4554. count:(NSUInteger)count {
  4555. self = [super init];
  4556. if (self) {
  4557. _dictionary = [[NSMutableDictionary alloc] init];
  4558. if (count && values && keys) {
  4559. for (NSUInteger i = 0; i < count; ++i) {
  4560. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4561. }
  4562. }
  4563. }
  4564. return self;
  4565. }
  4566. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4567. self = [self initWithValues:NULL forKeys:NULL count:0];
  4568. if (self) {
  4569. if (dictionary) {
  4570. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4571. }
  4572. }
  4573. return self;
  4574. }
  4575. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4576. #pragma unused(numItems)
  4577. return [self initWithValues:NULL forKeys:NULL count:0];
  4578. }
  4579. - (void)dealloc {
  4580. [_dictionary release];
  4581. [super dealloc];
  4582. }
  4583. - (instancetype)copyWithZone:(NSZone *)zone {
  4584. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4585. }
  4586. - (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
  4587. if (self == other) {
  4588. return YES;
  4589. }
  4590. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4591. return NO;
  4592. }
  4593. return [_dictionary isEqual:other->_dictionary];
  4594. }
  4595. - (NSUInteger)hash {
  4596. return _dictionary.count;
  4597. }
  4598. - (NSString *)description {
  4599. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4600. }
  4601. - (NSUInteger)count {
  4602. return _dictionary.count;
  4603. }
  4604. - (void)enumerateKeysAndValuesUsingBlock:
  4605. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4606. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4607. NSNumber *aValue,
  4608. BOOL *stop) {
  4609. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4610. }];
  4611. }
  4612. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4613. NSUInteger count = _dictionary.count;
  4614. if (count == 0) {
  4615. return 0;
  4616. }
  4617. GPBType valueType = GPBGetFieldType(field);
  4618. GPBType keyType = field.mapKeyType;
  4619. __block size_t result = 0;
  4620. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4621. NSNumber *aValue,
  4622. BOOL *stop) {
  4623. #pragma unused(stop)
  4624. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4625. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  4626. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4627. }];
  4628. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4629. result += tagSize * count;
  4630. return result;
  4631. }
  4632. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4633. asField:(GPBFieldDescriptor *)field {
  4634. GPBType valueType = GPBGetFieldType(field);
  4635. GPBType keyType = field.mapKeyType;
  4636. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4637. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4638. NSNumber *aValue,
  4639. BOOL *stop) {
  4640. #pragma unused(stop)
  4641. // Write the tag.
  4642. [outputStream writeInt32NoTag:tag];
  4643. // Write the size of the message.
  4644. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4645. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  4646. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4647. // Write the fields.
  4648. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4649. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  4650. }];
  4651. }
  4652. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4653. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4654. }
  4655. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4656. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4657. #pragma unused(stop)
  4658. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4659. }];
  4660. }
  4661. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  4662. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4663. if (wrapped && value) {
  4664. *value = [wrapped unsignedIntValue];
  4665. }
  4666. return (wrapped != NULL);
  4667. }
  4668. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4669. if (otherDictionary) {
  4670. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4671. }
  4672. }
  4673. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  4674. [_dictionary setObject:@(value) forKey:@(key)];
  4675. }
  4676. - (void)removeValueForKey:(uint64_t)aKey {
  4677. [_dictionary removeObjectForKey:@(aKey)];
  4678. }
  4679. - (void)removeAll {
  4680. [_dictionary removeAllObjects];
  4681. }
  4682. @end
  4683. #pragma mark - UInt64 -> Int32
  4684. @implementation GPBUInt64Int32Dictionary {
  4685. @package
  4686. NSMutableDictionary *_dictionary;
  4687. }
  4688. + (instancetype)dictionary {
  4689. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4690. }
  4691. + (instancetype)dictionaryWithValue:(int32_t)value
  4692. forKey:(uint64_t)key {
  4693. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4694. // on to get the type correct.
  4695. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  4696. forKeys:&key
  4697. count:1] autorelease];
  4698. }
  4699. + (instancetype)dictionaryWithValues:(const int32_t [])values
  4700. forKeys:(const uint64_t [])keys
  4701. count:(NSUInteger)count {
  4702. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4703. // on to get the type correct.
  4704. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  4705. forKeys:keys
  4706. count:count] autorelease];
  4707. }
  4708. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4709. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4710. // on to get the type correct.
  4711. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4712. }
  4713. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4714. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4715. }
  4716. - (instancetype)init {
  4717. return [self initWithValues:NULL forKeys:NULL count:0];
  4718. }
  4719. - (instancetype)initWithValues:(const int32_t [])values
  4720. forKeys:(const uint64_t [])keys
  4721. count:(NSUInteger)count {
  4722. self = [super init];
  4723. if (self) {
  4724. _dictionary = [[NSMutableDictionary alloc] init];
  4725. if (count && values && keys) {
  4726. for (NSUInteger i = 0; i < count; ++i) {
  4727. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4728. }
  4729. }
  4730. }
  4731. return self;
  4732. }
  4733. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4734. self = [self initWithValues:NULL forKeys:NULL count:0];
  4735. if (self) {
  4736. if (dictionary) {
  4737. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4738. }
  4739. }
  4740. return self;
  4741. }
  4742. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4743. #pragma unused(numItems)
  4744. return [self initWithValues:NULL forKeys:NULL count:0];
  4745. }
  4746. - (void)dealloc {
  4747. [_dictionary release];
  4748. [super dealloc];
  4749. }
  4750. - (instancetype)copyWithZone:(NSZone *)zone {
  4751. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  4752. }
  4753. - (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
  4754. if (self == other) {
  4755. return YES;
  4756. }
  4757. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  4758. return NO;
  4759. }
  4760. return [_dictionary isEqual:other->_dictionary];
  4761. }
  4762. - (NSUInteger)hash {
  4763. return _dictionary.count;
  4764. }
  4765. - (NSString *)description {
  4766. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4767. }
  4768. - (NSUInteger)count {
  4769. return _dictionary.count;
  4770. }
  4771. - (void)enumerateKeysAndValuesUsingBlock:
  4772. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  4773. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4774. NSNumber *aValue,
  4775. BOOL *stop) {
  4776. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  4777. }];
  4778. }
  4779. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4780. NSUInteger count = _dictionary.count;
  4781. if (count == 0) {
  4782. return 0;
  4783. }
  4784. GPBType valueType = GPBGetFieldType(field);
  4785. GPBType keyType = field.mapKeyType;
  4786. __block size_t result = 0;
  4787. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4788. NSNumber *aValue,
  4789. BOOL *stop) {
  4790. #pragma unused(stop)
  4791. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4792. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  4793. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4794. }];
  4795. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4796. result += tagSize * count;
  4797. return result;
  4798. }
  4799. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4800. asField:(GPBFieldDescriptor *)field {
  4801. GPBType valueType = GPBGetFieldType(field);
  4802. GPBType keyType = field.mapKeyType;
  4803. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4804. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4805. NSNumber *aValue,
  4806. BOOL *stop) {
  4807. #pragma unused(stop)
  4808. // Write the tag.
  4809. [outputStream writeInt32NoTag:tag];
  4810. // Write the size of the message.
  4811. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4812. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  4813. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4814. // Write the fields.
  4815. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4816. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  4817. }];
  4818. }
  4819. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4820. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  4821. }
  4822. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4823. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  4824. #pragma unused(stop)
  4825. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  4826. }];
  4827. }
  4828. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  4829. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4830. if (wrapped && value) {
  4831. *value = [wrapped intValue];
  4832. }
  4833. return (wrapped != NULL);
  4834. }
  4835. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  4836. if (otherDictionary) {
  4837. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4838. }
  4839. }
  4840. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  4841. [_dictionary setObject:@(value) forKey:@(key)];
  4842. }
  4843. - (void)removeValueForKey:(uint64_t)aKey {
  4844. [_dictionary removeObjectForKey:@(aKey)];
  4845. }
  4846. - (void)removeAll {
  4847. [_dictionary removeAllObjects];
  4848. }
  4849. @end
  4850. #pragma mark - UInt64 -> UInt64
  4851. @implementation GPBUInt64UInt64Dictionary {
  4852. @package
  4853. NSMutableDictionary *_dictionary;
  4854. }
  4855. + (instancetype)dictionary {
  4856. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4857. }
  4858. + (instancetype)dictionaryWithValue:(uint64_t)value
  4859. forKey:(uint64_t)key {
  4860. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4861. // on to get the type correct.
  4862. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  4863. forKeys:&key
  4864. count:1] autorelease];
  4865. }
  4866. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  4867. forKeys:(const uint64_t [])keys
  4868. count:(NSUInteger)count {
  4869. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4870. // on to get the type correct.
  4871. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  4872. forKeys:keys
  4873. count:count] autorelease];
  4874. }
  4875. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  4876. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4877. // on to get the type correct.
  4878. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4879. }
  4880. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4881. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4882. }
  4883. - (instancetype)init {
  4884. return [self initWithValues:NULL forKeys:NULL count:0];
  4885. }
  4886. - (instancetype)initWithValues:(const uint64_t [])values
  4887. forKeys:(const uint64_t [])keys
  4888. count:(NSUInteger)count {
  4889. self = [super init];
  4890. if (self) {
  4891. _dictionary = [[NSMutableDictionary alloc] init];
  4892. if (count && values && keys) {
  4893. for (NSUInteger i = 0; i < count; ++i) {
  4894. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4895. }
  4896. }
  4897. }
  4898. return self;
  4899. }
  4900. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  4901. self = [self initWithValues:NULL forKeys:NULL count:0];
  4902. if (self) {
  4903. if (dictionary) {
  4904. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4905. }
  4906. }
  4907. return self;
  4908. }
  4909. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4910. #pragma unused(numItems)
  4911. return [self initWithValues:NULL forKeys:NULL count:0];
  4912. }
  4913. - (void)dealloc {
  4914. [_dictionary release];
  4915. [super dealloc];
  4916. }
  4917. - (instancetype)copyWithZone:(NSZone *)zone {
  4918. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  4919. }
  4920. - (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
  4921. if (self == other) {
  4922. return YES;
  4923. }
  4924. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  4925. return NO;
  4926. }
  4927. return [_dictionary isEqual:other->_dictionary];
  4928. }
  4929. - (NSUInteger)hash {
  4930. return _dictionary.count;
  4931. }
  4932. - (NSString *)description {
  4933. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4934. }
  4935. - (NSUInteger)count {
  4936. return _dictionary.count;
  4937. }
  4938. - (void)enumerateKeysAndValuesUsingBlock:
  4939. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  4940. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4941. NSNumber *aValue,
  4942. BOOL *stop) {
  4943. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  4944. }];
  4945. }
  4946. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4947. NSUInteger count = _dictionary.count;
  4948. if (count == 0) {
  4949. return 0;
  4950. }
  4951. GPBType valueType = GPBGetFieldType(field);
  4952. GPBType keyType = field.mapKeyType;
  4953. __block size_t result = 0;
  4954. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4955. NSNumber *aValue,
  4956. BOOL *stop) {
  4957. #pragma unused(stop)
  4958. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4959. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  4960. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4961. }];
  4962. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4963. result += tagSize * count;
  4964. return result;
  4965. }
  4966. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4967. asField:(GPBFieldDescriptor *)field {
  4968. GPBType valueType = GPBGetFieldType(field);
  4969. GPBType keyType = field.mapKeyType;
  4970. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4971. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4972. NSNumber *aValue,
  4973. BOOL *stop) {
  4974. #pragma unused(stop)
  4975. // Write the tag.
  4976. [outputStream writeInt32NoTag:tag];
  4977. // Write the size of the message.
  4978. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4979. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  4980. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4981. // Write the fields.
  4982. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4983. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  4984. }];
  4985. }
  4986. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4987. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  4988. }
  4989. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4990. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  4991. #pragma unused(stop)
  4992. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  4993. }];
  4994. }
  4995. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  4996. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4997. if (wrapped && value) {
  4998. *value = [wrapped unsignedLongLongValue];
  4999. }
  5000. return (wrapped != NULL);
  5001. }
  5002. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5003. if (otherDictionary) {
  5004. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5005. }
  5006. }
  5007. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5008. [_dictionary setObject:@(value) forKey:@(key)];
  5009. }
  5010. - (void)removeValueForKey:(uint64_t)aKey {
  5011. [_dictionary removeObjectForKey:@(aKey)];
  5012. }
  5013. - (void)removeAll {
  5014. [_dictionary removeAllObjects];
  5015. }
  5016. @end
  5017. #pragma mark - UInt64 -> Int64
  5018. @implementation GPBUInt64Int64Dictionary {
  5019. @package
  5020. NSMutableDictionary *_dictionary;
  5021. }
  5022. + (instancetype)dictionary {
  5023. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5024. }
  5025. + (instancetype)dictionaryWithValue:(int64_t)value
  5026. forKey:(uint64_t)key {
  5027. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5028. // on to get the type correct.
  5029. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5030. forKeys:&key
  5031. count:1] autorelease];
  5032. }
  5033. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5034. forKeys:(const uint64_t [])keys
  5035. count:(NSUInteger)count {
  5036. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5037. // on to get the type correct.
  5038. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5039. forKeys:keys
  5040. count:count] autorelease];
  5041. }
  5042. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5043. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5044. // on to get the type correct.
  5045. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5046. }
  5047. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5048. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5049. }
  5050. - (instancetype)init {
  5051. return [self initWithValues:NULL forKeys:NULL count:0];
  5052. }
  5053. - (instancetype)initWithValues:(const int64_t [])values
  5054. forKeys:(const uint64_t [])keys
  5055. count:(NSUInteger)count {
  5056. self = [super init];
  5057. if (self) {
  5058. _dictionary = [[NSMutableDictionary alloc] init];
  5059. if (count && values && keys) {
  5060. for (NSUInteger i = 0; i < count; ++i) {
  5061. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5062. }
  5063. }
  5064. }
  5065. return self;
  5066. }
  5067. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5068. self = [self initWithValues:NULL forKeys:NULL count:0];
  5069. if (self) {
  5070. if (dictionary) {
  5071. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5072. }
  5073. }
  5074. return self;
  5075. }
  5076. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5077. #pragma unused(numItems)
  5078. return [self initWithValues:NULL forKeys:NULL count:0];
  5079. }
  5080. - (void)dealloc {
  5081. [_dictionary release];
  5082. [super dealloc];
  5083. }
  5084. - (instancetype)copyWithZone:(NSZone *)zone {
  5085. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5086. }
  5087. - (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
  5088. if (self == other) {
  5089. return YES;
  5090. }
  5091. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5092. return NO;
  5093. }
  5094. return [_dictionary isEqual:other->_dictionary];
  5095. }
  5096. - (NSUInteger)hash {
  5097. return _dictionary.count;
  5098. }
  5099. - (NSString *)description {
  5100. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5101. }
  5102. - (NSUInteger)count {
  5103. return _dictionary.count;
  5104. }
  5105. - (void)enumerateKeysAndValuesUsingBlock:
  5106. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5107. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5108. NSNumber *aValue,
  5109. BOOL *stop) {
  5110. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5111. }];
  5112. }
  5113. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5114. NSUInteger count = _dictionary.count;
  5115. if (count == 0) {
  5116. return 0;
  5117. }
  5118. GPBType valueType = GPBGetFieldType(field);
  5119. GPBType keyType = field.mapKeyType;
  5120. __block size_t result = 0;
  5121. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5122. NSNumber *aValue,
  5123. BOOL *stop) {
  5124. #pragma unused(stop)
  5125. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5126. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  5127. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5128. }];
  5129. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5130. result += tagSize * count;
  5131. return result;
  5132. }
  5133. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5134. asField:(GPBFieldDescriptor *)field {
  5135. GPBType valueType = GPBGetFieldType(field);
  5136. GPBType keyType = field.mapKeyType;
  5137. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5138. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5139. NSNumber *aValue,
  5140. BOOL *stop) {
  5141. #pragma unused(stop)
  5142. // Write the tag.
  5143. [outputStream writeInt32NoTag:tag];
  5144. // Write the size of the message.
  5145. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5146. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  5147. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5148. // Write the fields.
  5149. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5150. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  5151. }];
  5152. }
  5153. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5154. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5155. }
  5156. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5157. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5158. #pragma unused(stop)
  5159. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5160. }];
  5161. }
  5162. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5163. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5164. if (wrapped && value) {
  5165. *value = [wrapped longLongValue];
  5166. }
  5167. return (wrapped != NULL);
  5168. }
  5169. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5170. if (otherDictionary) {
  5171. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5172. }
  5173. }
  5174. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5175. [_dictionary setObject:@(value) forKey:@(key)];
  5176. }
  5177. - (void)removeValueForKey:(uint64_t)aKey {
  5178. [_dictionary removeObjectForKey:@(aKey)];
  5179. }
  5180. - (void)removeAll {
  5181. [_dictionary removeAllObjects];
  5182. }
  5183. @end
  5184. #pragma mark - UInt64 -> Bool
  5185. @implementation GPBUInt64BoolDictionary {
  5186. @package
  5187. NSMutableDictionary *_dictionary;
  5188. }
  5189. + (instancetype)dictionary {
  5190. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5191. }
  5192. + (instancetype)dictionaryWithValue:(BOOL)value
  5193. forKey:(uint64_t)key {
  5194. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5195. // on to get the type correct.
  5196. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5197. forKeys:&key
  5198. count:1] autorelease];
  5199. }
  5200. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5201. forKeys:(const uint64_t [])keys
  5202. count:(NSUInteger)count {
  5203. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5204. // on to get the type correct.
  5205. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5206. forKeys:keys
  5207. count:count] autorelease];
  5208. }
  5209. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5210. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5211. // on to get the type correct.
  5212. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5213. }
  5214. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5215. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5216. }
  5217. - (instancetype)init {
  5218. return [self initWithValues:NULL forKeys:NULL count:0];
  5219. }
  5220. - (instancetype)initWithValues:(const BOOL [])values
  5221. forKeys:(const uint64_t [])keys
  5222. count:(NSUInteger)count {
  5223. self = [super init];
  5224. if (self) {
  5225. _dictionary = [[NSMutableDictionary alloc] init];
  5226. if (count && values && keys) {
  5227. for (NSUInteger i = 0; i < count; ++i) {
  5228. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5229. }
  5230. }
  5231. }
  5232. return self;
  5233. }
  5234. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5235. self = [self initWithValues:NULL forKeys:NULL count:0];
  5236. if (self) {
  5237. if (dictionary) {
  5238. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5239. }
  5240. }
  5241. return self;
  5242. }
  5243. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5244. #pragma unused(numItems)
  5245. return [self initWithValues:NULL forKeys:NULL count:0];
  5246. }
  5247. - (void)dealloc {
  5248. [_dictionary release];
  5249. [super dealloc];
  5250. }
  5251. - (instancetype)copyWithZone:(NSZone *)zone {
  5252. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5253. }
  5254. - (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
  5255. if (self == other) {
  5256. return YES;
  5257. }
  5258. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5259. return NO;
  5260. }
  5261. return [_dictionary isEqual:other->_dictionary];
  5262. }
  5263. - (NSUInteger)hash {
  5264. return _dictionary.count;
  5265. }
  5266. - (NSString *)description {
  5267. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5268. }
  5269. - (NSUInteger)count {
  5270. return _dictionary.count;
  5271. }
  5272. - (void)enumerateKeysAndValuesUsingBlock:
  5273. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5274. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5275. NSNumber *aValue,
  5276. BOOL *stop) {
  5277. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5278. }];
  5279. }
  5280. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5281. NSUInteger count = _dictionary.count;
  5282. if (count == 0) {
  5283. return 0;
  5284. }
  5285. GPBType valueType = GPBGetFieldType(field);
  5286. GPBType keyType = field.mapKeyType;
  5287. __block size_t result = 0;
  5288. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5289. NSNumber *aValue,
  5290. BOOL *stop) {
  5291. #pragma unused(stop)
  5292. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5293. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  5294. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5295. }];
  5296. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5297. result += tagSize * count;
  5298. return result;
  5299. }
  5300. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5301. asField:(GPBFieldDescriptor *)field {
  5302. GPBType valueType = GPBGetFieldType(field);
  5303. GPBType keyType = field.mapKeyType;
  5304. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5305. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5306. NSNumber *aValue,
  5307. BOOL *stop) {
  5308. #pragma unused(stop)
  5309. // Write the tag.
  5310. [outputStream writeInt32NoTag:tag];
  5311. // Write the size of the message.
  5312. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5313. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  5314. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5315. // Write the fields.
  5316. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5317. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  5318. }];
  5319. }
  5320. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5321. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5322. }
  5323. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5324. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5325. #pragma unused(stop)
  5326. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5327. }];
  5328. }
  5329. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5330. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5331. if (wrapped && value) {
  5332. *value = [wrapped boolValue];
  5333. }
  5334. return (wrapped != NULL);
  5335. }
  5336. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5337. if (otherDictionary) {
  5338. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5339. }
  5340. }
  5341. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5342. [_dictionary setObject:@(value) forKey:@(key)];
  5343. }
  5344. - (void)removeValueForKey:(uint64_t)aKey {
  5345. [_dictionary removeObjectForKey:@(aKey)];
  5346. }
  5347. - (void)removeAll {
  5348. [_dictionary removeAllObjects];
  5349. }
  5350. @end
  5351. #pragma mark - UInt64 -> Float
  5352. @implementation GPBUInt64FloatDictionary {
  5353. @package
  5354. NSMutableDictionary *_dictionary;
  5355. }
  5356. + (instancetype)dictionary {
  5357. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5358. }
  5359. + (instancetype)dictionaryWithValue:(float)value
  5360. forKey:(uint64_t)key {
  5361. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5362. // on to get the type correct.
  5363. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5364. forKeys:&key
  5365. count:1] autorelease];
  5366. }
  5367. + (instancetype)dictionaryWithValues:(const float [])values
  5368. forKeys:(const uint64_t [])keys
  5369. count:(NSUInteger)count {
  5370. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5371. // on to get the type correct.
  5372. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5373. forKeys:keys
  5374. count:count] autorelease];
  5375. }
  5376. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5377. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5378. // on to get the type correct.
  5379. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5380. }
  5381. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5382. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5383. }
  5384. - (instancetype)init {
  5385. return [self initWithValues:NULL forKeys:NULL count:0];
  5386. }
  5387. - (instancetype)initWithValues:(const float [])values
  5388. forKeys:(const uint64_t [])keys
  5389. count:(NSUInteger)count {
  5390. self = [super init];
  5391. if (self) {
  5392. _dictionary = [[NSMutableDictionary alloc] init];
  5393. if (count && values && keys) {
  5394. for (NSUInteger i = 0; i < count; ++i) {
  5395. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5396. }
  5397. }
  5398. }
  5399. return self;
  5400. }
  5401. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5402. self = [self initWithValues:NULL forKeys:NULL count:0];
  5403. if (self) {
  5404. if (dictionary) {
  5405. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5406. }
  5407. }
  5408. return self;
  5409. }
  5410. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5411. #pragma unused(numItems)
  5412. return [self initWithValues:NULL forKeys:NULL count:0];
  5413. }
  5414. - (void)dealloc {
  5415. [_dictionary release];
  5416. [super dealloc];
  5417. }
  5418. - (instancetype)copyWithZone:(NSZone *)zone {
  5419. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5420. }
  5421. - (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
  5422. if (self == other) {
  5423. return YES;
  5424. }
  5425. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5426. return NO;
  5427. }
  5428. return [_dictionary isEqual:other->_dictionary];
  5429. }
  5430. - (NSUInteger)hash {
  5431. return _dictionary.count;
  5432. }
  5433. - (NSString *)description {
  5434. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5435. }
  5436. - (NSUInteger)count {
  5437. return _dictionary.count;
  5438. }
  5439. - (void)enumerateKeysAndValuesUsingBlock:
  5440. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5441. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5442. NSNumber *aValue,
  5443. BOOL *stop) {
  5444. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5445. }];
  5446. }
  5447. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5448. NSUInteger count = _dictionary.count;
  5449. if (count == 0) {
  5450. return 0;
  5451. }
  5452. GPBType valueType = GPBGetFieldType(field);
  5453. GPBType keyType = field.mapKeyType;
  5454. __block size_t result = 0;
  5455. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5456. NSNumber *aValue,
  5457. BOOL *stop) {
  5458. #pragma unused(stop)
  5459. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5460. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  5461. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5462. }];
  5463. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5464. result += tagSize * count;
  5465. return result;
  5466. }
  5467. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5468. asField:(GPBFieldDescriptor *)field {
  5469. GPBType valueType = GPBGetFieldType(field);
  5470. GPBType keyType = field.mapKeyType;
  5471. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5472. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5473. NSNumber *aValue,
  5474. BOOL *stop) {
  5475. #pragma unused(stop)
  5476. // Write the tag.
  5477. [outputStream writeInt32NoTag:tag];
  5478. // Write the size of the message.
  5479. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5480. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  5481. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5482. // Write the fields.
  5483. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5484. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  5485. }];
  5486. }
  5487. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5488. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5489. }
  5490. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5491. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5492. #pragma unused(stop)
  5493. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5494. }];
  5495. }
  5496. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5497. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5498. if (wrapped && value) {
  5499. *value = [wrapped floatValue];
  5500. }
  5501. return (wrapped != NULL);
  5502. }
  5503. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5504. if (otherDictionary) {
  5505. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5506. }
  5507. }
  5508. - (void)setValue:(float)value forKey:(uint64_t)key {
  5509. [_dictionary setObject:@(value) forKey:@(key)];
  5510. }
  5511. - (void)removeValueForKey:(uint64_t)aKey {
  5512. [_dictionary removeObjectForKey:@(aKey)];
  5513. }
  5514. - (void)removeAll {
  5515. [_dictionary removeAllObjects];
  5516. }
  5517. @end
  5518. #pragma mark - UInt64 -> Double
  5519. @implementation GPBUInt64DoubleDictionary {
  5520. @package
  5521. NSMutableDictionary *_dictionary;
  5522. }
  5523. + (instancetype)dictionary {
  5524. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5525. }
  5526. + (instancetype)dictionaryWithValue:(double)value
  5527. forKey:(uint64_t)key {
  5528. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5529. // on to get the type correct.
  5530. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5531. forKeys:&key
  5532. count:1] autorelease];
  5533. }
  5534. + (instancetype)dictionaryWithValues:(const double [])values
  5535. forKeys:(const uint64_t [])keys
  5536. count:(NSUInteger)count {
  5537. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5538. // on to get the type correct.
  5539. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5540. forKeys:keys
  5541. count:count] autorelease];
  5542. }
  5543. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5544. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5545. // on to get the type correct.
  5546. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5547. }
  5548. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5549. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5550. }
  5551. - (instancetype)init {
  5552. return [self initWithValues:NULL forKeys:NULL count:0];
  5553. }
  5554. - (instancetype)initWithValues:(const double [])values
  5555. forKeys:(const uint64_t [])keys
  5556. count:(NSUInteger)count {
  5557. self = [super init];
  5558. if (self) {
  5559. _dictionary = [[NSMutableDictionary alloc] init];
  5560. if (count && values && keys) {
  5561. for (NSUInteger i = 0; i < count; ++i) {
  5562. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5563. }
  5564. }
  5565. }
  5566. return self;
  5567. }
  5568. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5569. self = [self initWithValues:NULL forKeys:NULL count:0];
  5570. if (self) {
  5571. if (dictionary) {
  5572. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5573. }
  5574. }
  5575. return self;
  5576. }
  5577. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5578. #pragma unused(numItems)
  5579. return [self initWithValues:NULL forKeys:NULL count:0];
  5580. }
  5581. - (void)dealloc {
  5582. [_dictionary release];
  5583. [super dealloc];
  5584. }
  5585. - (instancetype)copyWithZone:(NSZone *)zone {
  5586. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5587. }
  5588. - (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
  5589. if (self == other) {
  5590. return YES;
  5591. }
  5592. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5593. return NO;
  5594. }
  5595. return [_dictionary isEqual:other->_dictionary];
  5596. }
  5597. - (NSUInteger)hash {
  5598. return _dictionary.count;
  5599. }
  5600. - (NSString *)description {
  5601. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5602. }
  5603. - (NSUInteger)count {
  5604. return _dictionary.count;
  5605. }
  5606. - (void)enumerateKeysAndValuesUsingBlock:
  5607. (void (^)(uint64_t key, double value, BOOL *stop))block {
  5608. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5609. NSNumber *aValue,
  5610. BOOL *stop) {
  5611. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  5612. }];
  5613. }
  5614. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5615. NSUInteger count = _dictionary.count;
  5616. if (count == 0) {
  5617. return 0;
  5618. }
  5619. GPBType valueType = GPBGetFieldType(field);
  5620. GPBType keyType = field.mapKeyType;
  5621. __block size_t result = 0;
  5622. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5623. NSNumber *aValue,
  5624. BOOL *stop) {
  5625. #pragma unused(stop)
  5626. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5627. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  5628. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5629. }];
  5630. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5631. result += tagSize * count;
  5632. return result;
  5633. }
  5634. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5635. asField:(GPBFieldDescriptor *)field {
  5636. GPBType valueType = GPBGetFieldType(field);
  5637. GPBType keyType = field.mapKeyType;
  5638. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5639. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5640. NSNumber *aValue,
  5641. BOOL *stop) {
  5642. #pragma unused(stop)
  5643. // Write the tag.
  5644. [outputStream writeInt32NoTag:tag];
  5645. // Write the size of the message.
  5646. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5647. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  5648. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5649. // Write the fields.
  5650. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5651. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  5652. }];
  5653. }
  5654. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5655. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5656. }
  5657. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5658. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  5659. #pragma unused(stop)
  5660. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5661. }];
  5662. }
  5663. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  5664. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5665. if (wrapped && value) {
  5666. *value = [wrapped doubleValue];
  5667. }
  5668. return (wrapped != NULL);
  5669. }
  5670. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5671. if (otherDictionary) {
  5672. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5673. }
  5674. }
  5675. - (void)setValue:(double)value forKey:(uint64_t)key {
  5676. [_dictionary setObject:@(value) forKey:@(key)];
  5677. }
  5678. - (void)removeValueForKey:(uint64_t)aKey {
  5679. [_dictionary removeObjectForKey:@(aKey)];
  5680. }
  5681. - (void)removeAll {
  5682. [_dictionary removeAllObjects];
  5683. }
  5684. @end
  5685. #pragma mark - UInt64 -> Enum
  5686. @implementation GPBUInt64EnumDictionary {
  5687. @package
  5688. NSMutableDictionary *_dictionary;
  5689. GPBEnumValidationFunc _validationFunc;
  5690. }
  5691. @synthesize validationFunc = _validationFunc;
  5692. + (instancetype)dictionary {
  5693. return [[[self alloc] initWithValidationFunction:NULL
  5694. rawValues:NULL
  5695. forKeys:NULL
  5696. count:0] autorelease];
  5697. }
  5698. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  5699. return [[[self alloc] initWithValidationFunction:func
  5700. rawValues:NULL
  5701. forKeys:NULL
  5702. count:0] autorelease];
  5703. }
  5704. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  5705. rawValue:(int32_t)rawValue
  5706. forKey:(uint64_t)key {
  5707. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5708. // on to get the type correct.
  5709. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  5710. rawValues:&rawValue
  5711. forKeys:&key
  5712. count:1] autorelease];
  5713. }
  5714. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  5715. rawValues:(const int32_t [])rawValues
  5716. forKeys:(const uint64_t [])keys
  5717. count:(NSUInteger)count {
  5718. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5719. // on to get the type correct.
  5720. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  5721. rawValues:rawValues
  5722. forKeys:keys
  5723. count:count] autorelease];
  5724. }
  5725. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  5726. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5727. // on to get the type correct.
  5728. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5729. }
  5730. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  5731. capacity:(NSUInteger)numItems {
  5732. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  5733. }
  5734. - (instancetype)init {
  5735. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  5736. }
  5737. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  5738. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5739. }
  5740. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5741. rawValues:(const int32_t [])rawValues
  5742. forKeys:(const uint64_t [])keys
  5743. count:(NSUInteger)count {
  5744. self = [super init];
  5745. if (self) {
  5746. _dictionary = [[NSMutableDictionary alloc] init];
  5747. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  5748. if (count && rawValues && keys) {
  5749. for (NSUInteger i = 0; i < count; ++i) {
  5750. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  5751. }
  5752. }
  5753. }
  5754. return self;
  5755. }
  5756. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  5757. self = [self initWithValidationFunction:dictionary.validationFunc
  5758. rawValues:NULL
  5759. forKeys:NULL
  5760. count:0];
  5761. if (self) {
  5762. if (dictionary) {
  5763. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5764. }
  5765. }
  5766. return self;
  5767. }
  5768. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5769. capacity:(NSUInteger)numItems {
  5770. #pragma unused(numItems)
  5771. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5772. }
  5773. - (void)dealloc {
  5774. [_dictionary release];
  5775. [super dealloc];
  5776. }
  5777. - (instancetype)copyWithZone:(NSZone *)zone {
  5778. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  5779. }
  5780. - (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
  5781. if (self == other) {
  5782. return YES;
  5783. }
  5784. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  5785. return NO;
  5786. }
  5787. return [_dictionary isEqual:other->_dictionary];
  5788. }
  5789. - (NSUInteger)hash {
  5790. return _dictionary.count;
  5791. }
  5792. - (NSString *)description {
  5793. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5794. }
  5795. - (NSUInteger)count {
  5796. return _dictionary.count;
  5797. }
  5798. - (void)enumerateKeysAndRawValuesUsingBlock:
  5799. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5800. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5801. NSNumber *aValue,
  5802. BOOL *stop) {
  5803. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5804. }];
  5805. }
  5806. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5807. NSUInteger count = _dictionary.count;
  5808. if (count == 0) {
  5809. return 0;
  5810. }
  5811. GPBType valueType = GPBGetFieldType(field);
  5812. GPBType keyType = field.mapKeyType;
  5813. __block size_t result = 0;
  5814. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5815. NSNumber *aValue,
  5816. BOOL *stop) {
  5817. #pragma unused(stop)
  5818. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5819. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  5820. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5821. }];
  5822. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5823. result += tagSize * count;
  5824. return result;
  5825. }
  5826. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5827. asField:(GPBFieldDescriptor *)field {
  5828. GPBType valueType = GPBGetFieldType(field);
  5829. GPBType keyType = field.mapKeyType;
  5830. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5831. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5832. NSNumber *aValue,
  5833. BOOL *stop) {
  5834. #pragma unused(stop)
  5835. // Write the tag.
  5836. [outputStream writeInt32NoTag:tag];
  5837. // Write the size of the message.
  5838. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5839. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  5840. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5841. // Write the fields.
  5842. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5843. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  5844. }];
  5845. }
  5846. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  5847. forKey:(GPBValue *)key
  5848. keyType:(GPBType)keyType {
  5849. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyType);
  5850. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  5851. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  5852. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  5853. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyType);
  5854. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  5855. [outputStream release];
  5856. return data;
  5857. }
  5858. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5859. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  5860. }
  5861. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5862. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5863. #pragma unused(stop)
  5864. block([NSString stringWithFormat:@"%llu", key], @(value));
  5865. }];
  5866. }
  5867. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5868. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5869. if (wrapped && value) {
  5870. int32_t result = [wrapped intValue];
  5871. if (!_validationFunc(result)) {
  5872. result = kGPBUnrecognizedEnumeratorValue;
  5873. }
  5874. *value = result;
  5875. }
  5876. return (wrapped != NULL);
  5877. }
  5878. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  5879. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5880. if (wrapped && rawValue) {
  5881. *rawValue = [wrapped intValue];
  5882. }
  5883. return (wrapped != NULL);
  5884. }
  5885. - (void)enumerateKeysAndValuesUsingBlock:
  5886. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5887. GPBEnumValidationFunc func = _validationFunc;
  5888. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5889. NSNumber *aValue,
  5890. BOOL *stop) {
  5891. int32_t unwrapped = [aValue intValue];
  5892. if (!func(unwrapped)) {
  5893. unwrapped = kGPBUnrecognizedEnumeratorValue;
  5894. }
  5895. block([aKey unsignedLongLongValue], unwrapped, stop);
  5896. }];
  5897. }
  5898. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  5899. if (otherDictionary) {
  5900. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5901. }
  5902. }
  5903. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  5904. [_dictionary setObject:@(value) forKey:@(key)];
  5905. }
  5906. - (void)removeValueForKey:(uint64_t)aKey {
  5907. [_dictionary removeObjectForKey:@(aKey)];
  5908. }
  5909. - (void)removeAll {
  5910. [_dictionary removeAllObjects];
  5911. }
  5912. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5913. if (!_validationFunc(value)) {
  5914. [NSException raise:NSInvalidArgumentException
  5915. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  5916. value];
  5917. }
  5918. [_dictionary setObject:@(value) forKey:@(key)];
  5919. }
  5920. @end
  5921. #pragma mark - UInt64 -> Object
  5922. @implementation GPBUInt64ObjectDictionary {
  5923. @package
  5924. NSMutableDictionary *_dictionary;
  5925. }
  5926. + (instancetype)dictionary {
  5927. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5928. }
  5929. + (instancetype)dictionaryWithValue:(id)value
  5930. forKey:(uint64_t)key {
  5931. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5932. // on to get the type correct.
  5933. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithValues:&value
  5934. forKeys:&key
  5935. count:1] autorelease];
  5936. }
  5937. + (instancetype)dictionaryWithValues:(const id [])values
  5938. forKeys:(const uint64_t [])keys
  5939. count:(NSUInteger)count {
  5940. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5941. // on to get the type correct.
  5942. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithValues:values
  5943. forKeys:keys
  5944. count:count] autorelease];
  5945. }
  5946. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  5947. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5948. // on to get the type correct.
  5949. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5950. }
  5951. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5952. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5953. }
  5954. - (instancetype)init {
  5955. return [self initWithValues:NULL forKeys:NULL count:0];
  5956. }
  5957. - (instancetype)initWithValues:(const id [])values
  5958. forKeys:(const uint64_t [])keys
  5959. count:(NSUInteger)count {
  5960. self = [super init];
  5961. if (self) {
  5962. _dictionary = [[NSMutableDictionary alloc] init];
  5963. if (count && values && keys) {
  5964. for (NSUInteger i = 0; i < count; ++i) {
  5965. [_dictionary setObject:values[i] forKey:@(keys[i])];
  5966. }
  5967. }
  5968. }
  5969. return self;
  5970. }
  5971. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  5972. self = [self initWithValues:NULL forKeys:NULL count:0];
  5973. if (self) {
  5974. if (dictionary) {
  5975. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5976. }
  5977. }
  5978. return self;
  5979. }
  5980. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5981. #pragma unused(numItems)
  5982. return [self initWithValues:NULL forKeys:NULL count:0];
  5983. }
  5984. - (void)dealloc {
  5985. [_dictionary release];
  5986. [super dealloc];
  5987. }
  5988. - (instancetype)copyWithZone:(NSZone *)zone {
  5989. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  5990. }
  5991. - (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
  5992. if (self == other) {
  5993. return YES;
  5994. }
  5995. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  5996. return NO;
  5997. }
  5998. return [_dictionary isEqual:other->_dictionary];
  5999. }
  6000. - (NSUInteger)hash {
  6001. return _dictionary.count;
  6002. }
  6003. - (NSString *)description {
  6004. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6005. }
  6006. - (NSUInteger)count {
  6007. return _dictionary.count;
  6008. }
  6009. - (void)enumerateKeysAndValuesUsingBlock:
  6010. (void (^)(uint64_t key, id value, BOOL *stop))block {
  6011. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6012. id aValue,
  6013. BOOL *stop) {
  6014. block([aKey unsignedLongLongValue], aValue, stop);
  6015. }];
  6016. }
  6017. - (BOOL)isInitialized {
  6018. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6019. if (!msg.initialized) {
  6020. return NO;
  6021. }
  6022. }
  6023. return YES;
  6024. }
  6025. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6026. GPBUInt64ObjectDictionary *newDict =
  6027. [[GPBUInt64ObjectDictionary alloc] init];
  6028. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6029. GPBMessage *msg,
  6030. BOOL *stop) {
  6031. #pragma unused(stop)
  6032. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6033. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6034. [copiedMsg release];
  6035. }];
  6036. return newDict;
  6037. }
  6038. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6039. NSUInteger count = _dictionary.count;
  6040. if (count == 0) {
  6041. return 0;
  6042. }
  6043. GPBType valueType = GPBGetFieldType(field);
  6044. GPBType keyType = field.mapKeyType;
  6045. __block size_t result = 0;
  6046. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6047. id aValue,
  6048. BOOL *stop) {
  6049. #pragma unused(stop)
  6050. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6051. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  6052. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6053. }];
  6054. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6055. result += tagSize * count;
  6056. return result;
  6057. }
  6058. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6059. asField:(GPBFieldDescriptor *)field {
  6060. GPBType valueType = GPBGetFieldType(field);
  6061. GPBType keyType = field.mapKeyType;
  6062. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6063. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6064. id aValue,
  6065. BOOL *stop) {
  6066. #pragma unused(stop)
  6067. // Write the tag.
  6068. [outputStream writeInt32NoTag:tag];
  6069. // Write the size of the message.
  6070. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6071. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  6072. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6073. // Write the fields.
  6074. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6075. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  6076. }];
  6077. }
  6078. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6079. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6080. }
  6081. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6082. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, id value, BOOL *stop) {
  6083. #pragma unused(stop)
  6084. block([NSString stringWithFormat:@"%llu", key], value);
  6085. }];
  6086. }
  6087. - (id)valueForKey:(uint64_t)key {
  6088. id result = [_dictionary objectForKey:@(key)];
  6089. return result;
  6090. }
  6091. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6092. if (otherDictionary) {
  6093. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6094. }
  6095. }
  6096. - (void)setValue:(id)value forKey:(uint64_t)key {
  6097. [_dictionary setObject:value forKey:@(key)];
  6098. }
  6099. - (void)removeValueForKey:(uint64_t)aKey {
  6100. [_dictionary removeObjectForKey:@(aKey)];
  6101. }
  6102. - (void)removeAll {
  6103. [_dictionary removeAllObjects];
  6104. }
  6105. @end
  6106. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6107. // This block of code is generated, do not edit it directly.
  6108. #pragma mark - Int64 -> UInt32
  6109. @implementation GPBInt64UInt32Dictionary {
  6110. @package
  6111. NSMutableDictionary *_dictionary;
  6112. }
  6113. + (instancetype)dictionary {
  6114. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6115. }
  6116. + (instancetype)dictionaryWithValue:(uint32_t)value
  6117. forKey:(int64_t)key {
  6118. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6119. // on to get the type correct.
  6120. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6121. forKeys:&key
  6122. count:1] autorelease];
  6123. }
  6124. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6125. forKeys:(const int64_t [])keys
  6126. count:(NSUInteger)count {
  6127. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6128. // on to get the type correct.
  6129. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6130. forKeys:keys
  6131. count:count] autorelease];
  6132. }
  6133. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6134. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6135. // on to get the type correct.
  6136. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6137. }
  6138. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6139. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6140. }
  6141. - (instancetype)init {
  6142. return [self initWithValues:NULL forKeys:NULL count:0];
  6143. }
  6144. - (instancetype)initWithValues:(const uint32_t [])values
  6145. forKeys:(const int64_t [])keys
  6146. count:(NSUInteger)count {
  6147. self = [super init];
  6148. if (self) {
  6149. _dictionary = [[NSMutableDictionary alloc] init];
  6150. if (count && values && keys) {
  6151. for (NSUInteger i = 0; i < count; ++i) {
  6152. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6153. }
  6154. }
  6155. }
  6156. return self;
  6157. }
  6158. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6159. self = [self initWithValues:NULL forKeys:NULL count:0];
  6160. if (self) {
  6161. if (dictionary) {
  6162. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6163. }
  6164. }
  6165. return self;
  6166. }
  6167. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6168. #pragma unused(numItems)
  6169. return [self initWithValues:NULL forKeys:NULL count:0];
  6170. }
  6171. - (void)dealloc {
  6172. [_dictionary release];
  6173. [super dealloc];
  6174. }
  6175. - (instancetype)copyWithZone:(NSZone *)zone {
  6176. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6177. }
  6178. - (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
  6179. if (self == other) {
  6180. return YES;
  6181. }
  6182. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6183. return NO;
  6184. }
  6185. return [_dictionary isEqual:other->_dictionary];
  6186. }
  6187. - (NSUInteger)hash {
  6188. return _dictionary.count;
  6189. }
  6190. - (NSString *)description {
  6191. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6192. }
  6193. - (NSUInteger)count {
  6194. return _dictionary.count;
  6195. }
  6196. - (void)enumerateKeysAndValuesUsingBlock:
  6197. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6198. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6199. NSNumber *aValue,
  6200. BOOL *stop) {
  6201. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6202. }];
  6203. }
  6204. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6205. NSUInteger count = _dictionary.count;
  6206. if (count == 0) {
  6207. return 0;
  6208. }
  6209. GPBType valueType = GPBGetFieldType(field);
  6210. GPBType keyType = field.mapKeyType;
  6211. __block size_t result = 0;
  6212. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6213. NSNumber *aValue,
  6214. BOOL *stop) {
  6215. #pragma unused(stop)
  6216. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6217. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  6218. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6219. }];
  6220. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6221. result += tagSize * count;
  6222. return result;
  6223. }
  6224. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6225. asField:(GPBFieldDescriptor *)field {
  6226. GPBType valueType = GPBGetFieldType(field);
  6227. GPBType keyType = field.mapKeyType;
  6228. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6229. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6230. NSNumber *aValue,
  6231. BOOL *stop) {
  6232. #pragma unused(stop)
  6233. // Write the tag.
  6234. [outputStream writeInt32NoTag:tag];
  6235. // Write the size of the message.
  6236. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6237. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  6238. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6239. // Write the fields.
  6240. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6241. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  6242. }];
  6243. }
  6244. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6245. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6246. }
  6247. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6248. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6249. #pragma unused(stop)
  6250. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6251. }];
  6252. }
  6253. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6254. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6255. if (wrapped && value) {
  6256. *value = [wrapped unsignedIntValue];
  6257. }
  6258. return (wrapped != NULL);
  6259. }
  6260. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6261. if (otherDictionary) {
  6262. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6263. }
  6264. }
  6265. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6266. [_dictionary setObject:@(value) forKey:@(key)];
  6267. }
  6268. - (void)removeValueForKey:(int64_t)aKey {
  6269. [_dictionary removeObjectForKey:@(aKey)];
  6270. }
  6271. - (void)removeAll {
  6272. [_dictionary removeAllObjects];
  6273. }
  6274. @end
  6275. #pragma mark - Int64 -> Int32
  6276. @implementation GPBInt64Int32Dictionary {
  6277. @package
  6278. NSMutableDictionary *_dictionary;
  6279. }
  6280. + (instancetype)dictionary {
  6281. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6282. }
  6283. + (instancetype)dictionaryWithValue:(int32_t)value
  6284. forKey:(int64_t)key {
  6285. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6286. // on to get the type correct.
  6287. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6288. forKeys:&key
  6289. count:1] autorelease];
  6290. }
  6291. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6292. forKeys:(const int64_t [])keys
  6293. count:(NSUInteger)count {
  6294. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6295. // on to get the type correct.
  6296. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6297. forKeys:keys
  6298. count:count] autorelease];
  6299. }
  6300. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6301. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6302. // on to get the type correct.
  6303. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6304. }
  6305. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6306. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6307. }
  6308. - (instancetype)init {
  6309. return [self initWithValues:NULL forKeys:NULL count:0];
  6310. }
  6311. - (instancetype)initWithValues:(const int32_t [])values
  6312. forKeys:(const int64_t [])keys
  6313. count:(NSUInteger)count {
  6314. self = [super init];
  6315. if (self) {
  6316. _dictionary = [[NSMutableDictionary alloc] init];
  6317. if (count && values && keys) {
  6318. for (NSUInteger i = 0; i < count; ++i) {
  6319. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6320. }
  6321. }
  6322. }
  6323. return self;
  6324. }
  6325. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6326. self = [self initWithValues:NULL forKeys:NULL count:0];
  6327. if (self) {
  6328. if (dictionary) {
  6329. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6330. }
  6331. }
  6332. return self;
  6333. }
  6334. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6335. #pragma unused(numItems)
  6336. return [self initWithValues:NULL forKeys:NULL count:0];
  6337. }
  6338. - (void)dealloc {
  6339. [_dictionary release];
  6340. [super dealloc];
  6341. }
  6342. - (instancetype)copyWithZone:(NSZone *)zone {
  6343. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6344. }
  6345. - (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
  6346. if (self == other) {
  6347. return YES;
  6348. }
  6349. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6350. return NO;
  6351. }
  6352. return [_dictionary isEqual:other->_dictionary];
  6353. }
  6354. - (NSUInteger)hash {
  6355. return _dictionary.count;
  6356. }
  6357. - (NSString *)description {
  6358. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6359. }
  6360. - (NSUInteger)count {
  6361. return _dictionary.count;
  6362. }
  6363. - (void)enumerateKeysAndValuesUsingBlock:
  6364. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6365. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6366. NSNumber *aValue,
  6367. BOOL *stop) {
  6368. block([aKey longLongValue], [aValue intValue], stop);
  6369. }];
  6370. }
  6371. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6372. NSUInteger count = _dictionary.count;
  6373. if (count == 0) {
  6374. return 0;
  6375. }
  6376. GPBType valueType = GPBGetFieldType(field);
  6377. GPBType keyType = field.mapKeyType;
  6378. __block size_t result = 0;
  6379. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6380. NSNumber *aValue,
  6381. BOOL *stop) {
  6382. #pragma unused(stop)
  6383. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6384. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  6385. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6386. }];
  6387. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6388. result += tagSize * count;
  6389. return result;
  6390. }
  6391. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6392. asField:(GPBFieldDescriptor *)field {
  6393. GPBType valueType = GPBGetFieldType(field);
  6394. GPBType keyType = field.mapKeyType;
  6395. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6396. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6397. NSNumber *aValue,
  6398. BOOL *stop) {
  6399. #pragma unused(stop)
  6400. // Write the tag.
  6401. [outputStream writeInt32NoTag:tag];
  6402. // Write the size of the message.
  6403. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6404. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  6405. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6406. // Write the fields.
  6407. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6408. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  6409. }];
  6410. }
  6411. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6412. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6413. }
  6414. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6415. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6416. #pragma unused(stop)
  6417. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6418. }];
  6419. }
  6420. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6421. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6422. if (wrapped && value) {
  6423. *value = [wrapped intValue];
  6424. }
  6425. return (wrapped != NULL);
  6426. }
  6427. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6428. if (otherDictionary) {
  6429. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6430. }
  6431. }
  6432. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6433. [_dictionary setObject:@(value) forKey:@(key)];
  6434. }
  6435. - (void)removeValueForKey:(int64_t)aKey {
  6436. [_dictionary removeObjectForKey:@(aKey)];
  6437. }
  6438. - (void)removeAll {
  6439. [_dictionary removeAllObjects];
  6440. }
  6441. @end
  6442. #pragma mark - Int64 -> UInt64
  6443. @implementation GPBInt64UInt64Dictionary {
  6444. @package
  6445. NSMutableDictionary *_dictionary;
  6446. }
  6447. + (instancetype)dictionary {
  6448. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6449. }
  6450. + (instancetype)dictionaryWithValue:(uint64_t)value
  6451. forKey:(int64_t)key {
  6452. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6453. // on to get the type correct.
  6454. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6455. forKeys:&key
  6456. count:1] autorelease];
  6457. }
  6458. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6459. forKeys:(const int64_t [])keys
  6460. count:(NSUInteger)count {
  6461. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6462. // on to get the type correct.
  6463. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6464. forKeys:keys
  6465. count:count] autorelease];
  6466. }
  6467. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6468. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6469. // on to get the type correct.
  6470. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6471. }
  6472. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6473. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6474. }
  6475. - (instancetype)init {
  6476. return [self initWithValues:NULL forKeys:NULL count:0];
  6477. }
  6478. - (instancetype)initWithValues:(const uint64_t [])values
  6479. forKeys:(const int64_t [])keys
  6480. count:(NSUInteger)count {
  6481. self = [super init];
  6482. if (self) {
  6483. _dictionary = [[NSMutableDictionary alloc] init];
  6484. if (count && values && keys) {
  6485. for (NSUInteger i = 0; i < count; ++i) {
  6486. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6487. }
  6488. }
  6489. }
  6490. return self;
  6491. }
  6492. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6493. self = [self initWithValues:NULL forKeys:NULL count:0];
  6494. if (self) {
  6495. if (dictionary) {
  6496. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6497. }
  6498. }
  6499. return self;
  6500. }
  6501. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6502. #pragma unused(numItems)
  6503. return [self initWithValues:NULL forKeys:NULL count:0];
  6504. }
  6505. - (void)dealloc {
  6506. [_dictionary release];
  6507. [super dealloc];
  6508. }
  6509. - (instancetype)copyWithZone:(NSZone *)zone {
  6510. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6511. }
  6512. - (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
  6513. if (self == other) {
  6514. return YES;
  6515. }
  6516. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6517. return NO;
  6518. }
  6519. return [_dictionary isEqual:other->_dictionary];
  6520. }
  6521. - (NSUInteger)hash {
  6522. return _dictionary.count;
  6523. }
  6524. - (NSString *)description {
  6525. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6526. }
  6527. - (NSUInteger)count {
  6528. return _dictionary.count;
  6529. }
  6530. - (void)enumerateKeysAndValuesUsingBlock:
  6531. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  6532. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6533. NSNumber *aValue,
  6534. BOOL *stop) {
  6535. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  6536. }];
  6537. }
  6538. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6539. NSUInteger count = _dictionary.count;
  6540. if (count == 0) {
  6541. return 0;
  6542. }
  6543. GPBType valueType = GPBGetFieldType(field);
  6544. GPBType keyType = field.mapKeyType;
  6545. __block size_t result = 0;
  6546. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6547. NSNumber *aValue,
  6548. BOOL *stop) {
  6549. #pragma unused(stop)
  6550. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6551. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  6552. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6553. }];
  6554. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6555. result += tagSize * count;
  6556. return result;
  6557. }
  6558. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6559. asField:(GPBFieldDescriptor *)field {
  6560. GPBType valueType = GPBGetFieldType(field);
  6561. GPBType keyType = field.mapKeyType;
  6562. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6563. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6564. NSNumber *aValue,
  6565. BOOL *stop) {
  6566. #pragma unused(stop)
  6567. // Write the tag.
  6568. [outputStream writeInt32NoTag:tag];
  6569. // Write the size of the message.
  6570. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6571. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  6572. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6573. // Write the fields.
  6574. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6575. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  6576. }];
  6577. }
  6578. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6579. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6580. }
  6581. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6582. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  6583. #pragma unused(stop)
  6584. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6585. }];
  6586. }
  6587. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  6588. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6589. if (wrapped && value) {
  6590. *value = [wrapped unsignedLongLongValue];
  6591. }
  6592. return (wrapped != NULL);
  6593. }
  6594. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6595. if (otherDictionary) {
  6596. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6597. }
  6598. }
  6599. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  6600. [_dictionary setObject:@(value) forKey:@(key)];
  6601. }
  6602. - (void)removeValueForKey:(int64_t)aKey {
  6603. [_dictionary removeObjectForKey:@(aKey)];
  6604. }
  6605. - (void)removeAll {
  6606. [_dictionary removeAllObjects];
  6607. }
  6608. @end
  6609. #pragma mark - Int64 -> Int64
  6610. @implementation GPBInt64Int64Dictionary {
  6611. @package
  6612. NSMutableDictionary *_dictionary;
  6613. }
  6614. + (instancetype)dictionary {
  6615. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6616. }
  6617. + (instancetype)dictionaryWithValue:(int64_t)value
  6618. forKey:(int64_t)key {
  6619. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6620. // on to get the type correct.
  6621. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  6622. forKeys:&key
  6623. count:1] autorelease];
  6624. }
  6625. + (instancetype)dictionaryWithValues:(const int64_t [])values
  6626. forKeys:(const int64_t [])keys
  6627. count:(NSUInteger)count {
  6628. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6629. // on to get the type correct.
  6630. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  6631. forKeys:keys
  6632. count:count] autorelease];
  6633. }
  6634. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6635. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6636. // on to get the type correct.
  6637. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6638. }
  6639. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6640. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6641. }
  6642. - (instancetype)init {
  6643. return [self initWithValues:NULL forKeys:NULL count:0];
  6644. }
  6645. - (instancetype)initWithValues:(const int64_t [])values
  6646. forKeys:(const int64_t [])keys
  6647. count:(NSUInteger)count {
  6648. self = [super init];
  6649. if (self) {
  6650. _dictionary = [[NSMutableDictionary alloc] init];
  6651. if (count && values && keys) {
  6652. for (NSUInteger i = 0; i < count; ++i) {
  6653. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6654. }
  6655. }
  6656. }
  6657. return self;
  6658. }
  6659. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6660. self = [self initWithValues:NULL forKeys:NULL count:0];
  6661. if (self) {
  6662. if (dictionary) {
  6663. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6664. }
  6665. }
  6666. return self;
  6667. }
  6668. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6669. #pragma unused(numItems)
  6670. return [self initWithValues:NULL forKeys:NULL count:0];
  6671. }
  6672. - (void)dealloc {
  6673. [_dictionary release];
  6674. [super dealloc];
  6675. }
  6676. - (instancetype)copyWithZone:(NSZone *)zone {
  6677. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  6678. }
  6679. - (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
  6680. if (self == other) {
  6681. return YES;
  6682. }
  6683. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  6684. return NO;
  6685. }
  6686. return [_dictionary isEqual:other->_dictionary];
  6687. }
  6688. - (NSUInteger)hash {
  6689. return _dictionary.count;
  6690. }
  6691. - (NSString *)description {
  6692. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6693. }
  6694. - (NSUInteger)count {
  6695. return _dictionary.count;
  6696. }
  6697. - (void)enumerateKeysAndValuesUsingBlock:
  6698. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  6699. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6700. NSNumber *aValue,
  6701. BOOL *stop) {
  6702. block([aKey longLongValue], [aValue longLongValue], stop);
  6703. }];
  6704. }
  6705. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6706. NSUInteger count = _dictionary.count;
  6707. if (count == 0) {
  6708. return 0;
  6709. }
  6710. GPBType valueType = GPBGetFieldType(field);
  6711. GPBType keyType = field.mapKeyType;
  6712. __block size_t result = 0;
  6713. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6714. NSNumber *aValue,
  6715. BOOL *stop) {
  6716. #pragma unused(stop)
  6717. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6718. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  6719. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6720. }];
  6721. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6722. result += tagSize * count;
  6723. return result;
  6724. }
  6725. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6726. asField:(GPBFieldDescriptor *)field {
  6727. GPBType valueType = GPBGetFieldType(field);
  6728. GPBType keyType = field.mapKeyType;
  6729. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6730. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6731. NSNumber *aValue,
  6732. BOOL *stop) {
  6733. #pragma unused(stop)
  6734. // Write the tag.
  6735. [outputStream writeInt32NoTag:tag];
  6736. // Write the size of the message.
  6737. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6738. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  6739. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6740. // Write the fields.
  6741. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6742. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  6743. }];
  6744. }
  6745. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6746. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  6747. }
  6748. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6749. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  6750. #pragma unused(stop)
  6751. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  6752. }];
  6753. }
  6754. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  6755. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6756. if (wrapped && value) {
  6757. *value = [wrapped longLongValue];
  6758. }
  6759. return (wrapped != NULL);
  6760. }
  6761. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  6762. if (otherDictionary) {
  6763. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6764. }
  6765. }
  6766. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  6767. [_dictionary setObject:@(value) forKey:@(key)];
  6768. }
  6769. - (void)removeValueForKey:(int64_t)aKey {
  6770. [_dictionary removeObjectForKey:@(aKey)];
  6771. }
  6772. - (void)removeAll {
  6773. [_dictionary removeAllObjects];
  6774. }
  6775. @end
  6776. #pragma mark - Int64 -> Bool
  6777. @implementation GPBInt64BoolDictionary {
  6778. @package
  6779. NSMutableDictionary *_dictionary;
  6780. }
  6781. + (instancetype)dictionary {
  6782. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6783. }
  6784. + (instancetype)dictionaryWithValue:(BOOL)value
  6785. forKey:(int64_t)key {
  6786. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6787. // on to get the type correct.
  6788. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  6789. forKeys:&key
  6790. count:1] autorelease];
  6791. }
  6792. + (instancetype)dictionaryWithValues:(const BOOL [])values
  6793. forKeys:(const int64_t [])keys
  6794. count:(NSUInteger)count {
  6795. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6796. // on to get the type correct.
  6797. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  6798. forKeys:keys
  6799. count:count] autorelease];
  6800. }
  6801. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  6802. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6803. // on to get the type correct.
  6804. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6805. }
  6806. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6807. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6808. }
  6809. - (instancetype)init {
  6810. return [self initWithValues:NULL forKeys:NULL count:0];
  6811. }
  6812. - (instancetype)initWithValues:(const BOOL [])values
  6813. forKeys:(const int64_t [])keys
  6814. count:(NSUInteger)count {
  6815. self = [super init];
  6816. if (self) {
  6817. _dictionary = [[NSMutableDictionary alloc] init];
  6818. if (count && values && keys) {
  6819. for (NSUInteger i = 0; i < count; ++i) {
  6820. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6821. }
  6822. }
  6823. }
  6824. return self;
  6825. }
  6826. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  6827. self = [self initWithValues:NULL forKeys:NULL count:0];
  6828. if (self) {
  6829. if (dictionary) {
  6830. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6831. }
  6832. }
  6833. return self;
  6834. }
  6835. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6836. #pragma unused(numItems)
  6837. return [self initWithValues:NULL forKeys:NULL count:0];
  6838. }
  6839. - (void)dealloc {
  6840. [_dictionary release];
  6841. [super dealloc];
  6842. }
  6843. - (instancetype)copyWithZone:(NSZone *)zone {
  6844. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  6845. }
  6846. - (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
  6847. if (self == other) {
  6848. return YES;
  6849. }
  6850. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  6851. return NO;
  6852. }
  6853. return [_dictionary isEqual:other->_dictionary];
  6854. }
  6855. - (NSUInteger)hash {
  6856. return _dictionary.count;
  6857. }
  6858. - (NSString *)description {
  6859. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6860. }
  6861. - (NSUInteger)count {
  6862. return _dictionary.count;
  6863. }
  6864. - (void)enumerateKeysAndValuesUsingBlock:
  6865. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  6866. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6867. NSNumber *aValue,
  6868. BOOL *stop) {
  6869. block([aKey longLongValue], [aValue boolValue], stop);
  6870. }];
  6871. }
  6872. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6873. NSUInteger count = _dictionary.count;
  6874. if (count == 0) {
  6875. return 0;
  6876. }
  6877. GPBType valueType = GPBGetFieldType(field);
  6878. GPBType keyType = field.mapKeyType;
  6879. __block size_t result = 0;
  6880. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6881. NSNumber *aValue,
  6882. BOOL *stop) {
  6883. #pragma unused(stop)
  6884. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6885. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  6886. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6887. }];
  6888. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6889. result += tagSize * count;
  6890. return result;
  6891. }
  6892. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6893. asField:(GPBFieldDescriptor *)field {
  6894. GPBType valueType = GPBGetFieldType(field);
  6895. GPBType keyType = field.mapKeyType;
  6896. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6897. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6898. NSNumber *aValue,
  6899. BOOL *stop) {
  6900. #pragma unused(stop)
  6901. // Write the tag.
  6902. [outputStream writeInt32NoTag:tag];
  6903. // Write the size of the message.
  6904. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6905. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  6906. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6907. // Write the fields.
  6908. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6909. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  6910. }];
  6911. }
  6912. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6913. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  6914. }
  6915. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6916. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  6917. #pragma unused(stop)
  6918. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  6919. }];
  6920. }
  6921. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  6922. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6923. if (wrapped && value) {
  6924. *value = [wrapped boolValue];
  6925. }
  6926. return (wrapped != NULL);
  6927. }
  6928. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  6929. if (otherDictionary) {
  6930. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6931. }
  6932. }
  6933. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  6934. [_dictionary setObject:@(value) forKey:@(key)];
  6935. }
  6936. - (void)removeValueForKey:(int64_t)aKey {
  6937. [_dictionary removeObjectForKey:@(aKey)];
  6938. }
  6939. - (void)removeAll {
  6940. [_dictionary removeAllObjects];
  6941. }
  6942. @end
  6943. #pragma mark - Int64 -> Float
  6944. @implementation GPBInt64FloatDictionary {
  6945. @package
  6946. NSMutableDictionary *_dictionary;
  6947. }
  6948. + (instancetype)dictionary {
  6949. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6950. }
  6951. + (instancetype)dictionaryWithValue:(float)value
  6952. forKey:(int64_t)key {
  6953. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6954. // on to get the type correct.
  6955. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  6956. forKeys:&key
  6957. count:1] autorelease];
  6958. }
  6959. + (instancetype)dictionaryWithValues:(const float [])values
  6960. forKeys:(const int64_t [])keys
  6961. count:(NSUInteger)count {
  6962. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6963. // on to get the type correct.
  6964. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  6965. forKeys:keys
  6966. count:count] autorelease];
  6967. }
  6968. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  6969. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6970. // on to get the type correct.
  6971. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6972. }
  6973. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6974. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6975. }
  6976. - (instancetype)init {
  6977. return [self initWithValues:NULL forKeys:NULL count:0];
  6978. }
  6979. - (instancetype)initWithValues:(const float [])values
  6980. forKeys:(const int64_t [])keys
  6981. count:(NSUInteger)count {
  6982. self = [super init];
  6983. if (self) {
  6984. _dictionary = [[NSMutableDictionary alloc] init];
  6985. if (count && values && keys) {
  6986. for (NSUInteger i = 0; i < count; ++i) {
  6987. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6988. }
  6989. }
  6990. }
  6991. return self;
  6992. }
  6993. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  6994. self = [self initWithValues:NULL forKeys:NULL count:0];
  6995. if (self) {
  6996. if (dictionary) {
  6997. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6998. }
  6999. }
  7000. return self;
  7001. }
  7002. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7003. #pragma unused(numItems)
  7004. return [self initWithValues:NULL forKeys:NULL count:0];
  7005. }
  7006. - (void)dealloc {
  7007. [_dictionary release];
  7008. [super dealloc];
  7009. }
  7010. - (instancetype)copyWithZone:(NSZone *)zone {
  7011. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7012. }
  7013. - (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
  7014. if (self == other) {
  7015. return YES;
  7016. }
  7017. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7018. return NO;
  7019. }
  7020. return [_dictionary isEqual:other->_dictionary];
  7021. }
  7022. - (NSUInteger)hash {
  7023. return _dictionary.count;
  7024. }
  7025. - (NSString *)description {
  7026. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7027. }
  7028. - (NSUInteger)count {
  7029. return _dictionary.count;
  7030. }
  7031. - (void)enumerateKeysAndValuesUsingBlock:
  7032. (void (^)(int64_t key, float value, BOOL *stop))block {
  7033. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7034. NSNumber *aValue,
  7035. BOOL *stop) {
  7036. block([aKey longLongValue], [aValue floatValue], stop);
  7037. }];
  7038. }
  7039. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7040. NSUInteger count = _dictionary.count;
  7041. if (count == 0) {
  7042. return 0;
  7043. }
  7044. GPBType valueType = GPBGetFieldType(field);
  7045. GPBType keyType = field.mapKeyType;
  7046. __block size_t result = 0;
  7047. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7048. NSNumber *aValue,
  7049. BOOL *stop) {
  7050. #pragma unused(stop)
  7051. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7052. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  7053. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7054. }];
  7055. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7056. result += tagSize * count;
  7057. return result;
  7058. }
  7059. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7060. asField:(GPBFieldDescriptor *)field {
  7061. GPBType valueType = GPBGetFieldType(field);
  7062. GPBType keyType = field.mapKeyType;
  7063. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7064. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7065. NSNumber *aValue,
  7066. BOOL *stop) {
  7067. #pragma unused(stop)
  7068. // Write the tag.
  7069. [outputStream writeInt32NoTag:tag];
  7070. // Write the size of the message.
  7071. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7072. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  7073. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7074. // Write the fields.
  7075. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7076. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  7077. }];
  7078. }
  7079. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7080. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7081. }
  7082. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7083. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7084. #pragma unused(stop)
  7085. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7086. }];
  7087. }
  7088. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7089. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7090. if (wrapped && value) {
  7091. *value = [wrapped floatValue];
  7092. }
  7093. return (wrapped != NULL);
  7094. }
  7095. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7096. if (otherDictionary) {
  7097. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7098. }
  7099. }
  7100. - (void)setValue:(float)value forKey:(int64_t)key {
  7101. [_dictionary setObject:@(value) forKey:@(key)];
  7102. }
  7103. - (void)removeValueForKey:(int64_t)aKey {
  7104. [_dictionary removeObjectForKey:@(aKey)];
  7105. }
  7106. - (void)removeAll {
  7107. [_dictionary removeAllObjects];
  7108. }
  7109. @end
  7110. #pragma mark - Int64 -> Double
  7111. @implementation GPBInt64DoubleDictionary {
  7112. @package
  7113. NSMutableDictionary *_dictionary;
  7114. }
  7115. + (instancetype)dictionary {
  7116. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7117. }
  7118. + (instancetype)dictionaryWithValue:(double)value
  7119. forKey:(int64_t)key {
  7120. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7121. // on to get the type correct.
  7122. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7123. forKeys:&key
  7124. count:1] autorelease];
  7125. }
  7126. + (instancetype)dictionaryWithValues:(const double [])values
  7127. forKeys:(const int64_t [])keys
  7128. count:(NSUInteger)count {
  7129. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7130. // on to get the type correct.
  7131. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7132. forKeys:keys
  7133. count:count] autorelease];
  7134. }
  7135. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7136. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7137. // on to get the type correct.
  7138. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7139. }
  7140. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7141. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7142. }
  7143. - (instancetype)init {
  7144. return [self initWithValues:NULL forKeys:NULL count:0];
  7145. }
  7146. - (instancetype)initWithValues:(const double [])values
  7147. forKeys:(const int64_t [])keys
  7148. count:(NSUInteger)count {
  7149. self = [super init];
  7150. if (self) {
  7151. _dictionary = [[NSMutableDictionary alloc] init];
  7152. if (count && values && keys) {
  7153. for (NSUInteger i = 0; i < count; ++i) {
  7154. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7155. }
  7156. }
  7157. }
  7158. return self;
  7159. }
  7160. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7161. self = [self initWithValues:NULL forKeys:NULL count:0];
  7162. if (self) {
  7163. if (dictionary) {
  7164. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7165. }
  7166. }
  7167. return self;
  7168. }
  7169. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7170. #pragma unused(numItems)
  7171. return [self initWithValues:NULL forKeys:NULL count:0];
  7172. }
  7173. - (void)dealloc {
  7174. [_dictionary release];
  7175. [super dealloc];
  7176. }
  7177. - (instancetype)copyWithZone:(NSZone *)zone {
  7178. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7179. }
  7180. - (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
  7181. if (self == other) {
  7182. return YES;
  7183. }
  7184. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7185. return NO;
  7186. }
  7187. return [_dictionary isEqual:other->_dictionary];
  7188. }
  7189. - (NSUInteger)hash {
  7190. return _dictionary.count;
  7191. }
  7192. - (NSString *)description {
  7193. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7194. }
  7195. - (NSUInteger)count {
  7196. return _dictionary.count;
  7197. }
  7198. - (void)enumerateKeysAndValuesUsingBlock:
  7199. (void (^)(int64_t key, double value, BOOL *stop))block {
  7200. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7201. NSNumber *aValue,
  7202. BOOL *stop) {
  7203. block([aKey longLongValue], [aValue doubleValue], stop);
  7204. }];
  7205. }
  7206. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7207. NSUInteger count = _dictionary.count;
  7208. if (count == 0) {
  7209. return 0;
  7210. }
  7211. GPBType valueType = GPBGetFieldType(field);
  7212. GPBType keyType = field.mapKeyType;
  7213. __block size_t result = 0;
  7214. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7215. NSNumber *aValue,
  7216. BOOL *stop) {
  7217. #pragma unused(stop)
  7218. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7219. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  7220. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7221. }];
  7222. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7223. result += tagSize * count;
  7224. return result;
  7225. }
  7226. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7227. asField:(GPBFieldDescriptor *)field {
  7228. GPBType valueType = GPBGetFieldType(field);
  7229. GPBType keyType = field.mapKeyType;
  7230. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7231. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7232. NSNumber *aValue,
  7233. BOOL *stop) {
  7234. #pragma unused(stop)
  7235. // Write the tag.
  7236. [outputStream writeInt32NoTag:tag];
  7237. // Write the size of the message.
  7238. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7239. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  7240. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7241. // Write the fields.
  7242. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7243. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  7244. }];
  7245. }
  7246. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7247. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7248. }
  7249. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7250. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7251. #pragma unused(stop)
  7252. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7253. }];
  7254. }
  7255. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7256. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7257. if (wrapped && value) {
  7258. *value = [wrapped doubleValue];
  7259. }
  7260. return (wrapped != NULL);
  7261. }
  7262. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7263. if (otherDictionary) {
  7264. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7265. }
  7266. }
  7267. - (void)setValue:(double)value forKey:(int64_t)key {
  7268. [_dictionary setObject:@(value) forKey:@(key)];
  7269. }
  7270. - (void)removeValueForKey:(int64_t)aKey {
  7271. [_dictionary removeObjectForKey:@(aKey)];
  7272. }
  7273. - (void)removeAll {
  7274. [_dictionary removeAllObjects];
  7275. }
  7276. @end
  7277. #pragma mark - Int64 -> Enum
  7278. @implementation GPBInt64EnumDictionary {
  7279. @package
  7280. NSMutableDictionary *_dictionary;
  7281. GPBEnumValidationFunc _validationFunc;
  7282. }
  7283. @synthesize validationFunc = _validationFunc;
  7284. + (instancetype)dictionary {
  7285. return [[[self alloc] initWithValidationFunction:NULL
  7286. rawValues:NULL
  7287. forKeys:NULL
  7288. count:0] autorelease];
  7289. }
  7290. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7291. return [[[self alloc] initWithValidationFunction:func
  7292. rawValues:NULL
  7293. forKeys:NULL
  7294. count:0] autorelease];
  7295. }
  7296. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7297. rawValue:(int32_t)rawValue
  7298. forKey:(int64_t)key {
  7299. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7300. // on to get the type correct.
  7301. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7302. rawValues:&rawValue
  7303. forKeys:&key
  7304. count:1] autorelease];
  7305. }
  7306. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7307. rawValues:(const int32_t [])rawValues
  7308. forKeys:(const int64_t [])keys
  7309. count:(NSUInteger)count {
  7310. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7311. // on to get the type correct.
  7312. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7313. rawValues:rawValues
  7314. forKeys:keys
  7315. count:count] autorelease];
  7316. }
  7317. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7318. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7319. // on to get the type correct.
  7320. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7321. }
  7322. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7323. capacity:(NSUInteger)numItems {
  7324. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7325. }
  7326. - (instancetype)init {
  7327. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7328. }
  7329. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7330. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7331. }
  7332. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7333. rawValues:(const int32_t [])rawValues
  7334. forKeys:(const int64_t [])keys
  7335. count:(NSUInteger)count {
  7336. self = [super init];
  7337. if (self) {
  7338. _dictionary = [[NSMutableDictionary alloc] init];
  7339. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7340. if (count && rawValues && keys) {
  7341. for (NSUInteger i = 0; i < count; ++i) {
  7342. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7343. }
  7344. }
  7345. }
  7346. return self;
  7347. }
  7348. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7349. self = [self initWithValidationFunction:dictionary.validationFunc
  7350. rawValues:NULL
  7351. forKeys:NULL
  7352. count:0];
  7353. if (self) {
  7354. if (dictionary) {
  7355. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7356. }
  7357. }
  7358. return self;
  7359. }
  7360. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7361. capacity:(NSUInteger)numItems {
  7362. #pragma unused(numItems)
  7363. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7364. }
  7365. - (void)dealloc {
  7366. [_dictionary release];
  7367. [super dealloc];
  7368. }
  7369. - (instancetype)copyWithZone:(NSZone *)zone {
  7370. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7371. }
  7372. - (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
  7373. if (self == other) {
  7374. return YES;
  7375. }
  7376. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7377. return NO;
  7378. }
  7379. return [_dictionary isEqual:other->_dictionary];
  7380. }
  7381. - (NSUInteger)hash {
  7382. return _dictionary.count;
  7383. }
  7384. - (NSString *)description {
  7385. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7386. }
  7387. - (NSUInteger)count {
  7388. return _dictionary.count;
  7389. }
  7390. - (void)enumerateKeysAndRawValuesUsingBlock:
  7391. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7392. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7393. NSNumber *aValue,
  7394. BOOL *stop) {
  7395. block([aKey longLongValue], [aValue intValue], stop);
  7396. }];
  7397. }
  7398. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7399. NSUInteger count = _dictionary.count;
  7400. if (count == 0) {
  7401. return 0;
  7402. }
  7403. GPBType valueType = GPBGetFieldType(field);
  7404. GPBType keyType = field.mapKeyType;
  7405. __block size_t result = 0;
  7406. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7407. NSNumber *aValue,
  7408. BOOL *stop) {
  7409. #pragma unused(stop)
  7410. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7411. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  7412. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7413. }];
  7414. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7415. result += tagSize * count;
  7416. return result;
  7417. }
  7418. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7419. asField:(GPBFieldDescriptor *)field {
  7420. GPBType valueType = GPBGetFieldType(field);
  7421. GPBType keyType = field.mapKeyType;
  7422. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7423. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7424. NSNumber *aValue,
  7425. BOOL *stop) {
  7426. #pragma unused(stop)
  7427. // Write the tag.
  7428. [outputStream writeInt32NoTag:tag];
  7429. // Write the size of the message.
  7430. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7431. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  7432. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7433. // Write the fields.
  7434. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7435. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  7436. }];
  7437. }
  7438. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7439. forKey:(GPBValue *)key
  7440. keyType:(GPBType)keyType {
  7441. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyType);
  7442. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  7443. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7444. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7445. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyType);
  7446. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  7447. [outputStream release];
  7448. return data;
  7449. }
  7450. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7451. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7452. }
  7453. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7454. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7455. #pragma unused(stop)
  7456. block([NSString stringWithFormat:@"%lld", key], @(value));
  7457. }];
  7458. }
  7459. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7460. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7461. if (wrapped && value) {
  7462. int32_t result = [wrapped intValue];
  7463. if (!_validationFunc(result)) {
  7464. result = kGPBUnrecognizedEnumeratorValue;
  7465. }
  7466. *value = result;
  7467. }
  7468. return (wrapped != NULL);
  7469. }
  7470. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  7471. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7472. if (wrapped && rawValue) {
  7473. *rawValue = [wrapped intValue];
  7474. }
  7475. return (wrapped != NULL);
  7476. }
  7477. - (void)enumerateKeysAndValuesUsingBlock:
  7478. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7479. GPBEnumValidationFunc func = _validationFunc;
  7480. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7481. NSNumber *aValue,
  7482. BOOL *stop) {
  7483. int32_t unwrapped = [aValue intValue];
  7484. if (!func(unwrapped)) {
  7485. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7486. }
  7487. block([aKey longLongValue], unwrapped, stop);
  7488. }];
  7489. }
  7490. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7491. if (otherDictionary) {
  7492. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7493. }
  7494. }
  7495. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7496. [_dictionary setObject:@(value) forKey:@(key)];
  7497. }
  7498. - (void)removeValueForKey:(int64_t)aKey {
  7499. [_dictionary removeObjectForKey:@(aKey)];
  7500. }
  7501. - (void)removeAll {
  7502. [_dictionary removeAllObjects];
  7503. }
  7504. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  7505. if (!_validationFunc(value)) {
  7506. [NSException raise:NSInvalidArgumentException
  7507. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7508. value];
  7509. }
  7510. [_dictionary setObject:@(value) forKey:@(key)];
  7511. }
  7512. @end
  7513. #pragma mark - Int64 -> Object
  7514. @implementation GPBInt64ObjectDictionary {
  7515. @package
  7516. NSMutableDictionary *_dictionary;
  7517. }
  7518. + (instancetype)dictionary {
  7519. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7520. }
  7521. + (instancetype)dictionaryWithValue:(id)value
  7522. forKey:(int64_t)key {
  7523. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7524. // on to get the type correct.
  7525. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithValues:&value
  7526. forKeys:&key
  7527. count:1] autorelease];
  7528. }
  7529. + (instancetype)dictionaryWithValues:(const id [])values
  7530. forKeys:(const int64_t [])keys
  7531. count:(NSUInteger)count {
  7532. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7533. // on to get the type correct.
  7534. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithValues:values
  7535. forKeys:keys
  7536. count:count] autorelease];
  7537. }
  7538. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7539. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7540. // on to get the type correct.
  7541. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7542. }
  7543. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7544. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7545. }
  7546. - (instancetype)init {
  7547. return [self initWithValues:NULL forKeys:NULL count:0];
  7548. }
  7549. - (instancetype)initWithValues:(const id [])values
  7550. forKeys:(const int64_t [])keys
  7551. count:(NSUInteger)count {
  7552. self = [super init];
  7553. if (self) {
  7554. _dictionary = [[NSMutableDictionary alloc] init];
  7555. if (count && values && keys) {
  7556. for (NSUInteger i = 0; i < count; ++i) {
  7557. [_dictionary setObject:values[i] forKey:@(keys[i])];
  7558. }
  7559. }
  7560. }
  7561. return self;
  7562. }
  7563. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7564. self = [self initWithValues:NULL forKeys:NULL count:0];
  7565. if (self) {
  7566. if (dictionary) {
  7567. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7568. }
  7569. }
  7570. return self;
  7571. }
  7572. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7573. #pragma unused(numItems)
  7574. return [self initWithValues:NULL forKeys:NULL count:0];
  7575. }
  7576. - (void)dealloc {
  7577. [_dictionary release];
  7578. [super dealloc];
  7579. }
  7580. - (instancetype)copyWithZone:(NSZone *)zone {
  7581. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  7582. }
  7583. - (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
  7584. if (self == other) {
  7585. return YES;
  7586. }
  7587. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  7588. return NO;
  7589. }
  7590. return [_dictionary isEqual:other->_dictionary];
  7591. }
  7592. - (NSUInteger)hash {
  7593. return _dictionary.count;
  7594. }
  7595. - (NSString *)description {
  7596. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7597. }
  7598. - (NSUInteger)count {
  7599. return _dictionary.count;
  7600. }
  7601. - (void)enumerateKeysAndValuesUsingBlock:
  7602. (void (^)(int64_t key, id value, BOOL *stop))block {
  7603. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7604. id aValue,
  7605. BOOL *stop) {
  7606. block([aKey longLongValue], aValue, stop);
  7607. }];
  7608. }
  7609. - (BOOL)isInitialized {
  7610. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  7611. if (!msg.initialized) {
  7612. return NO;
  7613. }
  7614. }
  7615. return YES;
  7616. }
  7617. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  7618. GPBInt64ObjectDictionary *newDict =
  7619. [[GPBInt64ObjectDictionary alloc] init];
  7620. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  7621. GPBMessage *msg,
  7622. BOOL *stop) {
  7623. #pragma unused(stop)
  7624. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  7625. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  7626. [copiedMsg release];
  7627. }];
  7628. return newDict;
  7629. }
  7630. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7631. NSUInteger count = _dictionary.count;
  7632. if (count == 0) {
  7633. return 0;
  7634. }
  7635. GPBType valueType = GPBGetFieldType(field);
  7636. GPBType keyType = field.mapKeyType;
  7637. __block size_t result = 0;
  7638. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7639. id aValue,
  7640. BOOL *stop) {
  7641. #pragma unused(stop)
  7642. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7643. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  7644. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7645. }];
  7646. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7647. result += tagSize * count;
  7648. return result;
  7649. }
  7650. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7651. asField:(GPBFieldDescriptor *)field {
  7652. GPBType valueType = GPBGetFieldType(field);
  7653. GPBType keyType = field.mapKeyType;
  7654. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7655. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7656. id aValue,
  7657. BOOL *stop) {
  7658. #pragma unused(stop)
  7659. // Write the tag.
  7660. [outputStream writeInt32NoTag:tag];
  7661. // Write the size of the message.
  7662. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7663. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  7664. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7665. // Write the fields.
  7666. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7667. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  7668. }];
  7669. }
  7670. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7671. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  7672. }
  7673. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7674. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, id value, BOOL *stop) {
  7675. #pragma unused(stop)
  7676. block([NSString stringWithFormat:@"%lld", key], value);
  7677. }];
  7678. }
  7679. - (id)valueForKey:(int64_t)key {
  7680. id result = [_dictionary objectForKey:@(key)];
  7681. return result;
  7682. }
  7683. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  7684. if (otherDictionary) {
  7685. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7686. }
  7687. }
  7688. - (void)setValue:(id)value forKey:(int64_t)key {
  7689. [_dictionary setObject:value forKey:@(key)];
  7690. }
  7691. - (void)removeValueForKey:(int64_t)aKey {
  7692. [_dictionary removeObjectForKey:@(aKey)];
  7693. }
  7694. - (void)removeAll {
  7695. [_dictionary removeAllObjects];
  7696. }
  7697. @end
  7698. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  7699. // This block of code is generated, do not edit it directly.
  7700. #pragma mark - String -> UInt32
  7701. @implementation GPBStringUInt32Dictionary {
  7702. @package
  7703. NSMutableDictionary *_dictionary;
  7704. }
  7705. + (instancetype)dictionary {
  7706. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7707. }
  7708. + (instancetype)dictionaryWithValue:(uint32_t)value
  7709. forKey:(NSString *)key {
  7710. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7711. // on to get the type correct.
  7712. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  7713. forKeys:&key
  7714. count:1] autorelease];
  7715. }
  7716. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  7717. forKeys:(const NSString * [])keys
  7718. count:(NSUInteger)count {
  7719. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7720. // on to get the type correct.
  7721. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  7722. forKeys:keys
  7723. count:count] autorelease];
  7724. }
  7725. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  7726. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7727. // on to get the type correct.
  7728. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7729. }
  7730. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7731. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7732. }
  7733. - (instancetype)init {
  7734. return [self initWithValues:NULL forKeys:NULL count:0];
  7735. }
  7736. - (instancetype)initWithValues:(const uint32_t [])values
  7737. forKeys:(const NSString * [])keys
  7738. count:(NSUInteger)count {
  7739. self = [super init];
  7740. if (self) {
  7741. _dictionary = [[NSMutableDictionary alloc] init];
  7742. if (count && values && keys) {
  7743. for (NSUInteger i = 0; i < count; ++i) {
  7744. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7745. }
  7746. }
  7747. }
  7748. return self;
  7749. }
  7750. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  7751. self = [self initWithValues:NULL forKeys:NULL count:0];
  7752. if (self) {
  7753. if (dictionary) {
  7754. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7755. }
  7756. }
  7757. return self;
  7758. }
  7759. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7760. #pragma unused(numItems)
  7761. return [self initWithValues:NULL forKeys:NULL count:0];
  7762. }
  7763. - (void)dealloc {
  7764. [_dictionary release];
  7765. [super dealloc];
  7766. }
  7767. - (instancetype)copyWithZone:(NSZone *)zone {
  7768. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7769. }
  7770. - (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
  7771. if (self == other) {
  7772. return YES;
  7773. }
  7774. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  7775. return NO;
  7776. }
  7777. return [_dictionary isEqual:other->_dictionary];
  7778. }
  7779. - (NSUInteger)hash {
  7780. return _dictionary.count;
  7781. }
  7782. - (NSString *)description {
  7783. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7784. }
  7785. - (NSUInteger)count {
  7786. return _dictionary.count;
  7787. }
  7788. - (void)enumerateKeysAndValuesUsingBlock:
  7789. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  7790. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  7791. NSNumber *aValue,
  7792. BOOL *stop) {
  7793. block(aKey, [aValue unsignedIntValue], stop);
  7794. }];
  7795. }
  7796. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7797. NSUInteger count = _dictionary.count;
  7798. if (count == 0) {
  7799. return 0;
  7800. }
  7801. GPBType valueType = GPBGetFieldType(field);
  7802. GPBType keyType = field.mapKeyType;
  7803. __block size_t result = 0;
  7804. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  7805. NSNumber *aValue,
  7806. BOOL *stop) {
  7807. #pragma unused(stop)
  7808. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  7809. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  7810. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7811. }];
  7812. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7813. result += tagSize * count;
  7814. return result;
  7815. }
  7816. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7817. asField:(GPBFieldDescriptor *)field {
  7818. GPBType valueType = GPBGetFieldType(field);
  7819. GPBType keyType = field.mapKeyType;
  7820. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7821. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  7822. NSNumber *aValue,
  7823. BOOL *stop) {
  7824. #pragma unused(stop)
  7825. // Write the tag.
  7826. [outputStream writeInt32NoTag:tag];
  7827. // Write the size of the message.
  7828. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  7829. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  7830. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7831. // Write the fields.
  7832. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  7833. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  7834. }];
  7835. }
  7836. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7837. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  7838. }
  7839. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7840. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  7841. #pragma unused(stop)
  7842. block(key, [NSString stringWithFormat:@"%u", value]);
  7843. }];
  7844. }
  7845. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  7846. NSNumber *wrapped = [_dictionary objectForKey:key];
  7847. if (wrapped && value) {
  7848. *value = [wrapped unsignedIntValue];
  7849. }
  7850. return (wrapped != NULL);
  7851. }
  7852. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  7853. if (otherDictionary) {
  7854. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7855. }
  7856. }
  7857. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  7858. [_dictionary setObject:@(value) forKey:key];
  7859. }
  7860. - (void)removeValueForKey:(NSString *)aKey {
  7861. [_dictionary removeObjectForKey:aKey];
  7862. }
  7863. - (void)removeAll {
  7864. [_dictionary removeAllObjects];
  7865. }
  7866. @end
  7867. #pragma mark - String -> Int32
  7868. @implementation GPBStringInt32Dictionary {
  7869. @package
  7870. NSMutableDictionary *_dictionary;
  7871. }
  7872. + (instancetype)dictionary {
  7873. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7874. }
  7875. + (instancetype)dictionaryWithValue:(int32_t)value
  7876. forKey:(NSString *)key {
  7877. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7878. // on to get the type correct.
  7879. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  7880. forKeys:&key
  7881. count:1] autorelease];
  7882. }
  7883. + (instancetype)dictionaryWithValues:(const int32_t [])values
  7884. forKeys:(const NSString * [])keys
  7885. count:(NSUInteger)count {
  7886. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7887. // on to get the type correct.
  7888. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  7889. forKeys:keys
  7890. count:count] autorelease];
  7891. }
  7892. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  7893. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7894. // on to get the type correct.
  7895. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7896. }
  7897. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7898. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7899. }
  7900. - (instancetype)init {
  7901. return [self initWithValues:NULL forKeys:NULL count:0];
  7902. }
  7903. - (instancetype)initWithValues:(const int32_t [])values
  7904. forKeys:(const NSString * [])keys
  7905. count:(NSUInteger)count {
  7906. self = [super init];
  7907. if (self) {
  7908. _dictionary = [[NSMutableDictionary alloc] init];
  7909. if (count && values && keys) {
  7910. for (NSUInteger i = 0; i < count; ++i) {
  7911. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7912. }
  7913. }
  7914. }
  7915. return self;
  7916. }
  7917. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  7918. self = [self initWithValues:NULL forKeys:NULL count:0];
  7919. if (self) {
  7920. if (dictionary) {
  7921. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7922. }
  7923. }
  7924. return self;
  7925. }
  7926. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7927. #pragma unused(numItems)
  7928. return [self initWithValues:NULL forKeys:NULL count:0];
  7929. }
  7930. - (void)dealloc {
  7931. [_dictionary release];
  7932. [super dealloc];
  7933. }
  7934. - (instancetype)copyWithZone:(NSZone *)zone {
  7935. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7936. }
  7937. - (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
  7938. if (self == other) {
  7939. return YES;
  7940. }
  7941. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  7942. return NO;
  7943. }
  7944. return [_dictionary isEqual:other->_dictionary];
  7945. }
  7946. - (NSUInteger)hash {
  7947. return _dictionary.count;
  7948. }
  7949. - (NSString *)description {
  7950. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7951. }
  7952. - (NSUInteger)count {
  7953. return _dictionary.count;
  7954. }
  7955. - (void)enumerateKeysAndValuesUsingBlock:
  7956. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  7957. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  7958. NSNumber *aValue,
  7959. BOOL *stop) {
  7960. block(aKey, [aValue intValue], stop);
  7961. }];
  7962. }
  7963. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7964. NSUInteger count = _dictionary.count;
  7965. if (count == 0) {
  7966. return 0;
  7967. }
  7968. GPBType valueType = GPBGetFieldType(field);
  7969. GPBType keyType = field.mapKeyType;
  7970. __block size_t result = 0;
  7971. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  7972. NSNumber *aValue,
  7973. BOOL *stop) {
  7974. #pragma unused(stop)
  7975. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  7976. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  7977. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7978. }];
  7979. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7980. result += tagSize * count;
  7981. return result;
  7982. }
  7983. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7984. asField:(GPBFieldDescriptor *)field {
  7985. GPBType valueType = GPBGetFieldType(field);
  7986. GPBType keyType = field.mapKeyType;
  7987. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7988. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  7989. NSNumber *aValue,
  7990. BOOL *stop) {
  7991. #pragma unused(stop)
  7992. // Write the tag.
  7993. [outputStream writeInt32NoTag:tag];
  7994. // Write the size of the message.
  7995. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  7996. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  7997. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7998. // Write the fields.
  7999. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8000. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  8001. }];
  8002. }
  8003. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8004. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8005. }
  8006. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8007. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8008. #pragma unused(stop)
  8009. block(key, [NSString stringWithFormat:@"%d", value]);
  8010. }];
  8011. }
  8012. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8013. NSNumber *wrapped = [_dictionary objectForKey:key];
  8014. if (wrapped && value) {
  8015. *value = [wrapped intValue];
  8016. }
  8017. return (wrapped != NULL);
  8018. }
  8019. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8020. if (otherDictionary) {
  8021. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8022. }
  8023. }
  8024. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8025. [_dictionary setObject:@(value) forKey:key];
  8026. }
  8027. - (void)removeValueForKey:(NSString *)aKey {
  8028. [_dictionary removeObjectForKey:aKey];
  8029. }
  8030. - (void)removeAll {
  8031. [_dictionary removeAllObjects];
  8032. }
  8033. @end
  8034. #pragma mark - String -> UInt64
  8035. @implementation GPBStringUInt64Dictionary {
  8036. @package
  8037. NSMutableDictionary *_dictionary;
  8038. }
  8039. + (instancetype)dictionary {
  8040. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8041. }
  8042. + (instancetype)dictionaryWithValue:(uint64_t)value
  8043. forKey:(NSString *)key {
  8044. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8045. // on to get the type correct.
  8046. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8047. forKeys:&key
  8048. count:1] autorelease];
  8049. }
  8050. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8051. forKeys:(const NSString * [])keys
  8052. count:(NSUInteger)count {
  8053. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8054. // on to get the type correct.
  8055. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8056. forKeys:keys
  8057. count:count] autorelease];
  8058. }
  8059. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8060. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8061. // on to get the type correct.
  8062. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8063. }
  8064. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8065. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8066. }
  8067. - (instancetype)init {
  8068. return [self initWithValues:NULL forKeys:NULL count:0];
  8069. }
  8070. - (instancetype)initWithValues:(const uint64_t [])values
  8071. forKeys:(const NSString * [])keys
  8072. count:(NSUInteger)count {
  8073. self = [super init];
  8074. if (self) {
  8075. _dictionary = [[NSMutableDictionary alloc] init];
  8076. if (count && values && keys) {
  8077. for (NSUInteger i = 0; i < count; ++i) {
  8078. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8079. }
  8080. }
  8081. }
  8082. return self;
  8083. }
  8084. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8085. self = [self initWithValues:NULL forKeys:NULL count:0];
  8086. if (self) {
  8087. if (dictionary) {
  8088. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8089. }
  8090. }
  8091. return self;
  8092. }
  8093. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8094. #pragma unused(numItems)
  8095. return [self initWithValues:NULL forKeys:NULL count:0];
  8096. }
  8097. - (void)dealloc {
  8098. [_dictionary release];
  8099. [super dealloc];
  8100. }
  8101. - (instancetype)copyWithZone:(NSZone *)zone {
  8102. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8103. }
  8104. - (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
  8105. if (self == other) {
  8106. return YES;
  8107. }
  8108. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8109. return NO;
  8110. }
  8111. return [_dictionary isEqual:other->_dictionary];
  8112. }
  8113. - (NSUInteger)hash {
  8114. return _dictionary.count;
  8115. }
  8116. - (NSString *)description {
  8117. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8118. }
  8119. - (NSUInteger)count {
  8120. return _dictionary.count;
  8121. }
  8122. - (void)enumerateKeysAndValuesUsingBlock:
  8123. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8124. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8125. NSNumber *aValue,
  8126. BOOL *stop) {
  8127. block(aKey, [aValue unsignedLongLongValue], stop);
  8128. }];
  8129. }
  8130. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8131. NSUInteger count = _dictionary.count;
  8132. if (count == 0) {
  8133. return 0;
  8134. }
  8135. GPBType valueType = GPBGetFieldType(field);
  8136. GPBType keyType = field.mapKeyType;
  8137. __block size_t result = 0;
  8138. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8139. NSNumber *aValue,
  8140. BOOL *stop) {
  8141. #pragma unused(stop)
  8142. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8143. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  8144. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8145. }];
  8146. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8147. result += tagSize * count;
  8148. return result;
  8149. }
  8150. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8151. asField:(GPBFieldDescriptor *)field {
  8152. GPBType valueType = GPBGetFieldType(field);
  8153. GPBType keyType = field.mapKeyType;
  8154. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8155. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8156. NSNumber *aValue,
  8157. BOOL *stop) {
  8158. #pragma unused(stop)
  8159. // Write the tag.
  8160. [outputStream writeInt32NoTag:tag];
  8161. // Write the size of the message.
  8162. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8163. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  8164. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8165. // Write the fields.
  8166. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8167. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  8168. }];
  8169. }
  8170. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8171. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8172. }
  8173. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8174. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8175. #pragma unused(stop)
  8176. block(key, [NSString stringWithFormat:@"%llu", value]);
  8177. }];
  8178. }
  8179. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8180. NSNumber *wrapped = [_dictionary objectForKey:key];
  8181. if (wrapped && value) {
  8182. *value = [wrapped unsignedLongLongValue];
  8183. }
  8184. return (wrapped != NULL);
  8185. }
  8186. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8187. if (otherDictionary) {
  8188. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8189. }
  8190. }
  8191. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8192. [_dictionary setObject:@(value) forKey:key];
  8193. }
  8194. - (void)removeValueForKey:(NSString *)aKey {
  8195. [_dictionary removeObjectForKey:aKey];
  8196. }
  8197. - (void)removeAll {
  8198. [_dictionary removeAllObjects];
  8199. }
  8200. @end
  8201. #pragma mark - String -> Int64
  8202. @implementation GPBStringInt64Dictionary {
  8203. @package
  8204. NSMutableDictionary *_dictionary;
  8205. }
  8206. + (instancetype)dictionary {
  8207. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8208. }
  8209. + (instancetype)dictionaryWithValue:(int64_t)value
  8210. forKey:(NSString *)key {
  8211. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8212. // on to get the type correct.
  8213. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8214. forKeys:&key
  8215. count:1] autorelease];
  8216. }
  8217. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8218. forKeys:(const NSString * [])keys
  8219. count:(NSUInteger)count {
  8220. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8221. // on to get the type correct.
  8222. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8223. forKeys:keys
  8224. count:count] autorelease];
  8225. }
  8226. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8227. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8228. // on to get the type correct.
  8229. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8230. }
  8231. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8232. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8233. }
  8234. - (instancetype)init {
  8235. return [self initWithValues:NULL forKeys:NULL count:0];
  8236. }
  8237. - (instancetype)initWithValues:(const int64_t [])values
  8238. forKeys:(const NSString * [])keys
  8239. count:(NSUInteger)count {
  8240. self = [super init];
  8241. if (self) {
  8242. _dictionary = [[NSMutableDictionary alloc] init];
  8243. if (count && values && keys) {
  8244. for (NSUInteger i = 0; i < count; ++i) {
  8245. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8246. }
  8247. }
  8248. }
  8249. return self;
  8250. }
  8251. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8252. self = [self initWithValues:NULL forKeys:NULL count:0];
  8253. if (self) {
  8254. if (dictionary) {
  8255. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8256. }
  8257. }
  8258. return self;
  8259. }
  8260. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8261. #pragma unused(numItems)
  8262. return [self initWithValues:NULL forKeys:NULL count:0];
  8263. }
  8264. - (void)dealloc {
  8265. [_dictionary release];
  8266. [super dealloc];
  8267. }
  8268. - (instancetype)copyWithZone:(NSZone *)zone {
  8269. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8270. }
  8271. - (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
  8272. if (self == other) {
  8273. return YES;
  8274. }
  8275. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8276. return NO;
  8277. }
  8278. return [_dictionary isEqual:other->_dictionary];
  8279. }
  8280. - (NSUInteger)hash {
  8281. return _dictionary.count;
  8282. }
  8283. - (NSString *)description {
  8284. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8285. }
  8286. - (NSUInteger)count {
  8287. return _dictionary.count;
  8288. }
  8289. - (void)enumerateKeysAndValuesUsingBlock:
  8290. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8291. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8292. NSNumber *aValue,
  8293. BOOL *stop) {
  8294. block(aKey, [aValue longLongValue], stop);
  8295. }];
  8296. }
  8297. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8298. NSUInteger count = _dictionary.count;
  8299. if (count == 0) {
  8300. return 0;
  8301. }
  8302. GPBType valueType = GPBGetFieldType(field);
  8303. GPBType keyType = field.mapKeyType;
  8304. __block size_t result = 0;
  8305. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8306. NSNumber *aValue,
  8307. BOOL *stop) {
  8308. #pragma unused(stop)
  8309. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8310. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  8311. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8312. }];
  8313. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8314. result += tagSize * count;
  8315. return result;
  8316. }
  8317. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8318. asField:(GPBFieldDescriptor *)field {
  8319. GPBType valueType = GPBGetFieldType(field);
  8320. GPBType keyType = field.mapKeyType;
  8321. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8322. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8323. NSNumber *aValue,
  8324. BOOL *stop) {
  8325. #pragma unused(stop)
  8326. // Write the tag.
  8327. [outputStream writeInt32NoTag:tag];
  8328. // Write the size of the message.
  8329. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8330. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  8331. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8332. // Write the fields.
  8333. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8334. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  8335. }];
  8336. }
  8337. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8338. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8339. }
  8340. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8341. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8342. #pragma unused(stop)
  8343. block(key, [NSString stringWithFormat:@"%lld", value]);
  8344. }];
  8345. }
  8346. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8347. NSNumber *wrapped = [_dictionary objectForKey:key];
  8348. if (wrapped && value) {
  8349. *value = [wrapped longLongValue];
  8350. }
  8351. return (wrapped != NULL);
  8352. }
  8353. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8354. if (otherDictionary) {
  8355. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8356. }
  8357. }
  8358. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8359. [_dictionary setObject:@(value) forKey:key];
  8360. }
  8361. - (void)removeValueForKey:(NSString *)aKey {
  8362. [_dictionary removeObjectForKey:aKey];
  8363. }
  8364. - (void)removeAll {
  8365. [_dictionary removeAllObjects];
  8366. }
  8367. @end
  8368. #pragma mark - String -> Bool
  8369. @implementation GPBStringBoolDictionary {
  8370. @package
  8371. NSMutableDictionary *_dictionary;
  8372. }
  8373. + (instancetype)dictionary {
  8374. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8375. }
  8376. + (instancetype)dictionaryWithValue:(BOOL)value
  8377. forKey:(NSString *)key {
  8378. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8379. // on to get the type correct.
  8380. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  8381. forKeys:&key
  8382. count:1] autorelease];
  8383. }
  8384. + (instancetype)dictionaryWithValues:(const BOOL [])values
  8385. forKeys:(const NSString * [])keys
  8386. count:(NSUInteger)count {
  8387. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8388. // on to get the type correct.
  8389. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  8390. forKeys:keys
  8391. count:count] autorelease];
  8392. }
  8393. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8394. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8395. // on to get the type correct.
  8396. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8397. }
  8398. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8399. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8400. }
  8401. - (instancetype)init {
  8402. return [self initWithValues:NULL forKeys:NULL count:0];
  8403. }
  8404. - (instancetype)initWithValues:(const BOOL [])values
  8405. forKeys:(const NSString * [])keys
  8406. count:(NSUInteger)count {
  8407. self = [super init];
  8408. if (self) {
  8409. _dictionary = [[NSMutableDictionary alloc] init];
  8410. if (count && values && keys) {
  8411. for (NSUInteger i = 0; i < count; ++i) {
  8412. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8413. }
  8414. }
  8415. }
  8416. return self;
  8417. }
  8418. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8419. self = [self initWithValues:NULL forKeys:NULL count:0];
  8420. if (self) {
  8421. if (dictionary) {
  8422. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8423. }
  8424. }
  8425. return self;
  8426. }
  8427. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8428. #pragma unused(numItems)
  8429. return [self initWithValues:NULL forKeys:NULL count:0];
  8430. }
  8431. - (void)dealloc {
  8432. [_dictionary release];
  8433. [super dealloc];
  8434. }
  8435. - (instancetype)copyWithZone:(NSZone *)zone {
  8436. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8437. }
  8438. - (BOOL)isEqual:(GPBStringBoolDictionary *)other {
  8439. if (self == other) {
  8440. return YES;
  8441. }
  8442. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8443. return NO;
  8444. }
  8445. return [_dictionary isEqual:other->_dictionary];
  8446. }
  8447. - (NSUInteger)hash {
  8448. return _dictionary.count;
  8449. }
  8450. - (NSString *)description {
  8451. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8452. }
  8453. - (NSUInteger)count {
  8454. return _dictionary.count;
  8455. }
  8456. - (void)enumerateKeysAndValuesUsingBlock:
  8457. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  8458. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8459. NSNumber *aValue,
  8460. BOOL *stop) {
  8461. block(aKey, [aValue boolValue], stop);
  8462. }];
  8463. }
  8464. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8465. NSUInteger count = _dictionary.count;
  8466. if (count == 0) {
  8467. return 0;
  8468. }
  8469. GPBType valueType = GPBGetFieldType(field);
  8470. GPBType keyType = field.mapKeyType;
  8471. __block size_t result = 0;
  8472. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8473. NSNumber *aValue,
  8474. BOOL *stop) {
  8475. #pragma unused(stop)
  8476. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8477. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  8478. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8479. }];
  8480. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8481. result += tagSize * count;
  8482. return result;
  8483. }
  8484. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8485. asField:(GPBFieldDescriptor *)field {
  8486. GPBType valueType = GPBGetFieldType(field);
  8487. GPBType keyType = field.mapKeyType;
  8488. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8489. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8490. NSNumber *aValue,
  8491. BOOL *stop) {
  8492. #pragma unused(stop)
  8493. // Write the tag.
  8494. [outputStream writeInt32NoTag:tag];
  8495. // Write the size of the message.
  8496. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8497. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  8498. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8499. // Write the fields.
  8500. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8501. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  8502. }];
  8503. }
  8504. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8505. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8506. }
  8507. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8508. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  8509. #pragma unused(stop)
  8510. block(key, (value ? @"true" : @"false"));
  8511. }];
  8512. }
  8513. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  8514. NSNumber *wrapped = [_dictionary objectForKey:key];
  8515. if (wrapped && value) {
  8516. *value = [wrapped boolValue];
  8517. }
  8518. return (wrapped != NULL);
  8519. }
  8520. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  8521. if (otherDictionary) {
  8522. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8523. }
  8524. }
  8525. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  8526. [_dictionary setObject:@(value) forKey:key];
  8527. }
  8528. - (void)removeValueForKey:(NSString *)aKey {
  8529. [_dictionary removeObjectForKey:aKey];
  8530. }
  8531. - (void)removeAll {
  8532. [_dictionary removeAllObjects];
  8533. }
  8534. @end
  8535. #pragma mark - String -> Float
  8536. @implementation GPBStringFloatDictionary {
  8537. @package
  8538. NSMutableDictionary *_dictionary;
  8539. }
  8540. + (instancetype)dictionary {
  8541. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8542. }
  8543. + (instancetype)dictionaryWithValue:(float)value
  8544. forKey:(NSString *)key {
  8545. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8546. // on to get the type correct.
  8547. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  8548. forKeys:&key
  8549. count:1] autorelease];
  8550. }
  8551. + (instancetype)dictionaryWithValues:(const float [])values
  8552. forKeys:(const NSString * [])keys
  8553. count:(NSUInteger)count {
  8554. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8555. // on to get the type correct.
  8556. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  8557. forKeys:keys
  8558. count:count] autorelease];
  8559. }
  8560. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  8561. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8562. // on to get the type correct.
  8563. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8564. }
  8565. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8566. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8567. }
  8568. - (instancetype)init {
  8569. return [self initWithValues:NULL forKeys:NULL count:0];
  8570. }
  8571. - (instancetype)initWithValues:(const float [])values
  8572. forKeys:(const NSString * [])keys
  8573. count:(NSUInteger)count {
  8574. self = [super init];
  8575. if (self) {
  8576. _dictionary = [[NSMutableDictionary alloc] init];
  8577. if (count && values && keys) {
  8578. for (NSUInteger i = 0; i < count; ++i) {
  8579. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8580. }
  8581. }
  8582. }
  8583. return self;
  8584. }
  8585. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  8586. self = [self initWithValues:NULL forKeys:NULL count:0];
  8587. if (self) {
  8588. if (dictionary) {
  8589. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8590. }
  8591. }
  8592. return self;
  8593. }
  8594. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8595. #pragma unused(numItems)
  8596. return [self initWithValues:NULL forKeys:NULL count:0];
  8597. }
  8598. - (void)dealloc {
  8599. [_dictionary release];
  8600. [super dealloc];
  8601. }
  8602. - (instancetype)copyWithZone:(NSZone *)zone {
  8603. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  8604. }
  8605. - (BOOL)isEqual:(GPBStringFloatDictionary *)other {
  8606. if (self == other) {
  8607. return YES;
  8608. }
  8609. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  8610. return NO;
  8611. }
  8612. return [_dictionary isEqual:other->_dictionary];
  8613. }
  8614. - (NSUInteger)hash {
  8615. return _dictionary.count;
  8616. }
  8617. - (NSString *)description {
  8618. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8619. }
  8620. - (NSUInteger)count {
  8621. return _dictionary.count;
  8622. }
  8623. - (void)enumerateKeysAndValuesUsingBlock:
  8624. (void (^)(NSString *key, float value, BOOL *stop))block {
  8625. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8626. NSNumber *aValue,
  8627. BOOL *stop) {
  8628. block(aKey, [aValue floatValue], stop);
  8629. }];
  8630. }
  8631. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8632. NSUInteger count = _dictionary.count;
  8633. if (count == 0) {
  8634. return 0;
  8635. }
  8636. GPBType valueType = GPBGetFieldType(field);
  8637. GPBType keyType = field.mapKeyType;
  8638. __block size_t result = 0;
  8639. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8640. NSNumber *aValue,
  8641. BOOL *stop) {
  8642. #pragma unused(stop)
  8643. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8644. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  8645. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8646. }];
  8647. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8648. result += tagSize * count;
  8649. return result;
  8650. }
  8651. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8652. asField:(GPBFieldDescriptor *)field {
  8653. GPBType valueType = GPBGetFieldType(field);
  8654. GPBType keyType = field.mapKeyType;
  8655. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8656. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8657. NSNumber *aValue,
  8658. BOOL *stop) {
  8659. #pragma unused(stop)
  8660. // Write the tag.
  8661. [outputStream writeInt32NoTag:tag];
  8662. // Write the size of the message.
  8663. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8664. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  8665. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8666. // Write the fields.
  8667. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8668. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  8669. }];
  8670. }
  8671. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8672. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  8673. }
  8674. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8675. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  8676. #pragma unused(stop)
  8677. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  8678. }];
  8679. }
  8680. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  8681. NSNumber *wrapped = [_dictionary objectForKey:key];
  8682. if (wrapped && value) {
  8683. *value = [wrapped floatValue];
  8684. }
  8685. return (wrapped != NULL);
  8686. }
  8687. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  8688. if (otherDictionary) {
  8689. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8690. }
  8691. }
  8692. - (void)setValue:(float)value forKey:(NSString *)key {
  8693. [_dictionary setObject:@(value) forKey:key];
  8694. }
  8695. - (void)removeValueForKey:(NSString *)aKey {
  8696. [_dictionary removeObjectForKey:aKey];
  8697. }
  8698. - (void)removeAll {
  8699. [_dictionary removeAllObjects];
  8700. }
  8701. @end
  8702. #pragma mark - String -> Double
  8703. @implementation GPBStringDoubleDictionary {
  8704. @package
  8705. NSMutableDictionary *_dictionary;
  8706. }
  8707. + (instancetype)dictionary {
  8708. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8709. }
  8710. + (instancetype)dictionaryWithValue:(double)value
  8711. forKey:(NSString *)key {
  8712. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8713. // on to get the type correct.
  8714. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  8715. forKeys:&key
  8716. count:1] autorelease];
  8717. }
  8718. + (instancetype)dictionaryWithValues:(const double [])values
  8719. forKeys:(const NSString * [])keys
  8720. count:(NSUInteger)count {
  8721. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8722. // on to get the type correct.
  8723. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  8724. forKeys:keys
  8725. count:count] autorelease];
  8726. }
  8727. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  8728. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8729. // on to get the type correct.
  8730. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8731. }
  8732. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8733. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8734. }
  8735. - (instancetype)init {
  8736. return [self initWithValues:NULL forKeys:NULL count:0];
  8737. }
  8738. - (instancetype)initWithValues:(const double [])values
  8739. forKeys:(const NSString * [])keys
  8740. count:(NSUInteger)count {
  8741. self = [super init];
  8742. if (self) {
  8743. _dictionary = [[NSMutableDictionary alloc] init];
  8744. if (count && values && keys) {
  8745. for (NSUInteger i = 0; i < count; ++i) {
  8746. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8747. }
  8748. }
  8749. }
  8750. return self;
  8751. }
  8752. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  8753. self = [self initWithValues:NULL forKeys:NULL count:0];
  8754. if (self) {
  8755. if (dictionary) {
  8756. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8757. }
  8758. }
  8759. return self;
  8760. }
  8761. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8762. #pragma unused(numItems)
  8763. return [self initWithValues:NULL forKeys:NULL count:0];
  8764. }
  8765. - (void)dealloc {
  8766. [_dictionary release];
  8767. [super dealloc];
  8768. }
  8769. - (instancetype)copyWithZone:(NSZone *)zone {
  8770. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  8771. }
  8772. - (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
  8773. if (self == other) {
  8774. return YES;
  8775. }
  8776. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  8777. return NO;
  8778. }
  8779. return [_dictionary isEqual:other->_dictionary];
  8780. }
  8781. - (NSUInteger)hash {
  8782. return _dictionary.count;
  8783. }
  8784. - (NSString *)description {
  8785. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8786. }
  8787. - (NSUInteger)count {
  8788. return _dictionary.count;
  8789. }
  8790. - (void)enumerateKeysAndValuesUsingBlock:
  8791. (void (^)(NSString *key, double value, BOOL *stop))block {
  8792. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8793. NSNumber *aValue,
  8794. BOOL *stop) {
  8795. block(aKey, [aValue doubleValue], stop);
  8796. }];
  8797. }
  8798. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8799. NSUInteger count = _dictionary.count;
  8800. if (count == 0) {
  8801. return 0;
  8802. }
  8803. GPBType valueType = GPBGetFieldType(field);
  8804. GPBType keyType = field.mapKeyType;
  8805. __block size_t result = 0;
  8806. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8807. NSNumber *aValue,
  8808. BOOL *stop) {
  8809. #pragma unused(stop)
  8810. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8811. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  8812. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8813. }];
  8814. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8815. result += tagSize * count;
  8816. return result;
  8817. }
  8818. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8819. asField:(GPBFieldDescriptor *)field {
  8820. GPBType valueType = GPBGetFieldType(field);
  8821. GPBType keyType = field.mapKeyType;
  8822. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8823. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8824. NSNumber *aValue,
  8825. BOOL *stop) {
  8826. #pragma unused(stop)
  8827. // Write the tag.
  8828. [outputStream writeInt32NoTag:tag];
  8829. // Write the size of the message.
  8830. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8831. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  8832. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8833. // Write the fields.
  8834. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8835. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  8836. }];
  8837. }
  8838. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8839. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  8840. }
  8841. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8842. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  8843. #pragma unused(stop)
  8844. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  8845. }];
  8846. }
  8847. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  8848. NSNumber *wrapped = [_dictionary objectForKey:key];
  8849. if (wrapped && value) {
  8850. *value = [wrapped doubleValue];
  8851. }
  8852. return (wrapped != NULL);
  8853. }
  8854. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  8855. if (otherDictionary) {
  8856. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8857. }
  8858. }
  8859. - (void)setValue:(double)value forKey:(NSString *)key {
  8860. [_dictionary setObject:@(value) forKey:key];
  8861. }
  8862. - (void)removeValueForKey:(NSString *)aKey {
  8863. [_dictionary removeObjectForKey:aKey];
  8864. }
  8865. - (void)removeAll {
  8866. [_dictionary removeAllObjects];
  8867. }
  8868. @end
  8869. #pragma mark - String -> Enum
  8870. @implementation GPBStringEnumDictionary {
  8871. @package
  8872. NSMutableDictionary *_dictionary;
  8873. GPBEnumValidationFunc _validationFunc;
  8874. }
  8875. @synthesize validationFunc = _validationFunc;
  8876. + (instancetype)dictionary {
  8877. return [[[self alloc] initWithValidationFunction:NULL
  8878. rawValues:NULL
  8879. forKeys:NULL
  8880. count:0] autorelease];
  8881. }
  8882. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  8883. return [[[self alloc] initWithValidationFunction:func
  8884. rawValues:NULL
  8885. forKeys:NULL
  8886. count:0] autorelease];
  8887. }
  8888. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  8889. rawValue:(int32_t)rawValue
  8890. forKey:(NSString *)key {
  8891. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8892. // on to get the type correct.
  8893. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  8894. rawValues:&rawValue
  8895. forKeys:&key
  8896. count:1] autorelease];
  8897. }
  8898. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  8899. rawValues:(const int32_t [])rawValues
  8900. forKeys:(const NSString * [])keys
  8901. count:(NSUInteger)count {
  8902. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8903. // on to get the type correct.
  8904. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  8905. rawValues:rawValues
  8906. forKeys:keys
  8907. count:count] autorelease];
  8908. }
  8909. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  8910. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8911. // on to get the type correct.
  8912. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8913. }
  8914. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  8915. capacity:(NSUInteger)numItems {
  8916. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  8917. }
  8918. - (instancetype)init {
  8919. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  8920. }
  8921. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  8922. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8923. }
  8924. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8925. rawValues:(const int32_t [])rawValues
  8926. forKeys:(const NSString * [])keys
  8927. count:(NSUInteger)count {
  8928. self = [super init];
  8929. if (self) {
  8930. _dictionary = [[NSMutableDictionary alloc] init];
  8931. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  8932. if (count && rawValues && keys) {
  8933. for (NSUInteger i = 0; i < count; ++i) {
  8934. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  8935. }
  8936. }
  8937. }
  8938. return self;
  8939. }
  8940. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  8941. self = [self initWithValidationFunction:dictionary.validationFunc
  8942. rawValues:NULL
  8943. forKeys:NULL
  8944. count:0];
  8945. if (self) {
  8946. if (dictionary) {
  8947. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8948. }
  8949. }
  8950. return self;
  8951. }
  8952. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8953. capacity:(NSUInteger)numItems {
  8954. #pragma unused(numItems)
  8955. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8956. }
  8957. - (void)dealloc {
  8958. [_dictionary release];
  8959. [super dealloc];
  8960. }
  8961. - (instancetype)copyWithZone:(NSZone *)zone {
  8962. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  8963. }
  8964. - (BOOL)isEqual:(GPBStringEnumDictionary *)other {
  8965. if (self == other) {
  8966. return YES;
  8967. }
  8968. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  8969. return NO;
  8970. }
  8971. return [_dictionary isEqual:other->_dictionary];
  8972. }
  8973. - (NSUInteger)hash {
  8974. return _dictionary.count;
  8975. }
  8976. - (NSString *)description {
  8977. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8978. }
  8979. - (NSUInteger)count {
  8980. return _dictionary.count;
  8981. }
  8982. - (void)enumerateKeysAndRawValuesUsingBlock:
  8983. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8984. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8985. NSNumber *aValue,
  8986. BOOL *stop) {
  8987. block(aKey, [aValue intValue], stop);
  8988. }];
  8989. }
  8990. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8991. NSUInteger count = _dictionary.count;
  8992. if (count == 0) {
  8993. return 0;
  8994. }
  8995. GPBType valueType = GPBGetFieldType(field);
  8996. GPBType keyType = field.mapKeyType;
  8997. __block size_t result = 0;
  8998. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8999. NSNumber *aValue,
  9000. BOOL *stop) {
  9001. #pragma unused(stop)
  9002. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9003. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  9004. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9005. }];
  9006. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9007. result += tagSize * count;
  9008. return result;
  9009. }
  9010. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9011. asField:(GPBFieldDescriptor *)field {
  9012. GPBType valueType = GPBGetFieldType(field);
  9013. GPBType keyType = field.mapKeyType;
  9014. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9015. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9016. NSNumber *aValue,
  9017. BOOL *stop) {
  9018. #pragma unused(stop)
  9019. // Write the tag.
  9020. [outputStream writeInt32NoTag:tag];
  9021. // Write the size of the message.
  9022. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9023. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  9024. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9025. // Write the fields.
  9026. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  9027. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  9028. }];
  9029. }
  9030. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9031. forKey:(GPBValue *)key
  9032. keyType:(GPBType)keyType {
  9033. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyType);
  9034. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  9035. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9036. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9037. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyType);
  9038. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  9039. [outputStream release];
  9040. return data;
  9041. }
  9042. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9043. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9044. }
  9045. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9046. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9047. #pragma unused(stop)
  9048. block(key, @(value));
  9049. }];
  9050. }
  9051. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9052. NSNumber *wrapped = [_dictionary objectForKey:key];
  9053. if (wrapped && value) {
  9054. int32_t result = [wrapped intValue];
  9055. if (!_validationFunc(result)) {
  9056. result = kGPBUnrecognizedEnumeratorValue;
  9057. }
  9058. *value = result;
  9059. }
  9060. return (wrapped != NULL);
  9061. }
  9062. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9063. NSNumber *wrapped = [_dictionary objectForKey:key];
  9064. if (wrapped && rawValue) {
  9065. *rawValue = [wrapped intValue];
  9066. }
  9067. return (wrapped != NULL);
  9068. }
  9069. - (void)enumerateKeysAndValuesUsingBlock:
  9070. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9071. GPBEnumValidationFunc func = _validationFunc;
  9072. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9073. NSNumber *aValue,
  9074. BOOL *stop) {
  9075. int32_t unwrapped = [aValue intValue];
  9076. if (!func(unwrapped)) {
  9077. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9078. }
  9079. block(aKey, unwrapped, stop);
  9080. }];
  9081. }
  9082. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9083. if (otherDictionary) {
  9084. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9085. }
  9086. }
  9087. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9088. [_dictionary setObject:@(value) forKey:key];
  9089. }
  9090. - (void)removeValueForKey:(NSString *)aKey {
  9091. [_dictionary removeObjectForKey:aKey];
  9092. }
  9093. - (void)removeAll {
  9094. [_dictionary removeAllObjects];
  9095. }
  9096. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9097. if (!_validationFunc(value)) {
  9098. [NSException raise:NSInvalidArgumentException
  9099. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9100. value];
  9101. }
  9102. [_dictionary setObject:@(value) forKey:key];
  9103. }
  9104. @end
  9105. //%PDDM-EXPAND-END (5 expansions)
  9106. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9107. // This block of code is generated, do not edit it directly.
  9108. #pragma mark - Bool -> UInt32
  9109. @implementation GPBBoolUInt32Dictionary {
  9110. @package
  9111. uint32_t _values[2];
  9112. BOOL _valueSet[2];
  9113. }
  9114. + (instancetype)dictionary {
  9115. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9116. }
  9117. + (instancetype)dictionaryWithValue:(uint32_t)value
  9118. forKey:(BOOL)key {
  9119. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9120. // on to get the type correct.
  9121. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9122. forKeys:&key
  9123. count:1] autorelease];
  9124. }
  9125. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9126. forKeys:(const BOOL [])keys
  9127. count:(NSUInteger)count {
  9128. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9129. // on to get the type correct.
  9130. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9131. forKeys:keys
  9132. count:count] autorelease];
  9133. }
  9134. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9135. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9136. // on to get the type correct.
  9137. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9138. }
  9139. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9140. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9141. }
  9142. - (instancetype)init {
  9143. return [self initWithValues:NULL forKeys:NULL count:0];
  9144. }
  9145. - (instancetype)initWithValues:(const uint32_t [])values
  9146. forKeys:(const BOOL [])keys
  9147. count:(NSUInteger)count {
  9148. self = [super init];
  9149. if (self) {
  9150. for (NSUInteger i = 0; i < count; ++i) {
  9151. int idx = keys[i] ? 1 : 0;
  9152. _values[idx] = values[i];
  9153. _valueSet[idx] = YES;
  9154. }
  9155. }
  9156. return self;
  9157. }
  9158. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9159. self = [self initWithValues:NULL forKeys:NULL count:0];
  9160. if (self) {
  9161. if (dictionary) {
  9162. for (int i = 0; i < 2; ++i) {
  9163. if (dictionary->_valueSet[i]) {
  9164. _values[i] = dictionary->_values[i];
  9165. _valueSet[i] = YES;
  9166. }
  9167. }
  9168. }
  9169. }
  9170. return self;
  9171. }
  9172. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9173. #pragma unused(numItems)
  9174. return [self initWithValues:NULL forKeys:NULL count:0];
  9175. }
  9176. - (instancetype)copyWithZone:(NSZone *)zone {
  9177. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9178. }
  9179. - (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
  9180. if (self == other) {
  9181. return YES;
  9182. }
  9183. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9184. return NO;
  9185. }
  9186. if ((_valueSet[0] != other->_valueSet[0]) ||
  9187. (_valueSet[1] != other->_valueSet[1])) {
  9188. return NO;
  9189. }
  9190. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9191. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9192. return NO;
  9193. }
  9194. return YES;
  9195. }
  9196. - (NSUInteger)hash {
  9197. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9198. }
  9199. - (NSString *)description {
  9200. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9201. if (_valueSet[0]) {
  9202. [result appendFormat:@"NO: %u", _values[0]];
  9203. }
  9204. if (_valueSet[1]) {
  9205. [result appendFormat:@"YES: %u", _values[1]];
  9206. }
  9207. [result appendString:@" }"];
  9208. return result;
  9209. }
  9210. - (NSUInteger)count {
  9211. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9212. }
  9213. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9214. int idx = (key ? 1 : 0);
  9215. if (_valueSet[idx]) {
  9216. if (value) {
  9217. *value = _values[idx];
  9218. }
  9219. return YES;
  9220. }
  9221. return NO;
  9222. }
  9223. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9224. int idx = (key->valueBool ? 1 : 0);
  9225. _values[idx] = value->valueUInt32;
  9226. _valueSet[idx] = YES;
  9227. }
  9228. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9229. if (_valueSet[0]) {
  9230. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9231. }
  9232. if (_valueSet[1]) {
  9233. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9234. }
  9235. }
  9236. - (void)enumerateKeysAndValuesUsingBlock:
  9237. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9238. BOOL stop = NO;
  9239. if (_valueSet[0]) {
  9240. block(NO, _values[0], &stop);
  9241. }
  9242. if (!stop && _valueSet[1]) {
  9243. block(YES, _values[1], &stop);
  9244. }
  9245. }
  9246. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9247. GPBType valueType = GPBGetFieldType(field);
  9248. NSUInteger count = 0;
  9249. size_t result = 0;
  9250. for (int i = 0; i < 2; ++i) {
  9251. if (_valueSet[i]) {
  9252. ++count;
  9253. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9254. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9255. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9256. }
  9257. }
  9258. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9259. result += tagSize * count;
  9260. return result;
  9261. }
  9262. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9263. asField:(GPBFieldDescriptor *)field {
  9264. GPBType valueType = GPBGetFieldType(field);
  9265. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9266. for (int i = 0; i < 2; ++i) {
  9267. if (_valueSet[i]) {
  9268. // Write the tag.
  9269. [outputStream writeInt32NoTag:tag];
  9270. // Write the size of the message.
  9271. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9272. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9273. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9274. // Write the fields.
  9275. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9276. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  9277. }
  9278. }
  9279. }
  9280. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9281. if (otherDictionary) {
  9282. for (int i = 0; i < 2; ++i) {
  9283. if (otherDictionary->_valueSet[i]) {
  9284. _valueSet[i] = YES;
  9285. _values[i] = otherDictionary->_values[i];
  9286. }
  9287. }
  9288. }
  9289. }
  9290. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  9291. int idx = (key ? 1 : 0);
  9292. _values[idx] = value;
  9293. _valueSet[idx] = YES;
  9294. }
  9295. - (void)removeValueForKey:(BOOL)aKey {
  9296. _valueSet[aKey ? 1 : 0] = NO;
  9297. }
  9298. - (void)removeAll {
  9299. _valueSet[0] = NO;
  9300. _valueSet[1] = NO;
  9301. }
  9302. @end
  9303. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9304. // This block of code is generated, do not edit it directly.
  9305. #pragma mark - Bool -> Int32
  9306. @implementation GPBBoolInt32Dictionary {
  9307. @package
  9308. int32_t _values[2];
  9309. BOOL _valueSet[2];
  9310. }
  9311. + (instancetype)dictionary {
  9312. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9313. }
  9314. + (instancetype)dictionaryWithValue:(int32_t)value
  9315. forKey:(BOOL)key {
  9316. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9317. // on to get the type correct.
  9318. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  9319. forKeys:&key
  9320. count:1] autorelease];
  9321. }
  9322. + (instancetype)dictionaryWithValues:(const int32_t [])values
  9323. forKeys:(const BOOL [])keys
  9324. count:(NSUInteger)count {
  9325. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9326. // on to get the type correct.
  9327. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  9328. forKeys:keys
  9329. count:count] autorelease];
  9330. }
  9331. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9332. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9333. // on to get the type correct.
  9334. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9335. }
  9336. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9337. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9338. }
  9339. - (instancetype)init {
  9340. return [self initWithValues:NULL forKeys:NULL count:0];
  9341. }
  9342. - (instancetype)initWithValues:(const int32_t [])values
  9343. forKeys:(const BOOL [])keys
  9344. count:(NSUInteger)count {
  9345. self = [super init];
  9346. if (self) {
  9347. for (NSUInteger i = 0; i < count; ++i) {
  9348. int idx = keys[i] ? 1 : 0;
  9349. _values[idx] = values[i];
  9350. _valueSet[idx] = YES;
  9351. }
  9352. }
  9353. return self;
  9354. }
  9355. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9356. self = [self initWithValues:NULL forKeys:NULL count:0];
  9357. if (self) {
  9358. if (dictionary) {
  9359. for (int i = 0; i < 2; ++i) {
  9360. if (dictionary->_valueSet[i]) {
  9361. _values[i] = dictionary->_values[i];
  9362. _valueSet[i] = YES;
  9363. }
  9364. }
  9365. }
  9366. }
  9367. return self;
  9368. }
  9369. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9370. #pragma unused(numItems)
  9371. return [self initWithValues:NULL forKeys:NULL count:0];
  9372. }
  9373. - (instancetype)copyWithZone:(NSZone *)zone {
  9374. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9375. }
  9376. - (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
  9377. if (self == other) {
  9378. return YES;
  9379. }
  9380. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  9381. return NO;
  9382. }
  9383. if ((_valueSet[0] != other->_valueSet[0]) ||
  9384. (_valueSet[1] != other->_valueSet[1])) {
  9385. return NO;
  9386. }
  9387. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9388. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9389. return NO;
  9390. }
  9391. return YES;
  9392. }
  9393. - (NSUInteger)hash {
  9394. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9395. }
  9396. - (NSString *)description {
  9397. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9398. if (_valueSet[0]) {
  9399. [result appendFormat:@"NO: %d", _values[0]];
  9400. }
  9401. if (_valueSet[1]) {
  9402. [result appendFormat:@"YES: %d", _values[1]];
  9403. }
  9404. [result appendString:@" }"];
  9405. return result;
  9406. }
  9407. - (NSUInteger)count {
  9408. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9409. }
  9410. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  9411. int idx = (key ? 1 : 0);
  9412. if (_valueSet[idx]) {
  9413. if (value) {
  9414. *value = _values[idx];
  9415. }
  9416. return YES;
  9417. }
  9418. return NO;
  9419. }
  9420. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9421. int idx = (key->valueBool ? 1 : 0);
  9422. _values[idx] = value->valueInt32;
  9423. _valueSet[idx] = YES;
  9424. }
  9425. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9426. if (_valueSet[0]) {
  9427. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9428. }
  9429. if (_valueSet[1]) {
  9430. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9431. }
  9432. }
  9433. - (void)enumerateKeysAndValuesUsingBlock:
  9434. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  9435. BOOL stop = NO;
  9436. if (_valueSet[0]) {
  9437. block(NO, _values[0], &stop);
  9438. }
  9439. if (!stop && _valueSet[1]) {
  9440. block(YES, _values[1], &stop);
  9441. }
  9442. }
  9443. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9444. GPBType valueType = GPBGetFieldType(field);
  9445. NSUInteger count = 0;
  9446. size_t result = 0;
  9447. for (int i = 0; i < 2; ++i) {
  9448. if (_valueSet[i]) {
  9449. ++count;
  9450. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9451. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9452. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9453. }
  9454. }
  9455. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9456. result += tagSize * count;
  9457. return result;
  9458. }
  9459. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9460. asField:(GPBFieldDescriptor *)field {
  9461. GPBType valueType = GPBGetFieldType(field);
  9462. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9463. for (int i = 0; i < 2; ++i) {
  9464. if (_valueSet[i]) {
  9465. // Write the tag.
  9466. [outputStream writeInt32NoTag:tag];
  9467. // Write the size of the message.
  9468. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9469. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9470. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9471. // Write the fields.
  9472. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9473. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  9474. }
  9475. }
  9476. }
  9477. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  9478. if (otherDictionary) {
  9479. for (int i = 0; i < 2; ++i) {
  9480. if (otherDictionary->_valueSet[i]) {
  9481. _valueSet[i] = YES;
  9482. _values[i] = otherDictionary->_values[i];
  9483. }
  9484. }
  9485. }
  9486. }
  9487. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  9488. int idx = (key ? 1 : 0);
  9489. _values[idx] = value;
  9490. _valueSet[idx] = YES;
  9491. }
  9492. - (void)removeValueForKey:(BOOL)aKey {
  9493. _valueSet[aKey ? 1 : 0] = NO;
  9494. }
  9495. - (void)removeAll {
  9496. _valueSet[0] = NO;
  9497. _valueSet[1] = NO;
  9498. }
  9499. @end
  9500. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  9501. // This block of code is generated, do not edit it directly.
  9502. #pragma mark - Bool -> UInt64
  9503. @implementation GPBBoolUInt64Dictionary {
  9504. @package
  9505. uint64_t _values[2];
  9506. BOOL _valueSet[2];
  9507. }
  9508. + (instancetype)dictionary {
  9509. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9510. }
  9511. + (instancetype)dictionaryWithValue:(uint64_t)value
  9512. forKey:(BOOL)key {
  9513. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9514. // on to get the type correct.
  9515. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  9516. forKeys:&key
  9517. count:1] autorelease];
  9518. }
  9519. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  9520. forKeys:(const BOOL [])keys
  9521. count:(NSUInteger)count {
  9522. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9523. // on to get the type correct.
  9524. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  9525. forKeys:keys
  9526. count:count] autorelease];
  9527. }
  9528. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  9529. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9530. // on to get the type correct.
  9531. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9532. }
  9533. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9534. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9535. }
  9536. - (instancetype)init {
  9537. return [self initWithValues:NULL forKeys:NULL count:0];
  9538. }
  9539. - (instancetype)initWithValues:(const uint64_t [])values
  9540. forKeys:(const BOOL [])keys
  9541. count:(NSUInteger)count {
  9542. self = [super init];
  9543. if (self) {
  9544. for (NSUInteger i = 0; i < count; ++i) {
  9545. int idx = keys[i] ? 1 : 0;
  9546. _values[idx] = values[i];
  9547. _valueSet[idx] = YES;
  9548. }
  9549. }
  9550. return self;
  9551. }
  9552. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  9553. self = [self initWithValues:NULL forKeys:NULL count:0];
  9554. if (self) {
  9555. if (dictionary) {
  9556. for (int i = 0; i < 2; ++i) {
  9557. if (dictionary->_valueSet[i]) {
  9558. _values[i] = dictionary->_values[i];
  9559. _valueSet[i] = YES;
  9560. }
  9561. }
  9562. }
  9563. }
  9564. return self;
  9565. }
  9566. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9567. #pragma unused(numItems)
  9568. return [self initWithValues:NULL forKeys:NULL count:0];
  9569. }
  9570. - (instancetype)copyWithZone:(NSZone *)zone {
  9571. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9572. }
  9573. - (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
  9574. if (self == other) {
  9575. return YES;
  9576. }
  9577. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  9578. return NO;
  9579. }
  9580. if ((_valueSet[0] != other->_valueSet[0]) ||
  9581. (_valueSet[1] != other->_valueSet[1])) {
  9582. return NO;
  9583. }
  9584. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9585. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9586. return NO;
  9587. }
  9588. return YES;
  9589. }
  9590. - (NSUInteger)hash {
  9591. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9592. }
  9593. - (NSString *)description {
  9594. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9595. if (_valueSet[0]) {
  9596. [result appendFormat:@"NO: %llu", _values[0]];
  9597. }
  9598. if (_valueSet[1]) {
  9599. [result appendFormat:@"YES: %llu", _values[1]];
  9600. }
  9601. [result appendString:@" }"];
  9602. return result;
  9603. }
  9604. - (NSUInteger)count {
  9605. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9606. }
  9607. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  9608. int idx = (key ? 1 : 0);
  9609. if (_valueSet[idx]) {
  9610. if (value) {
  9611. *value = _values[idx];
  9612. }
  9613. return YES;
  9614. }
  9615. return NO;
  9616. }
  9617. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9618. int idx = (key->valueBool ? 1 : 0);
  9619. _values[idx] = value->valueUInt64;
  9620. _valueSet[idx] = YES;
  9621. }
  9622. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9623. if (_valueSet[0]) {
  9624. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  9625. }
  9626. if (_valueSet[1]) {
  9627. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  9628. }
  9629. }
  9630. - (void)enumerateKeysAndValuesUsingBlock:
  9631. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  9632. BOOL stop = NO;
  9633. if (_valueSet[0]) {
  9634. block(NO, _values[0], &stop);
  9635. }
  9636. if (!stop && _valueSet[1]) {
  9637. block(YES, _values[1], &stop);
  9638. }
  9639. }
  9640. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9641. GPBType valueType = GPBGetFieldType(field);
  9642. NSUInteger count = 0;
  9643. size_t result = 0;
  9644. for (int i = 0; i < 2; ++i) {
  9645. if (_valueSet[i]) {
  9646. ++count;
  9647. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9648. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9649. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9650. }
  9651. }
  9652. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9653. result += tagSize * count;
  9654. return result;
  9655. }
  9656. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9657. asField:(GPBFieldDescriptor *)field {
  9658. GPBType valueType = GPBGetFieldType(field);
  9659. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9660. for (int i = 0; i < 2; ++i) {
  9661. if (_valueSet[i]) {
  9662. // Write the tag.
  9663. [outputStream writeInt32NoTag:tag];
  9664. // Write the size of the message.
  9665. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9666. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9667. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9668. // Write the fields.
  9669. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9670. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  9671. }
  9672. }
  9673. }
  9674. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  9675. if (otherDictionary) {
  9676. for (int i = 0; i < 2; ++i) {
  9677. if (otherDictionary->_valueSet[i]) {
  9678. _valueSet[i] = YES;
  9679. _values[i] = otherDictionary->_values[i];
  9680. }
  9681. }
  9682. }
  9683. }
  9684. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  9685. int idx = (key ? 1 : 0);
  9686. _values[idx] = value;
  9687. _valueSet[idx] = YES;
  9688. }
  9689. - (void)removeValueForKey:(BOOL)aKey {
  9690. _valueSet[aKey ? 1 : 0] = NO;
  9691. }
  9692. - (void)removeAll {
  9693. _valueSet[0] = NO;
  9694. _valueSet[1] = NO;
  9695. }
  9696. @end
  9697. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  9698. // This block of code is generated, do not edit it directly.
  9699. #pragma mark - Bool -> Int64
  9700. @implementation GPBBoolInt64Dictionary {
  9701. @package
  9702. int64_t _values[2];
  9703. BOOL _valueSet[2];
  9704. }
  9705. + (instancetype)dictionary {
  9706. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9707. }
  9708. + (instancetype)dictionaryWithValue:(int64_t)value
  9709. forKey:(BOOL)key {
  9710. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9711. // on to get the type correct.
  9712. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  9713. forKeys:&key
  9714. count:1] autorelease];
  9715. }
  9716. + (instancetype)dictionaryWithValues:(const int64_t [])values
  9717. forKeys:(const BOOL [])keys
  9718. count:(NSUInteger)count {
  9719. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9720. // on to get the type correct.
  9721. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  9722. forKeys:keys
  9723. count:count] autorelease];
  9724. }
  9725. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  9726. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9727. // on to get the type correct.
  9728. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9729. }
  9730. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9731. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9732. }
  9733. - (instancetype)init {
  9734. return [self initWithValues:NULL forKeys:NULL count:0];
  9735. }
  9736. - (instancetype)initWithValues:(const int64_t [])values
  9737. forKeys:(const BOOL [])keys
  9738. count:(NSUInteger)count {
  9739. self = [super init];
  9740. if (self) {
  9741. for (NSUInteger i = 0; i < count; ++i) {
  9742. int idx = keys[i] ? 1 : 0;
  9743. _values[idx] = values[i];
  9744. _valueSet[idx] = YES;
  9745. }
  9746. }
  9747. return self;
  9748. }
  9749. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  9750. self = [self initWithValues:NULL forKeys:NULL count:0];
  9751. if (self) {
  9752. if (dictionary) {
  9753. for (int i = 0; i < 2; ++i) {
  9754. if (dictionary->_valueSet[i]) {
  9755. _values[i] = dictionary->_values[i];
  9756. _valueSet[i] = YES;
  9757. }
  9758. }
  9759. }
  9760. }
  9761. return self;
  9762. }
  9763. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9764. #pragma unused(numItems)
  9765. return [self initWithValues:NULL forKeys:NULL count:0];
  9766. }
  9767. - (instancetype)copyWithZone:(NSZone *)zone {
  9768. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9769. }
  9770. - (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
  9771. if (self == other) {
  9772. return YES;
  9773. }
  9774. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  9775. return NO;
  9776. }
  9777. if ((_valueSet[0] != other->_valueSet[0]) ||
  9778. (_valueSet[1] != other->_valueSet[1])) {
  9779. return NO;
  9780. }
  9781. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9782. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9783. return NO;
  9784. }
  9785. return YES;
  9786. }
  9787. - (NSUInteger)hash {
  9788. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9789. }
  9790. - (NSString *)description {
  9791. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9792. if (_valueSet[0]) {
  9793. [result appendFormat:@"NO: %lld", _values[0]];
  9794. }
  9795. if (_valueSet[1]) {
  9796. [result appendFormat:@"YES: %lld", _values[1]];
  9797. }
  9798. [result appendString:@" }"];
  9799. return result;
  9800. }
  9801. - (NSUInteger)count {
  9802. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9803. }
  9804. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  9805. int idx = (key ? 1 : 0);
  9806. if (_valueSet[idx]) {
  9807. if (value) {
  9808. *value = _values[idx];
  9809. }
  9810. return YES;
  9811. }
  9812. return NO;
  9813. }
  9814. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9815. int idx = (key->valueBool ? 1 : 0);
  9816. _values[idx] = value->valueInt64;
  9817. _valueSet[idx] = YES;
  9818. }
  9819. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9820. if (_valueSet[0]) {
  9821. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  9822. }
  9823. if (_valueSet[1]) {
  9824. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  9825. }
  9826. }
  9827. - (void)enumerateKeysAndValuesUsingBlock:
  9828. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  9829. BOOL stop = NO;
  9830. if (_valueSet[0]) {
  9831. block(NO, _values[0], &stop);
  9832. }
  9833. if (!stop && _valueSet[1]) {
  9834. block(YES, _values[1], &stop);
  9835. }
  9836. }
  9837. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9838. GPBType valueType = GPBGetFieldType(field);
  9839. NSUInteger count = 0;
  9840. size_t result = 0;
  9841. for (int i = 0; i < 2; ++i) {
  9842. if (_valueSet[i]) {
  9843. ++count;
  9844. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9845. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9846. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9847. }
  9848. }
  9849. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9850. result += tagSize * count;
  9851. return result;
  9852. }
  9853. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9854. asField:(GPBFieldDescriptor *)field {
  9855. GPBType valueType = GPBGetFieldType(field);
  9856. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9857. for (int i = 0; i < 2; ++i) {
  9858. if (_valueSet[i]) {
  9859. // Write the tag.
  9860. [outputStream writeInt32NoTag:tag];
  9861. // Write the size of the message.
  9862. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9863. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9864. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9865. // Write the fields.
  9866. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9867. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  9868. }
  9869. }
  9870. }
  9871. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  9872. if (otherDictionary) {
  9873. for (int i = 0; i < 2; ++i) {
  9874. if (otherDictionary->_valueSet[i]) {
  9875. _valueSet[i] = YES;
  9876. _values[i] = otherDictionary->_values[i];
  9877. }
  9878. }
  9879. }
  9880. }
  9881. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  9882. int idx = (key ? 1 : 0);
  9883. _values[idx] = value;
  9884. _valueSet[idx] = YES;
  9885. }
  9886. - (void)removeValueForKey:(BOOL)aKey {
  9887. _valueSet[aKey ? 1 : 0] = NO;
  9888. }
  9889. - (void)removeAll {
  9890. _valueSet[0] = NO;
  9891. _valueSet[1] = NO;
  9892. }
  9893. @end
  9894. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  9895. // This block of code is generated, do not edit it directly.
  9896. #pragma mark - Bool -> Bool
  9897. @implementation GPBBoolBoolDictionary {
  9898. @package
  9899. BOOL _values[2];
  9900. BOOL _valueSet[2];
  9901. }
  9902. + (instancetype)dictionary {
  9903. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9904. }
  9905. + (instancetype)dictionaryWithValue:(BOOL)value
  9906. forKey:(BOOL)key {
  9907. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9908. // on to get the type correct.
  9909. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  9910. forKeys:&key
  9911. count:1] autorelease];
  9912. }
  9913. + (instancetype)dictionaryWithValues:(const BOOL [])values
  9914. forKeys:(const BOOL [])keys
  9915. count:(NSUInteger)count {
  9916. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9917. // on to get the type correct.
  9918. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  9919. forKeys:keys
  9920. count:count] autorelease];
  9921. }
  9922. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  9923. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9924. // on to get the type correct.
  9925. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9926. }
  9927. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9928. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9929. }
  9930. - (instancetype)init {
  9931. return [self initWithValues:NULL forKeys:NULL count:0];
  9932. }
  9933. - (instancetype)initWithValues:(const BOOL [])values
  9934. forKeys:(const BOOL [])keys
  9935. count:(NSUInteger)count {
  9936. self = [super init];
  9937. if (self) {
  9938. for (NSUInteger i = 0; i < count; ++i) {
  9939. int idx = keys[i] ? 1 : 0;
  9940. _values[idx] = values[i];
  9941. _valueSet[idx] = YES;
  9942. }
  9943. }
  9944. return self;
  9945. }
  9946. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  9947. self = [self initWithValues:NULL forKeys:NULL count:0];
  9948. if (self) {
  9949. if (dictionary) {
  9950. for (int i = 0; i < 2; ++i) {
  9951. if (dictionary->_valueSet[i]) {
  9952. _values[i] = dictionary->_values[i];
  9953. _valueSet[i] = YES;
  9954. }
  9955. }
  9956. }
  9957. }
  9958. return self;
  9959. }
  9960. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9961. #pragma unused(numItems)
  9962. return [self initWithValues:NULL forKeys:NULL count:0];
  9963. }
  9964. - (instancetype)copyWithZone:(NSZone *)zone {
  9965. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9966. }
  9967. - (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
  9968. if (self == other) {
  9969. return YES;
  9970. }
  9971. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  9972. return NO;
  9973. }
  9974. if ((_valueSet[0] != other->_valueSet[0]) ||
  9975. (_valueSet[1] != other->_valueSet[1])) {
  9976. return NO;
  9977. }
  9978. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9979. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9980. return NO;
  9981. }
  9982. return YES;
  9983. }
  9984. - (NSUInteger)hash {
  9985. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9986. }
  9987. - (NSString *)description {
  9988. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9989. if (_valueSet[0]) {
  9990. [result appendFormat:@"NO: %d", _values[0]];
  9991. }
  9992. if (_valueSet[1]) {
  9993. [result appendFormat:@"YES: %d", _values[1]];
  9994. }
  9995. [result appendString:@" }"];
  9996. return result;
  9997. }
  9998. - (NSUInteger)count {
  9999. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10000. }
  10001. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10002. int idx = (key ? 1 : 0);
  10003. if (_valueSet[idx]) {
  10004. if (value) {
  10005. *value = _values[idx];
  10006. }
  10007. return YES;
  10008. }
  10009. return NO;
  10010. }
  10011. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10012. int idx = (key->valueBool ? 1 : 0);
  10013. _values[idx] = value->valueBool;
  10014. _valueSet[idx] = YES;
  10015. }
  10016. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10017. if (_valueSet[0]) {
  10018. block(@"false", (_values[0] ? @"true" : @"false"));
  10019. }
  10020. if (_valueSet[1]) {
  10021. block(@"true", (_values[1] ? @"true" : @"false"));
  10022. }
  10023. }
  10024. - (void)enumerateKeysAndValuesUsingBlock:
  10025. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10026. BOOL stop = NO;
  10027. if (_valueSet[0]) {
  10028. block(NO, _values[0], &stop);
  10029. }
  10030. if (!stop && _valueSet[1]) {
  10031. block(YES, _values[1], &stop);
  10032. }
  10033. }
  10034. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10035. GPBType valueType = GPBGetFieldType(field);
  10036. NSUInteger count = 0;
  10037. size_t result = 0;
  10038. for (int i = 0; i < 2; ++i) {
  10039. if (_valueSet[i]) {
  10040. ++count;
  10041. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10042. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10043. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10044. }
  10045. }
  10046. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10047. result += tagSize * count;
  10048. return result;
  10049. }
  10050. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10051. asField:(GPBFieldDescriptor *)field {
  10052. GPBType valueType = GPBGetFieldType(field);
  10053. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10054. for (int i = 0; i < 2; ++i) {
  10055. if (_valueSet[i]) {
  10056. // Write the tag.
  10057. [outputStream writeInt32NoTag:tag];
  10058. // Write the size of the message.
  10059. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10060. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10061. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10062. // Write the fields.
  10063. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10064. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10065. }
  10066. }
  10067. }
  10068. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10069. if (otherDictionary) {
  10070. for (int i = 0; i < 2; ++i) {
  10071. if (otherDictionary->_valueSet[i]) {
  10072. _valueSet[i] = YES;
  10073. _values[i] = otherDictionary->_values[i];
  10074. }
  10075. }
  10076. }
  10077. }
  10078. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10079. int idx = (key ? 1 : 0);
  10080. _values[idx] = value;
  10081. _valueSet[idx] = YES;
  10082. }
  10083. - (void)removeValueForKey:(BOOL)aKey {
  10084. _valueSet[aKey ? 1 : 0] = NO;
  10085. }
  10086. - (void)removeAll {
  10087. _valueSet[0] = NO;
  10088. _valueSet[1] = NO;
  10089. }
  10090. @end
  10091. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10092. // This block of code is generated, do not edit it directly.
  10093. #pragma mark - Bool -> Float
  10094. @implementation GPBBoolFloatDictionary {
  10095. @package
  10096. float _values[2];
  10097. BOOL _valueSet[2];
  10098. }
  10099. + (instancetype)dictionary {
  10100. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10101. }
  10102. + (instancetype)dictionaryWithValue:(float)value
  10103. forKey:(BOOL)key {
  10104. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10105. // on to get the type correct.
  10106. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10107. forKeys:&key
  10108. count:1] autorelease];
  10109. }
  10110. + (instancetype)dictionaryWithValues:(const float [])values
  10111. forKeys:(const BOOL [])keys
  10112. count:(NSUInteger)count {
  10113. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10114. // on to get the type correct.
  10115. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10116. forKeys:keys
  10117. count:count] autorelease];
  10118. }
  10119. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10120. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10121. // on to get the type correct.
  10122. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10123. }
  10124. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10125. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10126. }
  10127. - (instancetype)init {
  10128. return [self initWithValues:NULL forKeys:NULL count:0];
  10129. }
  10130. - (instancetype)initWithValues:(const float [])values
  10131. forKeys:(const BOOL [])keys
  10132. count:(NSUInteger)count {
  10133. self = [super init];
  10134. if (self) {
  10135. for (NSUInteger i = 0; i < count; ++i) {
  10136. int idx = keys[i] ? 1 : 0;
  10137. _values[idx] = values[i];
  10138. _valueSet[idx] = YES;
  10139. }
  10140. }
  10141. return self;
  10142. }
  10143. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10144. self = [self initWithValues:NULL forKeys:NULL count:0];
  10145. if (self) {
  10146. if (dictionary) {
  10147. for (int i = 0; i < 2; ++i) {
  10148. if (dictionary->_valueSet[i]) {
  10149. _values[i] = dictionary->_values[i];
  10150. _valueSet[i] = YES;
  10151. }
  10152. }
  10153. }
  10154. }
  10155. return self;
  10156. }
  10157. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10158. #pragma unused(numItems)
  10159. return [self initWithValues:NULL forKeys:NULL count:0];
  10160. }
  10161. - (instancetype)copyWithZone:(NSZone *)zone {
  10162. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10163. }
  10164. - (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
  10165. if (self == other) {
  10166. return YES;
  10167. }
  10168. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10169. return NO;
  10170. }
  10171. if ((_valueSet[0] != other->_valueSet[0]) ||
  10172. (_valueSet[1] != other->_valueSet[1])) {
  10173. return NO;
  10174. }
  10175. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10176. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10177. return NO;
  10178. }
  10179. return YES;
  10180. }
  10181. - (NSUInteger)hash {
  10182. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10183. }
  10184. - (NSString *)description {
  10185. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10186. if (_valueSet[0]) {
  10187. [result appendFormat:@"NO: %f", _values[0]];
  10188. }
  10189. if (_valueSet[1]) {
  10190. [result appendFormat:@"YES: %f", _values[1]];
  10191. }
  10192. [result appendString:@" }"];
  10193. return result;
  10194. }
  10195. - (NSUInteger)count {
  10196. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10197. }
  10198. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  10199. int idx = (key ? 1 : 0);
  10200. if (_valueSet[idx]) {
  10201. if (value) {
  10202. *value = _values[idx];
  10203. }
  10204. return YES;
  10205. }
  10206. return NO;
  10207. }
  10208. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10209. int idx = (key->valueBool ? 1 : 0);
  10210. _values[idx] = value->valueFloat;
  10211. _valueSet[idx] = YES;
  10212. }
  10213. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10214. if (_valueSet[0]) {
  10215. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  10216. }
  10217. if (_valueSet[1]) {
  10218. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  10219. }
  10220. }
  10221. - (void)enumerateKeysAndValuesUsingBlock:
  10222. (void (^)(BOOL key, float value, BOOL *stop))block {
  10223. BOOL stop = NO;
  10224. if (_valueSet[0]) {
  10225. block(NO, _values[0], &stop);
  10226. }
  10227. if (!stop && _valueSet[1]) {
  10228. block(YES, _values[1], &stop);
  10229. }
  10230. }
  10231. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10232. GPBType valueType = GPBGetFieldType(field);
  10233. NSUInteger count = 0;
  10234. size_t result = 0;
  10235. for (int i = 0; i < 2; ++i) {
  10236. if (_valueSet[i]) {
  10237. ++count;
  10238. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10239. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10240. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10241. }
  10242. }
  10243. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10244. result += tagSize * count;
  10245. return result;
  10246. }
  10247. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10248. asField:(GPBFieldDescriptor *)field {
  10249. GPBType valueType = GPBGetFieldType(field);
  10250. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10251. for (int i = 0; i < 2; ++i) {
  10252. if (_valueSet[i]) {
  10253. // Write the tag.
  10254. [outputStream writeInt32NoTag:tag];
  10255. // Write the size of the message.
  10256. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10257. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10258. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10259. // Write the fields.
  10260. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10261. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10262. }
  10263. }
  10264. }
  10265. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  10266. if (otherDictionary) {
  10267. for (int i = 0; i < 2; ++i) {
  10268. if (otherDictionary->_valueSet[i]) {
  10269. _valueSet[i] = YES;
  10270. _values[i] = otherDictionary->_values[i];
  10271. }
  10272. }
  10273. }
  10274. }
  10275. - (void)setValue:(float)value forKey:(BOOL)key {
  10276. int idx = (key ? 1 : 0);
  10277. _values[idx] = value;
  10278. _valueSet[idx] = YES;
  10279. }
  10280. - (void)removeValueForKey:(BOOL)aKey {
  10281. _valueSet[aKey ? 1 : 0] = NO;
  10282. }
  10283. - (void)removeAll {
  10284. _valueSet[0] = NO;
  10285. _valueSet[1] = NO;
  10286. }
  10287. @end
  10288. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  10289. // This block of code is generated, do not edit it directly.
  10290. #pragma mark - Bool -> Double
  10291. @implementation GPBBoolDoubleDictionary {
  10292. @package
  10293. double _values[2];
  10294. BOOL _valueSet[2];
  10295. }
  10296. + (instancetype)dictionary {
  10297. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10298. }
  10299. + (instancetype)dictionaryWithValue:(double)value
  10300. forKey:(BOOL)key {
  10301. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10302. // on to get the type correct.
  10303. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  10304. forKeys:&key
  10305. count:1] autorelease];
  10306. }
  10307. + (instancetype)dictionaryWithValues:(const double [])values
  10308. forKeys:(const BOOL [])keys
  10309. count:(NSUInteger)count {
  10310. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10311. // on to get the type correct.
  10312. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  10313. forKeys:keys
  10314. count:count] autorelease];
  10315. }
  10316. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10317. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10318. // on to get the type correct.
  10319. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10320. }
  10321. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10322. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10323. }
  10324. - (instancetype)init {
  10325. return [self initWithValues:NULL forKeys:NULL count:0];
  10326. }
  10327. - (instancetype)initWithValues:(const double [])values
  10328. forKeys:(const BOOL [])keys
  10329. count:(NSUInteger)count {
  10330. self = [super init];
  10331. if (self) {
  10332. for (NSUInteger i = 0; i < count; ++i) {
  10333. int idx = keys[i] ? 1 : 0;
  10334. _values[idx] = values[i];
  10335. _valueSet[idx] = YES;
  10336. }
  10337. }
  10338. return self;
  10339. }
  10340. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10341. self = [self initWithValues:NULL forKeys:NULL count:0];
  10342. if (self) {
  10343. if (dictionary) {
  10344. for (int i = 0; i < 2; ++i) {
  10345. if (dictionary->_valueSet[i]) {
  10346. _values[i] = dictionary->_values[i];
  10347. _valueSet[i] = YES;
  10348. }
  10349. }
  10350. }
  10351. }
  10352. return self;
  10353. }
  10354. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10355. #pragma unused(numItems)
  10356. return [self initWithValues:NULL forKeys:NULL count:0];
  10357. }
  10358. - (instancetype)copyWithZone:(NSZone *)zone {
  10359. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  10360. }
  10361. - (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
  10362. if (self == other) {
  10363. return YES;
  10364. }
  10365. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  10366. return NO;
  10367. }
  10368. if ((_valueSet[0] != other->_valueSet[0]) ||
  10369. (_valueSet[1] != other->_valueSet[1])) {
  10370. return NO;
  10371. }
  10372. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10373. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10374. return NO;
  10375. }
  10376. return YES;
  10377. }
  10378. - (NSUInteger)hash {
  10379. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10380. }
  10381. - (NSString *)description {
  10382. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10383. if (_valueSet[0]) {
  10384. [result appendFormat:@"NO: %lf", _values[0]];
  10385. }
  10386. if (_valueSet[1]) {
  10387. [result appendFormat:@"YES: %lf", _values[1]];
  10388. }
  10389. [result appendString:@" }"];
  10390. return result;
  10391. }
  10392. - (NSUInteger)count {
  10393. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10394. }
  10395. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  10396. int idx = (key ? 1 : 0);
  10397. if (_valueSet[idx]) {
  10398. if (value) {
  10399. *value = _values[idx];
  10400. }
  10401. return YES;
  10402. }
  10403. return NO;
  10404. }
  10405. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10406. int idx = (key->valueBool ? 1 : 0);
  10407. _values[idx] = value->valueDouble;
  10408. _valueSet[idx] = YES;
  10409. }
  10410. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10411. if (_valueSet[0]) {
  10412. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  10413. }
  10414. if (_valueSet[1]) {
  10415. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  10416. }
  10417. }
  10418. - (void)enumerateKeysAndValuesUsingBlock:
  10419. (void (^)(BOOL key, double value, BOOL *stop))block {
  10420. BOOL stop = NO;
  10421. if (_valueSet[0]) {
  10422. block(NO, _values[0], &stop);
  10423. }
  10424. if (!stop && _valueSet[1]) {
  10425. block(YES, _values[1], &stop);
  10426. }
  10427. }
  10428. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10429. GPBType valueType = GPBGetFieldType(field);
  10430. NSUInteger count = 0;
  10431. size_t result = 0;
  10432. for (int i = 0; i < 2; ++i) {
  10433. if (_valueSet[i]) {
  10434. ++count;
  10435. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10436. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10437. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10438. }
  10439. }
  10440. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10441. result += tagSize * count;
  10442. return result;
  10443. }
  10444. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10445. asField:(GPBFieldDescriptor *)field {
  10446. GPBType valueType = GPBGetFieldType(field);
  10447. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10448. for (int i = 0; i < 2; ++i) {
  10449. if (_valueSet[i]) {
  10450. // Write the tag.
  10451. [outputStream writeInt32NoTag:tag];
  10452. // Write the size of the message.
  10453. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10454. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10455. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10456. // Write the fields.
  10457. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10458. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10459. }
  10460. }
  10461. }
  10462. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  10463. if (otherDictionary) {
  10464. for (int i = 0; i < 2; ++i) {
  10465. if (otherDictionary->_valueSet[i]) {
  10466. _valueSet[i] = YES;
  10467. _values[i] = otherDictionary->_values[i];
  10468. }
  10469. }
  10470. }
  10471. }
  10472. - (void)setValue:(double)value forKey:(BOOL)key {
  10473. int idx = (key ? 1 : 0);
  10474. _values[idx] = value;
  10475. _valueSet[idx] = YES;
  10476. }
  10477. - (void)removeValueForKey:(BOOL)aKey {
  10478. _valueSet[aKey ? 1 : 0] = NO;
  10479. }
  10480. - (void)removeAll {
  10481. _valueSet[0] = NO;
  10482. _valueSet[1] = NO;
  10483. }
  10484. @end
  10485. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  10486. // This block of code is generated, do not edit it directly.
  10487. #pragma mark - Bool -> Object
  10488. @implementation GPBBoolObjectDictionary {
  10489. @package
  10490. id _values[2];
  10491. }
  10492. + (instancetype)dictionary {
  10493. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10494. }
  10495. + (instancetype)dictionaryWithValue:(id)value
  10496. forKey:(BOOL)key {
  10497. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10498. // on to get the type correct.
  10499. return [[(GPBBoolObjectDictionary*)[self alloc] initWithValues:&value
  10500. forKeys:&key
  10501. count:1] autorelease];
  10502. }
  10503. + (instancetype)dictionaryWithValues:(const id [])values
  10504. forKeys:(const BOOL [])keys
  10505. count:(NSUInteger)count {
  10506. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10507. // on to get the type correct.
  10508. return [[(GPBBoolObjectDictionary*)[self alloc] initWithValues:values
  10509. forKeys:keys
  10510. count:count] autorelease];
  10511. }
  10512. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  10513. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10514. // on to get the type correct.
  10515. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10516. }
  10517. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10518. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10519. }
  10520. - (instancetype)init {
  10521. return [self initWithValues:NULL forKeys:NULL count:0];
  10522. }
  10523. - (instancetype)initWithValues:(const id [])values
  10524. forKeys:(const BOOL [])keys
  10525. count:(NSUInteger)count {
  10526. self = [super init];
  10527. if (self) {
  10528. for (NSUInteger i = 0; i < count; ++i) {
  10529. int idx = keys[i] ? 1 : 0;
  10530. [_values[idx] release];
  10531. _values[idx] = (id)[values[i] retain];
  10532. }
  10533. }
  10534. return self;
  10535. }
  10536. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  10537. self = [self initWithValues:NULL forKeys:NULL count:0];
  10538. if (self) {
  10539. if (dictionary) {
  10540. _values[0] = [dictionary->_values[0] retain];
  10541. _values[1] = [dictionary->_values[1] retain];
  10542. }
  10543. }
  10544. return self;
  10545. }
  10546. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10547. #pragma unused(numItems)
  10548. return [self initWithValues:NULL forKeys:NULL count:0];
  10549. }
  10550. - (void)dealloc {
  10551. [_values[0] release];
  10552. [_values[1] release];
  10553. [super dealloc];
  10554. }
  10555. - (instancetype)copyWithZone:(NSZone *)zone {
  10556. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  10557. }
  10558. - (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
  10559. if (self == other) {
  10560. return YES;
  10561. }
  10562. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  10563. return NO;
  10564. }
  10565. if (((_values[0] != nil) != (other->_values[0] != nil)) ||
  10566. ((_values[1] != nil) != (other->_values[1] != nil))) {
  10567. return NO;
  10568. }
  10569. if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
  10570. ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
  10571. return NO;
  10572. }
  10573. return YES;
  10574. }
  10575. - (NSUInteger)hash {
  10576. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10577. }
  10578. - (NSString *)description {
  10579. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10580. if ((_values[0] != nil)) {
  10581. [result appendFormat:@"NO: %@", _values[0]];
  10582. }
  10583. if ((_values[1] != nil)) {
  10584. [result appendFormat:@"YES: %@", _values[1]];
  10585. }
  10586. [result appendString:@" }"];
  10587. return result;
  10588. }
  10589. - (NSUInteger)count {
  10590. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10591. }
  10592. - (id)valueForKey:(BOOL)key {
  10593. return _values[key ? 1 : 0];
  10594. }
  10595. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10596. int idx = (key->valueBool ? 1 : 0);
  10597. [_values[idx] release];
  10598. _values[idx] = [value->valueString retain];
  10599. }
  10600. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10601. if (_values[0] != nil) {
  10602. block(@"false", _values[0]);
  10603. }
  10604. if ((_values[1] != nil)) {
  10605. block(@"true", _values[1]);
  10606. }
  10607. }
  10608. - (void)enumerateKeysAndValuesUsingBlock:
  10609. (void (^)(BOOL key, id value, BOOL *stop))block {
  10610. BOOL stop = NO;
  10611. if (_values[0] != nil) {
  10612. block(NO, _values[0], &stop);
  10613. }
  10614. if (!stop && (_values[1] != nil)) {
  10615. block(YES, _values[1], &stop);
  10616. }
  10617. }
  10618. - (BOOL)isInitialized {
  10619. if (_values[0] && ![_values[0] isInitialized]) {
  10620. return NO;
  10621. }
  10622. if (_values[1] && ![_values[1] isInitialized]) {
  10623. return NO;
  10624. }
  10625. return YES;
  10626. }
  10627. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  10628. GPBBoolObjectDictionary *newDict =
  10629. [[GPBBoolObjectDictionary alloc] init];
  10630. for (int i = 0; i < 2; ++i) {
  10631. if (_values[i] != nil) {
  10632. newDict->_values[i] = [_values[i] copyWithZone:zone];
  10633. }
  10634. }
  10635. return newDict;
  10636. }
  10637. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10638. GPBType valueType = GPBGetFieldType(field);
  10639. NSUInteger count = 0;
  10640. size_t result = 0;
  10641. for (int i = 0; i < 2; ++i) {
  10642. if (_values[i] != nil) {
  10643. ++count;
  10644. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10645. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10646. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10647. }
  10648. }
  10649. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10650. result += tagSize * count;
  10651. return result;
  10652. }
  10653. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10654. asField:(GPBFieldDescriptor *)field {
  10655. GPBType valueType = GPBGetFieldType(field);
  10656. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10657. for (int i = 0; i < 2; ++i) {
  10658. if (_values[i] != nil) {
  10659. // Write the tag.
  10660. [outputStream writeInt32NoTag:tag];
  10661. // Write the size of the message.
  10662. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10663. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10664. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10665. // Write the fields.
  10666. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10667. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10668. }
  10669. }
  10670. }
  10671. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  10672. if (otherDictionary) {
  10673. for (int i = 0; i < 2; ++i) {
  10674. if (otherDictionary->_values[i] != nil) {
  10675. [_values[i] release];
  10676. _values[i] = [otherDictionary->_values[i] retain];
  10677. }
  10678. }
  10679. }
  10680. }
  10681. - (void)setValue:(id)value forKey:(BOOL)key {
  10682. int idx = (key ? 1 : 0);
  10683. [_values[idx] release];
  10684. _values[idx] = [value retain];
  10685. }
  10686. - (void)removeValueForKey:(BOOL)aKey {
  10687. int idx = (aKey ? 1 : 0);
  10688. [_values[idx] release];
  10689. _values[idx] = nil;
  10690. }
  10691. - (void)removeAll {
  10692. for (int i = 0; i < 2; ++i) {
  10693. [_values[i] release];
  10694. _values[i] = nil;
  10695. }
  10696. }
  10697. @end
  10698. //%PDDM-EXPAND-END (8 expansions)
  10699. #pragma mark - Bool -> Enum
  10700. @implementation GPBBoolEnumDictionary {
  10701. @package
  10702. GPBEnumValidationFunc _validationFunc;
  10703. int32_t _values[2];
  10704. BOOL _valueSet[2];
  10705. }
  10706. @synthesize validationFunc = _validationFunc;
  10707. + (instancetype)dictionary {
  10708. return [[[self alloc] initWithValidationFunction:NULL
  10709. rawValues:NULL
  10710. forKeys:NULL
  10711. count:0] autorelease];
  10712. }
  10713. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  10714. return [[[self alloc] initWithValidationFunction:func
  10715. rawValues:NULL
  10716. forKeys:NULL
  10717. count:0] autorelease];
  10718. }
  10719. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  10720. rawValue:(int32_t)rawValue
  10721. forKey:(BOOL)key {
  10722. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10723. // on to get the type correct.
  10724. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  10725. rawValues:&rawValue
  10726. forKeys:&key
  10727. count:1] autorelease];
  10728. }
  10729. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  10730. rawValues:(const int32_t [])values
  10731. forKeys:(const BOOL [])keys
  10732. count:(NSUInteger)count {
  10733. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10734. // on to get the type correct.
  10735. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  10736. rawValues:values
  10737. forKeys:keys
  10738. count:count] autorelease];
  10739. }
  10740. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  10741. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10742. // on to get the type correct.
  10743. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10744. }
  10745. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  10746. capacity:(NSUInteger)numItems {
  10747. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  10748. }
  10749. - (instancetype)init {
  10750. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  10751. }
  10752. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  10753. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10754. }
  10755. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10756. rawValues:(const int32_t [])rawValues
  10757. forKeys:(const BOOL [])keys
  10758. count:(NSUInteger)count {
  10759. self = [super init];
  10760. if (self) {
  10761. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  10762. for (NSUInteger i = 0; i < count; ++i) {
  10763. int idx = keys[i] ? 1 : 0;
  10764. _values[idx] = rawValues[i];
  10765. _valueSet[idx] = YES;
  10766. }
  10767. }
  10768. return self;
  10769. }
  10770. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  10771. self = [self initWithValidationFunction:dictionary.validationFunc
  10772. rawValues:NULL
  10773. forKeys:NULL
  10774. count:0];
  10775. if (self) {
  10776. if (dictionary) {
  10777. for (int i = 0; i < 2; ++i) {
  10778. if (dictionary->_valueSet[i]) {
  10779. _values[i] = dictionary->_values[i];
  10780. _valueSet[i] = YES;
  10781. }
  10782. }
  10783. }
  10784. }
  10785. return self;
  10786. }
  10787. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10788. capacity:(NSUInteger)numItems {
  10789. #pragma unused(numItems)
  10790. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10791. }
  10792. - (instancetype)copyWithZone:(NSZone *)zone {
  10793. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  10794. }
  10795. - (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
  10796. if (self == other) {
  10797. return YES;
  10798. }
  10799. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  10800. return NO;
  10801. }
  10802. if ((_valueSet[0] != other->_valueSet[0]) ||
  10803. (_valueSet[1] != other->_valueSet[1])) {
  10804. return NO;
  10805. }
  10806. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10807. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10808. return NO;
  10809. }
  10810. return YES;
  10811. }
  10812. - (NSUInteger)hash {
  10813. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10814. }
  10815. - (NSString *)description {
  10816. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10817. if (_valueSet[0]) {
  10818. [result appendFormat:@"NO: %d", _values[0]];
  10819. }
  10820. if (_valueSet[1]) {
  10821. [result appendFormat:@"YES: %d", _values[1]];
  10822. }
  10823. [result appendString:@" }"];
  10824. return result;
  10825. }
  10826. - (NSUInteger)count {
  10827. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10828. }
  10829. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  10830. int idx = (key ? 1 : 0);
  10831. if (_valueSet[idx]) {
  10832. if (value) {
  10833. int32_t result = _values[idx];
  10834. if (!_validationFunc(result)) {
  10835. result = kGPBUnrecognizedEnumeratorValue;
  10836. }
  10837. *value = result;
  10838. }
  10839. return YES;
  10840. }
  10841. return NO;
  10842. }
  10843. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  10844. int idx = (key ? 1 : 0);
  10845. if (_valueSet[idx]) {
  10846. if (rawValue) {
  10847. *rawValue = _values[idx];
  10848. }
  10849. return YES;
  10850. }
  10851. return NO;
  10852. }
  10853. - (void)enumerateKeysAndValuesUsingBlock:
  10854. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10855. BOOL stop = NO;
  10856. if (_valueSet[0]) {
  10857. block(NO, _values[0], &stop);
  10858. }
  10859. if (!stop && _valueSet[1]) {
  10860. block(YES, _values[1], &stop);
  10861. }
  10862. }
  10863. - (void)enumerateKeysAndRawValuesUsingBlock:
  10864. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  10865. BOOL stop = NO;
  10866. GPBEnumValidationFunc func = _validationFunc;
  10867. int32_t validatedValue;
  10868. if (_valueSet[0]) {
  10869. validatedValue = _values[0];
  10870. if (!func(validatedValue)) {
  10871. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10872. }
  10873. block(NO, validatedValue, &stop);
  10874. }
  10875. if (!stop && _valueSet[1]) {
  10876. validatedValue = _values[1];
  10877. if (!func(validatedValue)) {
  10878. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10879. }
  10880. block(YES, validatedValue, &stop);
  10881. }
  10882. }
  10883. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10884. // This block of code is generated, do not edit it directly.
  10885. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  10886. forKey:(GPBValue *)key
  10887. keyType:(GPBType)keyType {
  10888. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyType);
  10889. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  10890. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  10891. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  10892. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyType);
  10893. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  10894. [outputStream release];
  10895. return data;
  10896. }
  10897. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10898. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10899. GPBType valueType = GPBGetFieldType(field);
  10900. NSUInteger count = 0;
  10901. size_t result = 0;
  10902. for (int i = 0; i < 2; ++i) {
  10903. if (_valueSet[i]) {
  10904. ++count;
  10905. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10906. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  10907. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10908. }
  10909. }
  10910. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10911. result += tagSize * count;
  10912. return result;
  10913. }
  10914. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10915. asField:(GPBFieldDescriptor *)field {
  10916. GPBType valueType = GPBGetFieldType(field);
  10917. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10918. for (int i = 0; i < 2; ++i) {
  10919. if (_valueSet[i]) {
  10920. // Write the tag.
  10921. [outputStream writeInt32NoTag:tag];
  10922. // Write the size of the message.
  10923. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10924. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  10925. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10926. // Write the fields.
  10927. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10928. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10929. }
  10930. }
  10931. }
  10932. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10933. if (_valueSet[0]) {
  10934. block(@"false", @(_values[0]));
  10935. }
  10936. if (_valueSet[1]) {
  10937. block(@"true", @(_values[1]));
  10938. }
  10939. }
  10940. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10941. int idx = (key->valueBool ? 1 : 0);
  10942. _values[idx] = value->valueInt32;
  10943. _valueSet[idx] = YES;
  10944. }
  10945. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  10946. if (otherDictionary) {
  10947. for (int i = 0; i < 2; ++i) {
  10948. if (otherDictionary->_valueSet[i]) {
  10949. _valueSet[i] = YES;
  10950. _values[i] = otherDictionary->_values[i];
  10951. }
  10952. }
  10953. }
  10954. }
  10955. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  10956. if (!_validationFunc(value)) {
  10957. [NSException raise:NSInvalidArgumentException
  10958. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  10959. value];
  10960. }
  10961. int idx = (key ? 1 : 0);
  10962. _values[idx] = value;
  10963. _valueSet[idx] = YES;
  10964. }
  10965. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  10966. int idx = (key ? 1 : 0);
  10967. _values[idx] = rawValue;
  10968. _valueSet[idx] = YES;
  10969. }
  10970. - (void)removeValueForKey:(BOOL)aKey {
  10971. _valueSet[aKey ? 1 : 0] = NO;
  10972. }
  10973. - (void)removeAll {
  10974. _valueSet[0] = NO;
  10975. _valueSet[1] = NO;
  10976. }
  10977. @end