GPBDictionary.m 462 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530
  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. enum {
  45. kMapKeyFieldNumber = 1,
  46. kMapValueFieldNumber = 2,
  47. };
  48. static BOOL DictDefault_IsValidValue(int32_t value) {
  49. // Anything but the bad value marker is allowed.
  50. return (value != kGPBUnrecognizedEnumeratorValue);
  51. }
  52. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  53. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  54. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  55. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  56. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  57. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  58. //% } else {
  59. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  60. //% return 0;
  61. //% }
  62. //%}
  63. //%
  64. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  65. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  66. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  67. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  68. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  69. //% } else {
  70. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  71. //% }
  72. //%}
  73. //%
  74. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  75. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  76. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  77. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  78. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  79. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  80. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  81. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  82. //% } else {
  83. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  84. //% return 0;
  85. //% }
  86. //%}
  87. //%
  88. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  89. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  90. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  91. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  92. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  93. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  94. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  95. //% } else {
  96. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  97. //% }
  98. //%}
  99. //%
  100. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  101. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  102. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  103. //% #pragma unused(dataType) // For when asserts are off in release.
  104. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  105. //%}
  106. //%
  107. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  108. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  109. //% #pragma unused(dataType) // For when asserts are off in release.
  110. //% [stream write##VALUE_NAME##:fieldNum value:value];
  111. //%}
  112. //%
  113. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  114. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  115. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  116. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  117. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  118. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  119. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  120. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  121. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  122. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  123. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  124. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  125. // This block of code is generated, do not edit it directly.
  126. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  127. if (dataType == GPBDataTypeInt32) {
  128. return GPBComputeInt32Size(fieldNum, value);
  129. } else if (dataType == GPBDataTypeSInt32) {
  130. return GPBComputeSInt32Size(fieldNum, value);
  131. } else if (dataType == GPBDataTypeSFixed32) {
  132. return GPBComputeSFixed32Size(fieldNum, value);
  133. } else {
  134. NSCAssert(NO, @"Unexpected type %d", dataType);
  135. return 0;
  136. }
  137. }
  138. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  139. if (dataType == GPBDataTypeInt32) {
  140. [stream writeInt32:fieldNum value:value];
  141. } else if (dataType == GPBDataTypeSInt32) {
  142. [stream writeSInt32:fieldNum value:value];
  143. } else if (dataType == GPBDataTypeSFixed32) {
  144. [stream writeSFixed32:fieldNum value:value];
  145. } else {
  146. NSCAssert(NO, @"Unexpected type %d", dataType);
  147. }
  148. }
  149. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  150. if (dataType == GPBDataTypeUInt32) {
  151. return GPBComputeUInt32Size(fieldNum, value);
  152. } else if (dataType == GPBDataTypeFixed32) {
  153. return GPBComputeFixed32Size(fieldNum, value);
  154. } else {
  155. NSCAssert(NO, @"Unexpected type %d", dataType);
  156. return 0;
  157. }
  158. }
  159. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  160. if (dataType == GPBDataTypeUInt32) {
  161. [stream writeUInt32:fieldNum value:value];
  162. } else if (dataType == GPBDataTypeFixed32) {
  163. [stream writeFixed32:fieldNum value:value];
  164. } else {
  165. NSCAssert(NO, @"Unexpected type %d", dataType);
  166. }
  167. }
  168. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  169. if (dataType == GPBDataTypeInt64) {
  170. return GPBComputeInt64Size(fieldNum, value);
  171. } else if (dataType == GPBDataTypeSInt64) {
  172. return GPBComputeSInt64Size(fieldNum, value);
  173. } else if (dataType == GPBDataTypeSFixed64) {
  174. return GPBComputeSFixed64Size(fieldNum, value);
  175. } else {
  176. NSCAssert(NO, @"Unexpected type %d", dataType);
  177. return 0;
  178. }
  179. }
  180. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  181. if (dataType == GPBDataTypeInt64) {
  182. [stream writeInt64:fieldNum value:value];
  183. } else if (dataType == GPBDataTypeSInt64) {
  184. [stream writeSInt64:fieldNum value:value];
  185. } else if (dataType == GPBDataTypeSFixed64) {
  186. [stream writeSFixed64:fieldNum value:value];
  187. } else {
  188. NSCAssert(NO, @"Unexpected type %d", dataType);
  189. }
  190. }
  191. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  192. if (dataType == GPBDataTypeUInt64) {
  193. return GPBComputeUInt64Size(fieldNum, value);
  194. } else if (dataType == GPBDataTypeFixed64) {
  195. return GPBComputeFixed64Size(fieldNum, value);
  196. } else {
  197. NSCAssert(NO, @"Unexpected type %d", dataType);
  198. return 0;
  199. }
  200. }
  201. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  202. if (dataType == GPBDataTypeUInt64) {
  203. [stream writeUInt64:fieldNum value:value];
  204. } else if (dataType == GPBDataTypeFixed64) {
  205. [stream writeFixed64:fieldNum value:value];
  206. } else {
  207. NSCAssert(NO, @"Unexpected type %d", dataType);
  208. }
  209. }
  210. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  211. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  212. #pragma unused(dataType) // For when asserts are off in release.
  213. return GPBComputeBoolSize(fieldNum, value);
  214. }
  215. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  216. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  217. #pragma unused(dataType) // For when asserts are off in release.
  218. [stream writeBool:fieldNum value:value];
  219. }
  220. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  221. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  222. #pragma unused(dataType) // For when asserts are off in release.
  223. return GPBComputeEnumSize(fieldNum, value);
  224. }
  225. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  226. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  227. #pragma unused(dataType) // For when asserts are off in release.
  228. [stream writeEnum:fieldNum value:value];
  229. }
  230. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  231. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  232. #pragma unused(dataType) // For when asserts are off in release.
  233. return GPBComputeFloatSize(fieldNum, value);
  234. }
  235. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
  236. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  237. #pragma unused(dataType) // For when asserts are off in release.
  238. [stream writeFloat:fieldNum value:value];
  239. }
  240. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  241. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  242. #pragma unused(dataType) // For when asserts are off in release.
  243. return GPBComputeDoubleSize(fieldNum, value);
  244. }
  245. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
  246. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  247. #pragma unused(dataType) // For when asserts are off in release.
  248. [stream writeDouble:fieldNum value:value];
  249. }
  250. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  251. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  252. #pragma unused(dataType) // For when asserts are off in release.
  253. return GPBComputeStringSize(fieldNum, value);
  254. }
  255. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  256. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  257. #pragma unused(dataType) // For when asserts are off in release.
  258. [stream writeString:fieldNum value:value];
  259. }
  260. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  261. if (dataType == GPBDataTypeMessage) {
  262. return GPBComputeMessageSize(fieldNum, value);
  263. } else if (dataType == GPBDataTypeString) {
  264. return GPBComputeStringSize(fieldNum, value);
  265. } else if (dataType == GPBDataTypeBytes) {
  266. return GPBComputeBytesSize(fieldNum, value);
  267. } else {
  268. NSCAssert(NO, @"Unexpected type %d", dataType);
  269. return 0;
  270. }
  271. }
  272. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  273. if (dataType == GPBDataTypeMessage) {
  274. [stream writeMessage:fieldNum value:value];
  275. } else if (dataType == GPBDataTypeString) {
  276. [stream writeString:fieldNum value:value];
  277. } else if (dataType == GPBDataTypeBytes) {
  278. [stream writeBytes:fieldNum value:value];
  279. } else {
  280. NSCAssert(NO, @"Unexpected type %d", dataType);
  281. }
  282. }
  283. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  284. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  285. GPBDataType mapValueType = GPBGetFieldDataType(field);
  286. __block size_t result = 0;
  287. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  288. #pragma unused(stop)
  289. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  290. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  291. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  292. }];
  293. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  294. result += tagSize * dict.count;
  295. return result;
  296. }
  297. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  298. NSDictionary *dict,
  299. GPBFieldDescriptor *field) {
  300. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  301. GPBDataType mapValueType = GPBGetFieldDataType(field);
  302. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  303. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  304. #pragma unused(stop)
  305. // Write the tag.
  306. [outputStream writeInt32NoTag:tag];
  307. // Write the size of the message.
  308. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  309. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  310. // Write the size and fields.
  311. [outputStream writeInt32NoTag:(int32_t)msgSize];
  312. [outputStream writeString:kMapKeyFieldNumber value:key];
  313. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  314. }];
  315. }
  316. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  317. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  318. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  319. #pragma unused(field) // For when asserts are off in release.
  320. for (GPBMessage *msg in [dict objectEnumerator]) {
  321. if (!msg.initialized) {
  322. return NO;
  323. }
  324. }
  325. return YES;
  326. }
  327. // Note: if the type is an object, it the retain pass back to the caller.
  328. static void ReadValue(GPBCodedInputStream *stream,
  329. GPBGenericValue *valueToFill,
  330. GPBDataType type,
  331. GPBExtensionRegistry *registry,
  332. GPBFieldDescriptor *field) {
  333. switch (type) {
  334. case GPBDataTypeBool:
  335. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  336. break;
  337. case GPBDataTypeFixed32:
  338. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  339. break;
  340. case GPBDataTypeSFixed32:
  341. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  342. break;
  343. case GPBDataTypeFloat:
  344. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  345. break;
  346. case GPBDataTypeFixed64:
  347. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  348. break;
  349. case GPBDataTypeSFixed64:
  350. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  351. break;
  352. case GPBDataTypeDouble:
  353. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  354. break;
  355. case GPBDataTypeInt32:
  356. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  357. break;
  358. case GPBDataTypeInt64:
  359. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  360. break;
  361. case GPBDataTypeSInt32:
  362. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  363. break;
  364. case GPBDataTypeSInt64:
  365. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  366. break;
  367. case GPBDataTypeUInt32:
  368. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  369. break;
  370. case GPBDataTypeUInt64:
  371. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  372. break;
  373. case GPBDataTypeBytes:
  374. [valueToFill->valueData release];
  375. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  376. break;
  377. case GPBDataTypeString:
  378. [valueToFill->valueString release];
  379. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  380. break;
  381. case GPBDataTypeMessage: {
  382. GPBMessage *message = [[field.msgClass alloc] init];
  383. [stream readMessage:message extensionRegistry:registry];
  384. [valueToFill->valueMessage release];
  385. valueToFill->valueMessage = message;
  386. break;
  387. }
  388. case GPBDataTypeGroup:
  389. NSCAssert(NO, @"Can't happen");
  390. break;
  391. case GPBDataTypeEnum:
  392. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  393. break;
  394. }
  395. }
  396. void GPBDictionaryReadEntry(id mapDictionary,
  397. GPBCodedInputStream *stream,
  398. GPBExtensionRegistry *registry,
  399. GPBFieldDescriptor *field,
  400. GPBMessage *parentMessage) {
  401. GPBDataType keyDataType = field.mapKeyDataType;
  402. GPBDataType valueDataType = GPBGetFieldDataType(field);
  403. GPBGenericValue key;
  404. GPBGenericValue value;
  405. // Zero them (but pick up any enum default for proto2).
  406. key.valueString = value.valueString = nil;
  407. if (valueDataType == GPBDataTypeEnum) {
  408. value = field.defaultValue;
  409. }
  410. GPBCodedInputStreamState *state = &stream->state_;
  411. uint32_t keyTag =
  412. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  413. uint32_t valueTag =
  414. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  415. BOOL hitError = NO;
  416. while (YES) {
  417. uint32_t tag = GPBCodedInputStreamReadTag(state);
  418. if (tag == keyTag) {
  419. ReadValue(stream, &key, keyDataType, registry, field);
  420. } else if (tag == valueTag) {
  421. ReadValue(stream, &value, valueDataType, registry, field);
  422. } else if (tag == 0) {
  423. // zero signals EOF / limit reached
  424. break;
  425. } else { // Unknown
  426. if (![stream skipField:tag]){
  427. hitError = YES;
  428. break;
  429. }
  430. }
  431. }
  432. if (!hitError) {
  433. // Handle the special defaults and/or missing key/value.
  434. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  435. key.valueString = [@"" retain];
  436. }
  437. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  438. switch (valueDataType) {
  439. case GPBDataTypeString:
  440. value.valueString = [@"" retain];
  441. break;
  442. case GPBDataTypeBytes:
  443. value.valueData = [GPBEmptyNSData() retain];
  444. break;
  445. #if defined(__clang_analyzer__)
  446. case GPBDataTypeGroup:
  447. // Maps can't really have Groups as the value type, but this case is needed
  448. // so the analyzer won't report the posibility of send nil in for the value
  449. // in the NSMutableDictionary case below.
  450. #endif
  451. case GPBDataTypeMessage: {
  452. value.valueMessage = [[field.msgClass alloc] init];
  453. break;
  454. }
  455. default:
  456. // Nothing
  457. break;
  458. }
  459. }
  460. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  461. // mapDictionary is an NSMutableDictionary
  462. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  463. forKey:key.valueString];
  464. } else {
  465. if (valueDataType == GPBDataTypeEnum) {
  466. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  467. [field isValidEnumValue:value.valueEnum]) {
  468. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  469. } else {
  470. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  471. forKey:&key
  472. keyDataType:keyDataType];
  473. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  474. }
  475. } else {
  476. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  477. }
  478. }
  479. }
  480. if (GPBDataTypeIsObject(keyDataType)) {
  481. [key.valueString release];
  482. }
  483. if (GPBDataTypeIsObject(valueDataType)) {
  484. [value.valueString release];
  485. }
  486. }
  487. //
  488. // Macros for the common basic cases.
  489. //
  490. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  491. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  492. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  493. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  494. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  495. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  496. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  497. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  498. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  499. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  500. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  501. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  502. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  503. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, value)
  504. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  505. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, object)
  506. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME)
  507. //%#pragma mark - KEY_NAME -> VALUE_NAME
  508. //%
  509. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  510. //% @package
  511. //% NSMutableDictionary *_dictionary;
  512. //%}
  513. //%
  514. //%+ (instancetype)dictionary {
  515. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  516. //%}
  517. //%
  518. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)##VNAME
  519. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  520. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  521. //% // on to get the type correct.
  522. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  523. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  524. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  525. //%}
  526. //%
  527. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  528. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  529. //% ##VNAME$S## count:(NSUInteger)count {
  530. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  531. //% // on to get the type correct.
  532. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  533. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  534. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  535. //%}
  536. //%
  537. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  538. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  539. //% // on to get the type correct.
  540. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  541. //%}
  542. //%
  543. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  544. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  545. //%}
  546. //%
  547. //%- (instancetype)init {
  548. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  549. //%}
  550. //%
  551. //%- (instancetype)initWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  552. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  553. //% ##VNAME$S## count:(NSUInteger)count {
  554. //% self = [super init];
  555. //% if (self) {
  556. //% _dictionary = [[NSMutableDictionary alloc] init];
  557. //% if (count && VNAME##s && keys) {
  558. //% for (NSUInteger i = 0; i < count; ++i) {
  559. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
  560. //% }
  561. //% }
  562. //% }
  563. //% return self;
  564. //%}
  565. //%
  566. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  567. //% self = [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  568. //% if (self) {
  569. //% if (dictionary) {
  570. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  571. //% }
  572. //% }
  573. //% return self;
  574. //%}
  575. //%
  576. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  577. //% #pragma unused(numItems)
  578. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  579. //%}
  580. //%
  581. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  582. //%
  583. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  584. //%
  585. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  586. //%
  587. //%@end
  588. //%
  589. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  590. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  591. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  592. //%#pragma mark - KEY_NAME -> VALUE_NAME
  593. //%
  594. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  595. //% @package
  596. //% NSMutableDictionary *_dictionary;
  597. //% GPBEnumValidationFunc _validationFunc;
  598. //%}
  599. //%
  600. //%@synthesize validationFunc = _validationFunc;
  601. //%
  602. //%+ (instancetype)dictionary {
  603. //% return [[[self alloc] initWithValidationFunction:NULL
  604. //% rawValues:NULL
  605. //% forKeys:NULL
  606. //% count:0] autorelease];
  607. //%}
  608. //%
  609. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  610. //% return [[[self alloc] initWithValidationFunction:func
  611. //% rawValues:NULL
  612. //% forKeys:NULL
  613. //% count:0] autorelease];
  614. //%}
  615. //%
  616. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  617. //% rawValue:(VALUE_TYPE)rawValue
  618. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  619. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  620. //% // on to get the type correct.
  621. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  622. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  623. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  624. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  625. //%}
  626. //%
  627. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  628. //% rawValues:(const VALUE_TYPE [])rawValues
  629. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  630. //% count:(NSUInteger)count {
  631. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  632. //% // on to get the type correct.
  633. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  634. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  635. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  636. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  637. //%}
  638. //%
  639. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  640. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  641. //% // on to get the type correct.
  642. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  643. //%}
  644. //%
  645. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  646. //% capacity:(NSUInteger)numItems {
  647. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  648. //%}
  649. //%
  650. //%- (instancetype)init {
  651. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  652. //%}
  653. //%
  654. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  655. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  656. //%}
  657. //%
  658. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  659. //% rawValues:(const VALUE_TYPE [])rawValues
  660. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  661. //% count:(NSUInteger)count {
  662. //% self = [super init];
  663. //% if (self) {
  664. //% _dictionary = [[NSMutableDictionary alloc] init];
  665. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  666. //% if (count && rawValues && keys) {
  667. //% for (NSUInteger i = 0; i < count; ++i) {
  668. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  669. //% }
  670. //% }
  671. //% }
  672. //% return self;
  673. //%}
  674. //%
  675. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  676. //% self = [self initWithValidationFunction:dictionary.validationFunc
  677. //% rawValues:NULL
  678. //% forKeys:NULL
  679. //% count:0];
  680. //% if (self) {
  681. //% if (dictionary) {
  682. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  683. //% }
  684. //% }
  685. //% return self;
  686. //%}
  687. //%
  688. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  689. //% capacity:(NSUInteger)numItems {
  690. //% #pragma unused(numItems)
  691. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  692. //%}
  693. //%
  694. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  695. //%
  696. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  697. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  698. //% if (wrapped && value) {
  699. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  700. //% if (!_validationFunc(result)) {
  701. //% result = kGPBUnrecognizedEnumeratorValue;
  702. //% }
  703. //% *value = result;
  704. //% }
  705. //% return (wrapped != NULL);
  706. //%}
  707. //%
  708. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  709. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  710. //% if (wrapped && rawValue) {
  711. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  712. //% }
  713. //% return (wrapped != NULL);
  714. //%}
  715. //%
  716. //%- (void)enumerateKeysAndValuesUsingBlock:
  717. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  718. //% GPBEnumValidationFunc func = _validationFunc;
  719. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  720. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  721. //% BOOL *stop) {
  722. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  723. //% if (!func(unwrapped)) {
  724. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  725. //% }
  726. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  727. //% }];
  728. //%}
  729. //%
  730. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  731. //%
  732. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  733. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  734. //% [NSException raise:NSInvalidArgumentException
  735. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  736. //% value];
  737. //% }
  738. //%
  739. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  740. //% if (_autocreator) {
  741. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  742. //% }
  743. //%}
  744. //%
  745. //%@end
  746. //%
  747. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  748. //%- (void)dealloc {
  749. //% NSAssert(!_autocreator,
  750. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  751. //% [self class], _autocreator);
  752. //% [_dictionary release];
  753. //% [super dealloc];
  754. //%}
  755. //%
  756. //%- (instancetype)copyWithZone:(NSZone *)zone {
  757. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  758. //%}
  759. //%
  760. //%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
  761. //% if (self == other) {
  762. //% return YES;
  763. //% }
  764. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  765. //% return NO;
  766. //% }
  767. //% return [_dictionary isEqual:other->_dictionary];
  768. //%}
  769. //%
  770. //%- (NSUInteger)hash {
  771. //% return _dictionary.count;
  772. //%}
  773. //%
  774. //%- (NSString *)description {
  775. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  776. //%}
  777. //%
  778. //%- (NSUInteger)count {
  779. //% return _dictionary.count;
  780. //%}
  781. //%
  782. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:
  783. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop))block {
  784. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  785. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  786. //% BOOL *stop) {
  787. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME$u), stop);
  788. //% }];
  789. //%}
  790. //%
  791. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  792. //% NSUInteger count = _dictionary.count;
  793. //% if (count == 0) {
  794. //% return 0;
  795. //% }
  796. //%
  797. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  798. //% GPBDataType keyDataType = field.mapKeyDataType;
  799. //% __block size_t result = 0;
  800. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  801. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u##,
  802. //% BOOL *stop) {
  803. //% #pragma unused(stop)
  804. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  805. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  806. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  807. //% }];
  808. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  809. //% result += tagSize * count;
  810. //% return result;
  811. //%}
  812. //%
  813. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  814. //% asField:(GPBFieldDescriptor *)field {
  815. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  816. //% GPBDataType keyDataType = field.mapKeyDataType;
  817. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  818. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  819. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  820. //% BOOL *stop) {
  821. //% #pragma unused(stop)
  822. //% // Write the tag.
  823. //% [outputStream writeInt32NoTag:tag];
  824. //% // Write the size of the message.
  825. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  826. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  827. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  828. //% // Write the fields.
  829. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  830. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  831. //% }];
  832. //%}
  833. //%
  834. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  835. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  836. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  837. //%}
  838. //%
  839. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  840. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop) {
  841. //% #pragma unused(stop)
  842. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME));
  843. //% }];
  844. //%}
  845. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  846. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  847. //% if (otherDictionary) {
  848. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  849. //% if (_autocreator) {
  850. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  851. //% }
  852. //% }
  853. //%}
  854. //%
  855. //%- (void)set##ACCESSOR_NAME##VNAME$u##:(VALUE_TYPE)VNAME forKey:(KEY_TYPE##KisP$S##KisP)key {
  856. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)];
  857. //% if (_autocreator) {
  858. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  859. //% }
  860. //%}
  861. //%
  862. //%- (void)remove##VNAME$u##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  863. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  864. //%}
  865. //%
  866. //%- (void)removeAll {
  867. //% [_dictionary removeAllObjects];
  868. //%}
  869. //
  870. // Custom Generation for Bool keys
  871. //
  872. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  873. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, value)
  874. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  875. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, object)
  876. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME)
  877. //%#pragma mark - Bool -> VALUE_NAME
  878. //%
  879. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  880. //% @package
  881. //% VALUE_TYPE _values[2];
  882. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  883. //%
  884. //%+ (instancetype)dictionary {
  885. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  886. //%}
  887. //%
  888. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)VNAME
  889. //% ##VNAME$S## forKey:(BOOL)key {
  890. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  891. //% // on to get the type correct.
  892. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  893. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  894. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  895. //%}
  896. //%
  897. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  898. //% ##VNAME$S## forKeys:(const BOOL [])keys
  899. //% ##VNAME$S## count:(NSUInteger)count {
  900. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  901. //% // on to get the type correct.
  902. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  903. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  904. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  905. //%}
  906. //%
  907. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  908. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  909. //% // on to get the type correct.
  910. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  911. //%}
  912. //%
  913. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  914. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  915. //%}
  916. //%
  917. //%- (instancetype)init {
  918. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  919. //%}
  920. //%
  921. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  922. //%
  923. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  924. //% #pragma unused(numItems)
  925. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  926. //%}
  927. //%
  928. //%BOOL_DICT_DEALLOC##HELPER()
  929. //%
  930. //%- (instancetype)copyWithZone:(NSZone *)zone {
  931. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  932. //%}
  933. //%
  934. //%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
  935. //% if (self == other) {
  936. //% return YES;
  937. //% }
  938. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  939. //% return NO;
  940. //% }
  941. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
  942. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
  943. //% return NO;
  944. //% }
  945. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
  946. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
  947. //% return NO;
  948. //% }
  949. //% return YES;
  950. //%}
  951. //%
  952. //%- (NSUInteger)hash {
  953. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  954. //%}
  955. //%
  956. //%- (NSString *)description {
  957. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  958. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  959. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  960. //% }
  961. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  962. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  963. //% }
  964. //% [result appendString:@" }"];
  965. //% return result;
  966. //%}
  967. //%
  968. //%- (NSUInteger)count {
  969. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  970. //%}
  971. //%
  972. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  973. //%
  974. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  975. //%
  976. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  977. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  978. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  979. //% }
  980. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  981. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  982. //% }
  983. //%}
  984. //%
  985. //%- (void)enumerateKeysAnd##VNAME$u##sUsingBlock:
  986. //% (void (^)(BOOL key, VALUE_TYPE VNAME, BOOL *stop))block {
  987. //% BOOL stop = NO;
  988. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  989. //% block(NO, _values[0], &stop);
  990. //% }
  991. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  992. //% block(YES, _values[1], &stop);
  993. //% }
  994. //%}
  995. //%
  996. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  997. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  998. //% NSUInteger count = 0;
  999. //% size_t result = 0;
  1000. //% for (int i = 0; i < 2; ++i) {
  1001. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1002. //% ++count;
  1003. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1004. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1005. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1006. //% }
  1007. //% }
  1008. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1009. //% result += tagSize * count;
  1010. //% return result;
  1011. //%}
  1012. //%
  1013. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1014. //% asField:(GPBFieldDescriptor *)field {
  1015. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1016. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1017. //% for (int i = 0; i < 2; ++i) {
  1018. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1019. //% // Write the tag.
  1020. //% [outputStream writeInt32NoTag:tag];
  1021. //% // Write the size of the message.
  1022. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1023. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1024. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1025. //% // Write the fields.
  1026. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1027. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1028. //% }
  1029. //% }
  1030. //%}
  1031. //%
  1032. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1033. //%
  1034. //%@end
  1035. //%
  1036. //
  1037. // Helpers for PODs
  1038. //
  1039. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1040. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  1041. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1042. //% if (wrapped && value) {
  1043. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1044. //% }
  1045. //% return (wrapped != NULL);
  1046. //%}
  1047. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1048. //%@(VALUE)
  1049. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1050. //%[VALUE unsignedIntValue]
  1051. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1052. //%[VALUE intValue]
  1053. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1054. //%[VALUE unsignedLongLongValue]
  1055. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1056. //%[VALUE longLongValue]
  1057. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1058. //%[VALUE boolValue]
  1059. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1060. //%[VALUE floatValue]
  1061. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1062. //%[VALUE doubleValue]
  1063. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1064. //%[VALUE intValue]
  1065. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1066. //%[NSString stringWithFormat:@"%u", VALUE]
  1067. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1068. //%[NSString stringWithFormat:@"%d", VALUE]
  1069. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1070. //%[NSString stringWithFormat:@"%llu", VALUE]
  1071. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1072. //%[NSString stringWithFormat:@"%lld", VALUE]
  1073. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1074. //%(VALUE ? @"true" : @"false")
  1075. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1076. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1077. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1078. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1079. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1080. //%@(VALUE)
  1081. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1082. //%NSNumber *
  1083. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1084. //%VAL1 != VAL2
  1085. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1086. // Empty
  1087. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1088. // Empty
  1089. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1090. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1091. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1092. // Empty
  1093. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1094. // Empty
  1095. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1096. // Empty
  1097. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1098. // Empty
  1099. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1100. // Empty
  1101. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1102. // Empty
  1103. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1104. // Empty
  1105. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1106. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1107. //% forKey:(GPBGenericValue *)key
  1108. //% keyDataType:(GPBDataType)keyDataType {
  1109. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1110. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1111. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1112. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1113. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1114. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1115. //% [outputStream release];
  1116. //% return data;
  1117. //%}
  1118. //%
  1119. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1120. //%value##VALUE_NAME
  1121. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1122. // Empty
  1123. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1124. // Empty
  1125. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1126. //% BOOL _valueSet[2];
  1127. //%
  1128. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1129. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1130. //% forKeys:(const BOOL [])keys
  1131. //% count:(NSUInteger)count {
  1132. //% self = [super init];
  1133. //% if (self) {
  1134. //% for (NSUInteger i = 0; i < count; ++i) {
  1135. //% int idx = keys[i] ? 1 : 0;
  1136. //% _values[idx] = values[i];
  1137. //% _valueSet[idx] = YES;
  1138. //% }
  1139. //% }
  1140. //% return self;
  1141. //%}
  1142. //%
  1143. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1144. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1145. //% if (self) {
  1146. //% if (dictionary) {
  1147. //% for (int i = 0; i < 2; ++i) {
  1148. //% if (dictionary->_valueSet[i]) {
  1149. //% _values[i] = dictionary->_values[i];
  1150. //% _valueSet[i] = YES;
  1151. //% }
  1152. //% }
  1153. //% }
  1154. //% }
  1155. //% return self;
  1156. //%}
  1157. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1158. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1159. //%- (void)dealloc {
  1160. //% NSAssert(!_autocreator,
  1161. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1162. //% [self class], _autocreator);
  1163. //% [super dealloc];
  1164. //%}
  1165. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1166. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1167. //%BOOL_DICT_HASPOD(IDX, REF)
  1168. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1169. //%REF##_valueSet[IDX]
  1170. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1171. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1172. //% int idx = (key ? 1 : 0);
  1173. //% if (_valueSet[idx]) {
  1174. //% if (value) {
  1175. //% *value = _values[idx];
  1176. //% }
  1177. //% return YES;
  1178. //% }
  1179. //% return NO;
  1180. //%}
  1181. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1182. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1183. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1184. //% int idx = (key->valueBool ? 1 : 0);
  1185. //% _values[idx] = value->value##VALUE_NAME;
  1186. //% _valueSet[idx] = YES;
  1187. //%}
  1188. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1189. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1190. //% if (otherDictionary) {
  1191. //% for (int i = 0; i < 2; ++i) {
  1192. //% if (otherDictionary->_valueSet[i]) {
  1193. //% _valueSet[i] = YES;
  1194. //% _values[i] = otherDictionary->_values[i];
  1195. //% }
  1196. //% }
  1197. //% if (_autocreator) {
  1198. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1199. //% }
  1200. //% }
  1201. //%}
  1202. //%
  1203. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1204. //% int idx = (key ? 1 : 0);
  1205. //% _values[idx] = value;
  1206. //% _valueSet[idx] = YES;
  1207. //% if (_autocreator) {
  1208. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1209. //% }
  1210. //%}
  1211. //%
  1212. //%- (void)removeValueForKey:(BOOL)aKey {
  1213. //% _valueSet[aKey ? 1 : 0] = NO;
  1214. //%}
  1215. //%
  1216. //%- (void)removeAll {
  1217. //% _valueSet[0] = NO;
  1218. //% _valueSet[1] = NO;
  1219. //%}
  1220. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1221. //%STR_FORMAT_##VALUE_NAME()
  1222. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1223. //%%u
  1224. //%PDDM-DEFINE STR_FORMAT_Int32()
  1225. //%%d
  1226. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1227. //%%llu
  1228. //%PDDM-DEFINE STR_FORMAT_Int64()
  1229. //%%lld
  1230. //%PDDM-DEFINE STR_FORMAT_Bool()
  1231. //%%d
  1232. //%PDDM-DEFINE STR_FORMAT_Float()
  1233. //%%f
  1234. //%PDDM-DEFINE STR_FORMAT_Double()
  1235. //%%lf
  1236. //
  1237. // Helpers for Objects
  1238. //
  1239. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1240. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1241. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1242. //% return result;
  1243. //%}
  1244. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1245. //%VALUE
  1246. //%PDDM-DEFINE UNWRAPString(VALUE)
  1247. //%VALUE
  1248. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1249. //%VALUE
  1250. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1251. //%VALUE
  1252. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1253. //%VALUE
  1254. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1255. //%ENUM_TYPEOBJECT_##TYPE()
  1256. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1257. //%NSString *
  1258. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1259. //%id ##
  1260. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1261. //%![VAL1 isEqual:VAL2]
  1262. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1263. //%- (BOOL)isInitialized {
  1264. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1265. //% if (!msg.initialized) {
  1266. //% return NO;
  1267. //% }
  1268. //% }
  1269. //% return YES;
  1270. //%}
  1271. //%
  1272. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1273. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1274. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1275. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1276. //% GPBMessage *msg,
  1277. //% BOOL *stop) {
  1278. //% #pragma unused(stop)
  1279. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1280. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1281. //% [copiedMsg release];
  1282. //% }];
  1283. //% return newDict;
  1284. //%}
  1285. //%
  1286. //%
  1287. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1288. //%- (BOOL)isInitialized {
  1289. //% if (_values[0] && ![_values[0] isInitialized]) {
  1290. //% return NO;
  1291. //% }
  1292. //% if (_values[1] && ![_values[1] isInitialized]) {
  1293. //% return NO;
  1294. //% }
  1295. //% return YES;
  1296. //%}
  1297. //%
  1298. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1299. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1300. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1301. //% for (int i = 0; i < 2; ++i) {
  1302. //% if (_values[i] != nil) {
  1303. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1304. //% }
  1305. //% }
  1306. //% return newDict;
  1307. //%}
  1308. //%
  1309. //%
  1310. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1311. // Empty
  1312. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1313. //%valueString
  1314. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1315. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1316. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1317. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1318. //%##EXTRA_INDENT$S## }
  1319. //%
  1320. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1321. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1322. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1323. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1324. //%##EXTRA_INDENT$S## }
  1325. //%
  1326. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1327. // Empty
  1328. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1329. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1330. //% forKeys:(const BOOL [])keys
  1331. //% count:(NSUInteger)count {
  1332. //% self = [super init];
  1333. //% if (self) {
  1334. //% for (NSUInteger i = 0; i < count; ++i) {
  1335. //% if (!objects[i]) {
  1336. //% [NSException raise:NSInvalidArgumentException
  1337. //% format:@"Attempting to add nil object to a Dictionary"];
  1338. //% }
  1339. //% int idx = keys[i] ? 1 : 0;
  1340. //% [_values[idx] release];
  1341. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1342. //% }
  1343. //% }
  1344. //% return self;
  1345. //%}
  1346. //%
  1347. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1348. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1349. //% if (self) {
  1350. //% if (dictionary) {
  1351. //% _values[0] = [dictionary->_values[0] retain];
  1352. //% _values[1] = [dictionary->_values[1] retain];
  1353. //% }
  1354. //% }
  1355. //% return self;
  1356. //%}
  1357. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1358. //%- (void)dealloc {
  1359. //% NSAssert(!_autocreator,
  1360. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1361. //% [self class], _autocreator);
  1362. //% [_values[0] release];
  1363. //% [_values[1] release];
  1364. //% [super dealloc];
  1365. //%}
  1366. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1367. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1368. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1369. //%REF##_values[IDX] != nil
  1370. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1371. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1372. //% return _values[key ? 1 : 0];
  1373. //%}
  1374. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1375. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1376. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1377. //% int idx = (key->valueBool ? 1 : 0);
  1378. //% [_values[idx] release];
  1379. //% _values[idx] = [value->valueString retain];
  1380. //%}
  1381. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1382. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1383. //% if (otherDictionary) {
  1384. //% for (int i = 0; i < 2; ++i) {
  1385. //% if (otherDictionary->_values[i] != nil) {
  1386. //% [_values[i] release];
  1387. //% _values[i] = [otherDictionary->_values[i] retain];
  1388. //% }
  1389. //% }
  1390. //% if (_autocreator) {
  1391. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1392. //% }
  1393. //% }
  1394. //%}
  1395. //%
  1396. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1397. //% if (!object) {
  1398. //% [NSException raise:NSInvalidArgumentException
  1399. //% format:@"Attempting to add nil object to a Dictionary"];
  1400. //% }
  1401. //% int idx = (key ? 1 : 0);
  1402. //% [_values[idx] release];
  1403. //% _values[idx] = [object retain];
  1404. //% if (_autocreator) {
  1405. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1406. //% }
  1407. //%}
  1408. //%
  1409. //%- (void)removeObjectForKey:(BOOL)aKey {
  1410. //% int idx = (aKey ? 1 : 0);
  1411. //% [_values[idx] release];
  1412. //% _values[idx] = nil;
  1413. //%}
  1414. //%
  1415. //%- (void)removeAll {
  1416. //% for (int i = 0; i < 2; ++i) {
  1417. //% [_values[i] release];
  1418. //% _values[i] = nil;
  1419. //% }
  1420. //%}
  1421. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1422. //%%@
  1423. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1424. // This block of code is generated, do not edit it directly.
  1425. #pragma mark - UInt32 -> UInt32
  1426. @implementation GPBUInt32UInt32Dictionary {
  1427. @package
  1428. NSMutableDictionary *_dictionary;
  1429. }
  1430. + (instancetype)dictionary {
  1431. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1432. }
  1433. + (instancetype)dictionaryWithValue:(uint32_t)value
  1434. forKey:(uint32_t)key {
  1435. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1436. // on to get the type correct.
  1437. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1438. forKeys:&key
  1439. count:1] autorelease];
  1440. }
  1441. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1442. forKeys:(const uint32_t [])keys
  1443. count:(NSUInteger)count {
  1444. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1445. // on to get the type correct.
  1446. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1447. forKeys:keys
  1448. count:count] autorelease];
  1449. }
  1450. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1451. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1452. // on to get the type correct.
  1453. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1454. }
  1455. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1456. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1457. }
  1458. - (instancetype)init {
  1459. return [self initWithValues:NULL forKeys:NULL count:0];
  1460. }
  1461. - (instancetype)initWithValues:(const uint32_t [])values
  1462. forKeys:(const uint32_t [])keys
  1463. count:(NSUInteger)count {
  1464. self = [super init];
  1465. if (self) {
  1466. _dictionary = [[NSMutableDictionary alloc] init];
  1467. if (count && values && keys) {
  1468. for (NSUInteger i = 0; i < count; ++i) {
  1469. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1470. }
  1471. }
  1472. }
  1473. return self;
  1474. }
  1475. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1476. self = [self initWithValues:NULL forKeys:NULL count:0];
  1477. if (self) {
  1478. if (dictionary) {
  1479. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1480. }
  1481. }
  1482. return self;
  1483. }
  1484. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1485. #pragma unused(numItems)
  1486. return [self initWithValues:NULL forKeys:NULL count:0];
  1487. }
  1488. - (void)dealloc {
  1489. NSAssert(!_autocreator,
  1490. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1491. [self class], _autocreator);
  1492. [_dictionary release];
  1493. [super dealloc];
  1494. }
  1495. - (instancetype)copyWithZone:(NSZone *)zone {
  1496. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1497. }
  1498. - (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
  1499. if (self == other) {
  1500. return YES;
  1501. }
  1502. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1503. return NO;
  1504. }
  1505. return [_dictionary isEqual:other->_dictionary];
  1506. }
  1507. - (NSUInteger)hash {
  1508. return _dictionary.count;
  1509. }
  1510. - (NSString *)description {
  1511. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1512. }
  1513. - (NSUInteger)count {
  1514. return _dictionary.count;
  1515. }
  1516. - (void)enumerateKeysAndValuesUsingBlock:
  1517. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1518. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1519. NSNumber *aValue,
  1520. BOOL *stop) {
  1521. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1522. }];
  1523. }
  1524. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1525. NSUInteger count = _dictionary.count;
  1526. if (count == 0) {
  1527. return 0;
  1528. }
  1529. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1530. GPBDataType keyDataType = field.mapKeyDataType;
  1531. __block size_t result = 0;
  1532. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1533. NSNumber *aValue,
  1534. BOOL *stop) {
  1535. #pragma unused(stop)
  1536. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1537. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1538. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1539. }];
  1540. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1541. result += tagSize * count;
  1542. return result;
  1543. }
  1544. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1545. asField:(GPBFieldDescriptor *)field {
  1546. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1547. GPBDataType keyDataType = field.mapKeyDataType;
  1548. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1549. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1550. NSNumber *aValue,
  1551. BOOL *stop) {
  1552. #pragma unused(stop)
  1553. // Write the tag.
  1554. [outputStream writeInt32NoTag:tag];
  1555. // Write the size of the message.
  1556. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1557. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1558. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1559. // Write the fields.
  1560. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1561. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1562. }];
  1563. }
  1564. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1565. forGPBGenericValueKey:(GPBGenericValue *)key {
  1566. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1567. }
  1568. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1569. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1570. #pragma unused(stop)
  1571. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1572. }];
  1573. }
  1574. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1575. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1576. if (wrapped && value) {
  1577. *value = [wrapped unsignedIntValue];
  1578. }
  1579. return (wrapped != NULL);
  1580. }
  1581. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1582. if (otherDictionary) {
  1583. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1584. if (_autocreator) {
  1585. GPBAutocreatedDictionaryModified(_autocreator, self);
  1586. }
  1587. }
  1588. }
  1589. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1590. [_dictionary setObject:@(value) forKey:@(key)];
  1591. if (_autocreator) {
  1592. GPBAutocreatedDictionaryModified(_autocreator, self);
  1593. }
  1594. }
  1595. - (void)removeValueForKey:(uint32_t)aKey {
  1596. [_dictionary removeObjectForKey:@(aKey)];
  1597. }
  1598. - (void)removeAll {
  1599. [_dictionary removeAllObjects];
  1600. }
  1601. @end
  1602. #pragma mark - UInt32 -> Int32
  1603. @implementation GPBUInt32Int32Dictionary {
  1604. @package
  1605. NSMutableDictionary *_dictionary;
  1606. }
  1607. + (instancetype)dictionary {
  1608. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1609. }
  1610. + (instancetype)dictionaryWithValue:(int32_t)value
  1611. forKey:(uint32_t)key {
  1612. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1613. // on to get the type correct.
  1614. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1615. forKeys:&key
  1616. count:1] autorelease];
  1617. }
  1618. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1619. forKeys:(const uint32_t [])keys
  1620. count:(NSUInteger)count {
  1621. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1622. // on to get the type correct.
  1623. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1624. forKeys:keys
  1625. count:count] autorelease];
  1626. }
  1627. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1628. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1629. // on to get the type correct.
  1630. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1631. }
  1632. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1633. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1634. }
  1635. - (instancetype)init {
  1636. return [self initWithValues:NULL forKeys:NULL count:0];
  1637. }
  1638. - (instancetype)initWithValues:(const int32_t [])values
  1639. forKeys:(const uint32_t [])keys
  1640. count:(NSUInteger)count {
  1641. self = [super init];
  1642. if (self) {
  1643. _dictionary = [[NSMutableDictionary alloc] init];
  1644. if (count && values && keys) {
  1645. for (NSUInteger i = 0; i < count; ++i) {
  1646. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1647. }
  1648. }
  1649. }
  1650. return self;
  1651. }
  1652. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1653. self = [self initWithValues:NULL forKeys:NULL count:0];
  1654. if (self) {
  1655. if (dictionary) {
  1656. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1657. }
  1658. }
  1659. return self;
  1660. }
  1661. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1662. #pragma unused(numItems)
  1663. return [self initWithValues:NULL forKeys:NULL count:0];
  1664. }
  1665. - (void)dealloc {
  1666. NSAssert(!_autocreator,
  1667. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1668. [self class], _autocreator);
  1669. [_dictionary release];
  1670. [super dealloc];
  1671. }
  1672. - (instancetype)copyWithZone:(NSZone *)zone {
  1673. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1674. }
  1675. - (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
  1676. if (self == other) {
  1677. return YES;
  1678. }
  1679. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1680. return NO;
  1681. }
  1682. return [_dictionary isEqual:other->_dictionary];
  1683. }
  1684. - (NSUInteger)hash {
  1685. return _dictionary.count;
  1686. }
  1687. - (NSString *)description {
  1688. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1689. }
  1690. - (NSUInteger)count {
  1691. return _dictionary.count;
  1692. }
  1693. - (void)enumerateKeysAndValuesUsingBlock:
  1694. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1695. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1696. NSNumber *aValue,
  1697. BOOL *stop) {
  1698. block([aKey unsignedIntValue], [aValue intValue], stop);
  1699. }];
  1700. }
  1701. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1702. NSUInteger count = _dictionary.count;
  1703. if (count == 0) {
  1704. return 0;
  1705. }
  1706. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1707. GPBDataType keyDataType = field.mapKeyDataType;
  1708. __block size_t result = 0;
  1709. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1710. NSNumber *aValue,
  1711. BOOL *stop) {
  1712. #pragma unused(stop)
  1713. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1714. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1715. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1716. }];
  1717. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1718. result += tagSize * count;
  1719. return result;
  1720. }
  1721. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1722. asField:(GPBFieldDescriptor *)field {
  1723. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1724. GPBDataType keyDataType = field.mapKeyDataType;
  1725. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1726. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1727. NSNumber *aValue,
  1728. BOOL *stop) {
  1729. #pragma unused(stop)
  1730. // Write the tag.
  1731. [outputStream writeInt32NoTag:tag];
  1732. // Write the size of the message.
  1733. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1734. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1735. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1736. // Write the fields.
  1737. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1738. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1739. }];
  1740. }
  1741. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1742. forGPBGenericValueKey:(GPBGenericValue *)key {
  1743. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1744. }
  1745. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1746. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1747. #pragma unused(stop)
  1748. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1749. }];
  1750. }
  1751. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1752. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1753. if (wrapped && value) {
  1754. *value = [wrapped intValue];
  1755. }
  1756. return (wrapped != NULL);
  1757. }
  1758. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1759. if (otherDictionary) {
  1760. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1761. if (_autocreator) {
  1762. GPBAutocreatedDictionaryModified(_autocreator, self);
  1763. }
  1764. }
  1765. }
  1766. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1767. [_dictionary setObject:@(value) forKey:@(key)];
  1768. if (_autocreator) {
  1769. GPBAutocreatedDictionaryModified(_autocreator, self);
  1770. }
  1771. }
  1772. - (void)removeValueForKey:(uint32_t)aKey {
  1773. [_dictionary removeObjectForKey:@(aKey)];
  1774. }
  1775. - (void)removeAll {
  1776. [_dictionary removeAllObjects];
  1777. }
  1778. @end
  1779. #pragma mark - UInt32 -> UInt64
  1780. @implementation GPBUInt32UInt64Dictionary {
  1781. @package
  1782. NSMutableDictionary *_dictionary;
  1783. }
  1784. + (instancetype)dictionary {
  1785. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1786. }
  1787. + (instancetype)dictionaryWithValue:(uint64_t)value
  1788. forKey:(uint32_t)key {
  1789. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1790. // on to get the type correct.
  1791. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1792. forKeys:&key
  1793. count:1] autorelease];
  1794. }
  1795. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1796. forKeys:(const uint32_t [])keys
  1797. count:(NSUInteger)count {
  1798. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1799. // on to get the type correct.
  1800. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1801. forKeys:keys
  1802. count:count] autorelease];
  1803. }
  1804. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1805. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1806. // on to get the type correct.
  1807. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1808. }
  1809. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1810. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1811. }
  1812. - (instancetype)init {
  1813. return [self initWithValues:NULL forKeys:NULL count:0];
  1814. }
  1815. - (instancetype)initWithValues:(const uint64_t [])values
  1816. forKeys:(const uint32_t [])keys
  1817. count:(NSUInteger)count {
  1818. self = [super init];
  1819. if (self) {
  1820. _dictionary = [[NSMutableDictionary alloc] init];
  1821. if (count && values && keys) {
  1822. for (NSUInteger i = 0; i < count; ++i) {
  1823. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1824. }
  1825. }
  1826. }
  1827. return self;
  1828. }
  1829. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1830. self = [self initWithValues:NULL forKeys:NULL count:0];
  1831. if (self) {
  1832. if (dictionary) {
  1833. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1834. }
  1835. }
  1836. return self;
  1837. }
  1838. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1839. #pragma unused(numItems)
  1840. return [self initWithValues:NULL forKeys:NULL count:0];
  1841. }
  1842. - (void)dealloc {
  1843. NSAssert(!_autocreator,
  1844. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1845. [self class], _autocreator);
  1846. [_dictionary release];
  1847. [super dealloc];
  1848. }
  1849. - (instancetype)copyWithZone:(NSZone *)zone {
  1850. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1851. }
  1852. - (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
  1853. if (self == other) {
  1854. return YES;
  1855. }
  1856. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1857. return NO;
  1858. }
  1859. return [_dictionary isEqual:other->_dictionary];
  1860. }
  1861. - (NSUInteger)hash {
  1862. return _dictionary.count;
  1863. }
  1864. - (NSString *)description {
  1865. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1866. }
  1867. - (NSUInteger)count {
  1868. return _dictionary.count;
  1869. }
  1870. - (void)enumerateKeysAndValuesUsingBlock:
  1871. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1872. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1873. NSNumber *aValue,
  1874. BOOL *stop) {
  1875. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1876. }];
  1877. }
  1878. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1879. NSUInteger count = _dictionary.count;
  1880. if (count == 0) {
  1881. return 0;
  1882. }
  1883. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1884. GPBDataType keyDataType = field.mapKeyDataType;
  1885. __block size_t result = 0;
  1886. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1887. NSNumber *aValue,
  1888. BOOL *stop) {
  1889. #pragma unused(stop)
  1890. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1891. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1892. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1893. }];
  1894. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1895. result += tagSize * count;
  1896. return result;
  1897. }
  1898. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1899. asField:(GPBFieldDescriptor *)field {
  1900. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1901. GPBDataType keyDataType = field.mapKeyDataType;
  1902. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1903. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1904. NSNumber *aValue,
  1905. BOOL *stop) {
  1906. #pragma unused(stop)
  1907. // Write the tag.
  1908. [outputStream writeInt32NoTag:tag];
  1909. // Write the size of the message.
  1910. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1911. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1912. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1913. // Write the fields.
  1914. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1915. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1916. }];
  1917. }
  1918. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1919. forGPBGenericValueKey:(GPBGenericValue *)key {
  1920. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1921. }
  1922. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1923. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1924. #pragma unused(stop)
  1925. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1926. }];
  1927. }
  1928. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1929. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1930. if (wrapped && value) {
  1931. *value = [wrapped unsignedLongLongValue];
  1932. }
  1933. return (wrapped != NULL);
  1934. }
  1935. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1936. if (otherDictionary) {
  1937. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1938. if (_autocreator) {
  1939. GPBAutocreatedDictionaryModified(_autocreator, self);
  1940. }
  1941. }
  1942. }
  1943. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1944. [_dictionary setObject:@(value) forKey:@(key)];
  1945. if (_autocreator) {
  1946. GPBAutocreatedDictionaryModified(_autocreator, self);
  1947. }
  1948. }
  1949. - (void)removeValueForKey:(uint32_t)aKey {
  1950. [_dictionary removeObjectForKey:@(aKey)];
  1951. }
  1952. - (void)removeAll {
  1953. [_dictionary removeAllObjects];
  1954. }
  1955. @end
  1956. #pragma mark - UInt32 -> Int64
  1957. @implementation GPBUInt32Int64Dictionary {
  1958. @package
  1959. NSMutableDictionary *_dictionary;
  1960. }
  1961. + (instancetype)dictionary {
  1962. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1963. }
  1964. + (instancetype)dictionaryWithValue:(int64_t)value
  1965. forKey:(uint32_t)key {
  1966. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1967. // on to get the type correct.
  1968. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  1969. forKeys:&key
  1970. count:1] autorelease];
  1971. }
  1972. + (instancetype)dictionaryWithValues:(const int64_t [])values
  1973. forKeys:(const uint32_t [])keys
  1974. count:(NSUInteger)count {
  1975. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1976. // on to get the type correct.
  1977. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  1978. forKeys:keys
  1979. count:count] autorelease];
  1980. }
  1981. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1982. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1983. // on to get the type correct.
  1984. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1985. }
  1986. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1987. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1988. }
  1989. - (instancetype)init {
  1990. return [self initWithValues:NULL forKeys:NULL count:0];
  1991. }
  1992. - (instancetype)initWithValues:(const int64_t [])values
  1993. forKeys:(const uint32_t [])keys
  1994. count:(NSUInteger)count {
  1995. self = [super init];
  1996. if (self) {
  1997. _dictionary = [[NSMutableDictionary alloc] init];
  1998. if (count && values && keys) {
  1999. for (NSUInteger i = 0; i < count; ++i) {
  2000. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2001. }
  2002. }
  2003. }
  2004. return self;
  2005. }
  2006. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2007. self = [self initWithValues:NULL forKeys:NULL count:0];
  2008. if (self) {
  2009. if (dictionary) {
  2010. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2011. }
  2012. }
  2013. return self;
  2014. }
  2015. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2016. #pragma unused(numItems)
  2017. return [self initWithValues:NULL forKeys:NULL count:0];
  2018. }
  2019. - (void)dealloc {
  2020. NSAssert(!_autocreator,
  2021. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2022. [self class], _autocreator);
  2023. [_dictionary release];
  2024. [super dealloc];
  2025. }
  2026. - (instancetype)copyWithZone:(NSZone *)zone {
  2027. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2028. }
  2029. - (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
  2030. if (self == other) {
  2031. return YES;
  2032. }
  2033. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2034. return NO;
  2035. }
  2036. return [_dictionary isEqual:other->_dictionary];
  2037. }
  2038. - (NSUInteger)hash {
  2039. return _dictionary.count;
  2040. }
  2041. - (NSString *)description {
  2042. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2043. }
  2044. - (NSUInteger)count {
  2045. return _dictionary.count;
  2046. }
  2047. - (void)enumerateKeysAndValuesUsingBlock:
  2048. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2049. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2050. NSNumber *aValue,
  2051. BOOL *stop) {
  2052. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2053. }];
  2054. }
  2055. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2056. NSUInteger count = _dictionary.count;
  2057. if (count == 0) {
  2058. return 0;
  2059. }
  2060. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2061. GPBDataType keyDataType = field.mapKeyDataType;
  2062. __block size_t result = 0;
  2063. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2064. NSNumber *aValue,
  2065. BOOL *stop) {
  2066. #pragma unused(stop)
  2067. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2068. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2069. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2070. }];
  2071. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2072. result += tagSize * count;
  2073. return result;
  2074. }
  2075. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2076. asField:(GPBFieldDescriptor *)field {
  2077. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2078. GPBDataType keyDataType = field.mapKeyDataType;
  2079. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2080. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2081. NSNumber *aValue,
  2082. BOOL *stop) {
  2083. #pragma unused(stop)
  2084. // Write the tag.
  2085. [outputStream writeInt32NoTag:tag];
  2086. // Write the size of the message.
  2087. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2088. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2089. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2090. // Write the fields.
  2091. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2092. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2093. }];
  2094. }
  2095. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2096. forGPBGenericValueKey:(GPBGenericValue *)key {
  2097. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2098. }
  2099. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2100. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2101. #pragma unused(stop)
  2102. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2103. }];
  2104. }
  2105. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  2106. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2107. if (wrapped && value) {
  2108. *value = [wrapped longLongValue];
  2109. }
  2110. return (wrapped != NULL);
  2111. }
  2112. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2113. if (otherDictionary) {
  2114. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2115. if (_autocreator) {
  2116. GPBAutocreatedDictionaryModified(_autocreator, self);
  2117. }
  2118. }
  2119. }
  2120. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  2121. [_dictionary setObject:@(value) forKey:@(key)];
  2122. if (_autocreator) {
  2123. GPBAutocreatedDictionaryModified(_autocreator, self);
  2124. }
  2125. }
  2126. - (void)removeValueForKey:(uint32_t)aKey {
  2127. [_dictionary removeObjectForKey:@(aKey)];
  2128. }
  2129. - (void)removeAll {
  2130. [_dictionary removeAllObjects];
  2131. }
  2132. @end
  2133. #pragma mark - UInt32 -> Bool
  2134. @implementation GPBUInt32BoolDictionary {
  2135. @package
  2136. NSMutableDictionary *_dictionary;
  2137. }
  2138. + (instancetype)dictionary {
  2139. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2140. }
  2141. + (instancetype)dictionaryWithValue:(BOOL)value
  2142. forKey:(uint32_t)key {
  2143. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2144. // on to get the type correct.
  2145. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2146. forKeys:&key
  2147. count:1] autorelease];
  2148. }
  2149. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2150. forKeys:(const uint32_t [])keys
  2151. count:(NSUInteger)count {
  2152. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2153. // on to get the type correct.
  2154. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2155. forKeys:keys
  2156. count:count] autorelease];
  2157. }
  2158. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2159. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2160. // on to get the type correct.
  2161. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2162. }
  2163. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2164. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2165. }
  2166. - (instancetype)init {
  2167. return [self initWithValues:NULL forKeys:NULL count:0];
  2168. }
  2169. - (instancetype)initWithValues:(const BOOL [])values
  2170. forKeys:(const uint32_t [])keys
  2171. count:(NSUInteger)count {
  2172. self = [super init];
  2173. if (self) {
  2174. _dictionary = [[NSMutableDictionary alloc] init];
  2175. if (count && values && keys) {
  2176. for (NSUInteger i = 0; i < count; ++i) {
  2177. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2178. }
  2179. }
  2180. }
  2181. return self;
  2182. }
  2183. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2184. self = [self initWithValues:NULL forKeys:NULL count:0];
  2185. if (self) {
  2186. if (dictionary) {
  2187. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2188. }
  2189. }
  2190. return self;
  2191. }
  2192. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2193. #pragma unused(numItems)
  2194. return [self initWithValues:NULL forKeys:NULL count:0];
  2195. }
  2196. - (void)dealloc {
  2197. NSAssert(!_autocreator,
  2198. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2199. [self class], _autocreator);
  2200. [_dictionary release];
  2201. [super dealloc];
  2202. }
  2203. - (instancetype)copyWithZone:(NSZone *)zone {
  2204. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2205. }
  2206. - (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
  2207. if (self == other) {
  2208. return YES;
  2209. }
  2210. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2211. return NO;
  2212. }
  2213. return [_dictionary isEqual:other->_dictionary];
  2214. }
  2215. - (NSUInteger)hash {
  2216. return _dictionary.count;
  2217. }
  2218. - (NSString *)description {
  2219. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2220. }
  2221. - (NSUInteger)count {
  2222. return _dictionary.count;
  2223. }
  2224. - (void)enumerateKeysAndValuesUsingBlock:
  2225. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2226. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2227. NSNumber *aValue,
  2228. BOOL *stop) {
  2229. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2230. }];
  2231. }
  2232. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2233. NSUInteger count = _dictionary.count;
  2234. if (count == 0) {
  2235. return 0;
  2236. }
  2237. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2238. GPBDataType keyDataType = field.mapKeyDataType;
  2239. __block size_t result = 0;
  2240. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2241. NSNumber *aValue,
  2242. BOOL *stop) {
  2243. #pragma unused(stop)
  2244. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2245. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2246. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2247. }];
  2248. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2249. result += tagSize * count;
  2250. return result;
  2251. }
  2252. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2253. asField:(GPBFieldDescriptor *)field {
  2254. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2255. GPBDataType keyDataType = field.mapKeyDataType;
  2256. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2258. NSNumber *aValue,
  2259. BOOL *stop) {
  2260. #pragma unused(stop)
  2261. // Write the tag.
  2262. [outputStream writeInt32NoTag:tag];
  2263. // Write the size of the message.
  2264. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2265. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2266. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2267. // Write the fields.
  2268. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2269. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2270. }];
  2271. }
  2272. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2273. forGPBGenericValueKey:(GPBGenericValue *)key {
  2274. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2275. }
  2276. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2277. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2278. #pragma unused(stop)
  2279. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2280. }];
  2281. }
  2282. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2283. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2284. if (wrapped && value) {
  2285. *value = [wrapped boolValue];
  2286. }
  2287. return (wrapped != NULL);
  2288. }
  2289. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2290. if (otherDictionary) {
  2291. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2292. if (_autocreator) {
  2293. GPBAutocreatedDictionaryModified(_autocreator, self);
  2294. }
  2295. }
  2296. }
  2297. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2298. [_dictionary setObject:@(value) forKey:@(key)];
  2299. if (_autocreator) {
  2300. GPBAutocreatedDictionaryModified(_autocreator, self);
  2301. }
  2302. }
  2303. - (void)removeValueForKey:(uint32_t)aKey {
  2304. [_dictionary removeObjectForKey:@(aKey)];
  2305. }
  2306. - (void)removeAll {
  2307. [_dictionary removeAllObjects];
  2308. }
  2309. @end
  2310. #pragma mark - UInt32 -> Float
  2311. @implementation GPBUInt32FloatDictionary {
  2312. @package
  2313. NSMutableDictionary *_dictionary;
  2314. }
  2315. + (instancetype)dictionary {
  2316. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2317. }
  2318. + (instancetype)dictionaryWithValue:(float)value
  2319. forKey:(uint32_t)key {
  2320. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2321. // on to get the type correct.
  2322. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2323. forKeys:&key
  2324. count:1] autorelease];
  2325. }
  2326. + (instancetype)dictionaryWithValues:(const float [])values
  2327. forKeys:(const uint32_t [])keys
  2328. count:(NSUInteger)count {
  2329. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2330. // on to get the type correct.
  2331. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2332. forKeys:keys
  2333. count:count] autorelease];
  2334. }
  2335. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2336. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2337. // on to get the type correct.
  2338. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2339. }
  2340. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2341. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2342. }
  2343. - (instancetype)init {
  2344. return [self initWithValues:NULL forKeys:NULL count:0];
  2345. }
  2346. - (instancetype)initWithValues:(const float [])values
  2347. forKeys:(const uint32_t [])keys
  2348. count:(NSUInteger)count {
  2349. self = [super init];
  2350. if (self) {
  2351. _dictionary = [[NSMutableDictionary alloc] init];
  2352. if (count && values && keys) {
  2353. for (NSUInteger i = 0; i < count; ++i) {
  2354. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2355. }
  2356. }
  2357. }
  2358. return self;
  2359. }
  2360. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2361. self = [self initWithValues:NULL forKeys:NULL count:0];
  2362. if (self) {
  2363. if (dictionary) {
  2364. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2365. }
  2366. }
  2367. return self;
  2368. }
  2369. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2370. #pragma unused(numItems)
  2371. return [self initWithValues:NULL forKeys:NULL count:0];
  2372. }
  2373. - (void)dealloc {
  2374. NSAssert(!_autocreator,
  2375. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2376. [self class], _autocreator);
  2377. [_dictionary release];
  2378. [super dealloc];
  2379. }
  2380. - (instancetype)copyWithZone:(NSZone *)zone {
  2381. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2382. }
  2383. - (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
  2384. if (self == other) {
  2385. return YES;
  2386. }
  2387. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2388. return NO;
  2389. }
  2390. return [_dictionary isEqual:other->_dictionary];
  2391. }
  2392. - (NSUInteger)hash {
  2393. return _dictionary.count;
  2394. }
  2395. - (NSString *)description {
  2396. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2397. }
  2398. - (NSUInteger)count {
  2399. return _dictionary.count;
  2400. }
  2401. - (void)enumerateKeysAndValuesUsingBlock:
  2402. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2403. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2404. NSNumber *aValue,
  2405. BOOL *stop) {
  2406. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2407. }];
  2408. }
  2409. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2410. NSUInteger count = _dictionary.count;
  2411. if (count == 0) {
  2412. return 0;
  2413. }
  2414. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2415. GPBDataType keyDataType = field.mapKeyDataType;
  2416. __block size_t result = 0;
  2417. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2418. NSNumber *aValue,
  2419. BOOL *stop) {
  2420. #pragma unused(stop)
  2421. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2422. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2423. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2424. }];
  2425. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2426. result += tagSize * count;
  2427. return result;
  2428. }
  2429. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2430. asField:(GPBFieldDescriptor *)field {
  2431. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2432. GPBDataType keyDataType = field.mapKeyDataType;
  2433. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2434. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2435. NSNumber *aValue,
  2436. BOOL *stop) {
  2437. #pragma unused(stop)
  2438. // Write the tag.
  2439. [outputStream writeInt32NoTag:tag];
  2440. // Write the size of the message.
  2441. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2442. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2443. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2444. // Write the fields.
  2445. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2446. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2447. }];
  2448. }
  2449. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2450. forGPBGenericValueKey:(GPBGenericValue *)key {
  2451. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2452. }
  2453. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2454. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2455. #pragma unused(stop)
  2456. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2457. }];
  2458. }
  2459. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2460. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2461. if (wrapped && value) {
  2462. *value = [wrapped floatValue];
  2463. }
  2464. return (wrapped != NULL);
  2465. }
  2466. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2467. if (otherDictionary) {
  2468. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2469. if (_autocreator) {
  2470. GPBAutocreatedDictionaryModified(_autocreator, self);
  2471. }
  2472. }
  2473. }
  2474. - (void)setValue:(float)value forKey:(uint32_t)key {
  2475. [_dictionary setObject:@(value) forKey:@(key)];
  2476. if (_autocreator) {
  2477. GPBAutocreatedDictionaryModified(_autocreator, self);
  2478. }
  2479. }
  2480. - (void)removeValueForKey:(uint32_t)aKey {
  2481. [_dictionary removeObjectForKey:@(aKey)];
  2482. }
  2483. - (void)removeAll {
  2484. [_dictionary removeAllObjects];
  2485. }
  2486. @end
  2487. #pragma mark - UInt32 -> Double
  2488. @implementation GPBUInt32DoubleDictionary {
  2489. @package
  2490. NSMutableDictionary *_dictionary;
  2491. }
  2492. + (instancetype)dictionary {
  2493. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2494. }
  2495. + (instancetype)dictionaryWithValue:(double)value
  2496. forKey:(uint32_t)key {
  2497. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2498. // on to get the type correct.
  2499. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2500. forKeys:&key
  2501. count:1] autorelease];
  2502. }
  2503. + (instancetype)dictionaryWithValues:(const double [])values
  2504. forKeys:(const uint32_t [])keys
  2505. count:(NSUInteger)count {
  2506. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2507. // on to get the type correct.
  2508. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2509. forKeys:keys
  2510. count:count] autorelease];
  2511. }
  2512. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2513. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2514. // on to get the type correct.
  2515. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2516. }
  2517. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2518. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2519. }
  2520. - (instancetype)init {
  2521. return [self initWithValues:NULL forKeys:NULL count:0];
  2522. }
  2523. - (instancetype)initWithValues:(const double [])values
  2524. forKeys:(const uint32_t [])keys
  2525. count:(NSUInteger)count {
  2526. self = [super init];
  2527. if (self) {
  2528. _dictionary = [[NSMutableDictionary alloc] init];
  2529. if (count && values && keys) {
  2530. for (NSUInteger i = 0; i < count; ++i) {
  2531. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2532. }
  2533. }
  2534. }
  2535. return self;
  2536. }
  2537. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2538. self = [self initWithValues:NULL forKeys:NULL count:0];
  2539. if (self) {
  2540. if (dictionary) {
  2541. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2542. }
  2543. }
  2544. return self;
  2545. }
  2546. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2547. #pragma unused(numItems)
  2548. return [self initWithValues:NULL forKeys:NULL count:0];
  2549. }
  2550. - (void)dealloc {
  2551. NSAssert(!_autocreator,
  2552. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2553. [self class], _autocreator);
  2554. [_dictionary release];
  2555. [super dealloc];
  2556. }
  2557. - (instancetype)copyWithZone:(NSZone *)zone {
  2558. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2559. }
  2560. - (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
  2561. if (self == other) {
  2562. return YES;
  2563. }
  2564. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2565. return NO;
  2566. }
  2567. return [_dictionary isEqual:other->_dictionary];
  2568. }
  2569. - (NSUInteger)hash {
  2570. return _dictionary.count;
  2571. }
  2572. - (NSString *)description {
  2573. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2574. }
  2575. - (NSUInteger)count {
  2576. return _dictionary.count;
  2577. }
  2578. - (void)enumerateKeysAndValuesUsingBlock:
  2579. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2580. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2581. NSNumber *aValue,
  2582. BOOL *stop) {
  2583. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2584. }];
  2585. }
  2586. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2587. NSUInteger count = _dictionary.count;
  2588. if (count == 0) {
  2589. return 0;
  2590. }
  2591. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2592. GPBDataType keyDataType = field.mapKeyDataType;
  2593. __block size_t result = 0;
  2594. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2595. NSNumber *aValue,
  2596. BOOL *stop) {
  2597. #pragma unused(stop)
  2598. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2599. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2600. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2601. }];
  2602. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2603. result += tagSize * count;
  2604. return result;
  2605. }
  2606. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2607. asField:(GPBFieldDescriptor *)field {
  2608. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2609. GPBDataType keyDataType = field.mapKeyDataType;
  2610. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2611. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2612. NSNumber *aValue,
  2613. BOOL *stop) {
  2614. #pragma unused(stop)
  2615. // Write the tag.
  2616. [outputStream writeInt32NoTag:tag];
  2617. // Write the size of the message.
  2618. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2619. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2620. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2621. // Write the fields.
  2622. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2623. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2624. }];
  2625. }
  2626. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2627. forGPBGenericValueKey:(GPBGenericValue *)key {
  2628. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2629. }
  2630. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2631. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2632. #pragma unused(stop)
  2633. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2634. }];
  2635. }
  2636. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2637. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2638. if (wrapped && value) {
  2639. *value = [wrapped doubleValue];
  2640. }
  2641. return (wrapped != NULL);
  2642. }
  2643. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2644. if (otherDictionary) {
  2645. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2646. if (_autocreator) {
  2647. GPBAutocreatedDictionaryModified(_autocreator, self);
  2648. }
  2649. }
  2650. }
  2651. - (void)setValue:(double)value forKey:(uint32_t)key {
  2652. [_dictionary setObject:@(value) forKey:@(key)];
  2653. if (_autocreator) {
  2654. GPBAutocreatedDictionaryModified(_autocreator, self);
  2655. }
  2656. }
  2657. - (void)removeValueForKey:(uint32_t)aKey {
  2658. [_dictionary removeObjectForKey:@(aKey)];
  2659. }
  2660. - (void)removeAll {
  2661. [_dictionary removeAllObjects];
  2662. }
  2663. @end
  2664. #pragma mark - UInt32 -> Enum
  2665. @implementation GPBUInt32EnumDictionary {
  2666. @package
  2667. NSMutableDictionary *_dictionary;
  2668. GPBEnumValidationFunc _validationFunc;
  2669. }
  2670. @synthesize validationFunc = _validationFunc;
  2671. + (instancetype)dictionary {
  2672. return [[[self alloc] initWithValidationFunction:NULL
  2673. rawValues:NULL
  2674. forKeys:NULL
  2675. count:0] autorelease];
  2676. }
  2677. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2678. return [[[self alloc] initWithValidationFunction:func
  2679. rawValues:NULL
  2680. forKeys:NULL
  2681. count:0] autorelease];
  2682. }
  2683. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2684. rawValue:(int32_t)rawValue
  2685. forKey:(uint32_t)key {
  2686. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2687. // on to get the type correct.
  2688. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2689. rawValues:&rawValue
  2690. forKeys:&key
  2691. count:1] autorelease];
  2692. }
  2693. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2694. rawValues:(const int32_t [])rawValues
  2695. forKeys:(const uint32_t [])keys
  2696. count:(NSUInteger)count {
  2697. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2698. // on to get the type correct.
  2699. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2700. rawValues:rawValues
  2701. forKeys:keys
  2702. count:count] autorelease];
  2703. }
  2704. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2705. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2706. // on to get the type correct.
  2707. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2708. }
  2709. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2710. capacity:(NSUInteger)numItems {
  2711. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2712. }
  2713. - (instancetype)init {
  2714. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2715. }
  2716. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2717. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2718. }
  2719. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2720. rawValues:(const int32_t [])rawValues
  2721. forKeys:(const uint32_t [])keys
  2722. count:(NSUInteger)count {
  2723. self = [super init];
  2724. if (self) {
  2725. _dictionary = [[NSMutableDictionary alloc] init];
  2726. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2727. if (count && rawValues && keys) {
  2728. for (NSUInteger i = 0; i < count; ++i) {
  2729. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2730. }
  2731. }
  2732. }
  2733. return self;
  2734. }
  2735. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2736. self = [self initWithValidationFunction:dictionary.validationFunc
  2737. rawValues:NULL
  2738. forKeys:NULL
  2739. count:0];
  2740. if (self) {
  2741. if (dictionary) {
  2742. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2743. }
  2744. }
  2745. return self;
  2746. }
  2747. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2748. capacity:(NSUInteger)numItems {
  2749. #pragma unused(numItems)
  2750. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2751. }
  2752. - (void)dealloc {
  2753. NSAssert(!_autocreator,
  2754. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2755. [self class], _autocreator);
  2756. [_dictionary release];
  2757. [super dealloc];
  2758. }
  2759. - (instancetype)copyWithZone:(NSZone *)zone {
  2760. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2761. }
  2762. - (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
  2763. if (self == other) {
  2764. return YES;
  2765. }
  2766. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2767. return NO;
  2768. }
  2769. return [_dictionary isEqual:other->_dictionary];
  2770. }
  2771. - (NSUInteger)hash {
  2772. return _dictionary.count;
  2773. }
  2774. - (NSString *)description {
  2775. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2776. }
  2777. - (NSUInteger)count {
  2778. return _dictionary.count;
  2779. }
  2780. - (void)enumerateKeysAndRawValuesUsingBlock:
  2781. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2782. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2783. NSNumber *aValue,
  2784. BOOL *stop) {
  2785. block([aKey unsignedIntValue], [aValue intValue], stop);
  2786. }];
  2787. }
  2788. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2789. NSUInteger count = _dictionary.count;
  2790. if (count == 0) {
  2791. return 0;
  2792. }
  2793. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2794. GPBDataType keyDataType = field.mapKeyDataType;
  2795. __block size_t result = 0;
  2796. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2797. NSNumber *aValue,
  2798. BOOL *stop) {
  2799. #pragma unused(stop)
  2800. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2801. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2802. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2803. }];
  2804. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2805. result += tagSize * count;
  2806. return result;
  2807. }
  2808. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2809. asField:(GPBFieldDescriptor *)field {
  2810. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2811. GPBDataType keyDataType = field.mapKeyDataType;
  2812. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2813. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2814. NSNumber *aValue,
  2815. BOOL *stop) {
  2816. #pragma unused(stop)
  2817. // Write the tag.
  2818. [outputStream writeInt32NoTag:tag];
  2819. // Write the size of the message.
  2820. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2821. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2822. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2823. // Write the fields.
  2824. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2825. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2826. }];
  2827. }
  2828. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2829. forKey:(GPBGenericValue *)key
  2830. keyDataType:(GPBDataType)keyDataType {
  2831. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2832. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2833. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2834. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2835. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2836. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2837. [outputStream release];
  2838. return data;
  2839. }
  2840. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2841. forGPBGenericValueKey:(GPBGenericValue *)key {
  2842. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2843. }
  2844. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2845. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2846. #pragma unused(stop)
  2847. block([NSString stringWithFormat:@"%u", key], @(value));
  2848. }];
  2849. }
  2850. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2851. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2852. if (wrapped && value) {
  2853. int32_t result = [wrapped intValue];
  2854. if (!_validationFunc(result)) {
  2855. result = kGPBUnrecognizedEnumeratorValue;
  2856. }
  2857. *value = result;
  2858. }
  2859. return (wrapped != NULL);
  2860. }
  2861. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2862. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2863. if (wrapped && rawValue) {
  2864. *rawValue = [wrapped intValue];
  2865. }
  2866. return (wrapped != NULL);
  2867. }
  2868. - (void)enumerateKeysAndValuesUsingBlock:
  2869. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2870. GPBEnumValidationFunc func = _validationFunc;
  2871. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2872. NSNumber *aValue,
  2873. BOOL *stop) {
  2874. int32_t unwrapped = [aValue intValue];
  2875. if (!func(unwrapped)) {
  2876. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2877. }
  2878. block([aKey unsignedIntValue], unwrapped, stop);
  2879. }];
  2880. }
  2881. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2882. if (otherDictionary) {
  2883. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2884. if (_autocreator) {
  2885. GPBAutocreatedDictionaryModified(_autocreator, self);
  2886. }
  2887. }
  2888. }
  2889. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2890. [_dictionary setObject:@(value) forKey:@(key)];
  2891. if (_autocreator) {
  2892. GPBAutocreatedDictionaryModified(_autocreator, self);
  2893. }
  2894. }
  2895. - (void)removeValueForKey:(uint32_t)aKey {
  2896. [_dictionary removeObjectForKey:@(aKey)];
  2897. }
  2898. - (void)removeAll {
  2899. [_dictionary removeAllObjects];
  2900. }
  2901. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2902. if (!_validationFunc(value)) {
  2903. [NSException raise:NSInvalidArgumentException
  2904. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2905. value];
  2906. }
  2907. [_dictionary setObject:@(value) forKey:@(key)];
  2908. if (_autocreator) {
  2909. GPBAutocreatedDictionaryModified(_autocreator, self);
  2910. }
  2911. }
  2912. @end
  2913. #pragma mark - UInt32 -> Object
  2914. @implementation GPBUInt32ObjectDictionary {
  2915. @package
  2916. NSMutableDictionary *_dictionary;
  2917. }
  2918. + (instancetype)dictionary {
  2919. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  2920. }
  2921. + (instancetype)dictionaryWithObject:(id)object
  2922. forKey:(uint32_t)key {
  2923. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2924. // on to get the type correct.
  2925. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  2926. forKeys:&key
  2927. count:1] autorelease];
  2928. }
  2929. + (instancetype)dictionaryWithObjects:(const id [])objects
  2930. forKeys:(const uint32_t [])keys
  2931. count:(NSUInteger)count {
  2932. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2933. // on to get the type correct.
  2934. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  2935. forKeys:keys
  2936. count:count] autorelease];
  2937. }
  2938. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2939. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2940. // on to get the type correct.
  2941. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2942. }
  2943. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2944. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2945. }
  2946. - (instancetype)init {
  2947. return [self initWithObjects:NULL forKeys:NULL count:0];
  2948. }
  2949. - (instancetype)initWithObjects:(const id [])objects
  2950. forKeys:(const uint32_t [])keys
  2951. count:(NSUInteger)count {
  2952. self = [super init];
  2953. if (self) {
  2954. _dictionary = [[NSMutableDictionary alloc] init];
  2955. if (count && objects && keys) {
  2956. for (NSUInteger i = 0; i < count; ++i) {
  2957. if (!objects[i]) {
  2958. [NSException raise:NSInvalidArgumentException
  2959. format:@"Attempting to add nil object to a Dictionary"];
  2960. }
  2961. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  2962. }
  2963. }
  2964. }
  2965. return self;
  2966. }
  2967. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2968. self = [self initWithObjects:NULL forKeys:NULL count:0];
  2969. if (self) {
  2970. if (dictionary) {
  2971. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2972. }
  2973. }
  2974. return self;
  2975. }
  2976. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2977. #pragma unused(numItems)
  2978. return [self initWithObjects:NULL forKeys:NULL count:0];
  2979. }
  2980. - (void)dealloc {
  2981. NSAssert(!_autocreator,
  2982. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2983. [self class], _autocreator);
  2984. [_dictionary release];
  2985. [super dealloc];
  2986. }
  2987. - (instancetype)copyWithZone:(NSZone *)zone {
  2988. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2989. }
  2990. - (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
  2991. if (self == other) {
  2992. return YES;
  2993. }
  2994. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2995. return NO;
  2996. }
  2997. return [_dictionary isEqual:other->_dictionary];
  2998. }
  2999. - (NSUInteger)hash {
  3000. return _dictionary.count;
  3001. }
  3002. - (NSString *)description {
  3003. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3004. }
  3005. - (NSUInteger)count {
  3006. return _dictionary.count;
  3007. }
  3008. - (void)enumerateKeysAndObjectsUsingBlock:
  3009. (void (^)(uint32_t key, id object, BOOL *stop))block {
  3010. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3011. id aObject,
  3012. BOOL *stop) {
  3013. block([aKey unsignedIntValue], aObject, stop);
  3014. }];
  3015. }
  3016. - (BOOL)isInitialized {
  3017. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  3018. if (!msg.initialized) {
  3019. return NO;
  3020. }
  3021. }
  3022. return YES;
  3023. }
  3024. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  3025. GPBUInt32ObjectDictionary *newDict =
  3026. [[GPBUInt32ObjectDictionary alloc] init];
  3027. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  3028. GPBMessage *msg,
  3029. BOOL *stop) {
  3030. #pragma unused(stop)
  3031. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3032. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  3033. [copiedMsg release];
  3034. }];
  3035. return newDict;
  3036. }
  3037. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3038. NSUInteger count = _dictionary.count;
  3039. if (count == 0) {
  3040. return 0;
  3041. }
  3042. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3043. GPBDataType keyDataType = field.mapKeyDataType;
  3044. __block size_t result = 0;
  3045. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3046. id aObject,
  3047. BOOL *stop) {
  3048. #pragma unused(stop)
  3049. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3050. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3051. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3052. }];
  3053. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3054. result += tagSize * count;
  3055. return result;
  3056. }
  3057. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3058. asField:(GPBFieldDescriptor *)field {
  3059. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3060. GPBDataType keyDataType = field.mapKeyDataType;
  3061. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3062. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3063. id aObject,
  3064. BOOL *stop) {
  3065. #pragma unused(stop)
  3066. // Write the tag.
  3067. [outputStream writeInt32NoTag:tag];
  3068. // Write the size of the message.
  3069. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3070. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3071. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3072. // Write the fields.
  3073. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3074. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  3075. }];
  3076. }
  3077. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3078. forGPBGenericValueKey:(GPBGenericValue *)key {
  3079. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3080. }
  3081. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3082. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3083. #pragma unused(stop)
  3084. block([NSString stringWithFormat:@"%u", key], object);
  3085. }];
  3086. }
  3087. - (id)objectForKey:(uint32_t)key {
  3088. id result = [_dictionary objectForKey:@(key)];
  3089. return result;
  3090. }
  3091. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3092. if (otherDictionary) {
  3093. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3094. if (_autocreator) {
  3095. GPBAutocreatedDictionaryModified(_autocreator, self);
  3096. }
  3097. }
  3098. }
  3099. - (void)setObject:(id)object forKey:(uint32_t)key {
  3100. if (!object) {
  3101. [NSException raise:NSInvalidArgumentException
  3102. format:@"Attempting to add nil object to a Dictionary"];
  3103. }
  3104. [_dictionary setObject:object forKey:@(key)];
  3105. if (_autocreator) {
  3106. GPBAutocreatedDictionaryModified(_autocreator, self);
  3107. }
  3108. }
  3109. - (void)removeObjectForKey:(uint32_t)aKey {
  3110. [_dictionary removeObjectForKey:@(aKey)];
  3111. }
  3112. - (void)removeAll {
  3113. [_dictionary removeAllObjects];
  3114. }
  3115. @end
  3116. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3117. // This block of code is generated, do not edit it directly.
  3118. #pragma mark - Int32 -> UInt32
  3119. @implementation GPBInt32UInt32Dictionary {
  3120. @package
  3121. NSMutableDictionary *_dictionary;
  3122. }
  3123. + (instancetype)dictionary {
  3124. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3125. }
  3126. + (instancetype)dictionaryWithValue:(uint32_t)value
  3127. forKey:(int32_t)key {
  3128. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3129. // on to get the type correct.
  3130. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  3131. forKeys:&key
  3132. count:1] autorelease];
  3133. }
  3134. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  3135. forKeys:(const int32_t [])keys
  3136. count:(NSUInteger)count {
  3137. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3138. // on to get the type correct.
  3139. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  3140. forKeys:keys
  3141. count:count] autorelease];
  3142. }
  3143. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3144. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3145. // on to get the type correct.
  3146. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3147. }
  3148. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3149. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3150. }
  3151. - (instancetype)init {
  3152. return [self initWithValues:NULL forKeys:NULL count:0];
  3153. }
  3154. - (instancetype)initWithValues:(const uint32_t [])values
  3155. forKeys:(const int32_t [])keys
  3156. count:(NSUInteger)count {
  3157. self = [super init];
  3158. if (self) {
  3159. _dictionary = [[NSMutableDictionary alloc] init];
  3160. if (count && values && keys) {
  3161. for (NSUInteger i = 0; i < count; ++i) {
  3162. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3163. }
  3164. }
  3165. }
  3166. return self;
  3167. }
  3168. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3169. self = [self initWithValues:NULL forKeys:NULL count:0];
  3170. if (self) {
  3171. if (dictionary) {
  3172. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3173. }
  3174. }
  3175. return self;
  3176. }
  3177. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3178. #pragma unused(numItems)
  3179. return [self initWithValues:NULL forKeys:NULL count:0];
  3180. }
  3181. - (void)dealloc {
  3182. NSAssert(!_autocreator,
  3183. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3184. [self class], _autocreator);
  3185. [_dictionary release];
  3186. [super dealloc];
  3187. }
  3188. - (instancetype)copyWithZone:(NSZone *)zone {
  3189. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3190. }
  3191. - (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
  3192. if (self == other) {
  3193. return YES;
  3194. }
  3195. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3196. return NO;
  3197. }
  3198. return [_dictionary isEqual:other->_dictionary];
  3199. }
  3200. - (NSUInteger)hash {
  3201. return _dictionary.count;
  3202. }
  3203. - (NSString *)description {
  3204. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3205. }
  3206. - (NSUInteger)count {
  3207. return _dictionary.count;
  3208. }
  3209. - (void)enumerateKeysAndValuesUsingBlock:
  3210. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3211. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3212. NSNumber *aValue,
  3213. BOOL *stop) {
  3214. block([aKey intValue], [aValue unsignedIntValue], stop);
  3215. }];
  3216. }
  3217. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3218. NSUInteger count = _dictionary.count;
  3219. if (count == 0) {
  3220. return 0;
  3221. }
  3222. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3223. GPBDataType keyDataType = field.mapKeyDataType;
  3224. __block size_t result = 0;
  3225. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3226. NSNumber *aValue,
  3227. BOOL *stop) {
  3228. #pragma unused(stop)
  3229. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3230. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3231. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3232. }];
  3233. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3234. result += tagSize * count;
  3235. return result;
  3236. }
  3237. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3238. asField:(GPBFieldDescriptor *)field {
  3239. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3240. GPBDataType keyDataType = field.mapKeyDataType;
  3241. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3242. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3243. NSNumber *aValue,
  3244. BOOL *stop) {
  3245. #pragma unused(stop)
  3246. // Write the tag.
  3247. [outputStream writeInt32NoTag:tag];
  3248. // Write the size of the message.
  3249. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3250. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3251. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3252. // Write the fields.
  3253. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3254. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3255. }];
  3256. }
  3257. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3258. forGPBGenericValueKey:(GPBGenericValue *)key {
  3259. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3260. }
  3261. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3262. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3263. #pragma unused(stop)
  3264. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3265. }];
  3266. }
  3267. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3268. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3269. if (wrapped && value) {
  3270. *value = [wrapped unsignedIntValue];
  3271. }
  3272. return (wrapped != NULL);
  3273. }
  3274. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3275. if (otherDictionary) {
  3276. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3277. if (_autocreator) {
  3278. GPBAutocreatedDictionaryModified(_autocreator, self);
  3279. }
  3280. }
  3281. }
  3282. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3283. [_dictionary setObject:@(value) forKey:@(key)];
  3284. if (_autocreator) {
  3285. GPBAutocreatedDictionaryModified(_autocreator, self);
  3286. }
  3287. }
  3288. - (void)removeValueForKey:(int32_t)aKey {
  3289. [_dictionary removeObjectForKey:@(aKey)];
  3290. }
  3291. - (void)removeAll {
  3292. [_dictionary removeAllObjects];
  3293. }
  3294. @end
  3295. #pragma mark - Int32 -> Int32
  3296. @implementation GPBInt32Int32Dictionary {
  3297. @package
  3298. NSMutableDictionary *_dictionary;
  3299. }
  3300. + (instancetype)dictionary {
  3301. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3302. }
  3303. + (instancetype)dictionaryWithValue:(int32_t)value
  3304. forKey:(int32_t)key {
  3305. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3306. // on to get the type correct.
  3307. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3308. forKeys:&key
  3309. count:1] autorelease];
  3310. }
  3311. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3312. forKeys:(const int32_t [])keys
  3313. count:(NSUInteger)count {
  3314. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3315. // on to get the type correct.
  3316. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3317. forKeys:keys
  3318. count:count] autorelease];
  3319. }
  3320. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3321. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3322. // on to get the type correct.
  3323. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3324. }
  3325. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3326. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3327. }
  3328. - (instancetype)init {
  3329. return [self initWithValues:NULL forKeys:NULL count:0];
  3330. }
  3331. - (instancetype)initWithValues:(const int32_t [])values
  3332. forKeys:(const int32_t [])keys
  3333. count:(NSUInteger)count {
  3334. self = [super init];
  3335. if (self) {
  3336. _dictionary = [[NSMutableDictionary alloc] init];
  3337. if (count && values && keys) {
  3338. for (NSUInteger i = 0; i < count; ++i) {
  3339. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3340. }
  3341. }
  3342. }
  3343. return self;
  3344. }
  3345. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3346. self = [self initWithValues:NULL forKeys:NULL count:0];
  3347. if (self) {
  3348. if (dictionary) {
  3349. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3350. }
  3351. }
  3352. return self;
  3353. }
  3354. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3355. #pragma unused(numItems)
  3356. return [self initWithValues:NULL forKeys:NULL count:0];
  3357. }
  3358. - (void)dealloc {
  3359. NSAssert(!_autocreator,
  3360. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3361. [self class], _autocreator);
  3362. [_dictionary release];
  3363. [super dealloc];
  3364. }
  3365. - (instancetype)copyWithZone:(NSZone *)zone {
  3366. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3367. }
  3368. - (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
  3369. if (self == other) {
  3370. return YES;
  3371. }
  3372. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3373. return NO;
  3374. }
  3375. return [_dictionary isEqual:other->_dictionary];
  3376. }
  3377. - (NSUInteger)hash {
  3378. return _dictionary.count;
  3379. }
  3380. - (NSString *)description {
  3381. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3382. }
  3383. - (NSUInteger)count {
  3384. return _dictionary.count;
  3385. }
  3386. - (void)enumerateKeysAndValuesUsingBlock:
  3387. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3388. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3389. NSNumber *aValue,
  3390. BOOL *stop) {
  3391. block([aKey intValue], [aValue intValue], stop);
  3392. }];
  3393. }
  3394. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3395. NSUInteger count = _dictionary.count;
  3396. if (count == 0) {
  3397. return 0;
  3398. }
  3399. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3400. GPBDataType keyDataType = field.mapKeyDataType;
  3401. __block size_t result = 0;
  3402. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3403. NSNumber *aValue,
  3404. BOOL *stop) {
  3405. #pragma unused(stop)
  3406. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3407. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3408. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3409. }];
  3410. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3411. result += tagSize * count;
  3412. return result;
  3413. }
  3414. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3415. asField:(GPBFieldDescriptor *)field {
  3416. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3417. GPBDataType keyDataType = field.mapKeyDataType;
  3418. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3419. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3420. NSNumber *aValue,
  3421. BOOL *stop) {
  3422. #pragma unused(stop)
  3423. // Write the tag.
  3424. [outputStream writeInt32NoTag:tag];
  3425. // Write the size of the message.
  3426. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3427. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3428. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3429. // Write the fields.
  3430. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3431. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3432. }];
  3433. }
  3434. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3435. forGPBGenericValueKey:(GPBGenericValue *)key {
  3436. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3437. }
  3438. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3439. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3440. #pragma unused(stop)
  3441. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3442. }];
  3443. }
  3444. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3445. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3446. if (wrapped && value) {
  3447. *value = [wrapped intValue];
  3448. }
  3449. return (wrapped != NULL);
  3450. }
  3451. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3452. if (otherDictionary) {
  3453. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3454. if (_autocreator) {
  3455. GPBAutocreatedDictionaryModified(_autocreator, self);
  3456. }
  3457. }
  3458. }
  3459. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3460. [_dictionary setObject:@(value) forKey:@(key)];
  3461. if (_autocreator) {
  3462. GPBAutocreatedDictionaryModified(_autocreator, self);
  3463. }
  3464. }
  3465. - (void)removeValueForKey:(int32_t)aKey {
  3466. [_dictionary removeObjectForKey:@(aKey)];
  3467. }
  3468. - (void)removeAll {
  3469. [_dictionary removeAllObjects];
  3470. }
  3471. @end
  3472. #pragma mark - Int32 -> UInt64
  3473. @implementation GPBInt32UInt64Dictionary {
  3474. @package
  3475. NSMutableDictionary *_dictionary;
  3476. }
  3477. + (instancetype)dictionary {
  3478. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3479. }
  3480. + (instancetype)dictionaryWithValue:(uint64_t)value
  3481. forKey:(int32_t)key {
  3482. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3483. // on to get the type correct.
  3484. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3485. forKeys:&key
  3486. count:1] autorelease];
  3487. }
  3488. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3489. forKeys:(const int32_t [])keys
  3490. count:(NSUInteger)count {
  3491. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3492. // on to get the type correct.
  3493. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3494. forKeys:keys
  3495. count:count] autorelease];
  3496. }
  3497. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3498. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3499. // on to get the type correct.
  3500. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3501. }
  3502. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3503. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3504. }
  3505. - (instancetype)init {
  3506. return [self initWithValues:NULL forKeys:NULL count:0];
  3507. }
  3508. - (instancetype)initWithValues:(const uint64_t [])values
  3509. forKeys:(const int32_t [])keys
  3510. count:(NSUInteger)count {
  3511. self = [super init];
  3512. if (self) {
  3513. _dictionary = [[NSMutableDictionary alloc] init];
  3514. if (count && values && keys) {
  3515. for (NSUInteger i = 0; i < count; ++i) {
  3516. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3517. }
  3518. }
  3519. }
  3520. return self;
  3521. }
  3522. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3523. self = [self initWithValues:NULL forKeys:NULL count:0];
  3524. if (self) {
  3525. if (dictionary) {
  3526. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3527. }
  3528. }
  3529. return self;
  3530. }
  3531. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3532. #pragma unused(numItems)
  3533. return [self initWithValues:NULL forKeys:NULL count:0];
  3534. }
  3535. - (void)dealloc {
  3536. NSAssert(!_autocreator,
  3537. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3538. [self class], _autocreator);
  3539. [_dictionary release];
  3540. [super dealloc];
  3541. }
  3542. - (instancetype)copyWithZone:(NSZone *)zone {
  3543. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3544. }
  3545. - (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
  3546. if (self == other) {
  3547. return YES;
  3548. }
  3549. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3550. return NO;
  3551. }
  3552. return [_dictionary isEqual:other->_dictionary];
  3553. }
  3554. - (NSUInteger)hash {
  3555. return _dictionary.count;
  3556. }
  3557. - (NSString *)description {
  3558. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3559. }
  3560. - (NSUInteger)count {
  3561. return _dictionary.count;
  3562. }
  3563. - (void)enumerateKeysAndValuesUsingBlock:
  3564. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3565. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3566. NSNumber *aValue,
  3567. BOOL *stop) {
  3568. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3569. }];
  3570. }
  3571. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3572. NSUInteger count = _dictionary.count;
  3573. if (count == 0) {
  3574. return 0;
  3575. }
  3576. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3577. GPBDataType keyDataType = field.mapKeyDataType;
  3578. __block size_t result = 0;
  3579. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3580. NSNumber *aValue,
  3581. BOOL *stop) {
  3582. #pragma unused(stop)
  3583. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3584. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3585. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3586. }];
  3587. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3588. result += tagSize * count;
  3589. return result;
  3590. }
  3591. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3592. asField:(GPBFieldDescriptor *)field {
  3593. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3594. GPBDataType keyDataType = field.mapKeyDataType;
  3595. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3596. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3597. NSNumber *aValue,
  3598. BOOL *stop) {
  3599. #pragma unused(stop)
  3600. // Write the tag.
  3601. [outputStream writeInt32NoTag:tag];
  3602. // Write the size of the message.
  3603. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3604. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3605. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3606. // Write the fields.
  3607. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3608. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3609. }];
  3610. }
  3611. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3612. forGPBGenericValueKey:(GPBGenericValue *)key {
  3613. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3614. }
  3615. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3616. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3617. #pragma unused(stop)
  3618. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3619. }];
  3620. }
  3621. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3622. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3623. if (wrapped && value) {
  3624. *value = [wrapped unsignedLongLongValue];
  3625. }
  3626. return (wrapped != NULL);
  3627. }
  3628. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3629. if (otherDictionary) {
  3630. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3631. if (_autocreator) {
  3632. GPBAutocreatedDictionaryModified(_autocreator, self);
  3633. }
  3634. }
  3635. }
  3636. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3637. [_dictionary setObject:@(value) forKey:@(key)];
  3638. if (_autocreator) {
  3639. GPBAutocreatedDictionaryModified(_autocreator, self);
  3640. }
  3641. }
  3642. - (void)removeValueForKey:(int32_t)aKey {
  3643. [_dictionary removeObjectForKey:@(aKey)];
  3644. }
  3645. - (void)removeAll {
  3646. [_dictionary removeAllObjects];
  3647. }
  3648. @end
  3649. #pragma mark - Int32 -> Int64
  3650. @implementation GPBInt32Int64Dictionary {
  3651. @package
  3652. NSMutableDictionary *_dictionary;
  3653. }
  3654. + (instancetype)dictionary {
  3655. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3656. }
  3657. + (instancetype)dictionaryWithValue:(int64_t)value
  3658. forKey:(int32_t)key {
  3659. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3660. // on to get the type correct.
  3661. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3662. forKeys:&key
  3663. count:1] autorelease];
  3664. }
  3665. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3666. forKeys:(const int32_t [])keys
  3667. count:(NSUInteger)count {
  3668. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3669. // on to get the type correct.
  3670. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3671. forKeys:keys
  3672. count:count] autorelease];
  3673. }
  3674. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3675. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3676. // on to get the type correct.
  3677. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3678. }
  3679. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3680. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3681. }
  3682. - (instancetype)init {
  3683. return [self initWithValues:NULL forKeys:NULL count:0];
  3684. }
  3685. - (instancetype)initWithValues:(const int64_t [])values
  3686. forKeys:(const int32_t [])keys
  3687. count:(NSUInteger)count {
  3688. self = [super init];
  3689. if (self) {
  3690. _dictionary = [[NSMutableDictionary alloc] init];
  3691. if (count && values && keys) {
  3692. for (NSUInteger i = 0; i < count; ++i) {
  3693. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3694. }
  3695. }
  3696. }
  3697. return self;
  3698. }
  3699. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3700. self = [self initWithValues:NULL forKeys:NULL count:0];
  3701. if (self) {
  3702. if (dictionary) {
  3703. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3704. }
  3705. }
  3706. return self;
  3707. }
  3708. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3709. #pragma unused(numItems)
  3710. return [self initWithValues:NULL forKeys:NULL count:0];
  3711. }
  3712. - (void)dealloc {
  3713. NSAssert(!_autocreator,
  3714. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3715. [self class], _autocreator);
  3716. [_dictionary release];
  3717. [super dealloc];
  3718. }
  3719. - (instancetype)copyWithZone:(NSZone *)zone {
  3720. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3721. }
  3722. - (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
  3723. if (self == other) {
  3724. return YES;
  3725. }
  3726. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3727. return NO;
  3728. }
  3729. return [_dictionary isEqual:other->_dictionary];
  3730. }
  3731. - (NSUInteger)hash {
  3732. return _dictionary.count;
  3733. }
  3734. - (NSString *)description {
  3735. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3736. }
  3737. - (NSUInteger)count {
  3738. return _dictionary.count;
  3739. }
  3740. - (void)enumerateKeysAndValuesUsingBlock:
  3741. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3742. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3743. NSNumber *aValue,
  3744. BOOL *stop) {
  3745. block([aKey intValue], [aValue longLongValue], stop);
  3746. }];
  3747. }
  3748. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3749. NSUInteger count = _dictionary.count;
  3750. if (count == 0) {
  3751. return 0;
  3752. }
  3753. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3754. GPBDataType keyDataType = field.mapKeyDataType;
  3755. __block size_t result = 0;
  3756. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3757. NSNumber *aValue,
  3758. BOOL *stop) {
  3759. #pragma unused(stop)
  3760. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3761. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3762. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3763. }];
  3764. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3765. result += tagSize * count;
  3766. return result;
  3767. }
  3768. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3769. asField:(GPBFieldDescriptor *)field {
  3770. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3771. GPBDataType keyDataType = field.mapKeyDataType;
  3772. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3773. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3774. NSNumber *aValue,
  3775. BOOL *stop) {
  3776. #pragma unused(stop)
  3777. // Write the tag.
  3778. [outputStream writeInt32NoTag:tag];
  3779. // Write the size of the message.
  3780. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3781. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3782. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3783. // Write the fields.
  3784. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3785. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3786. }];
  3787. }
  3788. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3789. forGPBGenericValueKey:(GPBGenericValue *)key {
  3790. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3791. }
  3792. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3793. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3794. #pragma unused(stop)
  3795. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3796. }];
  3797. }
  3798. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3799. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3800. if (wrapped && value) {
  3801. *value = [wrapped longLongValue];
  3802. }
  3803. return (wrapped != NULL);
  3804. }
  3805. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3806. if (otherDictionary) {
  3807. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3808. if (_autocreator) {
  3809. GPBAutocreatedDictionaryModified(_autocreator, self);
  3810. }
  3811. }
  3812. }
  3813. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3814. [_dictionary setObject:@(value) forKey:@(key)];
  3815. if (_autocreator) {
  3816. GPBAutocreatedDictionaryModified(_autocreator, self);
  3817. }
  3818. }
  3819. - (void)removeValueForKey:(int32_t)aKey {
  3820. [_dictionary removeObjectForKey:@(aKey)];
  3821. }
  3822. - (void)removeAll {
  3823. [_dictionary removeAllObjects];
  3824. }
  3825. @end
  3826. #pragma mark - Int32 -> Bool
  3827. @implementation GPBInt32BoolDictionary {
  3828. @package
  3829. NSMutableDictionary *_dictionary;
  3830. }
  3831. + (instancetype)dictionary {
  3832. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3833. }
  3834. + (instancetype)dictionaryWithValue:(BOOL)value
  3835. forKey:(int32_t)key {
  3836. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3837. // on to get the type correct.
  3838. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3839. forKeys:&key
  3840. count:1] autorelease];
  3841. }
  3842. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3843. forKeys:(const int32_t [])keys
  3844. count:(NSUInteger)count {
  3845. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3846. // on to get the type correct.
  3847. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3848. forKeys:keys
  3849. count:count] autorelease];
  3850. }
  3851. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3852. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3853. // on to get the type correct.
  3854. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3855. }
  3856. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3857. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3858. }
  3859. - (instancetype)init {
  3860. return [self initWithValues:NULL forKeys:NULL count:0];
  3861. }
  3862. - (instancetype)initWithValues:(const BOOL [])values
  3863. forKeys:(const int32_t [])keys
  3864. count:(NSUInteger)count {
  3865. self = [super init];
  3866. if (self) {
  3867. _dictionary = [[NSMutableDictionary alloc] init];
  3868. if (count && values && keys) {
  3869. for (NSUInteger i = 0; i < count; ++i) {
  3870. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3871. }
  3872. }
  3873. }
  3874. return self;
  3875. }
  3876. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3877. self = [self initWithValues:NULL forKeys:NULL count:0];
  3878. if (self) {
  3879. if (dictionary) {
  3880. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3881. }
  3882. }
  3883. return self;
  3884. }
  3885. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3886. #pragma unused(numItems)
  3887. return [self initWithValues:NULL forKeys:NULL count:0];
  3888. }
  3889. - (void)dealloc {
  3890. NSAssert(!_autocreator,
  3891. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3892. [self class], _autocreator);
  3893. [_dictionary release];
  3894. [super dealloc];
  3895. }
  3896. - (instancetype)copyWithZone:(NSZone *)zone {
  3897. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3898. }
  3899. - (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
  3900. if (self == other) {
  3901. return YES;
  3902. }
  3903. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3904. return NO;
  3905. }
  3906. return [_dictionary isEqual:other->_dictionary];
  3907. }
  3908. - (NSUInteger)hash {
  3909. return _dictionary.count;
  3910. }
  3911. - (NSString *)description {
  3912. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3913. }
  3914. - (NSUInteger)count {
  3915. return _dictionary.count;
  3916. }
  3917. - (void)enumerateKeysAndValuesUsingBlock:
  3918. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3919. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3920. NSNumber *aValue,
  3921. BOOL *stop) {
  3922. block([aKey intValue], [aValue boolValue], stop);
  3923. }];
  3924. }
  3925. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3926. NSUInteger count = _dictionary.count;
  3927. if (count == 0) {
  3928. return 0;
  3929. }
  3930. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3931. GPBDataType keyDataType = field.mapKeyDataType;
  3932. __block size_t result = 0;
  3933. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3934. NSNumber *aValue,
  3935. BOOL *stop) {
  3936. #pragma unused(stop)
  3937. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3938. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3939. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3940. }];
  3941. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3942. result += tagSize * count;
  3943. return result;
  3944. }
  3945. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3946. asField:(GPBFieldDescriptor *)field {
  3947. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3948. GPBDataType keyDataType = field.mapKeyDataType;
  3949. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3950. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3951. NSNumber *aValue,
  3952. BOOL *stop) {
  3953. #pragma unused(stop)
  3954. // Write the tag.
  3955. [outputStream writeInt32NoTag:tag];
  3956. // Write the size of the message.
  3957. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3958. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3959. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3960. // Write the fields.
  3961. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3962. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  3963. }];
  3964. }
  3965. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3966. forGPBGenericValueKey:(GPBGenericValue *)key {
  3967. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3968. }
  3969. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3970. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3971. #pragma unused(stop)
  3972. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3973. }];
  3974. }
  3975. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  3976. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3977. if (wrapped && value) {
  3978. *value = [wrapped boolValue];
  3979. }
  3980. return (wrapped != NULL);
  3981. }
  3982. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3983. if (otherDictionary) {
  3984. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3985. if (_autocreator) {
  3986. GPBAutocreatedDictionaryModified(_autocreator, self);
  3987. }
  3988. }
  3989. }
  3990. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  3991. [_dictionary setObject:@(value) forKey:@(key)];
  3992. if (_autocreator) {
  3993. GPBAutocreatedDictionaryModified(_autocreator, self);
  3994. }
  3995. }
  3996. - (void)removeValueForKey:(int32_t)aKey {
  3997. [_dictionary removeObjectForKey:@(aKey)];
  3998. }
  3999. - (void)removeAll {
  4000. [_dictionary removeAllObjects];
  4001. }
  4002. @end
  4003. #pragma mark - Int32 -> Float
  4004. @implementation GPBInt32FloatDictionary {
  4005. @package
  4006. NSMutableDictionary *_dictionary;
  4007. }
  4008. + (instancetype)dictionary {
  4009. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4010. }
  4011. + (instancetype)dictionaryWithValue:(float)value
  4012. forKey:(int32_t)key {
  4013. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4014. // on to get the type correct.
  4015. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  4016. forKeys:&key
  4017. count:1] autorelease];
  4018. }
  4019. + (instancetype)dictionaryWithValues:(const float [])values
  4020. forKeys:(const int32_t [])keys
  4021. count:(NSUInteger)count {
  4022. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4023. // on to get the type correct.
  4024. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  4025. forKeys:keys
  4026. count:count] autorelease];
  4027. }
  4028. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4029. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4030. // on to get the type correct.
  4031. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4032. }
  4033. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4034. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4035. }
  4036. - (instancetype)init {
  4037. return [self initWithValues:NULL forKeys:NULL count:0];
  4038. }
  4039. - (instancetype)initWithValues:(const float [])values
  4040. forKeys:(const int32_t [])keys
  4041. count:(NSUInteger)count {
  4042. self = [super init];
  4043. if (self) {
  4044. _dictionary = [[NSMutableDictionary alloc] init];
  4045. if (count && values && keys) {
  4046. for (NSUInteger i = 0; i < count; ++i) {
  4047. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4048. }
  4049. }
  4050. }
  4051. return self;
  4052. }
  4053. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4054. self = [self initWithValues:NULL forKeys:NULL count:0];
  4055. if (self) {
  4056. if (dictionary) {
  4057. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4058. }
  4059. }
  4060. return self;
  4061. }
  4062. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4063. #pragma unused(numItems)
  4064. return [self initWithValues:NULL forKeys:NULL count:0];
  4065. }
  4066. - (void)dealloc {
  4067. NSAssert(!_autocreator,
  4068. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4069. [self class], _autocreator);
  4070. [_dictionary release];
  4071. [super dealloc];
  4072. }
  4073. - (instancetype)copyWithZone:(NSZone *)zone {
  4074. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4075. }
  4076. - (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
  4077. if (self == other) {
  4078. return YES;
  4079. }
  4080. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4081. return NO;
  4082. }
  4083. return [_dictionary isEqual:other->_dictionary];
  4084. }
  4085. - (NSUInteger)hash {
  4086. return _dictionary.count;
  4087. }
  4088. - (NSString *)description {
  4089. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4090. }
  4091. - (NSUInteger)count {
  4092. return _dictionary.count;
  4093. }
  4094. - (void)enumerateKeysAndValuesUsingBlock:
  4095. (void (^)(int32_t key, float value, BOOL *stop))block {
  4096. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4097. NSNumber *aValue,
  4098. BOOL *stop) {
  4099. block([aKey intValue], [aValue floatValue], stop);
  4100. }];
  4101. }
  4102. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4103. NSUInteger count = _dictionary.count;
  4104. if (count == 0) {
  4105. return 0;
  4106. }
  4107. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4108. GPBDataType keyDataType = field.mapKeyDataType;
  4109. __block size_t result = 0;
  4110. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4111. NSNumber *aValue,
  4112. BOOL *stop) {
  4113. #pragma unused(stop)
  4114. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4115. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4116. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4117. }];
  4118. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4119. result += tagSize * count;
  4120. return result;
  4121. }
  4122. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4123. asField:(GPBFieldDescriptor *)field {
  4124. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4125. GPBDataType keyDataType = field.mapKeyDataType;
  4126. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4127. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4128. NSNumber *aValue,
  4129. BOOL *stop) {
  4130. #pragma unused(stop)
  4131. // Write the tag.
  4132. [outputStream writeInt32NoTag:tag];
  4133. // Write the size of the message.
  4134. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4135. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4136. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4137. // Write the fields.
  4138. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4139. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4140. }];
  4141. }
  4142. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4143. forGPBGenericValueKey:(GPBGenericValue *)key {
  4144. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4145. }
  4146. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4147. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4148. #pragma unused(stop)
  4149. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4150. }];
  4151. }
  4152. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  4153. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4154. if (wrapped && value) {
  4155. *value = [wrapped floatValue];
  4156. }
  4157. return (wrapped != NULL);
  4158. }
  4159. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4160. if (otherDictionary) {
  4161. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4162. if (_autocreator) {
  4163. GPBAutocreatedDictionaryModified(_autocreator, self);
  4164. }
  4165. }
  4166. }
  4167. - (void)setValue:(float)value forKey:(int32_t)key {
  4168. [_dictionary setObject:@(value) forKey:@(key)];
  4169. if (_autocreator) {
  4170. GPBAutocreatedDictionaryModified(_autocreator, self);
  4171. }
  4172. }
  4173. - (void)removeValueForKey:(int32_t)aKey {
  4174. [_dictionary removeObjectForKey:@(aKey)];
  4175. }
  4176. - (void)removeAll {
  4177. [_dictionary removeAllObjects];
  4178. }
  4179. @end
  4180. #pragma mark - Int32 -> Double
  4181. @implementation GPBInt32DoubleDictionary {
  4182. @package
  4183. NSMutableDictionary *_dictionary;
  4184. }
  4185. + (instancetype)dictionary {
  4186. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4187. }
  4188. + (instancetype)dictionaryWithValue:(double)value
  4189. forKey:(int32_t)key {
  4190. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4191. // on to get the type correct.
  4192. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  4193. forKeys:&key
  4194. count:1] autorelease];
  4195. }
  4196. + (instancetype)dictionaryWithValues:(const double [])values
  4197. forKeys:(const int32_t [])keys
  4198. count:(NSUInteger)count {
  4199. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4200. // on to get the type correct.
  4201. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  4202. forKeys:keys
  4203. count:count] autorelease];
  4204. }
  4205. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4206. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4207. // on to get the type correct.
  4208. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4209. }
  4210. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4211. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4212. }
  4213. - (instancetype)init {
  4214. return [self initWithValues:NULL forKeys:NULL count:0];
  4215. }
  4216. - (instancetype)initWithValues:(const double [])values
  4217. forKeys:(const int32_t [])keys
  4218. count:(NSUInteger)count {
  4219. self = [super init];
  4220. if (self) {
  4221. _dictionary = [[NSMutableDictionary alloc] init];
  4222. if (count && values && keys) {
  4223. for (NSUInteger i = 0; i < count; ++i) {
  4224. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4225. }
  4226. }
  4227. }
  4228. return self;
  4229. }
  4230. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4231. self = [self initWithValues:NULL forKeys:NULL count:0];
  4232. if (self) {
  4233. if (dictionary) {
  4234. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4235. }
  4236. }
  4237. return self;
  4238. }
  4239. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4240. #pragma unused(numItems)
  4241. return [self initWithValues:NULL forKeys:NULL count:0];
  4242. }
  4243. - (void)dealloc {
  4244. NSAssert(!_autocreator,
  4245. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4246. [self class], _autocreator);
  4247. [_dictionary release];
  4248. [super dealloc];
  4249. }
  4250. - (instancetype)copyWithZone:(NSZone *)zone {
  4251. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4252. }
  4253. - (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
  4254. if (self == other) {
  4255. return YES;
  4256. }
  4257. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4258. return NO;
  4259. }
  4260. return [_dictionary isEqual:other->_dictionary];
  4261. }
  4262. - (NSUInteger)hash {
  4263. return _dictionary.count;
  4264. }
  4265. - (NSString *)description {
  4266. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4267. }
  4268. - (NSUInteger)count {
  4269. return _dictionary.count;
  4270. }
  4271. - (void)enumerateKeysAndValuesUsingBlock:
  4272. (void (^)(int32_t key, double value, BOOL *stop))block {
  4273. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4274. NSNumber *aValue,
  4275. BOOL *stop) {
  4276. block([aKey intValue], [aValue doubleValue], stop);
  4277. }];
  4278. }
  4279. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4280. NSUInteger count = _dictionary.count;
  4281. if (count == 0) {
  4282. return 0;
  4283. }
  4284. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4285. GPBDataType keyDataType = field.mapKeyDataType;
  4286. __block size_t result = 0;
  4287. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4288. NSNumber *aValue,
  4289. BOOL *stop) {
  4290. #pragma unused(stop)
  4291. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4292. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4293. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4294. }];
  4295. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4296. result += tagSize * count;
  4297. return result;
  4298. }
  4299. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4300. asField:(GPBFieldDescriptor *)field {
  4301. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4302. GPBDataType keyDataType = field.mapKeyDataType;
  4303. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4304. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4305. NSNumber *aValue,
  4306. BOOL *stop) {
  4307. #pragma unused(stop)
  4308. // Write the tag.
  4309. [outputStream writeInt32NoTag:tag];
  4310. // Write the size of the message.
  4311. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4312. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4313. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4314. // Write the fields.
  4315. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4316. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4317. }];
  4318. }
  4319. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4320. forGPBGenericValueKey:(GPBGenericValue *)key {
  4321. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4322. }
  4323. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4324. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4325. #pragma unused(stop)
  4326. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4327. }];
  4328. }
  4329. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4330. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4331. if (wrapped && value) {
  4332. *value = [wrapped doubleValue];
  4333. }
  4334. return (wrapped != NULL);
  4335. }
  4336. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4337. if (otherDictionary) {
  4338. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4339. if (_autocreator) {
  4340. GPBAutocreatedDictionaryModified(_autocreator, self);
  4341. }
  4342. }
  4343. }
  4344. - (void)setValue:(double)value forKey:(int32_t)key {
  4345. [_dictionary setObject:@(value) forKey:@(key)];
  4346. if (_autocreator) {
  4347. GPBAutocreatedDictionaryModified(_autocreator, self);
  4348. }
  4349. }
  4350. - (void)removeValueForKey:(int32_t)aKey {
  4351. [_dictionary removeObjectForKey:@(aKey)];
  4352. }
  4353. - (void)removeAll {
  4354. [_dictionary removeAllObjects];
  4355. }
  4356. @end
  4357. #pragma mark - Int32 -> Enum
  4358. @implementation GPBInt32EnumDictionary {
  4359. @package
  4360. NSMutableDictionary *_dictionary;
  4361. GPBEnumValidationFunc _validationFunc;
  4362. }
  4363. @synthesize validationFunc = _validationFunc;
  4364. + (instancetype)dictionary {
  4365. return [[[self alloc] initWithValidationFunction:NULL
  4366. rawValues:NULL
  4367. forKeys:NULL
  4368. count:0] autorelease];
  4369. }
  4370. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4371. return [[[self alloc] initWithValidationFunction:func
  4372. rawValues:NULL
  4373. forKeys:NULL
  4374. count:0] autorelease];
  4375. }
  4376. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4377. rawValue:(int32_t)rawValue
  4378. forKey:(int32_t)key {
  4379. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4380. // on to get the type correct.
  4381. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4382. rawValues:&rawValue
  4383. forKeys:&key
  4384. count:1] autorelease];
  4385. }
  4386. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4387. rawValues:(const int32_t [])rawValues
  4388. forKeys:(const int32_t [])keys
  4389. count:(NSUInteger)count {
  4390. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4391. // on to get the type correct.
  4392. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4393. rawValues:rawValues
  4394. forKeys:keys
  4395. count:count] autorelease];
  4396. }
  4397. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4398. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4399. // on to get the type correct.
  4400. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4401. }
  4402. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4403. capacity:(NSUInteger)numItems {
  4404. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4405. }
  4406. - (instancetype)init {
  4407. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4408. }
  4409. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4410. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4411. }
  4412. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4413. rawValues:(const int32_t [])rawValues
  4414. forKeys:(const int32_t [])keys
  4415. count:(NSUInteger)count {
  4416. self = [super init];
  4417. if (self) {
  4418. _dictionary = [[NSMutableDictionary alloc] init];
  4419. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4420. if (count && rawValues && keys) {
  4421. for (NSUInteger i = 0; i < count; ++i) {
  4422. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4423. }
  4424. }
  4425. }
  4426. return self;
  4427. }
  4428. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4429. self = [self initWithValidationFunction:dictionary.validationFunc
  4430. rawValues:NULL
  4431. forKeys:NULL
  4432. count:0];
  4433. if (self) {
  4434. if (dictionary) {
  4435. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4436. }
  4437. }
  4438. return self;
  4439. }
  4440. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4441. capacity:(NSUInteger)numItems {
  4442. #pragma unused(numItems)
  4443. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4444. }
  4445. - (void)dealloc {
  4446. NSAssert(!_autocreator,
  4447. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4448. [self class], _autocreator);
  4449. [_dictionary release];
  4450. [super dealloc];
  4451. }
  4452. - (instancetype)copyWithZone:(NSZone *)zone {
  4453. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4454. }
  4455. - (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
  4456. if (self == other) {
  4457. return YES;
  4458. }
  4459. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4460. return NO;
  4461. }
  4462. return [_dictionary isEqual:other->_dictionary];
  4463. }
  4464. - (NSUInteger)hash {
  4465. return _dictionary.count;
  4466. }
  4467. - (NSString *)description {
  4468. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4469. }
  4470. - (NSUInteger)count {
  4471. return _dictionary.count;
  4472. }
  4473. - (void)enumerateKeysAndRawValuesUsingBlock:
  4474. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4475. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4476. NSNumber *aValue,
  4477. BOOL *stop) {
  4478. block([aKey intValue], [aValue intValue], stop);
  4479. }];
  4480. }
  4481. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4482. NSUInteger count = _dictionary.count;
  4483. if (count == 0) {
  4484. return 0;
  4485. }
  4486. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4487. GPBDataType keyDataType = field.mapKeyDataType;
  4488. __block size_t result = 0;
  4489. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4490. NSNumber *aValue,
  4491. BOOL *stop) {
  4492. #pragma unused(stop)
  4493. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4494. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4495. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4496. }];
  4497. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4498. result += tagSize * count;
  4499. return result;
  4500. }
  4501. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4502. asField:(GPBFieldDescriptor *)field {
  4503. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4504. GPBDataType keyDataType = field.mapKeyDataType;
  4505. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4506. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4507. NSNumber *aValue,
  4508. BOOL *stop) {
  4509. #pragma unused(stop)
  4510. // Write the tag.
  4511. [outputStream writeInt32NoTag:tag];
  4512. // Write the size of the message.
  4513. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4514. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4515. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4516. // Write the fields.
  4517. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4518. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4519. }];
  4520. }
  4521. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4522. forKey:(GPBGenericValue *)key
  4523. keyDataType:(GPBDataType)keyDataType {
  4524. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4525. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4526. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4527. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4528. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4529. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4530. [outputStream release];
  4531. return data;
  4532. }
  4533. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4534. forGPBGenericValueKey:(GPBGenericValue *)key {
  4535. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4536. }
  4537. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4538. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4539. #pragma unused(stop)
  4540. block([NSString stringWithFormat:@"%d", key], @(value));
  4541. }];
  4542. }
  4543. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4544. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4545. if (wrapped && value) {
  4546. int32_t result = [wrapped intValue];
  4547. if (!_validationFunc(result)) {
  4548. result = kGPBUnrecognizedEnumeratorValue;
  4549. }
  4550. *value = result;
  4551. }
  4552. return (wrapped != NULL);
  4553. }
  4554. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4555. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4556. if (wrapped && rawValue) {
  4557. *rawValue = [wrapped intValue];
  4558. }
  4559. return (wrapped != NULL);
  4560. }
  4561. - (void)enumerateKeysAndValuesUsingBlock:
  4562. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4563. GPBEnumValidationFunc func = _validationFunc;
  4564. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4565. NSNumber *aValue,
  4566. BOOL *stop) {
  4567. int32_t unwrapped = [aValue intValue];
  4568. if (!func(unwrapped)) {
  4569. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4570. }
  4571. block([aKey intValue], unwrapped, stop);
  4572. }];
  4573. }
  4574. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4575. if (otherDictionary) {
  4576. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4577. if (_autocreator) {
  4578. GPBAutocreatedDictionaryModified(_autocreator, self);
  4579. }
  4580. }
  4581. }
  4582. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4583. [_dictionary setObject:@(value) forKey:@(key)];
  4584. if (_autocreator) {
  4585. GPBAutocreatedDictionaryModified(_autocreator, self);
  4586. }
  4587. }
  4588. - (void)removeValueForKey:(int32_t)aKey {
  4589. [_dictionary removeObjectForKey:@(aKey)];
  4590. }
  4591. - (void)removeAll {
  4592. [_dictionary removeAllObjects];
  4593. }
  4594. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4595. if (!_validationFunc(value)) {
  4596. [NSException raise:NSInvalidArgumentException
  4597. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4598. value];
  4599. }
  4600. [_dictionary setObject:@(value) forKey:@(key)];
  4601. if (_autocreator) {
  4602. GPBAutocreatedDictionaryModified(_autocreator, self);
  4603. }
  4604. }
  4605. @end
  4606. #pragma mark - Int32 -> Object
  4607. @implementation GPBInt32ObjectDictionary {
  4608. @package
  4609. NSMutableDictionary *_dictionary;
  4610. }
  4611. + (instancetype)dictionary {
  4612. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4613. }
  4614. + (instancetype)dictionaryWithObject:(id)object
  4615. forKey:(int32_t)key {
  4616. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4617. // on to get the type correct.
  4618. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4619. forKeys:&key
  4620. count:1] autorelease];
  4621. }
  4622. + (instancetype)dictionaryWithObjects:(const id [])objects
  4623. forKeys:(const int32_t [])keys
  4624. count:(NSUInteger)count {
  4625. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4626. // on to get the type correct.
  4627. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4628. forKeys:keys
  4629. count:count] autorelease];
  4630. }
  4631. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4632. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4633. // on to get the type correct.
  4634. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4635. }
  4636. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4637. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4638. }
  4639. - (instancetype)init {
  4640. return [self initWithObjects:NULL forKeys:NULL count:0];
  4641. }
  4642. - (instancetype)initWithObjects:(const id [])objects
  4643. forKeys:(const int32_t [])keys
  4644. count:(NSUInteger)count {
  4645. self = [super init];
  4646. if (self) {
  4647. _dictionary = [[NSMutableDictionary alloc] init];
  4648. if (count && objects && keys) {
  4649. for (NSUInteger i = 0; i < count; ++i) {
  4650. if (!objects[i]) {
  4651. [NSException raise:NSInvalidArgumentException
  4652. format:@"Attempting to add nil object to a Dictionary"];
  4653. }
  4654. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4655. }
  4656. }
  4657. }
  4658. return self;
  4659. }
  4660. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4661. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4662. if (self) {
  4663. if (dictionary) {
  4664. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4665. }
  4666. }
  4667. return self;
  4668. }
  4669. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4670. #pragma unused(numItems)
  4671. return [self initWithObjects:NULL forKeys:NULL count:0];
  4672. }
  4673. - (void)dealloc {
  4674. NSAssert(!_autocreator,
  4675. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4676. [self class], _autocreator);
  4677. [_dictionary release];
  4678. [super dealloc];
  4679. }
  4680. - (instancetype)copyWithZone:(NSZone *)zone {
  4681. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4682. }
  4683. - (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
  4684. if (self == other) {
  4685. return YES;
  4686. }
  4687. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4688. return NO;
  4689. }
  4690. return [_dictionary isEqual:other->_dictionary];
  4691. }
  4692. - (NSUInteger)hash {
  4693. return _dictionary.count;
  4694. }
  4695. - (NSString *)description {
  4696. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4697. }
  4698. - (NSUInteger)count {
  4699. return _dictionary.count;
  4700. }
  4701. - (void)enumerateKeysAndObjectsUsingBlock:
  4702. (void (^)(int32_t key, id object, BOOL *stop))block {
  4703. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4704. id aObject,
  4705. BOOL *stop) {
  4706. block([aKey intValue], aObject, stop);
  4707. }];
  4708. }
  4709. - (BOOL)isInitialized {
  4710. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4711. if (!msg.initialized) {
  4712. return NO;
  4713. }
  4714. }
  4715. return YES;
  4716. }
  4717. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4718. GPBInt32ObjectDictionary *newDict =
  4719. [[GPBInt32ObjectDictionary alloc] init];
  4720. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4721. GPBMessage *msg,
  4722. BOOL *stop) {
  4723. #pragma unused(stop)
  4724. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4725. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4726. [copiedMsg release];
  4727. }];
  4728. return newDict;
  4729. }
  4730. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4731. NSUInteger count = _dictionary.count;
  4732. if (count == 0) {
  4733. return 0;
  4734. }
  4735. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4736. GPBDataType keyDataType = field.mapKeyDataType;
  4737. __block size_t result = 0;
  4738. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4739. id aObject,
  4740. BOOL *stop) {
  4741. #pragma unused(stop)
  4742. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4743. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4744. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4745. }];
  4746. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4747. result += tagSize * count;
  4748. return result;
  4749. }
  4750. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4751. asField:(GPBFieldDescriptor *)field {
  4752. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4753. GPBDataType keyDataType = field.mapKeyDataType;
  4754. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4755. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4756. id aObject,
  4757. BOOL *stop) {
  4758. #pragma unused(stop)
  4759. // Write the tag.
  4760. [outputStream writeInt32NoTag:tag];
  4761. // Write the size of the message.
  4762. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4763. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4764. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4765. // Write the fields.
  4766. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4767. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  4768. }];
  4769. }
  4770. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4771. forGPBGenericValueKey:(GPBGenericValue *)key {
  4772. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4773. }
  4774. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4775. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4776. #pragma unused(stop)
  4777. block([NSString stringWithFormat:@"%d", key], object);
  4778. }];
  4779. }
  4780. - (id)objectForKey:(int32_t)key {
  4781. id result = [_dictionary objectForKey:@(key)];
  4782. return result;
  4783. }
  4784. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4785. if (otherDictionary) {
  4786. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4787. if (_autocreator) {
  4788. GPBAutocreatedDictionaryModified(_autocreator, self);
  4789. }
  4790. }
  4791. }
  4792. - (void)setObject:(id)object forKey:(int32_t)key {
  4793. if (!object) {
  4794. [NSException raise:NSInvalidArgumentException
  4795. format:@"Attempting to add nil object to a Dictionary"];
  4796. }
  4797. [_dictionary setObject:object forKey:@(key)];
  4798. if (_autocreator) {
  4799. GPBAutocreatedDictionaryModified(_autocreator, self);
  4800. }
  4801. }
  4802. - (void)removeObjectForKey:(int32_t)aKey {
  4803. [_dictionary removeObjectForKey:@(aKey)];
  4804. }
  4805. - (void)removeAll {
  4806. [_dictionary removeAllObjects];
  4807. }
  4808. @end
  4809. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4810. // This block of code is generated, do not edit it directly.
  4811. #pragma mark - UInt64 -> UInt32
  4812. @implementation GPBUInt64UInt32Dictionary {
  4813. @package
  4814. NSMutableDictionary *_dictionary;
  4815. }
  4816. + (instancetype)dictionary {
  4817. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4818. }
  4819. + (instancetype)dictionaryWithValue:(uint32_t)value
  4820. forKey:(uint64_t)key {
  4821. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4822. // on to get the type correct.
  4823. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4824. forKeys:&key
  4825. count:1] autorelease];
  4826. }
  4827. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4828. forKeys:(const uint64_t [])keys
  4829. count:(NSUInteger)count {
  4830. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4831. // on to get the type correct.
  4832. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4833. forKeys:keys
  4834. count:count] autorelease];
  4835. }
  4836. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4837. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4838. // on to get the type correct.
  4839. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4840. }
  4841. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4842. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4843. }
  4844. - (instancetype)init {
  4845. return [self initWithValues:NULL forKeys:NULL count:0];
  4846. }
  4847. - (instancetype)initWithValues:(const uint32_t [])values
  4848. forKeys:(const uint64_t [])keys
  4849. count:(NSUInteger)count {
  4850. self = [super init];
  4851. if (self) {
  4852. _dictionary = [[NSMutableDictionary alloc] init];
  4853. if (count && values && keys) {
  4854. for (NSUInteger i = 0; i < count; ++i) {
  4855. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4856. }
  4857. }
  4858. }
  4859. return self;
  4860. }
  4861. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4862. self = [self initWithValues:NULL forKeys:NULL count:0];
  4863. if (self) {
  4864. if (dictionary) {
  4865. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4866. }
  4867. }
  4868. return self;
  4869. }
  4870. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4871. #pragma unused(numItems)
  4872. return [self initWithValues:NULL forKeys:NULL count:0];
  4873. }
  4874. - (void)dealloc {
  4875. NSAssert(!_autocreator,
  4876. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4877. [self class], _autocreator);
  4878. [_dictionary release];
  4879. [super dealloc];
  4880. }
  4881. - (instancetype)copyWithZone:(NSZone *)zone {
  4882. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4883. }
  4884. - (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
  4885. if (self == other) {
  4886. return YES;
  4887. }
  4888. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4889. return NO;
  4890. }
  4891. return [_dictionary isEqual:other->_dictionary];
  4892. }
  4893. - (NSUInteger)hash {
  4894. return _dictionary.count;
  4895. }
  4896. - (NSString *)description {
  4897. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4898. }
  4899. - (NSUInteger)count {
  4900. return _dictionary.count;
  4901. }
  4902. - (void)enumerateKeysAndValuesUsingBlock:
  4903. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4904. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4905. NSNumber *aValue,
  4906. BOOL *stop) {
  4907. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4908. }];
  4909. }
  4910. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4911. NSUInteger count = _dictionary.count;
  4912. if (count == 0) {
  4913. return 0;
  4914. }
  4915. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4916. GPBDataType keyDataType = field.mapKeyDataType;
  4917. __block size_t result = 0;
  4918. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4919. NSNumber *aValue,
  4920. BOOL *stop) {
  4921. #pragma unused(stop)
  4922. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4923. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4924. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4925. }];
  4926. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4927. result += tagSize * count;
  4928. return result;
  4929. }
  4930. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4931. asField:(GPBFieldDescriptor *)field {
  4932. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4933. GPBDataType keyDataType = field.mapKeyDataType;
  4934. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4935. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4936. NSNumber *aValue,
  4937. BOOL *stop) {
  4938. #pragma unused(stop)
  4939. // Write the tag.
  4940. [outputStream writeInt32NoTag:tag];
  4941. // Write the size of the message.
  4942. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4943. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4944. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4945. // Write the fields.
  4946. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4947. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4948. }];
  4949. }
  4950. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4951. forGPBGenericValueKey:(GPBGenericValue *)key {
  4952. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4953. }
  4954. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4955. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4956. #pragma unused(stop)
  4957. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4958. }];
  4959. }
  4960. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  4961. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4962. if (wrapped && value) {
  4963. *value = [wrapped unsignedIntValue];
  4964. }
  4965. return (wrapped != NULL);
  4966. }
  4967. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4968. if (otherDictionary) {
  4969. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4970. if (_autocreator) {
  4971. GPBAutocreatedDictionaryModified(_autocreator, self);
  4972. }
  4973. }
  4974. }
  4975. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  4976. [_dictionary setObject:@(value) forKey:@(key)];
  4977. if (_autocreator) {
  4978. GPBAutocreatedDictionaryModified(_autocreator, self);
  4979. }
  4980. }
  4981. - (void)removeValueForKey:(uint64_t)aKey {
  4982. [_dictionary removeObjectForKey:@(aKey)];
  4983. }
  4984. - (void)removeAll {
  4985. [_dictionary removeAllObjects];
  4986. }
  4987. @end
  4988. #pragma mark - UInt64 -> Int32
  4989. @implementation GPBUInt64Int32Dictionary {
  4990. @package
  4991. NSMutableDictionary *_dictionary;
  4992. }
  4993. + (instancetype)dictionary {
  4994. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4995. }
  4996. + (instancetype)dictionaryWithValue:(int32_t)value
  4997. forKey:(uint64_t)key {
  4998. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4999. // on to get the type correct.
  5000. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  5001. forKeys:&key
  5002. count:1] autorelease];
  5003. }
  5004. + (instancetype)dictionaryWithValues:(const int32_t [])values
  5005. forKeys:(const uint64_t [])keys
  5006. count:(NSUInteger)count {
  5007. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5008. // on to get the type correct.
  5009. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  5010. forKeys:keys
  5011. count:count] autorelease];
  5012. }
  5013. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5014. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5015. // on to get the type correct.
  5016. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5017. }
  5018. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5019. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5020. }
  5021. - (instancetype)init {
  5022. return [self initWithValues:NULL forKeys:NULL count:0];
  5023. }
  5024. - (instancetype)initWithValues:(const int32_t [])values
  5025. forKeys:(const uint64_t [])keys
  5026. count:(NSUInteger)count {
  5027. self = [super init];
  5028. if (self) {
  5029. _dictionary = [[NSMutableDictionary alloc] init];
  5030. if (count && values && keys) {
  5031. for (NSUInteger i = 0; i < count; ++i) {
  5032. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5033. }
  5034. }
  5035. }
  5036. return self;
  5037. }
  5038. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5039. self = [self initWithValues:NULL forKeys:NULL count:0];
  5040. if (self) {
  5041. if (dictionary) {
  5042. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5043. }
  5044. }
  5045. return self;
  5046. }
  5047. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5048. #pragma unused(numItems)
  5049. return [self initWithValues:NULL forKeys:NULL count:0];
  5050. }
  5051. - (void)dealloc {
  5052. NSAssert(!_autocreator,
  5053. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5054. [self class], _autocreator);
  5055. [_dictionary release];
  5056. [super dealloc];
  5057. }
  5058. - (instancetype)copyWithZone:(NSZone *)zone {
  5059. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5060. }
  5061. - (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
  5062. if (self == other) {
  5063. return YES;
  5064. }
  5065. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5066. return NO;
  5067. }
  5068. return [_dictionary isEqual:other->_dictionary];
  5069. }
  5070. - (NSUInteger)hash {
  5071. return _dictionary.count;
  5072. }
  5073. - (NSString *)description {
  5074. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5075. }
  5076. - (NSUInteger)count {
  5077. return _dictionary.count;
  5078. }
  5079. - (void)enumerateKeysAndValuesUsingBlock:
  5080. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5081. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5082. NSNumber *aValue,
  5083. BOOL *stop) {
  5084. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5085. }];
  5086. }
  5087. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5088. NSUInteger count = _dictionary.count;
  5089. if (count == 0) {
  5090. return 0;
  5091. }
  5092. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5093. GPBDataType keyDataType = field.mapKeyDataType;
  5094. __block size_t result = 0;
  5095. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5096. NSNumber *aValue,
  5097. BOOL *stop) {
  5098. #pragma unused(stop)
  5099. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5100. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5101. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5102. }];
  5103. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5104. result += tagSize * count;
  5105. return result;
  5106. }
  5107. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5108. asField:(GPBFieldDescriptor *)field {
  5109. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5110. GPBDataType keyDataType = field.mapKeyDataType;
  5111. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5112. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5113. NSNumber *aValue,
  5114. BOOL *stop) {
  5115. #pragma unused(stop)
  5116. // Write the tag.
  5117. [outputStream writeInt32NoTag:tag];
  5118. // Write the size of the message.
  5119. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5120. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5121. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5122. // Write the fields.
  5123. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5124. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5125. }];
  5126. }
  5127. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5128. forGPBGenericValueKey:(GPBGenericValue *)key {
  5129. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5130. }
  5131. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5132. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5133. #pragma unused(stop)
  5134. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5135. }];
  5136. }
  5137. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5138. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5139. if (wrapped && value) {
  5140. *value = [wrapped intValue];
  5141. }
  5142. return (wrapped != NULL);
  5143. }
  5144. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5145. if (otherDictionary) {
  5146. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5147. if (_autocreator) {
  5148. GPBAutocreatedDictionaryModified(_autocreator, self);
  5149. }
  5150. }
  5151. }
  5152. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5153. [_dictionary setObject:@(value) forKey:@(key)];
  5154. if (_autocreator) {
  5155. GPBAutocreatedDictionaryModified(_autocreator, self);
  5156. }
  5157. }
  5158. - (void)removeValueForKey:(uint64_t)aKey {
  5159. [_dictionary removeObjectForKey:@(aKey)];
  5160. }
  5161. - (void)removeAll {
  5162. [_dictionary removeAllObjects];
  5163. }
  5164. @end
  5165. #pragma mark - UInt64 -> UInt64
  5166. @implementation GPBUInt64UInt64Dictionary {
  5167. @package
  5168. NSMutableDictionary *_dictionary;
  5169. }
  5170. + (instancetype)dictionary {
  5171. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5172. }
  5173. + (instancetype)dictionaryWithValue:(uint64_t)value
  5174. forKey:(uint64_t)key {
  5175. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5176. // on to get the type correct.
  5177. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  5178. forKeys:&key
  5179. count:1] autorelease];
  5180. }
  5181. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  5182. forKeys:(const uint64_t [])keys
  5183. count:(NSUInteger)count {
  5184. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5185. // on to get the type correct.
  5186. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  5187. forKeys:keys
  5188. count:count] autorelease];
  5189. }
  5190. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5191. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5192. // on to get the type correct.
  5193. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5194. }
  5195. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5196. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5197. }
  5198. - (instancetype)init {
  5199. return [self initWithValues:NULL forKeys:NULL count:0];
  5200. }
  5201. - (instancetype)initWithValues:(const uint64_t [])values
  5202. forKeys:(const uint64_t [])keys
  5203. count:(NSUInteger)count {
  5204. self = [super init];
  5205. if (self) {
  5206. _dictionary = [[NSMutableDictionary alloc] init];
  5207. if (count && values && keys) {
  5208. for (NSUInteger i = 0; i < count; ++i) {
  5209. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5210. }
  5211. }
  5212. }
  5213. return self;
  5214. }
  5215. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5216. self = [self initWithValues:NULL forKeys:NULL count:0];
  5217. if (self) {
  5218. if (dictionary) {
  5219. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5220. }
  5221. }
  5222. return self;
  5223. }
  5224. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5225. #pragma unused(numItems)
  5226. return [self initWithValues:NULL forKeys:NULL count:0];
  5227. }
  5228. - (void)dealloc {
  5229. NSAssert(!_autocreator,
  5230. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5231. [self class], _autocreator);
  5232. [_dictionary release];
  5233. [super dealloc];
  5234. }
  5235. - (instancetype)copyWithZone:(NSZone *)zone {
  5236. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5237. }
  5238. - (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
  5239. if (self == other) {
  5240. return YES;
  5241. }
  5242. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5243. return NO;
  5244. }
  5245. return [_dictionary isEqual:other->_dictionary];
  5246. }
  5247. - (NSUInteger)hash {
  5248. return _dictionary.count;
  5249. }
  5250. - (NSString *)description {
  5251. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5252. }
  5253. - (NSUInteger)count {
  5254. return _dictionary.count;
  5255. }
  5256. - (void)enumerateKeysAndValuesUsingBlock:
  5257. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5258. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5259. NSNumber *aValue,
  5260. BOOL *stop) {
  5261. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5262. }];
  5263. }
  5264. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5265. NSUInteger count = _dictionary.count;
  5266. if (count == 0) {
  5267. return 0;
  5268. }
  5269. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5270. GPBDataType keyDataType = field.mapKeyDataType;
  5271. __block size_t result = 0;
  5272. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5273. NSNumber *aValue,
  5274. BOOL *stop) {
  5275. #pragma unused(stop)
  5276. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5277. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5278. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5279. }];
  5280. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5281. result += tagSize * count;
  5282. return result;
  5283. }
  5284. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5285. asField:(GPBFieldDescriptor *)field {
  5286. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5287. GPBDataType keyDataType = field.mapKeyDataType;
  5288. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5289. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5290. NSNumber *aValue,
  5291. BOOL *stop) {
  5292. #pragma unused(stop)
  5293. // Write the tag.
  5294. [outputStream writeInt32NoTag:tag];
  5295. // Write the size of the message.
  5296. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5297. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5298. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5299. // Write the fields.
  5300. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5301. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5302. }];
  5303. }
  5304. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5305. forGPBGenericValueKey:(GPBGenericValue *)key {
  5306. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5307. }
  5308. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5309. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5310. #pragma unused(stop)
  5311. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5312. }];
  5313. }
  5314. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  5315. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5316. if (wrapped && value) {
  5317. *value = [wrapped unsignedLongLongValue];
  5318. }
  5319. return (wrapped != NULL);
  5320. }
  5321. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5322. if (otherDictionary) {
  5323. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5324. if (_autocreator) {
  5325. GPBAutocreatedDictionaryModified(_autocreator, self);
  5326. }
  5327. }
  5328. }
  5329. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5330. [_dictionary setObject:@(value) forKey:@(key)];
  5331. if (_autocreator) {
  5332. GPBAutocreatedDictionaryModified(_autocreator, self);
  5333. }
  5334. }
  5335. - (void)removeValueForKey:(uint64_t)aKey {
  5336. [_dictionary removeObjectForKey:@(aKey)];
  5337. }
  5338. - (void)removeAll {
  5339. [_dictionary removeAllObjects];
  5340. }
  5341. @end
  5342. #pragma mark - UInt64 -> Int64
  5343. @implementation GPBUInt64Int64Dictionary {
  5344. @package
  5345. NSMutableDictionary *_dictionary;
  5346. }
  5347. + (instancetype)dictionary {
  5348. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5349. }
  5350. + (instancetype)dictionaryWithValue:(int64_t)value
  5351. forKey:(uint64_t)key {
  5352. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5353. // on to get the type correct.
  5354. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5355. forKeys:&key
  5356. count:1] autorelease];
  5357. }
  5358. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5359. forKeys:(const uint64_t [])keys
  5360. count:(NSUInteger)count {
  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 [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5364. forKeys:keys
  5365. count:count] autorelease];
  5366. }
  5367. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5368. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5369. // on to get the type correct.
  5370. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5371. }
  5372. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5373. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5374. }
  5375. - (instancetype)init {
  5376. return [self initWithValues:NULL forKeys:NULL count:0];
  5377. }
  5378. - (instancetype)initWithValues:(const int64_t [])values
  5379. forKeys:(const uint64_t [])keys
  5380. count:(NSUInteger)count {
  5381. self = [super init];
  5382. if (self) {
  5383. _dictionary = [[NSMutableDictionary alloc] init];
  5384. if (count && values && keys) {
  5385. for (NSUInteger i = 0; i < count; ++i) {
  5386. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5387. }
  5388. }
  5389. }
  5390. return self;
  5391. }
  5392. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5393. self = [self initWithValues:NULL forKeys:NULL count:0];
  5394. if (self) {
  5395. if (dictionary) {
  5396. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5397. }
  5398. }
  5399. return self;
  5400. }
  5401. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5402. #pragma unused(numItems)
  5403. return [self initWithValues:NULL forKeys:NULL count:0];
  5404. }
  5405. - (void)dealloc {
  5406. NSAssert(!_autocreator,
  5407. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5408. [self class], _autocreator);
  5409. [_dictionary release];
  5410. [super dealloc];
  5411. }
  5412. - (instancetype)copyWithZone:(NSZone *)zone {
  5413. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5414. }
  5415. - (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
  5416. if (self == other) {
  5417. return YES;
  5418. }
  5419. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5420. return NO;
  5421. }
  5422. return [_dictionary isEqual:other->_dictionary];
  5423. }
  5424. - (NSUInteger)hash {
  5425. return _dictionary.count;
  5426. }
  5427. - (NSString *)description {
  5428. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5429. }
  5430. - (NSUInteger)count {
  5431. return _dictionary.count;
  5432. }
  5433. - (void)enumerateKeysAndValuesUsingBlock:
  5434. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5435. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5436. NSNumber *aValue,
  5437. BOOL *stop) {
  5438. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5439. }];
  5440. }
  5441. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5442. NSUInteger count = _dictionary.count;
  5443. if (count == 0) {
  5444. return 0;
  5445. }
  5446. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5447. GPBDataType keyDataType = field.mapKeyDataType;
  5448. __block size_t result = 0;
  5449. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5450. NSNumber *aValue,
  5451. BOOL *stop) {
  5452. #pragma unused(stop)
  5453. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5454. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5455. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5456. }];
  5457. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5458. result += tagSize * count;
  5459. return result;
  5460. }
  5461. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5462. asField:(GPBFieldDescriptor *)field {
  5463. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5464. GPBDataType keyDataType = field.mapKeyDataType;
  5465. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5466. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5467. NSNumber *aValue,
  5468. BOOL *stop) {
  5469. #pragma unused(stop)
  5470. // Write the tag.
  5471. [outputStream writeInt32NoTag:tag];
  5472. // Write the size of the message.
  5473. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5474. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5475. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5476. // Write the fields.
  5477. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5478. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5479. }];
  5480. }
  5481. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5482. forGPBGenericValueKey:(GPBGenericValue *)key {
  5483. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5484. }
  5485. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5486. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5487. #pragma unused(stop)
  5488. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5489. }];
  5490. }
  5491. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5492. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5493. if (wrapped && value) {
  5494. *value = [wrapped longLongValue];
  5495. }
  5496. return (wrapped != NULL);
  5497. }
  5498. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5499. if (otherDictionary) {
  5500. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5501. if (_autocreator) {
  5502. GPBAutocreatedDictionaryModified(_autocreator, self);
  5503. }
  5504. }
  5505. }
  5506. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5507. [_dictionary setObject:@(value) forKey:@(key)];
  5508. if (_autocreator) {
  5509. GPBAutocreatedDictionaryModified(_autocreator, self);
  5510. }
  5511. }
  5512. - (void)removeValueForKey:(uint64_t)aKey {
  5513. [_dictionary removeObjectForKey:@(aKey)];
  5514. }
  5515. - (void)removeAll {
  5516. [_dictionary removeAllObjects];
  5517. }
  5518. @end
  5519. #pragma mark - UInt64 -> Bool
  5520. @implementation GPBUInt64BoolDictionary {
  5521. @package
  5522. NSMutableDictionary *_dictionary;
  5523. }
  5524. + (instancetype)dictionary {
  5525. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5526. }
  5527. + (instancetype)dictionaryWithValue:(BOOL)value
  5528. forKey:(uint64_t)key {
  5529. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5530. // on to get the type correct.
  5531. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5532. forKeys:&key
  5533. count:1] autorelease];
  5534. }
  5535. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5536. forKeys:(const uint64_t [])keys
  5537. count:(NSUInteger)count {
  5538. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5539. // on to get the type correct.
  5540. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5541. forKeys:keys
  5542. count:count] autorelease];
  5543. }
  5544. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5545. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5546. // on to get the type correct.
  5547. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5548. }
  5549. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5550. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5551. }
  5552. - (instancetype)init {
  5553. return [self initWithValues:NULL forKeys:NULL count:0];
  5554. }
  5555. - (instancetype)initWithValues:(const BOOL [])values
  5556. forKeys:(const uint64_t [])keys
  5557. count:(NSUInteger)count {
  5558. self = [super init];
  5559. if (self) {
  5560. _dictionary = [[NSMutableDictionary alloc] init];
  5561. if (count && values && keys) {
  5562. for (NSUInteger i = 0; i < count; ++i) {
  5563. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5564. }
  5565. }
  5566. }
  5567. return self;
  5568. }
  5569. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5570. self = [self initWithValues:NULL forKeys:NULL count:0];
  5571. if (self) {
  5572. if (dictionary) {
  5573. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5574. }
  5575. }
  5576. return self;
  5577. }
  5578. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5579. #pragma unused(numItems)
  5580. return [self initWithValues:NULL forKeys:NULL count:0];
  5581. }
  5582. - (void)dealloc {
  5583. NSAssert(!_autocreator,
  5584. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5585. [self class], _autocreator);
  5586. [_dictionary release];
  5587. [super dealloc];
  5588. }
  5589. - (instancetype)copyWithZone:(NSZone *)zone {
  5590. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5591. }
  5592. - (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
  5593. if (self == other) {
  5594. return YES;
  5595. }
  5596. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5597. return NO;
  5598. }
  5599. return [_dictionary isEqual:other->_dictionary];
  5600. }
  5601. - (NSUInteger)hash {
  5602. return _dictionary.count;
  5603. }
  5604. - (NSString *)description {
  5605. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5606. }
  5607. - (NSUInteger)count {
  5608. return _dictionary.count;
  5609. }
  5610. - (void)enumerateKeysAndValuesUsingBlock:
  5611. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5612. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5613. NSNumber *aValue,
  5614. BOOL *stop) {
  5615. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5616. }];
  5617. }
  5618. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5619. NSUInteger count = _dictionary.count;
  5620. if (count == 0) {
  5621. return 0;
  5622. }
  5623. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5624. GPBDataType keyDataType = field.mapKeyDataType;
  5625. __block size_t result = 0;
  5626. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5627. NSNumber *aValue,
  5628. BOOL *stop) {
  5629. #pragma unused(stop)
  5630. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5631. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5632. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5633. }];
  5634. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5635. result += tagSize * count;
  5636. return result;
  5637. }
  5638. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5639. asField:(GPBFieldDescriptor *)field {
  5640. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5641. GPBDataType keyDataType = field.mapKeyDataType;
  5642. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5643. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5644. NSNumber *aValue,
  5645. BOOL *stop) {
  5646. #pragma unused(stop)
  5647. // Write the tag.
  5648. [outputStream writeInt32NoTag:tag];
  5649. // Write the size of the message.
  5650. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5651. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5652. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5653. // Write the fields.
  5654. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5655. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5656. }];
  5657. }
  5658. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5659. forGPBGenericValueKey:(GPBGenericValue *)key {
  5660. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5661. }
  5662. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5663. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5664. #pragma unused(stop)
  5665. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5666. }];
  5667. }
  5668. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5669. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5670. if (wrapped && value) {
  5671. *value = [wrapped boolValue];
  5672. }
  5673. return (wrapped != NULL);
  5674. }
  5675. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5676. if (otherDictionary) {
  5677. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5678. if (_autocreator) {
  5679. GPBAutocreatedDictionaryModified(_autocreator, self);
  5680. }
  5681. }
  5682. }
  5683. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5684. [_dictionary setObject:@(value) forKey:@(key)];
  5685. if (_autocreator) {
  5686. GPBAutocreatedDictionaryModified(_autocreator, self);
  5687. }
  5688. }
  5689. - (void)removeValueForKey:(uint64_t)aKey {
  5690. [_dictionary removeObjectForKey:@(aKey)];
  5691. }
  5692. - (void)removeAll {
  5693. [_dictionary removeAllObjects];
  5694. }
  5695. @end
  5696. #pragma mark - UInt64 -> Float
  5697. @implementation GPBUInt64FloatDictionary {
  5698. @package
  5699. NSMutableDictionary *_dictionary;
  5700. }
  5701. + (instancetype)dictionary {
  5702. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5703. }
  5704. + (instancetype)dictionaryWithValue:(float)value
  5705. forKey:(uint64_t)key {
  5706. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5707. // on to get the type correct.
  5708. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5709. forKeys:&key
  5710. count:1] autorelease];
  5711. }
  5712. + (instancetype)dictionaryWithValues:(const float [])values
  5713. forKeys:(const uint64_t [])keys
  5714. count:(NSUInteger)count {
  5715. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5716. // on to get the type correct.
  5717. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5718. forKeys:keys
  5719. count:count] autorelease];
  5720. }
  5721. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5722. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5723. // on to get the type correct.
  5724. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5725. }
  5726. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5727. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5728. }
  5729. - (instancetype)init {
  5730. return [self initWithValues:NULL forKeys:NULL count:0];
  5731. }
  5732. - (instancetype)initWithValues:(const float [])values
  5733. forKeys:(const uint64_t [])keys
  5734. count:(NSUInteger)count {
  5735. self = [super init];
  5736. if (self) {
  5737. _dictionary = [[NSMutableDictionary alloc] init];
  5738. if (count && values && keys) {
  5739. for (NSUInteger i = 0; i < count; ++i) {
  5740. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5741. }
  5742. }
  5743. }
  5744. return self;
  5745. }
  5746. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5747. self = [self initWithValues:NULL forKeys:NULL count:0];
  5748. if (self) {
  5749. if (dictionary) {
  5750. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5751. }
  5752. }
  5753. return self;
  5754. }
  5755. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5756. #pragma unused(numItems)
  5757. return [self initWithValues:NULL forKeys:NULL count:0];
  5758. }
  5759. - (void)dealloc {
  5760. NSAssert(!_autocreator,
  5761. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5762. [self class], _autocreator);
  5763. [_dictionary release];
  5764. [super dealloc];
  5765. }
  5766. - (instancetype)copyWithZone:(NSZone *)zone {
  5767. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5768. }
  5769. - (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
  5770. if (self == other) {
  5771. return YES;
  5772. }
  5773. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5774. return NO;
  5775. }
  5776. return [_dictionary isEqual:other->_dictionary];
  5777. }
  5778. - (NSUInteger)hash {
  5779. return _dictionary.count;
  5780. }
  5781. - (NSString *)description {
  5782. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5783. }
  5784. - (NSUInteger)count {
  5785. return _dictionary.count;
  5786. }
  5787. - (void)enumerateKeysAndValuesUsingBlock:
  5788. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5789. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5790. NSNumber *aValue,
  5791. BOOL *stop) {
  5792. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5793. }];
  5794. }
  5795. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5796. NSUInteger count = _dictionary.count;
  5797. if (count == 0) {
  5798. return 0;
  5799. }
  5800. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5801. GPBDataType keyDataType = field.mapKeyDataType;
  5802. __block size_t result = 0;
  5803. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5804. NSNumber *aValue,
  5805. BOOL *stop) {
  5806. #pragma unused(stop)
  5807. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5808. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5809. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5810. }];
  5811. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5812. result += tagSize * count;
  5813. return result;
  5814. }
  5815. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5816. asField:(GPBFieldDescriptor *)field {
  5817. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5818. GPBDataType keyDataType = field.mapKeyDataType;
  5819. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5820. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5821. NSNumber *aValue,
  5822. BOOL *stop) {
  5823. #pragma unused(stop)
  5824. // Write the tag.
  5825. [outputStream writeInt32NoTag:tag];
  5826. // Write the size of the message.
  5827. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5828. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5829. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5830. // Write the fields.
  5831. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5832. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5833. }];
  5834. }
  5835. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5836. forGPBGenericValueKey:(GPBGenericValue *)key {
  5837. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5838. }
  5839. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5840. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5841. #pragma unused(stop)
  5842. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5843. }];
  5844. }
  5845. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5846. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5847. if (wrapped && value) {
  5848. *value = [wrapped floatValue];
  5849. }
  5850. return (wrapped != NULL);
  5851. }
  5852. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5853. if (otherDictionary) {
  5854. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5855. if (_autocreator) {
  5856. GPBAutocreatedDictionaryModified(_autocreator, self);
  5857. }
  5858. }
  5859. }
  5860. - (void)setValue:(float)value forKey:(uint64_t)key {
  5861. [_dictionary setObject:@(value) forKey:@(key)];
  5862. if (_autocreator) {
  5863. GPBAutocreatedDictionaryModified(_autocreator, self);
  5864. }
  5865. }
  5866. - (void)removeValueForKey:(uint64_t)aKey {
  5867. [_dictionary removeObjectForKey:@(aKey)];
  5868. }
  5869. - (void)removeAll {
  5870. [_dictionary removeAllObjects];
  5871. }
  5872. @end
  5873. #pragma mark - UInt64 -> Double
  5874. @implementation GPBUInt64DoubleDictionary {
  5875. @package
  5876. NSMutableDictionary *_dictionary;
  5877. }
  5878. + (instancetype)dictionary {
  5879. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5880. }
  5881. + (instancetype)dictionaryWithValue:(double)value
  5882. forKey:(uint64_t)key {
  5883. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5884. // on to get the type correct.
  5885. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5886. forKeys:&key
  5887. count:1] autorelease];
  5888. }
  5889. + (instancetype)dictionaryWithValues:(const double [])values
  5890. forKeys:(const uint64_t [])keys
  5891. count:(NSUInteger)count {
  5892. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5893. // on to get the type correct.
  5894. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5895. forKeys:keys
  5896. count:count] autorelease];
  5897. }
  5898. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5899. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5900. // on to get the type correct.
  5901. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5902. }
  5903. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5904. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5905. }
  5906. - (instancetype)init {
  5907. return [self initWithValues:NULL forKeys:NULL count:0];
  5908. }
  5909. - (instancetype)initWithValues:(const double [])values
  5910. forKeys:(const uint64_t [])keys
  5911. count:(NSUInteger)count {
  5912. self = [super init];
  5913. if (self) {
  5914. _dictionary = [[NSMutableDictionary alloc] init];
  5915. if (count && values && keys) {
  5916. for (NSUInteger i = 0; i < count; ++i) {
  5917. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5918. }
  5919. }
  5920. }
  5921. return self;
  5922. }
  5923. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5924. self = [self initWithValues:NULL forKeys:NULL count:0];
  5925. if (self) {
  5926. if (dictionary) {
  5927. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5928. }
  5929. }
  5930. return self;
  5931. }
  5932. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5933. #pragma unused(numItems)
  5934. return [self initWithValues:NULL forKeys:NULL count:0];
  5935. }
  5936. - (void)dealloc {
  5937. NSAssert(!_autocreator,
  5938. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5939. [self class], _autocreator);
  5940. [_dictionary release];
  5941. [super dealloc];
  5942. }
  5943. - (instancetype)copyWithZone:(NSZone *)zone {
  5944. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5945. }
  5946. - (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
  5947. if (self == other) {
  5948. return YES;
  5949. }
  5950. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5951. return NO;
  5952. }
  5953. return [_dictionary isEqual:other->_dictionary];
  5954. }
  5955. - (NSUInteger)hash {
  5956. return _dictionary.count;
  5957. }
  5958. - (NSString *)description {
  5959. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5960. }
  5961. - (NSUInteger)count {
  5962. return _dictionary.count;
  5963. }
  5964. - (void)enumerateKeysAndValuesUsingBlock:
  5965. (void (^)(uint64_t key, double value, BOOL *stop))block {
  5966. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5967. NSNumber *aValue,
  5968. BOOL *stop) {
  5969. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  5970. }];
  5971. }
  5972. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5973. NSUInteger count = _dictionary.count;
  5974. if (count == 0) {
  5975. return 0;
  5976. }
  5977. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5978. GPBDataType keyDataType = field.mapKeyDataType;
  5979. __block size_t result = 0;
  5980. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5981. NSNumber *aValue,
  5982. BOOL *stop) {
  5983. #pragma unused(stop)
  5984. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5985. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5986. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5987. }];
  5988. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5989. result += tagSize * count;
  5990. return result;
  5991. }
  5992. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5993. asField:(GPBFieldDescriptor *)field {
  5994. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5995. GPBDataType keyDataType = field.mapKeyDataType;
  5996. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5997. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5998. NSNumber *aValue,
  5999. BOOL *stop) {
  6000. #pragma unused(stop)
  6001. // Write the tag.
  6002. [outputStream writeInt32NoTag:tag];
  6003. // Write the size of the message.
  6004. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6005. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6006. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6007. // Write the fields.
  6008. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6009. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6010. }];
  6011. }
  6012. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6013. forGPBGenericValueKey:(GPBGenericValue *)key {
  6014. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  6015. }
  6016. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6017. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  6018. #pragma unused(stop)
  6019. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6020. }];
  6021. }
  6022. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  6023. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6024. if (wrapped && value) {
  6025. *value = [wrapped doubleValue];
  6026. }
  6027. return (wrapped != NULL);
  6028. }
  6029. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  6030. if (otherDictionary) {
  6031. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6032. if (_autocreator) {
  6033. GPBAutocreatedDictionaryModified(_autocreator, self);
  6034. }
  6035. }
  6036. }
  6037. - (void)setValue:(double)value forKey:(uint64_t)key {
  6038. [_dictionary setObject:@(value) forKey:@(key)];
  6039. if (_autocreator) {
  6040. GPBAutocreatedDictionaryModified(_autocreator, self);
  6041. }
  6042. }
  6043. - (void)removeValueForKey:(uint64_t)aKey {
  6044. [_dictionary removeObjectForKey:@(aKey)];
  6045. }
  6046. - (void)removeAll {
  6047. [_dictionary removeAllObjects];
  6048. }
  6049. @end
  6050. #pragma mark - UInt64 -> Enum
  6051. @implementation GPBUInt64EnumDictionary {
  6052. @package
  6053. NSMutableDictionary *_dictionary;
  6054. GPBEnumValidationFunc _validationFunc;
  6055. }
  6056. @synthesize validationFunc = _validationFunc;
  6057. + (instancetype)dictionary {
  6058. return [[[self alloc] initWithValidationFunction:NULL
  6059. rawValues:NULL
  6060. forKeys:NULL
  6061. count:0] autorelease];
  6062. }
  6063. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6064. return [[[self alloc] initWithValidationFunction:func
  6065. rawValues:NULL
  6066. forKeys:NULL
  6067. count:0] autorelease];
  6068. }
  6069. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6070. rawValue:(int32_t)rawValue
  6071. forKey:(uint64_t)key {
  6072. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6073. // on to get the type correct.
  6074. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6075. rawValues:&rawValue
  6076. forKeys:&key
  6077. count:1] autorelease];
  6078. }
  6079. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6080. rawValues:(const int32_t [])rawValues
  6081. forKeys:(const uint64_t [])keys
  6082. count:(NSUInteger)count {
  6083. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6084. // on to get the type correct.
  6085. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6086. rawValues:rawValues
  6087. forKeys:keys
  6088. count:count] autorelease];
  6089. }
  6090. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6091. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6092. // on to get the type correct.
  6093. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6094. }
  6095. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6096. capacity:(NSUInteger)numItems {
  6097. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6098. }
  6099. - (instancetype)init {
  6100. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6101. }
  6102. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6103. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6104. }
  6105. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6106. rawValues:(const int32_t [])rawValues
  6107. forKeys:(const uint64_t [])keys
  6108. count:(NSUInteger)count {
  6109. self = [super init];
  6110. if (self) {
  6111. _dictionary = [[NSMutableDictionary alloc] init];
  6112. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6113. if (count && rawValues && keys) {
  6114. for (NSUInteger i = 0; i < count; ++i) {
  6115. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6116. }
  6117. }
  6118. }
  6119. return self;
  6120. }
  6121. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6122. self = [self initWithValidationFunction:dictionary.validationFunc
  6123. rawValues:NULL
  6124. forKeys:NULL
  6125. count:0];
  6126. if (self) {
  6127. if (dictionary) {
  6128. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6129. }
  6130. }
  6131. return self;
  6132. }
  6133. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6134. capacity:(NSUInteger)numItems {
  6135. #pragma unused(numItems)
  6136. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6137. }
  6138. - (void)dealloc {
  6139. NSAssert(!_autocreator,
  6140. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6141. [self class], _autocreator);
  6142. [_dictionary release];
  6143. [super dealloc];
  6144. }
  6145. - (instancetype)copyWithZone:(NSZone *)zone {
  6146. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6147. }
  6148. - (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
  6149. if (self == other) {
  6150. return YES;
  6151. }
  6152. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6153. return NO;
  6154. }
  6155. return [_dictionary isEqual:other->_dictionary];
  6156. }
  6157. - (NSUInteger)hash {
  6158. return _dictionary.count;
  6159. }
  6160. - (NSString *)description {
  6161. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6162. }
  6163. - (NSUInteger)count {
  6164. return _dictionary.count;
  6165. }
  6166. - (void)enumerateKeysAndRawValuesUsingBlock:
  6167. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6168. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6169. NSNumber *aValue,
  6170. BOOL *stop) {
  6171. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6172. }];
  6173. }
  6174. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6175. NSUInteger count = _dictionary.count;
  6176. if (count == 0) {
  6177. return 0;
  6178. }
  6179. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6180. GPBDataType keyDataType = field.mapKeyDataType;
  6181. __block size_t result = 0;
  6182. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6183. NSNumber *aValue,
  6184. BOOL *stop) {
  6185. #pragma unused(stop)
  6186. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6187. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6188. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6189. }];
  6190. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6191. result += tagSize * count;
  6192. return result;
  6193. }
  6194. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6195. asField:(GPBFieldDescriptor *)field {
  6196. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6197. GPBDataType keyDataType = field.mapKeyDataType;
  6198. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6199. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6200. NSNumber *aValue,
  6201. BOOL *stop) {
  6202. #pragma unused(stop)
  6203. // Write the tag.
  6204. [outputStream writeInt32NoTag:tag];
  6205. // Write the size of the message.
  6206. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6207. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6208. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6209. // Write the fields.
  6210. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6211. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6212. }];
  6213. }
  6214. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6215. forKey:(GPBGenericValue *)key
  6216. keyDataType:(GPBDataType)keyDataType {
  6217. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6218. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6219. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6220. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6221. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6222. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6223. [outputStream release];
  6224. return data;
  6225. }
  6226. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6227. forGPBGenericValueKey:(GPBGenericValue *)key {
  6228. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6229. }
  6230. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6231. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6232. #pragma unused(stop)
  6233. block([NSString stringWithFormat:@"%llu", key], @(value));
  6234. }];
  6235. }
  6236. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  6237. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6238. if (wrapped && value) {
  6239. int32_t result = [wrapped intValue];
  6240. if (!_validationFunc(result)) {
  6241. result = kGPBUnrecognizedEnumeratorValue;
  6242. }
  6243. *value = result;
  6244. }
  6245. return (wrapped != NULL);
  6246. }
  6247. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  6248. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6249. if (wrapped && rawValue) {
  6250. *rawValue = [wrapped intValue];
  6251. }
  6252. return (wrapped != NULL);
  6253. }
  6254. - (void)enumerateKeysAndValuesUsingBlock:
  6255. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6256. GPBEnumValidationFunc func = _validationFunc;
  6257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6258. NSNumber *aValue,
  6259. BOOL *stop) {
  6260. int32_t unwrapped = [aValue intValue];
  6261. if (!func(unwrapped)) {
  6262. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6263. }
  6264. block([aKey unsignedLongLongValue], unwrapped, stop);
  6265. }];
  6266. }
  6267. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6268. if (otherDictionary) {
  6269. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6270. if (_autocreator) {
  6271. GPBAutocreatedDictionaryModified(_autocreator, self);
  6272. }
  6273. }
  6274. }
  6275. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6276. [_dictionary setObject:@(value) forKey:@(key)];
  6277. if (_autocreator) {
  6278. GPBAutocreatedDictionaryModified(_autocreator, self);
  6279. }
  6280. }
  6281. - (void)removeValueForKey:(uint64_t)aKey {
  6282. [_dictionary removeObjectForKey:@(aKey)];
  6283. }
  6284. - (void)removeAll {
  6285. [_dictionary removeAllObjects];
  6286. }
  6287. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  6288. if (!_validationFunc(value)) {
  6289. [NSException raise:NSInvalidArgumentException
  6290. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6291. value];
  6292. }
  6293. [_dictionary setObject:@(value) forKey:@(key)];
  6294. if (_autocreator) {
  6295. GPBAutocreatedDictionaryModified(_autocreator, self);
  6296. }
  6297. }
  6298. @end
  6299. #pragma mark - UInt64 -> Object
  6300. @implementation GPBUInt64ObjectDictionary {
  6301. @package
  6302. NSMutableDictionary *_dictionary;
  6303. }
  6304. + (instancetype)dictionary {
  6305. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6306. }
  6307. + (instancetype)dictionaryWithObject:(id)object
  6308. forKey:(uint64_t)key {
  6309. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6310. // on to get the type correct.
  6311. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6312. forKeys:&key
  6313. count:1] autorelease];
  6314. }
  6315. + (instancetype)dictionaryWithObjects:(const id [])objects
  6316. forKeys:(const uint64_t [])keys
  6317. count:(NSUInteger)count {
  6318. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6319. // on to get the type correct.
  6320. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6321. forKeys:keys
  6322. count:count] autorelease];
  6323. }
  6324. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6325. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6326. // on to get the type correct.
  6327. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6328. }
  6329. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6330. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6331. }
  6332. - (instancetype)init {
  6333. return [self initWithObjects:NULL forKeys:NULL count:0];
  6334. }
  6335. - (instancetype)initWithObjects:(const id [])objects
  6336. forKeys:(const uint64_t [])keys
  6337. count:(NSUInteger)count {
  6338. self = [super init];
  6339. if (self) {
  6340. _dictionary = [[NSMutableDictionary alloc] init];
  6341. if (count && objects && keys) {
  6342. for (NSUInteger i = 0; i < count; ++i) {
  6343. if (!objects[i]) {
  6344. [NSException raise:NSInvalidArgumentException
  6345. format:@"Attempting to add nil object to a Dictionary"];
  6346. }
  6347. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6348. }
  6349. }
  6350. }
  6351. return self;
  6352. }
  6353. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6354. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6355. if (self) {
  6356. if (dictionary) {
  6357. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6358. }
  6359. }
  6360. return self;
  6361. }
  6362. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6363. #pragma unused(numItems)
  6364. return [self initWithObjects:NULL forKeys:NULL count:0];
  6365. }
  6366. - (void)dealloc {
  6367. NSAssert(!_autocreator,
  6368. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6369. [self class], _autocreator);
  6370. [_dictionary release];
  6371. [super dealloc];
  6372. }
  6373. - (instancetype)copyWithZone:(NSZone *)zone {
  6374. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6375. }
  6376. - (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
  6377. if (self == other) {
  6378. return YES;
  6379. }
  6380. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6381. return NO;
  6382. }
  6383. return [_dictionary isEqual:other->_dictionary];
  6384. }
  6385. - (NSUInteger)hash {
  6386. return _dictionary.count;
  6387. }
  6388. - (NSString *)description {
  6389. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6390. }
  6391. - (NSUInteger)count {
  6392. return _dictionary.count;
  6393. }
  6394. - (void)enumerateKeysAndObjectsUsingBlock:
  6395. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6396. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6397. id aObject,
  6398. BOOL *stop) {
  6399. block([aKey unsignedLongLongValue], aObject, stop);
  6400. }];
  6401. }
  6402. - (BOOL)isInitialized {
  6403. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6404. if (!msg.initialized) {
  6405. return NO;
  6406. }
  6407. }
  6408. return YES;
  6409. }
  6410. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6411. GPBUInt64ObjectDictionary *newDict =
  6412. [[GPBUInt64ObjectDictionary alloc] init];
  6413. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6414. GPBMessage *msg,
  6415. BOOL *stop) {
  6416. #pragma unused(stop)
  6417. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6418. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6419. [copiedMsg release];
  6420. }];
  6421. return newDict;
  6422. }
  6423. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6424. NSUInteger count = _dictionary.count;
  6425. if (count == 0) {
  6426. return 0;
  6427. }
  6428. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6429. GPBDataType keyDataType = field.mapKeyDataType;
  6430. __block size_t result = 0;
  6431. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6432. id aObject,
  6433. BOOL *stop) {
  6434. #pragma unused(stop)
  6435. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6436. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6437. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6438. }];
  6439. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6440. result += tagSize * count;
  6441. return result;
  6442. }
  6443. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6444. asField:(GPBFieldDescriptor *)field {
  6445. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6446. GPBDataType keyDataType = field.mapKeyDataType;
  6447. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6448. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6449. id aObject,
  6450. BOOL *stop) {
  6451. #pragma unused(stop)
  6452. // Write the tag.
  6453. [outputStream writeInt32NoTag:tag];
  6454. // Write the size of the message.
  6455. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6456. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6457. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6458. // Write the fields.
  6459. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6460. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  6461. }];
  6462. }
  6463. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6464. forGPBGenericValueKey:(GPBGenericValue *)key {
  6465. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6466. }
  6467. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6468. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6469. #pragma unused(stop)
  6470. block([NSString stringWithFormat:@"%llu", key], object);
  6471. }];
  6472. }
  6473. - (id)objectForKey:(uint64_t)key {
  6474. id result = [_dictionary objectForKey:@(key)];
  6475. return result;
  6476. }
  6477. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6478. if (otherDictionary) {
  6479. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6480. if (_autocreator) {
  6481. GPBAutocreatedDictionaryModified(_autocreator, self);
  6482. }
  6483. }
  6484. }
  6485. - (void)setObject:(id)object forKey:(uint64_t)key {
  6486. if (!object) {
  6487. [NSException raise:NSInvalidArgumentException
  6488. format:@"Attempting to add nil object to a Dictionary"];
  6489. }
  6490. [_dictionary setObject:object forKey:@(key)];
  6491. if (_autocreator) {
  6492. GPBAutocreatedDictionaryModified(_autocreator, self);
  6493. }
  6494. }
  6495. - (void)removeObjectForKey:(uint64_t)aKey {
  6496. [_dictionary removeObjectForKey:@(aKey)];
  6497. }
  6498. - (void)removeAll {
  6499. [_dictionary removeAllObjects];
  6500. }
  6501. @end
  6502. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6503. // This block of code is generated, do not edit it directly.
  6504. #pragma mark - Int64 -> UInt32
  6505. @implementation GPBInt64UInt32Dictionary {
  6506. @package
  6507. NSMutableDictionary *_dictionary;
  6508. }
  6509. + (instancetype)dictionary {
  6510. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6511. }
  6512. + (instancetype)dictionaryWithValue:(uint32_t)value
  6513. forKey:(int64_t)key {
  6514. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6515. // on to get the type correct.
  6516. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6517. forKeys:&key
  6518. count:1] autorelease];
  6519. }
  6520. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6521. forKeys:(const int64_t [])keys
  6522. count:(NSUInteger)count {
  6523. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6524. // on to get the type correct.
  6525. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6526. forKeys:keys
  6527. count:count] autorelease];
  6528. }
  6529. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6530. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6531. // on to get the type correct.
  6532. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6533. }
  6534. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6535. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6536. }
  6537. - (instancetype)init {
  6538. return [self initWithValues:NULL forKeys:NULL count:0];
  6539. }
  6540. - (instancetype)initWithValues:(const uint32_t [])values
  6541. forKeys:(const int64_t [])keys
  6542. count:(NSUInteger)count {
  6543. self = [super init];
  6544. if (self) {
  6545. _dictionary = [[NSMutableDictionary alloc] init];
  6546. if (count && values && keys) {
  6547. for (NSUInteger i = 0; i < count; ++i) {
  6548. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6549. }
  6550. }
  6551. }
  6552. return self;
  6553. }
  6554. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6555. self = [self initWithValues:NULL forKeys:NULL count:0];
  6556. if (self) {
  6557. if (dictionary) {
  6558. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6559. }
  6560. }
  6561. return self;
  6562. }
  6563. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6564. #pragma unused(numItems)
  6565. return [self initWithValues:NULL forKeys:NULL count:0];
  6566. }
  6567. - (void)dealloc {
  6568. NSAssert(!_autocreator,
  6569. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6570. [self class], _autocreator);
  6571. [_dictionary release];
  6572. [super dealloc];
  6573. }
  6574. - (instancetype)copyWithZone:(NSZone *)zone {
  6575. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6576. }
  6577. - (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
  6578. if (self == other) {
  6579. return YES;
  6580. }
  6581. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6582. return NO;
  6583. }
  6584. return [_dictionary isEqual:other->_dictionary];
  6585. }
  6586. - (NSUInteger)hash {
  6587. return _dictionary.count;
  6588. }
  6589. - (NSString *)description {
  6590. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6591. }
  6592. - (NSUInteger)count {
  6593. return _dictionary.count;
  6594. }
  6595. - (void)enumerateKeysAndValuesUsingBlock:
  6596. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6597. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6598. NSNumber *aValue,
  6599. BOOL *stop) {
  6600. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6601. }];
  6602. }
  6603. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6604. NSUInteger count = _dictionary.count;
  6605. if (count == 0) {
  6606. return 0;
  6607. }
  6608. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6609. GPBDataType keyDataType = field.mapKeyDataType;
  6610. __block size_t result = 0;
  6611. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6612. NSNumber *aValue,
  6613. BOOL *stop) {
  6614. #pragma unused(stop)
  6615. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6616. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6617. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6618. }];
  6619. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6620. result += tagSize * count;
  6621. return result;
  6622. }
  6623. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6624. asField:(GPBFieldDescriptor *)field {
  6625. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6626. GPBDataType keyDataType = field.mapKeyDataType;
  6627. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6628. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6629. NSNumber *aValue,
  6630. BOOL *stop) {
  6631. #pragma unused(stop)
  6632. // Write the tag.
  6633. [outputStream writeInt32NoTag:tag];
  6634. // Write the size of the message.
  6635. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6636. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6637. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6638. // Write the fields.
  6639. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6640. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6641. }];
  6642. }
  6643. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6644. forGPBGenericValueKey:(GPBGenericValue *)key {
  6645. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6646. }
  6647. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6648. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6649. #pragma unused(stop)
  6650. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6651. }];
  6652. }
  6653. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6654. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6655. if (wrapped && value) {
  6656. *value = [wrapped unsignedIntValue];
  6657. }
  6658. return (wrapped != NULL);
  6659. }
  6660. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6661. if (otherDictionary) {
  6662. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6663. if (_autocreator) {
  6664. GPBAutocreatedDictionaryModified(_autocreator, self);
  6665. }
  6666. }
  6667. }
  6668. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6669. [_dictionary setObject:@(value) forKey:@(key)];
  6670. if (_autocreator) {
  6671. GPBAutocreatedDictionaryModified(_autocreator, self);
  6672. }
  6673. }
  6674. - (void)removeValueForKey:(int64_t)aKey {
  6675. [_dictionary removeObjectForKey:@(aKey)];
  6676. }
  6677. - (void)removeAll {
  6678. [_dictionary removeAllObjects];
  6679. }
  6680. @end
  6681. #pragma mark - Int64 -> Int32
  6682. @implementation GPBInt64Int32Dictionary {
  6683. @package
  6684. NSMutableDictionary *_dictionary;
  6685. }
  6686. + (instancetype)dictionary {
  6687. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6688. }
  6689. + (instancetype)dictionaryWithValue:(int32_t)value
  6690. forKey:(int64_t)key {
  6691. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6692. // on to get the type correct.
  6693. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6694. forKeys:&key
  6695. count:1] autorelease];
  6696. }
  6697. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6698. forKeys:(const int64_t [])keys
  6699. count:(NSUInteger)count {
  6700. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6701. // on to get the type correct.
  6702. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6703. forKeys:keys
  6704. count:count] autorelease];
  6705. }
  6706. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6707. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6708. // on to get the type correct.
  6709. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6710. }
  6711. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6712. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6713. }
  6714. - (instancetype)init {
  6715. return [self initWithValues:NULL forKeys:NULL count:0];
  6716. }
  6717. - (instancetype)initWithValues:(const int32_t [])values
  6718. forKeys:(const int64_t [])keys
  6719. count:(NSUInteger)count {
  6720. self = [super init];
  6721. if (self) {
  6722. _dictionary = [[NSMutableDictionary alloc] init];
  6723. if (count && values && keys) {
  6724. for (NSUInteger i = 0; i < count; ++i) {
  6725. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6726. }
  6727. }
  6728. }
  6729. return self;
  6730. }
  6731. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6732. self = [self initWithValues:NULL forKeys:NULL count:0];
  6733. if (self) {
  6734. if (dictionary) {
  6735. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6736. }
  6737. }
  6738. return self;
  6739. }
  6740. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6741. #pragma unused(numItems)
  6742. return [self initWithValues:NULL forKeys:NULL count:0];
  6743. }
  6744. - (void)dealloc {
  6745. NSAssert(!_autocreator,
  6746. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6747. [self class], _autocreator);
  6748. [_dictionary release];
  6749. [super dealloc];
  6750. }
  6751. - (instancetype)copyWithZone:(NSZone *)zone {
  6752. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6753. }
  6754. - (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
  6755. if (self == other) {
  6756. return YES;
  6757. }
  6758. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6759. return NO;
  6760. }
  6761. return [_dictionary isEqual:other->_dictionary];
  6762. }
  6763. - (NSUInteger)hash {
  6764. return _dictionary.count;
  6765. }
  6766. - (NSString *)description {
  6767. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6768. }
  6769. - (NSUInteger)count {
  6770. return _dictionary.count;
  6771. }
  6772. - (void)enumerateKeysAndValuesUsingBlock:
  6773. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6774. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6775. NSNumber *aValue,
  6776. BOOL *stop) {
  6777. block([aKey longLongValue], [aValue intValue], stop);
  6778. }];
  6779. }
  6780. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6781. NSUInteger count = _dictionary.count;
  6782. if (count == 0) {
  6783. return 0;
  6784. }
  6785. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6786. GPBDataType keyDataType = field.mapKeyDataType;
  6787. __block size_t result = 0;
  6788. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6789. NSNumber *aValue,
  6790. BOOL *stop) {
  6791. #pragma unused(stop)
  6792. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6793. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6794. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6795. }];
  6796. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6797. result += tagSize * count;
  6798. return result;
  6799. }
  6800. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6801. asField:(GPBFieldDescriptor *)field {
  6802. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6803. GPBDataType keyDataType = field.mapKeyDataType;
  6804. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6805. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6806. NSNumber *aValue,
  6807. BOOL *stop) {
  6808. #pragma unused(stop)
  6809. // Write the tag.
  6810. [outputStream writeInt32NoTag:tag];
  6811. // Write the size of the message.
  6812. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6813. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6814. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6815. // Write the fields.
  6816. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6817. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6818. }];
  6819. }
  6820. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6821. forGPBGenericValueKey:(GPBGenericValue *)key {
  6822. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6823. }
  6824. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6825. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6826. #pragma unused(stop)
  6827. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6828. }];
  6829. }
  6830. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6831. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6832. if (wrapped && value) {
  6833. *value = [wrapped intValue];
  6834. }
  6835. return (wrapped != NULL);
  6836. }
  6837. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6838. if (otherDictionary) {
  6839. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6840. if (_autocreator) {
  6841. GPBAutocreatedDictionaryModified(_autocreator, self);
  6842. }
  6843. }
  6844. }
  6845. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6846. [_dictionary setObject:@(value) forKey:@(key)];
  6847. if (_autocreator) {
  6848. GPBAutocreatedDictionaryModified(_autocreator, self);
  6849. }
  6850. }
  6851. - (void)removeValueForKey:(int64_t)aKey {
  6852. [_dictionary removeObjectForKey:@(aKey)];
  6853. }
  6854. - (void)removeAll {
  6855. [_dictionary removeAllObjects];
  6856. }
  6857. @end
  6858. #pragma mark - Int64 -> UInt64
  6859. @implementation GPBInt64UInt64Dictionary {
  6860. @package
  6861. NSMutableDictionary *_dictionary;
  6862. }
  6863. + (instancetype)dictionary {
  6864. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6865. }
  6866. + (instancetype)dictionaryWithValue:(uint64_t)value
  6867. forKey:(int64_t)key {
  6868. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6869. // on to get the type correct.
  6870. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6871. forKeys:&key
  6872. count:1] autorelease];
  6873. }
  6874. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6875. forKeys:(const int64_t [])keys
  6876. count:(NSUInteger)count {
  6877. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6878. // on to get the type correct.
  6879. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6880. forKeys:keys
  6881. count:count] autorelease];
  6882. }
  6883. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6884. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6885. // on to get the type correct.
  6886. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6887. }
  6888. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6889. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6890. }
  6891. - (instancetype)init {
  6892. return [self initWithValues:NULL forKeys:NULL count:0];
  6893. }
  6894. - (instancetype)initWithValues:(const uint64_t [])values
  6895. forKeys:(const int64_t [])keys
  6896. count:(NSUInteger)count {
  6897. self = [super init];
  6898. if (self) {
  6899. _dictionary = [[NSMutableDictionary alloc] init];
  6900. if (count && values && keys) {
  6901. for (NSUInteger i = 0; i < count; ++i) {
  6902. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6903. }
  6904. }
  6905. }
  6906. return self;
  6907. }
  6908. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6909. self = [self initWithValues:NULL forKeys:NULL count:0];
  6910. if (self) {
  6911. if (dictionary) {
  6912. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6913. }
  6914. }
  6915. return self;
  6916. }
  6917. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6918. #pragma unused(numItems)
  6919. return [self initWithValues:NULL forKeys:NULL count:0];
  6920. }
  6921. - (void)dealloc {
  6922. NSAssert(!_autocreator,
  6923. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6924. [self class], _autocreator);
  6925. [_dictionary release];
  6926. [super dealloc];
  6927. }
  6928. - (instancetype)copyWithZone:(NSZone *)zone {
  6929. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6930. }
  6931. - (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
  6932. if (self == other) {
  6933. return YES;
  6934. }
  6935. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6936. return NO;
  6937. }
  6938. return [_dictionary isEqual:other->_dictionary];
  6939. }
  6940. - (NSUInteger)hash {
  6941. return _dictionary.count;
  6942. }
  6943. - (NSString *)description {
  6944. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6945. }
  6946. - (NSUInteger)count {
  6947. return _dictionary.count;
  6948. }
  6949. - (void)enumerateKeysAndValuesUsingBlock:
  6950. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  6951. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6952. NSNumber *aValue,
  6953. BOOL *stop) {
  6954. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  6955. }];
  6956. }
  6957. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6958. NSUInteger count = _dictionary.count;
  6959. if (count == 0) {
  6960. return 0;
  6961. }
  6962. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6963. GPBDataType keyDataType = field.mapKeyDataType;
  6964. __block size_t result = 0;
  6965. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6966. NSNumber *aValue,
  6967. BOOL *stop) {
  6968. #pragma unused(stop)
  6969. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6970. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6971. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6972. }];
  6973. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6974. result += tagSize * count;
  6975. return result;
  6976. }
  6977. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6978. asField:(GPBFieldDescriptor *)field {
  6979. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6980. GPBDataType keyDataType = field.mapKeyDataType;
  6981. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6982. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6983. NSNumber *aValue,
  6984. BOOL *stop) {
  6985. #pragma unused(stop)
  6986. // Write the tag.
  6987. [outputStream writeInt32NoTag:tag];
  6988. // Write the size of the message.
  6989. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6990. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6991. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6992. // Write the fields.
  6993. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6994. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6995. }];
  6996. }
  6997. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6998. forGPBGenericValueKey:(GPBGenericValue *)key {
  6999. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  7000. }
  7001. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7002. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  7003. #pragma unused(stop)
  7004. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  7005. }];
  7006. }
  7007. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  7008. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7009. if (wrapped && value) {
  7010. *value = [wrapped unsignedLongLongValue];
  7011. }
  7012. return (wrapped != NULL);
  7013. }
  7014. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  7015. if (otherDictionary) {
  7016. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7017. if (_autocreator) {
  7018. GPBAutocreatedDictionaryModified(_autocreator, self);
  7019. }
  7020. }
  7021. }
  7022. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  7023. [_dictionary setObject:@(value) forKey:@(key)];
  7024. if (_autocreator) {
  7025. GPBAutocreatedDictionaryModified(_autocreator, self);
  7026. }
  7027. }
  7028. - (void)removeValueForKey:(int64_t)aKey {
  7029. [_dictionary removeObjectForKey:@(aKey)];
  7030. }
  7031. - (void)removeAll {
  7032. [_dictionary removeAllObjects];
  7033. }
  7034. @end
  7035. #pragma mark - Int64 -> Int64
  7036. @implementation GPBInt64Int64Dictionary {
  7037. @package
  7038. NSMutableDictionary *_dictionary;
  7039. }
  7040. + (instancetype)dictionary {
  7041. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7042. }
  7043. + (instancetype)dictionaryWithValue:(int64_t)value
  7044. forKey:(int64_t)key {
  7045. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7046. // on to get the type correct.
  7047. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  7048. forKeys:&key
  7049. count:1] autorelease];
  7050. }
  7051. + (instancetype)dictionaryWithValues:(const int64_t [])values
  7052. forKeys:(const int64_t [])keys
  7053. count:(NSUInteger)count {
  7054. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7055. // on to get the type correct.
  7056. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  7057. forKeys:keys
  7058. count:count] autorelease];
  7059. }
  7060. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7061. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7062. // on to get the type correct.
  7063. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7064. }
  7065. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7066. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7067. }
  7068. - (instancetype)init {
  7069. return [self initWithValues:NULL forKeys:NULL count:0];
  7070. }
  7071. - (instancetype)initWithValues:(const int64_t [])values
  7072. forKeys:(const int64_t [])keys
  7073. count:(NSUInteger)count {
  7074. self = [super init];
  7075. if (self) {
  7076. _dictionary = [[NSMutableDictionary alloc] init];
  7077. if (count && values && keys) {
  7078. for (NSUInteger i = 0; i < count; ++i) {
  7079. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7080. }
  7081. }
  7082. }
  7083. return self;
  7084. }
  7085. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7086. self = [self initWithValues:NULL forKeys:NULL count:0];
  7087. if (self) {
  7088. if (dictionary) {
  7089. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7090. }
  7091. }
  7092. return self;
  7093. }
  7094. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7095. #pragma unused(numItems)
  7096. return [self initWithValues:NULL forKeys:NULL count:0];
  7097. }
  7098. - (void)dealloc {
  7099. NSAssert(!_autocreator,
  7100. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7101. [self class], _autocreator);
  7102. [_dictionary release];
  7103. [super dealloc];
  7104. }
  7105. - (instancetype)copyWithZone:(NSZone *)zone {
  7106. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7107. }
  7108. - (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
  7109. if (self == other) {
  7110. return YES;
  7111. }
  7112. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7113. return NO;
  7114. }
  7115. return [_dictionary isEqual:other->_dictionary];
  7116. }
  7117. - (NSUInteger)hash {
  7118. return _dictionary.count;
  7119. }
  7120. - (NSString *)description {
  7121. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7122. }
  7123. - (NSUInteger)count {
  7124. return _dictionary.count;
  7125. }
  7126. - (void)enumerateKeysAndValuesUsingBlock:
  7127. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7128. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7129. NSNumber *aValue,
  7130. BOOL *stop) {
  7131. block([aKey longLongValue], [aValue longLongValue], stop);
  7132. }];
  7133. }
  7134. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7135. NSUInteger count = _dictionary.count;
  7136. if (count == 0) {
  7137. return 0;
  7138. }
  7139. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7140. GPBDataType keyDataType = field.mapKeyDataType;
  7141. __block size_t result = 0;
  7142. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7143. NSNumber *aValue,
  7144. BOOL *stop) {
  7145. #pragma unused(stop)
  7146. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7147. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7148. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7149. }];
  7150. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7151. result += tagSize * count;
  7152. return result;
  7153. }
  7154. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7155. asField:(GPBFieldDescriptor *)field {
  7156. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7157. GPBDataType keyDataType = field.mapKeyDataType;
  7158. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7159. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7160. NSNumber *aValue,
  7161. BOOL *stop) {
  7162. #pragma unused(stop)
  7163. // Write the tag.
  7164. [outputStream writeInt32NoTag:tag];
  7165. // Write the size of the message.
  7166. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7167. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7168. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7169. // Write the fields.
  7170. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7171. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7172. }];
  7173. }
  7174. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7175. forGPBGenericValueKey:(GPBGenericValue *)key {
  7176. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7177. }
  7178. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7179. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7180. #pragma unused(stop)
  7181. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7182. }];
  7183. }
  7184. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  7185. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7186. if (wrapped && value) {
  7187. *value = [wrapped longLongValue];
  7188. }
  7189. return (wrapped != NULL);
  7190. }
  7191. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7192. if (otherDictionary) {
  7193. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7194. if (_autocreator) {
  7195. GPBAutocreatedDictionaryModified(_autocreator, self);
  7196. }
  7197. }
  7198. }
  7199. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  7200. [_dictionary setObject:@(value) forKey:@(key)];
  7201. if (_autocreator) {
  7202. GPBAutocreatedDictionaryModified(_autocreator, self);
  7203. }
  7204. }
  7205. - (void)removeValueForKey:(int64_t)aKey {
  7206. [_dictionary removeObjectForKey:@(aKey)];
  7207. }
  7208. - (void)removeAll {
  7209. [_dictionary removeAllObjects];
  7210. }
  7211. @end
  7212. #pragma mark - Int64 -> Bool
  7213. @implementation GPBInt64BoolDictionary {
  7214. @package
  7215. NSMutableDictionary *_dictionary;
  7216. }
  7217. + (instancetype)dictionary {
  7218. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7219. }
  7220. + (instancetype)dictionaryWithValue:(BOOL)value
  7221. forKey:(int64_t)key {
  7222. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7223. // on to get the type correct.
  7224. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  7225. forKeys:&key
  7226. count:1] autorelease];
  7227. }
  7228. + (instancetype)dictionaryWithValues:(const BOOL [])values
  7229. forKeys:(const int64_t [])keys
  7230. count:(NSUInteger)count {
  7231. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7232. // on to get the type correct.
  7233. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  7234. forKeys:keys
  7235. count:count] autorelease];
  7236. }
  7237. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7238. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7239. // on to get the type correct.
  7240. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7241. }
  7242. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7243. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7244. }
  7245. - (instancetype)init {
  7246. return [self initWithValues:NULL forKeys:NULL count:0];
  7247. }
  7248. - (instancetype)initWithValues:(const BOOL [])values
  7249. forKeys:(const int64_t [])keys
  7250. count:(NSUInteger)count {
  7251. self = [super init];
  7252. if (self) {
  7253. _dictionary = [[NSMutableDictionary alloc] init];
  7254. if (count && values && keys) {
  7255. for (NSUInteger i = 0; i < count; ++i) {
  7256. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7257. }
  7258. }
  7259. }
  7260. return self;
  7261. }
  7262. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7263. self = [self initWithValues:NULL forKeys:NULL count:0];
  7264. if (self) {
  7265. if (dictionary) {
  7266. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7267. }
  7268. }
  7269. return self;
  7270. }
  7271. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7272. #pragma unused(numItems)
  7273. return [self initWithValues:NULL forKeys:NULL count:0];
  7274. }
  7275. - (void)dealloc {
  7276. NSAssert(!_autocreator,
  7277. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7278. [self class], _autocreator);
  7279. [_dictionary release];
  7280. [super dealloc];
  7281. }
  7282. - (instancetype)copyWithZone:(NSZone *)zone {
  7283. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7284. }
  7285. - (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
  7286. if (self == other) {
  7287. return YES;
  7288. }
  7289. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7290. return NO;
  7291. }
  7292. return [_dictionary isEqual:other->_dictionary];
  7293. }
  7294. - (NSUInteger)hash {
  7295. return _dictionary.count;
  7296. }
  7297. - (NSString *)description {
  7298. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7299. }
  7300. - (NSUInteger)count {
  7301. return _dictionary.count;
  7302. }
  7303. - (void)enumerateKeysAndValuesUsingBlock:
  7304. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7305. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7306. NSNumber *aValue,
  7307. BOOL *stop) {
  7308. block([aKey longLongValue], [aValue boolValue], stop);
  7309. }];
  7310. }
  7311. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7312. NSUInteger count = _dictionary.count;
  7313. if (count == 0) {
  7314. return 0;
  7315. }
  7316. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7317. GPBDataType keyDataType = field.mapKeyDataType;
  7318. __block size_t result = 0;
  7319. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7320. NSNumber *aValue,
  7321. BOOL *stop) {
  7322. #pragma unused(stop)
  7323. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7324. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7325. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7326. }];
  7327. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7328. result += tagSize * count;
  7329. return result;
  7330. }
  7331. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7332. asField:(GPBFieldDescriptor *)field {
  7333. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7334. GPBDataType keyDataType = field.mapKeyDataType;
  7335. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7336. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7337. NSNumber *aValue,
  7338. BOOL *stop) {
  7339. #pragma unused(stop)
  7340. // Write the tag.
  7341. [outputStream writeInt32NoTag:tag];
  7342. // Write the size of the message.
  7343. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7344. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7345. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7346. // Write the fields.
  7347. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7348. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7349. }];
  7350. }
  7351. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7352. forGPBGenericValueKey:(GPBGenericValue *)key {
  7353. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7354. }
  7355. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7356. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7357. #pragma unused(stop)
  7358. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7359. }];
  7360. }
  7361. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  7362. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7363. if (wrapped && value) {
  7364. *value = [wrapped boolValue];
  7365. }
  7366. return (wrapped != NULL);
  7367. }
  7368. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7369. if (otherDictionary) {
  7370. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7371. if (_autocreator) {
  7372. GPBAutocreatedDictionaryModified(_autocreator, self);
  7373. }
  7374. }
  7375. }
  7376. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  7377. [_dictionary setObject:@(value) forKey:@(key)];
  7378. if (_autocreator) {
  7379. GPBAutocreatedDictionaryModified(_autocreator, self);
  7380. }
  7381. }
  7382. - (void)removeValueForKey:(int64_t)aKey {
  7383. [_dictionary removeObjectForKey:@(aKey)];
  7384. }
  7385. - (void)removeAll {
  7386. [_dictionary removeAllObjects];
  7387. }
  7388. @end
  7389. #pragma mark - Int64 -> Float
  7390. @implementation GPBInt64FloatDictionary {
  7391. @package
  7392. NSMutableDictionary *_dictionary;
  7393. }
  7394. + (instancetype)dictionary {
  7395. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7396. }
  7397. + (instancetype)dictionaryWithValue:(float)value
  7398. forKey:(int64_t)key {
  7399. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7400. // on to get the type correct.
  7401. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  7402. forKeys:&key
  7403. count:1] autorelease];
  7404. }
  7405. + (instancetype)dictionaryWithValues:(const float [])values
  7406. forKeys:(const int64_t [])keys
  7407. count:(NSUInteger)count {
  7408. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7409. // on to get the type correct.
  7410. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  7411. forKeys:keys
  7412. count:count] autorelease];
  7413. }
  7414. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7415. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7416. // on to get the type correct.
  7417. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7418. }
  7419. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7420. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7421. }
  7422. - (instancetype)init {
  7423. return [self initWithValues:NULL forKeys:NULL count:0];
  7424. }
  7425. - (instancetype)initWithValues:(const float [])values
  7426. forKeys:(const int64_t [])keys
  7427. count:(NSUInteger)count {
  7428. self = [super init];
  7429. if (self) {
  7430. _dictionary = [[NSMutableDictionary alloc] init];
  7431. if (count && values && keys) {
  7432. for (NSUInteger i = 0; i < count; ++i) {
  7433. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7434. }
  7435. }
  7436. }
  7437. return self;
  7438. }
  7439. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7440. self = [self initWithValues:NULL forKeys:NULL count:0];
  7441. if (self) {
  7442. if (dictionary) {
  7443. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7444. }
  7445. }
  7446. return self;
  7447. }
  7448. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7449. #pragma unused(numItems)
  7450. return [self initWithValues:NULL forKeys:NULL count:0];
  7451. }
  7452. - (void)dealloc {
  7453. NSAssert(!_autocreator,
  7454. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7455. [self class], _autocreator);
  7456. [_dictionary release];
  7457. [super dealloc];
  7458. }
  7459. - (instancetype)copyWithZone:(NSZone *)zone {
  7460. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7461. }
  7462. - (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
  7463. if (self == other) {
  7464. return YES;
  7465. }
  7466. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7467. return NO;
  7468. }
  7469. return [_dictionary isEqual:other->_dictionary];
  7470. }
  7471. - (NSUInteger)hash {
  7472. return _dictionary.count;
  7473. }
  7474. - (NSString *)description {
  7475. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7476. }
  7477. - (NSUInteger)count {
  7478. return _dictionary.count;
  7479. }
  7480. - (void)enumerateKeysAndValuesUsingBlock:
  7481. (void (^)(int64_t key, float value, BOOL *stop))block {
  7482. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7483. NSNumber *aValue,
  7484. BOOL *stop) {
  7485. block([aKey longLongValue], [aValue floatValue], stop);
  7486. }];
  7487. }
  7488. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7489. NSUInteger count = _dictionary.count;
  7490. if (count == 0) {
  7491. return 0;
  7492. }
  7493. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7494. GPBDataType keyDataType = field.mapKeyDataType;
  7495. __block size_t result = 0;
  7496. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7497. NSNumber *aValue,
  7498. BOOL *stop) {
  7499. #pragma unused(stop)
  7500. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7501. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7502. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7503. }];
  7504. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7505. result += tagSize * count;
  7506. return result;
  7507. }
  7508. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7509. asField:(GPBFieldDescriptor *)field {
  7510. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7511. GPBDataType keyDataType = field.mapKeyDataType;
  7512. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7513. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7514. NSNumber *aValue,
  7515. BOOL *stop) {
  7516. #pragma unused(stop)
  7517. // Write the tag.
  7518. [outputStream writeInt32NoTag:tag];
  7519. // Write the size of the message.
  7520. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7521. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7522. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7523. // Write the fields.
  7524. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7525. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7526. }];
  7527. }
  7528. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7529. forGPBGenericValueKey:(GPBGenericValue *)key {
  7530. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7531. }
  7532. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7533. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7534. #pragma unused(stop)
  7535. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7536. }];
  7537. }
  7538. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7539. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7540. if (wrapped && value) {
  7541. *value = [wrapped floatValue];
  7542. }
  7543. return (wrapped != NULL);
  7544. }
  7545. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7546. if (otherDictionary) {
  7547. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7548. if (_autocreator) {
  7549. GPBAutocreatedDictionaryModified(_autocreator, self);
  7550. }
  7551. }
  7552. }
  7553. - (void)setValue:(float)value forKey:(int64_t)key {
  7554. [_dictionary setObject:@(value) forKey:@(key)];
  7555. if (_autocreator) {
  7556. GPBAutocreatedDictionaryModified(_autocreator, self);
  7557. }
  7558. }
  7559. - (void)removeValueForKey:(int64_t)aKey {
  7560. [_dictionary removeObjectForKey:@(aKey)];
  7561. }
  7562. - (void)removeAll {
  7563. [_dictionary removeAllObjects];
  7564. }
  7565. @end
  7566. #pragma mark - Int64 -> Double
  7567. @implementation GPBInt64DoubleDictionary {
  7568. @package
  7569. NSMutableDictionary *_dictionary;
  7570. }
  7571. + (instancetype)dictionary {
  7572. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7573. }
  7574. + (instancetype)dictionaryWithValue:(double)value
  7575. forKey:(int64_t)key {
  7576. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7577. // on to get the type correct.
  7578. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7579. forKeys:&key
  7580. count:1] autorelease];
  7581. }
  7582. + (instancetype)dictionaryWithValues:(const double [])values
  7583. forKeys:(const int64_t [])keys
  7584. count:(NSUInteger)count {
  7585. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7586. // on to get the type correct.
  7587. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7588. forKeys:keys
  7589. count:count] autorelease];
  7590. }
  7591. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7592. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7593. // on to get the type correct.
  7594. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7595. }
  7596. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7597. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7598. }
  7599. - (instancetype)init {
  7600. return [self initWithValues:NULL forKeys:NULL count:0];
  7601. }
  7602. - (instancetype)initWithValues:(const double [])values
  7603. forKeys:(const int64_t [])keys
  7604. count:(NSUInteger)count {
  7605. self = [super init];
  7606. if (self) {
  7607. _dictionary = [[NSMutableDictionary alloc] init];
  7608. if (count && values && keys) {
  7609. for (NSUInteger i = 0; i < count; ++i) {
  7610. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7611. }
  7612. }
  7613. }
  7614. return self;
  7615. }
  7616. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7617. self = [self initWithValues:NULL forKeys:NULL count:0];
  7618. if (self) {
  7619. if (dictionary) {
  7620. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7621. }
  7622. }
  7623. return self;
  7624. }
  7625. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7626. #pragma unused(numItems)
  7627. return [self initWithValues:NULL forKeys:NULL count:0];
  7628. }
  7629. - (void)dealloc {
  7630. NSAssert(!_autocreator,
  7631. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7632. [self class], _autocreator);
  7633. [_dictionary release];
  7634. [super dealloc];
  7635. }
  7636. - (instancetype)copyWithZone:(NSZone *)zone {
  7637. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7638. }
  7639. - (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
  7640. if (self == other) {
  7641. return YES;
  7642. }
  7643. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7644. return NO;
  7645. }
  7646. return [_dictionary isEqual:other->_dictionary];
  7647. }
  7648. - (NSUInteger)hash {
  7649. return _dictionary.count;
  7650. }
  7651. - (NSString *)description {
  7652. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7653. }
  7654. - (NSUInteger)count {
  7655. return _dictionary.count;
  7656. }
  7657. - (void)enumerateKeysAndValuesUsingBlock:
  7658. (void (^)(int64_t key, double value, BOOL *stop))block {
  7659. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7660. NSNumber *aValue,
  7661. BOOL *stop) {
  7662. block([aKey longLongValue], [aValue doubleValue], stop);
  7663. }];
  7664. }
  7665. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7666. NSUInteger count = _dictionary.count;
  7667. if (count == 0) {
  7668. return 0;
  7669. }
  7670. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7671. GPBDataType keyDataType = field.mapKeyDataType;
  7672. __block size_t result = 0;
  7673. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7674. NSNumber *aValue,
  7675. BOOL *stop) {
  7676. #pragma unused(stop)
  7677. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7678. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7679. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7680. }];
  7681. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7682. result += tagSize * count;
  7683. return result;
  7684. }
  7685. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7686. asField:(GPBFieldDescriptor *)field {
  7687. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7688. GPBDataType keyDataType = field.mapKeyDataType;
  7689. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7690. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7691. NSNumber *aValue,
  7692. BOOL *stop) {
  7693. #pragma unused(stop)
  7694. // Write the tag.
  7695. [outputStream writeInt32NoTag:tag];
  7696. // Write the size of the message.
  7697. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7698. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7699. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7700. // Write the fields.
  7701. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7702. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7703. }];
  7704. }
  7705. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7706. forGPBGenericValueKey:(GPBGenericValue *)key {
  7707. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7708. }
  7709. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7710. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7711. #pragma unused(stop)
  7712. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7713. }];
  7714. }
  7715. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7716. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7717. if (wrapped && value) {
  7718. *value = [wrapped doubleValue];
  7719. }
  7720. return (wrapped != NULL);
  7721. }
  7722. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7723. if (otherDictionary) {
  7724. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7725. if (_autocreator) {
  7726. GPBAutocreatedDictionaryModified(_autocreator, self);
  7727. }
  7728. }
  7729. }
  7730. - (void)setValue:(double)value forKey:(int64_t)key {
  7731. [_dictionary setObject:@(value) forKey:@(key)];
  7732. if (_autocreator) {
  7733. GPBAutocreatedDictionaryModified(_autocreator, self);
  7734. }
  7735. }
  7736. - (void)removeValueForKey:(int64_t)aKey {
  7737. [_dictionary removeObjectForKey:@(aKey)];
  7738. }
  7739. - (void)removeAll {
  7740. [_dictionary removeAllObjects];
  7741. }
  7742. @end
  7743. #pragma mark - Int64 -> Enum
  7744. @implementation GPBInt64EnumDictionary {
  7745. @package
  7746. NSMutableDictionary *_dictionary;
  7747. GPBEnumValidationFunc _validationFunc;
  7748. }
  7749. @synthesize validationFunc = _validationFunc;
  7750. + (instancetype)dictionary {
  7751. return [[[self alloc] initWithValidationFunction:NULL
  7752. rawValues:NULL
  7753. forKeys:NULL
  7754. count:0] autorelease];
  7755. }
  7756. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7757. return [[[self alloc] initWithValidationFunction:func
  7758. rawValues:NULL
  7759. forKeys:NULL
  7760. count:0] autorelease];
  7761. }
  7762. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7763. rawValue:(int32_t)rawValue
  7764. forKey:(int64_t)key {
  7765. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7766. // on to get the type correct.
  7767. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7768. rawValues:&rawValue
  7769. forKeys:&key
  7770. count:1] autorelease];
  7771. }
  7772. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7773. rawValues:(const int32_t [])rawValues
  7774. forKeys:(const int64_t [])keys
  7775. count:(NSUInteger)count {
  7776. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7777. // on to get the type correct.
  7778. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7779. rawValues:rawValues
  7780. forKeys:keys
  7781. count:count] autorelease];
  7782. }
  7783. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7784. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7785. // on to get the type correct.
  7786. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7787. }
  7788. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7789. capacity:(NSUInteger)numItems {
  7790. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7791. }
  7792. - (instancetype)init {
  7793. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7794. }
  7795. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7796. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7797. }
  7798. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7799. rawValues:(const int32_t [])rawValues
  7800. forKeys:(const int64_t [])keys
  7801. count:(NSUInteger)count {
  7802. self = [super init];
  7803. if (self) {
  7804. _dictionary = [[NSMutableDictionary alloc] init];
  7805. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7806. if (count && rawValues && keys) {
  7807. for (NSUInteger i = 0; i < count; ++i) {
  7808. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7809. }
  7810. }
  7811. }
  7812. return self;
  7813. }
  7814. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7815. self = [self initWithValidationFunction:dictionary.validationFunc
  7816. rawValues:NULL
  7817. forKeys:NULL
  7818. count:0];
  7819. if (self) {
  7820. if (dictionary) {
  7821. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7822. }
  7823. }
  7824. return self;
  7825. }
  7826. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7827. capacity:(NSUInteger)numItems {
  7828. #pragma unused(numItems)
  7829. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7830. }
  7831. - (void)dealloc {
  7832. NSAssert(!_autocreator,
  7833. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7834. [self class], _autocreator);
  7835. [_dictionary release];
  7836. [super dealloc];
  7837. }
  7838. - (instancetype)copyWithZone:(NSZone *)zone {
  7839. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7840. }
  7841. - (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
  7842. if (self == other) {
  7843. return YES;
  7844. }
  7845. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7846. return NO;
  7847. }
  7848. return [_dictionary isEqual:other->_dictionary];
  7849. }
  7850. - (NSUInteger)hash {
  7851. return _dictionary.count;
  7852. }
  7853. - (NSString *)description {
  7854. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7855. }
  7856. - (NSUInteger)count {
  7857. return _dictionary.count;
  7858. }
  7859. - (void)enumerateKeysAndRawValuesUsingBlock:
  7860. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7861. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7862. NSNumber *aValue,
  7863. BOOL *stop) {
  7864. block([aKey longLongValue], [aValue intValue], stop);
  7865. }];
  7866. }
  7867. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7868. NSUInteger count = _dictionary.count;
  7869. if (count == 0) {
  7870. return 0;
  7871. }
  7872. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7873. GPBDataType keyDataType = field.mapKeyDataType;
  7874. __block size_t result = 0;
  7875. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7876. NSNumber *aValue,
  7877. BOOL *stop) {
  7878. #pragma unused(stop)
  7879. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7880. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7881. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7882. }];
  7883. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7884. result += tagSize * count;
  7885. return result;
  7886. }
  7887. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7888. asField:(GPBFieldDescriptor *)field {
  7889. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7890. GPBDataType keyDataType = field.mapKeyDataType;
  7891. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7892. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7893. NSNumber *aValue,
  7894. BOOL *stop) {
  7895. #pragma unused(stop)
  7896. // Write the tag.
  7897. [outputStream writeInt32NoTag:tag];
  7898. // Write the size of the message.
  7899. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7900. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7901. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7902. // Write the fields.
  7903. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7904. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7905. }];
  7906. }
  7907. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7908. forKey:(GPBGenericValue *)key
  7909. keyDataType:(GPBDataType)keyDataType {
  7910. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7911. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7912. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7913. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7914. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7915. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7916. [outputStream release];
  7917. return data;
  7918. }
  7919. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7920. forGPBGenericValueKey:(GPBGenericValue *)key {
  7921. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7922. }
  7923. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7924. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7925. #pragma unused(stop)
  7926. block([NSString stringWithFormat:@"%lld", key], @(value));
  7927. }];
  7928. }
  7929. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7930. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7931. if (wrapped && value) {
  7932. int32_t result = [wrapped intValue];
  7933. if (!_validationFunc(result)) {
  7934. result = kGPBUnrecognizedEnumeratorValue;
  7935. }
  7936. *value = result;
  7937. }
  7938. return (wrapped != NULL);
  7939. }
  7940. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  7941. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7942. if (wrapped && rawValue) {
  7943. *rawValue = [wrapped intValue];
  7944. }
  7945. return (wrapped != NULL);
  7946. }
  7947. - (void)enumerateKeysAndValuesUsingBlock:
  7948. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7949. GPBEnumValidationFunc func = _validationFunc;
  7950. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7951. NSNumber *aValue,
  7952. BOOL *stop) {
  7953. int32_t unwrapped = [aValue intValue];
  7954. if (!func(unwrapped)) {
  7955. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7956. }
  7957. block([aKey longLongValue], unwrapped, stop);
  7958. }];
  7959. }
  7960. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7961. if (otherDictionary) {
  7962. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7963. if (_autocreator) {
  7964. GPBAutocreatedDictionaryModified(_autocreator, self);
  7965. }
  7966. }
  7967. }
  7968. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7969. [_dictionary setObject:@(value) forKey:@(key)];
  7970. if (_autocreator) {
  7971. GPBAutocreatedDictionaryModified(_autocreator, self);
  7972. }
  7973. }
  7974. - (void)removeValueForKey:(int64_t)aKey {
  7975. [_dictionary removeObjectForKey:@(aKey)];
  7976. }
  7977. - (void)removeAll {
  7978. [_dictionary removeAllObjects];
  7979. }
  7980. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  7981. if (!_validationFunc(value)) {
  7982. [NSException raise:NSInvalidArgumentException
  7983. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7984. value];
  7985. }
  7986. [_dictionary setObject:@(value) forKey:@(key)];
  7987. if (_autocreator) {
  7988. GPBAutocreatedDictionaryModified(_autocreator, self);
  7989. }
  7990. }
  7991. @end
  7992. #pragma mark - Int64 -> Object
  7993. @implementation GPBInt64ObjectDictionary {
  7994. @package
  7995. NSMutableDictionary *_dictionary;
  7996. }
  7997. + (instancetype)dictionary {
  7998. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  7999. }
  8000. + (instancetype)dictionaryWithObject:(id)object
  8001. forKey:(int64_t)key {
  8002. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8003. // on to get the type correct.
  8004. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  8005. forKeys:&key
  8006. count:1] autorelease];
  8007. }
  8008. + (instancetype)dictionaryWithObjects:(const id [])objects
  8009. forKeys:(const int64_t [])keys
  8010. count:(NSUInteger)count {
  8011. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8012. // on to get the type correct.
  8013. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  8014. forKeys:keys
  8015. count:count] autorelease];
  8016. }
  8017. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8018. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8019. // on to get the type correct.
  8020. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8021. }
  8022. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8023. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8024. }
  8025. - (instancetype)init {
  8026. return [self initWithObjects:NULL forKeys:NULL count:0];
  8027. }
  8028. - (instancetype)initWithObjects:(const id [])objects
  8029. forKeys:(const int64_t [])keys
  8030. count:(NSUInteger)count {
  8031. self = [super init];
  8032. if (self) {
  8033. _dictionary = [[NSMutableDictionary alloc] init];
  8034. if (count && objects && keys) {
  8035. for (NSUInteger i = 0; i < count; ++i) {
  8036. if (!objects[i]) {
  8037. [NSException raise:NSInvalidArgumentException
  8038. format:@"Attempting to add nil object to a Dictionary"];
  8039. }
  8040. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  8041. }
  8042. }
  8043. }
  8044. return self;
  8045. }
  8046. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8047. self = [self initWithObjects:NULL forKeys:NULL count:0];
  8048. if (self) {
  8049. if (dictionary) {
  8050. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8051. }
  8052. }
  8053. return self;
  8054. }
  8055. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8056. #pragma unused(numItems)
  8057. return [self initWithObjects:NULL forKeys:NULL count:0];
  8058. }
  8059. - (void)dealloc {
  8060. NSAssert(!_autocreator,
  8061. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8062. [self class], _autocreator);
  8063. [_dictionary release];
  8064. [super dealloc];
  8065. }
  8066. - (instancetype)copyWithZone:(NSZone *)zone {
  8067. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8068. }
  8069. - (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
  8070. if (self == other) {
  8071. return YES;
  8072. }
  8073. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8074. return NO;
  8075. }
  8076. return [_dictionary isEqual:other->_dictionary];
  8077. }
  8078. - (NSUInteger)hash {
  8079. return _dictionary.count;
  8080. }
  8081. - (NSString *)description {
  8082. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8083. }
  8084. - (NSUInteger)count {
  8085. return _dictionary.count;
  8086. }
  8087. - (void)enumerateKeysAndObjectsUsingBlock:
  8088. (void (^)(int64_t key, id object, BOOL *stop))block {
  8089. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8090. id aObject,
  8091. BOOL *stop) {
  8092. block([aKey longLongValue], aObject, stop);
  8093. }];
  8094. }
  8095. - (BOOL)isInitialized {
  8096. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8097. if (!msg.initialized) {
  8098. return NO;
  8099. }
  8100. }
  8101. return YES;
  8102. }
  8103. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8104. GPBInt64ObjectDictionary *newDict =
  8105. [[GPBInt64ObjectDictionary alloc] init];
  8106. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8107. GPBMessage *msg,
  8108. BOOL *stop) {
  8109. #pragma unused(stop)
  8110. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8111. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8112. [copiedMsg release];
  8113. }];
  8114. return newDict;
  8115. }
  8116. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8117. NSUInteger count = _dictionary.count;
  8118. if (count == 0) {
  8119. return 0;
  8120. }
  8121. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8122. GPBDataType keyDataType = field.mapKeyDataType;
  8123. __block size_t result = 0;
  8124. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8125. id aObject,
  8126. BOOL *stop) {
  8127. #pragma unused(stop)
  8128. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8129. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8130. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8131. }];
  8132. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8133. result += tagSize * count;
  8134. return result;
  8135. }
  8136. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8137. asField:(GPBFieldDescriptor *)field {
  8138. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8139. GPBDataType keyDataType = field.mapKeyDataType;
  8140. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8141. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8142. id aObject,
  8143. BOOL *stop) {
  8144. #pragma unused(stop)
  8145. // Write the tag.
  8146. [outputStream writeInt32NoTag:tag];
  8147. // Write the size of the message.
  8148. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8149. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8150. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8151. // Write the fields.
  8152. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8153. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  8154. }];
  8155. }
  8156. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8157. forGPBGenericValueKey:(GPBGenericValue *)key {
  8158. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8159. }
  8160. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8161. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8162. #pragma unused(stop)
  8163. block([NSString stringWithFormat:@"%lld", key], object);
  8164. }];
  8165. }
  8166. - (id)objectForKey:(int64_t)key {
  8167. id result = [_dictionary objectForKey:@(key)];
  8168. return result;
  8169. }
  8170. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8171. if (otherDictionary) {
  8172. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8173. if (_autocreator) {
  8174. GPBAutocreatedDictionaryModified(_autocreator, self);
  8175. }
  8176. }
  8177. }
  8178. - (void)setObject:(id)object forKey:(int64_t)key {
  8179. if (!object) {
  8180. [NSException raise:NSInvalidArgumentException
  8181. format:@"Attempting to add nil object to a Dictionary"];
  8182. }
  8183. [_dictionary setObject:object forKey:@(key)];
  8184. if (_autocreator) {
  8185. GPBAutocreatedDictionaryModified(_autocreator, self);
  8186. }
  8187. }
  8188. - (void)removeObjectForKey:(int64_t)aKey {
  8189. [_dictionary removeObjectForKey:@(aKey)];
  8190. }
  8191. - (void)removeAll {
  8192. [_dictionary removeAllObjects];
  8193. }
  8194. @end
  8195. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8196. // This block of code is generated, do not edit it directly.
  8197. #pragma mark - String -> UInt32
  8198. @implementation GPBStringUInt32Dictionary {
  8199. @package
  8200. NSMutableDictionary *_dictionary;
  8201. }
  8202. + (instancetype)dictionary {
  8203. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8204. }
  8205. + (instancetype)dictionaryWithValue:(uint32_t)value
  8206. forKey:(NSString *)key {
  8207. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8208. // on to get the type correct.
  8209. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  8210. forKeys:&key
  8211. count:1] autorelease];
  8212. }
  8213. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  8214. forKeys:(const NSString * [])keys
  8215. count:(NSUInteger)count {
  8216. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8217. // on to get the type correct.
  8218. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  8219. forKeys:keys
  8220. count:count] autorelease];
  8221. }
  8222. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8223. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8224. // on to get the type correct.
  8225. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8226. }
  8227. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8228. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8229. }
  8230. - (instancetype)init {
  8231. return [self initWithValues:NULL forKeys:NULL count:0];
  8232. }
  8233. - (instancetype)initWithValues:(const uint32_t [])values
  8234. forKeys:(const NSString * [])keys
  8235. count:(NSUInteger)count {
  8236. self = [super init];
  8237. if (self) {
  8238. _dictionary = [[NSMutableDictionary alloc] init];
  8239. if (count && values && keys) {
  8240. for (NSUInteger i = 0; i < count; ++i) {
  8241. if (!keys[i]) {
  8242. [NSException raise:NSInvalidArgumentException
  8243. format:@"Attempting to add nil key to a Dictionary"];
  8244. }
  8245. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8246. }
  8247. }
  8248. }
  8249. return self;
  8250. }
  8251. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)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. NSAssert(!_autocreator,
  8266. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8267. [self class], _autocreator);
  8268. [_dictionary release];
  8269. [super dealloc];
  8270. }
  8271. - (instancetype)copyWithZone:(NSZone *)zone {
  8272. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8273. }
  8274. - (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
  8275. if (self == other) {
  8276. return YES;
  8277. }
  8278. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8279. return NO;
  8280. }
  8281. return [_dictionary isEqual:other->_dictionary];
  8282. }
  8283. - (NSUInteger)hash {
  8284. return _dictionary.count;
  8285. }
  8286. - (NSString *)description {
  8287. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8288. }
  8289. - (NSUInteger)count {
  8290. return _dictionary.count;
  8291. }
  8292. - (void)enumerateKeysAndValuesUsingBlock:
  8293. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8294. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8295. NSNumber *aValue,
  8296. BOOL *stop) {
  8297. block(aKey, [aValue unsignedIntValue], stop);
  8298. }];
  8299. }
  8300. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8301. NSUInteger count = _dictionary.count;
  8302. if (count == 0) {
  8303. return 0;
  8304. }
  8305. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8306. GPBDataType keyDataType = field.mapKeyDataType;
  8307. __block size_t result = 0;
  8308. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8309. NSNumber *aValue,
  8310. BOOL *stop) {
  8311. #pragma unused(stop)
  8312. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8313. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8314. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8315. }];
  8316. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8317. result += tagSize * count;
  8318. return result;
  8319. }
  8320. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8321. asField:(GPBFieldDescriptor *)field {
  8322. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8323. GPBDataType keyDataType = field.mapKeyDataType;
  8324. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8325. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8326. NSNumber *aValue,
  8327. BOOL *stop) {
  8328. #pragma unused(stop)
  8329. // Write the tag.
  8330. [outputStream writeInt32NoTag:tag];
  8331. // Write the size of the message.
  8332. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8333. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8334. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8335. // Write the fields.
  8336. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8337. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8338. }];
  8339. }
  8340. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8341. forGPBGenericValueKey:(GPBGenericValue *)key {
  8342. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8343. }
  8344. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8345. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8346. #pragma unused(stop)
  8347. block(key, [NSString stringWithFormat:@"%u", value]);
  8348. }];
  8349. }
  8350. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  8351. NSNumber *wrapped = [_dictionary objectForKey:key];
  8352. if (wrapped && value) {
  8353. *value = [wrapped unsignedIntValue];
  8354. }
  8355. return (wrapped != NULL);
  8356. }
  8357. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8358. if (otherDictionary) {
  8359. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8360. if (_autocreator) {
  8361. GPBAutocreatedDictionaryModified(_autocreator, self);
  8362. }
  8363. }
  8364. }
  8365. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  8366. if (!key) {
  8367. [NSException raise:NSInvalidArgumentException
  8368. format:@"Attempting to add nil key to a Dictionary"];
  8369. }
  8370. [_dictionary setObject:@(value) forKey:key];
  8371. if (_autocreator) {
  8372. GPBAutocreatedDictionaryModified(_autocreator, self);
  8373. }
  8374. }
  8375. - (void)removeValueForKey:(NSString *)aKey {
  8376. [_dictionary removeObjectForKey:aKey];
  8377. }
  8378. - (void)removeAll {
  8379. [_dictionary removeAllObjects];
  8380. }
  8381. @end
  8382. #pragma mark - String -> Int32
  8383. @implementation GPBStringInt32Dictionary {
  8384. @package
  8385. NSMutableDictionary *_dictionary;
  8386. }
  8387. + (instancetype)dictionary {
  8388. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8389. }
  8390. + (instancetype)dictionaryWithValue:(int32_t)value
  8391. forKey:(NSString *)key {
  8392. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8393. // on to get the type correct.
  8394. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  8395. forKeys:&key
  8396. count:1] autorelease];
  8397. }
  8398. + (instancetype)dictionaryWithValues:(const int32_t [])values
  8399. forKeys:(const NSString * [])keys
  8400. count:(NSUInteger)count {
  8401. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8402. // on to get the type correct.
  8403. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  8404. forKeys:keys
  8405. count:count] autorelease];
  8406. }
  8407. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8408. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8409. // on to get the type correct.
  8410. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8411. }
  8412. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8413. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8414. }
  8415. - (instancetype)init {
  8416. return [self initWithValues:NULL forKeys:NULL count:0];
  8417. }
  8418. - (instancetype)initWithValues:(const int32_t [])values
  8419. forKeys:(const NSString * [])keys
  8420. count:(NSUInteger)count {
  8421. self = [super init];
  8422. if (self) {
  8423. _dictionary = [[NSMutableDictionary alloc] init];
  8424. if (count && values && keys) {
  8425. for (NSUInteger i = 0; i < count; ++i) {
  8426. if (!keys[i]) {
  8427. [NSException raise:NSInvalidArgumentException
  8428. format:@"Attempting to add nil key to a Dictionary"];
  8429. }
  8430. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8431. }
  8432. }
  8433. }
  8434. return self;
  8435. }
  8436. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8437. self = [self initWithValues:NULL forKeys:NULL count:0];
  8438. if (self) {
  8439. if (dictionary) {
  8440. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8441. }
  8442. }
  8443. return self;
  8444. }
  8445. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8446. #pragma unused(numItems)
  8447. return [self initWithValues:NULL forKeys:NULL count:0];
  8448. }
  8449. - (void)dealloc {
  8450. NSAssert(!_autocreator,
  8451. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8452. [self class], _autocreator);
  8453. [_dictionary release];
  8454. [super dealloc];
  8455. }
  8456. - (instancetype)copyWithZone:(NSZone *)zone {
  8457. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8458. }
  8459. - (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
  8460. if (self == other) {
  8461. return YES;
  8462. }
  8463. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8464. return NO;
  8465. }
  8466. return [_dictionary isEqual:other->_dictionary];
  8467. }
  8468. - (NSUInteger)hash {
  8469. return _dictionary.count;
  8470. }
  8471. - (NSString *)description {
  8472. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8473. }
  8474. - (NSUInteger)count {
  8475. return _dictionary.count;
  8476. }
  8477. - (void)enumerateKeysAndValuesUsingBlock:
  8478. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8479. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8480. NSNumber *aValue,
  8481. BOOL *stop) {
  8482. block(aKey, [aValue intValue], stop);
  8483. }];
  8484. }
  8485. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8486. NSUInteger count = _dictionary.count;
  8487. if (count == 0) {
  8488. return 0;
  8489. }
  8490. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8491. GPBDataType keyDataType = field.mapKeyDataType;
  8492. __block size_t result = 0;
  8493. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8494. NSNumber *aValue,
  8495. BOOL *stop) {
  8496. #pragma unused(stop)
  8497. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8498. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8499. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8500. }];
  8501. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8502. result += tagSize * count;
  8503. return result;
  8504. }
  8505. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8506. asField:(GPBFieldDescriptor *)field {
  8507. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8508. GPBDataType keyDataType = field.mapKeyDataType;
  8509. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8510. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8511. NSNumber *aValue,
  8512. BOOL *stop) {
  8513. #pragma unused(stop)
  8514. // Write the tag.
  8515. [outputStream writeInt32NoTag:tag];
  8516. // Write the size of the message.
  8517. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8518. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8519. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8520. // Write the fields.
  8521. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8522. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8523. }];
  8524. }
  8525. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8526. forGPBGenericValueKey:(GPBGenericValue *)key {
  8527. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8528. }
  8529. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8530. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8531. #pragma unused(stop)
  8532. block(key, [NSString stringWithFormat:@"%d", value]);
  8533. }];
  8534. }
  8535. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8536. NSNumber *wrapped = [_dictionary objectForKey:key];
  8537. if (wrapped && value) {
  8538. *value = [wrapped intValue];
  8539. }
  8540. return (wrapped != NULL);
  8541. }
  8542. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8543. if (otherDictionary) {
  8544. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8545. if (_autocreator) {
  8546. GPBAutocreatedDictionaryModified(_autocreator, self);
  8547. }
  8548. }
  8549. }
  8550. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8551. if (!key) {
  8552. [NSException raise:NSInvalidArgumentException
  8553. format:@"Attempting to add nil key to a Dictionary"];
  8554. }
  8555. [_dictionary setObject:@(value) forKey:key];
  8556. if (_autocreator) {
  8557. GPBAutocreatedDictionaryModified(_autocreator, self);
  8558. }
  8559. }
  8560. - (void)removeValueForKey:(NSString *)aKey {
  8561. [_dictionary removeObjectForKey:aKey];
  8562. }
  8563. - (void)removeAll {
  8564. [_dictionary removeAllObjects];
  8565. }
  8566. @end
  8567. #pragma mark - String -> UInt64
  8568. @implementation GPBStringUInt64Dictionary {
  8569. @package
  8570. NSMutableDictionary *_dictionary;
  8571. }
  8572. + (instancetype)dictionary {
  8573. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8574. }
  8575. + (instancetype)dictionaryWithValue:(uint64_t)value
  8576. forKey:(NSString *)key {
  8577. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8578. // on to get the type correct.
  8579. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8580. forKeys:&key
  8581. count:1] autorelease];
  8582. }
  8583. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8584. forKeys:(const NSString * [])keys
  8585. count:(NSUInteger)count {
  8586. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8587. // on to get the type correct.
  8588. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8589. forKeys:keys
  8590. count:count] autorelease];
  8591. }
  8592. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8593. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8594. // on to get the type correct.
  8595. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8596. }
  8597. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8598. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8599. }
  8600. - (instancetype)init {
  8601. return [self initWithValues:NULL forKeys:NULL count:0];
  8602. }
  8603. - (instancetype)initWithValues:(const uint64_t [])values
  8604. forKeys:(const NSString * [])keys
  8605. count:(NSUInteger)count {
  8606. self = [super init];
  8607. if (self) {
  8608. _dictionary = [[NSMutableDictionary alloc] init];
  8609. if (count && values && keys) {
  8610. for (NSUInteger i = 0; i < count; ++i) {
  8611. if (!keys[i]) {
  8612. [NSException raise:NSInvalidArgumentException
  8613. format:@"Attempting to add nil key to a Dictionary"];
  8614. }
  8615. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8616. }
  8617. }
  8618. }
  8619. return self;
  8620. }
  8621. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8622. self = [self initWithValues:NULL forKeys:NULL count:0];
  8623. if (self) {
  8624. if (dictionary) {
  8625. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8626. }
  8627. }
  8628. return self;
  8629. }
  8630. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8631. #pragma unused(numItems)
  8632. return [self initWithValues:NULL forKeys:NULL count:0];
  8633. }
  8634. - (void)dealloc {
  8635. NSAssert(!_autocreator,
  8636. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8637. [self class], _autocreator);
  8638. [_dictionary release];
  8639. [super dealloc];
  8640. }
  8641. - (instancetype)copyWithZone:(NSZone *)zone {
  8642. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8643. }
  8644. - (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
  8645. if (self == other) {
  8646. return YES;
  8647. }
  8648. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8649. return NO;
  8650. }
  8651. return [_dictionary isEqual:other->_dictionary];
  8652. }
  8653. - (NSUInteger)hash {
  8654. return _dictionary.count;
  8655. }
  8656. - (NSString *)description {
  8657. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8658. }
  8659. - (NSUInteger)count {
  8660. return _dictionary.count;
  8661. }
  8662. - (void)enumerateKeysAndValuesUsingBlock:
  8663. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8664. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8665. NSNumber *aValue,
  8666. BOOL *stop) {
  8667. block(aKey, [aValue unsignedLongLongValue], stop);
  8668. }];
  8669. }
  8670. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8671. NSUInteger count = _dictionary.count;
  8672. if (count == 0) {
  8673. return 0;
  8674. }
  8675. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8676. GPBDataType keyDataType = field.mapKeyDataType;
  8677. __block size_t result = 0;
  8678. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8679. NSNumber *aValue,
  8680. BOOL *stop) {
  8681. #pragma unused(stop)
  8682. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8683. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8684. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8685. }];
  8686. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8687. result += tagSize * count;
  8688. return result;
  8689. }
  8690. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8691. asField:(GPBFieldDescriptor *)field {
  8692. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8693. GPBDataType keyDataType = field.mapKeyDataType;
  8694. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8695. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8696. NSNumber *aValue,
  8697. BOOL *stop) {
  8698. #pragma unused(stop)
  8699. // Write the tag.
  8700. [outputStream writeInt32NoTag:tag];
  8701. // Write the size of the message.
  8702. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8703. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8704. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8705. // Write the fields.
  8706. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8707. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8708. }];
  8709. }
  8710. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8711. forGPBGenericValueKey:(GPBGenericValue *)key {
  8712. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8713. }
  8714. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8715. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8716. #pragma unused(stop)
  8717. block(key, [NSString stringWithFormat:@"%llu", value]);
  8718. }];
  8719. }
  8720. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8721. NSNumber *wrapped = [_dictionary objectForKey:key];
  8722. if (wrapped && value) {
  8723. *value = [wrapped unsignedLongLongValue];
  8724. }
  8725. return (wrapped != NULL);
  8726. }
  8727. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8728. if (otherDictionary) {
  8729. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8730. if (_autocreator) {
  8731. GPBAutocreatedDictionaryModified(_autocreator, self);
  8732. }
  8733. }
  8734. }
  8735. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8736. if (!key) {
  8737. [NSException raise:NSInvalidArgumentException
  8738. format:@"Attempting to add nil key to a Dictionary"];
  8739. }
  8740. [_dictionary setObject:@(value) forKey:key];
  8741. if (_autocreator) {
  8742. GPBAutocreatedDictionaryModified(_autocreator, self);
  8743. }
  8744. }
  8745. - (void)removeValueForKey:(NSString *)aKey {
  8746. [_dictionary removeObjectForKey:aKey];
  8747. }
  8748. - (void)removeAll {
  8749. [_dictionary removeAllObjects];
  8750. }
  8751. @end
  8752. #pragma mark - String -> Int64
  8753. @implementation GPBStringInt64Dictionary {
  8754. @package
  8755. NSMutableDictionary *_dictionary;
  8756. }
  8757. + (instancetype)dictionary {
  8758. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8759. }
  8760. + (instancetype)dictionaryWithValue:(int64_t)value
  8761. forKey:(NSString *)key {
  8762. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8763. // on to get the type correct.
  8764. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8765. forKeys:&key
  8766. count:1] autorelease];
  8767. }
  8768. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8769. forKeys:(const NSString * [])keys
  8770. count:(NSUInteger)count {
  8771. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8772. // on to get the type correct.
  8773. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8774. forKeys:keys
  8775. count:count] autorelease];
  8776. }
  8777. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8778. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8779. // on to get the type correct.
  8780. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8781. }
  8782. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8783. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8784. }
  8785. - (instancetype)init {
  8786. return [self initWithValues:NULL forKeys:NULL count:0];
  8787. }
  8788. - (instancetype)initWithValues:(const int64_t [])values
  8789. forKeys:(const NSString * [])keys
  8790. count:(NSUInteger)count {
  8791. self = [super init];
  8792. if (self) {
  8793. _dictionary = [[NSMutableDictionary alloc] init];
  8794. if (count && values && keys) {
  8795. for (NSUInteger i = 0; i < count; ++i) {
  8796. if (!keys[i]) {
  8797. [NSException raise:NSInvalidArgumentException
  8798. format:@"Attempting to add nil key to a Dictionary"];
  8799. }
  8800. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8801. }
  8802. }
  8803. }
  8804. return self;
  8805. }
  8806. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8807. self = [self initWithValues:NULL forKeys:NULL count:0];
  8808. if (self) {
  8809. if (dictionary) {
  8810. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8811. }
  8812. }
  8813. return self;
  8814. }
  8815. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8816. #pragma unused(numItems)
  8817. return [self initWithValues:NULL forKeys:NULL count:0];
  8818. }
  8819. - (void)dealloc {
  8820. NSAssert(!_autocreator,
  8821. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8822. [self class], _autocreator);
  8823. [_dictionary release];
  8824. [super dealloc];
  8825. }
  8826. - (instancetype)copyWithZone:(NSZone *)zone {
  8827. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8828. }
  8829. - (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
  8830. if (self == other) {
  8831. return YES;
  8832. }
  8833. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8834. return NO;
  8835. }
  8836. return [_dictionary isEqual:other->_dictionary];
  8837. }
  8838. - (NSUInteger)hash {
  8839. return _dictionary.count;
  8840. }
  8841. - (NSString *)description {
  8842. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8843. }
  8844. - (NSUInteger)count {
  8845. return _dictionary.count;
  8846. }
  8847. - (void)enumerateKeysAndValuesUsingBlock:
  8848. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8849. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8850. NSNumber *aValue,
  8851. BOOL *stop) {
  8852. block(aKey, [aValue longLongValue], stop);
  8853. }];
  8854. }
  8855. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8856. NSUInteger count = _dictionary.count;
  8857. if (count == 0) {
  8858. return 0;
  8859. }
  8860. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8861. GPBDataType keyDataType = field.mapKeyDataType;
  8862. __block size_t result = 0;
  8863. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8864. NSNumber *aValue,
  8865. BOOL *stop) {
  8866. #pragma unused(stop)
  8867. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8868. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8869. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8870. }];
  8871. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8872. result += tagSize * count;
  8873. return result;
  8874. }
  8875. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8876. asField:(GPBFieldDescriptor *)field {
  8877. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8878. GPBDataType keyDataType = field.mapKeyDataType;
  8879. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8880. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8881. NSNumber *aValue,
  8882. BOOL *stop) {
  8883. #pragma unused(stop)
  8884. // Write the tag.
  8885. [outputStream writeInt32NoTag:tag];
  8886. // Write the size of the message.
  8887. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8888. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8889. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8890. // Write the fields.
  8891. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8892. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8893. }];
  8894. }
  8895. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8896. forGPBGenericValueKey:(GPBGenericValue *)key {
  8897. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8898. }
  8899. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8900. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8901. #pragma unused(stop)
  8902. block(key, [NSString stringWithFormat:@"%lld", value]);
  8903. }];
  8904. }
  8905. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8906. NSNumber *wrapped = [_dictionary objectForKey:key];
  8907. if (wrapped && value) {
  8908. *value = [wrapped longLongValue];
  8909. }
  8910. return (wrapped != NULL);
  8911. }
  8912. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8913. if (otherDictionary) {
  8914. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8915. if (_autocreator) {
  8916. GPBAutocreatedDictionaryModified(_autocreator, self);
  8917. }
  8918. }
  8919. }
  8920. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8921. if (!key) {
  8922. [NSException raise:NSInvalidArgumentException
  8923. format:@"Attempting to add nil key to a Dictionary"];
  8924. }
  8925. [_dictionary setObject:@(value) forKey:key];
  8926. if (_autocreator) {
  8927. GPBAutocreatedDictionaryModified(_autocreator, self);
  8928. }
  8929. }
  8930. - (void)removeValueForKey:(NSString *)aKey {
  8931. [_dictionary removeObjectForKey:aKey];
  8932. }
  8933. - (void)removeAll {
  8934. [_dictionary removeAllObjects];
  8935. }
  8936. @end
  8937. #pragma mark - String -> Bool
  8938. @implementation GPBStringBoolDictionary {
  8939. @package
  8940. NSMutableDictionary *_dictionary;
  8941. }
  8942. + (instancetype)dictionary {
  8943. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8944. }
  8945. + (instancetype)dictionaryWithValue:(BOOL)value
  8946. forKey:(NSString *)key {
  8947. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8948. // on to get the type correct.
  8949. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  8950. forKeys:&key
  8951. count:1] autorelease];
  8952. }
  8953. + (instancetype)dictionaryWithValues:(const BOOL [])values
  8954. forKeys:(const NSString * [])keys
  8955. count:(NSUInteger)count {
  8956. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8957. // on to get the type correct.
  8958. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  8959. forKeys:keys
  8960. count:count] autorelease];
  8961. }
  8962. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8963. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8964. // on to get the type correct.
  8965. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8966. }
  8967. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8968. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8969. }
  8970. - (instancetype)init {
  8971. return [self initWithValues:NULL forKeys:NULL count:0];
  8972. }
  8973. - (instancetype)initWithValues:(const BOOL [])values
  8974. forKeys:(const NSString * [])keys
  8975. count:(NSUInteger)count {
  8976. self = [super init];
  8977. if (self) {
  8978. _dictionary = [[NSMutableDictionary alloc] init];
  8979. if (count && values && keys) {
  8980. for (NSUInteger i = 0; i < count; ++i) {
  8981. if (!keys[i]) {
  8982. [NSException raise:NSInvalidArgumentException
  8983. format:@"Attempting to add nil key to a Dictionary"];
  8984. }
  8985. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8986. }
  8987. }
  8988. }
  8989. return self;
  8990. }
  8991. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8992. self = [self initWithValues:NULL forKeys:NULL count:0];
  8993. if (self) {
  8994. if (dictionary) {
  8995. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8996. }
  8997. }
  8998. return self;
  8999. }
  9000. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9001. #pragma unused(numItems)
  9002. return [self initWithValues:NULL forKeys:NULL count:0];
  9003. }
  9004. - (void)dealloc {
  9005. NSAssert(!_autocreator,
  9006. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9007. [self class], _autocreator);
  9008. [_dictionary release];
  9009. [super dealloc];
  9010. }
  9011. - (instancetype)copyWithZone:(NSZone *)zone {
  9012. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9013. }
  9014. - (BOOL)isEqual:(GPBStringBoolDictionary *)other {
  9015. if (self == other) {
  9016. return YES;
  9017. }
  9018. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  9019. return NO;
  9020. }
  9021. return [_dictionary isEqual:other->_dictionary];
  9022. }
  9023. - (NSUInteger)hash {
  9024. return _dictionary.count;
  9025. }
  9026. - (NSString *)description {
  9027. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9028. }
  9029. - (NSUInteger)count {
  9030. return _dictionary.count;
  9031. }
  9032. - (void)enumerateKeysAndValuesUsingBlock:
  9033. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  9034. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9035. NSNumber *aValue,
  9036. BOOL *stop) {
  9037. block(aKey, [aValue boolValue], stop);
  9038. }];
  9039. }
  9040. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9041. NSUInteger count = _dictionary.count;
  9042. if (count == 0) {
  9043. return 0;
  9044. }
  9045. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9046. GPBDataType keyDataType = field.mapKeyDataType;
  9047. __block size_t result = 0;
  9048. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9049. NSNumber *aValue,
  9050. BOOL *stop) {
  9051. #pragma unused(stop)
  9052. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9053. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9054. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9055. }];
  9056. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9057. result += tagSize * count;
  9058. return result;
  9059. }
  9060. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9061. asField:(GPBFieldDescriptor *)field {
  9062. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9063. GPBDataType keyDataType = field.mapKeyDataType;
  9064. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9065. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9066. NSNumber *aValue,
  9067. BOOL *stop) {
  9068. #pragma unused(stop)
  9069. // Write the tag.
  9070. [outputStream writeInt32NoTag:tag];
  9071. // Write the size of the message.
  9072. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9073. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9074. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9075. // Write the fields.
  9076. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9077. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  9078. }];
  9079. }
  9080. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9081. forGPBGenericValueKey:(GPBGenericValue *)key {
  9082. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  9083. }
  9084. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9085. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  9086. #pragma unused(stop)
  9087. block(key, (value ? @"true" : @"false"));
  9088. }];
  9089. }
  9090. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  9091. NSNumber *wrapped = [_dictionary objectForKey:key];
  9092. if (wrapped && value) {
  9093. *value = [wrapped boolValue];
  9094. }
  9095. return (wrapped != NULL);
  9096. }
  9097. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9098. if (otherDictionary) {
  9099. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9100. if (_autocreator) {
  9101. GPBAutocreatedDictionaryModified(_autocreator, self);
  9102. }
  9103. }
  9104. }
  9105. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  9106. if (!key) {
  9107. [NSException raise:NSInvalidArgumentException
  9108. format:@"Attempting to add nil key to a Dictionary"];
  9109. }
  9110. [_dictionary setObject:@(value) forKey:key];
  9111. if (_autocreator) {
  9112. GPBAutocreatedDictionaryModified(_autocreator, self);
  9113. }
  9114. }
  9115. - (void)removeValueForKey:(NSString *)aKey {
  9116. [_dictionary removeObjectForKey:aKey];
  9117. }
  9118. - (void)removeAll {
  9119. [_dictionary removeAllObjects];
  9120. }
  9121. @end
  9122. #pragma mark - String -> Float
  9123. @implementation GPBStringFloatDictionary {
  9124. @package
  9125. NSMutableDictionary *_dictionary;
  9126. }
  9127. + (instancetype)dictionary {
  9128. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9129. }
  9130. + (instancetype)dictionaryWithValue:(float)value
  9131. forKey:(NSString *)key {
  9132. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9133. // on to get the type correct.
  9134. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  9135. forKeys:&key
  9136. count:1] autorelease];
  9137. }
  9138. + (instancetype)dictionaryWithValues:(const float [])values
  9139. forKeys:(const NSString * [])keys
  9140. count:(NSUInteger)count {
  9141. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9142. // on to get the type correct.
  9143. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  9144. forKeys:keys
  9145. count:count] autorelease];
  9146. }
  9147. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9148. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9149. // on to get the type correct.
  9150. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9151. }
  9152. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9153. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9154. }
  9155. - (instancetype)init {
  9156. return [self initWithValues:NULL forKeys:NULL count:0];
  9157. }
  9158. - (instancetype)initWithValues:(const float [])values
  9159. forKeys:(const NSString * [])keys
  9160. count:(NSUInteger)count {
  9161. self = [super init];
  9162. if (self) {
  9163. _dictionary = [[NSMutableDictionary alloc] init];
  9164. if (count && values && keys) {
  9165. for (NSUInteger i = 0; i < count; ++i) {
  9166. if (!keys[i]) {
  9167. [NSException raise:NSInvalidArgumentException
  9168. format:@"Attempting to add nil key to a Dictionary"];
  9169. }
  9170. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9171. }
  9172. }
  9173. }
  9174. return self;
  9175. }
  9176. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9177. self = [self initWithValues:NULL forKeys:NULL count:0];
  9178. if (self) {
  9179. if (dictionary) {
  9180. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9181. }
  9182. }
  9183. return self;
  9184. }
  9185. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9186. #pragma unused(numItems)
  9187. return [self initWithValues:NULL forKeys:NULL count:0];
  9188. }
  9189. - (void)dealloc {
  9190. NSAssert(!_autocreator,
  9191. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9192. [self class], _autocreator);
  9193. [_dictionary release];
  9194. [super dealloc];
  9195. }
  9196. - (instancetype)copyWithZone:(NSZone *)zone {
  9197. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9198. }
  9199. - (BOOL)isEqual:(GPBStringFloatDictionary *)other {
  9200. if (self == other) {
  9201. return YES;
  9202. }
  9203. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9204. return NO;
  9205. }
  9206. return [_dictionary isEqual:other->_dictionary];
  9207. }
  9208. - (NSUInteger)hash {
  9209. return _dictionary.count;
  9210. }
  9211. - (NSString *)description {
  9212. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9213. }
  9214. - (NSUInteger)count {
  9215. return _dictionary.count;
  9216. }
  9217. - (void)enumerateKeysAndValuesUsingBlock:
  9218. (void (^)(NSString *key, float value, BOOL *stop))block {
  9219. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9220. NSNumber *aValue,
  9221. BOOL *stop) {
  9222. block(aKey, [aValue floatValue], stop);
  9223. }];
  9224. }
  9225. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9226. NSUInteger count = _dictionary.count;
  9227. if (count == 0) {
  9228. return 0;
  9229. }
  9230. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9231. GPBDataType keyDataType = field.mapKeyDataType;
  9232. __block size_t result = 0;
  9233. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9234. NSNumber *aValue,
  9235. BOOL *stop) {
  9236. #pragma unused(stop)
  9237. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9238. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9239. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9240. }];
  9241. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9242. result += tagSize * count;
  9243. return result;
  9244. }
  9245. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9246. asField:(GPBFieldDescriptor *)field {
  9247. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9248. GPBDataType keyDataType = field.mapKeyDataType;
  9249. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9250. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9251. NSNumber *aValue,
  9252. BOOL *stop) {
  9253. #pragma unused(stop)
  9254. // Write the tag.
  9255. [outputStream writeInt32NoTag:tag];
  9256. // Write the size of the message.
  9257. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9258. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9259. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9260. // Write the fields.
  9261. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9262. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9263. }];
  9264. }
  9265. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9266. forGPBGenericValueKey:(GPBGenericValue *)key {
  9267. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9268. }
  9269. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9270. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9271. #pragma unused(stop)
  9272. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9273. }];
  9274. }
  9275. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  9276. NSNumber *wrapped = [_dictionary objectForKey:key];
  9277. if (wrapped && value) {
  9278. *value = [wrapped floatValue];
  9279. }
  9280. return (wrapped != NULL);
  9281. }
  9282. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9283. if (otherDictionary) {
  9284. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9285. if (_autocreator) {
  9286. GPBAutocreatedDictionaryModified(_autocreator, self);
  9287. }
  9288. }
  9289. }
  9290. - (void)setValue:(float)value forKey:(NSString *)key {
  9291. if (!key) {
  9292. [NSException raise:NSInvalidArgumentException
  9293. format:@"Attempting to add nil key to a Dictionary"];
  9294. }
  9295. [_dictionary setObject:@(value) forKey:key];
  9296. if (_autocreator) {
  9297. GPBAutocreatedDictionaryModified(_autocreator, self);
  9298. }
  9299. }
  9300. - (void)removeValueForKey:(NSString *)aKey {
  9301. [_dictionary removeObjectForKey:aKey];
  9302. }
  9303. - (void)removeAll {
  9304. [_dictionary removeAllObjects];
  9305. }
  9306. @end
  9307. #pragma mark - String -> Double
  9308. @implementation GPBStringDoubleDictionary {
  9309. @package
  9310. NSMutableDictionary *_dictionary;
  9311. }
  9312. + (instancetype)dictionary {
  9313. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9314. }
  9315. + (instancetype)dictionaryWithValue:(double)value
  9316. forKey:(NSString *)key {
  9317. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9318. // on to get the type correct.
  9319. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  9320. forKeys:&key
  9321. count:1] autorelease];
  9322. }
  9323. + (instancetype)dictionaryWithValues:(const double [])values
  9324. forKeys:(const NSString * [])keys
  9325. count:(NSUInteger)count {
  9326. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9327. // on to get the type correct.
  9328. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  9329. forKeys:keys
  9330. count:count] autorelease];
  9331. }
  9332. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9333. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9334. // on to get the type correct.
  9335. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9336. }
  9337. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9338. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9339. }
  9340. - (instancetype)init {
  9341. return [self initWithValues:NULL forKeys:NULL count:0];
  9342. }
  9343. - (instancetype)initWithValues:(const double [])values
  9344. forKeys:(const NSString * [])keys
  9345. count:(NSUInteger)count {
  9346. self = [super init];
  9347. if (self) {
  9348. _dictionary = [[NSMutableDictionary alloc] init];
  9349. if (count && values && keys) {
  9350. for (NSUInteger i = 0; i < count; ++i) {
  9351. if (!keys[i]) {
  9352. [NSException raise:NSInvalidArgumentException
  9353. format:@"Attempting to add nil key to a Dictionary"];
  9354. }
  9355. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9356. }
  9357. }
  9358. }
  9359. return self;
  9360. }
  9361. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9362. self = [self initWithValues:NULL forKeys:NULL count:0];
  9363. if (self) {
  9364. if (dictionary) {
  9365. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9366. }
  9367. }
  9368. return self;
  9369. }
  9370. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9371. #pragma unused(numItems)
  9372. return [self initWithValues:NULL forKeys:NULL count:0];
  9373. }
  9374. - (void)dealloc {
  9375. NSAssert(!_autocreator,
  9376. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9377. [self class], _autocreator);
  9378. [_dictionary release];
  9379. [super dealloc];
  9380. }
  9381. - (instancetype)copyWithZone:(NSZone *)zone {
  9382. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9383. }
  9384. - (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
  9385. if (self == other) {
  9386. return YES;
  9387. }
  9388. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9389. return NO;
  9390. }
  9391. return [_dictionary isEqual:other->_dictionary];
  9392. }
  9393. - (NSUInteger)hash {
  9394. return _dictionary.count;
  9395. }
  9396. - (NSString *)description {
  9397. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9398. }
  9399. - (NSUInteger)count {
  9400. return _dictionary.count;
  9401. }
  9402. - (void)enumerateKeysAndValuesUsingBlock:
  9403. (void (^)(NSString *key, double value, BOOL *stop))block {
  9404. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9405. NSNumber *aValue,
  9406. BOOL *stop) {
  9407. block(aKey, [aValue doubleValue], stop);
  9408. }];
  9409. }
  9410. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9411. NSUInteger count = _dictionary.count;
  9412. if (count == 0) {
  9413. return 0;
  9414. }
  9415. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9416. GPBDataType keyDataType = field.mapKeyDataType;
  9417. __block size_t result = 0;
  9418. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9419. NSNumber *aValue,
  9420. BOOL *stop) {
  9421. #pragma unused(stop)
  9422. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9423. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9424. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9425. }];
  9426. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9427. result += tagSize * count;
  9428. return result;
  9429. }
  9430. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9431. asField:(GPBFieldDescriptor *)field {
  9432. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9433. GPBDataType keyDataType = field.mapKeyDataType;
  9434. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9435. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9436. NSNumber *aValue,
  9437. BOOL *stop) {
  9438. #pragma unused(stop)
  9439. // Write the tag.
  9440. [outputStream writeInt32NoTag:tag];
  9441. // Write the size of the message.
  9442. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9443. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9444. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9445. // Write the fields.
  9446. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9447. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9448. }];
  9449. }
  9450. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9451. forGPBGenericValueKey:(GPBGenericValue *)key {
  9452. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9453. }
  9454. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9455. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9456. #pragma unused(stop)
  9457. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9458. }];
  9459. }
  9460. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  9461. NSNumber *wrapped = [_dictionary objectForKey:key];
  9462. if (wrapped && value) {
  9463. *value = [wrapped doubleValue];
  9464. }
  9465. return (wrapped != NULL);
  9466. }
  9467. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9468. if (otherDictionary) {
  9469. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9470. if (_autocreator) {
  9471. GPBAutocreatedDictionaryModified(_autocreator, self);
  9472. }
  9473. }
  9474. }
  9475. - (void)setValue:(double)value forKey:(NSString *)key {
  9476. if (!key) {
  9477. [NSException raise:NSInvalidArgumentException
  9478. format:@"Attempting to add nil key to a Dictionary"];
  9479. }
  9480. [_dictionary setObject:@(value) forKey:key];
  9481. if (_autocreator) {
  9482. GPBAutocreatedDictionaryModified(_autocreator, self);
  9483. }
  9484. }
  9485. - (void)removeValueForKey:(NSString *)aKey {
  9486. [_dictionary removeObjectForKey:aKey];
  9487. }
  9488. - (void)removeAll {
  9489. [_dictionary removeAllObjects];
  9490. }
  9491. @end
  9492. #pragma mark - String -> Enum
  9493. @implementation GPBStringEnumDictionary {
  9494. @package
  9495. NSMutableDictionary *_dictionary;
  9496. GPBEnumValidationFunc _validationFunc;
  9497. }
  9498. @synthesize validationFunc = _validationFunc;
  9499. + (instancetype)dictionary {
  9500. return [[[self alloc] initWithValidationFunction:NULL
  9501. rawValues:NULL
  9502. forKeys:NULL
  9503. count:0] autorelease];
  9504. }
  9505. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9506. return [[[self alloc] initWithValidationFunction:func
  9507. rawValues:NULL
  9508. forKeys:NULL
  9509. count:0] autorelease];
  9510. }
  9511. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9512. rawValue:(int32_t)rawValue
  9513. forKey:(NSString *)key {
  9514. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9515. // on to get the type correct.
  9516. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9517. rawValues:&rawValue
  9518. forKeys:&key
  9519. count:1] autorelease];
  9520. }
  9521. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9522. rawValues:(const int32_t [])rawValues
  9523. forKeys:(const NSString * [])keys
  9524. count:(NSUInteger)count {
  9525. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9526. // on to get the type correct.
  9527. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9528. rawValues:rawValues
  9529. forKeys:keys
  9530. count:count] autorelease];
  9531. }
  9532. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9533. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9534. // on to get the type correct.
  9535. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9536. }
  9537. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9538. capacity:(NSUInteger)numItems {
  9539. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9540. }
  9541. - (instancetype)init {
  9542. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9543. }
  9544. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9545. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9546. }
  9547. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9548. rawValues:(const int32_t [])rawValues
  9549. forKeys:(const NSString * [])keys
  9550. count:(NSUInteger)count {
  9551. self = [super init];
  9552. if (self) {
  9553. _dictionary = [[NSMutableDictionary alloc] init];
  9554. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9555. if (count && rawValues && keys) {
  9556. for (NSUInteger i = 0; i < count; ++i) {
  9557. if (!keys[i]) {
  9558. [NSException raise:NSInvalidArgumentException
  9559. format:@"Attempting to add nil key to a Dictionary"];
  9560. }
  9561. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9562. }
  9563. }
  9564. }
  9565. return self;
  9566. }
  9567. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9568. self = [self initWithValidationFunction:dictionary.validationFunc
  9569. rawValues:NULL
  9570. forKeys:NULL
  9571. count:0];
  9572. if (self) {
  9573. if (dictionary) {
  9574. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9575. }
  9576. }
  9577. return self;
  9578. }
  9579. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9580. capacity:(NSUInteger)numItems {
  9581. #pragma unused(numItems)
  9582. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9583. }
  9584. - (void)dealloc {
  9585. NSAssert(!_autocreator,
  9586. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9587. [self class], _autocreator);
  9588. [_dictionary release];
  9589. [super dealloc];
  9590. }
  9591. - (instancetype)copyWithZone:(NSZone *)zone {
  9592. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9593. }
  9594. - (BOOL)isEqual:(GPBStringEnumDictionary *)other {
  9595. if (self == other) {
  9596. return YES;
  9597. }
  9598. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9599. return NO;
  9600. }
  9601. return [_dictionary isEqual:other->_dictionary];
  9602. }
  9603. - (NSUInteger)hash {
  9604. return _dictionary.count;
  9605. }
  9606. - (NSString *)description {
  9607. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9608. }
  9609. - (NSUInteger)count {
  9610. return _dictionary.count;
  9611. }
  9612. - (void)enumerateKeysAndRawValuesUsingBlock:
  9613. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9614. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9615. NSNumber *aValue,
  9616. BOOL *stop) {
  9617. block(aKey, [aValue intValue], stop);
  9618. }];
  9619. }
  9620. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9621. NSUInteger count = _dictionary.count;
  9622. if (count == 0) {
  9623. return 0;
  9624. }
  9625. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9626. GPBDataType keyDataType = field.mapKeyDataType;
  9627. __block size_t result = 0;
  9628. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9629. NSNumber *aValue,
  9630. BOOL *stop) {
  9631. #pragma unused(stop)
  9632. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9633. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9634. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9635. }];
  9636. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9637. result += tagSize * count;
  9638. return result;
  9639. }
  9640. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9641. asField:(GPBFieldDescriptor *)field {
  9642. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9643. GPBDataType keyDataType = field.mapKeyDataType;
  9644. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9645. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9646. NSNumber *aValue,
  9647. BOOL *stop) {
  9648. #pragma unused(stop)
  9649. // Write the tag.
  9650. [outputStream writeInt32NoTag:tag];
  9651. // Write the size of the message.
  9652. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9653. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9654. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9655. // Write the fields.
  9656. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9657. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9658. }];
  9659. }
  9660. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9661. forKey:(GPBGenericValue *)key
  9662. keyDataType:(GPBDataType)keyDataType {
  9663. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9664. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9665. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9666. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9667. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9668. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9669. [outputStream release];
  9670. return data;
  9671. }
  9672. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9673. forGPBGenericValueKey:(GPBGenericValue *)key {
  9674. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9675. }
  9676. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9677. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9678. #pragma unused(stop)
  9679. block(key, @(value));
  9680. }];
  9681. }
  9682. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9683. NSNumber *wrapped = [_dictionary objectForKey:key];
  9684. if (wrapped && value) {
  9685. int32_t result = [wrapped intValue];
  9686. if (!_validationFunc(result)) {
  9687. result = kGPBUnrecognizedEnumeratorValue;
  9688. }
  9689. *value = result;
  9690. }
  9691. return (wrapped != NULL);
  9692. }
  9693. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9694. NSNumber *wrapped = [_dictionary objectForKey:key];
  9695. if (wrapped && rawValue) {
  9696. *rawValue = [wrapped intValue];
  9697. }
  9698. return (wrapped != NULL);
  9699. }
  9700. - (void)enumerateKeysAndValuesUsingBlock:
  9701. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9702. GPBEnumValidationFunc func = _validationFunc;
  9703. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9704. NSNumber *aValue,
  9705. BOOL *stop) {
  9706. int32_t unwrapped = [aValue intValue];
  9707. if (!func(unwrapped)) {
  9708. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9709. }
  9710. block(aKey, unwrapped, stop);
  9711. }];
  9712. }
  9713. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9714. if (otherDictionary) {
  9715. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9716. if (_autocreator) {
  9717. GPBAutocreatedDictionaryModified(_autocreator, self);
  9718. }
  9719. }
  9720. }
  9721. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9722. if (!key) {
  9723. [NSException raise:NSInvalidArgumentException
  9724. format:@"Attempting to add nil key to a Dictionary"];
  9725. }
  9726. [_dictionary setObject:@(value) forKey:key];
  9727. if (_autocreator) {
  9728. GPBAutocreatedDictionaryModified(_autocreator, self);
  9729. }
  9730. }
  9731. - (void)removeValueForKey:(NSString *)aKey {
  9732. [_dictionary removeObjectForKey:aKey];
  9733. }
  9734. - (void)removeAll {
  9735. [_dictionary removeAllObjects];
  9736. }
  9737. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9738. if (!key) {
  9739. [NSException raise:NSInvalidArgumentException
  9740. format:@"Attempting to add nil key to a Dictionary"];
  9741. }
  9742. if (!_validationFunc(value)) {
  9743. [NSException raise:NSInvalidArgumentException
  9744. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9745. value];
  9746. }
  9747. [_dictionary setObject:@(value) forKey:key];
  9748. if (_autocreator) {
  9749. GPBAutocreatedDictionaryModified(_autocreator, self);
  9750. }
  9751. }
  9752. @end
  9753. //%PDDM-EXPAND-END (5 expansions)
  9754. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9755. // This block of code is generated, do not edit it directly.
  9756. #pragma mark - Bool -> UInt32
  9757. @implementation GPBBoolUInt32Dictionary {
  9758. @package
  9759. uint32_t _values[2];
  9760. BOOL _valueSet[2];
  9761. }
  9762. + (instancetype)dictionary {
  9763. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9764. }
  9765. + (instancetype)dictionaryWithValue:(uint32_t)value
  9766. forKey:(BOOL)key {
  9767. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9768. // on to get the type correct.
  9769. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9770. forKeys:&key
  9771. count:1] autorelease];
  9772. }
  9773. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9774. forKeys:(const BOOL [])keys
  9775. count:(NSUInteger)count {
  9776. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9777. // on to get the type correct.
  9778. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9779. forKeys:keys
  9780. count:count] autorelease];
  9781. }
  9782. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9783. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9784. // on to get the type correct.
  9785. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9786. }
  9787. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9788. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9789. }
  9790. - (instancetype)init {
  9791. return [self initWithValues:NULL forKeys:NULL count:0];
  9792. }
  9793. - (instancetype)initWithValues:(const uint32_t [])values
  9794. forKeys:(const BOOL [])keys
  9795. count:(NSUInteger)count {
  9796. self = [super init];
  9797. if (self) {
  9798. for (NSUInteger i = 0; i < count; ++i) {
  9799. int idx = keys[i] ? 1 : 0;
  9800. _values[idx] = values[i];
  9801. _valueSet[idx] = YES;
  9802. }
  9803. }
  9804. return self;
  9805. }
  9806. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9807. self = [self initWithValues:NULL forKeys:NULL count:0];
  9808. if (self) {
  9809. if (dictionary) {
  9810. for (int i = 0; i < 2; ++i) {
  9811. if (dictionary->_valueSet[i]) {
  9812. _values[i] = dictionary->_values[i];
  9813. _valueSet[i] = YES;
  9814. }
  9815. }
  9816. }
  9817. }
  9818. return self;
  9819. }
  9820. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9821. #pragma unused(numItems)
  9822. return [self initWithValues:NULL forKeys:NULL count:0];
  9823. }
  9824. #if !defined(NS_BLOCK_ASSERTIONS)
  9825. - (void)dealloc {
  9826. NSAssert(!_autocreator,
  9827. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9828. [self class], _autocreator);
  9829. [super dealloc];
  9830. }
  9831. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9832. - (instancetype)copyWithZone:(NSZone *)zone {
  9833. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9834. }
  9835. - (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
  9836. if (self == other) {
  9837. return YES;
  9838. }
  9839. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9840. return NO;
  9841. }
  9842. if ((_valueSet[0] != other->_valueSet[0]) ||
  9843. (_valueSet[1] != other->_valueSet[1])) {
  9844. return NO;
  9845. }
  9846. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9847. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9848. return NO;
  9849. }
  9850. return YES;
  9851. }
  9852. - (NSUInteger)hash {
  9853. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9854. }
  9855. - (NSString *)description {
  9856. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9857. if (_valueSet[0]) {
  9858. [result appendFormat:@"NO: %u", _values[0]];
  9859. }
  9860. if (_valueSet[1]) {
  9861. [result appendFormat:@"YES: %u", _values[1]];
  9862. }
  9863. [result appendString:@" }"];
  9864. return result;
  9865. }
  9866. - (NSUInteger)count {
  9867. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9868. }
  9869. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9870. int idx = (key ? 1 : 0);
  9871. if (_valueSet[idx]) {
  9872. if (value) {
  9873. *value = _values[idx];
  9874. }
  9875. return YES;
  9876. }
  9877. return NO;
  9878. }
  9879. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9880. forGPBGenericValueKey:(GPBGenericValue *)key {
  9881. int idx = (key->valueBool ? 1 : 0);
  9882. _values[idx] = value->valueUInt32;
  9883. _valueSet[idx] = YES;
  9884. }
  9885. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9886. if (_valueSet[0]) {
  9887. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9888. }
  9889. if (_valueSet[1]) {
  9890. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9891. }
  9892. }
  9893. - (void)enumerateKeysAndValuesUsingBlock:
  9894. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9895. BOOL stop = NO;
  9896. if (_valueSet[0]) {
  9897. block(NO, _values[0], &stop);
  9898. }
  9899. if (!stop && _valueSet[1]) {
  9900. block(YES, _values[1], &stop);
  9901. }
  9902. }
  9903. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9904. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9905. NSUInteger count = 0;
  9906. size_t result = 0;
  9907. for (int i = 0; i < 2; ++i) {
  9908. if (_valueSet[i]) {
  9909. ++count;
  9910. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9911. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9912. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9913. }
  9914. }
  9915. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9916. result += tagSize * count;
  9917. return result;
  9918. }
  9919. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9920. asField:(GPBFieldDescriptor *)field {
  9921. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9922. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9923. for (int i = 0; i < 2; ++i) {
  9924. if (_valueSet[i]) {
  9925. // Write the tag.
  9926. [outputStream writeInt32NoTag:tag];
  9927. // Write the size of the message.
  9928. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9929. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9930. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9931. // Write the fields.
  9932. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9933. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9934. }
  9935. }
  9936. }
  9937. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9938. if (otherDictionary) {
  9939. for (int i = 0; i < 2; ++i) {
  9940. if (otherDictionary->_valueSet[i]) {
  9941. _valueSet[i] = YES;
  9942. _values[i] = otherDictionary->_values[i];
  9943. }
  9944. }
  9945. if (_autocreator) {
  9946. GPBAutocreatedDictionaryModified(_autocreator, self);
  9947. }
  9948. }
  9949. }
  9950. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  9951. int idx = (key ? 1 : 0);
  9952. _values[idx] = value;
  9953. _valueSet[idx] = YES;
  9954. if (_autocreator) {
  9955. GPBAutocreatedDictionaryModified(_autocreator, self);
  9956. }
  9957. }
  9958. - (void)removeValueForKey:(BOOL)aKey {
  9959. _valueSet[aKey ? 1 : 0] = NO;
  9960. }
  9961. - (void)removeAll {
  9962. _valueSet[0] = NO;
  9963. _valueSet[1] = NO;
  9964. }
  9965. @end
  9966. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9967. // This block of code is generated, do not edit it directly.
  9968. #pragma mark - Bool -> Int32
  9969. @implementation GPBBoolInt32Dictionary {
  9970. @package
  9971. int32_t _values[2];
  9972. BOOL _valueSet[2];
  9973. }
  9974. + (instancetype)dictionary {
  9975. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9976. }
  9977. + (instancetype)dictionaryWithValue:(int32_t)value
  9978. forKey:(BOOL)key {
  9979. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9980. // on to get the type correct.
  9981. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  9982. forKeys:&key
  9983. count:1] autorelease];
  9984. }
  9985. + (instancetype)dictionaryWithValues:(const int32_t [])values
  9986. forKeys:(const BOOL [])keys
  9987. count:(NSUInteger)count {
  9988. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9989. // on to get the type correct.
  9990. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  9991. forKeys:keys
  9992. count:count] autorelease];
  9993. }
  9994. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9995. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9996. // on to get the type correct.
  9997. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9998. }
  9999. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10000. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10001. }
  10002. - (instancetype)init {
  10003. return [self initWithValues:NULL forKeys:NULL count:0];
  10004. }
  10005. - (instancetype)initWithValues:(const int32_t [])values
  10006. forKeys:(const BOOL [])keys
  10007. count:(NSUInteger)count {
  10008. self = [super init];
  10009. if (self) {
  10010. for (NSUInteger i = 0; i < count; ++i) {
  10011. int idx = keys[i] ? 1 : 0;
  10012. _values[idx] = values[i];
  10013. _valueSet[idx] = YES;
  10014. }
  10015. }
  10016. return self;
  10017. }
  10018. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10019. self = [self initWithValues:NULL forKeys:NULL count:0];
  10020. if (self) {
  10021. if (dictionary) {
  10022. for (int i = 0; i < 2; ++i) {
  10023. if (dictionary->_valueSet[i]) {
  10024. _values[i] = dictionary->_values[i];
  10025. _valueSet[i] = YES;
  10026. }
  10027. }
  10028. }
  10029. }
  10030. return self;
  10031. }
  10032. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10033. #pragma unused(numItems)
  10034. return [self initWithValues:NULL forKeys:NULL count:0];
  10035. }
  10036. #if !defined(NS_BLOCK_ASSERTIONS)
  10037. - (void)dealloc {
  10038. NSAssert(!_autocreator,
  10039. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10040. [self class], _autocreator);
  10041. [super dealloc];
  10042. }
  10043. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10044. - (instancetype)copyWithZone:(NSZone *)zone {
  10045. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10046. }
  10047. - (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
  10048. if (self == other) {
  10049. return YES;
  10050. }
  10051. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  10052. return NO;
  10053. }
  10054. if ((_valueSet[0] != other->_valueSet[0]) ||
  10055. (_valueSet[1] != other->_valueSet[1])) {
  10056. return NO;
  10057. }
  10058. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10059. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10060. return NO;
  10061. }
  10062. return YES;
  10063. }
  10064. - (NSUInteger)hash {
  10065. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10066. }
  10067. - (NSString *)description {
  10068. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10069. if (_valueSet[0]) {
  10070. [result appendFormat:@"NO: %d", _values[0]];
  10071. }
  10072. if (_valueSet[1]) {
  10073. [result appendFormat:@"YES: %d", _values[1]];
  10074. }
  10075. [result appendString:@" }"];
  10076. return result;
  10077. }
  10078. - (NSUInteger)count {
  10079. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10080. }
  10081. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  10082. int idx = (key ? 1 : 0);
  10083. if (_valueSet[idx]) {
  10084. if (value) {
  10085. *value = _values[idx];
  10086. }
  10087. return YES;
  10088. }
  10089. return NO;
  10090. }
  10091. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10092. forGPBGenericValueKey:(GPBGenericValue *)key {
  10093. int idx = (key->valueBool ? 1 : 0);
  10094. _values[idx] = value->valueInt32;
  10095. _valueSet[idx] = YES;
  10096. }
  10097. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10098. if (_valueSet[0]) {
  10099. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  10100. }
  10101. if (_valueSet[1]) {
  10102. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  10103. }
  10104. }
  10105. - (void)enumerateKeysAndValuesUsingBlock:
  10106. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10107. BOOL stop = NO;
  10108. if (_valueSet[0]) {
  10109. block(NO, _values[0], &stop);
  10110. }
  10111. if (!stop && _valueSet[1]) {
  10112. block(YES, _values[1], &stop);
  10113. }
  10114. }
  10115. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10116. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10117. NSUInteger count = 0;
  10118. size_t result = 0;
  10119. for (int i = 0; i < 2; ++i) {
  10120. if (_valueSet[i]) {
  10121. ++count;
  10122. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10123. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10124. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10125. }
  10126. }
  10127. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10128. result += tagSize * count;
  10129. return result;
  10130. }
  10131. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10132. asField:(GPBFieldDescriptor *)field {
  10133. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10134. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10135. for (int i = 0; i < 2; ++i) {
  10136. if (_valueSet[i]) {
  10137. // Write the tag.
  10138. [outputStream writeInt32NoTag:tag];
  10139. // Write the size of the message.
  10140. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10141. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10142. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10143. // Write the fields.
  10144. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10145. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10146. }
  10147. }
  10148. }
  10149. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10150. if (otherDictionary) {
  10151. for (int i = 0; i < 2; ++i) {
  10152. if (otherDictionary->_valueSet[i]) {
  10153. _valueSet[i] = YES;
  10154. _values[i] = otherDictionary->_values[i];
  10155. }
  10156. }
  10157. if (_autocreator) {
  10158. GPBAutocreatedDictionaryModified(_autocreator, self);
  10159. }
  10160. }
  10161. }
  10162. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  10163. int idx = (key ? 1 : 0);
  10164. _values[idx] = value;
  10165. _valueSet[idx] = YES;
  10166. if (_autocreator) {
  10167. GPBAutocreatedDictionaryModified(_autocreator, self);
  10168. }
  10169. }
  10170. - (void)removeValueForKey:(BOOL)aKey {
  10171. _valueSet[aKey ? 1 : 0] = NO;
  10172. }
  10173. - (void)removeAll {
  10174. _valueSet[0] = NO;
  10175. _valueSet[1] = NO;
  10176. }
  10177. @end
  10178. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10179. // This block of code is generated, do not edit it directly.
  10180. #pragma mark - Bool -> UInt64
  10181. @implementation GPBBoolUInt64Dictionary {
  10182. @package
  10183. uint64_t _values[2];
  10184. BOOL _valueSet[2];
  10185. }
  10186. + (instancetype)dictionary {
  10187. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10188. }
  10189. + (instancetype)dictionaryWithValue:(uint64_t)value
  10190. forKey:(BOOL)key {
  10191. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10192. // on to get the type correct.
  10193. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  10194. forKeys:&key
  10195. count:1] autorelease];
  10196. }
  10197. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  10198. forKeys:(const BOOL [])keys
  10199. count:(NSUInteger)count {
  10200. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10201. // on to get the type correct.
  10202. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  10203. forKeys:keys
  10204. count:count] autorelease];
  10205. }
  10206. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10207. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10208. // on to get the type correct.
  10209. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10210. }
  10211. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10212. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10213. }
  10214. - (instancetype)init {
  10215. return [self initWithValues:NULL forKeys:NULL count:0];
  10216. }
  10217. - (instancetype)initWithValues:(const uint64_t [])values
  10218. forKeys:(const BOOL [])keys
  10219. count:(NSUInteger)count {
  10220. self = [super init];
  10221. if (self) {
  10222. for (NSUInteger i = 0; i < count; ++i) {
  10223. int idx = keys[i] ? 1 : 0;
  10224. _values[idx] = values[i];
  10225. _valueSet[idx] = YES;
  10226. }
  10227. }
  10228. return self;
  10229. }
  10230. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10231. self = [self initWithValues:NULL forKeys:NULL count:0];
  10232. if (self) {
  10233. if (dictionary) {
  10234. for (int i = 0; i < 2; ++i) {
  10235. if (dictionary->_valueSet[i]) {
  10236. _values[i] = dictionary->_values[i];
  10237. _valueSet[i] = YES;
  10238. }
  10239. }
  10240. }
  10241. }
  10242. return self;
  10243. }
  10244. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10245. #pragma unused(numItems)
  10246. return [self initWithValues:NULL forKeys:NULL count:0];
  10247. }
  10248. #if !defined(NS_BLOCK_ASSERTIONS)
  10249. - (void)dealloc {
  10250. NSAssert(!_autocreator,
  10251. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10252. [self class], _autocreator);
  10253. [super dealloc];
  10254. }
  10255. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10256. - (instancetype)copyWithZone:(NSZone *)zone {
  10257. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10258. }
  10259. - (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
  10260. if (self == other) {
  10261. return YES;
  10262. }
  10263. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10264. return NO;
  10265. }
  10266. if ((_valueSet[0] != other->_valueSet[0]) ||
  10267. (_valueSet[1] != other->_valueSet[1])) {
  10268. return NO;
  10269. }
  10270. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10271. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10272. return NO;
  10273. }
  10274. return YES;
  10275. }
  10276. - (NSUInteger)hash {
  10277. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10278. }
  10279. - (NSString *)description {
  10280. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10281. if (_valueSet[0]) {
  10282. [result appendFormat:@"NO: %llu", _values[0]];
  10283. }
  10284. if (_valueSet[1]) {
  10285. [result appendFormat:@"YES: %llu", _values[1]];
  10286. }
  10287. [result appendString:@" }"];
  10288. return result;
  10289. }
  10290. - (NSUInteger)count {
  10291. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10292. }
  10293. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  10294. int idx = (key ? 1 : 0);
  10295. if (_valueSet[idx]) {
  10296. if (value) {
  10297. *value = _values[idx];
  10298. }
  10299. return YES;
  10300. }
  10301. return NO;
  10302. }
  10303. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10304. forGPBGenericValueKey:(GPBGenericValue *)key {
  10305. int idx = (key->valueBool ? 1 : 0);
  10306. _values[idx] = value->valueUInt64;
  10307. _valueSet[idx] = YES;
  10308. }
  10309. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10310. if (_valueSet[0]) {
  10311. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10312. }
  10313. if (_valueSet[1]) {
  10314. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10315. }
  10316. }
  10317. - (void)enumerateKeysAndValuesUsingBlock:
  10318. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10319. BOOL stop = NO;
  10320. if (_valueSet[0]) {
  10321. block(NO, _values[0], &stop);
  10322. }
  10323. if (!stop && _valueSet[1]) {
  10324. block(YES, _values[1], &stop);
  10325. }
  10326. }
  10327. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10328. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10329. NSUInteger count = 0;
  10330. size_t result = 0;
  10331. for (int i = 0; i < 2; ++i) {
  10332. if (_valueSet[i]) {
  10333. ++count;
  10334. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10335. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10336. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10337. }
  10338. }
  10339. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10340. result += tagSize * count;
  10341. return result;
  10342. }
  10343. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10344. asField:(GPBFieldDescriptor *)field {
  10345. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10346. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10347. for (int i = 0; i < 2; ++i) {
  10348. if (_valueSet[i]) {
  10349. // Write the tag.
  10350. [outputStream writeInt32NoTag:tag];
  10351. // Write the size of the message.
  10352. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10353. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10354. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10355. // Write the fields.
  10356. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10357. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10358. }
  10359. }
  10360. }
  10361. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10362. if (otherDictionary) {
  10363. for (int i = 0; i < 2; ++i) {
  10364. if (otherDictionary->_valueSet[i]) {
  10365. _valueSet[i] = YES;
  10366. _values[i] = otherDictionary->_values[i];
  10367. }
  10368. }
  10369. if (_autocreator) {
  10370. GPBAutocreatedDictionaryModified(_autocreator, self);
  10371. }
  10372. }
  10373. }
  10374. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  10375. int idx = (key ? 1 : 0);
  10376. _values[idx] = value;
  10377. _valueSet[idx] = YES;
  10378. if (_autocreator) {
  10379. GPBAutocreatedDictionaryModified(_autocreator, self);
  10380. }
  10381. }
  10382. - (void)removeValueForKey:(BOOL)aKey {
  10383. _valueSet[aKey ? 1 : 0] = NO;
  10384. }
  10385. - (void)removeAll {
  10386. _valueSet[0] = NO;
  10387. _valueSet[1] = NO;
  10388. }
  10389. @end
  10390. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10391. // This block of code is generated, do not edit it directly.
  10392. #pragma mark - Bool -> Int64
  10393. @implementation GPBBoolInt64Dictionary {
  10394. @package
  10395. int64_t _values[2];
  10396. BOOL _valueSet[2];
  10397. }
  10398. + (instancetype)dictionary {
  10399. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10400. }
  10401. + (instancetype)dictionaryWithValue:(int64_t)value
  10402. forKey:(BOOL)key {
  10403. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10404. // on to get the type correct.
  10405. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  10406. forKeys:&key
  10407. count:1] autorelease];
  10408. }
  10409. + (instancetype)dictionaryWithValues:(const int64_t [])values
  10410. forKeys:(const BOOL [])keys
  10411. count:(NSUInteger)count {
  10412. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10413. // on to get the type correct.
  10414. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  10415. forKeys:keys
  10416. count:count] autorelease];
  10417. }
  10418. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10419. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10420. // on to get the type correct.
  10421. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10422. }
  10423. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10424. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10425. }
  10426. - (instancetype)init {
  10427. return [self initWithValues:NULL forKeys:NULL count:0];
  10428. }
  10429. - (instancetype)initWithValues:(const int64_t [])values
  10430. forKeys:(const BOOL [])keys
  10431. count:(NSUInteger)count {
  10432. self = [super init];
  10433. if (self) {
  10434. for (NSUInteger i = 0; i < count; ++i) {
  10435. int idx = keys[i] ? 1 : 0;
  10436. _values[idx] = values[i];
  10437. _valueSet[idx] = YES;
  10438. }
  10439. }
  10440. return self;
  10441. }
  10442. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10443. self = [self initWithValues:NULL forKeys:NULL count:0];
  10444. if (self) {
  10445. if (dictionary) {
  10446. for (int i = 0; i < 2; ++i) {
  10447. if (dictionary->_valueSet[i]) {
  10448. _values[i] = dictionary->_values[i];
  10449. _valueSet[i] = YES;
  10450. }
  10451. }
  10452. }
  10453. }
  10454. return self;
  10455. }
  10456. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10457. #pragma unused(numItems)
  10458. return [self initWithValues:NULL forKeys:NULL count:0];
  10459. }
  10460. #if !defined(NS_BLOCK_ASSERTIONS)
  10461. - (void)dealloc {
  10462. NSAssert(!_autocreator,
  10463. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10464. [self class], _autocreator);
  10465. [super dealloc];
  10466. }
  10467. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10468. - (instancetype)copyWithZone:(NSZone *)zone {
  10469. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10470. }
  10471. - (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
  10472. if (self == other) {
  10473. return YES;
  10474. }
  10475. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10476. return NO;
  10477. }
  10478. if ((_valueSet[0] != other->_valueSet[0]) ||
  10479. (_valueSet[1] != other->_valueSet[1])) {
  10480. return NO;
  10481. }
  10482. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10483. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10484. return NO;
  10485. }
  10486. return YES;
  10487. }
  10488. - (NSUInteger)hash {
  10489. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10490. }
  10491. - (NSString *)description {
  10492. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10493. if (_valueSet[0]) {
  10494. [result appendFormat:@"NO: %lld", _values[0]];
  10495. }
  10496. if (_valueSet[1]) {
  10497. [result appendFormat:@"YES: %lld", _values[1]];
  10498. }
  10499. [result appendString:@" }"];
  10500. return result;
  10501. }
  10502. - (NSUInteger)count {
  10503. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10504. }
  10505. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  10506. int idx = (key ? 1 : 0);
  10507. if (_valueSet[idx]) {
  10508. if (value) {
  10509. *value = _values[idx];
  10510. }
  10511. return YES;
  10512. }
  10513. return NO;
  10514. }
  10515. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10516. forGPBGenericValueKey:(GPBGenericValue *)key {
  10517. int idx = (key->valueBool ? 1 : 0);
  10518. _values[idx] = value->valueInt64;
  10519. _valueSet[idx] = YES;
  10520. }
  10521. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10522. if (_valueSet[0]) {
  10523. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10524. }
  10525. if (_valueSet[1]) {
  10526. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10527. }
  10528. }
  10529. - (void)enumerateKeysAndValuesUsingBlock:
  10530. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10531. BOOL stop = NO;
  10532. if (_valueSet[0]) {
  10533. block(NO, _values[0], &stop);
  10534. }
  10535. if (!stop && _valueSet[1]) {
  10536. block(YES, _values[1], &stop);
  10537. }
  10538. }
  10539. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10540. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10541. NSUInteger count = 0;
  10542. size_t result = 0;
  10543. for (int i = 0; i < 2; ++i) {
  10544. if (_valueSet[i]) {
  10545. ++count;
  10546. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10547. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10548. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10549. }
  10550. }
  10551. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10552. result += tagSize * count;
  10553. return result;
  10554. }
  10555. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10556. asField:(GPBFieldDescriptor *)field {
  10557. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10558. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10559. for (int i = 0; i < 2; ++i) {
  10560. if (_valueSet[i]) {
  10561. // Write the tag.
  10562. [outputStream writeInt32NoTag:tag];
  10563. // Write the size of the message.
  10564. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10565. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10566. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10567. // Write the fields.
  10568. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10569. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10570. }
  10571. }
  10572. }
  10573. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10574. if (otherDictionary) {
  10575. for (int i = 0; i < 2; ++i) {
  10576. if (otherDictionary->_valueSet[i]) {
  10577. _valueSet[i] = YES;
  10578. _values[i] = otherDictionary->_values[i];
  10579. }
  10580. }
  10581. if (_autocreator) {
  10582. GPBAutocreatedDictionaryModified(_autocreator, self);
  10583. }
  10584. }
  10585. }
  10586. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  10587. int idx = (key ? 1 : 0);
  10588. _values[idx] = value;
  10589. _valueSet[idx] = YES;
  10590. if (_autocreator) {
  10591. GPBAutocreatedDictionaryModified(_autocreator, self);
  10592. }
  10593. }
  10594. - (void)removeValueForKey:(BOOL)aKey {
  10595. _valueSet[aKey ? 1 : 0] = NO;
  10596. }
  10597. - (void)removeAll {
  10598. _valueSet[0] = NO;
  10599. _valueSet[1] = NO;
  10600. }
  10601. @end
  10602. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10603. // This block of code is generated, do not edit it directly.
  10604. #pragma mark - Bool -> Bool
  10605. @implementation GPBBoolBoolDictionary {
  10606. @package
  10607. BOOL _values[2];
  10608. BOOL _valueSet[2];
  10609. }
  10610. + (instancetype)dictionary {
  10611. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10612. }
  10613. + (instancetype)dictionaryWithValue:(BOOL)value
  10614. forKey:(BOOL)key {
  10615. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10616. // on to get the type correct.
  10617. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  10618. forKeys:&key
  10619. count:1] autorelease];
  10620. }
  10621. + (instancetype)dictionaryWithValues:(const BOOL [])values
  10622. forKeys:(const BOOL [])keys
  10623. count:(NSUInteger)count {
  10624. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10625. // on to get the type correct.
  10626. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  10627. forKeys:keys
  10628. count:count] autorelease];
  10629. }
  10630. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10631. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10632. // on to get the type correct.
  10633. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10634. }
  10635. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10636. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10637. }
  10638. - (instancetype)init {
  10639. return [self initWithValues:NULL forKeys:NULL count:0];
  10640. }
  10641. - (instancetype)initWithValues:(const BOOL [])values
  10642. forKeys:(const BOOL [])keys
  10643. count:(NSUInteger)count {
  10644. self = [super init];
  10645. if (self) {
  10646. for (NSUInteger i = 0; i < count; ++i) {
  10647. int idx = keys[i] ? 1 : 0;
  10648. _values[idx] = values[i];
  10649. _valueSet[idx] = YES;
  10650. }
  10651. }
  10652. return self;
  10653. }
  10654. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10655. self = [self initWithValues:NULL forKeys:NULL count:0];
  10656. if (self) {
  10657. if (dictionary) {
  10658. for (int i = 0; i < 2; ++i) {
  10659. if (dictionary->_valueSet[i]) {
  10660. _values[i] = dictionary->_values[i];
  10661. _valueSet[i] = YES;
  10662. }
  10663. }
  10664. }
  10665. }
  10666. return self;
  10667. }
  10668. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10669. #pragma unused(numItems)
  10670. return [self initWithValues:NULL forKeys:NULL count:0];
  10671. }
  10672. #if !defined(NS_BLOCK_ASSERTIONS)
  10673. - (void)dealloc {
  10674. NSAssert(!_autocreator,
  10675. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10676. [self class], _autocreator);
  10677. [super dealloc];
  10678. }
  10679. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10680. - (instancetype)copyWithZone:(NSZone *)zone {
  10681. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10682. }
  10683. - (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
  10684. if (self == other) {
  10685. return YES;
  10686. }
  10687. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10688. return NO;
  10689. }
  10690. if ((_valueSet[0] != other->_valueSet[0]) ||
  10691. (_valueSet[1] != other->_valueSet[1])) {
  10692. return NO;
  10693. }
  10694. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10695. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10696. return NO;
  10697. }
  10698. return YES;
  10699. }
  10700. - (NSUInteger)hash {
  10701. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10702. }
  10703. - (NSString *)description {
  10704. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10705. if (_valueSet[0]) {
  10706. [result appendFormat:@"NO: %d", _values[0]];
  10707. }
  10708. if (_valueSet[1]) {
  10709. [result appendFormat:@"YES: %d", _values[1]];
  10710. }
  10711. [result appendString:@" }"];
  10712. return result;
  10713. }
  10714. - (NSUInteger)count {
  10715. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10716. }
  10717. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10718. int idx = (key ? 1 : 0);
  10719. if (_valueSet[idx]) {
  10720. if (value) {
  10721. *value = _values[idx];
  10722. }
  10723. return YES;
  10724. }
  10725. return NO;
  10726. }
  10727. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10728. forGPBGenericValueKey:(GPBGenericValue *)key {
  10729. int idx = (key->valueBool ? 1 : 0);
  10730. _values[idx] = value->valueBool;
  10731. _valueSet[idx] = YES;
  10732. }
  10733. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10734. if (_valueSet[0]) {
  10735. block(@"false", (_values[0] ? @"true" : @"false"));
  10736. }
  10737. if (_valueSet[1]) {
  10738. block(@"true", (_values[1] ? @"true" : @"false"));
  10739. }
  10740. }
  10741. - (void)enumerateKeysAndValuesUsingBlock:
  10742. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10743. BOOL stop = NO;
  10744. if (_valueSet[0]) {
  10745. block(NO, _values[0], &stop);
  10746. }
  10747. if (!stop && _valueSet[1]) {
  10748. block(YES, _values[1], &stop);
  10749. }
  10750. }
  10751. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10752. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10753. NSUInteger count = 0;
  10754. size_t result = 0;
  10755. for (int i = 0; i < 2; ++i) {
  10756. if (_valueSet[i]) {
  10757. ++count;
  10758. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10759. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10760. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10761. }
  10762. }
  10763. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10764. result += tagSize * count;
  10765. return result;
  10766. }
  10767. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10768. asField:(GPBFieldDescriptor *)field {
  10769. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10770. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10771. for (int i = 0; i < 2; ++i) {
  10772. if (_valueSet[i]) {
  10773. // Write the tag.
  10774. [outputStream writeInt32NoTag:tag];
  10775. // Write the size of the message.
  10776. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10777. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10778. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10779. // Write the fields.
  10780. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10781. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10782. }
  10783. }
  10784. }
  10785. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10786. if (otherDictionary) {
  10787. for (int i = 0; i < 2; ++i) {
  10788. if (otherDictionary->_valueSet[i]) {
  10789. _valueSet[i] = YES;
  10790. _values[i] = otherDictionary->_values[i];
  10791. }
  10792. }
  10793. if (_autocreator) {
  10794. GPBAutocreatedDictionaryModified(_autocreator, self);
  10795. }
  10796. }
  10797. }
  10798. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10799. int idx = (key ? 1 : 0);
  10800. _values[idx] = value;
  10801. _valueSet[idx] = YES;
  10802. if (_autocreator) {
  10803. GPBAutocreatedDictionaryModified(_autocreator, self);
  10804. }
  10805. }
  10806. - (void)removeValueForKey:(BOOL)aKey {
  10807. _valueSet[aKey ? 1 : 0] = NO;
  10808. }
  10809. - (void)removeAll {
  10810. _valueSet[0] = NO;
  10811. _valueSet[1] = NO;
  10812. }
  10813. @end
  10814. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10815. // This block of code is generated, do not edit it directly.
  10816. #pragma mark - Bool -> Float
  10817. @implementation GPBBoolFloatDictionary {
  10818. @package
  10819. float _values[2];
  10820. BOOL _valueSet[2];
  10821. }
  10822. + (instancetype)dictionary {
  10823. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10824. }
  10825. + (instancetype)dictionaryWithValue:(float)value
  10826. forKey:(BOOL)key {
  10827. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10828. // on to get the type correct.
  10829. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10830. forKeys:&key
  10831. count:1] autorelease];
  10832. }
  10833. + (instancetype)dictionaryWithValues:(const float [])values
  10834. forKeys:(const BOOL [])keys
  10835. count:(NSUInteger)count {
  10836. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10837. // on to get the type correct.
  10838. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10839. forKeys:keys
  10840. count:count] autorelease];
  10841. }
  10842. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10843. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10844. // on to get the type correct.
  10845. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10846. }
  10847. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10848. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10849. }
  10850. - (instancetype)init {
  10851. return [self initWithValues:NULL forKeys:NULL count:0];
  10852. }
  10853. - (instancetype)initWithValues:(const float [])values
  10854. forKeys:(const BOOL [])keys
  10855. count:(NSUInteger)count {
  10856. self = [super init];
  10857. if (self) {
  10858. for (NSUInteger i = 0; i < count; ++i) {
  10859. int idx = keys[i] ? 1 : 0;
  10860. _values[idx] = values[i];
  10861. _valueSet[idx] = YES;
  10862. }
  10863. }
  10864. return self;
  10865. }
  10866. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10867. self = [self initWithValues:NULL forKeys:NULL count:0];
  10868. if (self) {
  10869. if (dictionary) {
  10870. for (int i = 0; i < 2; ++i) {
  10871. if (dictionary->_valueSet[i]) {
  10872. _values[i] = dictionary->_values[i];
  10873. _valueSet[i] = YES;
  10874. }
  10875. }
  10876. }
  10877. }
  10878. return self;
  10879. }
  10880. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10881. #pragma unused(numItems)
  10882. return [self initWithValues:NULL forKeys:NULL count:0];
  10883. }
  10884. #if !defined(NS_BLOCK_ASSERTIONS)
  10885. - (void)dealloc {
  10886. NSAssert(!_autocreator,
  10887. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10888. [self class], _autocreator);
  10889. [super dealloc];
  10890. }
  10891. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10892. - (instancetype)copyWithZone:(NSZone *)zone {
  10893. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10894. }
  10895. - (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
  10896. if (self == other) {
  10897. return YES;
  10898. }
  10899. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10900. return NO;
  10901. }
  10902. if ((_valueSet[0] != other->_valueSet[0]) ||
  10903. (_valueSet[1] != other->_valueSet[1])) {
  10904. return NO;
  10905. }
  10906. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10907. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10908. return NO;
  10909. }
  10910. return YES;
  10911. }
  10912. - (NSUInteger)hash {
  10913. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10914. }
  10915. - (NSString *)description {
  10916. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10917. if (_valueSet[0]) {
  10918. [result appendFormat:@"NO: %f", _values[0]];
  10919. }
  10920. if (_valueSet[1]) {
  10921. [result appendFormat:@"YES: %f", _values[1]];
  10922. }
  10923. [result appendString:@" }"];
  10924. return result;
  10925. }
  10926. - (NSUInteger)count {
  10927. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10928. }
  10929. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  10930. int idx = (key ? 1 : 0);
  10931. if (_valueSet[idx]) {
  10932. if (value) {
  10933. *value = _values[idx];
  10934. }
  10935. return YES;
  10936. }
  10937. return NO;
  10938. }
  10939. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10940. forGPBGenericValueKey:(GPBGenericValue *)key {
  10941. int idx = (key->valueBool ? 1 : 0);
  10942. _values[idx] = value->valueFloat;
  10943. _valueSet[idx] = YES;
  10944. }
  10945. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10946. if (_valueSet[0]) {
  10947. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  10948. }
  10949. if (_valueSet[1]) {
  10950. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  10951. }
  10952. }
  10953. - (void)enumerateKeysAndValuesUsingBlock:
  10954. (void (^)(BOOL key, float value, BOOL *stop))block {
  10955. BOOL stop = NO;
  10956. if (_valueSet[0]) {
  10957. block(NO, _values[0], &stop);
  10958. }
  10959. if (!stop && _valueSet[1]) {
  10960. block(YES, _values[1], &stop);
  10961. }
  10962. }
  10963. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10964. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10965. NSUInteger count = 0;
  10966. size_t result = 0;
  10967. for (int i = 0; i < 2; ++i) {
  10968. if (_valueSet[i]) {
  10969. ++count;
  10970. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10971. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10972. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10973. }
  10974. }
  10975. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10976. result += tagSize * count;
  10977. return result;
  10978. }
  10979. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10980. asField:(GPBFieldDescriptor *)field {
  10981. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10982. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10983. for (int i = 0; i < 2; ++i) {
  10984. if (_valueSet[i]) {
  10985. // Write the tag.
  10986. [outputStream writeInt32NoTag:tag];
  10987. // Write the size of the message.
  10988. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10989. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10990. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10991. // Write the fields.
  10992. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10993. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10994. }
  10995. }
  10996. }
  10997. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  10998. if (otherDictionary) {
  10999. for (int i = 0; i < 2; ++i) {
  11000. if (otherDictionary->_valueSet[i]) {
  11001. _valueSet[i] = YES;
  11002. _values[i] = otherDictionary->_values[i];
  11003. }
  11004. }
  11005. if (_autocreator) {
  11006. GPBAutocreatedDictionaryModified(_autocreator, self);
  11007. }
  11008. }
  11009. }
  11010. - (void)setValue:(float)value forKey:(BOOL)key {
  11011. int idx = (key ? 1 : 0);
  11012. _values[idx] = value;
  11013. _valueSet[idx] = YES;
  11014. if (_autocreator) {
  11015. GPBAutocreatedDictionaryModified(_autocreator, self);
  11016. }
  11017. }
  11018. - (void)removeValueForKey:(BOOL)aKey {
  11019. _valueSet[aKey ? 1 : 0] = NO;
  11020. }
  11021. - (void)removeAll {
  11022. _valueSet[0] = NO;
  11023. _valueSet[1] = NO;
  11024. }
  11025. @end
  11026. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  11027. // This block of code is generated, do not edit it directly.
  11028. #pragma mark - Bool -> Double
  11029. @implementation GPBBoolDoubleDictionary {
  11030. @package
  11031. double _values[2];
  11032. BOOL _valueSet[2];
  11033. }
  11034. + (instancetype)dictionary {
  11035. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  11036. }
  11037. + (instancetype)dictionaryWithValue:(double)value
  11038. forKey:(BOOL)key {
  11039. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11040. // on to get the type correct.
  11041. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  11042. forKeys:&key
  11043. count:1] autorelease];
  11044. }
  11045. + (instancetype)dictionaryWithValues:(const double [])values
  11046. forKeys:(const BOOL [])keys
  11047. count:(NSUInteger)count {
  11048. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11049. // on to get the type correct.
  11050. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  11051. forKeys:keys
  11052. count:count] autorelease];
  11053. }
  11054. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11055. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11056. // on to get the type correct.
  11057. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11058. }
  11059. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11060. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11061. }
  11062. - (instancetype)init {
  11063. return [self initWithValues:NULL forKeys:NULL count:0];
  11064. }
  11065. - (instancetype)initWithValues:(const double [])values
  11066. forKeys:(const BOOL [])keys
  11067. count:(NSUInteger)count {
  11068. self = [super init];
  11069. if (self) {
  11070. for (NSUInteger i = 0; i < count; ++i) {
  11071. int idx = keys[i] ? 1 : 0;
  11072. _values[idx] = values[i];
  11073. _valueSet[idx] = YES;
  11074. }
  11075. }
  11076. return self;
  11077. }
  11078. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11079. self = [self initWithValues:NULL forKeys:NULL count:0];
  11080. if (self) {
  11081. if (dictionary) {
  11082. for (int i = 0; i < 2; ++i) {
  11083. if (dictionary->_valueSet[i]) {
  11084. _values[i] = dictionary->_values[i];
  11085. _valueSet[i] = YES;
  11086. }
  11087. }
  11088. }
  11089. }
  11090. return self;
  11091. }
  11092. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11093. #pragma unused(numItems)
  11094. return [self initWithValues:NULL forKeys:NULL count:0];
  11095. }
  11096. #if !defined(NS_BLOCK_ASSERTIONS)
  11097. - (void)dealloc {
  11098. NSAssert(!_autocreator,
  11099. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11100. [self class], _autocreator);
  11101. [super dealloc];
  11102. }
  11103. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11104. - (instancetype)copyWithZone:(NSZone *)zone {
  11105. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  11106. }
  11107. - (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
  11108. if (self == other) {
  11109. return YES;
  11110. }
  11111. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  11112. return NO;
  11113. }
  11114. if ((_valueSet[0] != other->_valueSet[0]) ||
  11115. (_valueSet[1] != other->_valueSet[1])) {
  11116. return NO;
  11117. }
  11118. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11119. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11120. return NO;
  11121. }
  11122. return YES;
  11123. }
  11124. - (NSUInteger)hash {
  11125. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11126. }
  11127. - (NSString *)description {
  11128. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11129. if (_valueSet[0]) {
  11130. [result appendFormat:@"NO: %lf", _values[0]];
  11131. }
  11132. if (_valueSet[1]) {
  11133. [result appendFormat:@"YES: %lf", _values[1]];
  11134. }
  11135. [result appendString:@" }"];
  11136. return result;
  11137. }
  11138. - (NSUInteger)count {
  11139. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11140. }
  11141. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  11142. int idx = (key ? 1 : 0);
  11143. if (_valueSet[idx]) {
  11144. if (value) {
  11145. *value = _values[idx];
  11146. }
  11147. return YES;
  11148. }
  11149. return NO;
  11150. }
  11151. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11152. forGPBGenericValueKey:(GPBGenericValue *)key {
  11153. int idx = (key->valueBool ? 1 : 0);
  11154. _values[idx] = value->valueDouble;
  11155. _valueSet[idx] = YES;
  11156. }
  11157. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11158. if (_valueSet[0]) {
  11159. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11160. }
  11161. if (_valueSet[1]) {
  11162. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11163. }
  11164. }
  11165. - (void)enumerateKeysAndValuesUsingBlock:
  11166. (void (^)(BOOL key, double value, BOOL *stop))block {
  11167. BOOL stop = NO;
  11168. if (_valueSet[0]) {
  11169. block(NO, _values[0], &stop);
  11170. }
  11171. if (!stop && _valueSet[1]) {
  11172. block(YES, _values[1], &stop);
  11173. }
  11174. }
  11175. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11176. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11177. NSUInteger count = 0;
  11178. size_t result = 0;
  11179. for (int i = 0; i < 2; ++i) {
  11180. if (_valueSet[i]) {
  11181. ++count;
  11182. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11183. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11184. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11185. }
  11186. }
  11187. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11188. result += tagSize * count;
  11189. return result;
  11190. }
  11191. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11192. asField:(GPBFieldDescriptor *)field {
  11193. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11194. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11195. for (int i = 0; i < 2; ++i) {
  11196. if (_valueSet[i]) {
  11197. // Write the tag.
  11198. [outputStream writeInt32NoTag:tag];
  11199. // Write the size of the message.
  11200. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11201. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11202. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11203. // Write the fields.
  11204. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11205. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11206. }
  11207. }
  11208. }
  11209. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11210. if (otherDictionary) {
  11211. for (int i = 0; i < 2; ++i) {
  11212. if (otherDictionary->_valueSet[i]) {
  11213. _valueSet[i] = YES;
  11214. _values[i] = otherDictionary->_values[i];
  11215. }
  11216. }
  11217. if (_autocreator) {
  11218. GPBAutocreatedDictionaryModified(_autocreator, self);
  11219. }
  11220. }
  11221. }
  11222. - (void)setValue:(double)value forKey:(BOOL)key {
  11223. int idx = (key ? 1 : 0);
  11224. _values[idx] = value;
  11225. _valueSet[idx] = YES;
  11226. if (_autocreator) {
  11227. GPBAutocreatedDictionaryModified(_autocreator, self);
  11228. }
  11229. }
  11230. - (void)removeValueForKey:(BOOL)aKey {
  11231. _valueSet[aKey ? 1 : 0] = NO;
  11232. }
  11233. - (void)removeAll {
  11234. _valueSet[0] = NO;
  11235. _valueSet[1] = NO;
  11236. }
  11237. @end
  11238. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11239. // This block of code is generated, do not edit it directly.
  11240. #pragma mark - Bool -> Object
  11241. @implementation GPBBoolObjectDictionary {
  11242. @package
  11243. id _values[2];
  11244. }
  11245. + (instancetype)dictionary {
  11246. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11247. }
  11248. + (instancetype)dictionaryWithObject:(id)object
  11249. forKey:(BOOL)key {
  11250. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11251. // on to get the type correct.
  11252. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11253. forKeys:&key
  11254. count:1] autorelease];
  11255. }
  11256. + (instancetype)dictionaryWithObjects:(const id [])objects
  11257. forKeys:(const BOOL [])keys
  11258. count:(NSUInteger)count {
  11259. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11260. // on to get the type correct.
  11261. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11262. forKeys:keys
  11263. count:count] autorelease];
  11264. }
  11265. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11266. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11267. // on to get the type correct.
  11268. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11269. }
  11270. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11271. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11272. }
  11273. - (instancetype)init {
  11274. return [self initWithObjects:NULL forKeys:NULL count:0];
  11275. }
  11276. - (instancetype)initWithObjects:(const id [])objects
  11277. forKeys:(const BOOL [])keys
  11278. count:(NSUInteger)count {
  11279. self = [super init];
  11280. if (self) {
  11281. for (NSUInteger i = 0; i < count; ++i) {
  11282. if (!objects[i]) {
  11283. [NSException raise:NSInvalidArgumentException
  11284. format:@"Attempting to add nil object to a Dictionary"];
  11285. }
  11286. int idx = keys[i] ? 1 : 0;
  11287. [_values[idx] release];
  11288. _values[idx] = (id)[objects[i] retain];
  11289. }
  11290. }
  11291. return self;
  11292. }
  11293. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11294. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11295. if (self) {
  11296. if (dictionary) {
  11297. _values[0] = [dictionary->_values[0] retain];
  11298. _values[1] = [dictionary->_values[1] retain];
  11299. }
  11300. }
  11301. return self;
  11302. }
  11303. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11304. #pragma unused(numItems)
  11305. return [self initWithObjects:NULL forKeys:NULL count:0];
  11306. }
  11307. - (void)dealloc {
  11308. NSAssert(!_autocreator,
  11309. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11310. [self class], _autocreator);
  11311. [_values[0] release];
  11312. [_values[1] release];
  11313. [super dealloc];
  11314. }
  11315. - (instancetype)copyWithZone:(NSZone *)zone {
  11316. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11317. }
  11318. - (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
  11319. if (self == other) {
  11320. return YES;
  11321. }
  11322. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11323. return NO;
  11324. }
  11325. if (((_values[0] != nil) != (other->_values[0] != nil)) ||
  11326. ((_values[1] != nil) != (other->_values[1] != nil))) {
  11327. return NO;
  11328. }
  11329. if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
  11330. ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
  11331. return NO;
  11332. }
  11333. return YES;
  11334. }
  11335. - (NSUInteger)hash {
  11336. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11337. }
  11338. - (NSString *)description {
  11339. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11340. if ((_values[0] != nil)) {
  11341. [result appendFormat:@"NO: %@", _values[0]];
  11342. }
  11343. if ((_values[1] != nil)) {
  11344. [result appendFormat:@"YES: %@", _values[1]];
  11345. }
  11346. [result appendString:@" }"];
  11347. return result;
  11348. }
  11349. - (NSUInteger)count {
  11350. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11351. }
  11352. - (id)objectForKey:(BOOL)key {
  11353. return _values[key ? 1 : 0];
  11354. }
  11355. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11356. forGPBGenericValueKey:(GPBGenericValue *)key {
  11357. int idx = (key->valueBool ? 1 : 0);
  11358. [_values[idx] release];
  11359. _values[idx] = [value->valueString retain];
  11360. }
  11361. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11362. if (_values[0] != nil) {
  11363. block(@"false", _values[0]);
  11364. }
  11365. if ((_values[1] != nil)) {
  11366. block(@"true", _values[1]);
  11367. }
  11368. }
  11369. - (void)enumerateKeysAndObjectsUsingBlock:
  11370. (void (^)(BOOL key, id object, BOOL *stop))block {
  11371. BOOL stop = NO;
  11372. if (_values[0] != nil) {
  11373. block(NO, _values[0], &stop);
  11374. }
  11375. if (!stop && (_values[1] != nil)) {
  11376. block(YES, _values[1], &stop);
  11377. }
  11378. }
  11379. - (BOOL)isInitialized {
  11380. if (_values[0] && ![_values[0] isInitialized]) {
  11381. return NO;
  11382. }
  11383. if (_values[1] && ![_values[1] isInitialized]) {
  11384. return NO;
  11385. }
  11386. return YES;
  11387. }
  11388. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11389. GPBBoolObjectDictionary *newDict =
  11390. [[GPBBoolObjectDictionary alloc] init];
  11391. for (int i = 0; i < 2; ++i) {
  11392. if (_values[i] != nil) {
  11393. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11394. }
  11395. }
  11396. return newDict;
  11397. }
  11398. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11399. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11400. NSUInteger count = 0;
  11401. size_t result = 0;
  11402. for (int i = 0; i < 2; ++i) {
  11403. if (_values[i] != nil) {
  11404. ++count;
  11405. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11406. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11407. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11408. }
  11409. }
  11410. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11411. result += tagSize * count;
  11412. return result;
  11413. }
  11414. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11415. asField:(GPBFieldDescriptor *)field {
  11416. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11417. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11418. for (int i = 0; i < 2; ++i) {
  11419. if (_values[i] != nil) {
  11420. // Write the tag.
  11421. [outputStream writeInt32NoTag:tag];
  11422. // Write the size of the message.
  11423. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11424. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11425. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11426. // Write the fields.
  11427. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11428. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11429. }
  11430. }
  11431. }
  11432. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11433. if (otherDictionary) {
  11434. for (int i = 0; i < 2; ++i) {
  11435. if (otherDictionary->_values[i] != nil) {
  11436. [_values[i] release];
  11437. _values[i] = [otherDictionary->_values[i] retain];
  11438. }
  11439. }
  11440. if (_autocreator) {
  11441. GPBAutocreatedDictionaryModified(_autocreator, self);
  11442. }
  11443. }
  11444. }
  11445. - (void)setObject:(id)object forKey:(BOOL)key {
  11446. if (!object) {
  11447. [NSException raise:NSInvalidArgumentException
  11448. format:@"Attempting to add nil object to a Dictionary"];
  11449. }
  11450. int idx = (key ? 1 : 0);
  11451. [_values[idx] release];
  11452. _values[idx] = [object retain];
  11453. if (_autocreator) {
  11454. GPBAutocreatedDictionaryModified(_autocreator, self);
  11455. }
  11456. }
  11457. - (void)removeObjectForKey:(BOOL)aKey {
  11458. int idx = (aKey ? 1 : 0);
  11459. [_values[idx] release];
  11460. _values[idx] = nil;
  11461. }
  11462. - (void)removeAll {
  11463. for (int i = 0; i < 2; ++i) {
  11464. [_values[i] release];
  11465. _values[i] = nil;
  11466. }
  11467. }
  11468. @end
  11469. //%PDDM-EXPAND-END (8 expansions)
  11470. #pragma mark - Bool -> Enum
  11471. @implementation GPBBoolEnumDictionary {
  11472. @package
  11473. GPBEnumValidationFunc _validationFunc;
  11474. int32_t _values[2];
  11475. BOOL _valueSet[2];
  11476. }
  11477. @synthesize validationFunc = _validationFunc;
  11478. + (instancetype)dictionary {
  11479. return [[[self alloc] initWithValidationFunction:NULL
  11480. rawValues:NULL
  11481. forKeys:NULL
  11482. count:0] autorelease];
  11483. }
  11484. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11485. return [[[self alloc] initWithValidationFunction:func
  11486. rawValues:NULL
  11487. forKeys:NULL
  11488. count:0] autorelease];
  11489. }
  11490. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11491. rawValue:(int32_t)rawValue
  11492. forKey:(BOOL)key {
  11493. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11494. // on to get the type correct.
  11495. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11496. rawValues:&rawValue
  11497. forKeys:&key
  11498. count:1] autorelease];
  11499. }
  11500. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11501. rawValues:(const int32_t [])values
  11502. forKeys:(const BOOL [])keys
  11503. count:(NSUInteger)count {
  11504. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11505. // on to get the type correct.
  11506. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11507. rawValues:values
  11508. forKeys:keys
  11509. count:count] autorelease];
  11510. }
  11511. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11512. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11513. // on to get the type correct.
  11514. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11515. }
  11516. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11517. capacity:(NSUInteger)numItems {
  11518. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11519. }
  11520. - (instancetype)init {
  11521. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11522. }
  11523. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11524. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11525. }
  11526. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11527. rawValues:(const int32_t [])rawValues
  11528. forKeys:(const BOOL [])keys
  11529. count:(NSUInteger)count {
  11530. self = [super init];
  11531. if (self) {
  11532. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11533. for (NSUInteger i = 0; i < count; ++i) {
  11534. int idx = keys[i] ? 1 : 0;
  11535. _values[idx] = rawValues[i];
  11536. _valueSet[idx] = YES;
  11537. }
  11538. }
  11539. return self;
  11540. }
  11541. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11542. self = [self initWithValidationFunction:dictionary.validationFunc
  11543. rawValues:NULL
  11544. forKeys:NULL
  11545. count:0];
  11546. if (self) {
  11547. if (dictionary) {
  11548. for (int i = 0; i < 2; ++i) {
  11549. if (dictionary->_valueSet[i]) {
  11550. _values[i] = dictionary->_values[i];
  11551. _valueSet[i] = YES;
  11552. }
  11553. }
  11554. }
  11555. }
  11556. return self;
  11557. }
  11558. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11559. capacity:(NSUInteger)numItems {
  11560. #pragma unused(numItems)
  11561. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11562. }
  11563. #if !defined(NS_BLOCK_ASSERTIONS)
  11564. - (void)dealloc {
  11565. NSAssert(!_autocreator,
  11566. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11567. [self class], _autocreator);
  11568. [super dealloc];
  11569. }
  11570. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11571. - (instancetype)copyWithZone:(NSZone *)zone {
  11572. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11573. }
  11574. - (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
  11575. if (self == other) {
  11576. return YES;
  11577. }
  11578. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11579. return NO;
  11580. }
  11581. if ((_valueSet[0] != other->_valueSet[0]) ||
  11582. (_valueSet[1] != other->_valueSet[1])) {
  11583. return NO;
  11584. }
  11585. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11586. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11587. return NO;
  11588. }
  11589. return YES;
  11590. }
  11591. - (NSUInteger)hash {
  11592. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11593. }
  11594. - (NSString *)description {
  11595. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11596. if (_valueSet[0]) {
  11597. [result appendFormat:@"NO: %d", _values[0]];
  11598. }
  11599. if (_valueSet[1]) {
  11600. [result appendFormat:@"YES: %d", _values[1]];
  11601. }
  11602. [result appendString:@" }"];
  11603. return result;
  11604. }
  11605. - (NSUInteger)count {
  11606. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11607. }
  11608. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  11609. int idx = (key ? 1 : 0);
  11610. if (_valueSet[idx]) {
  11611. if (value) {
  11612. int32_t result = _values[idx];
  11613. if (!_validationFunc(result)) {
  11614. result = kGPBUnrecognizedEnumeratorValue;
  11615. }
  11616. *value = result;
  11617. }
  11618. return YES;
  11619. }
  11620. return NO;
  11621. }
  11622. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  11623. int idx = (key ? 1 : 0);
  11624. if (_valueSet[idx]) {
  11625. if (rawValue) {
  11626. *rawValue = _values[idx];
  11627. }
  11628. return YES;
  11629. }
  11630. return NO;
  11631. }
  11632. - (void)enumerateKeysAndValuesUsingBlock:
  11633. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11634. BOOL stop = NO;
  11635. if (_valueSet[0]) {
  11636. block(NO, _values[0], &stop);
  11637. }
  11638. if (!stop && _valueSet[1]) {
  11639. block(YES, _values[1], &stop);
  11640. }
  11641. }
  11642. - (void)enumerateKeysAndRawValuesUsingBlock:
  11643. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11644. BOOL stop = NO;
  11645. GPBEnumValidationFunc func = _validationFunc;
  11646. int32_t validatedValue;
  11647. if (_valueSet[0]) {
  11648. validatedValue = _values[0];
  11649. if (!func(validatedValue)) {
  11650. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11651. }
  11652. block(NO, validatedValue, &stop);
  11653. }
  11654. if (!stop && _valueSet[1]) {
  11655. validatedValue = _values[1];
  11656. if (!func(validatedValue)) {
  11657. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11658. }
  11659. block(YES, validatedValue, &stop);
  11660. }
  11661. }
  11662. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11663. // This block of code is generated, do not edit it directly.
  11664. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11665. forKey:(GPBGenericValue *)key
  11666. keyDataType:(GPBDataType)keyDataType {
  11667. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11668. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11669. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11670. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11671. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11672. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11673. [outputStream release];
  11674. return data;
  11675. }
  11676. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11677. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11678. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11679. NSUInteger count = 0;
  11680. size_t result = 0;
  11681. for (int i = 0; i < 2; ++i) {
  11682. if (_valueSet[i]) {
  11683. ++count;
  11684. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11685. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11686. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11687. }
  11688. }
  11689. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11690. result += tagSize * count;
  11691. return result;
  11692. }
  11693. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11694. asField:(GPBFieldDescriptor *)field {
  11695. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11696. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11697. for (int i = 0; i < 2; ++i) {
  11698. if (_valueSet[i]) {
  11699. // Write the tag.
  11700. [outputStream writeInt32NoTag:tag];
  11701. // Write the size of the message.
  11702. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11703. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11704. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11705. // Write the fields.
  11706. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11707. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11708. }
  11709. }
  11710. }
  11711. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11712. if (_valueSet[0]) {
  11713. block(@"false", @(_values[0]));
  11714. }
  11715. if (_valueSet[1]) {
  11716. block(@"true", @(_values[1]));
  11717. }
  11718. }
  11719. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11720. forGPBGenericValueKey:(GPBGenericValue *)key {
  11721. int idx = (key->valueBool ? 1 : 0);
  11722. _values[idx] = value->valueInt32;
  11723. _valueSet[idx] = YES;
  11724. }
  11725. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11726. if (otherDictionary) {
  11727. for (int i = 0; i < 2; ++i) {
  11728. if (otherDictionary->_valueSet[i]) {
  11729. _valueSet[i] = YES;
  11730. _values[i] = otherDictionary->_values[i];
  11731. }
  11732. }
  11733. if (_autocreator) {
  11734. GPBAutocreatedDictionaryModified(_autocreator, self);
  11735. }
  11736. }
  11737. }
  11738. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  11739. if (!_validationFunc(value)) {
  11740. [NSException raise:NSInvalidArgumentException
  11741. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11742. value];
  11743. }
  11744. int idx = (key ? 1 : 0);
  11745. _values[idx] = value;
  11746. _valueSet[idx] = YES;
  11747. if (_autocreator) {
  11748. GPBAutocreatedDictionaryModified(_autocreator, self);
  11749. }
  11750. }
  11751. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11752. int idx = (key ? 1 : 0);
  11753. _values[idx] = rawValue;
  11754. _valueSet[idx] = YES;
  11755. if (_autocreator) {
  11756. GPBAutocreatedDictionaryModified(_autocreator, self);
  11757. }
  11758. }
  11759. - (void)removeValueForKey:(BOOL)aKey {
  11760. _valueSet[aKey ? 1 : 0] = NO;
  11761. }
  11762. - (void)removeAll {
  11763. _valueSet[0] = NO;
  11764. _valueSet[1] = NO;
  11765. }
  11766. @end
  11767. #pragma mark - NSDictionary Subclass
  11768. @implementation GPBAutocreatedDictionary {
  11769. NSMutableDictionary *_dictionary;
  11770. }
  11771. - (void)dealloc {
  11772. NSAssert(!_autocreator,
  11773. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11774. [self class], _autocreator);
  11775. [_dictionary release];
  11776. [super dealloc];
  11777. }
  11778. #pragma mark Required NSDictionary overrides
  11779. - (instancetype)initWithObjects:(const id [])objects
  11780. forKeys:(const id<NSCopying> [])keys
  11781. count:(NSUInteger)count {
  11782. self = [super init];
  11783. if (self) {
  11784. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11785. forKeys:keys
  11786. count:count];
  11787. }
  11788. return self;
  11789. }
  11790. - (NSUInteger)count {
  11791. return [_dictionary count];
  11792. }
  11793. - (id)objectForKey:(id)aKey {
  11794. return [_dictionary objectForKey:aKey];
  11795. }
  11796. - (NSEnumerator *)keyEnumerator {
  11797. if (_dictionary == nil) {
  11798. _dictionary = [[NSMutableDictionary alloc] init];
  11799. }
  11800. return [_dictionary keyEnumerator];
  11801. }
  11802. #pragma mark Required NSMutableDictionary overrides
  11803. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11804. // since we only autocreate empty dictionaries.
  11805. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11806. if (_dictionary == nil) {
  11807. _dictionary = [[NSMutableDictionary alloc] init];
  11808. }
  11809. [_dictionary setObject:anObject forKey:aKey];
  11810. if (_autocreator) {
  11811. GPBAutocreatedDictionaryModified(_autocreator, self);
  11812. }
  11813. }
  11814. - (void)removeObjectForKey:(id)aKey {
  11815. [_dictionary removeObjectForKey:aKey];
  11816. }
  11817. #pragma mark Extra things hooked
  11818. - (id)copyWithZone:(NSZone *)zone {
  11819. if (_dictionary == nil) {
  11820. _dictionary = [[NSMutableDictionary alloc] init];
  11821. }
  11822. return [_dictionary copyWithZone:zone];
  11823. }
  11824. - (id)mutableCopyWithZone:(NSZone *)zone {
  11825. if (_dictionary == nil) {
  11826. _dictionary = [[NSMutableDictionary alloc] init];
  11827. }
  11828. return [_dictionary mutableCopyWithZone:zone];
  11829. }
  11830. - (id)objectForKeyedSubscript:(id)key {
  11831. return [_dictionary objectForKeyedSubscript:key];
  11832. }
  11833. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11834. if (_dictionary == nil) {
  11835. _dictionary = [[NSMutableDictionary alloc] init];
  11836. }
  11837. [_dictionary setObject:obj forKeyedSubscript:key];
  11838. if (_autocreator) {
  11839. GPBAutocreatedDictionaryModified(_autocreator, self);
  11840. }
  11841. }
  11842. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11843. id obj,
  11844. BOOL *stop))block {
  11845. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11846. }
  11847. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11848. usingBlock:(void (^)(id key,
  11849. id obj,
  11850. BOOL *stop))block {
  11851. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11852. }
  11853. @end