GPBDictionary.m 456 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392
  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. case GPBDataTypeMessage: {
  446. value.valueMessage = [[field.msgClass alloc] init];
  447. break;
  448. }
  449. default:
  450. // Nothing
  451. break;
  452. }
  453. }
  454. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  455. // mapDictionary is an NSMutableDictionary
  456. [mapDictionary setObject:value.valueString forKey:key.valueString];
  457. } else {
  458. if (valueDataType == GPBDataTypeEnum) {
  459. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  460. [field isValidEnumValue:value.valueEnum]) {
  461. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  462. } else {
  463. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  464. forKey:&key
  465. keyDataType:keyDataType];
  466. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  467. }
  468. } else {
  469. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  470. }
  471. }
  472. }
  473. if (GPBDataTypeIsObject(keyDataType)) {
  474. [key.valueString release];
  475. }
  476. if (GPBDataTypeIsObject(valueDataType)) {
  477. [value.valueString release];
  478. }
  479. }
  480. //
  481. // Macros for the common basic cases.
  482. //
  483. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  484. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  485. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  486. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  487. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  488. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  489. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  490. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  491. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  492. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  493. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  494. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  495. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  496. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  497. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  498. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT)
  499. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  500. //%#pragma mark - KEY_NAME -> VALUE_NAME
  501. //%
  502. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  503. //% @package
  504. //% NSMutableDictionary *_dictionary;
  505. //%}
  506. //%
  507. //%+ (instancetype)dictionary {
  508. //% return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  509. //%}
  510. //%
  511. //%+ (instancetype)dictionaryWithValue:(VALUE_TYPE)value
  512. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  513. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  514. //% // on to get the type correct.
  515. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValues:&value
  516. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  517. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  518. //%}
  519. //%
  520. //%+ (instancetype)dictionaryWithValues:(const VALUE_TYPE [])values
  521. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  522. //% count:(NSUInteger)count {
  523. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  524. //% // on to get the type correct.
  525. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValues:values
  526. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  527. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  528. //%}
  529. //%
  530. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  531. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  532. //% // on to get the type correct.
  533. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  534. //%}
  535. //%
  536. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  537. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  538. //%}
  539. //%
  540. //%- (instancetype)init {
  541. //% return [self initWithValues:NULL forKeys:NULL count:0];
  542. //%}
  543. //%
  544. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  545. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  546. //% count:(NSUInteger)count {
  547. //% self = [super init];
  548. //% if (self) {
  549. //% _dictionary = [[NSMutableDictionary alloc] init];
  550. //% if (count && values && keys) {
  551. //% for (NSUInteger i = 0; i < count; ++i) {
  552. //% [_dictionary setObject:WRAPPED##VHELPER(values[i]) forKey:WRAPPED##KHELPER(keys[i])];
  553. //% }
  554. //% }
  555. //% }
  556. //% return self;
  557. //%}
  558. //%
  559. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  560. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  561. //% if (self) {
  562. //% if (dictionary) {
  563. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  564. //% }
  565. //% }
  566. //% return self;
  567. //%}
  568. //%
  569. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  570. //% #pragma unused(numItems)
  571. //% return [self initWithValues:NULL forKeys:NULL count:0];
  572. //%}
  573. //%
  574. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, )
  575. //%
  576. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  577. //%
  578. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, )
  579. //%
  580. //%@end
  581. //%
  582. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  583. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  584. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  585. //%#pragma mark - KEY_NAME -> VALUE_NAME
  586. //%
  587. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  588. //% @package
  589. //% NSMutableDictionary *_dictionary;
  590. //% GPBEnumValidationFunc _validationFunc;
  591. //%}
  592. //%
  593. //%@synthesize validationFunc = _validationFunc;
  594. //%
  595. //%+ (instancetype)dictionary {
  596. //% return [[[self alloc] initWithValidationFunction:NULL
  597. //% rawValues:NULL
  598. //% forKeys:NULL
  599. //% count:0] autorelease];
  600. //%}
  601. //%
  602. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  603. //% return [[[self alloc] initWithValidationFunction:func
  604. //% rawValues:NULL
  605. //% forKeys:NULL
  606. //% count:0] autorelease];
  607. //%}
  608. //%
  609. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  610. //% rawValue:(VALUE_TYPE)rawValue
  611. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  612. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  613. //% // on to get the type correct.
  614. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  615. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  616. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  617. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  618. //%}
  619. //%
  620. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  621. //% rawValues:(const VALUE_TYPE [])rawValues
  622. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  623. //% count:(NSUInteger)count {
  624. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  625. //% // on to get the type correct.
  626. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  627. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  628. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  629. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  630. //%}
  631. //%
  632. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  633. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  634. //% // on to get the type correct.
  635. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  636. //%}
  637. //%
  638. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  639. //% capacity:(NSUInteger)numItems {
  640. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  641. //%}
  642. //%
  643. //%- (instancetype)init {
  644. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  645. //%}
  646. //%
  647. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  648. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  649. //%}
  650. //%
  651. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  652. //% rawValues:(const VALUE_TYPE [])rawValues
  653. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  654. //% count:(NSUInteger)count {
  655. //% self = [super init];
  656. //% if (self) {
  657. //% _dictionary = [[NSMutableDictionary alloc] init];
  658. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  659. //% if (count && rawValues && keys) {
  660. //% for (NSUInteger i = 0; i < count; ++i) {
  661. //% [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  662. //% }
  663. //% }
  664. //% }
  665. //% return self;
  666. //%}
  667. //%
  668. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  669. //% self = [self initWithValidationFunction:dictionary.validationFunc
  670. //% rawValues:NULL
  671. //% forKeys:NULL
  672. //% count:0];
  673. //% if (self) {
  674. //% if (dictionary) {
  675. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  676. //% }
  677. //% }
  678. //% return self;
  679. //%}
  680. //%
  681. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  682. //% capacity:(NSUInteger)numItems {
  683. //% #pragma unused(numItems)
  684. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  685. //%}
  686. //%
  687. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Raw)
  688. //%
  689. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  690. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  691. //% if (wrapped && value) {
  692. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  693. //% if (!_validationFunc(result)) {
  694. //% result = kGPBUnrecognizedEnumeratorValue;
  695. //% }
  696. //% *value = result;
  697. //% }
  698. //% return (wrapped != NULL);
  699. //%}
  700. //%
  701. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  702. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  703. //% if (wrapped && rawValue) {
  704. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  705. //% }
  706. //% return (wrapped != NULL);
  707. //%}
  708. //%
  709. //%- (void)enumerateKeysAndValuesUsingBlock:
  710. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  711. //% GPBEnumValidationFunc func = _validationFunc;
  712. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  713. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  714. //% BOOL *stop) {
  715. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  716. //% if (!func(unwrapped)) {
  717. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  718. //% }
  719. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  720. //% }];
  721. //%}
  722. //%
  723. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Raw)
  724. //%
  725. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  726. //% if (!_validationFunc(value)) {
  727. //% [NSException raise:NSInvalidArgumentException
  728. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  729. //% value];
  730. //% }
  731. //%
  732. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  733. //% if (_autocreator) {
  734. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  735. //% }
  736. //%}
  737. //%
  738. //%@end
  739. //%
  740. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, ACCESSOR_NAME)
  741. //%- (void)dealloc {
  742. //% NSAssert(!_autocreator,
  743. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  744. //% [self class], _autocreator);
  745. //% [_dictionary release];
  746. //% [super dealloc];
  747. //%}
  748. //%
  749. //%- (instancetype)copyWithZone:(NSZone *)zone {
  750. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  751. //%}
  752. //%
  753. //%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
  754. //% if (self == other) {
  755. //% return YES;
  756. //% }
  757. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  758. //% return NO;
  759. //% }
  760. //% return [_dictionary isEqual:other->_dictionary];
  761. //%}
  762. //%
  763. //%- (NSUInteger)hash {
  764. //% return _dictionary.count;
  765. //%}
  766. //%
  767. //%- (NSString *)description {
  768. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  769. //%}
  770. //%
  771. //%- (NSUInteger)count {
  772. //% return _dictionary.count;
  773. //%}
  774. //%
  775. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##ValuesUsingBlock:
  776. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  777. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  778. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  779. //% BOOL *stop) {
  780. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(aValue), stop);
  781. //% }];
  782. //%}
  783. //%
  784. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  785. //% NSUInteger count = _dictionary.count;
  786. //% if (count == 0) {
  787. //% return 0;
  788. //% }
  789. //%
  790. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  791. //% GPBDataType keyDataType = field.mapKeyDataType;
  792. //% __block size_t result = 0;
  793. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  794. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  795. //% BOOL *stop) {
  796. //% #pragma unused(stop)
  797. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  798. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueDataType);
  799. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  800. //% }];
  801. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  802. //% result += tagSize * count;
  803. //% return result;
  804. //%}
  805. //%
  806. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  807. //% asField:(GPBFieldDescriptor *)field {
  808. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  809. //% GPBDataType keyDataType = field.mapKeyDataType;
  810. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  811. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  812. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  813. //% BOOL *stop) {
  814. //% #pragma unused(stop)
  815. //% // Write the tag.
  816. //% [outputStream writeInt32NoTag:tag];
  817. //% // Write the size of the message.
  818. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  819. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueDataType);
  820. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  821. //% // Write the fields.
  822. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  823. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueDataType);
  824. //% }];
  825. //%}
  826. //%
  827. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  828. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  829. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  830. //%}
  831. //%
  832. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  833. //% [self enumerateKeysAnd##ACCESSOR_NAME##ValuesUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop) {
  834. //% #pragma unused(stop)
  835. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(value));
  836. //% }];
  837. //%}
  838. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, ACCESSOR_NAME)
  839. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  840. //% if (otherDictionary) {
  841. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  842. //% if (_autocreator) {
  843. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  844. //% }
  845. //% }
  846. //%}
  847. //%
  848. //%- (void)set##ACCESSOR_NAME##Value:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  849. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  850. //% if (_autocreator) {
  851. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  852. //% }
  853. //%}
  854. //%
  855. //%- (void)removeValueForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  856. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  857. //%}
  858. //%
  859. //%- (void)removeAll {
  860. //% [_dictionary removeAllObjects];
  861. //%}
  862. //
  863. // Custom Generation for Bool keys
  864. //
  865. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  866. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD)
  867. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  868. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT)
  869. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER)
  870. //%#pragma mark - Bool -> VALUE_NAME
  871. //%
  872. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  873. //% @package
  874. //% VALUE_TYPE _values[2];
  875. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  876. //%
  877. //%+ (instancetype)dictionary {
  878. //% return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  879. //%}
  880. //%
  881. //%+ (instancetype)dictionaryWithValue:(VALUE_TYPE)value
  882. //% forKey:(BOOL)key {
  883. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  884. //% // on to get the type correct.
  885. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithValues:&value
  886. //% VALUE_NAME$S forKeys:&key
  887. //% VALUE_NAME$S count:1] autorelease];
  888. //%}
  889. //%
  890. //%+ (instancetype)dictionaryWithValues:(const VALUE_TYPE [])values
  891. //% forKeys:(const BOOL [])keys
  892. //% count:(NSUInteger)count {
  893. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  894. //% // on to get the type correct.
  895. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithValues:values
  896. //% VALUE_NAME$S forKeys:keys
  897. //% VALUE_NAME$S count:count] autorelease];
  898. //%}
  899. //%
  900. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  901. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  902. //% // on to get the type correct.
  903. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  904. //%}
  905. //%
  906. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  907. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  908. //%}
  909. //%
  910. //%- (instancetype)init {
  911. //% return [self initWithValues:NULL forKeys:NULL count:0];
  912. //%}
  913. //%
  914. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  915. //%
  916. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  917. //% #pragma unused(numItems)
  918. //% return [self initWithValues:NULL forKeys:NULL count:0];
  919. //%}
  920. //%
  921. //%BOOL_DICT_DEALLOC##HELPER()
  922. //%
  923. //%- (instancetype)copyWithZone:(NSZone *)zone {
  924. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  925. //%}
  926. //%
  927. //%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
  928. //% if (self == other) {
  929. //% return YES;
  930. //% }
  931. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  932. //% return NO;
  933. //% }
  934. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
  935. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
  936. //% return NO;
  937. //% }
  938. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
  939. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
  940. //% return NO;
  941. //% }
  942. //% return YES;
  943. //%}
  944. //%
  945. //%- (NSUInteger)hash {
  946. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  947. //%}
  948. //%
  949. //%- (NSString *)description {
  950. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  951. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  952. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  953. //% }
  954. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  955. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  956. //% }
  957. //% [result appendString:@" }"];
  958. //% return result;
  959. //%}
  960. //%
  961. //%- (NSUInteger)count {
  962. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  963. //%}
  964. //%
  965. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  966. //%
  967. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  968. //%
  969. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  970. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  971. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  972. //% }
  973. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  974. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  975. //% }
  976. //%}
  977. //%
  978. //%- (void)enumerateKeysAndValuesUsingBlock:
  979. //% (void (^)(BOOL key, VALUE_TYPE value, BOOL *stop))block {
  980. //% BOOL stop = NO;
  981. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  982. //% block(NO, _values[0], &stop);
  983. //% }
  984. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  985. //% block(YES, _values[1], &stop);
  986. //% }
  987. //%}
  988. //%
  989. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  990. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  991. //% NSUInteger count = 0;
  992. //% size_t result = 0;
  993. //% for (int i = 0; i < 2; ++i) {
  994. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  995. //% ++count;
  996. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  997. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  998. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  999. //% }
  1000. //% }
  1001. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1002. //% result += tagSize * count;
  1003. //% return result;
  1004. //%}
  1005. //%
  1006. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1007. //% asField:(GPBFieldDescriptor *)field {
  1008. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1009. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1010. //% for (int i = 0; i < 2; ++i) {
  1011. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1012. //% // Write the tag.
  1013. //% [outputStream writeInt32NoTag:tag];
  1014. //% // Write the size of the message.
  1015. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1016. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1017. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1018. //% // Write the fields.
  1019. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1020. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1021. //% }
  1022. //% }
  1023. //%}
  1024. //%
  1025. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1026. //%
  1027. //%@end
  1028. //%
  1029. //
  1030. // Helpers for PODs
  1031. //
  1032. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1033. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  1034. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1035. //% if (wrapped && value) {
  1036. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1037. //% }
  1038. //% return (wrapped != NULL);
  1039. //%}
  1040. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1041. //%@(VALUE)
  1042. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1043. //%[VALUE unsignedIntValue]
  1044. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1045. //%[VALUE intValue]
  1046. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1047. //%[VALUE unsignedLongLongValue]
  1048. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1049. //%[VALUE longLongValue]
  1050. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1051. //%[VALUE boolValue]
  1052. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1053. //%[VALUE floatValue]
  1054. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1055. //%[VALUE doubleValue]
  1056. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1057. //%[VALUE intValue]
  1058. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1059. //%[NSString stringWithFormat:@"%u", VALUE]
  1060. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1061. //%[NSString stringWithFormat:@"%d", VALUE]
  1062. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1063. //%[NSString stringWithFormat:@"%llu", VALUE]
  1064. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1065. //%[NSString stringWithFormat:@"%lld", VALUE]
  1066. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1067. //%(VALUE ? @"true" : @"false")
  1068. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1069. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1070. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1071. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1072. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1073. //%@(VALUE)
  1074. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1075. //%NSNumber *
  1076. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1077. //%VAL1 != VAL2
  1078. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1079. // Empty
  1080. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1081. // Empty
  1082. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1083. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1084. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1085. // Empty
  1086. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1087. // Empty
  1088. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1089. // Empty
  1090. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1091. // Empty
  1092. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1093. // Empty
  1094. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1095. // Empty
  1096. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1097. // Empty
  1098. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1099. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1100. //% forKey:(GPBGenericValue *)key
  1101. //% keyDataType:(GPBDataType)keyDataType {
  1102. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1103. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1104. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1105. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1106. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1107. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1108. //% [outputStream release];
  1109. //% return data;
  1110. //%}
  1111. //%
  1112. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1113. //%value##VALUE_NAME
  1114. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1115. //% BOOL _valueSet[2];
  1116. //%
  1117. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1118. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1119. //% forKeys:(const BOOL [])keys
  1120. //% count:(NSUInteger)count {
  1121. //% self = [super init];
  1122. //% if (self) {
  1123. //% for (NSUInteger i = 0; i < count; ++i) {
  1124. //% int idx = keys[i] ? 1 : 0;
  1125. //% _values[idx] = values[i];
  1126. //% _valueSet[idx] = YES;
  1127. //% }
  1128. //% }
  1129. //% return self;
  1130. //%}
  1131. //%
  1132. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1133. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1134. //% if (self) {
  1135. //% if (dictionary) {
  1136. //% for (int i = 0; i < 2; ++i) {
  1137. //% if (dictionary->_valueSet[i]) {
  1138. //% _values[i] = dictionary->_values[i];
  1139. //% _valueSet[i] = YES;
  1140. //% }
  1141. //% }
  1142. //% }
  1143. //% }
  1144. //% return self;
  1145. //%}
  1146. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1147. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1148. //%- (void)dealloc {
  1149. //% NSAssert(!_autocreator,
  1150. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1151. //% [self class], _autocreator);
  1152. //% [super dealloc];
  1153. //%}
  1154. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1155. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1156. //%BOOL_DICT_HASPOD(IDX, REF)
  1157. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1158. //%REF##_valueSet[IDX]
  1159. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1160. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1161. //% int idx = (key ? 1 : 0);
  1162. //% if (_valueSet[idx]) {
  1163. //% if (value) {
  1164. //% *value = _values[idx];
  1165. //% }
  1166. //% return YES;
  1167. //% }
  1168. //% return NO;
  1169. //%}
  1170. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1171. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1172. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1173. //% int idx = (key->valueBool ? 1 : 0);
  1174. //% _values[idx] = value->value##VALUE_NAME;
  1175. //% _valueSet[idx] = YES;
  1176. //%}
  1177. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1178. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1179. //% if (otherDictionary) {
  1180. //% for (int i = 0; i < 2; ++i) {
  1181. //% if (otherDictionary->_valueSet[i]) {
  1182. //% _valueSet[i] = YES;
  1183. //% _values[i] = otherDictionary->_values[i];
  1184. //% }
  1185. //% }
  1186. //% if (_autocreator) {
  1187. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1188. //% }
  1189. //% }
  1190. //%}
  1191. //%
  1192. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1193. //% int idx = (key ? 1 : 0);
  1194. //% _values[idx] = value;
  1195. //% _valueSet[idx] = YES;
  1196. //% if (_autocreator) {
  1197. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1198. //% }
  1199. //%}
  1200. //%
  1201. //%- (void)removeValueForKey:(BOOL)aKey {
  1202. //% _valueSet[aKey ? 1 : 0] = NO;
  1203. //%}
  1204. //%
  1205. //%- (void)removeAll {
  1206. //% _valueSet[0] = NO;
  1207. //% _valueSet[1] = NO;
  1208. //%}
  1209. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1210. //%STR_FORMAT_##VALUE_NAME()
  1211. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1212. //%%u
  1213. //%PDDM-DEFINE STR_FORMAT_Int32()
  1214. //%%d
  1215. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1216. //%%llu
  1217. //%PDDM-DEFINE STR_FORMAT_Int64()
  1218. //%%lld
  1219. //%PDDM-DEFINE STR_FORMAT_Bool()
  1220. //%%d
  1221. //%PDDM-DEFINE STR_FORMAT_Float()
  1222. //%%f
  1223. //%PDDM-DEFINE STR_FORMAT_Double()
  1224. //%%lf
  1225. //
  1226. // Helpers for Objects
  1227. //
  1228. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1229. //%- (VALUE_TYPE)valueForKey:(KEY_TYPE)key {
  1230. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1231. //% return result;
  1232. //%}
  1233. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1234. //%VALUE
  1235. //%PDDM-DEFINE UNWRAPString(VALUE)
  1236. //%VALUE
  1237. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1238. //%VALUE
  1239. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1240. //%VALUE
  1241. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1242. //%VALUE
  1243. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1244. //%ENUM_TYPEOBJECT_##TYPE()
  1245. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1246. //%NSString *
  1247. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1248. //%id ##
  1249. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1250. //%![VAL1 isEqual:VAL2]
  1251. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1252. //%- (BOOL)isInitialized {
  1253. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1254. //% if (!msg.initialized) {
  1255. //% return NO;
  1256. //% }
  1257. //% }
  1258. //% return YES;
  1259. //%}
  1260. //%
  1261. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1262. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1263. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1264. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1265. //% GPBMessage *msg,
  1266. //% BOOL *stop) {
  1267. //% #pragma unused(stop)
  1268. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1269. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1270. //% [copiedMsg release];
  1271. //% }];
  1272. //% return newDict;
  1273. //%}
  1274. //%
  1275. //%
  1276. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1277. //%- (BOOL)isInitialized {
  1278. //% if (_values[0] && ![_values[0] isInitialized]) {
  1279. //% return NO;
  1280. //% }
  1281. //% if (_values[1] && ![_values[1] isInitialized]) {
  1282. //% return NO;
  1283. //% }
  1284. //% return YES;
  1285. //%}
  1286. //%
  1287. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1288. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1289. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1290. //% for (int i = 0; i < 2; ++i) {
  1291. //% if (_values[i] != nil) {
  1292. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1293. //% }
  1294. //% }
  1295. //% return newDict;
  1296. //%}
  1297. //%
  1298. //%
  1299. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1300. // Empty
  1301. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1302. //%valueString
  1303. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1304. // Empty
  1305. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1306. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1307. //% forKeys:(const BOOL [])keys
  1308. //% count:(NSUInteger)count {
  1309. //% self = [super init];
  1310. //% if (self) {
  1311. //% for (NSUInteger i = 0; i < count; ++i) {
  1312. //% int idx = keys[i] ? 1 : 0;
  1313. //% [_values[idx] release];
  1314. //% _values[idx] = (VALUE_TYPE)[values[i] retain];
  1315. //% }
  1316. //% }
  1317. //% return self;
  1318. //%}
  1319. //%
  1320. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1321. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1322. //% if (self) {
  1323. //% if (dictionary) {
  1324. //% _values[0] = [dictionary->_values[0] retain];
  1325. //% _values[1] = [dictionary->_values[1] retain];
  1326. //% }
  1327. //% }
  1328. //% return self;
  1329. //%}
  1330. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1331. //%- (void)dealloc {
  1332. //% NSAssert(!_autocreator,
  1333. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1334. //% [self class], _autocreator);
  1335. //% [_values[0] release];
  1336. //% [_values[1] release];
  1337. //% [super dealloc];
  1338. //%}
  1339. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1340. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1341. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1342. //%REF##_values[IDX] != nil
  1343. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1344. //%- (VALUE_TYPE)valueForKey:(BOOL)key {
  1345. //% return _values[key ? 1 : 0];
  1346. //%}
  1347. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1348. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1349. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1350. //% int idx = (key->valueBool ? 1 : 0);
  1351. //% [_values[idx] release];
  1352. //% _values[idx] = [value->valueString retain];
  1353. //%}
  1354. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1355. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1356. //% if (otherDictionary) {
  1357. //% for (int i = 0; i < 2; ++i) {
  1358. //% if (otherDictionary->_values[i] != nil) {
  1359. //% [_values[i] release];
  1360. //% _values[i] = [otherDictionary->_values[i] retain];
  1361. //% }
  1362. //% }
  1363. //% if (_autocreator) {
  1364. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1365. //% }
  1366. //% }
  1367. //%}
  1368. //%
  1369. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1370. //% int idx = (key ? 1 : 0);
  1371. //% [_values[idx] release];
  1372. //% _values[idx] = [value retain];
  1373. //% if (_autocreator) {
  1374. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1375. //% }
  1376. //%}
  1377. //%
  1378. //%- (void)removeValueForKey:(BOOL)aKey {
  1379. //% int idx = (aKey ? 1 : 0);
  1380. //% [_values[idx] release];
  1381. //% _values[idx] = nil;
  1382. //%}
  1383. //%
  1384. //%- (void)removeAll {
  1385. //% for (int i = 0; i < 2; ++i) {
  1386. //% [_values[i] release];
  1387. //% _values[i] = nil;
  1388. //% }
  1389. //%}
  1390. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1391. //%%@
  1392. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1393. // This block of code is generated, do not edit it directly.
  1394. #pragma mark - UInt32 -> UInt32
  1395. @implementation GPBUInt32UInt32Dictionary {
  1396. @package
  1397. NSMutableDictionary *_dictionary;
  1398. }
  1399. + (instancetype)dictionary {
  1400. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1401. }
  1402. + (instancetype)dictionaryWithValue:(uint32_t)value
  1403. forKey:(uint32_t)key {
  1404. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1405. // on to get the type correct.
  1406. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1407. forKeys:&key
  1408. count:1] autorelease];
  1409. }
  1410. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1411. forKeys:(const uint32_t [])keys
  1412. count:(NSUInteger)count {
  1413. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1414. // on to get the type correct.
  1415. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1416. forKeys:keys
  1417. count:count] autorelease];
  1418. }
  1419. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1420. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1421. // on to get the type correct.
  1422. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1423. }
  1424. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1425. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1426. }
  1427. - (instancetype)init {
  1428. return [self initWithValues:NULL forKeys:NULL count:0];
  1429. }
  1430. - (instancetype)initWithValues:(const uint32_t [])values
  1431. forKeys:(const uint32_t [])keys
  1432. count:(NSUInteger)count {
  1433. self = [super init];
  1434. if (self) {
  1435. _dictionary = [[NSMutableDictionary alloc] init];
  1436. if (count && values && keys) {
  1437. for (NSUInteger i = 0; i < count; ++i) {
  1438. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1439. }
  1440. }
  1441. }
  1442. return self;
  1443. }
  1444. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1445. self = [self initWithValues:NULL forKeys:NULL count:0];
  1446. if (self) {
  1447. if (dictionary) {
  1448. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1449. }
  1450. }
  1451. return self;
  1452. }
  1453. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1454. #pragma unused(numItems)
  1455. return [self initWithValues:NULL forKeys:NULL count:0];
  1456. }
  1457. - (void)dealloc {
  1458. NSAssert(!_autocreator,
  1459. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1460. [self class], _autocreator);
  1461. [_dictionary release];
  1462. [super dealloc];
  1463. }
  1464. - (instancetype)copyWithZone:(NSZone *)zone {
  1465. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1466. }
  1467. - (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
  1468. if (self == other) {
  1469. return YES;
  1470. }
  1471. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1472. return NO;
  1473. }
  1474. return [_dictionary isEqual:other->_dictionary];
  1475. }
  1476. - (NSUInteger)hash {
  1477. return _dictionary.count;
  1478. }
  1479. - (NSString *)description {
  1480. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1481. }
  1482. - (NSUInteger)count {
  1483. return _dictionary.count;
  1484. }
  1485. - (void)enumerateKeysAndValuesUsingBlock:
  1486. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1487. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1488. NSNumber *aValue,
  1489. BOOL *stop) {
  1490. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1491. }];
  1492. }
  1493. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1494. NSUInteger count = _dictionary.count;
  1495. if (count == 0) {
  1496. return 0;
  1497. }
  1498. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1499. GPBDataType keyDataType = field.mapKeyDataType;
  1500. __block size_t result = 0;
  1501. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1502. NSNumber *aValue,
  1503. BOOL *stop) {
  1504. #pragma unused(stop)
  1505. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1506. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1507. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1508. }];
  1509. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1510. result += tagSize * count;
  1511. return result;
  1512. }
  1513. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1514. asField:(GPBFieldDescriptor *)field {
  1515. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1516. GPBDataType keyDataType = field.mapKeyDataType;
  1517. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1518. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1519. NSNumber *aValue,
  1520. BOOL *stop) {
  1521. #pragma unused(stop)
  1522. // Write the tag.
  1523. [outputStream writeInt32NoTag:tag];
  1524. // Write the size of the message.
  1525. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1526. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1527. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1528. // Write the fields.
  1529. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1530. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1531. }];
  1532. }
  1533. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1534. forGPBGenericValueKey:(GPBGenericValue *)key {
  1535. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1536. }
  1537. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1538. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1539. #pragma unused(stop)
  1540. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1541. }];
  1542. }
  1543. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1544. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1545. if (wrapped && value) {
  1546. *value = [wrapped unsignedIntValue];
  1547. }
  1548. return (wrapped != NULL);
  1549. }
  1550. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1551. if (otherDictionary) {
  1552. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1553. if (_autocreator) {
  1554. GPBAutocreatedDictionaryModified(_autocreator, self);
  1555. }
  1556. }
  1557. }
  1558. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1559. [_dictionary setObject:@(value) forKey:@(key)];
  1560. if (_autocreator) {
  1561. GPBAutocreatedDictionaryModified(_autocreator, self);
  1562. }
  1563. }
  1564. - (void)removeValueForKey:(uint32_t)aKey {
  1565. [_dictionary removeObjectForKey:@(aKey)];
  1566. }
  1567. - (void)removeAll {
  1568. [_dictionary removeAllObjects];
  1569. }
  1570. @end
  1571. #pragma mark - UInt32 -> Int32
  1572. @implementation GPBUInt32Int32Dictionary {
  1573. @package
  1574. NSMutableDictionary *_dictionary;
  1575. }
  1576. + (instancetype)dictionary {
  1577. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1578. }
  1579. + (instancetype)dictionaryWithValue:(int32_t)value
  1580. forKey:(uint32_t)key {
  1581. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1582. // on to get the type correct.
  1583. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1584. forKeys:&key
  1585. count:1] autorelease];
  1586. }
  1587. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1588. forKeys:(const uint32_t [])keys
  1589. count:(NSUInteger)count {
  1590. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1591. // on to get the type correct.
  1592. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1593. forKeys:keys
  1594. count:count] autorelease];
  1595. }
  1596. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1597. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1598. // on to get the type correct.
  1599. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1600. }
  1601. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1602. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1603. }
  1604. - (instancetype)init {
  1605. return [self initWithValues:NULL forKeys:NULL count:0];
  1606. }
  1607. - (instancetype)initWithValues:(const int32_t [])values
  1608. forKeys:(const uint32_t [])keys
  1609. count:(NSUInteger)count {
  1610. self = [super init];
  1611. if (self) {
  1612. _dictionary = [[NSMutableDictionary alloc] init];
  1613. if (count && values && keys) {
  1614. for (NSUInteger i = 0; i < count; ++i) {
  1615. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1616. }
  1617. }
  1618. }
  1619. return self;
  1620. }
  1621. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1622. self = [self initWithValues:NULL forKeys:NULL count:0];
  1623. if (self) {
  1624. if (dictionary) {
  1625. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1626. }
  1627. }
  1628. return self;
  1629. }
  1630. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1631. #pragma unused(numItems)
  1632. return [self initWithValues:NULL forKeys:NULL count:0];
  1633. }
  1634. - (void)dealloc {
  1635. NSAssert(!_autocreator,
  1636. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1637. [self class], _autocreator);
  1638. [_dictionary release];
  1639. [super dealloc];
  1640. }
  1641. - (instancetype)copyWithZone:(NSZone *)zone {
  1642. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1643. }
  1644. - (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
  1645. if (self == other) {
  1646. return YES;
  1647. }
  1648. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1649. return NO;
  1650. }
  1651. return [_dictionary isEqual:other->_dictionary];
  1652. }
  1653. - (NSUInteger)hash {
  1654. return _dictionary.count;
  1655. }
  1656. - (NSString *)description {
  1657. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1658. }
  1659. - (NSUInteger)count {
  1660. return _dictionary.count;
  1661. }
  1662. - (void)enumerateKeysAndValuesUsingBlock:
  1663. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1664. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1665. NSNumber *aValue,
  1666. BOOL *stop) {
  1667. block([aKey unsignedIntValue], [aValue intValue], stop);
  1668. }];
  1669. }
  1670. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1671. NSUInteger count = _dictionary.count;
  1672. if (count == 0) {
  1673. return 0;
  1674. }
  1675. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1676. GPBDataType keyDataType = field.mapKeyDataType;
  1677. __block size_t result = 0;
  1678. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1679. NSNumber *aValue,
  1680. BOOL *stop) {
  1681. #pragma unused(stop)
  1682. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1683. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1684. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1685. }];
  1686. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1687. result += tagSize * count;
  1688. return result;
  1689. }
  1690. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1691. asField:(GPBFieldDescriptor *)field {
  1692. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1693. GPBDataType keyDataType = field.mapKeyDataType;
  1694. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1695. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1696. NSNumber *aValue,
  1697. BOOL *stop) {
  1698. #pragma unused(stop)
  1699. // Write the tag.
  1700. [outputStream writeInt32NoTag:tag];
  1701. // Write the size of the message.
  1702. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1703. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1704. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1705. // Write the fields.
  1706. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1707. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1708. }];
  1709. }
  1710. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1711. forGPBGenericValueKey:(GPBGenericValue *)key {
  1712. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1713. }
  1714. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1715. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1716. #pragma unused(stop)
  1717. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1718. }];
  1719. }
  1720. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1721. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1722. if (wrapped && value) {
  1723. *value = [wrapped intValue];
  1724. }
  1725. return (wrapped != NULL);
  1726. }
  1727. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1728. if (otherDictionary) {
  1729. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1730. if (_autocreator) {
  1731. GPBAutocreatedDictionaryModified(_autocreator, self);
  1732. }
  1733. }
  1734. }
  1735. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1736. [_dictionary setObject:@(value) forKey:@(key)];
  1737. if (_autocreator) {
  1738. GPBAutocreatedDictionaryModified(_autocreator, self);
  1739. }
  1740. }
  1741. - (void)removeValueForKey:(uint32_t)aKey {
  1742. [_dictionary removeObjectForKey:@(aKey)];
  1743. }
  1744. - (void)removeAll {
  1745. [_dictionary removeAllObjects];
  1746. }
  1747. @end
  1748. #pragma mark - UInt32 -> UInt64
  1749. @implementation GPBUInt32UInt64Dictionary {
  1750. @package
  1751. NSMutableDictionary *_dictionary;
  1752. }
  1753. + (instancetype)dictionary {
  1754. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1755. }
  1756. + (instancetype)dictionaryWithValue:(uint64_t)value
  1757. forKey:(uint32_t)key {
  1758. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1759. // on to get the type correct.
  1760. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1761. forKeys:&key
  1762. count:1] autorelease];
  1763. }
  1764. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1765. forKeys:(const uint32_t [])keys
  1766. count:(NSUInteger)count {
  1767. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1768. // on to get the type correct.
  1769. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1770. forKeys:keys
  1771. count:count] autorelease];
  1772. }
  1773. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1774. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1775. // on to get the type correct.
  1776. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1777. }
  1778. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1779. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1780. }
  1781. - (instancetype)init {
  1782. return [self initWithValues:NULL forKeys:NULL count:0];
  1783. }
  1784. - (instancetype)initWithValues:(const uint64_t [])values
  1785. forKeys:(const uint32_t [])keys
  1786. count:(NSUInteger)count {
  1787. self = [super init];
  1788. if (self) {
  1789. _dictionary = [[NSMutableDictionary alloc] init];
  1790. if (count && values && keys) {
  1791. for (NSUInteger i = 0; i < count; ++i) {
  1792. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1793. }
  1794. }
  1795. }
  1796. return self;
  1797. }
  1798. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1799. self = [self initWithValues:NULL forKeys:NULL count:0];
  1800. if (self) {
  1801. if (dictionary) {
  1802. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1803. }
  1804. }
  1805. return self;
  1806. }
  1807. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1808. #pragma unused(numItems)
  1809. return [self initWithValues:NULL forKeys:NULL count:0];
  1810. }
  1811. - (void)dealloc {
  1812. NSAssert(!_autocreator,
  1813. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1814. [self class], _autocreator);
  1815. [_dictionary release];
  1816. [super dealloc];
  1817. }
  1818. - (instancetype)copyWithZone:(NSZone *)zone {
  1819. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1820. }
  1821. - (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
  1822. if (self == other) {
  1823. return YES;
  1824. }
  1825. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1826. return NO;
  1827. }
  1828. return [_dictionary isEqual:other->_dictionary];
  1829. }
  1830. - (NSUInteger)hash {
  1831. return _dictionary.count;
  1832. }
  1833. - (NSString *)description {
  1834. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1835. }
  1836. - (NSUInteger)count {
  1837. return _dictionary.count;
  1838. }
  1839. - (void)enumerateKeysAndValuesUsingBlock:
  1840. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1841. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1842. NSNumber *aValue,
  1843. BOOL *stop) {
  1844. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1845. }];
  1846. }
  1847. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1848. NSUInteger count = _dictionary.count;
  1849. if (count == 0) {
  1850. return 0;
  1851. }
  1852. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1853. GPBDataType keyDataType = field.mapKeyDataType;
  1854. __block size_t result = 0;
  1855. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1856. NSNumber *aValue,
  1857. BOOL *stop) {
  1858. #pragma unused(stop)
  1859. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1860. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1861. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1862. }];
  1863. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1864. result += tagSize * count;
  1865. return result;
  1866. }
  1867. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1868. asField:(GPBFieldDescriptor *)field {
  1869. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1870. GPBDataType keyDataType = field.mapKeyDataType;
  1871. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1872. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1873. NSNumber *aValue,
  1874. BOOL *stop) {
  1875. #pragma unused(stop)
  1876. // Write the tag.
  1877. [outputStream writeInt32NoTag:tag];
  1878. // Write the size of the message.
  1879. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1880. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1881. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1882. // Write the fields.
  1883. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1884. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1885. }];
  1886. }
  1887. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1888. forGPBGenericValueKey:(GPBGenericValue *)key {
  1889. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1890. }
  1891. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1892. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1893. #pragma unused(stop)
  1894. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1895. }];
  1896. }
  1897. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1898. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1899. if (wrapped && value) {
  1900. *value = [wrapped unsignedLongLongValue];
  1901. }
  1902. return (wrapped != NULL);
  1903. }
  1904. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1905. if (otherDictionary) {
  1906. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1907. if (_autocreator) {
  1908. GPBAutocreatedDictionaryModified(_autocreator, self);
  1909. }
  1910. }
  1911. }
  1912. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1913. [_dictionary setObject:@(value) forKey:@(key)];
  1914. if (_autocreator) {
  1915. GPBAutocreatedDictionaryModified(_autocreator, self);
  1916. }
  1917. }
  1918. - (void)removeValueForKey:(uint32_t)aKey {
  1919. [_dictionary removeObjectForKey:@(aKey)];
  1920. }
  1921. - (void)removeAll {
  1922. [_dictionary removeAllObjects];
  1923. }
  1924. @end
  1925. #pragma mark - UInt32 -> Int64
  1926. @implementation GPBUInt32Int64Dictionary {
  1927. @package
  1928. NSMutableDictionary *_dictionary;
  1929. }
  1930. + (instancetype)dictionary {
  1931. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1932. }
  1933. + (instancetype)dictionaryWithValue:(int64_t)value
  1934. forKey:(uint32_t)key {
  1935. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1936. // on to get the type correct.
  1937. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  1938. forKeys:&key
  1939. count:1] autorelease];
  1940. }
  1941. + (instancetype)dictionaryWithValues:(const int64_t [])values
  1942. forKeys:(const uint32_t [])keys
  1943. count:(NSUInteger)count {
  1944. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1945. // on to get the type correct.
  1946. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  1947. forKeys:keys
  1948. count:count] autorelease];
  1949. }
  1950. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1951. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1952. // on to get the type correct.
  1953. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1954. }
  1955. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1956. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1957. }
  1958. - (instancetype)init {
  1959. return [self initWithValues:NULL forKeys:NULL count:0];
  1960. }
  1961. - (instancetype)initWithValues:(const int64_t [])values
  1962. forKeys:(const uint32_t [])keys
  1963. count:(NSUInteger)count {
  1964. self = [super init];
  1965. if (self) {
  1966. _dictionary = [[NSMutableDictionary alloc] init];
  1967. if (count && values && keys) {
  1968. for (NSUInteger i = 0; i < count; ++i) {
  1969. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1970. }
  1971. }
  1972. }
  1973. return self;
  1974. }
  1975. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1976. self = [self initWithValues:NULL forKeys:NULL count:0];
  1977. if (self) {
  1978. if (dictionary) {
  1979. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1980. }
  1981. }
  1982. return self;
  1983. }
  1984. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1985. #pragma unused(numItems)
  1986. return [self initWithValues:NULL forKeys:NULL count:0];
  1987. }
  1988. - (void)dealloc {
  1989. NSAssert(!_autocreator,
  1990. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1991. [self class], _autocreator);
  1992. [_dictionary release];
  1993. [super dealloc];
  1994. }
  1995. - (instancetype)copyWithZone:(NSZone *)zone {
  1996. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  1997. }
  1998. - (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
  1999. if (self == other) {
  2000. return YES;
  2001. }
  2002. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2003. return NO;
  2004. }
  2005. return [_dictionary isEqual:other->_dictionary];
  2006. }
  2007. - (NSUInteger)hash {
  2008. return _dictionary.count;
  2009. }
  2010. - (NSString *)description {
  2011. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2012. }
  2013. - (NSUInteger)count {
  2014. return _dictionary.count;
  2015. }
  2016. - (void)enumerateKeysAndValuesUsingBlock:
  2017. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2018. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2019. NSNumber *aValue,
  2020. BOOL *stop) {
  2021. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2022. }];
  2023. }
  2024. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2025. NSUInteger count = _dictionary.count;
  2026. if (count == 0) {
  2027. return 0;
  2028. }
  2029. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2030. GPBDataType keyDataType = field.mapKeyDataType;
  2031. __block size_t result = 0;
  2032. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2033. NSNumber *aValue,
  2034. BOOL *stop) {
  2035. #pragma unused(stop)
  2036. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2037. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2038. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2039. }];
  2040. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2041. result += tagSize * count;
  2042. return result;
  2043. }
  2044. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2045. asField:(GPBFieldDescriptor *)field {
  2046. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2047. GPBDataType keyDataType = field.mapKeyDataType;
  2048. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2049. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2050. NSNumber *aValue,
  2051. BOOL *stop) {
  2052. #pragma unused(stop)
  2053. // Write the tag.
  2054. [outputStream writeInt32NoTag:tag];
  2055. // Write the size of the message.
  2056. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2057. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2058. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2059. // Write the fields.
  2060. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2061. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2062. }];
  2063. }
  2064. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2065. forGPBGenericValueKey:(GPBGenericValue *)key {
  2066. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2067. }
  2068. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2069. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2070. #pragma unused(stop)
  2071. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2072. }];
  2073. }
  2074. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  2075. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2076. if (wrapped && value) {
  2077. *value = [wrapped longLongValue];
  2078. }
  2079. return (wrapped != NULL);
  2080. }
  2081. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2082. if (otherDictionary) {
  2083. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2084. if (_autocreator) {
  2085. GPBAutocreatedDictionaryModified(_autocreator, self);
  2086. }
  2087. }
  2088. }
  2089. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  2090. [_dictionary setObject:@(value) forKey:@(key)];
  2091. if (_autocreator) {
  2092. GPBAutocreatedDictionaryModified(_autocreator, self);
  2093. }
  2094. }
  2095. - (void)removeValueForKey:(uint32_t)aKey {
  2096. [_dictionary removeObjectForKey:@(aKey)];
  2097. }
  2098. - (void)removeAll {
  2099. [_dictionary removeAllObjects];
  2100. }
  2101. @end
  2102. #pragma mark - UInt32 -> Bool
  2103. @implementation GPBUInt32BoolDictionary {
  2104. @package
  2105. NSMutableDictionary *_dictionary;
  2106. }
  2107. + (instancetype)dictionary {
  2108. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2109. }
  2110. + (instancetype)dictionaryWithValue:(BOOL)value
  2111. forKey:(uint32_t)key {
  2112. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2113. // on to get the type correct.
  2114. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2115. forKeys:&key
  2116. count:1] autorelease];
  2117. }
  2118. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2119. forKeys:(const uint32_t [])keys
  2120. count:(NSUInteger)count {
  2121. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2122. // on to get the type correct.
  2123. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2124. forKeys:keys
  2125. count:count] autorelease];
  2126. }
  2127. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2128. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2129. // on to get the type correct.
  2130. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2131. }
  2132. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2133. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2134. }
  2135. - (instancetype)init {
  2136. return [self initWithValues:NULL forKeys:NULL count:0];
  2137. }
  2138. - (instancetype)initWithValues:(const BOOL [])values
  2139. forKeys:(const uint32_t [])keys
  2140. count:(NSUInteger)count {
  2141. self = [super init];
  2142. if (self) {
  2143. _dictionary = [[NSMutableDictionary alloc] init];
  2144. if (count && values && keys) {
  2145. for (NSUInteger i = 0; i < count; ++i) {
  2146. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2147. }
  2148. }
  2149. }
  2150. return self;
  2151. }
  2152. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2153. self = [self initWithValues:NULL forKeys:NULL count:0];
  2154. if (self) {
  2155. if (dictionary) {
  2156. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2157. }
  2158. }
  2159. return self;
  2160. }
  2161. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2162. #pragma unused(numItems)
  2163. return [self initWithValues:NULL forKeys:NULL count:0];
  2164. }
  2165. - (void)dealloc {
  2166. NSAssert(!_autocreator,
  2167. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2168. [self class], _autocreator);
  2169. [_dictionary release];
  2170. [super dealloc];
  2171. }
  2172. - (instancetype)copyWithZone:(NSZone *)zone {
  2173. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2174. }
  2175. - (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
  2176. if (self == other) {
  2177. return YES;
  2178. }
  2179. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2180. return NO;
  2181. }
  2182. return [_dictionary isEqual:other->_dictionary];
  2183. }
  2184. - (NSUInteger)hash {
  2185. return _dictionary.count;
  2186. }
  2187. - (NSString *)description {
  2188. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2189. }
  2190. - (NSUInteger)count {
  2191. return _dictionary.count;
  2192. }
  2193. - (void)enumerateKeysAndValuesUsingBlock:
  2194. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2195. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2196. NSNumber *aValue,
  2197. BOOL *stop) {
  2198. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2199. }];
  2200. }
  2201. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2202. NSUInteger count = _dictionary.count;
  2203. if (count == 0) {
  2204. return 0;
  2205. }
  2206. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2207. GPBDataType keyDataType = field.mapKeyDataType;
  2208. __block size_t result = 0;
  2209. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2210. NSNumber *aValue,
  2211. BOOL *stop) {
  2212. #pragma unused(stop)
  2213. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2214. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2215. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2216. }];
  2217. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2218. result += tagSize * count;
  2219. return result;
  2220. }
  2221. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2222. asField:(GPBFieldDescriptor *)field {
  2223. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2224. GPBDataType keyDataType = field.mapKeyDataType;
  2225. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2226. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2227. NSNumber *aValue,
  2228. BOOL *stop) {
  2229. #pragma unused(stop)
  2230. // Write the tag.
  2231. [outputStream writeInt32NoTag:tag];
  2232. // Write the size of the message.
  2233. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2234. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2235. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2236. // Write the fields.
  2237. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2238. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2239. }];
  2240. }
  2241. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2242. forGPBGenericValueKey:(GPBGenericValue *)key {
  2243. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2244. }
  2245. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2246. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2247. #pragma unused(stop)
  2248. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2249. }];
  2250. }
  2251. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2252. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2253. if (wrapped && value) {
  2254. *value = [wrapped boolValue];
  2255. }
  2256. return (wrapped != NULL);
  2257. }
  2258. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2259. if (otherDictionary) {
  2260. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2261. if (_autocreator) {
  2262. GPBAutocreatedDictionaryModified(_autocreator, self);
  2263. }
  2264. }
  2265. }
  2266. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2267. [_dictionary setObject:@(value) forKey:@(key)];
  2268. if (_autocreator) {
  2269. GPBAutocreatedDictionaryModified(_autocreator, self);
  2270. }
  2271. }
  2272. - (void)removeValueForKey:(uint32_t)aKey {
  2273. [_dictionary removeObjectForKey:@(aKey)];
  2274. }
  2275. - (void)removeAll {
  2276. [_dictionary removeAllObjects];
  2277. }
  2278. @end
  2279. #pragma mark - UInt32 -> Float
  2280. @implementation GPBUInt32FloatDictionary {
  2281. @package
  2282. NSMutableDictionary *_dictionary;
  2283. }
  2284. + (instancetype)dictionary {
  2285. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2286. }
  2287. + (instancetype)dictionaryWithValue:(float)value
  2288. forKey:(uint32_t)key {
  2289. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2290. // on to get the type correct.
  2291. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2292. forKeys:&key
  2293. count:1] autorelease];
  2294. }
  2295. + (instancetype)dictionaryWithValues:(const float [])values
  2296. forKeys:(const uint32_t [])keys
  2297. count:(NSUInteger)count {
  2298. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2299. // on to get the type correct.
  2300. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2301. forKeys:keys
  2302. count:count] autorelease];
  2303. }
  2304. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2305. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2306. // on to get the type correct.
  2307. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2308. }
  2309. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2310. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2311. }
  2312. - (instancetype)init {
  2313. return [self initWithValues:NULL forKeys:NULL count:0];
  2314. }
  2315. - (instancetype)initWithValues:(const float [])values
  2316. forKeys:(const uint32_t [])keys
  2317. count:(NSUInteger)count {
  2318. self = [super init];
  2319. if (self) {
  2320. _dictionary = [[NSMutableDictionary alloc] init];
  2321. if (count && values && keys) {
  2322. for (NSUInteger i = 0; i < count; ++i) {
  2323. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2324. }
  2325. }
  2326. }
  2327. return self;
  2328. }
  2329. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2330. self = [self initWithValues:NULL forKeys:NULL count:0];
  2331. if (self) {
  2332. if (dictionary) {
  2333. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2334. }
  2335. }
  2336. return self;
  2337. }
  2338. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2339. #pragma unused(numItems)
  2340. return [self initWithValues:NULL forKeys:NULL count:0];
  2341. }
  2342. - (void)dealloc {
  2343. NSAssert(!_autocreator,
  2344. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2345. [self class], _autocreator);
  2346. [_dictionary release];
  2347. [super dealloc];
  2348. }
  2349. - (instancetype)copyWithZone:(NSZone *)zone {
  2350. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2351. }
  2352. - (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
  2353. if (self == other) {
  2354. return YES;
  2355. }
  2356. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2357. return NO;
  2358. }
  2359. return [_dictionary isEqual:other->_dictionary];
  2360. }
  2361. - (NSUInteger)hash {
  2362. return _dictionary.count;
  2363. }
  2364. - (NSString *)description {
  2365. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2366. }
  2367. - (NSUInteger)count {
  2368. return _dictionary.count;
  2369. }
  2370. - (void)enumerateKeysAndValuesUsingBlock:
  2371. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2372. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2373. NSNumber *aValue,
  2374. BOOL *stop) {
  2375. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2376. }];
  2377. }
  2378. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2379. NSUInteger count = _dictionary.count;
  2380. if (count == 0) {
  2381. return 0;
  2382. }
  2383. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2384. GPBDataType keyDataType = field.mapKeyDataType;
  2385. __block size_t result = 0;
  2386. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2387. NSNumber *aValue,
  2388. BOOL *stop) {
  2389. #pragma unused(stop)
  2390. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2391. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2392. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2393. }];
  2394. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2395. result += tagSize * count;
  2396. return result;
  2397. }
  2398. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2399. asField:(GPBFieldDescriptor *)field {
  2400. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2401. GPBDataType keyDataType = field.mapKeyDataType;
  2402. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2403. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2404. NSNumber *aValue,
  2405. BOOL *stop) {
  2406. #pragma unused(stop)
  2407. // Write the tag.
  2408. [outputStream writeInt32NoTag:tag];
  2409. // Write the size of the message.
  2410. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2411. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2412. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2413. // Write the fields.
  2414. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2415. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2416. }];
  2417. }
  2418. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2419. forGPBGenericValueKey:(GPBGenericValue *)key {
  2420. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2421. }
  2422. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2423. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2424. #pragma unused(stop)
  2425. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2426. }];
  2427. }
  2428. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2429. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2430. if (wrapped && value) {
  2431. *value = [wrapped floatValue];
  2432. }
  2433. return (wrapped != NULL);
  2434. }
  2435. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2436. if (otherDictionary) {
  2437. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2438. if (_autocreator) {
  2439. GPBAutocreatedDictionaryModified(_autocreator, self);
  2440. }
  2441. }
  2442. }
  2443. - (void)setValue:(float)value forKey:(uint32_t)key {
  2444. [_dictionary setObject:@(value) forKey:@(key)];
  2445. if (_autocreator) {
  2446. GPBAutocreatedDictionaryModified(_autocreator, self);
  2447. }
  2448. }
  2449. - (void)removeValueForKey:(uint32_t)aKey {
  2450. [_dictionary removeObjectForKey:@(aKey)];
  2451. }
  2452. - (void)removeAll {
  2453. [_dictionary removeAllObjects];
  2454. }
  2455. @end
  2456. #pragma mark - UInt32 -> Double
  2457. @implementation GPBUInt32DoubleDictionary {
  2458. @package
  2459. NSMutableDictionary *_dictionary;
  2460. }
  2461. + (instancetype)dictionary {
  2462. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2463. }
  2464. + (instancetype)dictionaryWithValue:(double)value
  2465. forKey:(uint32_t)key {
  2466. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2467. // on to get the type correct.
  2468. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2469. forKeys:&key
  2470. count:1] autorelease];
  2471. }
  2472. + (instancetype)dictionaryWithValues:(const double [])values
  2473. forKeys:(const uint32_t [])keys
  2474. count:(NSUInteger)count {
  2475. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2476. // on to get the type correct.
  2477. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2478. forKeys:keys
  2479. count:count] autorelease];
  2480. }
  2481. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2482. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2483. // on to get the type correct.
  2484. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2485. }
  2486. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2487. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2488. }
  2489. - (instancetype)init {
  2490. return [self initWithValues:NULL forKeys:NULL count:0];
  2491. }
  2492. - (instancetype)initWithValues:(const double [])values
  2493. forKeys:(const uint32_t [])keys
  2494. count:(NSUInteger)count {
  2495. self = [super init];
  2496. if (self) {
  2497. _dictionary = [[NSMutableDictionary alloc] init];
  2498. if (count && values && keys) {
  2499. for (NSUInteger i = 0; i < count; ++i) {
  2500. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2501. }
  2502. }
  2503. }
  2504. return self;
  2505. }
  2506. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2507. self = [self initWithValues:NULL forKeys:NULL count:0];
  2508. if (self) {
  2509. if (dictionary) {
  2510. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2511. }
  2512. }
  2513. return self;
  2514. }
  2515. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2516. #pragma unused(numItems)
  2517. return [self initWithValues:NULL forKeys:NULL count:0];
  2518. }
  2519. - (void)dealloc {
  2520. NSAssert(!_autocreator,
  2521. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2522. [self class], _autocreator);
  2523. [_dictionary release];
  2524. [super dealloc];
  2525. }
  2526. - (instancetype)copyWithZone:(NSZone *)zone {
  2527. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2528. }
  2529. - (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
  2530. if (self == other) {
  2531. return YES;
  2532. }
  2533. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2534. return NO;
  2535. }
  2536. return [_dictionary isEqual:other->_dictionary];
  2537. }
  2538. - (NSUInteger)hash {
  2539. return _dictionary.count;
  2540. }
  2541. - (NSString *)description {
  2542. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2543. }
  2544. - (NSUInteger)count {
  2545. return _dictionary.count;
  2546. }
  2547. - (void)enumerateKeysAndValuesUsingBlock:
  2548. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2549. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2550. NSNumber *aValue,
  2551. BOOL *stop) {
  2552. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2553. }];
  2554. }
  2555. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2556. NSUInteger count = _dictionary.count;
  2557. if (count == 0) {
  2558. return 0;
  2559. }
  2560. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2561. GPBDataType keyDataType = field.mapKeyDataType;
  2562. __block size_t result = 0;
  2563. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2564. NSNumber *aValue,
  2565. BOOL *stop) {
  2566. #pragma unused(stop)
  2567. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2568. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2570. }];
  2571. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2572. result += tagSize * count;
  2573. return result;
  2574. }
  2575. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2576. asField:(GPBFieldDescriptor *)field {
  2577. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2578. GPBDataType keyDataType = field.mapKeyDataType;
  2579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2580. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2581. NSNumber *aValue,
  2582. BOOL *stop) {
  2583. #pragma unused(stop)
  2584. // Write the tag.
  2585. [outputStream writeInt32NoTag:tag];
  2586. // Write the size of the message.
  2587. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2588. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2589. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2590. // Write the fields.
  2591. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2592. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2593. }];
  2594. }
  2595. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2596. forGPBGenericValueKey:(GPBGenericValue *)key {
  2597. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2598. }
  2599. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2600. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2601. #pragma unused(stop)
  2602. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2603. }];
  2604. }
  2605. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2606. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2607. if (wrapped && value) {
  2608. *value = [wrapped doubleValue];
  2609. }
  2610. return (wrapped != NULL);
  2611. }
  2612. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2613. if (otherDictionary) {
  2614. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2615. if (_autocreator) {
  2616. GPBAutocreatedDictionaryModified(_autocreator, self);
  2617. }
  2618. }
  2619. }
  2620. - (void)setValue:(double)value forKey:(uint32_t)key {
  2621. [_dictionary setObject:@(value) forKey:@(key)];
  2622. if (_autocreator) {
  2623. GPBAutocreatedDictionaryModified(_autocreator, self);
  2624. }
  2625. }
  2626. - (void)removeValueForKey:(uint32_t)aKey {
  2627. [_dictionary removeObjectForKey:@(aKey)];
  2628. }
  2629. - (void)removeAll {
  2630. [_dictionary removeAllObjects];
  2631. }
  2632. @end
  2633. #pragma mark - UInt32 -> Enum
  2634. @implementation GPBUInt32EnumDictionary {
  2635. @package
  2636. NSMutableDictionary *_dictionary;
  2637. GPBEnumValidationFunc _validationFunc;
  2638. }
  2639. @synthesize validationFunc = _validationFunc;
  2640. + (instancetype)dictionary {
  2641. return [[[self alloc] initWithValidationFunction:NULL
  2642. rawValues:NULL
  2643. forKeys:NULL
  2644. count:0] autorelease];
  2645. }
  2646. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2647. return [[[self alloc] initWithValidationFunction:func
  2648. rawValues:NULL
  2649. forKeys:NULL
  2650. count:0] autorelease];
  2651. }
  2652. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2653. rawValue:(int32_t)rawValue
  2654. forKey:(uint32_t)key {
  2655. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2656. // on to get the type correct.
  2657. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2658. rawValues:&rawValue
  2659. forKeys:&key
  2660. count:1] autorelease];
  2661. }
  2662. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2663. rawValues:(const int32_t [])rawValues
  2664. forKeys:(const uint32_t [])keys
  2665. count:(NSUInteger)count {
  2666. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2667. // on to get the type correct.
  2668. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2669. rawValues:rawValues
  2670. forKeys:keys
  2671. count:count] autorelease];
  2672. }
  2673. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2674. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2675. // on to get the type correct.
  2676. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2677. }
  2678. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2679. capacity:(NSUInteger)numItems {
  2680. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2681. }
  2682. - (instancetype)init {
  2683. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2684. }
  2685. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2686. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2687. }
  2688. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2689. rawValues:(const int32_t [])rawValues
  2690. forKeys:(const uint32_t [])keys
  2691. count:(NSUInteger)count {
  2692. self = [super init];
  2693. if (self) {
  2694. _dictionary = [[NSMutableDictionary alloc] init];
  2695. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2696. if (count && rawValues && keys) {
  2697. for (NSUInteger i = 0; i < count; ++i) {
  2698. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2699. }
  2700. }
  2701. }
  2702. return self;
  2703. }
  2704. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2705. self = [self initWithValidationFunction:dictionary.validationFunc
  2706. rawValues:NULL
  2707. forKeys:NULL
  2708. count:0];
  2709. if (self) {
  2710. if (dictionary) {
  2711. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2712. }
  2713. }
  2714. return self;
  2715. }
  2716. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2717. capacity:(NSUInteger)numItems {
  2718. #pragma unused(numItems)
  2719. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2720. }
  2721. - (void)dealloc {
  2722. NSAssert(!_autocreator,
  2723. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2724. [self class], _autocreator);
  2725. [_dictionary release];
  2726. [super dealloc];
  2727. }
  2728. - (instancetype)copyWithZone:(NSZone *)zone {
  2729. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2730. }
  2731. - (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
  2732. if (self == other) {
  2733. return YES;
  2734. }
  2735. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2736. return NO;
  2737. }
  2738. return [_dictionary isEqual:other->_dictionary];
  2739. }
  2740. - (NSUInteger)hash {
  2741. return _dictionary.count;
  2742. }
  2743. - (NSString *)description {
  2744. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2745. }
  2746. - (NSUInteger)count {
  2747. return _dictionary.count;
  2748. }
  2749. - (void)enumerateKeysAndRawValuesUsingBlock:
  2750. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2751. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2752. NSNumber *aValue,
  2753. BOOL *stop) {
  2754. block([aKey unsignedIntValue], [aValue intValue], stop);
  2755. }];
  2756. }
  2757. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2758. NSUInteger count = _dictionary.count;
  2759. if (count == 0) {
  2760. return 0;
  2761. }
  2762. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2763. GPBDataType keyDataType = field.mapKeyDataType;
  2764. __block size_t result = 0;
  2765. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2766. NSNumber *aValue,
  2767. BOOL *stop) {
  2768. #pragma unused(stop)
  2769. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2770. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2771. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2772. }];
  2773. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2774. result += tagSize * count;
  2775. return result;
  2776. }
  2777. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2778. asField:(GPBFieldDescriptor *)field {
  2779. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2780. GPBDataType keyDataType = field.mapKeyDataType;
  2781. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2782. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2783. NSNumber *aValue,
  2784. BOOL *stop) {
  2785. #pragma unused(stop)
  2786. // Write the tag.
  2787. [outputStream writeInt32NoTag:tag];
  2788. // Write the size of the message.
  2789. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2790. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2791. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2792. // Write the fields.
  2793. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2794. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2795. }];
  2796. }
  2797. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2798. forKey:(GPBGenericValue *)key
  2799. keyDataType:(GPBDataType)keyDataType {
  2800. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2801. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2802. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2803. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2804. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2805. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2806. [outputStream release];
  2807. return data;
  2808. }
  2809. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2810. forGPBGenericValueKey:(GPBGenericValue *)key {
  2811. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2812. }
  2813. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2814. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2815. #pragma unused(stop)
  2816. block([NSString stringWithFormat:@"%u", key], @(value));
  2817. }];
  2818. }
  2819. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2820. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2821. if (wrapped && value) {
  2822. int32_t result = [wrapped intValue];
  2823. if (!_validationFunc(result)) {
  2824. result = kGPBUnrecognizedEnumeratorValue;
  2825. }
  2826. *value = result;
  2827. }
  2828. return (wrapped != NULL);
  2829. }
  2830. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2831. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2832. if (wrapped && rawValue) {
  2833. *rawValue = [wrapped intValue];
  2834. }
  2835. return (wrapped != NULL);
  2836. }
  2837. - (void)enumerateKeysAndValuesUsingBlock:
  2838. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2839. GPBEnumValidationFunc func = _validationFunc;
  2840. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2841. NSNumber *aValue,
  2842. BOOL *stop) {
  2843. int32_t unwrapped = [aValue intValue];
  2844. if (!func(unwrapped)) {
  2845. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2846. }
  2847. block([aKey unsignedIntValue], unwrapped, stop);
  2848. }];
  2849. }
  2850. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2851. if (otherDictionary) {
  2852. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2853. if (_autocreator) {
  2854. GPBAutocreatedDictionaryModified(_autocreator, self);
  2855. }
  2856. }
  2857. }
  2858. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2859. [_dictionary setObject:@(value) forKey:@(key)];
  2860. if (_autocreator) {
  2861. GPBAutocreatedDictionaryModified(_autocreator, self);
  2862. }
  2863. }
  2864. - (void)removeValueForKey:(uint32_t)aKey {
  2865. [_dictionary removeObjectForKey:@(aKey)];
  2866. }
  2867. - (void)removeAll {
  2868. [_dictionary removeAllObjects];
  2869. }
  2870. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2871. if (!_validationFunc(value)) {
  2872. [NSException raise:NSInvalidArgumentException
  2873. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2874. value];
  2875. }
  2876. [_dictionary setObject:@(value) forKey:@(key)];
  2877. if (_autocreator) {
  2878. GPBAutocreatedDictionaryModified(_autocreator, self);
  2879. }
  2880. }
  2881. @end
  2882. #pragma mark - UInt32 -> Object
  2883. @implementation GPBUInt32ObjectDictionary {
  2884. @package
  2885. NSMutableDictionary *_dictionary;
  2886. }
  2887. + (instancetype)dictionary {
  2888. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2889. }
  2890. + (instancetype)dictionaryWithValue:(id)value
  2891. forKey:(uint32_t)key {
  2892. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2893. // on to get the type correct.
  2894. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithValues:&value
  2895. forKeys:&key
  2896. count:1] autorelease];
  2897. }
  2898. + (instancetype)dictionaryWithValues:(const id [])values
  2899. forKeys:(const uint32_t [])keys
  2900. count:(NSUInteger)count {
  2901. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2902. // on to get the type correct.
  2903. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithValues:values
  2904. forKeys:keys
  2905. count:count] autorelease];
  2906. }
  2907. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2908. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2909. // on to get the type correct.
  2910. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2911. }
  2912. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2913. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2914. }
  2915. - (instancetype)init {
  2916. return [self initWithValues:NULL forKeys:NULL count:0];
  2917. }
  2918. - (instancetype)initWithValues:(const id [])values
  2919. forKeys:(const uint32_t [])keys
  2920. count:(NSUInteger)count {
  2921. self = [super init];
  2922. if (self) {
  2923. _dictionary = [[NSMutableDictionary alloc] init];
  2924. if (count && values && keys) {
  2925. for (NSUInteger i = 0; i < count; ++i) {
  2926. [_dictionary setObject:values[i] forKey:@(keys[i])];
  2927. }
  2928. }
  2929. }
  2930. return self;
  2931. }
  2932. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2933. self = [self initWithValues:NULL forKeys:NULL count:0];
  2934. if (self) {
  2935. if (dictionary) {
  2936. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2937. }
  2938. }
  2939. return self;
  2940. }
  2941. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2942. #pragma unused(numItems)
  2943. return [self initWithValues:NULL forKeys:NULL count:0];
  2944. }
  2945. - (void)dealloc {
  2946. NSAssert(!_autocreator,
  2947. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2948. [self class], _autocreator);
  2949. [_dictionary release];
  2950. [super dealloc];
  2951. }
  2952. - (instancetype)copyWithZone:(NSZone *)zone {
  2953. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2954. }
  2955. - (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
  2956. if (self == other) {
  2957. return YES;
  2958. }
  2959. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2960. return NO;
  2961. }
  2962. return [_dictionary isEqual:other->_dictionary];
  2963. }
  2964. - (NSUInteger)hash {
  2965. return _dictionary.count;
  2966. }
  2967. - (NSString *)description {
  2968. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2969. }
  2970. - (NSUInteger)count {
  2971. return _dictionary.count;
  2972. }
  2973. - (void)enumerateKeysAndValuesUsingBlock:
  2974. (void (^)(uint32_t key, id value, BOOL *stop))block {
  2975. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2976. id aValue,
  2977. BOOL *stop) {
  2978. block([aKey unsignedIntValue], aValue, stop);
  2979. }];
  2980. }
  2981. - (BOOL)isInitialized {
  2982. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2983. if (!msg.initialized) {
  2984. return NO;
  2985. }
  2986. }
  2987. return YES;
  2988. }
  2989. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2990. GPBUInt32ObjectDictionary *newDict =
  2991. [[GPBUInt32ObjectDictionary alloc] init];
  2992. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  2993. GPBMessage *msg,
  2994. BOOL *stop) {
  2995. #pragma unused(stop)
  2996. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  2997. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  2998. [copiedMsg release];
  2999. }];
  3000. return newDict;
  3001. }
  3002. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3003. NSUInteger count = _dictionary.count;
  3004. if (count == 0) {
  3005. return 0;
  3006. }
  3007. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3008. GPBDataType keyDataType = field.mapKeyDataType;
  3009. __block size_t result = 0;
  3010. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3011. id aValue,
  3012. BOOL *stop) {
  3013. #pragma unused(stop)
  3014. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3015. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  3016. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3017. }];
  3018. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3019. result += tagSize * count;
  3020. return result;
  3021. }
  3022. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3023. asField:(GPBFieldDescriptor *)field {
  3024. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3025. GPBDataType keyDataType = field.mapKeyDataType;
  3026. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3027. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3028. id aValue,
  3029. BOOL *stop) {
  3030. #pragma unused(stop)
  3031. // Write the tag.
  3032. [outputStream writeInt32NoTag:tag];
  3033. // Write the size of the message.
  3034. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3035. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  3036. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3037. // Write the fields.
  3038. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3039. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueDataType);
  3040. }];
  3041. }
  3042. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3043. forGPBGenericValueKey:(GPBGenericValue *)key {
  3044. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3045. }
  3046. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3047. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, id value, BOOL *stop) {
  3048. #pragma unused(stop)
  3049. block([NSString stringWithFormat:@"%u", key], value);
  3050. }];
  3051. }
  3052. - (id)valueForKey:(uint32_t)key {
  3053. id result = [_dictionary objectForKey:@(key)];
  3054. return result;
  3055. }
  3056. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3057. if (otherDictionary) {
  3058. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3059. if (_autocreator) {
  3060. GPBAutocreatedDictionaryModified(_autocreator, self);
  3061. }
  3062. }
  3063. }
  3064. - (void)setValue:(id)value forKey:(uint32_t)key {
  3065. [_dictionary setObject:value forKey:@(key)];
  3066. if (_autocreator) {
  3067. GPBAutocreatedDictionaryModified(_autocreator, self);
  3068. }
  3069. }
  3070. - (void)removeValueForKey:(uint32_t)aKey {
  3071. [_dictionary removeObjectForKey:@(aKey)];
  3072. }
  3073. - (void)removeAll {
  3074. [_dictionary removeAllObjects];
  3075. }
  3076. @end
  3077. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3078. // This block of code is generated, do not edit it directly.
  3079. #pragma mark - Int32 -> UInt32
  3080. @implementation GPBInt32UInt32Dictionary {
  3081. @package
  3082. NSMutableDictionary *_dictionary;
  3083. }
  3084. + (instancetype)dictionary {
  3085. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3086. }
  3087. + (instancetype)dictionaryWithValue:(uint32_t)value
  3088. forKey:(int32_t)key {
  3089. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3090. // on to get the type correct.
  3091. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  3092. forKeys:&key
  3093. count:1] autorelease];
  3094. }
  3095. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  3096. forKeys:(const int32_t [])keys
  3097. count:(NSUInteger)count {
  3098. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3099. // on to get the type correct.
  3100. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  3101. forKeys:keys
  3102. count:count] autorelease];
  3103. }
  3104. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3105. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3106. // on to get the type correct.
  3107. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3108. }
  3109. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3110. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3111. }
  3112. - (instancetype)init {
  3113. return [self initWithValues:NULL forKeys:NULL count:0];
  3114. }
  3115. - (instancetype)initWithValues:(const uint32_t [])values
  3116. forKeys:(const int32_t [])keys
  3117. count:(NSUInteger)count {
  3118. self = [super init];
  3119. if (self) {
  3120. _dictionary = [[NSMutableDictionary alloc] init];
  3121. if (count && values && keys) {
  3122. for (NSUInteger i = 0; i < count; ++i) {
  3123. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3124. }
  3125. }
  3126. }
  3127. return self;
  3128. }
  3129. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3130. self = [self initWithValues:NULL forKeys:NULL count:0];
  3131. if (self) {
  3132. if (dictionary) {
  3133. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3134. }
  3135. }
  3136. return self;
  3137. }
  3138. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3139. #pragma unused(numItems)
  3140. return [self initWithValues:NULL forKeys:NULL count:0];
  3141. }
  3142. - (void)dealloc {
  3143. NSAssert(!_autocreator,
  3144. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3145. [self class], _autocreator);
  3146. [_dictionary release];
  3147. [super dealloc];
  3148. }
  3149. - (instancetype)copyWithZone:(NSZone *)zone {
  3150. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3151. }
  3152. - (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
  3153. if (self == other) {
  3154. return YES;
  3155. }
  3156. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3157. return NO;
  3158. }
  3159. return [_dictionary isEqual:other->_dictionary];
  3160. }
  3161. - (NSUInteger)hash {
  3162. return _dictionary.count;
  3163. }
  3164. - (NSString *)description {
  3165. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3166. }
  3167. - (NSUInteger)count {
  3168. return _dictionary.count;
  3169. }
  3170. - (void)enumerateKeysAndValuesUsingBlock:
  3171. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3172. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3173. NSNumber *aValue,
  3174. BOOL *stop) {
  3175. block([aKey intValue], [aValue unsignedIntValue], stop);
  3176. }];
  3177. }
  3178. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3179. NSUInteger count = _dictionary.count;
  3180. if (count == 0) {
  3181. return 0;
  3182. }
  3183. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3184. GPBDataType keyDataType = field.mapKeyDataType;
  3185. __block size_t result = 0;
  3186. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3187. NSNumber *aValue,
  3188. BOOL *stop) {
  3189. #pragma unused(stop)
  3190. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3191. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3192. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3193. }];
  3194. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3195. result += tagSize * count;
  3196. return result;
  3197. }
  3198. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3199. asField:(GPBFieldDescriptor *)field {
  3200. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3201. GPBDataType keyDataType = field.mapKeyDataType;
  3202. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3203. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3204. NSNumber *aValue,
  3205. BOOL *stop) {
  3206. #pragma unused(stop)
  3207. // Write the tag.
  3208. [outputStream writeInt32NoTag:tag];
  3209. // Write the size of the message.
  3210. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3211. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3212. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3213. // Write the fields.
  3214. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3215. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3216. }];
  3217. }
  3218. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3219. forGPBGenericValueKey:(GPBGenericValue *)key {
  3220. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3221. }
  3222. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3223. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3224. #pragma unused(stop)
  3225. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3226. }];
  3227. }
  3228. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3229. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3230. if (wrapped && value) {
  3231. *value = [wrapped unsignedIntValue];
  3232. }
  3233. return (wrapped != NULL);
  3234. }
  3235. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3236. if (otherDictionary) {
  3237. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3238. if (_autocreator) {
  3239. GPBAutocreatedDictionaryModified(_autocreator, self);
  3240. }
  3241. }
  3242. }
  3243. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3244. [_dictionary setObject:@(value) forKey:@(key)];
  3245. if (_autocreator) {
  3246. GPBAutocreatedDictionaryModified(_autocreator, self);
  3247. }
  3248. }
  3249. - (void)removeValueForKey:(int32_t)aKey {
  3250. [_dictionary removeObjectForKey:@(aKey)];
  3251. }
  3252. - (void)removeAll {
  3253. [_dictionary removeAllObjects];
  3254. }
  3255. @end
  3256. #pragma mark - Int32 -> Int32
  3257. @implementation GPBInt32Int32Dictionary {
  3258. @package
  3259. NSMutableDictionary *_dictionary;
  3260. }
  3261. + (instancetype)dictionary {
  3262. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3263. }
  3264. + (instancetype)dictionaryWithValue:(int32_t)value
  3265. forKey:(int32_t)key {
  3266. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3267. // on to get the type correct.
  3268. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3269. forKeys:&key
  3270. count:1] autorelease];
  3271. }
  3272. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3273. forKeys:(const int32_t [])keys
  3274. count:(NSUInteger)count {
  3275. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3276. // on to get the type correct.
  3277. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3278. forKeys:keys
  3279. count:count] autorelease];
  3280. }
  3281. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3282. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3283. // on to get the type correct.
  3284. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3285. }
  3286. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3287. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3288. }
  3289. - (instancetype)init {
  3290. return [self initWithValues:NULL forKeys:NULL count:0];
  3291. }
  3292. - (instancetype)initWithValues:(const int32_t [])values
  3293. forKeys:(const int32_t [])keys
  3294. count:(NSUInteger)count {
  3295. self = [super init];
  3296. if (self) {
  3297. _dictionary = [[NSMutableDictionary alloc] init];
  3298. if (count && values && keys) {
  3299. for (NSUInteger i = 0; i < count; ++i) {
  3300. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3301. }
  3302. }
  3303. }
  3304. return self;
  3305. }
  3306. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3307. self = [self initWithValues:NULL forKeys:NULL count:0];
  3308. if (self) {
  3309. if (dictionary) {
  3310. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3311. }
  3312. }
  3313. return self;
  3314. }
  3315. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3316. #pragma unused(numItems)
  3317. return [self initWithValues:NULL forKeys:NULL count:0];
  3318. }
  3319. - (void)dealloc {
  3320. NSAssert(!_autocreator,
  3321. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3322. [self class], _autocreator);
  3323. [_dictionary release];
  3324. [super dealloc];
  3325. }
  3326. - (instancetype)copyWithZone:(NSZone *)zone {
  3327. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3328. }
  3329. - (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
  3330. if (self == other) {
  3331. return YES;
  3332. }
  3333. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3334. return NO;
  3335. }
  3336. return [_dictionary isEqual:other->_dictionary];
  3337. }
  3338. - (NSUInteger)hash {
  3339. return _dictionary.count;
  3340. }
  3341. - (NSString *)description {
  3342. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3343. }
  3344. - (NSUInteger)count {
  3345. return _dictionary.count;
  3346. }
  3347. - (void)enumerateKeysAndValuesUsingBlock:
  3348. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3349. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3350. NSNumber *aValue,
  3351. BOOL *stop) {
  3352. block([aKey intValue], [aValue intValue], stop);
  3353. }];
  3354. }
  3355. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3356. NSUInteger count = _dictionary.count;
  3357. if (count == 0) {
  3358. return 0;
  3359. }
  3360. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3361. GPBDataType keyDataType = field.mapKeyDataType;
  3362. __block size_t result = 0;
  3363. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3364. NSNumber *aValue,
  3365. BOOL *stop) {
  3366. #pragma unused(stop)
  3367. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3368. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3369. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3370. }];
  3371. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3372. result += tagSize * count;
  3373. return result;
  3374. }
  3375. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3376. asField:(GPBFieldDescriptor *)field {
  3377. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3378. GPBDataType keyDataType = field.mapKeyDataType;
  3379. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3380. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3381. NSNumber *aValue,
  3382. BOOL *stop) {
  3383. #pragma unused(stop)
  3384. // Write the tag.
  3385. [outputStream writeInt32NoTag:tag];
  3386. // Write the size of the message.
  3387. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3388. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3389. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3390. // Write the fields.
  3391. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3392. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3393. }];
  3394. }
  3395. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3396. forGPBGenericValueKey:(GPBGenericValue *)key {
  3397. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3398. }
  3399. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3400. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3401. #pragma unused(stop)
  3402. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3403. }];
  3404. }
  3405. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3406. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3407. if (wrapped && value) {
  3408. *value = [wrapped intValue];
  3409. }
  3410. return (wrapped != NULL);
  3411. }
  3412. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3413. if (otherDictionary) {
  3414. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3415. if (_autocreator) {
  3416. GPBAutocreatedDictionaryModified(_autocreator, self);
  3417. }
  3418. }
  3419. }
  3420. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3421. [_dictionary setObject:@(value) forKey:@(key)];
  3422. if (_autocreator) {
  3423. GPBAutocreatedDictionaryModified(_autocreator, self);
  3424. }
  3425. }
  3426. - (void)removeValueForKey:(int32_t)aKey {
  3427. [_dictionary removeObjectForKey:@(aKey)];
  3428. }
  3429. - (void)removeAll {
  3430. [_dictionary removeAllObjects];
  3431. }
  3432. @end
  3433. #pragma mark - Int32 -> UInt64
  3434. @implementation GPBInt32UInt64Dictionary {
  3435. @package
  3436. NSMutableDictionary *_dictionary;
  3437. }
  3438. + (instancetype)dictionary {
  3439. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3440. }
  3441. + (instancetype)dictionaryWithValue:(uint64_t)value
  3442. forKey:(int32_t)key {
  3443. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3444. // on to get the type correct.
  3445. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3446. forKeys:&key
  3447. count:1] autorelease];
  3448. }
  3449. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3450. forKeys:(const int32_t [])keys
  3451. count:(NSUInteger)count {
  3452. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3453. // on to get the type correct.
  3454. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3455. forKeys:keys
  3456. count:count] autorelease];
  3457. }
  3458. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3459. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3460. // on to get the type correct.
  3461. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3462. }
  3463. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3464. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3465. }
  3466. - (instancetype)init {
  3467. return [self initWithValues:NULL forKeys:NULL count:0];
  3468. }
  3469. - (instancetype)initWithValues:(const uint64_t [])values
  3470. forKeys:(const int32_t [])keys
  3471. count:(NSUInteger)count {
  3472. self = [super init];
  3473. if (self) {
  3474. _dictionary = [[NSMutableDictionary alloc] init];
  3475. if (count && values && keys) {
  3476. for (NSUInteger i = 0; i < count; ++i) {
  3477. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3478. }
  3479. }
  3480. }
  3481. return self;
  3482. }
  3483. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3484. self = [self initWithValues:NULL forKeys:NULL count:0];
  3485. if (self) {
  3486. if (dictionary) {
  3487. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3488. }
  3489. }
  3490. return self;
  3491. }
  3492. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3493. #pragma unused(numItems)
  3494. return [self initWithValues:NULL forKeys:NULL count:0];
  3495. }
  3496. - (void)dealloc {
  3497. NSAssert(!_autocreator,
  3498. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3499. [self class], _autocreator);
  3500. [_dictionary release];
  3501. [super dealloc];
  3502. }
  3503. - (instancetype)copyWithZone:(NSZone *)zone {
  3504. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3505. }
  3506. - (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
  3507. if (self == other) {
  3508. return YES;
  3509. }
  3510. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3511. return NO;
  3512. }
  3513. return [_dictionary isEqual:other->_dictionary];
  3514. }
  3515. - (NSUInteger)hash {
  3516. return _dictionary.count;
  3517. }
  3518. - (NSString *)description {
  3519. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3520. }
  3521. - (NSUInteger)count {
  3522. return _dictionary.count;
  3523. }
  3524. - (void)enumerateKeysAndValuesUsingBlock:
  3525. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3526. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3527. NSNumber *aValue,
  3528. BOOL *stop) {
  3529. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3530. }];
  3531. }
  3532. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3533. NSUInteger count = _dictionary.count;
  3534. if (count == 0) {
  3535. return 0;
  3536. }
  3537. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3538. GPBDataType keyDataType = field.mapKeyDataType;
  3539. __block size_t result = 0;
  3540. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3541. NSNumber *aValue,
  3542. BOOL *stop) {
  3543. #pragma unused(stop)
  3544. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3545. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3546. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3547. }];
  3548. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3549. result += tagSize * count;
  3550. return result;
  3551. }
  3552. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3553. asField:(GPBFieldDescriptor *)field {
  3554. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3555. GPBDataType keyDataType = field.mapKeyDataType;
  3556. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3557. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3558. NSNumber *aValue,
  3559. BOOL *stop) {
  3560. #pragma unused(stop)
  3561. // Write the tag.
  3562. [outputStream writeInt32NoTag:tag];
  3563. // Write the size of the message.
  3564. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3565. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3566. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3567. // Write the fields.
  3568. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3569. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3570. }];
  3571. }
  3572. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3573. forGPBGenericValueKey:(GPBGenericValue *)key {
  3574. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3575. }
  3576. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3577. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3578. #pragma unused(stop)
  3579. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3580. }];
  3581. }
  3582. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3583. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3584. if (wrapped && value) {
  3585. *value = [wrapped unsignedLongLongValue];
  3586. }
  3587. return (wrapped != NULL);
  3588. }
  3589. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3590. if (otherDictionary) {
  3591. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3592. if (_autocreator) {
  3593. GPBAutocreatedDictionaryModified(_autocreator, self);
  3594. }
  3595. }
  3596. }
  3597. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3598. [_dictionary setObject:@(value) forKey:@(key)];
  3599. if (_autocreator) {
  3600. GPBAutocreatedDictionaryModified(_autocreator, self);
  3601. }
  3602. }
  3603. - (void)removeValueForKey:(int32_t)aKey {
  3604. [_dictionary removeObjectForKey:@(aKey)];
  3605. }
  3606. - (void)removeAll {
  3607. [_dictionary removeAllObjects];
  3608. }
  3609. @end
  3610. #pragma mark - Int32 -> Int64
  3611. @implementation GPBInt32Int64Dictionary {
  3612. @package
  3613. NSMutableDictionary *_dictionary;
  3614. }
  3615. + (instancetype)dictionary {
  3616. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3617. }
  3618. + (instancetype)dictionaryWithValue:(int64_t)value
  3619. forKey:(int32_t)key {
  3620. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3621. // on to get the type correct.
  3622. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3623. forKeys:&key
  3624. count:1] autorelease];
  3625. }
  3626. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3627. forKeys:(const int32_t [])keys
  3628. count:(NSUInteger)count {
  3629. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3630. // on to get the type correct.
  3631. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3632. forKeys:keys
  3633. count:count] autorelease];
  3634. }
  3635. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3636. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3637. // on to get the type correct.
  3638. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3639. }
  3640. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3641. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3642. }
  3643. - (instancetype)init {
  3644. return [self initWithValues:NULL forKeys:NULL count:0];
  3645. }
  3646. - (instancetype)initWithValues:(const int64_t [])values
  3647. forKeys:(const int32_t [])keys
  3648. count:(NSUInteger)count {
  3649. self = [super init];
  3650. if (self) {
  3651. _dictionary = [[NSMutableDictionary alloc] init];
  3652. if (count && values && keys) {
  3653. for (NSUInteger i = 0; i < count; ++i) {
  3654. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3655. }
  3656. }
  3657. }
  3658. return self;
  3659. }
  3660. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3661. self = [self initWithValues:NULL forKeys:NULL count:0];
  3662. if (self) {
  3663. if (dictionary) {
  3664. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3665. }
  3666. }
  3667. return self;
  3668. }
  3669. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3670. #pragma unused(numItems)
  3671. return [self initWithValues:NULL forKeys:NULL count:0];
  3672. }
  3673. - (void)dealloc {
  3674. NSAssert(!_autocreator,
  3675. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3676. [self class], _autocreator);
  3677. [_dictionary release];
  3678. [super dealloc];
  3679. }
  3680. - (instancetype)copyWithZone:(NSZone *)zone {
  3681. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3682. }
  3683. - (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
  3684. if (self == other) {
  3685. return YES;
  3686. }
  3687. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3688. return NO;
  3689. }
  3690. return [_dictionary isEqual:other->_dictionary];
  3691. }
  3692. - (NSUInteger)hash {
  3693. return _dictionary.count;
  3694. }
  3695. - (NSString *)description {
  3696. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3697. }
  3698. - (NSUInteger)count {
  3699. return _dictionary.count;
  3700. }
  3701. - (void)enumerateKeysAndValuesUsingBlock:
  3702. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3703. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3704. NSNumber *aValue,
  3705. BOOL *stop) {
  3706. block([aKey intValue], [aValue longLongValue], stop);
  3707. }];
  3708. }
  3709. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3710. NSUInteger count = _dictionary.count;
  3711. if (count == 0) {
  3712. return 0;
  3713. }
  3714. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3715. GPBDataType keyDataType = field.mapKeyDataType;
  3716. __block size_t result = 0;
  3717. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3718. NSNumber *aValue,
  3719. BOOL *stop) {
  3720. #pragma unused(stop)
  3721. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3722. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3723. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3724. }];
  3725. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3726. result += tagSize * count;
  3727. return result;
  3728. }
  3729. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3730. asField:(GPBFieldDescriptor *)field {
  3731. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3732. GPBDataType keyDataType = field.mapKeyDataType;
  3733. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3734. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3735. NSNumber *aValue,
  3736. BOOL *stop) {
  3737. #pragma unused(stop)
  3738. // Write the tag.
  3739. [outputStream writeInt32NoTag:tag];
  3740. // Write the size of the message.
  3741. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3742. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3743. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3744. // Write the fields.
  3745. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3746. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3747. }];
  3748. }
  3749. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3750. forGPBGenericValueKey:(GPBGenericValue *)key {
  3751. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3752. }
  3753. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3754. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3755. #pragma unused(stop)
  3756. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3757. }];
  3758. }
  3759. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3760. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3761. if (wrapped && value) {
  3762. *value = [wrapped longLongValue];
  3763. }
  3764. return (wrapped != NULL);
  3765. }
  3766. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3767. if (otherDictionary) {
  3768. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3769. if (_autocreator) {
  3770. GPBAutocreatedDictionaryModified(_autocreator, self);
  3771. }
  3772. }
  3773. }
  3774. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3775. [_dictionary setObject:@(value) forKey:@(key)];
  3776. if (_autocreator) {
  3777. GPBAutocreatedDictionaryModified(_autocreator, self);
  3778. }
  3779. }
  3780. - (void)removeValueForKey:(int32_t)aKey {
  3781. [_dictionary removeObjectForKey:@(aKey)];
  3782. }
  3783. - (void)removeAll {
  3784. [_dictionary removeAllObjects];
  3785. }
  3786. @end
  3787. #pragma mark - Int32 -> Bool
  3788. @implementation GPBInt32BoolDictionary {
  3789. @package
  3790. NSMutableDictionary *_dictionary;
  3791. }
  3792. + (instancetype)dictionary {
  3793. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3794. }
  3795. + (instancetype)dictionaryWithValue:(BOOL)value
  3796. forKey:(int32_t)key {
  3797. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3798. // on to get the type correct.
  3799. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3800. forKeys:&key
  3801. count:1] autorelease];
  3802. }
  3803. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3804. forKeys:(const int32_t [])keys
  3805. count:(NSUInteger)count {
  3806. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3807. // on to get the type correct.
  3808. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3809. forKeys:keys
  3810. count:count] autorelease];
  3811. }
  3812. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3813. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3814. // on to get the type correct.
  3815. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3816. }
  3817. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3818. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3819. }
  3820. - (instancetype)init {
  3821. return [self initWithValues:NULL forKeys:NULL count:0];
  3822. }
  3823. - (instancetype)initWithValues:(const BOOL [])values
  3824. forKeys:(const int32_t [])keys
  3825. count:(NSUInteger)count {
  3826. self = [super init];
  3827. if (self) {
  3828. _dictionary = [[NSMutableDictionary alloc] init];
  3829. if (count && values && keys) {
  3830. for (NSUInteger i = 0; i < count; ++i) {
  3831. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3832. }
  3833. }
  3834. }
  3835. return self;
  3836. }
  3837. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3838. self = [self initWithValues:NULL forKeys:NULL count:0];
  3839. if (self) {
  3840. if (dictionary) {
  3841. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3842. }
  3843. }
  3844. return self;
  3845. }
  3846. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3847. #pragma unused(numItems)
  3848. return [self initWithValues:NULL forKeys:NULL count:0];
  3849. }
  3850. - (void)dealloc {
  3851. NSAssert(!_autocreator,
  3852. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3853. [self class], _autocreator);
  3854. [_dictionary release];
  3855. [super dealloc];
  3856. }
  3857. - (instancetype)copyWithZone:(NSZone *)zone {
  3858. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3859. }
  3860. - (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
  3861. if (self == other) {
  3862. return YES;
  3863. }
  3864. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3865. return NO;
  3866. }
  3867. return [_dictionary isEqual:other->_dictionary];
  3868. }
  3869. - (NSUInteger)hash {
  3870. return _dictionary.count;
  3871. }
  3872. - (NSString *)description {
  3873. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3874. }
  3875. - (NSUInteger)count {
  3876. return _dictionary.count;
  3877. }
  3878. - (void)enumerateKeysAndValuesUsingBlock:
  3879. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3880. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3881. NSNumber *aValue,
  3882. BOOL *stop) {
  3883. block([aKey intValue], [aValue boolValue], stop);
  3884. }];
  3885. }
  3886. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3887. NSUInteger count = _dictionary.count;
  3888. if (count == 0) {
  3889. return 0;
  3890. }
  3891. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3892. GPBDataType keyDataType = field.mapKeyDataType;
  3893. __block size_t result = 0;
  3894. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3895. NSNumber *aValue,
  3896. BOOL *stop) {
  3897. #pragma unused(stop)
  3898. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3899. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3900. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3901. }];
  3902. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3903. result += tagSize * count;
  3904. return result;
  3905. }
  3906. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3907. asField:(GPBFieldDescriptor *)field {
  3908. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3909. GPBDataType keyDataType = field.mapKeyDataType;
  3910. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3911. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3912. NSNumber *aValue,
  3913. BOOL *stop) {
  3914. #pragma unused(stop)
  3915. // Write the tag.
  3916. [outputStream writeInt32NoTag:tag];
  3917. // Write the size of the message.
  3918. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3919. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3920. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3921. // Write the fields.
  3922. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3923. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  3924. }];
  3925. }
  3926. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3927. forGPBGenericValueKey:(GPBGenericValue *)key {
  3928. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3929. }
  3930. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3931. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3932. #pragma unused(stop)
  3933. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3934. }];
  3935. }
  3936. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  3937. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3938. if (wrapped && value) {
  3939. *value = [wrapped boolValue];
  3940. }
  3941. return (wrapped != NULL);
  3942. }
  3943. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3944. if (otherDictionary) {
  3945. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3946. if (_autocreator) {
  3947. GPBAutocreatedDictionaryModified(_autocreator, self);
  3948. }
  3949. }
  3950. }
  3951. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  3952. [_dictionary setObject:@(value) forKey:@(key)];
  3953. if (_autocreator) {
  3954. GPBAutocreatedDictionaryModified(_autocreator, self);
  3955. }
  3956. }
  3957. - (void)removeValueForKey:(int32_t)aKey {
  3958. [_dictionary removeObjectForKey:@(aKey)];
  3959. }
  3960. - (void)removeAll {
  3961. [_dictionary removeAllObjects];
  3962. }
  3963. @end
  3964. #pragma mark - Int32 -> Float
  3965. @implementation GPBInt32FloatDictionary {
  3966. @package
  3967. NSMutableDictionary *_dictionary;
  3968. }
  3969. + (instancetype)dictionary {
  3970. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3971. }
  3972. + (instancetype)dictionaryWithValue:(float)value
  3973. forKey:(int32_t)key {
  3974. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3975. // on to get the type correct.
  3976. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  3977. forKeys:&key
  3978. count:1] autorelease];
  3979. }
  3980. + (instancetype)dictionaryWithValues:(const float [])values
  3981. forKeys:(const int32_t [])keys
  3982. count:(NSUInteger)count {
  3983. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3984. // on to get the type correct.
  3985. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  3986. forKeys:keys
  3987. count:count] autorelease];
  3988. }
  3989. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3990. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3991. // on to get the type correct.
  3992. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3993. }
  3994. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3995. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3996. }
  3997. - (instancetype)init {
  3998. return [self initWithValues:NULL forKeys:NULL count:0];
  3999. }
  4000. - (instancetype)initWithValues:(const float [])values
  4001. forKeys:(const int32_t [])keys
  4002. count:(NSUInteger)count {
  4003. self = [super init];
  4004. if (self) {
  4005. _dictionary = [[NSMutableDictionary alloc] init];
  4006. if (count && values && keys) {
  4007. for (NSUInteger i = 0; i < count; ++i) {
  4008. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4009. }
  4010. }
  4011. }
  4012. return self;
  4013. }
  4014. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4015. self = [self initWithValues:NULL forKeys:NULL count:0];
  4016. if (self) {
  4017. if (dictionary) {
  4018. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4019. }
  4020. }
  4021. return self;
  4022. }
  4023. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4024. #pragma unused(numItems)
  4025. return [self initWithValues:NULL forKeys:NULL count:0];
  4026. }
  4027. - (void)dealloc {
  4028. NSAssert(!_autocreator,
  4029. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4030. [self class], _autocreator);
  4031. [_dictionary release];
  4032. [super dealloc];
  4033. }
  4034. - (instancetype)copyWithZone:(NSZone *)zone {
  4035. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4036. }
  4037. - (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
  4038. if (self == other) {
  4039. return YES;
  4040. }
  4041. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4042. return NO;
  4043. }
  4044. return [_dictionary isEqual:other->_dictionary];
  4045. }
  4046. - (NSUInteger)hash {
  4047. return _dictionary.count;
  4048. }
  4049. - (NSString *)description {
  4050. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4051. }
  4052. - (NSUInteger)count {
  4053. return _dictionary.count;
  4054. }
  4055. - (void)enumerateKeysAndValuesUsingBlock:
  4056. (void (^)(int32_t key, float value, BOOL *stop))block {
  4057. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4058. NSNumber *aValue,
  4059. BOOL *stop) {
  4060. block([aKey intValue], [aValue floatValue], stop);
  4061. }];
  4062. }
  4063. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4064. NSUInteger count = _dictionary.count;
  4065. if (count == 0) {
  4066. return 0;
  4067. }
  4068. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4069. GPBDataType keyDataType = field.mapKeyDataType;
  4070. __block size_t result = 0;
  4071. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4072. NSNumber *aValue,
  4073. BOOL *stop) {
  4074. #pragma unused(stop)
  4075. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4076. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4077. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4078. }];
  4079. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4080. result += tagSize * count;
  4081. return result;
  4082. }
  4083. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4084. asField:(GPBFieldDescriptor *)field {
  4085. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4086. GPBDataType keyDataType = field.mapKeyDataType;
  4087. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4088. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4089. NSNumber *aValue,
  4090. BOOL *stop) {
  4091. #pragma unused(stop)
  4092. // Write the tag.
  4093. [outputStream writeInt32NoTag:tag];
  4094. // Write the size of the message.
  4095. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4096. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4097. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4098. // Write the fields.
  4099. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4100. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4101. }];
  4102. }
  4103. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4104. forGPBGenericValueKey:(GPBGenericValue *)key {
  4105. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4106. }
  4107. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4108. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4109. #pragma unused(stop)
  4110. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4111. }];
  4112. }
  4113. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  4114. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4115. if (wrapped && value) {
  4116. *value = [wrapped floatValue];
  4117. }
  4118. return (wrapped != NULL);
  4119. }
  4120. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4121. if (otherDictionary) {
  4122. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4123. if (_autocreator) {
  4124. GPBAutocreatedDictionaryModified(_autocreator, self);
  4125. }
  4126. }
  4127. }
  4128. - (void)setValue:(float)value forKey:(int32_t)key {
  4129. [_dictionary setObject:@(value) forKey:@(key)];
  4130. if (_autocreator) {
  4131. GPBAutocreatedDictionaryModified(_autocreator, self);
  4132. }
  4133. }
  4134. - (void)removeValueForKey:(int32_t)aKey {
  4135. [_dictionary removeObjectForKey:@(aKey)];
  4136. }
  4137. - (void)removeAll {
  4138. [_dictionary removeAllObjects];
  4139. }
  4140. @end
  4141. #pragma mark - Int32 -> Double
  4142. @implementation GPBInt32DoubleDictionary {
  4143. @package
  4144. NSMutableDictionary *_dictionary;
  4145. }
  4146. + (instancetype)dictionary {
  4147. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4148. }
  4149. + (instancetype)dictionaryWithValue:(double)value
  4150. forKey:(int32_t)key {
  4151. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4152. // on to get the type correct.
  4153. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  4154. forKeys:&key
  4155. count:1] autorelease];
  4156. }
  4157. + (instancetype)dictionaryWithValues:(const double [])values
  4158. forKeys:(const int32_t [])keys
  4159. count:(NSUInteger)count {
  4160. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4161. // on to get the type correct.
  4162. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  4163. forKeys:keys
  4164. count:count] autorelease];
  4165. }
  4166. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4167. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4168. // on to get the type correct.
  4169. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4170. }
  4171. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4172. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4173. }
  4174. - (instancetype)init {
  4175. return [self initWithValues:NULL forKeys:NULL count:0];
  4176. }
  4177. - (instancetype)initWithValues:(const double [])values
  4178. forKeys:(const int32_t [])keys
  4179. count:(NSUInteger)count {
  4180. self = [super init];
  4181. if (self) {
  4182. _dictionary = [[NSMutableDictionary alloc] init];
  4183. if (count && values && keys) {
  4184. for (NSUInteger i = 0; i < count; ++i) {
  4185. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4186. }
  4187. }
  4188. }
  4189. return self;
  4190. }
  4191. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4192. self = [self initWithValues:NULL forKeys:NULL count:0];
  4193. if (self) {
  4194. if (dictionary) {
  4195. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4196. }
  4197. }
  4198. return self;
  4199. }
  4200. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4201. #pragma unused(numItems)
  4202. return [self initWithValues:NULL forKeys:NULL count:0];
  4203. }
  4204. - (void)dealloc {
  4205. NSAssert(!_autocreator,
  4206. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4207. [self class], _autocreator);
  4208. [_dictionary release];
  4209. [super dealloc];
  4210. }
  4211. - (instancetype)copyWithZone:(NSZone *)zone {
  4212. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4213. }
  4214. - (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
  4215. if (self == other) {
  4216. return YES;
  4217. }
  4218. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4219. return NO;
  4220. }
  4221. return [_dictionary isEqual:other->_dictionary];
  4222. }
  4223. - (NSUInteger)hash {
  4224. return _dictionary.count;
  4225. }
  4226. - (NSString *)description {
  4227. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4228. }
  4229. - (NSUInteger)count {
  4230. return _dictionary.count;
  4231. }
  4232. - (void)enumerateKeysAndValuesUsingBlock:
  4233. (void (^)(int32_t key, double value, BOOL *stop))block {
  4234. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4235. NSNumber *aValue,
  4236. BOOL *stop) {
  4237. block([aKey intValue], [aValue doubleValue], stop);
  4238. }];
  4239. }
  4240. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4241. NSUInteger count = _dictionary.count;
  4242. if (count == 0) {
  4243. return 0;
  4244. }
  4245. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4246. GPBDataType keyDataType = field.mapKeyDataType;
  4247. __block size_t result = 0;
  4248. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4249. NSNumber *aValue,
  4250. BOOL *stop) {
  4251. #pragma unused(stop)
  4252. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4253. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4254. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4255. }];
  4256. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4257. result += tagSize * count;
  4258. return result;
  4259. }
  4260. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4261. asField:(GPBFieldDescriptor *)field {
  4262. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4263. GPBDataType keyDataType = field.mapKeyDataType;
  4264. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4265. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4266. NSNumber *aValue,
  4267. BOOL *stop) {
  4268. #pragma unused(stop)
  4269. // Write the tag.
  4270. [outputStream writeInt32NoTag:tag];
  4271. // Write the size of the message.
  4272. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4273. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4274. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4275. // Write the fields.
  4276. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4277. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4278. }];
  4279. }
  4280. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4281. forGPBGenericValueKey:(GPBGenericValue *)key {
  4282. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4283. }
  4284. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4285. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4286. #pragma unused(stop)
  4287. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4288. }];
  4289. }
  4290. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4291. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4292. if (wrapped && value) {
  4293. *value = [wrapped doubleValue];
  4294. }
  4295. return (wrapped != NULL);
  4296. }
  4297. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4298. if (otherDictionary) {
  4299. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4300. if (_autocreator) {
  4301. GPBAutocreatedDictionaryModified(_autocreator, self);
  4302. }
  4303. }
  4304. }
  4305. - (void)setValue:(double)value forKey:(int32_t)key {
  4306. [_dictionary setObject:@(value) forKey:@(key)];
  4307. if (_autocreator) {
  4308. GPBAutocreatedDictionaryModified(_autocreator, self);
  4309. }
  4310. }
  4311. - (void)removeValueForKey:(int32_t)aKey {
  4312. [_dictionary removeObjectForKey:@(aKey)];
  4313. }
  4314. - (void)removeAll {
  4315. [_dictionary removeAllObjects];
  4316. }
  4317. @end
  4318. #pragma mark - Int32 -> Enum
  4319. @implementation GPBInt32EnumDictionary {
  4320. @package
  4321. NSMutableDictionary *_dictionary;
  4322. GPBEnumValidationFunc _validationFunc;
  4323. }
  4324. @synthesize validationFunc = _validationFunc;
  4325. + (instancetype)dictionary {
  4326. return [[[self alloc] initWithValidationFunction:NULL
  4327. rawValues:NULL
  4328. forKeys:NULL
  4329. count:0] autorelease];
  4330. }
  4331. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4332. return [[[self alloc] initWithValidationFunction:func
  4333. rawValues:NULL
  4334. forKeys:NULL
  4335. count:0] autorelease];
  4336. }
  4337. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4338. rawValue:(int32_t)rawValue
  4339. forKey:(int32_t)key {
  4340. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4341. // on to get the type correct.
  4342. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4343. rawValues:&rawValue
  4344. forKeys:&key
  4345. count:1] autorelease];
  4346. }
  4347. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4348. rawValues:(const int32_t [])rawValues
  4349. forKeys:(const int32_t [])keys
  4350. count:(NSUInteger)count {
  4351. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4352. // on to get the type correct.
  4353. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4354. rawValues:rawValues
  4355. forKeys:keys
  4356. count:count] autorelease];
  4357. }
  4358. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4359. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4360. // on to get the type correct.
  4361. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4362. }
  4363. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4364. capacity:(NSUInteger)numItems {
  4365. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4366. }
  4367. - (instancetype)init {
  4368. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4369. }
  4370. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4371. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4372. }
  4373. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4374. rawValues:(const int32_t [])rawValues
  4375. forKeys:(const int32_t [])keys
  4376. count:(NSUInteger)count {
  4377. self = [super init];
  4378. if (self) {
  4379. _dictionary = [[NSMutableDictionary alloc] init];
  4380. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4381. if (count && rawValues && keys) {
  4382. for (NSUInteger i = 0; i < count; ++i) {
  4383. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4384. }
  4385. }
  4386. }
  4387. return self;
  4388. }
  4389. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4390. self = [self initWithValidationFunction:dictionary.validationFunc
  4391. rawValues:NULL
  4392. forKeys:NULL
  4393. count:0];
  4394. if (self) {
  4395. if (dictionary) {
  4396. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4397. }
  4398. }
  4399. return self;
  4400. }
  4401. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4402. capacity:(NSUInteger)numItems {
  4403. #pragma unused(numItems)
  4404. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4405. }
  4406. - (void)dealloc {
  4407. NSAssert(!_autocreator,
  4408. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4409. [self class], _autocreator);
  4410. [_dictionary release];
  4411. [super dealloc];
  4412. }
  4413. - (instancetype)copyWithZone:(NSZone *)zone {
  4414. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4415. }
  4416. - (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
  4417. if (self == other) {
  4418. return YES;
  4419. }
  4420. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4421. return NO;
  4422. }
  4423. return [_dictionary isEqual:other->_dictionary];
  4424. }
  4425. - (NSUInteger)hash {
  4426. return _dictionary.count;
  4427. }
  4428. - (NSString *)description {
  4429. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4430. }
  4431. - (NSUInteger)count {
  4432. return _dictionary.count;
  4433. }
  4434. - (void)enumerateKeysAndRawValuesUsingBlock:
  4435. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4436. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4437. NSNumber *aValue,
  4438. BOOL *stop) {
  4439. block([aKey intValue], [aValue intValue], stop);
  4440. }];
  4441. }
  4442. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4443. NSUInteger count = _dictionary.count;
  4444. if (count == 0) {
  4445. return 0;
  4446. }
  4447. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4448. GPBDataType keyDataType = field.mapKeyDataType;
  4449. __block size_t result = 0;
  4450. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4451. NSNumber *aValue,
  4452. BOOL *stop) {
  4453. #pragma unused(stop)
  4454. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4455. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4456. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4457. }];
  4458. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4459. result += tagSize * count;
  4460. return result;
  4461. }
  4462. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4463. asField:(GPBFieldDescriptor *)field {
  4464. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4465. GPBDataType keyDataType = field.mapKeyDataType;
  4466. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4467. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4468. NSNumber *aValue,
  4469. BOOL *stop) {
  4470. #pragma unused(stop)
  4471. // Write the tag.
  4472. [outputStream writeInt32NoTag:tag];
  4473. // Write the size of the message.
  4474. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4475. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4476. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4477. // Write the fields.
  4478. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4479. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4480. }];
  4481. }
  4482. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4483. forKey:(GPBGenericValue *)key
  4484. keyDataType:(GPBDataType)keyDataType {
  4485. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4486. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4487. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4488. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4489. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4490. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4491. [outputStream release];
  4492. return data;
  4493. }
  4494. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4495. forGPBGenericValueKey:(GPBGenericValue *)key {
  4496. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4497. }
  4498. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4499. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4500. #pragma unused(stop)
  4501. block([NSString stringWithFormat:@"%d", key], @(value));
  4502. }];
  4503. }
  4504. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4505. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4506. if (wrapped && value) {
  4507. int32_t result = [wrapped intValue];
  4508. if (!_validationFunc(result)) {
  4509. result = kGPBUnrecognizedEnumeratorValue;
  4510. }
  4511. *value = result;
  4512. }
  4513. return (wrapped != NULL);
  4514. }
  4515. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4516. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4517. if (wrapped && rawValue) {
  4518. *rawValue = [wrapped intValue];
  4519. }
  4520. return (wrapped != NULL);
  4521. }
  4522. - (void)enumerateKeysAndValuesUsingBlock:
  4523. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4524. GPBEnumValidationFunc func = _validationFunc;
  4525. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4526. NSNumber *aValue,
  4527. BOOL *stop) {
  4528. int32_t unwrapped = [aValue intValue];
  4529. if (!func(unwrapped)) {
  4530. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4531. }
  4532. block([aKey intValue], unwrapped, stop);
  4533. }];
  4534. }
  4535. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4536. if (otherDictionary) {
  4537. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4538. if (_autocreator) {
  4539. GPBAutocreatedDictionaryModified(_autocreator, self);
  4540. }
  4541. }
  4542. }
  4543. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4544. [_dictionary setObject:@(value) forKey:@(key)];
  4545. if (_autocreator) {
  4546. GPBAutocreatedDictionaryModified(_autocreator, self);
  4547. }
  4548. }
  4549. - (void)removeValueForKey:(int32_t)aKey {
  4550. [_dictionary removeObjectForKey:@(aKey)];
  4551. }
  4552. - (void)removeAll {
  4553. [_dictionary removeAllObjects];
  4554. }
  4555. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4556. if (!_validationFunc(value)) {
  4557. [NSException raise:NSInvalidArgumentException
  4558. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4559. value];
  4560. }
  4561. [_dictionary setObject:@(value) forKey:@(key)];
  4562. if (_autocreator) {
  4563. GPBAutocreatedDictionaryModified(_autocreator, self);
  4564. }
  4565. }
  4566. @end
  4567. #pragma mark - Int32 -> Object
  4568. @implementation GPBInt32ObjectDictionary {
  4569. @package
  4570. NSMutableDictionary *_dictionary;
  4571. }
  4572. + (instancetype)dictionary {
  4573. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4574. }
  4575. + (instancetype)dictionaryWithValue:(id)value
  4576. forKey:(int32_t)key {
  4577. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4578. // on to get the type correct.
  4579. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithValues:&value
  4580. forKeys:&key
  4581. count:1] autorelease];
  4582. }
  4583. + (instancetype)dictionaryWithValues:(const id [])values
  4584. forKeys:(const int32_t [])keys
  4585. count:(NSUInteger)count {
  4586. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4587. // on to get the type correct.
  4588. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithValues:values
  4589. forKeys:keys
  4590. count:count] autorelease];
  4591. }
  4592. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4593. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4594. // on to get the type correct.
  4595. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4596. }
  4597. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4598. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4599. }
  4600. - (instancetype)init {
  4601. return [self initWithValues:NULL forKeys:NULL count:0];
  4602. }
  4603. - (instancetype)initWithValues:(const id [])values
  4604. forKeys:(const int32_t [])keys
  4605. count:(NSUInteger)count {
  4606. self = [super init];
  4607. if (self) {
  4608. _dictionary = [[NSMutableDictionary alloc] init];
  4609. if (count && values && keys) {
  4610. for (NSUInteger i = 0; i < count; ++i) {
  4611. [_dictionary setObject:values[i] forKey:@(keys[i])];
  4612. }
  4613. }
  4614. }
  4615. return self;
  4616. }
  4617. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4618. self = [self initWithValues:NULL forKeys:NULL count:0];
  4619. if (self) {
  4620. if (dictionary) {
  4621. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4622. }
  4623. }
  4624. return self;
  4625. }
  4626. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4627. #pragma unused(numItems)
  4628. return [self initWithValues:NULL forKeys:NULL count:0];
  4629. }
  4630. - (void)dealloc {
  4631. NSAssert(!_autocreator,
  4632. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4633. [self class], _autocreator);
  4634. [_dictionary release];
  4635. [super dealloc];
  4636. }
  4637. - (instancetype)copyWithZone:(NSZone *)zone {
  4638. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4639. }
  4640. - (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
  4641. if (self == other) {
  4642. return YES;
  4643. }
  4644. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4645. return NO;
  4646. }
  4647. return [_dictionary isEqual:other->_dictionary];
  4648. }
  4649. - (NSUInteger)hash {
  4650. return _dictionary.count;
  4651. }
  4652. - (NSString *)description {
  4653. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4654. }
  4655. - (NSUInteger)count {
  4656. return _dictionary.count;
  4657. }
  4658. - (void)enumerateKeysAndValuesUsingBlock:
  4659. (void (^)(int32_t key, id value, BOOL *stop))block {
  4660. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4661. id aValue,
  4662. BOOL *stop) {
  4663. block([aKey intValue], aValue, stop);
  4664. }];
  4665. }
  4666. - (BOOL)isInitialized {
  4667. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4668. if (!msg.initialized) {
  4669. return NO;
  4670. }
  4671. }
  4672. return YES;
  4673. }
  4674. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4675. GPBInt32ObjectDictionary *newDict =
  4676. [[GPBInt32ObjectDictionary alloc] init];
  4677. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4678. GPBMessage *msg,
  4679. BOOL *stop) {
  4680. #pragma unused(stop)
  4681. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4682. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4683. [copiedMsg release];
  4684. }];
  4685. return newDict;
  4686. }
  4687. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4688. NSUInteger count = _dictionary.count;
  4689. if (count == 0) {
  4690. return 0;
  4691. }
  4692. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4693. GPBDataType keyDataType = field.mapKeyDataType;
  4694. __block size_t result = 0;
  4695. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4696. id aValue,
  4697. BOOL *stop) {
  4698. #pragma unused(stop)
  4699. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4700. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  4701. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4702. }];
  4703. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4704. result += tagSize * count;
  4705. return result;
  4706. }
  4707. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4708. asField:(GPBFieldDescriptor *)field {
  4709. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4710. GPBDataType keyDataType = field.mapKeyDataType;
  4711. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4712. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4713. id aValue,
  4714. BOOL *stop) {
  4715. #pragma unused(stop)
  4716. // Write the tag.
  4717. [outputStream writeInt32NoTag:tag];
  4718. // Write the size of the message.
  4719. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4720. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  4721. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4722. // Write the fields.
  4723. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4724. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueDataType);
  4725. }];
  4726. }
  4727. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4728. forGPBGenericValueKey:(GPBGenericValue *)key {
  4729. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4730. }
  4731. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4732. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, id value, BOOL *stop) {
  4733. #pragma unused(stop)
  4734. block([NSString stringWithFormat:@"%d", key], value);
  4735. }];
  4736. }
  4737. - (id)valueForKey:(int32_t)key {
  4738. id result = [_dictionary objectForKey:@(key)];
  4739. return result;
  4740. }
  4741. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4742. if (otherDictionary) {
  4743. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4744. if (_autocreator) {
  4745. GPBAutocreatedDictionaryModified(_autocreator, self);
  4746. }
  4747. }
  4748. }
  4749. - (void)setValue:(id)value forKey:(int32_t)key {
  4750. [_dictionary setObject:value forKey:@(key)];
  4751. if (_autocreator) {
  4752. GPBAutocreatedDictionaryModified(_autocreator, self);
  4753. }
  4754. }
  4755. - (void)removeValueForKey:(int32_t)aKey {
  4756. [_dictionary removeObjectForKey:@(aKey)];
  4757. }
  4758. - (void)removeAll {
  4759. [_dictionary removeAllObjects];
  4760. }
  4761. @end
  4762. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4763. // This block of code is generated, do not edit it directly.
  4764. #pragma mark - UInt64 -> UInt32
  4765. @implementation GPBUInt64UInt32Dictionary {
  4766. @package
  4767. NSMutableDictionary *_dictionary;
  4768. }
  4769. + (instancetype)dictionary {
  4770. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4771. }
  4772. + (instancetype)dictionaryWithValue:(uint32_t)value
  4773. forKey:(uint64_t)key {
  4774. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4775. // on to get the type correct.
  4776. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4777. forKeys:&key
  4778. count:1] autorelease];
  4779. }
  4780. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4781. forKeys:(const uint64_t [])keys
  4782. count:(NSUInteger)count {
  4783. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4784. // on to get the type correct.
  4785. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4786. forKeys:keys
  4787. count:count] autorelease];
  4788. }
  4789. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4790. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4791. // on to get the type correct.
  4792. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4793. }
  4794. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4795. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4796. }
  4797. - (instancetype)init {
  4798. return [self initWithValues:NULL forKeys:NULL count:0];
  4799. }
  4800. - (instancetype)initWithValues:(const uint32_t [])values
  4801. forKeys:(const uint64_t [])keys
  4802. count:(NSUInteger)count {
  4803. self = [super init];
  4804. if (self) {
  4805. _dictionary = [[NSMutableDictionary alloc] init];
  4806. if (count && values && keys) {
  4807. for (NSUInteger i = 0; i < count; ++i) {
  4808. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4809. }
  4810. }
  4811. }
  4812. return self;
  4813. }
  4814. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4815. self = [self initWithValues:NULL forKeys:NULL count:0];
  4816. if (self) {
  4817. if (dictionary) {
  4818. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4819. }
  4820. }
  4821. return self;
  4822. }
  4823. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4824. #pragma unused(numItems)
  4825. return [self initWithValues:NULL forKeys:NULL count:0];
  4826. }
  4827. - (void)dealloc {
  4828. NSAssert(!_autocreator,
  4829. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4830. [self class], _autocreator);
  4831. [_dictionary release];
  4832. [super dealloc];
  4833. }
  4834. - (instancetype)copyWithZone:(NSZone *)zone {
  4835. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4836. }
  4837. - (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
  4838. if (self == other) {
  4839. return YES;
  4840. }
  4841. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4842. return NO;
  4843. }
  4844. return [_dictionary isEqual:other->_dictionary];
  4845. }
  4846. - (NSUInteger)hash {
  4847. return _dictionary.count;
  4848. }
  4849. - (NSString *)description {
  4850. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4851. }
  4852. - (NSUInteger)count {
  4853. return _dictionary.count;
  4854. }
  4855. - (void)enumerateKeysAndValuesUsingBlock:
  4856. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4857. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4858. NSNumber *aValue,
  4859. BOOL *stop) {
  4860. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4861. }];
  4862. }
  4863. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4864. NSUInteger count = _dictionary.count;
  4865. if (count == 0) {
  4866. return 0;
  4867. }
  4868. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4869. GPBDataType keyDataType = field.mapKeyDataType;
  4870. __block size_t result = 0;
  4871. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4872. NSNumber *aValue,
  4873. BOOL *stop) {
  4874. #pragma unused(stop)
  4875. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4876. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4877. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4878. }];
  4879. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4880. result += tagSize * count;
  4881. return result;
  4882. }
  4883. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4884. asField:(GPBFieldDescriptor *)field {
  4885. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4886. GPBDataType keyDataType = field.mapKeyDataType;
  4887. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4888. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4889. NSNumber *aValue,
  4890. BOOL *stop) {
  4891. #pragma unused(stop)
  4892. // Write the tag.
  4893. [outputStream writeInt32NoTag:tag];
  4894. // Write the size of the message.
  4895. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4896. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4897. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4898. // Write the fields.
  4899. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4900. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4901. }];
  4902. }
  4903. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4904. forGPBGenericValueKey:(GPBGenericValue *)key {
  4905. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4906. }
  4907. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4908. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4909. #pragma unused(stop)
  4910. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4911. }];
  4912. }
  4913. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  4914. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4915. if (wrapped && value) {
  4916. *value = [wrapped unsignedIntValue];
  4917. }
  4918. return (wrapped != NULL);
  4919. }
  4920. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4921. if (otherDictionary) {
  4922. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4923. if (_autocreator) {
  4924. GPBAutocreatedDictionaryModified(_autocreator, self);
  4925. }
  4926. }
  4927. }
  4928. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  4929. [_dictionary setObject:@(value) forKey:@(key)];
  4930. if (_autocreator) {
  4931. GPBAutocreatedDictionaryModified(_autocreator, self);
  4932. }
  4933. }
  4934. - (void)removeValueForKey:(uint64_t)aKey {
  4935. [_dictionary removeObjectForKey:@(aKey)];
  4936. }
  4937. - (void)removeAll {
  4938. [_dictionary removeAllObjects];
  4939. }
  4940. @end
  4941. #pragma mark - UInt64 -> Int32
  4942. @implementation GPBUInt64Int32Dictionary {
  4943. @package
  4944. NSMutableDictionary *_dictionary;
  4945. }
  4946. + (instancetype)dictionary {
  4947. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4948. }
  4949. + (instancetype)dictionaryWithValue:(int32_t)value
  4950. forKey:(uint64_t)key {
  4951. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4952. // on to get the type correct.
  4953. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  4954. forKeys:&key
  4955. count:1] autorelease];
  4956. }
  4957. + (instancetype)dictionaryWithValues:(const int32_t [])values
  4958. forKeys:(const uint64_t [])keys
  4959. count:(NSUInteger)count {
  4960. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4961. // on to get the type correct.
  4962. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  4963. forKeys:keys
  4964. count:count] autorelease];
  4965. }
  4966. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4967. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4968. // on to get the type correct.
  4969. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4970. }
  4971. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4972. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4973. }
  4974. - (instancetype)init {
  4975. return [self initWithValues:NULL forKeys:NULL count:0];
  4976. }
  4977. - (instancetype)initWithValues:(const int32_t [])values
  4978. forKeys:(const uint64_t [])keys
  4979. count:(NSUInteger)count {
  4980. self = [super init];
  4981. if (self) {
  4982. _dictionary = [[NSMutableDictionary alloc] init];
  4983. if (count && values && keys) {
  4984. for (NSUInteger i = 0; i < count; ++i) {
  4985. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4986. }
  4987. }
  4988. }
  4989. return self;
  4990. }
  4991. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4992. self = [self initWithValues:NULL forKeys:NULL count:0];
  4993. if (self) {
  4994. if (dictionary) {
  4995. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4996. }
  4997. }
  4998. return self;
  4999. }
  5000. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5001. #pragma unused(numItems)
  5002. return [self initWithValues:NULL forKeys:NULL count:0];
  5003. }
  5004. - (void)dealloc {
  5005. NSAssert(!_autocreator,
  5006. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5007. [self class], _autocreator);
  5008. [_dictionary release];
  5009. [super dealloc];
  5010. }
  5011. - (instancetype)copyWithZone:(NSZone *)zone {
  5012. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5013. }
  5014. - (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
  5015. if (self == other) {
  5016. return YES;
  5017. }
  5018. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5019. return NO;
  5020. }
  5021. return [_dictionary isEqual:other->_dictionary];
  5022. }
  5023. - (NSUInteger)hash {
  5024. return _dictionary.count;
  5025. }
  5026. - (NSString *)description {
  5027. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5028. }
  5029. - (NSUInteger)count {
  5030. return _dictionary.count;
  5031. }
  5032. - (void)enumerateKeysAndValuesUsingBlock:
  5033. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5034. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5035. NSNumber *aValue,
  5036. BOOL *stop) {
  5037. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5038. }];
  5039. }
  5040. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5041. NSUInteger count = _dictionary.count;
  5042. if (count == 0) {
  5043. return 0;
  5044. }
  5045. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5046. GPBDataType keyDataType = field.mapKeyDataType;
  5047. __block size_t result = 0;
  5048. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5049. NSNumber *aValue,
  5050. BOOL *stop) {
  5051. #pragma unused(stop)
  5052. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5053. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5054. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5055. }];
  5056. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5057. result += tagSize * count;
  5058. return result;
  5059. }
  5060. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5061. asField:(GPBFieldDescriptor *)field {
  5062. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5063. GPBDataType keyDataType = field.mapKeyDataType;
  5064. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5065. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5066. NSNumber *aValue,
  5067. BOOL *stop) {
  5068. #pragma unused(stop)
  5069. // Write the tag.
  5070. [outputStream writeInt32NoTag:tag];
  5071. // Write the size of the message.
  5072. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5073. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5074. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5075. // Write the fields.
  5076. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5077. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5078. }];
  5079. }
  5080. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5081. forGPBGenericValueKey:(GPBGenericValue *)key {
  5082. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5083. }
  5084. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5085. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5086. #pragma unused(stop)
  5087. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5088. }];
  5089. }
  5090. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5091. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5092. if (wrapped && value) {
  5093. *value = [wrapped intValue];
  5094. }
  5095. return (wrapped != NULL);
  5096. }
  5097. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5098. if (otherDictionary) {
  5099. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5100. if (_autocreator) {
  5101. GPBAutocreatedDictionaryModified(_autocreator, self);
  5102. }
  5103. }
  5104. }
  5105. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5106. [_dictionary setObject:@(value) forKey:@(key)];
  5107. if (_autocreator) {
  5108. GPBAutocreatedDictionaryModified(_autocreator, self);
  5109. }
  5110. }
  5111. - (void)removeValueForKey:(uint64_t)aKey {
  5112. [_dictionary removeObjectForKey:@(aKey)];
  5113. }
  5114. - (void)removeAll {
  5115. [_dictionary removeAllObjects];
  5116. }
  5117. @end
  5118. #pragma mark - UInt64 -> UInt64
  5119. @implementation GPBUInt64UInt64Dictionary {
  5120. @package
  5121. NSMutableDictionary *_dictionary;
  5122. }
  5123. + (instancetype)dictionary {
  5124. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5125. }
  5126. + (instancetype)dictionaryWithValue:(uint64_t)value
  5127. forKey:(uint64_t)key {
  5128. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5129. // on to get the type correct.
  5130. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  5131. forKeys:&key
  5132. count:1] autorelease];
  5133. }
  5134. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  5135. forKeys:(const uint64_t [])keys
  5136. count:(NSUInteger)count {
  5137. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5138. // on to get the type correct.
  5139. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  5140. forKeys:keys
  5141. count:count] autorelease];
  5142. }
  5143. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5144. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5145. // on to get the type correct.
  5146. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5147. }
  5148. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5149. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5150. }
  5151. - (instancetype)init {
  5152. return [self initWithValues:NULL forKeys:NULL count:0];
  5153. }
  5154. - (instancetype)initWithValues:(const uint64_t [])values
  5155. forKeys:(const uint64_t [])keys
  5156. count:(NSUInteger)count {
  5157. self = [super init];
  5158. if (self) {
  5159. _dictionary = [[NSMutableDictionary alloc] init];
  5160. if (count && values && keys) {
  5161. for (NSUInteger i = 0; i < count; ++i) {
  5162. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5163. }
  5164. }
  5165. }
  5166. return self;
  5167. }
  5168. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5169. self = [self initWithValues:NULL forKeys:NULL count:0];
  5170. if (self) {
  5171. if (dictionary) {
  5172. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5173. }
  5174. }
  5175. return self;
  5176. }
  5177. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5178. #pragma unused(numItems)
  5179. return [self initWithValues:NULL forKeys:NULL count:0];
  5180. }
  5181. - (void)dealloc {
  5182. NSAssert(!_autocreator,
  5183. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5184. [self class], _autocreator);
  5185. [_dictionary release];
  5186. [super dealloc];
  5187. }
  5188. - (instancetype)copyWithZone:(NSZone *)zone {
  5189. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5190. }
  5191. - (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
  5192. if (self == other) {
  5193. return YES;
  5194. }
  5195. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5196. return NO;
  5197. }
  5198. return [_dictionary isEqual:other->_dictionary];
  5199. }
  5200. - (NSUInteger)hash {
  5201. return _dictionary.count;
  5202. }
  5203. - (NSString *)description {
  5204. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5205. }
  5206. - (NSUInteger)count {
  5207. return _dictionary.count;
  5208. }
  5209. - (void)enumerateKeysAndValuesUsingBlock:
  5210. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5211. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5212. NSNumber *aValue,
  5213. BOOL *stop) {
  5214. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5215. }];
  5216. }
  5217. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5218. NSUInteger count = _dictionary.count;
  5219. if (count == 0) {
  5220. return 0;
  5221. }
  5222. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5223. GPBDataType keyDataType = field.mapKeyDataType;
  5224. __block size_t result = 0;
  5225. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5226. NSNumber *aValue,
  5227. BOOL *stop) {
  5228. #pragma unused(stop)
  5229. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5230. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5231. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5232. }];
  5233. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5234. result += tagSize * count;
  5235. return result;
  5236. }
  5237. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5238. asField:(GPBFieldDescriptor *)field {
  5239. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5240. GPBDataType keyDataType = field.mapKeyDataType;
  5241. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5242. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5243. NSNumber *aValue,
  5244. BOOL *stop) {
  5245. #pragma unused(stop)
  5246. // Write the tag.
  5247. [outputStream writeInt32NoTag:tag];
  5248. // Write the size of the message.
  5249. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5250. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5251. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5252. // Write the fields.
  5253. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5254. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5255. }];
  5256. }
  5257. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5258. forGPBGenericValueKey:(GPBGenericValue *)key {
  5259. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5260. }
  5261. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5262. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5263. #pragma unused(stop)
  5264. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5265. }];
  5266. }
  5267. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  5268. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5269. if (wrapped && value) {
  5270. *value = [wrapped unsignedLongLongValue];
  5271. }
  5272. return (wrapped != NULL);
  5273. }
  5274. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5275. if (otherDictionary) {
  5276. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5277. if (_autocreator) {
  5278. GPBAutocreatedDictionaryModified(_autocreator, self);
  5279. }
  5280. }
  5281. }
  5282. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5283. [_dictionary setObject:@(value) forKey:@(key)];
  5284. if (_autocreator) {
  5285. GPBAutocreatedDictionaryModified(_autocreator, self);
  5286. }
  5287. }
  5288. - (void)removeValueForKey:(uint64_t)aKey {
  5289. [_dictionary removeObjectForKey:@(aKey)];
  5290. }
  5291. - (void)removeAll {
  5292. [_dictionary removeAllObjects];
  5293. }
  5294. @end
  5295. #pragma mark - UInt64 -> Int64
  5296. @implementation GPBUInt64Int64Dictionary {
  5297. @package
  5298. NSMutableDictionary *_dictionary;
  5299. }
  5300. + (instancetype)dictionary {
  5301. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5302. }
  5303. + (instancetype)dictionaryWithValue:(int64_t)value
  5304. forKey:(uint64_t)key {
  5305. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5306. // on to get the type correct.
  5307. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5308. forKeys:&key
  5309. count:1] autorelease];
  5310. }
  5311. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5312. forKeys:(const uint64_t [])keys
  5313. count:(NSUInteger)count {
  5314. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5315. // on to get the type correct.
  5316. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5317. forKeys:keys
  5318. count:count] autorelease];
  5319. }
  5320. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5321. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5322. // on to get the type correct.
  5323. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5324. }
  5325. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5326. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5327. }
  5328. - (instancetype)init {
  5329. return [self initWithValues:NULL forKeys:NULL count:0];
  5330. }
  5331. - (instancetype)initWithValues:(const int64_t [])values
  5332. forKeys:(const uint64_t [])keys
  5333. count:(NSUInteger)count {
  5334. self = [super init];
  5335. if (self) {
  5336. _dictionary = [[NSMutableDictionary alloc] init];
  5337. if (count && values && keys) {
  5338. for (NSUInteger i = 0; i < count; ++i) {
  5339. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5340. }
  5341. }
  5342. }
  5343. return self;
  5344. }
  5345. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5346. self = [self initWithValues:NULL forKeys:NULL count:0];
  5347. if (self) {
  5348. if (dictionary) {
  5349. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5350. }
  5351. }
  5352. return self;
  5353. }
  5354. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5355. #pragma unused(numItems)
  5356. return [self initWithValues:NULL forKeys:NULL count:0];
  5357. }
  5358. - (void)dealloc {
  5359. NSAssert(!_autocreator,
  5360. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5361. [self class], _autocreator);
  5362. [_dictionary release];
  5363. [super dealloc];
  5364. }
  5365. - (instancetype)copyWithZone:(NSZone *)zone {
  5366. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5367. }
  5368. - (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
  5369. if (self == other) {
  5370. return YES;
  5371. }
  5372. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5373. return NO;
  5374. }
  5375. return [_dictionary isEqual:other->_dictionary];
  5376. }
  5377. - (NSUInteger)hash {
  5378. return _dictionary.count;
  5379. }
  5380. - (NSString *)description {
  5381. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5382. }
  5383. - (NSUInteger)count {
  5384. return _dictionary.count;
  5385. }
  5386. - (void)enumerateKeysAndValuesUsingBlock:
  5387. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5388. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5389. NSNumber *aValue,
  5390. BOOL *stop) {
  5391. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5392. }];
  5393. }
  5394. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5395. NSUInteger count = _dictionary.count;
  5396. if (count == 0) {
  5397. return 0;
  5398. }
  5399. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5400. GPBDataType keyDataType = field.mapKeyDataType;
  5401. __block size_t result = 0;
  5402. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5403. NSNumber *aValue,
  5404. BOOL *stop) {
  5405. #pragma unused(stop)
  5406. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5407. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5408. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5409. }];
  5410. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5411. result += tagSize * count;
  5412. return result;
  5413. }
  5414. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5415. asField:(GPBFieldDescriptor *)field {
  5416. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5417. GPBDataType keyDataType = field.mapKeyDataType;
  5418. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5419. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5420. NSNumber *aValue,
  5421. BOOL *stop) {
  5422. #pragma unused(stop)
  5423. // Write the tag.
  5424. [outputStream writeInt32NoTag:tag];
  5425. // Write the size of the message.
  5426. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5427. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5428. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5429. // Write the fields.
  5430. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5431. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5432. }];
  5433. }
  5434. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5435. forGPBGenericValueKey:(GPBGenericValue *)key {
  5436. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5437. }
  5438. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5439. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5440. #pragma unused(stop)
  5441. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5442. }];
  5443. }
  5444. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5445. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5446. if (wrapped && value) {
  5447. *value = [wrapped longLongValue];
  5448. }
  5449. return (wrapped != NULL);
  5450. }
  5451. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5452. if (otherDictionary) {
  5453. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5454. if (_autocreator) {
  5455. GPBAutocreatedDictionaryModified(_autocreator, self);
  5456. }
  5457. }
  5458. }
  5459. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5460. [_dictionary setObject:@(value) forKey:@(key)];
  5461. if (_autocreator) {
  5462. GPBAutocreatedDictionaryModified(_autocreator, self);
  5463. }
  5464. }
  5465. - (void)removeValueForKey:(uint64_t)aKey {
  5466. [_dictionary removeObjectForKey:@(aKey)];
  5467. }
  5468. - (void)removeAll {
  5469. [_dictionary removeAllObjects];
  5470. }
  5471. @end
  5472. #pragma mark - UInt64 -> Bool
  5473. @implementation GPBUInt64BoolDictionary {
  5474. @package
  5475. NSMutableDictionary *_dictionary;
  5476. }
  5477. + (instancetype)dictionary {
  5478. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5479. }
  5480. + (instancetype)dictionaryWithValue:(BOOL)value
  5481. forKey:(uint64_t)key {
  5482. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5483. // on to get the type correct.
  5484. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5485. forKeys:&key
  5486. count:1] autorelease];
  5487. }
  5488. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5489. forKeys:(const uint64_t [])keys
  5490. count:(NSUInteger)count {
  5491. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5492. // on to get the type correct.
  5493. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5494. forKeys:keys
  5495. count:count] autorelease];
  5496. }
  5497. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5498. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5499. // on to get the type correct.
  5500. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5501. }
  5502. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5503. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5504. }
  5505. - (instancetype)init {
  5506. return [self initWithValues:NULL forKeys:NULL count:0];
  5507. }
  5508. - (instancetype)initWithValues:(const BOOL [])values
  5509. forKeys:(const uint64_t [])keys
  5510. count:(NSUInteger)count {
  5511. self = [super init];
  5512. if (self) {
  5513. _dictionary = [[NSMutableDictionary alloc] init];
  5514. if (count && values && keys) {
  5515. for (NSUInteger i = 0; i < count; ++i) {
  5516. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5517. }
  5518. }
  5519. }
  5520. return self;
  5521. }
  5522. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5523. self = [self initWithValues:NULL forKeys:NULL count:0];
  5524. if (self) {
  5525. if (dictionary) {
  5526. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5527. }
  5528. }
  5529. return self;
  5530. }
  5531. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5532. #pragma unused(numItems)
  5533. return [self initWithValues:NULL forKeys:NULL count:0];
  5534. }
  5535. - (void)dealloc {
  5536. NSAssert(!_autocreator,
  5537. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5538. [self class], _autocreator);
  5539. [_dictionary release];
  5540. [super dealloc];
  5541. }
  5542. - (instancetype)copyWithZone:(NSZone *)zone {
  5543. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5544. }
  5545. - (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
  5546. if (self == other) {
  5547. return YES;
  5548. }
  5549. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5550. return NO;
  5551. }
  5552. return [_dictionary isEqual:other->_dictionary];
  5553. }
  5554. - (NSUInteger)hash {
  5555. return _dictionary.count;
  5556. }
  5557. - (NSString *)description {
  5558. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5559. }
  5560. - (NSUInteger)count {
  5561. return _dictionary.count;
  5562. }
  5563. - (void)enumerateKeysAndValuesUsingBlock:
  5564. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5565. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5566. NSNumber *aValue,
  5567. BOOL *stop) {
  5568. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5569. }];
  5570. }
  5571. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5572. NSUInteger count = _dictionary.count;
  5573. if (count == 0) {
  5574. return 0;
  5575. }
  5576. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5577. GPBDataType keyDataType = field.mapKeyDataType;
  5578. __block size_t result = 0;
  5579. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5580. NSNumber *aValue,
  5581. BOOL *stop) {
  5582. #pragma unused(stop)
  5583. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5584. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5585. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5586. }];
  5587. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5588. result += tagSize * count;
  5589. return result;
  5590. }
  5591. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5592. asField:(GPBFieldDescriptor *)field {
  5593. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5594. GPBDataType keyDataType = field.mapKeyDataType;
  5595. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5596. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5597. NSNumber *aValue,
  5598. BOOL *stop) {
  5599. #pragma unused(stop)
  5600. // Write the tag.
  5601. [outputStream writeInt32NoTag:tag];
  5602. // Write the size of the message.
  5603. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5604. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5605. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5606. // Write the fields.
  5607. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5608. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5609. }];
  5610. }
  5611. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5612. forGPBGenericValueKey:(GPBGenericValue *)key {
  5613. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5614. }
  5615. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5616. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5617. #pragma unused(stop)
  5618. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5619. }];
  5620. }
  5621. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5622. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5623. if (wrapped && value) {
  5624. *value = [wrapped boolValue];
  5625. }
  5626. return (wrapped != NULL);
  5627. }
  5628. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5629. if (otherDictionary) {
  5630. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5631. if (_autocreator) {
  5632. GPBAutocreatedDictionaryModified(_autocreator, self);
  5633. }
  5634. }
  5635. }
  5636. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5637. [_dictionary setObject:@(value) forKey:@(key)];
  5638. if (_autocreator) {
  5639. GPBAutocreatedDictionaryModified(_autocreator, self);
  5640. }
  5641. }
  5642. - (void)removeValueForKey:(uint64_t)aKey {
  5643. [_dictionary removeObjectForKey:@(aKey)];
  5644. }
  5645. - (void)removeAll {
  5646. [_dictionary removeAllObjects];
  5647. }
  5648. @end
  5649. #pragma mark - UInt64 -> Float
  5650. @implementation GPBUInt64FloatDictionary {
  5651. @package
  5652. NSMutableDictionary *_dictionary;
  5653. }
  5654. + (instancetype)dictionary {
  5655. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5656. }
  5657. + (instancetype)dictionaryWithValue:(float)value
  5658. forKey:(uint64_t)key {
  5659. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5660. // on to get the type correct.
  5661. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5662. forKeys:&key
  5663. count:1] autorelease];
  5664. }
  5665. + (instancetype)dictionaryWithValues:(const float [])values
  5666. forKeys:(const uint64_t [])keys
  5667. count:(NSUInteger)count {
  5668. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5669. // on to get the type correct.
  5670. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5671. forKeys:keys
  5672. count:count] autorelease];
  5673. }
  5674. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5675. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5676. // on to get the type correct.
  5677. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5678. }
  5679. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5680. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5681. }
  5682. - (instancetype)init {
  5683. return [self initWithValues:NULL forKeys:NULL count:0];
  5684. }
  5685. - (instancetype)initWithValues:(const float [])values
  5686. forKeys:(const uint64_t [])keys
  5687. count:(NSUInteger)count {
  5688. self = [super init];
  5689. if (self) {
  5690. _dictionary = [[NSMutableDictionary alloc] init];
  5691. if (count && values && keys) {
  5692. for (NSUInteger i = 0; i < count; ++i) {
  5693. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5694. }
  5695. }
  5696. }
  5697. return self;
  5698. }
  5699. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5700. self = [self initWithValues:NULL forKeys:NULL count:0];
  5701. if (self) {
  5702. if (dictionary) {
  5703. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5704. }
  5705. }
  5706. return self;
  5707. }
  5708. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5709. #pragma unused(numItems)
  5710. return [self initWithValues:NULL forKeys:NULL count:0];
  5711. }
  5712. - (void)dealloc {
  5713. NSAssert(!_autocreator,
  5714. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5715. [self class], _autocreator);
  5716. [_dictionary release];
  5717. [super dealloc];
  5718. }
  5719. - (instancetype)copyWithZone:(NSZone *)zone {
  5720. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5721. }
  5722. - (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
  5723. if (self == other) {
  5724. return YES;
  5725. }
  5726. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5727. return NO;
  5728. }
  5729. return [_dictionary isEqual:other->_dictionary];
  5730. }
  5731. - (NSUInteger)hash {
  5732. return _dictionary.count;
  5733. }
  5734. - (NSString *)description {
  5735. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5736. }
  5737. - (NSUInteger)count {
  5738. return _dictionary.count;
  5739. }
  5740. - (void)enumerateKeysAndValuesUsingBlock:
  5741. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5742. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5743. NSNumber *aValue,
  5744. BOOL *stop) {
  5745. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5746. }];
  5747. }
  5748. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5749. NSUInteger count = _dictionary.count;
  5750. if (count == 0) {
  5751. return 0;
  5752. }
  5753. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5754. GPBDataType keyDataType = field.mapKeyDataType;
  5755. __block size_t result = 0;
  5756. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5757. NSNumber *aValue,
  5758. BOOL *stop) {
  5759. #pragma unused(stop)
  5760. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5761. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5762. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5763. }];
  5764. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5765. result += tagSize * count;
  5766. return result;
  5767. }
  5768. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5769. asField:(GPBFieldDescriptor *)field {
  5770. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5771. GPBDataType keyDataType = field.mapKeyDataType;
  5772. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5773. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5774. NSNumber *aValue,
  5775. BOOL *stop) {
  5776. #pragma unused(stop)
  5777. // Write the tag.
  5778. [outputStream writeInt32NoTag:tag];
  5779. // Write the size of the message.
  5780. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5781. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5782. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5783. // Write the fields.
  5784. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5785. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5786. }];
  5787. }
  5788. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5789. forGPBGenericValueKey:(GPBGenericValue *)key {
  5790. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5791. }
  5792. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5793. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5794. #pragma unused(stop)
  5795. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5796. }];
  5797. }
  5798. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5799. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5800. if (wrapped && value) {
  5801. *value = [wrapped floatValue];
  5802. }
  5803. return (wrapped != NULL);
  5804. }
  5805. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5806. if (otherDictionary) {
  5807. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5808. if (_autocreator) {
  5809. GPBAutocreatedDictionaryModified(_autocreator, self);
  5810. }
  5811. }
  5812. }
  5813. - (void)setValue:(float)value forKey:(uint64_t)key {
  5814. [_dictionary setObject:@(value) forKey:@(key)];
  5815. if (_autocreator) {
  5816. GPBAutocreatedDictionaryModified(_autocreator, self);
  5817. }
  5818. }
  5819. - (void)removeValueForKey:(uint64_t)aKey {
  5820. [_dictionary removeObjectForKey:@(aKey)];
  5821. }
  5822. - (void)removeAll {
  5823. [_dictionary removeAllObjects];
  5824. }
  5825. @end
  5826. #pragma mark - UInt64 -> Double
  5827. @implementation GPBUInt64DoubleDictionary {
  5828. @package
  5829. NSMutableDictionary *_dictionary;
  5830. }
  5831. + (instancetype)dictionary {
  5832. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5833. }
  5834. + (instancetype)dictionaryWithValue:(double)value
  5835. forKey:(uint64_t)key {
  5836. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5837. // on to get the type correct.
  5838. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5839. forKeys:&key
  5840. count:1] autorelease];
  5841. }
  5842. + (instancetype)dictionaryWithValues:(const double [])values
  5843. forKeys:(const uint64_t [])keys
  5844. count:(NSUInteger)count {
  5845. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5846. // on to get the type correct.
  5847. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5848. forKeys:keys
  5849. count:count] autorelease];
  5850. }
  5851. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5852. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5853. // on to get the type correct.
  5854. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5855. }
  5856. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5857. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5858. }
  5859. - (instancetype)init {
  5860. return [self initWithValues:NULL forKeys:NULL count:0];
  5861. }
  5862. - (instancetype)initWithValues:(const double [])values
  5863. forKeys:(const uint64_t [])keys
  5864. count:(NSUInteger)count {
  5865. self = [super init];
  5866. if (self) {
  5867. _dictionary = [[NSMutableDictionary alloc] init];
  5868. if (count && values && keys) {
  5869. for (NSUInteger i = 0; i < count; ++i) {
  5870. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5871. }
  5872. }
  5873. }
  5874. return self;
  5875. }
  5876. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5877. self = [self initWithValues:NULL forKeys:NULL count:0];
  5878. if (self) {
  5879. if (dictionary) {
  5880. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5881. }
  5882. }
  5883. return self;
  5884. }
  5885. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5886. #pragma unused(numItems)
  5887. return [self initWithValues:NULL forKeys:NULL count:0];
  5888. }
  5889. - (void)dealloc {
  5890. NSAssert(!_autocreator,
  5891. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5892. [self class], _autocreator);
  5893. [_dictionary release];
  5894. [super dealloc];
  5895. }
  5896. - (instancetype)copyWithZone:(NSZone *)zone {
  5897. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5898. }
  5899. - (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
  5900. if (self == other) {
  5901. return YES;
  5902. }
  5903. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5904. return NO;
  5905. }
  5906. return [_dictionary isEqual:other->_dictionary];
  5907. }
  5908. - (NSUInteger)hash {
  5909. return _dictionary.count;
  5910. }
  5911. - (NSString *)description {
  5912. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5913. }
  5914. - (NSUInteger)count {
  5915. return _dictionary.count;
  5916. }
  5917. - (void)enumerateKeysAndValuesUsingBlock:
  5918. (void (^)(uint64_t key, double value, BOOL *stop))block {
  5919. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5920. NSNumber *aValue,
  5921. BOOL *stop) {
  5922. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  5923. }];
  5924. }
  5925. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5926. NSUInteger count = _dictionary.count;
  5927. if (count == 0) {
  5928. return 0;
  5929. }
  5930. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5931. GPBDataType keyDataType = field.mapKeyDataType;
  5932. __block size_t result = 0;
  5933. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5934. NSNumber *aValue,
  5935. BOOL *stop) {
  5936. #pragma unused(stop)
  5937. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5938. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5939. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5940. }];
  5941. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5942. result += tagSize * count;
  5943. return result;
  5944. }
  5945. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5946. asField:(GPBFieldDescriptor *)field {
  5947. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5948. GPBDataType keyDataType = field.mapKeyDataType;
  5949. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5950. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5951. NSNumber *aValue,
  5952. BOOL *stop) {
  5953. #pragma unused(stop)
  5954. // Write the tag.
  5955. [outputStream writeInt32NoTag:tag];
  5956. // Write the size of the message.
  5957. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5958. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5959. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5960. // Write the fields.
  5961. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5962. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5963. }];
  5964. }
  5965. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5966. forGPBGenericValueKey:(GPBGenericValue *)key {
  5967. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5968. }
  5969. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5970. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  5971. #pragma unused(stop)
  5972. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5973. }];
  5974. }
  5975. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  5976. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5977. if (wrapped && value) {
  5978. *value = [wrapped doubleValue];
  5979. }
  5980. return (wrapped != NULL);
  5981. }
  5982. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5983. if (otherDictionary) {
  5984. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5985. if (_autocreator) {
  5986. GPBAutocreatedDictionaryModified(_autocreator, self);
  5987. }
  5988. }
  5989. }
  5990. - (void)setValue:(double)value forKey:(uint64_t)key {
  5991. [_dictionary setObject:@(value) forKey:@(key)];
  5992. if (_autocreator) {
  5993. GPBAutocreatedDictionaryModified(_autocreator, self);
  5994. }
  5995. }
  5996. - (void)removeValueForKey:(uint64_t)aKey {
  5997. [_dictionary removeObjectForKey:@(aKey)];
  5998. }
  5999. - (void)removeAll {
  6000. [_dictionary removeAllObjects];
  6001. }
  6002. @end
  6003. #pragma mark - UInt64 -> Enum
  6004. @implementation GPBUInt64EnumDictionary {
  6005. @package
  6006. NSMutableDictionary *_dictionary;
  6007. GPBEnumValidationFunc _validationFunc;
  6008. }
  6009. @synthesize validationFunc = _validationFunc;
  6010. + (instancetype)dictionary {
  6011. return [[[self alloc] initWithValidationFunction:NULL
  6012. rawValues:NULL
  6013. forKeys:NULL
  6014. count:0] autorelease];
  6015. }
  6016. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6017. return [[[self alloc] initWithValidationFunction:func
  6018. rawValues:NULL
  6019. forKeys:NULL
  6020. count:0] autorelease];
  6021. }
  6022. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6023. rawValue:(int32_t)rawValue
  6024. forKey:(uint64_t)key {
  6025. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6026. // on to get the type correct.
  6027. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6028. rawValues:&rawValue
  6029. forKeys:&key
  6030. count:1] autorelease];
  6031. }
  6032. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6033. rawValues:(const int32_t [])rawValues
  6034. forKeys:(const uint64_t [])keys
  6035. count:(NSUInteger)count {
  6036. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6037. // on to get the type correct.
  6038. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6039. rawValues:rawValues
  6040. forKeys:keys
  6041. count:count] autorelease];
  6042. }
  6043. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6044. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6045. // on to get the type correct.
  6046. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6047. }
  6048. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6049. capacity:(NSUInteger)numItems {
  6050. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6051. }
  6052. - (instancetype)init {
  6053. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6054. }
  6055. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6056. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6057. }
  6058. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6059. rawValues:(const int32_t [])rawValues
  6060. forKeys:(const uint64_t [])keys
  6061. count:(NSUInteger)count {
  6062. self = [super init];
  6063. if (self) {
  6064. _dictionary = [[NSMutableDictionary alloc] init];
  6065. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6066. if (count && rawValues && keys) {
  6067. for (NSUInteger i = 0; i < count; ++i) {
  6068. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6069. }
  6070. }
  6071. }
  6072. return self;
  6073. }
  6074. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6075. self = [self initWithValidationFunction:dictionary.validationFunc
  6076. rawValues:NULL
  6077. forKeys:NULL
  6078. count:0];
  6079. if (self) {
  6080. if (dictionary) {
  6081. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6082. }
  6083. }
  6084. return self;
  6085. }
  6086. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6087. capacity:(NSUInteger)numItems {
  6088. #pragma unused(numItems)
  6089. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6090. }
  6091. - (void)dealloc {
  6092. NSAssert(!_autocreator,
  6093. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6094. [self class], _autocreator);
  6095. [_dictionary release];
  6096. [super dealloc];
  6097. }
  6098. - (instancetype)copyWithZone:(NSZone *)zone {
  6099. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6100. }
  6101. - (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
  6102. if (self == other) {
  6103. return YES;
  6104. }
  6105. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6106. return NO;
  6107. }
  6108. return [_dictionary isEqual:other->_dictionary];
  6109. }
  6110. - (NSUInteger)hash {
  6111. return _dictionary.count;
  6112. }
  6113. - (NSString *)description {
  6114. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6115. }
  6116. - (NSUInteger)count {
  6117. return _dictionary.count;
  6118. }
  6119. - (void)enumerateKeysAndRawValuesUsingBlock:
  6120. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6121. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6122. NSNumber *aValue,
  6123. BOOL *stop) {
  6124. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6125. }];
  6126. }
  6127. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6128. NSUInteger count = _dictionary.count;
  6129. if (count == 0) {
  6130. return 0;
  6131. }
  6132. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6133. GPBDataType keyDataType = field.mapKeyDataType;
  6134. __block size_t result = 0;
  6135. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6136. NSNumber *aValue,
  6137. BOOL *stop) {
  6138. #pragma unused(stop)
  6139. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6140. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6141. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6142. }];
  6143. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6144. result += tagSize * count;
  6145. return result;
  6146. }
  6147. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6148. asField:(GPBFieldDescriptor *)field {
  6149. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6150. GPBDataType keyDataType = field.mapKeyDataType;
  6151. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6152. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6153. NSNumber *aValue,
  6154. BOOL *stop) {
  6155. #pragma unused(stop)
  6156. // Write the tag.
  6157. [outputStream writeInt32NoTag:tag];
  6158. // Write the size of the message.
  6159. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6160. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6161. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6162. // Write the fields.
  6163. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6164. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6165. }];
  6166. }
  6167. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6168. forKey:(GPBGenericValue *)key
  6169. keyDataType:(GPBDataType)keyDataType {
  6170. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6171. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6172. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6173. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6174. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6175. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6176. [outputStream release];
  6177. return data;
  6178. }
  6179. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6180. forGPBGenericValueKey:(GPBGenericValue *)key {
  6181. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6182. }
  6183. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6184. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6185. #pragma unused(stop)
  6186. block([NSString stringWithFormat:@"%llu", key], @(value));
  6187. }];
  6188. }
  6189. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  6190. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6191. if (wrapped && value) {
  6192. int32_t result = [wrapped intValue];
  6193. if (!_validationFunc(result)) {
  6194. result = kGPBUnrecognizedEnumeratorValue;
  6195. }
  6196. *value = result;
  6197. }
  6198. return (wrapped != NULL);
  6199. }
  6200. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  6201. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6202. if (wrapped && rawValue) {
  6203. *rawValue = [wrapped intValue];
  6204. }
  6205. return (wrapped != NULL);
  6206. }
  6207. - (void)enumerateKeysAndValuesUsingBlock:
  6208. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6209. GPBEnumValidationFunc func = _validationFunc;
  6210. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6211. NSNumber *aValue,
  6212. BOOL *stop) {
  6213. int32_t unwrapped = [aValue intValue];
  6214. if (!func(unwrapped)) {
  6215. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6216. }
  6217. block([aKey unsignedLongLongValue], unwrapped, stop);
  6218. }];
  6219. }
  6220. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6221. if (otherDictionary) {
  6222. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6223. if (_autocreator) {
  6224. GPBAutocreatedDictionaryModified(_autocreator, self);
  6225. }
  6226. }
  6227. }
  6228. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6229. [_dictionary setObject:@(value) forKey:@(key)];
  6230. if (_autocreator) {
  6231. GPBAutocreatedDictionaryModified(_autocreator, self);
  6232. }
  6233. }
  6234. - (void)removeValueForKey:(uint64_t)aKey {
  6235. [_dictionary removeObjectForKey:@(aKey)];
  6236. }
  6237. - (void)removeAll {
  6238. [_dictionary removeAllObjects];
  6239. }
  6240. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  6241. if (!_validationFunc(value)) {
  6242. [NSException raise:NSInvalidArgumentException
  6243. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6244. value];
  6245. }
  6246. [_dictionary setObject:@(value) forKey:@(key)];
  6247. if (_autocreator) {
  6248. GPBAutocreatedDictionaryModified(_autocreator, self);
  6249. }
  6250. }
  6251. @end
  6252. #pragma mark - UInt64 -> Object
  6253. @implementation GPBUInt64ObjectDictionary {
  6254. @package
  6255. NSMutableDictionary *_dictionary;
  6256. }
  6257. + (instancetype)dictionary {
  6258. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6259. }
  6260. + (instancetype)dictionaryWithValue:(id)value
  6261. forKey:(uint64_t)key {
  6262. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6263. // on to get the type correct.
  6264. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithValues:&value
  6265. forKeys:&key
  6266. count:1] autorelease];
  6267. }
  6268. + (instancetype)dictionaryWithValues:(const id [])values
  6269. forKeys:(const uint64_t [])keys
  6270. count:(NSUInteger)count {
  6271. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6272. // on to get the type correct.
  6273. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithValues:values
  6274. forKeys:keys
  6275. count:count] autorelease];
  6276. }
  6277. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6278. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6279. // on to get the type correct.
  6280. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6281. }
  6282. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6283. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6284. }
  6285. - (instancetype)init {
  6286. return [self initWithValues:NULL forKeys:NULL count:0];
  6287. }
  6288. - (instancetype)initWithValues:(const id [])values
  6289. forKeys:(const uint64_t [])keys
  6290. count:(NSUInteger)count {
  6291. self = [super init];
  6292. if (self) {
  6293. _dictionary = [[NSMutableDictionary alloc] init];
  6294. if (count && values && keys) {
  6295. for (NSUInteger i = 0; i < count; ++i) {
  6296. [_dictionary setObject:values[i] forKey:@(keys[i])];
  6297. }
  6298. }
  6299. }
  6300. return self;
  6301. }
  6302. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6303. self = [self initWithValues:NULL forKeys:NULL count:0];
  6304. if (self) {
  6305. if (dictionary) {
  6306. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6307. }
  6308. }
  6309. return self;
  6310. }
  6311. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6312. #pragma unused(numItems)
  6313. return [self initWithValues:NULL forKeys:NULL count:0];
  6314. }
  6315. - (void)dealloc {
  6316. NSAssert(!_autocreator,
  6317. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6318. [self class], _autocreator);
  6319. [_dictionary release];
  6320. [super dealloc];
  6321. }
  6322. - (instancetype)copyWithZone:(NSZone *)zone {
  6323. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6324. }
  6325. - (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
  6326. if (self == other) {
  6327. return YES;
  6328. }
  6329. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6330. return NO;
  6331. }
  6332. return [_dictionary isEqual:other->_dictionary];
  6333. }
  6334. - (NSUInteger)hash {
  6335. return _dictionary.count;
  6336. }
  6337. - (NSString *)description {
  6338. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6339. }
  6340. - (NSUInteger)count {
  6341. return _dictionary.count;
  6342. }
  6343. - (void)enumerateKeysAndValuesUsingBlock:
  6344. (void (^)(uint64_t key, id value, BOOL *stop))block {
  6345. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6346. id aValue,
  6347. BOOL *stop) {
  6348. block([aKey unsignedLongLongValue], aValue, stop);
  6349. }];
  6350. }
  6351. - (BOOL)isInitialized {
  6352. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6353. if (!msg.initialized) {
  6354. return NO;
  6355. }
  6356. }
  6357. return YES;
  6358. }
  6359. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6360. GPBUInt64ObjectDictionary *newDict =
  6361. [[GPBUInt64ObjectDictionary alloc] init];
  6362. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6363. GPBMessage *msg,
  6364. BOOL *stop) {
  6365. #pragma unused(stop)
  6366. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6367. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6368. [copiedMsg release];
  6369. }];
  6370. return newDict;
  6371. }
  6372. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6373. NSUInteger count = _dictionary.count;
  6374. if (count == 0) {
  6375. return 0;
  6376. }
  6377. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6378. GPBDataType keyDataType = field.mapKeyDataType;
  6379. __block size_t result = 0;
  6380. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6381. id aValue,
  6382. BOOL *stop) {
  6383. #pragma unused(stop)
  6384. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6385. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  6386. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6387. }];
  6388. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6389. result += tagSize * count;
  6390. return result;
  6391. }
  6392. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6393. asField:(GPBFieldDescriptor *)field {
  6394. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6395. GPBDataType keyDataType = field.mapKeyDataType;
  6396. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6397. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6398. id aValue,
  6399. BOOL *stop) {
  6400. #pragma unused(stop)
  6401. // Write the tag.
  6402. [outputStream writeInt32NoTag:tag];
  6403. // Write the size of the message.
  6404. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6405. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  6406. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6407. // Write the fields.
  6408. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6409. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueDataType);
  6410. }];
  6411. }
  6412. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6413. forGPBGenericValueKey:(GPBGenericValue *)key {
  6414. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6415. }
  6416. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6417. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, id value, BOOL *stop) {
  6418. #pragma unused(stop)
  6419. block([NSString stringWithFormat:@"%llu", key], value);
  6420. }];
  6421. }
  6422. - (id)valueForKey:(uint64_t)key {
  6423. id result = [_dictionary objectForKey:@(key)];
  6424. return result;
  6425. }
  6426. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6427. if (otherDictionary) {
  6428. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6429. if (_autocreator) {
  6430. GPBAutocreatedDictionaryModified(_autocreator, self);
  6431. }
  6432. }
  6433. }
  6434. - (void)setValue:(id)value forKey:(uint64_t)key {
  6435. [_dictionary setObject:value forKey:@(key)];
  6436. if (_autocreator) {
  6437. GPBAutocreatedDictionaryModified(_autocreator, self);
  6438. }
  6439. }
  6440. - (void)removeValueForKey:(uint64_t)aKey {
  6441. [_dictionary removeObjectForKey:@(aKey)];
  6442. }
  6443. - (void)removeAll {
  6444. [_dictionary removeAllObjects];
  6445. }
  6446. @end
  6447. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6448. // This block of code is generated, do not edit it directly.
  6449. #pragma mark - Int64 -> UInt32
  6450. @implementation GPBInt64UInt32Dictionary {
  6451. @package
  6452. NSMutableDictionary *_dictionary;
  6453. }
  6454. + (instancetype)dictionary {
  6455. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6456. }
  6457. + (instancetype)dictionaryWithValue:(uint32_t)value
  6458. forKey:(int64_t)key {
  6459. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6460. // on to get the type correct.
  6461. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6462. forKeys:&key
  6463. count:1] autorelease];
  6464. }
  6465. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6466. forKeys:(const int64_t [])keys
  6467. count:(NSUInteger)count {
  6468. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6469. // on to get the type correct.
  6470. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6471. forKeys:keys
  6472. count:count] autorelease];
  6473. }
  6474. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6475. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6476. // on to get the type correct.
  6477. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6478. }
  6479. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6480. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6481. }
  6482. - (instancetype)init {
  6483. return [self initWithValues:NULL forKeys:NULL count:0];
  6484. }
  6485. - (instancetype)initWithValues:(const uint32_t [])values
  6486. forKeys:(const int64_t [])keys
  6487. count:(NSUInteger)count {
  6488. self = [super init];
  6489. if (self) {
  6490. _dictionary = [[NSMutableDictionary alloc] init];
  6491. if (count && values && keys) {
  6492. for (NSUInteger i = 0; i < count; ++i) {
  6493. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6494. }
  6495. }
  6496. }
  6497. return self;
  6498. }
  6499. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6500. self = [self initWithValues:NULL forKeys:NULL count:0];
  6501. if (self) {
  6502. if (dictionary) {
  6503. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6504. }
  6505. }
  6506. return self;
  6507. }
  6508. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6509. #pragma unused(numItems)
  6510. return [self initWithValues:NULL forKeys:NULL count:0];
  6511. }
  6512. - (void)dealloc {
  6513. NSAssert(!_autocreator,
  6514. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6515. [self class], _autocreator);
  6516. [_dictionary release];
  6517. [super dealloc];
  6518. }
  6519. - (instancetype)copyWithZone:(NSZone *)zone {
  6520. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6521. }
  6522. - (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
  6523. if (self == other) {
  6524. return YES;
  6525. }
  6526. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6527. return NO;
  6528. }
  6529. return [_dictionary isEqual:other->_dictionary];
  6530. }
  6531. - (NSUInteger)hash {
  6532. return _dictionary.count;
  6533. }
  6534. - (NSString *)description {
  6535. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6536. }
  6537. - (NSUInteger)count {
  6538. return _dictionary.count;
  6539. }
  6540. - (void)enumerateKeysAndValuesUsingBlock:
  6541. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6542. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6543. NSNumber *aValue,
  6544. BOOL *stop) {
  6545. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6546. }];
  6547. }
  6548. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6549. NSUInteger count = _dictionary.count;
  6550. if (count == 0) {
  6551. return 0;
  6552. }
  6553. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6554. GPBDataType keyDataType = field.mapKeyDataType;
  6555. __block size_t result = 0;
  6556. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6557. NSNumber *aValue,
  6558. BOOL *stop) {
  6559. #pragma unused(stop)
  6560. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6561. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6562. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6563. }];
  6564. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6565. result += tagSize * count;
  6566. return result;
  6567. }
  6568. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6569. asField:(GPBFieldDescriptor *)field {
  6570. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6571. GPBDataType keyDataType = field.mapKeyDataType;
  6572. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6573. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6574. NSNumber *aValue,
  6575. BOOL *stop) {
  6576. #pragma unused(stop)
  6577. // Write the tag.
  6578. [outputStream writeInt32NoTag:tag];
  6579. // Write the size of the message.
  6580. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6581. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6582. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6583. // Write the fields.
  6584. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6585. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6586. }];
  6587. }
  6588. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6589. forGPBGenericValueKey:(GPBGenericValue *)key {
  6590. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6591. }
  6592. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6593. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6594. #pragma unused(stop)
  6595. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6596. }];
  6597. }
  6598. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6599. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6600. if (wrapped && value) {
  6601. *value = [wrapped unsignedIntValue];
  6602. }
  6603. return (wrapped != NULL);
  6604. }
  6605. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6606. if (otherDictionary) {
  6607. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6608. if (_autocreator) {
  6609. GPBAutocreatedDictionaryModified(_autocreator, self);
  6610. }
  6611. }
  6612. }
  6613. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6614. [_dictionary setObject:@(value) forKey:@(key)];
  6615. if (_autocreator) {
  6616. GPBAutocreatedDictionaryModified(_autocreator, self);
  6617. }
  6618. }
  6619. - (void)removeValueForKey:(int64_t)aKey {
  6620. [_dictionary removeObjectForKey:@(aKey)];
  6621. }
  6622. - (void)removeAll {
  6623. [_dictionary removeAllObjects];
  6624. }
  6625. @end
  6626. #pragma mark - Int64 -> Int32
  6627. @implementation GPBInt64Int32Dictionary {
  6628. @package
  6629. NSMutableDictionary *_dictionary;
  6630. }
  6631. + (instancetype)dictionary {
  6632. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6633. }
  6634. + (instancetype)dictionaryWithValue:(int32_t)value
  6635. forKey:(int64_t)key {
  6636. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6637. // on to get the type correct.
  6638. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6639. forKeys:&key
  6640. count:1] autorelease];
  6641. }
  6642. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6643. forKeys:(const int64_t [])keys
  6644. count:(NSUInteger)count {
  6645. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6646. // on to get the type correct.
  6647. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6648. forKeys:keys
  6649. count:count] autorelease];
  6650. }
  6651. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6652. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6653. // on to get the type correct.
  6654. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6655. }
  6656. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6657. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6658. }
  6659. - (instancetype)init {
  6660. return [self initWithValues:NULL forKeys:NULL count:0];
  6661. }
  6662. - (instancetype)initWithValues:(const int32_t [])values
  6663. forKeys:(const int64_t [])keys
  6664. count:(NSUInteger)count {
  6665. self = [super init];
  6666. if (self) {
  6667. _dictionary = [[NSMutableDictionary alloc] init];
  6668. if (count && values && keys) {
  6669. for (NSUInteger i = 0; i < count; ++i) {
  6670. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6671. }
  6672. }
  6673. }
  6674. return self;
  6675. }
  6676. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6677. self = [self initWithValues:NULL forKeys:NULL count:0];
  6678. if (self) {
  6679. if (dictionary) {
  6680. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6681. }
  6682. }
  6683. return self;
  6684. }
  6685. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6686. #pragma unused(numItems)
  6687. return [self initWithValues:NULL forKeys:NULL count:0];
  6688. }
  6689. - (void)dealloc {
  6690. NSAssert(!_autocreator,
  6691. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6692. [self class], _autocreator);
  6693. [_dictionary release];
  6694. [super dealloc];
  6695. }
  6696. - (instancetype)copyWithZone:(NSZone *)zone {
  6697. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6698. }
  6699. - (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
  6700. if (self == other) {
  6701. return YES;
  6702. }
  6703. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6704. return NO;
  6705. }
  6706. return [_dictionary isEqual:other->_dictionary];
  6707. }
  6708. - (NSUInteger)hash {
  6709. return _dictionary.count;
  6710. }
  6711. - (NSString *)description {
  6712. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6713. }
  6714. - (NSUInteger)count {
  6715. return _dictionary.count;
  6716. }
  6717. - (void)enumerateKeysAndValuesUsingBlock:
  6718. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6719. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6720. NSNumber *aValue,
  6721. BOOL *stop) {
  6722. block([aKey longLongValue], [aValue intValue], stop);
  6723. }];
  6724. }
  6725. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6726. NSUInteger count = _dictionary.count;
  6727. if (count == 0) {
  6728. return 0;
  6729. }
  6730. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6731. GPBDataType keyDataType = field.mapKeyDataType;
  6732. __block size_t result = 0;
  6733. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6734. NSNumber *aValue,
  6735. BOOL *stop) {
  6736. #pragma unused(stop)
  6737. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6738. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6739. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6740. }];
  6741. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6742. result += tagSize * count;
  6743. return result;
  6744. }
  6745. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6746. asField:(GPBFieldDescriptor *)field {
  6747. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6748. GPBDataType keyDataType = field.mapKeyDataType;
  6749. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6750. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6751. NSNumber *aValue,
  6752. BOOL *stop) {
  6753. #pragma unused(stop)
  6754. // Write the tag.
  6755. [outputStream writeInt32NoTag:tag];
  6756. // Write the size of the message.
  6757. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6758. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6759. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6760. // Write the fields.
  6761. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6762. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6763. }];
  6764. }
  6765. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6766. forGPBGenericValueKey:(GPBGenericValue *)key {
  6767. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6768. }
  6769. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6770. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6771. #pragma unused(stop)
  6772. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6773. }];
  6774. }
  6775. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6776. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6777. if (wrapped && value) {
  6778. *value = [wrapped intValue];
  6779. }
  6780. return (wrapped != NULL);
  6781. }
  6782. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6783. if (otherDictionary) {
  6784. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6785. if (_autocreator) {
  6786. GPBAutocreatedDictionaryModified(_autocreator, self);
  6787. }
  6788. }
  6789. }
  6790. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6791. [_dictionary setObject:@(value) forKey:@(key)];
  6792. if (_autocreator) {
  6793. GPBAutocreatedDictionaryModified(_autocreator, self);
  6794. }
  6795. }
  6796. - (void)removeValueForKey:(int64_t)aKey {
  6797. [_dictionary removeObjectForKey:@(aKey)];
  6798. }
  6799. - (void)removeAll {
  6800. [_dictionary removeAllObjects];
  6801. }
  6802. @end
  6803. #pragma mark - Int64 -> UInt64
  6804. @implementation GPBInt64UInt64Dictionary {
  6805. @package
  6806. NSMutableDictionary *_dictionary;
  6807. }
  6808. + (instancetype)dictionary {
  6809. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6810. }
  6811. + (instancetype)dictionaryWithValue:(uint64_t)value
  6812. forKey:(int64_t)key {
  6813. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6814. // on to get the type correct.
  6815. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6816. forKeys:&key
  6817. count:1] autorelease];
  6818. }
  6819. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6820. forKeys:(const int64_t [])keys
  6821. count:(NSUInteger)count {
  6822. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6823. // on to get the type correct.
  6824. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6825. forKeys:keys
  6826. count:count] autorelease];
  6827. }
  6828. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6829. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6830. // on to get the type correct.
  6831. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6832. }
  6833. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6834. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6835. }
  6836. - (instancetype)init {
  6837. return [self initWithValues:NULL forKeys:NULL count:0];
  6838. }
  6839. - (instancetype)initWithValues:(const uint64_t [])values
  6840. forKeys:(const int64_t [])keys
  6841. count:(NSUInteger)count {
  6842. self = [super init];
  6843. if (self) {
  6844. _dictionary = [[NSMutableDictionary alloc] init];
  6845. if (count && values && keys) {
  6846. for (NSUInteger i = 0; i < count; ++i) {
  6847. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6848. }
  6849. }
  6850. }
  6851. return self;
  6852. }
  6853. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6854. self = [self initWithValues:NULL forKeys:NULL count:0];
  6855. if (self) {
  6856. if (dictionary) {
  6857. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6858. }
  6859. }
  6860. return self;
  6861. }
  6862. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6863. #pragma unused(numItems)
  6864. return [self initWithValues:NULL forKeys:NULL count:0];
  6865. }
  6866. - (void)dealloc {
  6867. NSAssert(!_autocreator,
  6868. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6869. [self class], _autocreator);
  6870. [_dictionary release];
  6871. [super dealloc];
  6872. }
  6873. - (instancetype)copyWithZone:(NSZone *)zone {
  6874. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6875. }
  6876. - (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
  6877. if (self == other) {
  6878. return YES;
  6879. }
  6880. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6881. return NO;
  6882. }
  6883. return [_dictionary isEqual:other->_dictionary];
  6884. }
  6885. - (NSUInteger)hash {
  6886. return _dictionary.count;
  6887. }
  6888. - (NSString *)description {
  6889. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6890. }
  6891. - (NSUInteger)count {
  6892. return _dictionary.count;
  6893. }
  6894. - (void)enumerateKeysAndValuesUsingBlock:
  6895. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  6896. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6897. NSNumber *aValue,
  6898. BOOL *stop) {
  6899. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  6900. }];
  6901. }
  6902. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6903. NSUInteger count = _dictionary.count;
  6904. if (count == 0) {
  6905. return 0;
  6906. }
  6907. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6908. GPBDataType keyDataType = field.mapKeyDataType;
  6909. __block size_t result = 0;
  6910. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6911. NSNumber *aValue,
  6912. BOOL *stop) {
  6913. #pragma unused(stop)
  6914. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6915. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6916. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6917. }];
  6918. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6919. result += tagSize * count;
  6920. return result;
  6921. }
  6922. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6923. asField:(GPBFieldDescriptor *)field {
  6924. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6925. GPBDataType keyDataType = field.mapKeyDataType;
  6926. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6927. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6928. NSNumber *aValue,
  6929. BOOL *stop) {
  6930. #pragma unused(stop)
  6931. // Write the tag.
  6932. [outputStream writeInt32NoTag:tag];
  6933. // Write the size of the message.
  6934. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6935. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6936. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6937. // Write the fields.
  6938. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6939. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6940. }];
  6941. }
  6942. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6943. forGPBGenericValueKey:(GPBGenericValue *)key {
  6944. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6945. }
  6946. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6947. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  6948. #pragma unused(stop)
  6949. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6950. }];
  6951. }
  6952. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  6953. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6954. if (wrapped && value) {
  6955. *value = [wrapped unsignedLongLongValue];
  6956. }
  6957. return (wrapped != NULL);
  6958. }
  6959. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6960. if (otherDictionary) {
  6961. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6962. if (_autocreator) {
  6963. GPBAutocreatedDictionaryModified(_autocreator, self);
  6964. }
  6965. }
  6966. }
  6967. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  6968. [_dictionary setObject:@(value) forKey:@(key)];
  6969. if (_autocreator) {
  6970. GPBAutocreatedDictionaryModified(_autocreator, self);
  6971. }
  6972. }
  6973. - (void)removeValueForKey:(int64_t)aKey {
  6974. [_dictionary removeObjectForKey:@(aKey)];
  6975. }
  6976. - (void)removeAll {
  6977. [_dictionary removeAllObjects];
  6978. }
  6979. @end
  6980. #pragma mark - Int64 -> Int64
  6981. @implementation GPBInt64Int64Dictionary {
  6982. @package
  6983. NSMutableDictionary *_dictionary;
  6984. }
  6985. + (instancetype)dictionary {
  6986. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6987. }
  6988. + (instancetype)dictionaryWithValue:(int64_t)value
  6989. forKey:(int64_t)key {
  6990. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6991. // on to get the type correct.
  6992. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  6993. forKeys:&key
  6994. count:1] autorelease];
  6995. }
  6996. + (instancetype)dictionaryWithValues:(const int64_t [])values
  6997. forKeys:(const int64_t [])keys
  6998. count:(NSUInteger)count {
  6999. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7000. // on to get the type correct.
  7001. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  7002. forKeys:keys
  7003. count:count] autorelease];
  7004. }
  7005. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7006. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7007. // on to get the type correct.
  7008. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7009. }
  7010. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7011. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7012. }
  7013. - (instancetype)init {
  7014. return [self initWithValues:NULL forKeys:NULL count:0];
  7015. }
  7016. - (instancetype)initWithValues:(const int64_t [])values
  7017. forKeys:(const int64_t [])keys
  7018. count:(NSUInteger)count {
  7019. self = [super init];
  7020. if (self) {
  7021. _dictionary = [[NSMutableDictionary alloc] init];
  7022. if (count && values && keys) {
  7023. for (NSUInteger i = 0; i < count; ++i) {
  7024. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7025. }
  7026. }
  7027. }
  7028. return self;
  7029. }
  7030. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7031. self = [self initWithValues:NULL forKeys:NULL count:0];
  7032. if (self) {
  7033. if (dictionary) {
  7034. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7035. }
  7036. }
  7037. return self;
  7038. }
  7039. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7040. #pragma unused(numItems)
  7041. return [self initWithValues:NULL forKeys:NULL count:0];
  7042. }
  7043. - (void)dealloc {
  7044. NSAssert(!_autocreator,
  7045. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7046. [self class], _autocreator);
  7047. [_dictionary release];
  7048. [super dealloc];
  7049. }
  7050. - (instancetype)copyWithZone:(NSZone *)zone {
  7051. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7052. }
  7053. - (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
  7054. if (self == other) {
  7055. return YES;
  7056. }
  7057. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7058. return NO;
  7059. }
  7060. return [_dictionary isEqual:other->_dictionary];
  7061. }
  7062. - (NSUInteger)hash {
  7063. return _dictionary.count;
  7064. }
  7065. - (NSString *)description {
  7066. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7067. }
  7068. - (NSUInteger)count {
  7069. return _dictionary.count;
  7070. }
  7071. - (void)enumerateKeysAndValuesUsingBlock:
  7072. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7073. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7074. NSNumber *aValue,
  7075. BOOL *stop) {
  7076. block([aKey longLongValue], [aValue longLongValue], stop);
  7077. }];
  7078. }
  7079. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7080. NSUInteger count = _dictionary.count;
  7081. if (count == 0) {
  7082. return 0;
  7083. }
  7084. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7085. GPBDataType keyDataType = field.mapKeyDataType;
  7086. __block size_t result = 0;
  7087. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7088. NSNumber *aValue,
  7089. BOOL *stop) {
  7090. #pragma unused(stop)
  7091. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7092. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7093. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7094. }];
  7095. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7096. result += tagSize * count;
  7097. return result;
  7098. }
  7099. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7100. asField:(GPBFieldDescriptor *)field {
  7101. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7102. GPBDataType keyDataType = field.mapKeyDataType;
  7103. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7104. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7105. NSNumber *aValue,
  7106. BOOL *stop) {
  7107. #pragma unused(stop)
  7108. // Write the tag.
  7109. [outputStream writeInt32NoTag:tag];
  7110. // Write the size of the message.
  7111. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7112. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7113. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7114. // Write the fields.
  7115. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7116. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7117. }];
  7118. }
  7119. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7120. forGPBGenericValueKey:(GPBGenericValue *)key {
  7121. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7122. }
  7123. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7124. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7125. #pragma unused(stop)
  7126. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7127. }];
  7128. }
  7129. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  7130. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7131. if (wrapped && value) {
  7132. *value = [wrapped longLongValue];
  7133. }
  7134. return (wrapped != NULL);
  7135. }
  7136. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7137. if (otherDictionary) {
  7138. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7139. if (_autocreator) {
  7140. GPBAutocreatedDictionaryModified(_autocreator, self);
  7141. }
  7142. }
  7143. }
  7144. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  7145. [_dictionary setObject:@(value) forKey:@(key)];
  7146. if (_autocreator) {
  7147. GPBAutocreatedDictionaryModified(_autocreator, self);
  7148. }
  7149. }
  7150. - (void)removeValueForKey:(int64_t)aKey {
  7151. [_dictionary removeObjectForKey:@(aKey)];
  7152. }
  7153. - (void)removeAll {
  7154. [_dictionary removeAllObjects];
  7155. }
  7156. @end
  7157. #pragma mark - Int64 -> Bool
  7158. @implementation GPBInt64BoolDictionary {
  7159. @package
  7160. NSMutableDictionary *_dictionary;
  7161. }
  7162. + (instancetype)dictionary {
  7163. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7164. }
  7165. + (instancetype)dictionaryWithValue:(BOOL)value
  7166. forKey:(int64_t)key {
  7167. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7168. // on to get the type correct.
  7169. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  7170. forKeys:&key
  7171. count:1] autorelease];
  7172. }
  7173. + (instancetype)dictionaryWithValues:(const BOOL [])values
  7174. forKeys:(const int64_t [])keys
  7175. count:(NSUInteger)count {
  7176. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7177. // on to get the type correct.
  7178. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  7179. forKeys:keys
  7180. count:count] autorelease];
  7181. }
  7182. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7183. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7184. // on to get the type correct.
  7185. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7186. }
  7187. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7188. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7189. }
  7190. - (instancetype)init {
  7191. return [self initWithValues:NULL forKeys:NULL count:0];
  7192. }
  7193. - (instancetype)initWithValues:(const BOOL [])values
  7194. forKeys:(const int64_t [])keys
  7195. count:(NSUInteger)count {
  7196. self = [super init];
  7197. if (self) {
  7198. _dictionary = [[NSMutableDictionary alloc] init];
  7199. if (count && values && keys) {
  7200. for (NSUInteger i = 0; i < count; ++i) {
  7201. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7202. }
  7203. }
  7204. }
  7205. return self;
  7206. }
  7207. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7208. self = [self initWithValues:NULL forKeys:NULL count:0];
  7209. if (self) {
  7210. if (dictionary) {
  7211. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7212. }
  7213. }
  7214. return self;
  7215. }
  7216. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7217. #pragma unused(numItems)
  7218. return [self initWithValues:NULL forKeys:NULL count:0];
  7219. }
  7220. - (void)dealloc {
  7221. NSAssert(!_autocreator,
  7222. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7223. [self class], _autocreator);
  7224. [_dictionary release];
  7225. [super dealloc];
  7226. }
  7227. - (instancetype)copyWithZone:(NSZone *)zone {
  7228. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7229. }
  7230. - (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
  7231. if (self == other) {
  7232. return YES;
  7233. }
  7234. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7235. return NO;
  7236. }
  7237. return [_dictionary isEqual:other->_dictionary];
  7238. }
  7239. - (NSUInteger)hash {
  7240. return _dictionary.count;
  7241. }
  7242. - (NSString *)description {
  7243. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7244. }
  7245. - (NSUInteger)count {
  7246. return _dictionary.count;
  7247. }
  7248. - (void)enumerateKeysAndValuesUsingBlock:
  7249. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7250. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7251. NSNumber *aValue,
  7252. BOOL *stop) {
  7253. block([aKey longLongValue], [aValue boolValue], stop);
  7254. }];
  7255. }
  7256. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7257. NSUInteger count = _dictionary.count;
  7258. if (count == 0) {
  7259. return 0;
  7260. }
  7261. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7262. GPBDataType keyDataType = field.mapKeyDataType;
  7263. __block size_t result = 0;
  7264. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7265. NSNumber *aValue,
  7266. BOOL *stop) {
  7267. #pragma unused(stop)
  7268. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7269. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7270. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7271. }];
  7272. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7273. result += tagSize * count;
  7274. return result;
  7275. }
  7276. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7277. asField:(GPBFieldDescriptor *)field {
  7278. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7279. GPBDataType keyDataType = field.mapKeyDataType;
  7280. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7281. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7282. NSNumber *aValue,
  7283. BOOL *stop) {
  7284. #pragma unused(stop)
  7285. // Write the tag.
  7286. [outputStream writeInt32NoTag:tag];
  7287. // Write the size of the message.
  7288. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7289. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7290. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7291. // Write the fields.
  7292. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7293. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7294. }];
  7295. }
  7296. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7297. forGPBGenericValueKey:(GPBGenericValue *)key {
  7298. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7299. }
  7300. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7301. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7302. #pragma unused(stop)
  7303. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7304. }];
  7305. }
  7306. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  7307. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7308. if (wrapped && value) {
  7309. *value = [wrapped boolValue];
  7310. }
  7311. return (wrapped != NULL);
  7312. }
  7313. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7314. if (otherDictionary) {
  7315. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7316. if (_autocreator) {
  7317. GPBAutocreatedDictionaryModified(_autocreator, self);
  7318. }
  7319. }
  7320. }
  7321. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  7322. [_dictionary setObject:@(value) forKey:@(key)];
  7323. if (_autocreator) {
  7324. GPBAutocreatedDictionaryModified(_autocreator, self);
  7325. }
  7326. }
  7327. - (void)removeValueForKey:(int64_t)aKey {
  7328. [_dictionary removeObjectForKey:@(aKey)];
  7329. }
  7330. - (void)removeAll {
  7331. [_dictionary removeAllObjects];
  7332. }
  7333. @end
  7334. #pragma mark - Int64 -> Float
  7335. @implementation GPBInt64FloatDictionary {
  7336. @package
  7337. NSMutableDictionary *_dictionary;
  7338. }
  7339. + (instancetype)dictionary {
  7340. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7341. }
  7342. + (instancetype)dictionaryWithValue:(float)value
  7343. forKey:(int64_t)key {
  7344. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7345. // on to get the type correct.
  7346. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  7347. forKeys:&key
  7348. count:1] autorelease];
  7349. }
  7350. + (instancetype)dictionaryWithValues:(const float [])values
  7351. forKeys:(const int64_t [])keys
  7352. count:(NSUInteger)count {
  7353. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7354. // on to get the type correct.
  7355. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  7356. forKeys:keys
  7357. count:count] autorelease];
  7358. }
  7359. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7360. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7361. // on to get the type correct.
  7362. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7363. }
  7364. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7365. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7366. }
  7367. - (instancetype)init {
  7368. return [self initWithValues:NULL forKeys:NULL count:0];
  7369. }
  7370. - (instancetype)initWithValues:(const float [])values
  7371. forKeys:(const int64_t [])keys
  7372. count:(NSUInteger)count {
  7373. self = [super init];
  7374. if (self) {
  7375. _dictionary = [[NSMutableDictionary alloc] init];
  7376. if (count && values && keys) {
  7377. for (NSUInteger i = 0; i < count; ++i) {
  7378. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7379. }
  7380. }
  7381. }
  7382. return self;
  7383. }
  7384. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7385. self = [self initWithValues:NULL forKeys:NULL count:0];
  7386. if (self) {
  7387. if (dictionary) {
  7388. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7389. }
  7390. }
  7391. return self;
  7392. }
  7393. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7394. #pragma unused(numItems)
  7395. return [self initWithValues:NULL forKeys:NULL count:0];
  7396. }
  7397. - (void)dealloc {
  7398. NSAssert(!_autocreator,
  7399. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7400. [self class], _autocreator);
  7401. [_dictionary release];
  7402. [super dealloc];
  7403. }
  7404. - (instancetype)copyWithZone:(NSZone *)zone {
  7405. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7406. }
  7407. - (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
  7408. if (self == other) {
  7409. return YES;
  7410. }
  7411. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7412. return NO;
  7413. }
  7414. return [_dictionary isEqual:other->_dictionary];
  7415. }
  7416. - (NSUInteger)hash {
  7417. return _dictionary.count;
  7418. }
  7419. - (NSString *)description {
  7420. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7421. }
  7422. - (NSUInteger)count {
  7423. return _dictionary.count;
  7424. }
  7425. - (void)enumerateKeysAndValuesUsingBlock:
  7426. (void (^)(int64_t key, float value, BOOL *stop))block {
  7427. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7428. NSNumber *aValue,
  7429. BOOL *stop) {
  7430. block([aKey longLongValue], [aValue floatValue], stop);
  7431. }];
  7432. }
  7433. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7434. NSUInteger count = _dictionary.count;
  7435. if (count == 0) {
  7436. return 0;
  7437. }
  7438. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7439. GPBDataType keyDataType = field.mapKeyDataType;
  7440. __block size_t result = 0;
  7441. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7442. NSNumber *aValue,
  7443. BOOL *stop) {
  7444. #pragma unused(stop)
  7445. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7446. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7447. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7448. }];
  7449. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7450. result += tagSize * count;
  7451. return result;
  7452. }
  7453. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7454. asField:(GPBFieldDescriptor *)field {
  7455. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7456. GPBDataType keyDataType = field.mapKeyDataType;
  7457. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7458. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7459. NSNumber *aValue,
  7460. BOOL *stop) {
  7461. #pragma unused(stop)
  7462. // Write the tag.
  7463. [outputStream writeInt32NoTag:tag];
  7464. // Write the size of the message.
  7465. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7466. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7467. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7468. // Write the fields.
  7469. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7470. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7471. }];
  7472. }
  7473. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7474. forGPBGenericValueKey:(GPBGenericValue *)key {
  7475. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7476. }
  7477. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7478. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7479. #pragma unused(stop)
  7480. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7481. }];
  7482. }
  7483. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7484. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7485. if (wrapped && value) {
  7486. *value = [wrapped floatValue];
  7487. }
  7488. return (wrapped != NULL);
  7489. }
  7490. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7491. if (otherDictionary) {
  7492. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7493. if (_autocreator) {
  7494. GPBAutocreatedDictionaryModified(_autocreator, self);
  7495. }
  7496. }
  7497. }
  7498. - (void)setValue:(float)value forKey:(int64_t)key {
  7499. [_dictionary setObject:@(value) forKey:@(key)];
  7500. if (_autocreator) {
  7501. GPBAutocreatedDictionaryModified(_autocreator, self);
  7502. }
  7503. }
  7504. - (void)removeValueForKey:(int64_t)aKey {
  7505. [_dictionary removeObjectForKey:@(aKey)];
  7506. }
  7507. - (void)removeAll {
  7508. [_dictionary removeAllObjects];
  7509. }
  7510. @end
  7511. #pragma mark - Int64 -> Double
  7512. @implementation GPBInt64DoubleDictionary {
  7513. @package
  7514. NSMutableDictionary *_dictionary;
  7515. }
  7516. + (instancetype)dictionary {
  7517. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7518. }
  7519. + (instancetype)dictionaryWithValue:(double)value
  7520. forKey:(int64_t)key {
  7521. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7522. // on to get the type correct.
  7523. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7524. forKeys:&key
  7525. count:1] autorelease];
  7526. }
  7527. + (instancetype)dictionaryWithValues:(const double [])values
  7528. forKeys:(const int64_t [])keys
  7529. count:(NSUInteger)count {
  7530. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7531. // on to get the type correct.
  7532. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7533. forKeys:keys
  7534. count:count] autorelease];
  7535. }
  7536. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7537. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7538. // on to get the type correct.
  7539. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7540. }
  7541. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7542. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7543. }
  7544. - (instancetype)init {
  7545. return [self initWithValues:NULL forKeys:NULL count:0];
  7546. }
  7547. - (instancetype)initWithValues:(const double [])values
  7548. forKeys:(const int64_t [])keys
  7549. count:(NSUInteger)count {
  7550. self = [super init];
  7551. if (self) {
  7552. _dictionary = [[NSMutableDictionary alloc] init];
  7553. if (count && values && keys) {
  7554. for (NSUInteger i = 0; i < count; ++i) {
  7555. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7556. }
  7557. }
  7558. }
  7559. return self;
  7560. }
  7561. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7562. self = [self initWithValues:NULL forKeys:NULL count:0];
  7563. if (self) {
  7564. if (dictionary) {
  7565. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7566. }
  7567. }
  7568. return self;
  7569. }
  7570. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7571. #pragma unused(numItems)
  7572. return [self initWithValues:NULL forKeys:NULL count:0];
  7573. }
  7574. - (void)dealloc {
  7575. NSAssert(!_autocreator,
  7576. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7577. [self class], _autocreator);
  7578. [_dictionary release];
  7579. [super dealloc];
  7580. }
  7581. - (instancetype)copyWithZone:(NSZone *)zone {
  7582. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7583. }
  7584. - (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
  7585. if (self == other) {
  7586. return YES;
  7587. }
  7588. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7589. return NO;
  7590. }
  7591. return [_dictionary isEqual:other->_dictionary];
  7592. }
  7593. - (NSUInteger)hash {
  7594. return _dictionary.count;
  7595. }
  7596. - (NSString *)description {
  7597. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7598. }
  7599. - (NSUInteger)count {
  7600. return _dictionary.count;
  7601. }
  7602. - (void)enumerateKeysAndValuesUsingBlock:
  7603. (void (^)(int64_t key, double value, BOOL *stop))block {
  7604. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7605. NSNumber *aValue,
  7606. BOOL *stop) {
  7607. block([aKey longLongValue], [aValue doubleValue], stop);
  7608. }];
  7609. }
  7610. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7611. NSUInteger count = _dictionary.count;
  7612. if (count == 0) {
  7613. return 0;
  7614. }
  7615. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7616. GPBDataType keyDataType = field.mapKeyDataType;
  7617. __block size_t result = 0;
  7618. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7619. NSNumber *aValue,
  7620. BOOL *stop) {
  7621. #pragma unused(stop)
  7622. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7623. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7624. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7625. }];
  7626. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7627. result += tagSize * count;
  7628. return result;
  7629. }
  7630. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7631. asField:(GPBFieldDescriptor *)field {
  7632. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7633. GPBDataType keyDataType = field.mapKeyDataType;
  7634. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7635. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7636. NSNumber *aValue,
  7637. BOOL *stop) {
  7638. #pragma unused(stop)
  7639. // Write the tag.
  7640. [outputStream writeInt32NoTag:tag];
  7641. // Write the size of the message.
  7642. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7643. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7644. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7645. // Write the fields.
  7646. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7647. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7648. }];
  7649. }
  7650. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7651. forGPBGenericValueKey:(GPBGenericValue *)key {
  7652. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7653. }
  7654. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7655. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7656. #pragma unused(stop)
  7657. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7658. }];
  7659. }
  7660. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7661. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7662. if (wrapped && value) {
  7663. *value = [wrapped doubleValue];
  7664. }
  7665. return (wrapped != NULL);
  7666. }
  7667. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7668. if (otherDictionary) {
  7669. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7670. if (_autocreator) {
  7671. GPBAutocreatedDictionaryModified(_autocreator, self);
  7672. }
  7673. }
  7674. }
  7675. - (void)setValue:(double)value forKey:(int64_t)key {
  7676. [_dictionary setObject:@(value) forKey:@(key)];
  7677. if (_autocreator) {
  7678. GPBAutocreatedDictionaryModified(_autocreator, self);
  7679. }
  7680. }
  7681. - (void)removeValueForKey:(int64_t)aKey {
  7682. [_dictionary removeObjectForKey:@(aKey)];
  7683. }
  7684. - (void)removeAll {
  7685. [_dictionary removeAllObjects];
  7686. }
  7687. @end
  7688. #pragma mark - Int64 -> Enum
  7689. @implementation GPBInt64EnumDictionary {
  7690. @package
  7691. NSMutableDictionary *_dictionary;
  7692. GPBEnumValidationFunc _validationFunc;
  7693. }
  7694. @synthesize validationFunc = _validationFunc;
  7695. + (instancetype)dictionary {
  7696. return [[[self alloc] initWithValidationFunction:NULL
  7697. rawValues:NULL
  7698. forKeys:NULL
  7699. count:0] autorelease];
  7700. }
  7701. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7702. return [[[self alloc] initWithValidationFunction:func
  7703. rawValues:NULL
  7704. forKeys:NULL
  7705. count:0] autorelease];
  7706. }
  7707. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7708. rawValue:(int32_t)rawValue
  7709. forKey:(int64_t)key {
  7710. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7711. // on to get the type correct.
  7712. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7713. rawValues:&rawValue
  7714. forKeys:&key
  7715. count:1] autorelease];
  7716. }
  7717. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7718. rawValues:(const int32_t [])rawValues
  7719. forKeys:(const int64_t [])keys
  7720. count:(NSUInteger)count {
  7721. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7722. // on to get the type correct.
  7723. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7724. rawValues:rawValues
  7725. forKeys:keys
  7726. count:count] autorelease];
  7727. }
  7728. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7729. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7730. // on to get the type correct.
  7731. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7732. }
  7733. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7734. capacity:(NSUInteger)numItems {
  7735. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7736. }
  7737. - (instancetype)init {
  7738. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7739. }
  7740. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7741. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7742. }
  7743. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7744. rawValues:(const int32_t [])rawValues
  7745. forKeys:(const int64_t [])keys
  7746. count:(NSUInteger)count {
  7747. self = [super init];
  7748. if (self) {
  7749. _dictionary = [[NSMutableDictionary alloc] init];
  7750. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7751. if (count && rawValues && keys) {
  7752. for (NSUInteger i = 0; i < count; ++i) {
  7753. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7754. }
  7755. }
  7756. }
  7757. return self;
  7758. }
  7759. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7760. self = [self initWithValidationFunction:dictionary.validationFunc
  7761. rawValues:NULL
  7762. forKeys:NULL
  7763. count:0];
  7764. if (self) {
  7765. if (dictionary) {
  7766. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7767. }
  7768. }
  7769. return self;
  7770. }
  7771. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7772. capacity:(NSUInteger)numItems {
  7773. #pragma unused(numItems)
  7774. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7775. }
  7776. - (void)dealloc {
  7777. NSAssert(!_autocreator,
  7778. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7779. [self class], _autocreator);
  7780. [_dictionary release];
  7781. [super dealloc];
  7782. }
  7783. - (instancetype)copyWithZone:(NSZone *)zone {
  7784. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7785. }
  7786. - (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
  7787. if (self == other) {
  7788. return YES;
  7789. }
  7790. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7791. return NO;
  7792. }
  7793. return [_dictionary isEqual:other->_dictionary];
  7794. }
  7795. - (NSUInteger)hash {
  7796. return _dictionary.count;
  7797. }
  7798. - (NSString *)description {
  7799. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7800. }
  7801. - (NSUInteger)count {
  7802. return _dictionary.count;
  7803. }
  7804. - (void)enumerateKeysAndRawValuesUsingBlock:
  7805. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7806. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7807. NSNumber *aValue,
  7808. BOOL *stop) {
  7809. block([aKey longLongValue], [aValue intValue], stop);
  7810. }];
  7811. }
  7812. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7813. NSUInteger count = _dictionary.count;
  7814. if (count == 0) {
  7815. return 0;
  7816. }
  7817. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7818. GPBDataType keyDataType = field.mapKeyDataType;
  7819. __block size_t result = 0;
  7820. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7821. NSNumber *aValue,
  7822. BOOL *stop) {
  7823. #pragma unused(stop)
  7824. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7825. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7826. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7827. }];
  7828. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7829. result += tagSize * count;
  7830. return result;
  7831. }
  7832. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7833. asField:(GPBFieldDescriptor *)field {
  7834. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7835. GPBDataType keyDataType = field.mapKeyDataType;
  7836. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7837. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7838. NSNumber *aValue,
  7839. BOOL *stop) {
  7840. #pragma unused(stop)
  7841. // Write the tag.
  7842. [outputStream writeInt32NoTag:tag];
  7843. // Write the size of the message.
  7844. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7845. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7846. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7847. // Write the fields.
  7848. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7849. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7850. }];
  7851. }
  7852. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7853. forKey:(GPBGenericValue *)key
  7854. keyDataType:(GPBDataType)keyDataType {
  7855. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7856. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7857. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7858. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7859. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7860. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7861. [outputStream release];
  7862. return data;
  7863. }
  7864. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7865. forGPBGenericValueKey:(GPBGenericValue *)key {
  7866. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7867. }
  7868. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7869. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7870. #pragma unused(stop)
  7871. block([NSString stringWithFormat:@"%lld", key], @(value));
  7872. }];
  7873. }
  7874. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7875. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7876. if (wrapped && value) {
  7877. int32_t result = [wrapped intValue];
  7878. if (!_validationFunc(result)) {
  7879. result = kGPBUnrecognizedEnumeratorValue;
  7880. }
  7881. *value = result;
  7882. }
  7883. return (wrapped != NULL);
  7884. }
  7885. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  7886. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7887. if (wrapped && rawValue) {
  7888. *rawValue = [wrapped intValue];
  7889. }
  7890. return (wrapped != NULL);
  7891. }
  7892. - (void)enumerateKeysAndValuesUsingBlock:
  7893. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7894. GPBEnumValidationFunc func = _validationFunc;
  7895. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7896. NSNumber *aValue,
  7897. BOOL *stop) {
  7898. int32_t unwrapped = [aValue intValue];
  7899. if (!func(unwrapped)) {
  7900. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7901. }
  7902. block([aKey longLongValue], unwrapped, stop);
  7903. }];
  7904. }
  7905. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7906. if (otherDictionary) {
  7907. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7908. if (_autocreator) {
  7909. GPBAutocreatedDictionaryModified(_autocreator, self);
  7910. }
  7911. }
  7912. }
  7913. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7914. [_dictionary setObject:@(value) forKey:@(key)];
  7915. if (_autocreator) {
  7916. GPBAutocreatedDictionaryModified(_autocreator, self);
  7917. }
  7918. }
  7919. - (void)removeValueForKey:(int64_t)aKey {
  7920. [_dictionary removeObjectForKey:@(aKey)];
  7921. }
  7922. - (void)removeAll {
  7923. [_dictionary removeAllObjects];
  7924. }
  7925. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  7926. if (!_validationFunc(value)) {
  7927. [NSException raise:NSInvalidArgumentException
  7928. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7929. value];
  7930. }
  7931. [_dictionary setObject:@(value) forKey:@(key)];
  7932. if (_autocreator) {
  7933. GPBAutocreatedDictionaryModified(_autocreator, self);
  7934. }
  7935. }
  7936. @end
  7937. #pragma mark - Int64 -> Object
  7938. @implementation GPBInt64ObjectDictionary {
  7939. @package
  7940. NSMutableDictionary *_dictionary;
  7941. }
  7942. + (instancetype)dictionary {
  7943. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7944. }
  7945. + (instancetype)dictionaryWithValue:(id)value
  7946. forKey:(int64_t)key {
  7947. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7948. // on to get the type correct.
  7949. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithValues:&value
  7950. forKeys:&key
  7951. count:1] autorelease];
  7952. }
  7953. + (instancetype)dictionaryWithValues:(const id [])values
  7954. forKeys:(const int64_t [])keys
  7955. count:(NSUInteger)count {
  7956. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7957. // on to get the type correct.
  7958. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithValues:values
  7959. forKeys:keys
  7960. count:count] autorelease];
  7961. }
  7962. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7963. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7964. // on to get the type correct.
  7965. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7966. }
  7967. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7968. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7969. }
  7970. - (instancetype)init {
  7971. return [self initWithValues:NULL forKeys:NULL count:0];
  7972. }
  7973. - (instancetype)initWithValues:(const id [])values
  7974. forKeys:(const int64_t [])keys
  7975. count:(NSUInteger)count {
  7976. self = [super init];
  7977. if (self) {
  7978. _dictionary = [[NSMutableDictionary alloc] init];
  7979. if (count && values && keys) {
  7980. for (NSUInteger i = 0; i < count; ++i) {
  7981. [_dictionary setObject:values[i] forKey:@(keys[i])];
  7982. }
  7983. }
  7984. }
  7985. return self;
  7986. }
  7987. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7988. self = [self initWithValues:NULL forKeys:NULL count:0];
  7989. if (self) {
  7990. if (dictionary) {
  7991. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7992. }
  7993. }
  7994. return self;
  7995. }
  7996. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7997. #pragma unused(numItems)
  7998. return [self initWithValues:NULL forKeys:NULL count:0];
  7999. }
  8000. - (void)dealloc {
  8001. NSAssert(!_autocreator,
  8002. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8003. [self class], _autocreator);
  8004. [_dictionary release];
  8005. [super dealloc];
  8006. }
  8007. - (instancetype)copyWithZone:(NSZone *)zone {
  8008. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8009. }
  8010. - (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
  8011. if (self == other) {
  8012. return YES;
  8013. }
  8014. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8015. return NO;
  8016. }
  8017. return [_dictionary isEqual:other->_dictionary];
  8018. }
  8019. - (NSUInteger)hash {
  8020. return _dictionary.count;
  8021. }
  8022. - (NSString *)description {
  8023. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8024. }
  8025. - (NSUInteger)count {
  8026. return _dictionary.count;
  8027. }
  8028. - (void)enumerateKeysAndValuesUsingBlock:
  8029. (void (^)(int64_t key, id value, BOOL *stop))block {
  8030. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8031. id aValue,
  8032. BOOL *stop) {
  8033. block([aKey longLongValue], aValue, stop);
  8034. }];
  8035. }
  8036. - (BOOL)isInitialized {
  8037. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8038. if (!msg.initialized) {
  8039. return NO;
  8040. }
  8041. }
  8042. return YES;
  8043. }
  8044. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8045. GPBInt64ObjectDictionary *newDict =
  8046. [[GPBInt64ObjectDictionary alloc] init];
  8047. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8048. GPBMessage *msg,
  8049. BOOL *stop) {
  8050. #pragma unused(stop)
  8051. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8052. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8053. [copiedMsg release];
  8054. }];
  8055. return newDict;
  8056. }
  8057. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8058. NSUInteger count = _dictionary.count;
  8059. if (count == 0) {
  8060. return 0;
  8061. }
  8062. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8063. GPBDataType keyDataType = field.mapKeyDataType;
  8064. __block size_t result = 0;
  8065. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8066. id aValue,
  8067. BOOL *stop) {
  8068. #pragma unused(stop)
  8069. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8070. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  8071. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8072. }];
  8073. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8074. result += tagSize * count;
  8075. return result;
  8076. }
  8077. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8078. asField:(GPBFieldDescriptor *)field {
  8079. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8080. GPBDataType keyDataType = field.mapKeyDataType;
  8081. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8082. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8083. id aValue,
  8084. BOOL *stop) {
  8085. #pragma unused(stop)
  8086. // Write the tag.
  8087. [outputStream writeInt32NoTag:tag];
  8088. // Write the size of the message.
  8089. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8090. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueDataType);
  8091. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8092. // Write the fields.
  8093. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8094. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueDataType);
  8095. }];
  8096. }
  8097. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8098. forGPBGenericValueKey:(GPBGenericValue *)key {
  8099. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8100. }
  8101. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8102. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, id value, BOOL *stop) {
  8103. #pragma unused(stop)
  8104. block([NSString stringWithFormat:@"%lld", key], value);
  8105. }];
  8106. }
  8107. - (id)valueForKey:(int64_t)key {
  8108. id result = [_dictionary objectForKey:@(key)];
  8109. return result;
  8110. }
  8111. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8112. if (otherDictionary) {
  8113. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8114. if (_autocreator) {
  8115. GPBAutocreatedDictionaryModified(_autocreator, self);
  8116. }
  8117. }
  8118. }
  8119. - (void)setValue:(id)value forKey:(int64_t)key {
  8120. [_dictionary setObject:value forKey:@(key)];
  8121. if (_autocreator) {
  8122. GPBAutocreatedDictionaryModified(_autocreator, self);
  8123. }
  8124. }
  8125. - (void)removeValueForKey:(int64_t)aKey {
  8126. [_dictionary removeObjectForKey:@(aKey)];
  8127. }
  8128. - (void)removeAll {
  8129. [_dictionary removeAllObjects];
  8130. }
  8131. @end
  8132. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8133. // This block of code is generated, do not edit it directly.
  8134. #pragma mark - String -> UInt32
  8135. @implementation GPBStringUInt32Dictionary {
  8136. @package
  8137. NSMutableDictionary *_dictionary;
  8138. }
  8139. + (instancetype)dictionary {
  8140. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8141. }
  8142. + (instancetype)dictionaryWithValue:(uint32_t)value
  8143. forKey:(NSString *)key {
  8144. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8145. // on to get the type correct.
  8146. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  8147. forKeys:&key
  8148. count:1] autorelease];
  8149. }
  8150. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  8151. forKeys:(const NSString * [])keys
  8152. count:(NSUInteger)count {
  8153. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8154. // on to get the type correct.
  8155. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  8156. forKeys:keys
  8157. count:count] autorelease];
  8158. }
  8159. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8160. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8161. // on to get the type correct.
  8162. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8163. }
  8164. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8165. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8166. }
  8167. - (instancetype)init {
  8168. return [self initWithValues:NULL forKeys:NULL count:0];
  8169. }
  8170. - (instancetype)initWithValues:(const uint32_t [])values
  8171. forKeys:(const NSString * [])keys
  8172. count:(NSUInteger)count {
  8173. self = [super init];
  8174. if (self) {
  8175. _dictionary = [[NSMutableDictionary alloc] init];
  8176. if (count && values && keys) {
  8177. for (NSUInteger i = 0; i < count; ++i) {
  8178. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8179. }
  8180. }
  8181. }
  8182. return self;
  8183. }
  8184. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8185. self = [self initWithValues:NULL forKeys:NULL count:0];
  8186. if (self) {
  8187. if (dictionary) {
  8188. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8189. }
  8190. }
  8191. return self;
  8192. }
  8193. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8194. #pragma unused(numItems)
  8195. return [self initWithValues:NULL forKeys:NULL count:0];
  8196. }
  8197. - (void)dealloc {
  8198. NSAssert(!_autocreator,
  8199. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8200. [self class], _autocreator);
  8201. [_dictionary release];
  8202. [super dealloc];
  8203. }
  8204. - (instancetype)copyWithZone:(NSZone *)zone {
  8205. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8206. }
  8207. - (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
  8208. if (self == other) {
  8209. return YES;
  8210. }
  8211. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8212. return NO;
  8213. }
  8214. return [_dictionary isEqual:other->_dictionary];
  8215. }
  8216. - (NSUInteger)hash {
  8217. return _dictionary.count;
  8218. }
  8219. - (NSString *)description {
  8220. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8221. }
  8222. - (NSUInteger)count {
  8223. return _dictionary.count;
  8224. }
  8225. - (void)enumerateKeysAndValuesUsingBlock:
  8226. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8227. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8228. NSNumber *aValue,
  8229. BOOL *stop) {
  8230. block(aKey, [aValue unsignedIntValue], stop);
  8231. }];
  8232. }
  8233. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8234. NSUInteger count = _dictionary.count;
  8235. if (count == 0) {
  8236. return 0;
  8237. }
  8238. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8239. GPBDataType keyDataType = field.mapKeyDataType;
  8240. __block size_t result = 0;
  8241. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8242. NSNumber *aValue,
  8243. BOOL *stop) {
  8244. #pragma unused(stop)
  8245. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8246. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8247. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8248. }];
  8249. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8250. result += tagSize * count;
  8251. return result;
  8252. }
  8253. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8254. asField:(GPBFieldDescriptor *)field {
  8255. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8256. GPBDataType keyDataType = field.mapKeyDataType;
  8257. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8258. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8259. NSNumber *aValue,
  8260. BOOL *stop) {
  8261. #pragma unused(stop)
  8262. // Write the tag.
  8263. [outputStream writeInt32NoTag:tag];
  8264. // Write the size of the message.
  8265. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8266. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8267. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8268. // Write the fields.
  8269. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8270. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8271. }];
  8272. }
  8273. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8274. forGPBGenericValueKey:(GPBGenericValue *)key {
  8275. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8276. }
  8277. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8278. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8279. #pragma unused(stop)
  8280. block(key, [NSString stringWithFormat:@"%u", value]);
  8281. }];
  8282. }
  8283. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  8284. NSNumber *wrapped = [_dictionary objectForKey:key];
  8285. if (wrapped && value) {
  8286. *value = [wrapped unsignedIntValue];
  8287. }
  8288. return (wrapped != NULL);
  8289. }
  8290. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8291. if (otherDictionary) {
  8292. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8293. if (_autocreator) {
  8294. GPBAutocreatedDictionaryModified(_autocreator, self);
  8295. }
  8296. }
  8297. }
  8298. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  8299. [_dictionary setObject:@(value) forKey:key];
  8300. if (_autocreator) {
  8301. GPBAutocreatedDictionaryModified(_autocreator, self);
  8302. }
  8303. }
  8304. - (void)removeValueForKey:(NSString *)aKey {
  8305. [_dictionary removeObjectForKey:aKey];
  8306. }
  8307. - (void)removeAll {
  8308. [_dictionary removeAllObjects];
  8309. }
  8310. @end
  8311. #pragma mark - String -> Int32
  8312. @implementation GPBStringInt32Dictionary {
  8313. @package
  8314. NSMutableDictionary *_dictionary;
  8315. }
  8316. + (instancetype)dictionary {
  8317. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8318. }
  8319. + (instancetype)dictionaryWithValue:(int32_t)value
  8320. forKey:(NSString *)key {
  8321. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8322. // on to get the type correct.
  8323. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  8324. forKeys:&key
  8325. count:1] autorelease];
  8326. }
  8327. + (instancetype)dictionaryWithValues:(const int32_t [])values
  8328. forKeys:(const NSString * [])keys
  8329. count:(NSUInteger)count {
  8330. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8331. // on to get the type correct.
  8332. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  8333. forKeys:keys
  8334. count:count] autorelease];
  8335. }
  8336. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8337. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8338. // on to get the type correct.
  8339. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8340. }
  8341. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8342. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8343. }
  8344. - (instancetype)init {
  8345. return [self initWithValues:NULL forKeys:NULL count:0];
  8346. }
  8347. - (instancetype)initWithValues:(const int32_t [])values
  8348. forKeys:(const NSString * [])keys
  8349. count:(NSUInteger)count {
  8350. self = [super init];
  8351. if (self) {
  8352. _dictionary = [[NSMutableDictionary alloc] init];
  8353. if (count && values && keys) {
  8354. for (NSUInteger i = 0; i < count; ++i) {
  8355. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8356. }
  8357. }
  8358. }
  8359. return self;
  8360. }
  8361. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8362. self = [self initWithValues:NULL forKeys:NULL count:0];
  8363. if (self) {
  8364. if (dictionary) {
  8365. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8366. }
  8367. }
  8368. return self;
  8369. }
  8370. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8371. #pragma unused(numItems)
  8372. return [self initWithValues:NULL forKeys:NULL count:0];
  8373. }
  8374. - (void)dealloc {
  8375. NSAssert(!_autocreator,
  8376. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8377. [self class], _autocreator);
  8378. [_dictionary release];
  8379. [super dealloc];
  8380. }
  8381. - (instancetype)copyWithZone:(NSZone *)zone {
  8382. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8383. }
  8384. - (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
  8385. if (self == other) {
  8386. return YES;
  8387. }
  8388. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8389. return NO;
  8390. }
  8391. return [_dictionary isEqual:other->_dictionary];
  8392. }
  8393. - (NSUInteger)hash {
  8394. return _dictionary.count;
  8395. }
  8396. - (NSString *)description {
  8397. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8398. }
  8399. - (NSUInteger)count {
  8400. return _dictionary.count;
  8401. }
  8402. - (void)enumerateKeysAndValuesUsingBlock:
  8403. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8404. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8405. NSNumber *aValue,
  8406. BOOL *stop) {
  8407. block(aKey, [aValue intValue], stop);
  8408. }];
  8409. }
  8410. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8411. NSUInteger count = _dictionary.count;
  8412. if (count == 0) {
  8413. return 0;
  8414. }
  8415. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8416. GPBDataType keyDataType = field.mapKeyDataType;
  8417. __block size_t result = 0;
  8418. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8419. NSNumber *aValue,
  8420. BOOL *stop) {
  8421. #pragma unused(stop)
  8422. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8423. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8424. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8425. }];
  8426. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8427. result += tagSize * count;
  8428. return result;
  8429. }
  8430. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8431. asField:(GPBFieldDescriptor *)field {
  8432. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8433. GPBDataType keyDataType = field.mapKeyDataType;
  8434. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8435. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8436. NSNumber *aValue,
  8437. BOOL *stop) {
  8438. #pragma unused(stop)
  8439. // Write the tag.
  8440. [outputStream writeInt32NoTag:tag];
  8441. // Write the size of the message.
  8442. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8443. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8444. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8445. // Write the fields.
  8446. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8447. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8448. }];
  8449. }
  8450. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8451. forGPBGenericValueKey:(GPBGenericValue *)key {
  8452. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8453. }
  8454. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8455. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8456. #pragma unused(stop)
  8457. block(key, [NSString stringWithFormat:@"%d", value]);
  8458. }];
  8459. }
  8460. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8461. NSNumber *wrapped = [_dictionary objectForKey:key];
  8462. if (wrapped && value) {
  8463. *value = [wrapped intValue];
  8464. }
  8465. return (wrapped != NULL);
  8466. }
  8467. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8468. if (otherDictionary) {
  8469. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8470. if (_autocreator) {
  8471. GPBAutocreatedDictionaryModified(_autocreator, self);
  8472. }
  8473. }
  8474. }
  8475. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8476. [_dictionary setObject:@(value) forKey:key];
  8477. if (_autocreator) {
  8478. GPBAutocreatedDictionaryModified(_autocreator, self);
  8479. }
  8480. }
  8481. - (void)removeValueForKey:(NSString *)aKey {
  8482. [_dictionary removeObjectForKey:aKey];
  8483. }
  8484. - (void)removeAll {
  8485. [_dictionary removeAllObjects];
  8486. }
  8487. @end
  8488. #pragma mark - String -> UInt64
  8489. @implementation GPBStringUInt64Dictionary {
  8490. @package
  8491. NSMutableDictionary *_dictionary;
  8492. }
  8493. + (instancetype)dictionary {
  8494. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8495. }
  8496. + (instancetype)dictionaryWithValue:(uint64_t)value
  8497. forKey:(NSString *)key {
  8498. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8499. // on to get the type correct.
  8500. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8501. forKeys:&key
  8502. count:1] autorelease];
  8503. }
  8504. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8505. forKeys:(const NSString * [])keys
  8506. count:(NSUInteger)count {
  8507. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8508. // on to get the type correct.
  8509. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8510. forKeys:keys
  8511. count:count] autorelease];
  8512. }
  8513. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8514. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8515. // on to get the type correct.
  8516. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8517. }
  8518. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8519. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8520. }
  8521. - (instancetype)init {
  8522. return [self initWithValues:NULL forKeys:NULL count:0];
  8523. }
  8524. - (instancetype)initWithValues:(const uint64_t [])values
  8525. forKeys:(const NSString * [])keys
  8526. count:(NSUInteger)count {
  8527. self = [super init];
  8528. if (self) {
  8529. _dictionary = [[NSMutableDictionary alloc] init];
  8530. if (count && values && keys) {
  8531. for (NSUInteger i = 0; i < count; ++i) {
  8532. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8533. }
  8534. }
  8535. }
  8536. return self;
  8537. }
  8538. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8539. self = [self initWithValues:NULL forKeys:NULL count:0];
  8540. if (self) {
  8541. if (dictionary) {
  8542. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8543. }
  8544. }
  8545. return self;
  8546. }
  8547. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8548. #pragma unused(numItems)
  8549. return [self initWithValues:NULL forKeys:NULL count:0];
  8550. }
  8551. - (void)dealloc {
  8552. NSAssert(!_autocreator,
  8553. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8554. [self class], _autocreator);
  8555. [_dictionary release];
  8556. [super dealloc];
  8557. }
  8558. - (instancetype)copyWithZone:(NSZone *)zone {
  8559. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8560. }
  8561. - (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
  8562. if (self == other) {
  8563. return YES;
  8564. }
  8565. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8566. return NO;
  8567. }
  8568. return [_dictionary isEqual:other->_dictionary];
  8569. }
  8570. - (NSUInteger)hash {
  8571. return _dictionary.count;
  8572. }
  8573. - (NSString *)description {
  8574. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8575. }
  8576. - (NSUInteger)count {
  8577. return _dictionary.count;
  8578. }
  8579. - (void)enumerateKeysAndValuesUsingBlock:
  8580. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8581. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8582. NSNumber *aValue,
  8583. BOOL *stop) {
  8584. block(aKey, [aValue unsignedLongLongValue], stop);
  8585. }];
  8586. }
  8587. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8588. NSUInteger count = _dictionary.count;
  8589. if (count == 0) {
  8590. return 0;
  8591. }
  8592. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8593. GPBDataType keyDataType = field.mapKeyDataType;
  8594. __block size_t result = 0;
  8595. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8596. NSNumber *aValue,
  8597. BOOL *stop) {
  8598. #pragma unused(stop)
  8599. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8600. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8601. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8602. }];
  8603. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8604. result += tagSize * count;
  8605. return result;
  8606. }
  8607. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8608. asField:(GPBFieldDescriptor *)field {
  8609. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8610. GPBDataType keyDataType = field.mapKeyDataType;
  8611. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8612. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8613. NSNumber *aValue,
  8614. BOOL *stop) {
  8615. #pragma unused(stop)
  8616. // Write the tag.
  8617. [outputStream writeInt32NoTag:tag];
  8618. // Write the size of the message.
  8619. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8620. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8621. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8622. // Write the fields.
  8623. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8624. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8625. }];
  8626. }
  8627. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8628. forGPBGenericValueKey:(GPBGenericValue *)key {
  8629. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8630. }
  8631. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8632. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8633. #pragma unused(stop)
  8634. block(key, [NSString stringWithFormat:@"%llu", value]);
  8635. }];
  8636. }
  8637. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8638. NSNumber *wrapped = [_dictionary objectForKey:key];
  8639. if (wrapped && value) {
  8640. *value = [wrapped unsignedLongLongValue];
  8641. }
  8642. return (wrapped != NULL);
  8643. }
  8644. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8645. if (otherDictionary) {
  8646. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8647. if (_autocreator) {
  8648. GPBAutocreatedDictionaryModified(_autocreator, self);
  8649. }
  8650. }
  8651. }
  8652. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8653. [_dictionary setObject:@(value) forKey:key];
  8654. if (_autocreator) {
  8655. GPBAutocreatedDictionaryModified(_autocreator, self);
  8656. }
  8657. }
  8658. - (void)removeValueForKey:(NSString *)aKey {
  8659. [_dictionary removeObjectForKey:aKey];
  8660. }
  8661. - (void)removeAll {
  8662. [_dictionary removeAllObjects];
  8663. }
  8664. @end
  8665. #pragma mark - String -> Int64
  8666. @implementation GPBStringInt64Dictionary {
  8667. @package
  8668. NSMutableDictionary *_dictionary;
  8669. }
  8670. + (instancetype)dictionary {
  8671. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8672. }
  8673. + (instancetype)dictionaryWithValue:(int64_t)value
  8674. forKey:(NSString *)key {
  8675. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8676. // on to get the type correct.
  8677. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8678. forKeys:&key
  8679. count:1] autorelease];
  8680. }
  8681. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8682. forKeys:(const NSString * [])keys
  8683. count:(NSUInteger)count {
  8684. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8685. // on to get the type correct.
  8686. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8687. forKeys:keys
  8688. count:count] autorelease];
  8689. }
  8690. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8691. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8692. // on to get the type correct.
  8693. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8694. }
  8695. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8696. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8697. }
  8698. - (instancetype)init {
  8699. return [self initWithValues:NULL forKeys:NULL count:0];
  8700. }
  8701. - (instancetype)initWithValues:(const int64_t [])values
  8702. forKeys:(const NSString * [])keys
  8703. count:(NSUInteger)count {
  8704. self = [super init];
  8705. if (self) {
  8706. _dictionary = [[NSMutableDictionary alloc] init];
  8707. if (count && values && keys) {
  8708. for (NSUInteger i = 0; i < count; ++i) {
  8709. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8710. }
  8711. }
  8712. }
  8713. return self;
  8714. }
  8715. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8716. self = [self initWithValues:NULL forKeys:NULL count:0];
  8717. if (self) {
  8718. if (dictionary) {
  8719. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8720. }
  8721. }
  8722. return self;
  8723. }
  8724. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8725. #pragma unused(numItems)
  8726. return [self initWithValues:NULL forKeys:NULL count:0];
  8727. }
  8728. - (void)dealloc {
  8729. NSAssert(!_autocreator,
  8730. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8731. [self class], _autocreator);
  8732. [_dictionary release];
  8733. [super dealloc];
  8734. }
  8735. - (instancetype)copyWithZone:(NSZone *)zone {
  8736. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8737. }
  8738. - (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
  8739. if (self == other) {
  8740. return YES;
  8741. }
  8742. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8743. return NO;
  8744. }
  8745. return [_dictionary isEqual:other->_dictionary];
  8746. }
  8747. - (NSUInteger)hash {
  8748. return _dictionary.count;
  8749. }
  8750. - (NSString *)description {
  8751. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8752. }
  8753. - (NSUInteger)count {
  8754. return _dictionary.count;
  8755. }
  8756. - (void)enumerateKeysAndValuesUsingBlock:
  8757. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8758. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8759. NSNumber *aValue,
  8760. BOOL *stop) {
  8761. block(aKey, [aValue longLongValue], stop);
  8762. }];
  8763. }
  8764. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8765. NSUInteger count = _dictionary.count;
  8766. if (count == 0) {
  8767. return 0;
  8768. }
  8769. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8770. GPBDataType keyDataType = field.mapKeyDataType;
  8771. __block size_t result = 0;
  8772. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8773. NSNumber *aValue,
  8774. BOOL *stop) {
  8775. #pragma unused(stop)
  8776. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8777. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8778. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8779. }];
  8780. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8781. result += tagSize * count;
  8782. return result;
  8783. }
  8784. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8785. asField:(GPBFieldDescriptor *)field {
  8786. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8787. GPBDataType keyDataType = field.mapKeyDataType;
  8788. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8789. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8790. NSNumber *aValue,
  8791. BOOL *stop) {
  8792. #pragma unused(stop)
  8793. // Write the tag.
  8794. [outputStream writeInt32NoTag:tag];
  8795. // Write the size of the message.
  8796. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8797. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8798. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8799. // Write the fields.
  8800. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8801. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8802. }];
  8803. }
  8804. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8805. forGPBGenericValueKey:(GPBGenericValue *)key {
  8806. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8807. }
  8808. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8809. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8810. #pragma unused(stop)
  8811. block(key, [NSString stringWithFormat:@"%lld", value]);
  8812. }];
  8813. }
  8814. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8815. NSNumber *wrapped = [_dictionary objectForKey:key];
  8816. if (wrapped && value) {
  8817. *value = [wrapped longLongValue];
  8818. }
  8819. return (wrapped != NULL);
  8820. }
  8821. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8822. if (otherDictionary) {
  8823. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8824. if (_autocreator) {
  8825. GPBAutocreatedDictionaryModified(_autocreator, self);
  8826. }
  8827. }
  8828. }
  8829. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8830. [_dictionary setObject:@(value) forKey:key];
  8831. if (_autocreator) {
  8832. GPBAutocreatedDictionaryModified(_autocreator, self);
  8833. }
  8834. }
  8835. - (void)removeValueForKey:(NSString *)aKey {
  8836. [_dictionary removeObjectForKey:aKey];
  8837. }
  8838. - (void)removeAll {
  8839. [_dictionary removeAllObjects];
  8840. }
  8841. @end
  8842. #pragma mark - String -> Bool
  8843. @implementation GPBStringBoolDictionary {
  8844. @package
  8845. NSMutableDictionary *_dictionary;
  8846. }
  8847. + (instancetype)dictionary {
  8848. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8849. }
  8850. + (instancetype)dictionaryWithValue:(BOOL)value
  8851. forKey:(NSString *)key {
  8852. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8853. // on to get the type correct.
  8854. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  8855. forKeys:&key
  8856. count:1] autorelease];
  8857. }
  8858. + (instancetype)dictionaryWithValues:(const BOOL [])values
  8859. forKeys:(const NSString * [])keys
  8860. count:(NSUInteger)count {
  8861. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8862. // on to get the type correct.
  8863. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  8864. forKeys:keys
  8865. count:count] autorelease];
  8866. }
  8867. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8868. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8869. // on to get the type correct.
  8870. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8871. }
  8872. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8873. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8874. }
  8875. - (instancetype)init {
  8876. return [self initWithValues:NULL forKeys:NULL count:0];
  8877. }
  8878. - (instancetype)initWithValues:(const BOOL [])values
  8879. forKeys:(const NSString * [])keys
  8880. count:(NSUInteger)count {
  8881. self = [super init];
  8882. if (self) {
  8883. _dictionary = [[NSMutableDictionary alloc] init];
  8884. if (count && values && keys) {
  8885. for (NSUInteger i = 0; i < count; ++i) {
  8886. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8887. }
  8888. }
  8889. }
  8890. return self;
  8891. }
  8892. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8893. self = [self initWithValues:NULL forKeys:NULL count:0];
  8894. if (self) {
  8895. if (dictionary) {
  8896. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8897. }
  8898. }
  8899. return self;
  8900. }
  8901. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8902. #pragma unused(numItems)
  8903. return [self initWithValues:NULL forKeys:NULL count:0];
  8904. }
  8905. - (void)dealloc {
  8906. NSAssert(!_autocreator,
  8907. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8908. [self class], _autocreator);
  8909. [_dictionary release];
  8910. [super dealloc];
  8911. }
  8912. - (instancetype)copyWithZone:(NSZone *)zone {
  8913. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8914. }
  8915. - (BOOL)isEqual:(GPBStringBoolDictionary *)other {
  8916. if (self == other) {
  8917. return YES;
  8918. }
  8919. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8920. return NO;
  8921. }
  8922. return [_dictionary isEqual:other->_dictionary];
  8923. }
  8924. - (NSUInteger)hash {
  8925. return _dictionary.count;
  8926. }
  8927. - (NSString *)description {
  8928. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8929. }
  8930. - (NSUInteger)count {
  8931. return _dictionary.count;
  8932. }
  8933. - (void)enumerateKeysAndValuesUsingBlock:
  8934. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  8935. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8936. NSNumber *aValue,
  8937. BOOL *stop) {
  8938. block(aKey, [aValue boolValue], stop);
  8939. }];
  8940. }
  8941. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8942. NSUInteger count = _dictionary.count;
  8943. if (count == 0) {
  8944. return 0;
  8945. }
  8946. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8947. GPBDataType keyDataType = field.mapKeyDataType;
  8948. __block size_t result = 0;
  8949. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8950. NSNumber *aValue,
  8951. BOOL *stop) {
  8952. #pragma unused(stop)
  8953. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8954. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8955. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8956. }];
  8957. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8958. result += tagSize * count;
  8959. return result;
  8960. }
  8961. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8962. asField:(GPBFieldDescriptor *)field {
  8963. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8964. GPBDataType keyDataType = field.mapKeyDataType;
  8965. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8966. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8967. NSNumber *aValue,
  8968. BOOL *stop) {
  8969. #pragma unused(stop)
  8970. // Write the tag.
  8971. [outputStream writeInt32NoTag:tag];
  8972. // Write the size of the message.
  8973. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8974. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8975. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8976. // Write the fields.
  8977. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8978. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  8979. }];
  8980. }
  8981. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8982. forGPBGenericValueKey:(GPBGenericValue *)key {
  8983. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8984. }
  8985. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8986. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  8987. #pragma unused(stop)
  8988. block(key, (value ? @"true" : @"false"));
  8989. }];
  8990. }
  8991. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  8992. NSNumber *wrapped = [_dictionary objectForKey:key];
  8993. if (wrapped && value) {
  8994. *value = [wrapped boolValue];
  8995. }
  8996. return (wrapped != NULL);
  8997. }
  8998. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  8999. if (otherDictionary) {
  9000. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9001. if (_autocreator) {
  9002. GPBAutocreatedDictionaryModified(_autocreator, self);
  9003. }
  9004. }
  9005. }
  9006. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  9007. [_dictionary setObject:@(value) forKey:key];
  9008. if (_autocreator) {
  9009. GPBAutocreatedDictionaryModified(_autocreator, self);
  9010. }
  9011. }
  9012. - (void)removeValueForKey:(NSString *)aKey {
  9013. [_dictionary removeObjectForKey:aKey];
  9014. }
  9015. - (void)removeAll {
  9016. [_dictionary removeAllObjects];
  9017. }
  9018. @end
  9019. #pragma mark - String -> Float
  9020. @implementation GPBStringFloatDictionary {
  9021. @package
  9022. NSMutableDictionary *_dictionary;
  9023. }
  9024. + (instancetype)dictionary {
  9025. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9026. }
  9027. + (instancetype)dictionaryWithValue:(float)value
  9028. forKey:(NSString *)key {
  9029. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9030. // on to get the type correct.
  9031. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  9032. forKeys:&key
  9033. count:1] autorelease];
  9034. }
  9035. + (instancetype)dictionaryWithValues:(const float [])values
  9036. forKeys:(const NSString * [])keys
  9037. count:(NSUInteger)count {
  9038. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9039. // on to get the type correct.
  9040. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  9041. forKeys:keys
  9042. count:count] autorelease];
  9043. }
  9044. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9045. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9046. // on to get the type correct.
  9047. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9048. }
  9049. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9050. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9051. }
  9052. - (instancetype)init {
  9053. return [self initWithValues:NULL forKeys:NULL count:0];
  9054. }
  9055. - (instancetype)initWithValues:(const float [])values
  9056. forKeys:(const NSString * [])keys
  9057. count:(NSUInteger)count {
  9058. self = [super init];
  9059. if (self) {
  9060. _dictionary = [[NSMutableDictionary alloc] init];
  9061. if (count && values && keys) {
  9062. for (NSUInteger i = 0; i < count; ++i) {
  9063. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9064. }
  9065. }
  9066. }
  9067. return self;
  9068. }
  9069. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9070. self = [self initWithValues:NULL forKeys:NULL count:0];
  9071. if (self) {
  9072. if (dictionary) {
  9073. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9074. }
  9075. }
  9076. return self;
  9077. }
  9078. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9079. #pragma unused(numItems)
  9080. return [self initWithValues:NULL forKeys:NULL count:0];
  9081. }
  9082. - (void)dealloc {
  9083. NSAssert(!_autocreator,
  9084. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9085. [self class], _autocreator);
  9086. [_dictionary release];
  9087. [super dealloc];
  9088. }
  9089. - (instancetype)copyWithZone:(NSZone *)zone {
  9090. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9091. }
  9092. - (BOOL)isEqual:(GPBStringFloatDictionary *)other {
  9093. if (self == other) {
  9094. return YES;
  9095. }
  9096. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9097. return NO;
  9098. }
  9099. return [_dictionary isEqual:other->_dictionary];
  9100. }
  9101. - (NSUInteger)hash {
  9102. return _dictionary.count;
  9103. }
  9104. - (NSString *)description {
  9105. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9106. }
  9107. - (NSUInteger)count {
  9108. return _dictionary.count;
  9109. }
  9110. - (void)enumerateKeysAndValuesUsingBlock:
  9111. (void (^)(NSString *key, float value, BOOL *stop))block {
  9112. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9113. NSNumber *aValue,
  9114. BOOL *stop) {
  9115. block(aKey, [aValue floatValue], stop);
  9116. }];
  9117. }
  9118. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9119. NSUInteger count = _dictionary.count;
  9120. if (count == 0) {
  9121. return 0;
  9122. }
  9123. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9124. GPBDataType keyDataType = field.mapKeyDataType;
  9125. __block size_t result = 0;
  9126. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9127. NSNumber *aValue,
  9128. BOOL *stop) {
  9129. #pragma unused(stop)
  9130. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9131. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9132. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9133. }];
  9134. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9135. result += tagSize * count;
  9136. return result;
  9137. }
  9138. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9139. asField:(GPBFieldDescriptor *)field {
  9140. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9141. GPBDataType keyDataType = field.mapKeyDataType;
  9142. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9143. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9144. NSNumber *aValue,
  9145. BOOL *stop) {
  9146. #pragma unused(stop)
  9147. // Write the tag.
  9148. [outputStream writeInt32NoTag:tag];
  9149. // Write the size of the message.
  9150. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9151. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9152. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9153. // Write the fields.
  9154. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9155. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9156. }];
  9157. }
  9158. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9159. forGPBGenericValueKey:(GPBGenericValue *)key {
  9160. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9161. }
  9162. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9163. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9164. #pragma unused(stop)
  9165. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9166. }];
  9167. }
  9168. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  9169. NSNumber *wrapped = [_dictionary objectForKey:key];
  9170. if (wrapped && value) {
  9171. *value = [wrapped floatValue];
  9172. }
  9173. return (wrapped != NULL);
  9174. }
  9175. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9176. if (otherDictionary) {
  9177. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9178. if (_autocreator) {
  9179. GPBAutocreatedDictionaryModified(_autocreator, self);
  9180. }
  9181. }
  9182. }
  9183. - (void)setValue:(float)value forKey:(NSString *)key {
  9184. [_dictionary setObject:@(value) forKey:key];
  9185. if (_autocreator) {
  9186. GPBAutocreatedDictionaryModified(_autocreator, self);
  9187. }
  9188. }
  9189. - (void)removeValueForKey:(NSString *)aKey {
  9190. [_dictionary removeObjectForKey:aKey];
  9191. }
  9192. - (void)removeAll {
  9193. [_dictionary removeAllObjects];
  9194. }
  9195. @end
  9196. #pragma mark - String -> Double
  9197. @implementation GPBStringDoubleDictionary {
  9198. @package
  9199. NSMutableDictionary *_dictionary;
  9200. }
  9201. + (instancetype)dictionary {
  9202. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9203. }
  9204. + (instancetype)dictionaryWithValue:(double)value
  9205. forKey:(NSString *)key {
  9206. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9207. // on to get the type correct.
  9208. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  9209. forKeys:&key
  9210. count:1] autorelease];
  9211. }
  9212. + (instancetype)dictionaryWithValues:(const double [])values
  9213. forKeys:(const NSString * [])keys
  9214. count:(NSUInteger)count {
  9215. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9216. // on to get the type correct.
  9217. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  9218. forKeys:keys
  9219. count:count] autorelease];
  9220. }
  9221. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9222. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9223. // on to get the type correct.
  9224. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9225. }
  9226. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9227. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9228. }
  9229. - (instancetype)init {
  9230. return [self initWithValues:NULL forKeys:NULL count:0];
  9231. }
  9232. - (instancetype)initWithValues:(const double [])values
  9233. forKeys:(const NSString * [])keys
  9234. count:(NSUInteger)count {
  9235. self = [super init];
  9236. if (self) {
  9237. _dictionary = [[NSMutableDictionary alloc] init];
  9238. if (count && values && keys) {
  9239. for (NSUInteger i = 0; i < count; ++i) {
  9240. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9241. }
  9242. }
  9243. }
  9244. return self;
  9245. }
  9246. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9247. self = [self initWithValues:NULL forKeys:NULL count:0];
  9248. if (self) {
  9249. if (dictionary) {
  9250. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9251. }
  9252. }
  9253. return self;
  9254. }
  9255. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9256. #pragma unused(numItems)
  9257. return [self initWithValues:NULL forKeys:NULL count:0];
  9258. }
  9259. - (void)dealloc {
  9260. NSAssert(!_autocreator,
  9261. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9262. [self class], _autocreator);
  9263. [_dictionary release];
  9264. [super dealloc];
  9265. }
  9266. - (instancetype)copyWithZone:(NSZone *)zone {
  9267. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9268. }
  9269. - (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
  9270. if (self == other) {
  9271. return YES;
  9272. }
  9273. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9274. return NO;
  9275. }
  9276. return [_dictionary isEqual:other->_dictionary];
  9277. }
  9278. - (NSUInteger)hash {
  9279. return _dictionary.count;
  9280. }
  9281. - (NSString *)description {
  9282. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9283. }
  9284. - (NSUInteger)count {
  9285. return _dictionary.count;
  9286. }
  9287. - (void)enumerateKeysAndValuesUsingBlock:
  9288. (void (^)(NSString *key, double value, BOOL *stop))block {
  9289. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9290. NSNumber *aValue,
  9291. BOOL *stop) {
  9292. block(aKey, [aValue doubleValue], stop);
  9293. }];
  9294. }
  9295. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9296. NSUInteger count = _dictionary.count;
  9297. if (count == 0) {
  9298. return 0;
  9299. }
  9300. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9301. GPBDataType keyDataType = field.mapKeyDataType;
  9302. __block size_t result = 0;
  9303. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9304. NSNumber *aValue,
  9305. BOOL *stop) {
  9306. #pragma unused(stop)
  9307. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9308. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9309. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9310. }];
  9311. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9312. result += tagSize * count;
  9313. return result;
  9314. }
  9315. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9316. asField:(GPBFieldDescriptor *)field {
  9317. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9318. GPBDataType keyDataType = field.mapKeyDataType;
  9319. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9320. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9321. NSNumber *aValue,
  9322. BOOL *stop) {
  9323. #pragma unused(stop)
  9324. // Write the tag.
  9325. [outputStream writeInt32NoTag:tag];
  9326. // Write the size of the message.
  9327. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9328. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9329. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9330. // Write the fields.
  9331. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9332. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9333. }];
  9334. }
  9335. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9336. forGPBGenericValueKey:(GPBGenericValue *)key {
  9337. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9338. }
  9339. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9340. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9341. #pragma unused(stop)
  9342. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9343. }];
  9344. }
  9345. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  9346. NSNumber *wrapped = [_dictionary objectForKey:key];
  9347. if (wrapped && value) {
  9348. *value = [wrapped doubleValue];
  9349. }
  9350. return (wrapped != NULL);
  9351. }
  9352. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9353. if (otherDictionary) {
  9354. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9355. if (_autocreator) {
  9356. GPBAutocreatedDictionaryModified(_autocreator, self);
  9357. }
  9358. }
  9359. }
  9360. - (void)setValue:(double)value forKey:(NSString *)key {
  9361. [_dictionary setObject:@(value) forKey:key];
  9362. if (_autocreator) {
  9363. GPBAutocreatedDictionaryModified(_autocreator, self);
  9364. }
  9365. }
  9366. - (void)removeValueForKey:(NSString *)aKey {
  9367. [_dictionary removeObjectForKey:aKey];
  9368. }
  9369. - (void)removeAll {
  9370. [_dictionary removeAllObjects];
  9371. }
  9372. @end
  9373. #pragma mark - String -> Enum
  9374. @implementation GPBStringEnumDictionary {
  9375. @package
  9376. NSMutableDictionary *_dictionary;
  9377. GPBEnumValidationFunc _validationFunc;
  9378. }
  9379. @synthesize validationFunc = _validationFunc;
  9380. + (instancetype)dictionary {
  9381. return [[[self alloc] initWithValidationFunction:NULL
  9382. rawValues:NULL
  9383. forKeys:NULL
  9384. count:0] autorelease];
  9385. }
  9386. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9387. return [[[self alloc] initWithValidationFunction:func
  9388. rawValues:NULL
  9389. forKeys:NULL
  9390. count:0] autorelease];
  9391. }
  9392. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9393. rawValue:(int32_t)rawValue
  9394. forKey:(NSString *)key {
  9395. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9396. // on to get the type correct.
  9397. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9398. rawValues:&rawValue
  9399. forKeys:&key
  9400. count:1] autorelease];
  9401. }
  9402. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9403. rawValues:(const int32_t [])rawValues
  9404. forKeys:(const NSString * [])keys
  9405. count:(NSUInteger)count {
  9406. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9407. // on to get the type correct.
  9408. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9409. rawValues:rawValues
  9410. forKeys:keys
  9411. count:count] autorelease];
  9412. }
  9413. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9414. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9415. // on to get the type correct.
  9416. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9417. }
  9418. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9419. capacity:(NSUInteger)numItems {
  9420. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9421. }
  9422. - (instancetype)init {
  9423. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9424. }
  9425. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9426. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9427. }
  9428. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9429. rawValues:(const int32_t [])rawValues
  9430. forKeys:(const NSString * [])keys
  9431. count:(NSUInteger)count {
  9432. self = [super init];
  9433. if (self) {
  9434. _dictionary = [[NSMutableDictionary alloc] init];
  9435. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9436. if (count && rawValues && keys) {
  9437. for (NSUInteger i = 0; i < count; ++i) {
  9438. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9439. }
  9440. }
  9441. }
  9442. return self;
  9443. }
  9444. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9445. self = [self initWithValidationFunction:dictionary.validationFunc
  9446. rawValues:NULL
  9447. forKeys:NULL
  9448. count:0];
  9449. if (self) {
  9450. if (dictionary) {
  9451. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9452. }
  9453. }
  9454. return self;
  9455. }
  9456. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9457. capacity:(NSUInteger)numItems {
  9458. #pragma unused(numItems)
  9459. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9460. }
  9461. - (void)dealloc {
  9462. NSAssert(!_autocreator,
  9463. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9464. [self class], _autocreator);
  9465. [_dictionary release];
  9466. [super dealloc];
  9467. }
  9468. - (instancetype)copyWithZone:(NSZone *)zone {
  9469. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9470. }
  9471. - (BOOL)isEqual:(GPBStringEnumDictionary *)other {
  9472. if (self == other) {
  9473. return YES;
  9474. }
  9475. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9476. return NO;
  9477. }
  9478. return [_dictionary isEqual:other->_dictionary];
  9479. }
  9480. - (NSUInteger)hash {
  9481. return _dictionary.count;
  9482. }
  9483. - (NSString *)description {
  9484. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9485. }
  9486. - (NSUInteger)count {
  9487. return _dictionary.count;
  9488. }
  9489. - (void)enumerateKeysAndRawValuesUsingBlock:
  9490. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9491. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9492. NSNumber *aValue,
  9493. BOOL *stop) {
  9494. block(aKey, [aValue intValue], stop);
  9495. }];
  9496. }
  9497. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9498. NSUInteger count = _dictionary.count;
  9499. if (count == 0) {
  9500. return 0;
  9501. }
  9502. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9503. GPBDataType keyDataType = field.mapKeyDataType;
  9504. __block size_t result = 0;
  9505. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9506. NSNumber *aValue,
  9507. BOOL *stop) {
  9508. #pragma unused(stop)
  9509. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9510. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9511. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9512. }];
  9513. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9514. result += tagSize * count;
  9515. return result;
  9516. }
  9517. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9518. asField:(GPBFieldDescriptor *)field {
  9519. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9520. GPBDataType keyDataType = field.mapKeyDataType;
  9521. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9522. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9523. NSNumber *aValue,
  9524. BOOL *stop) {
  9525. #pragma unused(stop)
  9526. // Write the tag.
  9527. [outputStream writeInt32NoTag:tag];
  9528. // Write the size of the message.
  9529. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9530. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9531. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9532. // Write the fields.
  9533. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9534. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9535. }];
  9536. }
  9537. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9538. forKey:(GPBGenericValue *)key
  9539. keyDataType:(GPBDataType)keyDataType {
  9540. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9541. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9542. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9543. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9544. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9545. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9546. [outputStream release];
  9547. return data;
  9548. }
  9549. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9550. forGPBGenericValueKey:(GPBGenericValue *)key {
  9551. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9552. }
  9553. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9554. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9555. #pragma unused(stop)
  9556. block(key, @(value));
  9557. }];
  9558. }
  9559. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9560. NSNumber *wrapped = [_dictionary objectForKey:key];
  9561. if (wrapped && value) {
  9562. int32_t result = [wrapped intValue];
  9563. if (!_validationFunc(result)) {
  9564. result = kGPBUnrecognizedEnumeratorValue;
  9565. }
  9566. *value = result;
  9567. }
  9568. return (wrapped != NULL);
  9569. }
  9570. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9571. NSNumber *wrapped = [_dictionary objectForKey:key];
  9572. if (wrapped && rawValue) {
  9573. *rawValue = [wrapped intValue];
  9574. }
  9575. return (wrapped != NULL);
  9576. }
  9577. - (void)enumerateKeysAndValuesUsingBlock:
  9578. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9579. GPBEnumValidationFunc func = _validationFunc;
  9580. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9581. NSNumber *aValue,
  9582. BOOL *stop) {
  9583. int32_t unwrapped = [aValue intValue];
  9584. if (!func(unwrapped)) {
  9585. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9586. }
  9587. block(aKey, unwrapped, stop);
  9588. }];
  9589. }
  9590. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9591. if (otherDictionary) {
  9592. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9593. if (_autocreator) {
  9594. GPBAutocreatedDictionaryModified(_autocreator, self);
  9595. }
  9596. }
  9597. }
  9598. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9599. [_dictionary setObject:@(value) forKey:key];
  9600. if (_autocreator) {
  9601. GPBAutocreatedDictionaryModified(_autocreator, self);
  9602. }
  9603. }
  9604. - (void)removeValueForKey:(NSString *)aKey {
  9605. [_dictionary removeObjectForKey:aKey];
  9606. }
  9607. - (void)removeAll {
  9608. [_dictionary removeAllObjects];
  9609. }
  9610. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9611. if (!_validationFunc(value)) {
  9612. [NSException raise:NSInvalidArgumentException
  9613. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9614. value];
  9615. }
  9616. [_dictionary setObject:@(value) forKey:key];
  9617. if (_autocreator) {
  9618. GPBAutocreatedDictionaryModified(_autocreator, self);
  9619. }
  9620. }
  9621. @end
  9622. //%PDDM-EXPAND-END (5 expansions)
  9623. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9624. // This block of code is generated, do not edit it directly.
  9625. #pragma mark - Bool -> UInt32
  9626. @implementation GPBBoolUInt32Dictionary {
  9627. @package
  9628. uint32_t _values[2];
  9629. BOOL _valueSet[2];
  9630. }
  9631. + (instancetype)dictionary {
  9632. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9633. }
  9634. + (instancetype)dictionaryWithValue:(uint32_t)value
  9635. forKey:(BOOL)key {
  9636. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9637. // on to get the type correct.
  9638. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9639. forKeys:&key
  9640. count:1] autorelease];
  9641. }
  9642. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9643. forKeys:(const BOOL [])keys
  9644. count:(NSUInteger)count {
  9645. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9646. // on to get the type correct.
  9647. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9648. forKeys:keys
  9649. count:count] autorelease];
  9650. }
  9651. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9652. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9653. // on to get the type correct.
  9654. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9655. }
  9656. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9657. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9658. }
  9659. - (instancetype)init {
  9660. return [self initWithValues:NULL forKeys:NULL count:0];
  9661. }
  9662. - (instancetype)initWithValues:(const uint32_t [])values
  9663. forKeys:(const BOOL [])keys
  9664. count:(NSUInteger)count {
  9665. self = [super init];
  9666. if (self) {
  9667. for (NSUInteger i = 0; i < count; ++i) {
  9668. int idx = keys[i] ? 1 : 0;
  9669. _values[idx] = values[i];
  9670. _valueSet[idx] = YES;
  9671. }
  9672. }
  9673. return self;
  9674. }
  9675. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9676. self = [self initWithValues:NULL forKeys:NULL count:0];
  9677. if (self) {
  9678. if (dictionary) {
  9679. for (int i = 0; i < 2; ++i) {
  9680. if (dictionary->_valueSet[i]) {
  9681. _values[i] = dictionary->_values[i];
  9682. _valueSet[i] = YES;
  9683. }
  9684. }
  9685. }
  9686. }
  9687. return self;
  9688. }
  9689. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9690. #pragma unused(numItems)
  9691. return [self initWithValues:NULL forKeys:NULL count:0];
  9692. }
  9693. #if !defined(NS_BLOCK_ASSERTIONS)
  9694. - (void)dealloc {
  9695. NSAssert(!_autocreator,
  9696. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9697. [self class], _autocreator);
  9698. [super dealloc];
  9699. }
  9700. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9701. - (instancetype)copyWithZone:(NSZone *)zone {
  9702. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9703. }
  9704. - (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
  9705. if (self == other) {
  9706. return YES;
  9707. }
  9708. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9709. return NO;
  9710. }
  9711. if ((_valueSet[0] != other->_valueSet[0]) ||
  9712. (_valueSet[1] != other->_valueSet[1])) {
  9713. return NO;
  9714. }
  9715. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9716. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9717. return NO;
  9718. }
  9719. return YES;
  9720. }
  9721. - (NSUInteger)hash {
  9722. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9723. }
  9724. - (NSString *)description {
  9725. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9726. if (_valueSet[0]) {
  9727. [result appendFormat:@"NO: %u", _values[0]];
  9728. }
  9729. if (_valueSet[1]) {
  9730. [result appendFormat:@"YES: %u", _values[1]];
  9731. }
  9732. [result appendString:@" }"];
  9733. return result;
  9734. }
  9735. - (NSUInteger)count {
  9736. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9737. }
  9738. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9739. int idx = (key ? 1 : 0);
  9740. if (_valueSet[idx]) {
  9741. if (value) {
  9742. *value = _values[idx];
  9743. }
  9744. return YES;
  9745. }
  9746. return NO;
  9747. }
  9748. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9749. forGPBGenericValueKey:(GPBGenericValue *)key {
  9750. int idx = (key->valueBool ? 1 : 0);
  9751. _values[idx] = value->valueUInt32;
  9752. _valueSet[idx] = YES;
  9753. }
  9754. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9755. if (_valueSet[0]) {
  9756. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9757. }
  9758. if (_valueSet[1]) {
  9759. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9760. }
  9761. }
  9762. - (void)enumerateKeysAndValuesUsingBlock:
  9763. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9764. BOOL stop = NO;
  9765. if (_valueSet[0]) {
  9766. block(NO, _values[0], &stop);
  9767. }
  9768. if (!stop && _valueSet[1]) {
  9769. block(YES, _values[1], &stop);
  9770. }
  9771. }
  9772. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9773. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9774. NSUInteger count = 0;
  9775. size_t result = 0;
  9776. for (int i = 0; i < 2; ++i) {
  9777. if (_valueSet[i]) {
  9778. ++count;
  9779. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9780. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9781. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9782. }
  9783. }
  9784. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9785. result += tagSize * count;
  9786. return result;
  9787. }
  9788. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9789. asField:(GPBFieldDescriptor *)field {
  9790. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9791. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9792. for (int i = 0; i < 2; ++i) {
  9793. if (_valueSet[i]) {
  9794. // Write the tag.
  9795. [outputStream writeInt32NoTag:tag];
  9796. // Write the size of the message.
  9797. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9798. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9799. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9800. // Write the fields.
  9801. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9802. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9803. }
  9804. }
  9805. }
  9806. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9807. if (otherDictionary) {
  9808. for (int i = 0; i < 2; ++i) {
  9809. if (otherDictionary->_valueSet[i]) {
  9810. _valueSet[i] = YES;
  9811. _values[i] = otherDictionary->_values[i];
  9812. }
  9813. }
  9814. if (_autocreator) {
  9815. GPBAutocreatedDictionaryModified(_autocreator, self);
  9816. }
  9817. }
  9818. }
  9819. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  9820. int idx = (key ? 1 : 0);
  9821. _values[idx] = value;
  9822. _valueSet[idx] = YES;
  9823. if (_autocreator) {
  9824. GPBAutocreatedDictionaryModified(_autocreator, self);
  9825. }
  9826. }
  9827. - (void)removeValueForKey:(BOOL)aKey {
  9828. _valueSet[aKey ? 1 : 0] = NO;
  9829. }
  9830. - (void)removeAll {
  9831. _valueSet[0] = NO;
  9832. _valueSet[1] = NO;
  9833. }
  9834. @end
  9835. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9836. // This block of code is generated, do not edit it directly.
  9837. #pragma mark - Bool -> Int32
  9838. @implementation GPBBoolInt32Dictionary {
  9839. @package
  9840. int32_t _values[2];
  9841. BOOL _valueSet[2];
  9842. }
  9843. + (instancetype)dictionary {
  9844. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9845. }
  9846. + (instancetype)dictionaryWithValue:(int32_t)value
  9847. forKey:(BOOL)key {
  9848. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9849. // on to get the type correct.
  9850. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  9851. forKeys:&key
  9852. count:1] autorelease];
  9853. }
  9854. + (instancetype)dictionaryWithValues:(const int32_t [])values
  9855. forKeys:(const BOOL [])keys
  9856. count:(NSUInteger)count {
  9857. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9858. // on to get the type correct.
  9859. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  9860. forKeys:keys
  9861. count:count] autorelease];
  9862. }
  9863. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9864. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9865. // on to get the type correct.
  9866. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9867. }
  9868. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9869. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9870. }
  9871. - (instancetype)init {
  9872. return [self initWithValues:NULL forKeys:NULL count:0];
  9873. }
  9874. - (instancetype)initWithValues:(const int32_t [])values
  9875. forKeys:(const BOOL [])keys
  9876. count:(NSUInteger)count {
  9877. self = [super init];
  9878. if (self) {
  9879. for (NSUInteger i = 0; i < count; ++i) {
  9880. int idx = keys[i] ? 1 : 0;
  9881. _values[idx] = values[i];
  9882. _valueSet[idx] = YES;
  9883. }
  9884. }
  9885. return self;
  9886. }
  9887. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9888. self = [self initWithValues:NULL forKeys:NULL count:0];
  9889. if (self) {
  9890. if (dictionary) {
  9891. for (int i = 0; i < 2; ++i) {
  9892. if (dictionary->_valueSet[i]) {
  9893. _values[i] = dictionary->_values[i];
  9894. _valueSet[i] = YES;
  9895. }
  9896. }
  9897. }
  9898. }
  9899. return self;
  9900. }
  9901. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9902. #pragma unused(numItems)
  9903. return [self initWithValues:NULL forKeys:NULL count:0];
  9904. }
  9905. #if !defined(NS_BLOCK_ASSERTIONS)
  9906. - (void)dealloc {
  9907. NSAssert(!_autocreator,
  9908. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9909. [self class], _autocreator);
  9910. [super dealloc];
  9911. }
  9912. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9913. - (instancetype)copyWithZone:(NSZone *)zone {
  9914. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9915. }
  9916. - (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
  9917. if (self == other) {
  9918. return YES;
  9919. }
  9920. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  9921. return NO;
  9922. }
  9923. if ((_valueSet[0] != other->_valueSet[0]) ||
  9924. (_valueSet[1] != other->_valueSet[1])) {
  9925. return NO;
  9926. }
  9927. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9928. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9929. return NO;
  9930. }
  9931. return YES;
  9932. }
  9933. - (NSUInteger)hash {
  9934. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9935. }
  9936. - (NSString *)description {
  9937. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9938. if (_valueSet[0]) {
  9939. [result appendFormat:@"NO: %d", _values[0]];
  9940. }
  9941. if (_valueSet[1]) {
  9942. [result appendFormat:@"YES: %d", _values[1]];
  9943. }
  9944. [result appendString:@" }"];
  9945. return result;
  9946. }
  9947. - (NSUInteger)count {
  9948. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9949. }
  9950. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  9951. int idx = (key ? 1 : 0);
  9952. if (_valueSet[idx]) {
  9953. if (value) {
  9954. *value = _values[idx];
  9955. }
  9956. return YES;
  9957. }
  9958. return NO;
  9959. }
  9960. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9961. forGPBGenericValueKey:(GPBGenericValue *)key {
  9962. int idx = (key->valueBool ? 1 : 0);
  9963. _values[idx] = value->valueInt32;
  9964. _valueSet[idx] = YES;
  9965. }
  9966. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9967. if (_valueSet[0]) {
  9968. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9969. }
  9970. if (_valueSet[1]) {
  9971. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9972. }
  9973. }
  9974. - (void)enumerateKeysAndValuesUsingBlock:
  9975. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  9976. BOOL stop = NO;
  9977. if (_valueSet[0]) {
  9978. block(NO, _values[0], &stop);
  9979. }
  9980. if (!stop && _valueSet[1]) {
  9981. block(YES, _values[1], &stop);
  9982. }
  9983. }
  9984. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9985. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9986. NSUInteger count = 0;
  9987. size_t result = 0;
  9988. for (int i = 0; i < 2; ++i) {
  9989. if (_valueSet[i]) {
  9990. ++count;
  9991. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9992. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9993. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9994. }
  9995. }
  9996. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9997. result += tagSize * count;
  9998. return result;
  9999. }
  10000. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10001. asField:(GPBFieldDescriptor *)field {
  10002. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10003. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10004. for (int i = 0; i < 2; ++i) {
  10005. if (_valueSet[i]) {
  10006. // Write the tag.
  10007. [outputStream writeInt32NoTag:tag];
  10008. // Write the size of the message.
  10009. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10010. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10011. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10012. // Write the fields.
  10013. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10014. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10015. }
  10016. }
  10017. }
  10018. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10019. if (otherDictionary) {
  10020. for (int i = 0; i < 2; ++i) {
  10021. if (otherDictionary->_valueSet[i]) {
  10022. _valueSet[i] = YES;
  10023. _values[i] = otherDictionary->_values[i];
  10024. }
  10025. }
  10026. if (_autocreator) {
  10027. GPBAutocreatedDictionaryModified(_autocreator, self);
  10028. }
  10029. }
  10030. }
  10031. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  10032. int idx = (key ? 1 : 0);
  10033. _values[idx] = value;
  10034. _valueSet[idx] = YES;
  10035. if (_autocreator) {
  10036. GPBAutocreatedDictionaryModified(_autocreator, self);
  10037. }
  10038. }
  10039. - (void)removeValueForKey:(BOOL)aKey {
  10040. _valueSet[aKey ? 1 : 0] = NO;
  10041. }
  10042. - (void)removeAll {
  10043. _valueSet[0] = NO;
  10044. _valueSet[1] = NO;
  10045. }
  10046. @end
  10047. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10048. // This block of code is generated, do not edit it directly.
  10049. #pragma mark - Bool -> UInt64
  10050. @implementation GPBBoolUInt64Dictionary {
  10051. @package
  10052. uint64_t _values[2];
  10053. BOOL _valueSet[2];
  10054. }
  10055. + (instancetype)dictionary {
  10056. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10057. }
  10058. + (instancetype)dictionaryWithValue:(uint64_t)value
  10059. forKey:(BOOL)key {
  10060. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10061. // on to get the type correct.
  10062. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  10063. forKeys:&key
  10064. count:1] autorelease];
  10065. }
  10066. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  10067. forKeys:(const BOOL [])keys
  10068. count:(NSUInteger)count {
  10069. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10070. // on to get the type correct.
  10071. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  10072. forKeys:keys
  10073. count:count] autorelease];
  10074. }
  10075. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10076. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10077. // on to get the type correct.
  10078. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10079. }
  10080. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10081. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10082. }
  10083. - (instancetype)init {
  10084. return [self initWithValues:NULL forKeys:NULL count:0];
  10085. }
  10086. - (instancetype)initWithValues:(const uint64_t [])values
  10087. forKeys:(const BOOL [])keys
  10088. count:(NSUInteger)count {
  10089. self = [super init];
  10090. if (self) {
  10091. for (NSUInteger i = 0; i < count; ++i) {
  10092. int idx = keys[i] ? 1 : 0;
  10093. _values[idx] = values[i];
  10094. _valueSet[idx] = YES;
  10095. }
  10096. }
  10097. return self;
  10098. }
  10099. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10100. self = [self initWithValues:NULL forKeys:NULL count:0];
  10101. if (self) {
  10102. if (dictionary) {
  10103. for (int i = 0; i < 2; ++i) {
  10104. if (dictionary->_valueSet[i]) {
  10105. _values[i] = dictionary->_values[i];
  10106. _valueSet[i] = YES;
  10107. }
  10108. }
  10109. }
  10110. }
  10111. return self;
  10112. }
  10113. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10114. #pragma unused(numItems)
  10115. return [self initWithValues:NULL forKeys:NULL count:0];
  10116. }
  10117. #if !defined(NS_BLOCK_ASSERTIONS)
  10118. - (void)dealloc {
  10119. NSAssert(!_autocreator,
  10120. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10121. [self class], _autocreator);
  10122. [super dealloc];
  10123. }
  10124. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10125. - (instancetype)copyWithZone:(NSZone *)zone {
  10126. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10127. }
  10128. - (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
  10129. if (self == other) {
  10130. return YES;
  10131. }
  10132. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10133. return NO;
  10134. }
  10135. if ((_valueSet[0] != other->_valueSet[0]) ||
  10136. (_valueSet[1] != other->_valueSet[1])) {
  10137. return NO;
  10138. }
  10139. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10140. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10141. return NO;
  10142. }
  10143. return YES;
  10144. }
  10145. - (NSUInteger)hash {
  10146. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10147. }
  10148. - (NSString *)description {
  10149. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10150. if (_valueSet[0]) {
  10151. [result appendFormat:@"NO: %llu", _values[0]];
  10152. }
  10153. if (_valueSet[1]) {
  10154. [result appendFormat:@"YES: %llu", _values[1]];
  10155. }
  10156. [result appendString:@" }"];
  10157. return result;
  10158. }
  10159. - (NSUInteger)count {
  10160. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10161. }
  10162. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  10163. int idx = (key ? 1 : 0);
  10164. if (_valueSet[idx]) {
  10165. if (value) {
  10166. *value = _values[idx];
  10167. }
  10168. return YES;
  10169. }
  10170. return NO;
  10171. }
  10172. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10173. forGPBGenericValueKey:(GPBGenericValue *)key {
  10174. int idx = (key->valueBool ? 1 : 0);
  10175. _values[idx] = value->valueUInt64;
  10176. _valueSet[idx] = YES;
  10177. }
  10178. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10179. if (_valueSet[0]) {
  10180. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10181. }
  10182. if (_valueSet[1]) {
  10183. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10184. }
  10185. }
  10186. - (void)enumerateKeysAndValuesUsingBlock:
  10187. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10188. BOOL stop = NO;
  10189. if (_valueSet[0]) {
  10190. block(NO, _values[0], &stop);
  10191. }
  10192. if (!stop && _valueSet[1]) {
  10193. block(YES, _values[1], &stop);
  10194. }
  10195. }
  10196. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10197. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10198. NSUInteger count = 0;
  10199. size_t result = 0;
  10200. for (int i = 0; i < 2; ++i) {
  10201. if (_valueSet[i]) {
  10202. ++count;
  10203. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10204. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10205. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10206. }
  10207. }
  10208. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10209. result += tagSize * count;
  10210. return result;
  10211. }
  10212. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10213. asField:(GPBFieldDescriptor *)field {
  10214. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10215. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10216. for (int i = 0; i < 2; ++i) {
  10217. if (_valueSet[i]) {
  10218. // Write the tag.
  10219. [outputStream writeInt32NoTag:tag];
  10220. // Write the size of the message.
  10221. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10222. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10223. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10224. // Write the fields.
  10225. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10226. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10227. }
  10228. }
  10229. }
  10230. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10231. if (otherDictionary) {
  10232. for (int i = 0; i < 2; ++i) {
  10233. if (otherDictionary->_valueSet[i]) {
  10234. _valueSet[i] = YES;
  10235. _values[i] = otherDictionary->_values[i];
  10236. }
  10237. }
  10238. if (_autocreator) {
  10239. GPBAutocreatedDictionaryModified(_autocreator, self);
  10240. }
  10241. }
  10242. }
  10243. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  10244. int idx = (key ? 1 : 0);
  10245. _values[idx] = value;
  10246. _valueSet[idx] = YES;
  10247. if (_autocreator) {
  10248. GPBAutocreatedDictionaryModified(_autocreator, self);
  10249. }
  10250. }
  10251. - (void)removeValueForKey:(BOOL)aKey {
  10252. _valueSet[aKey ? 1 : 0] = NO;
  10253. }
  10254. - (void)removeAll {
  10255. _valueSet[0] = NO;
  10256. _valueSet[1] = NO;
  10257. }
  10258. @end
  10259. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10260. // This block of code is generated, do not edit it directly.
  10261. #pragma mark - Bool -> Int64
  10262. @implementation GPBBoolInt64Dictionary {
  10263. @package
  10264. int64_t _values[2];
  10265. BOOL _valueSet[2];
  10266. }
  10267. + (instancetype)dictionary {
  10268. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10269. }
  10270. + (instancetype)dictionaryWithValue:(int64_t)value
  10271. forKey:(BOOL)key {
  10272. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10273. // on to get the type correct.
  10274. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  10275. forKeys:&key
  10276. count:1] autorelease];
  10277. }
  10278. + (instancetype)dictionaryWithValues:(const int64_t [])values
  10279. forKeys:(const BOOL [])keys
  10280. count:(NSUInteger)count {
  10281. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10282. // on to get the type correct.
  10283. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  10284. forKeys:keys
  10285. count:count] autorelease];
  10286. }
  10287. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10288. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10289. // on to get the type correct.
  10290. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10291. }
  10292. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10293. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10294. }
  10295. - (instancetype)init {
  10296. return [self initWithValues:NULL forKeys:NULL count:0];
  10297. }
  10298. - (instancetype)initWithValues:(const int64_t [])values
  10299. forKeys:(const BOOL [])keys
  10300. count:(NSUInteger)count {
  10301. self = [super init];
  10302. if (self) {
  10303. for (NSUInteger i = 0; i < count; ++i) {
  10304. int idx = keys[i] ? 1 : 0;
  10305. _values[idx] = values[i];
  10306. _valueSet[idx] = YES;
  10307. }
  10308. }
  10309. return self;
  10310. }
  10311. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10312. self = [self initWithValues:NULL forKeys:NULL count:0];
  10313. if (self) {
  10314. if (dictionary) {
  10315. for (int i = 0; i < 2; ++i) {
  10316. if (dictionary->_valueSet[i]) {
  10317. _values[i] = dictionary->_values[i];
  10318. _valueSet[i] = YES;
  10319. }
  10320. }
  10321. }
  10322. }
  10323. return self;
  10324. }
  10325. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10326. #pragma unused(numItems)
  10327. return [self initWithValues:NULL forKeys:NULL count:0];
  10328. }
  10329. #if !defined(NS_BLOCK_ASSERTIONS)
  10330. - (void)dealloc {
  10331. NSAssert(!_autocreator,
  10332. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10333. [self class], _autocreator);
  10334. [super dealloc];
  10335. }
  10336. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10337. - (instancetype)copyWithZone:(NSZone *)zone {
  10338. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10339. }
  10340. - (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
  10341. if (self == other) {
  10342. return YES;
  10343. }
  10344. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10345. return NO;
  10346. }
  10347. if ((_valueSet[0] != other->_valueSet[0]) ||
  10348. (_valueSet[1] != other->_valueSet[1])) {
  10349. return NO;
  10350. }
  10351. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10352. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10353. return NO;
  10354. }
  10355. return YES;
  10356. }
  10357. - (NSUInteger)hash {
  10358. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10359. }
  10360. - (NSString *)description {
  10361. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10362. if (_valueSet[0]) {
  10363. [result appendFormat:@"NO: %lld", _values[0]];
  10364. }
  10365. if (_valueSet[1]) {
  10366. [result appendFormat:@"YES: %lld", _values[1]];
  10367. }
  10368. [result appendString:@" }"];
  10369. return result;
  10370. }
  10371. - (NSUInteger)count {
  10372. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10373. }
  10374. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  10375. int idx = (key ? 1 : 0);
  10376. if (_valueSet[idx]) {
  10377. if (value) {
  10378. *value = _values[idx];
  10379. }
  10380. return YES;
  10381. }
  10382. return NO;
  10383. }
  10384. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10385. forGPBGenericValueKey:(GPBGenericValue *)key {
  10386. int idx = (key->valueBool ? 1 : 0);
  10387. _values[idx] = value->valueInt64;
  10388. _valueSet[idx] = YES;
  10389. }
  10390. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10391. if (_valueSet[0]) {
  10392. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10393. }
  10394. if (_valueSet[1]) {
  10395. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10396. }
  10397. }
  10398. - (void)enumerateKeysAndValuesUsingBlock:
  10399. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10400. BOOL stop = NO;
  10401. if (_valueSet[0]) {
  10402. block(NO, _values[0], &stop);
  10403. }
  10404. if (!stop && _valueSet[1]) {
  10405. block(YES, _values[1], &stop);
  10406. }
  10407. }
  10408. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10409. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10410. NSUInteger count = 0;
  10411. size_t result = 0;
  10412. for (int i = 0; i < 2; ++i) {
  10413. if (_valueSet[i]) {
  10414. ++count;
  10415. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10416. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10417. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10418. }
  10419. }
  10420. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10421. result += tagSize * count;
  10422. return result;
  10423. }
  10424. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10425. asField:(GPBFieldDescriptor *)field {
  10426. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10427. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10428. for (int i = 0; i < 2; ++i) {
  10429. if (_valueSet[i]) {
  10430. // Write the tag.
  10431. [outputStream writeInt32NoTag:tag];
  10432. // Write the size of the message.
  10433. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10434. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10435. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10436. // Write the fields.
  10437. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10438. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10439. }
  10440. }
  10441. }
  10442. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10443. if (otherDictionary) {
  10444. for (int i = 0; i < 2; ++i) {
  10445. if (otherDictionary->_valueSet[i]) {
  10446. _valueSet[i] = YES;
  10447. _values[i] = otherDictionary->_values[i];
  10448. }
  10449. }
  10450. if (_autocreator) {
  10451. GPBAutocreatedDictionaryModified(_autocreator, self);
  10452. }
  10453. }
  10454. }
  10455. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  10456. int idx = (key ? 1 : 0);
  10457. _values[idx] = value;
  10458. _valueSet[idx] = YES;
  10459. if (_autocreator) {
  10460. GPBAutocreatedDictionaryModified(_autocreator, self);
  10461. }
  10462. }
  10463. - (void)removeValueForKey:(BOOL)aKey {
  10464. _valueSet[aKey ? 1 : 0] = NO;
  10465. }
  10466. - (void)removeAll {
  10467. _valueSet[0] = NO;
  10468. _valueSet[1] = NO;
  10469. }
  10470. @end
  10471. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10472. // This block of code is generated, do not edit it directly.
  10473. #pragma mark - Bool -> Bool
  10474. @implementation GPBBoolBoolDictionary {
  10475. @package
  10476. BOOL _values[2];
  10477. BOOL _valueSet[2];
  10478. }
  10479. + (instancetype)dictionary {
  10480. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10481. }
  10482. + (instancetype)dictionaryWithValue:(BOOL)value
  10483. forKey:(BOOL)key {
  10484. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10485. // on to get the type correct.
  10486. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  10487. forKeys:&key
  10488. count:1] autorelease];
  10489. }
  10490. + (instancetype)dictionaryWithValues:(const BOOL [])values
  10491. forKeys:(const BOOL [])keys
  10492. count:(NSUInteger)count {
  10493. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10494. // on to get the type correct.
  10495. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  10496. forKeys:keys
  10497. count:count] autorelease];
  10498. }
  10499. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10500. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10501. // on to get the type correct.
  10502. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10503. }
  10504. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10505. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10506. }
  10507. - (instancetype)init {
  10508. return [self initWithValues:NULL forKeys:NULL count:0];
  10509. }
  10510. - (instancetype)initWithValues:(const BOOL [])values
  10511. forKeys:(const BOOL [])keys
  10512. count:(NSUInteger)count {
  10513. self = [super init];
  10514. if (self) {
  10515. for (NSUInteger i = 0; i < count; ++i) {
  10516. int idx = keys[i] ? 1 : 0;
  10517. _values[idx] = values[i];
  10518. _valueSet[idx] = YES;
  10519. }
  10520. }
  10521. return self;
  10522. }
  10523. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10524. self = [self initWithValues:NULL forKeys:NULL count:0];
  10525. if (self) {
  10526. if (dictionary) {
  10527. for (int i = 0; i < 2; ++i) {
  10528. if (dictionary->_valueSet[i]) {
  10529. _values[i] = dictionary->_values[i];
  10530. _valueSet[i] = YES;
  10531. }
  10532. }
  10533. }
  10534. }
  10535. return self;
  10536. }
  10537. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10538. #pragma unused(numItems)
  10539. return [self initWithValues:NULL forKeys:NULL count:0];
  10540. }
  10541. #if !defined(NS_BLOCK_ASSERTIONS)
  10542. - (void)dealloc {
  10543. NSAssert(!_autocreator,
  10544. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10545. [self class], _autocreator);
  10546. [super dealloc];
  10547. }
  10548. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10549. - (instancetype)copyWithZone:(NSZone *)zone {
  10550. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10551. }
  10552. - (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
  10553. if (self == other) {
  10554. return YES;
  10555. }
  10556. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10557. return NO;
  10558. }
  10559. if ((_valueSet[0] != other->_valueSet[0]) ||
  10560. (_valueSet[1] != other->_valueSet[1])) {
  10561. return NO;
  10562. }
  10563. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10564. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10565. return NO;
  10566. }
  10567. return YES;
  10568. }
  10569. - (NSUInteger)hash {
  10570. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10571. }
  10572. - (NSString *)description {
  10573. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10574. if (_valueSet[0]) {
  10575. [result appendFormat:@"NO: %d", _values[0]];
  10576. }
  10577. if (_valueSet[1]) {
  10578. [result appendFormat:@"YES: %d", _values[1]];
  10579. }
  10580. [result appendString:@" }"];
  10581. return result;
  10582. }
  10583. - (NSUInteger)count {
  10584. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10585. }
  10586. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10587. int idx = (key ? 1 : 0);
  10588. if (_valueSet[idx]) {
  10589. if (value) {
  10590. *value = _values[idx];
  10591. }
  10592. return YES;
  10593. }
  10594. return NO;
  10595. }
  10596. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10597. forGPBGenericValueKey:(GPBGenericValue *)key {
  10598. int idx = (key->valueBool ? 1 : 0);
  10599. _values[idx] = value->valueBool;
  10600. _valueSet[idx] = YES;
  10601. }
  10602. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10603. if (_valueSet[0]) {
  10604. block(@"false", (_values[0] ? @"true" : @"false"));
  10605. }
  10606. if (_valueSet[1]) {
  10607. block(@"true", (_values[1] ? @"true" : @"false"));
  10608. }
  10609. }
  10610. - (void)enumerateKeysAndValuesUsingBlock:
  10611. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10612. BOOL stop = NO;
  10613. if (_valueSet[0]) {
  10614. block(NO, _values[0], &stop);
  10615. }
  10616. if (!stop && _valueSet[1]) {
  10617. block(YES, _values[1], &stop);
  10618. }
  10619. }
  10620. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10621. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10622. NSUInteger count = 0;
  10623. size_t result = 0;
  10624. for (int i = 0; i < 2; ++i) {
  10625. if (_valueSet[i]) {
  10626. ++count;
  10627. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10628. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10629. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10630. }
  10631. }
  10632. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10633. result += tagSize * count;
  10634. return result;
  10635. }
  10636. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10637. asField:(GPBFieldDescriptor *)field {
  10638. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10639. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10640. for (int i = 0; i < 2; ++i) {
  10641. if (_valueSet[i]) {
  10642. // Write the tag.
  10643. [outputStream writeInt32NoTag:tag];
  10644. // Write the size of the message.
  10645. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10646. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10647. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10648. // Write the fields.
  10649. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10650. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10651. }
  10652. }
  10653. }
  10654. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10655. if (otherDictionary) {
  10656. for (int i = 0; i < 2; ++i) {
  10657. if (otherDictionary->_valueSet[i]) {
  10658. _valueSet[i] = YES;
  10659. _values[i] = otherDictionary->_values[i];
  10660. }
  10661. }
  10662. if (_autocreator) {
  10663. GPBAutocreatedDictionaryModified(_autocreator, self);
  10664. }
  10665. }
  10666. }
  10667. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10668. int idx = (key ? 1 : 0);
  10669. _values[idx] = value;
  10670. _valueSet[idx] = YES;
  10671. if (_autocreator) {
  10672. GPBAutocreatedDictionaryModified(_autocreator, self);
  10673. }
  10674. }
  10675. - (void)removeValueForKey:(BOOL)aKey {
  10676. _valueSet[aKey ? 1 : 0] = NO;
  10677. }
  10678. - (void)removeAll {
  10679. _valueSet[0] = NO;
  10680. _valueSet[1] = NO;
  10681. }
  10682. @end
  10683. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10684. // This block of code is generated, do not edit it directly.
  10685. #pragma mark - Bool -> Float
  10686. @implementation GPBBoolFloatDictionary {
  10687. @package
  10688. float _values[2];
  10689. BOOL _valueSet[2];
  10690. }
  10691. + (instancetype)dictionary {
  10692. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10693. }
  10694. + (instancetype)dictionaryWithValue:(float)value
  10695. forKey:(BOOL)key {
  10696. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10697. // on to get the type correct.
  10698. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10699. forKeys:&key
  10700. count:1] autorelease];
  10701. }
  10702. + (instancetype)dictionaryWithValues:(const float [])values
  10703. forKeys:(const BOOL [])keys
  10704. count:(NSUInteger)count {
  10705. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10706. // on to get the type correct.
  10707. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10708. forKeys:keys
  10709. count:count] autorelease];
  10710. }
  10711. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10712. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10713. // on to get the type correct.
  10714. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10715. }
  10716. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10717. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10718. }
  10719. - (instancetype)init {
  10720. return [self initWithValues:NULL forKeys:NULL count:0];
  10721. }
  10722. - (instancetype)initWithValues:(const float [])values
  10723. forKeys:(const BOOL [])keys
  10724. count:(NSUInteger)count {
  10725. self = [super init];
  10726. if (self) {
  10727. for (NSUInteger i = 0; i < count; ++i) {
  10728. int idx = keys[i] ? 1 : 0;
  10729. _values[idx] = values[i];
  10730. _valueSet[idx] = YES;
  10731. }
  10732. }
  10733. return self;
  10734. }
  10735. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10736. self = [self initWithValues:NULL forKeys:NULL count:0];
  10737. if (self) {
  10738. if (dictionary) {
  10739. for (int i = 0; i < 2; ++i) {
  10740. if (dictionary->_valueSet[i]) {
  10741. _values[i] = dictionary->_values[i];
  10742. _valueSet[i] = YES;
  10743. }
  10744. }
  10745. }
  10746. }
  10747. return self;
  10748. }
  10749. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10750. #pragma unused(numItems)
  10751. return [self initWithValues:NULL forKeys:NULL count:0];
  10752. }
  10753. #if !defined(NS_BLOCK_ASSERTIONS)
  10754. - (void)dealloc {
  10755. NSAssert(!_autocreator,
  10756. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10757. [self class], _autocreator);
  10758. [super dealloc];
  10759. }
  10760. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10761. - (instancetype)copyWithZone:(NSZone *)zone {
  10762. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10763. }
  10764. - (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
  10765. if (self == other) {
  10766. return YES;
  10767. }
  10768. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10769. return NO;
  10770. }
  10771. if ((_valueSet[0] != other->_valueSet[0]) ||
  10772. (_valueSet[1] != other->_valueSet[1])) {
  10773. return NO;
  10774. }
  10775. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10776. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10777. return NO;
  10778. }
  10779. return YES;
  10780. }
  10781. - (NSUInteger)hash {
  10782. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10783. }
  10784. - (NSString *)description {
  10785. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10786. if (_valueSet[0]) {
  10787. [result appendFormat:@"NO: %f", _values[0]];
  10788. }
  10789. if (_valueSet[1]) {
  10790. [result appendFormat:@"YES: %f", _values[1]];
  10791. }
  10792. [result appendString:@" }"];
  10793. return result;
  10794. }
  10795. - (NSUInteger)count {
  10796. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10797. }
  10798. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  10799. int idx = (key ? 1 : 0);
  10800. if (_valueSet[idx]) {
  10801. if (value) {
  10802. *value = _values[idx];
  10803. }
  10804. return YES;
  10805. }
  10806. return NO;
  10807. }
  10808. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10809. forGPBGenericValueKey:(GPBGenericValue *)key {
  10810. int idx = (key->valueBool ? 1 : 0);
  10811. _values[idx] = value->valueFloat;
  10812. _valueSet[idx] = YES;
  10813. }
  10814. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10815. if (_valueSet[0]) {
  10816. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  10817. }
  10818. if (_valueSet[1]) {
  10819. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  10820. }
  10821. }
  10822. - (void)enumerateKeysAndValuesUsingBlock:
  10823. (void (^)(BOOL key, float value, BOOL *stop))block {
  10824. BOOL stop = NO;
  10825. if (_valueSet[0]) {
  10826. block(NO, _values[0], &stop);
  10827. }
  10828. if (!stop && _valueSet[1]) {
  10829. block(YES, _values[1], &stop);
  10830. }
  10831. }
  10832. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10833. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10834. NSUInteger count = 0;
  10835. size_t result = 0;
  10836. for (int i = 0; i < 2; ++i) {
  10837. if (_valueSet[i]) {
  10838. ++count;
  10839. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10840. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10841. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10842. }
  10843. }
  10844. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10845. result += tagSize * count;
  10846. return result;
  10847. }
  10848. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10849. asField:(GPBFieldDescriptor *)field {
  10850. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10851. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10852. for (int i = 0; i < 2; ++i) {
  10853. if (_valueSet[i]) {
  10854. // Write the tag.
  10855. [outputStream writeInt32NoTag:tag];
  10856. // Write the size of the message.
  10857. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10858. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10859. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10860. // Write the fields.
  10861. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10862. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10863. }
  10864. }
  10865. }
  10866. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  10867. if (otherDictionary) {
  10868. for (int i = 0; i < 2; ++i) {
  10869. if (otherDictionary->_valueSet[i]) {
  10870. _valueSet[i] = YES;
  10871. _values[i] = otherDictionary->_values[i];
  10872. }
  10873. }
  10874. if (_autocreator) {
  10875. GPBAutocreatedDictionaryModified(_autocreator, self);
  10876. }
  10877. }
  10878. }
  10879. - (void)setValue:(float)value forKey:(BOOL)key {
  10880. int idx = (key ? 1 : 0);
  10881. _values[idx] = value;
  10882. _valueSet[idx] = YES;
  10883. if (_autocreator) {
  10884. GPBAutocreatedDictionaryModified(_autocreator, self);
  10885. }
  10886. }
  10887. - (void)removeValueForKey:(BOOL)aKey {
  10888. _valueSet[aKey ? 1 : 0] = NO;
  10889. }
  10890. - (void)removeAll {
  10891. _valueSet[0] = NO;
  10892. _valueSet[1] = NO;
  10893. }
  10894. @end
  10895. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  10896. // This block of code is generated, do not edit it directly.
  10897. #pragma mark - Bool -> Double
  10898. @implementation GPBBoolDoubleDictionary {
  10899. @package
  10900. double _values[2];
  10901. BOOL _valueSet[2];
  10902. }
  10903. + (instancetype)dictionary {
  10904. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10905. }
  10906. + (instancetype)dictionaryWithValue:(double)value
  10907. forKey:(BOOL)key {
  10908. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10909. // on to get the type correct.
  10910. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  10911. forKeys:&key
  10912. count:1] autorelease];
  10913. }
  10914. + (instancetype)dictionaryWithValues:(const double [])values
  10915. forKeys:(const BOOL [])keys
  10916. count:(NSUInteger)count {
  10917. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10918. // on to get the type correct.
  10919. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  10920. forKeys:keys
  10921. count:count] autorelease];
  10922. }
  10923. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10924. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10925. // on to get the type correct.
  10926. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10927. }
  10928. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10929. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10930. }
  10931. - (instancetype)init {
  10932. return [self initWithValues:NULL forKeys:NULL count:0];
  10933. }
  10934. - (instancetype)initWithValues:(const double [])values
  10935. forKeys:(const BOOL [])keys
  10936. count:(NSUInteger)count {
  10937. self = [super init];
  10938. if (self) {
  10939. for (NSUInteger i = 0; i < count; ++i) {
  10940. int idx = keys[i] ? 1 : 0;
  10941. _values[idx] = values[i];
  10942. _valueSet[idx] = YES;
  10943. }
  10944. }
  10945. return self;
  10946. }
  10947. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10948. self = [self initWithValues:NULL forKeys:NULL count:0];
  10949. if (self) {
  10950. if (dictionary) {
  10951. for (int i = 0; i < 2; ++i) {
  10952. if (dictionary->_valueSet[i]) {
  10953. _values[i] = dictionary->_values[i];
  10954. _valueSet[i] = YES;
  10955. }
  10956. }
  10957. }
  10958. }
  10959. return self;
  10960. }
  10961. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10962. #pragma unused(numItems)
  10963. return [self initWithValues:NULL forKeys:NULL count:0];
  10964. }
  10965. #if !defined(NS_BLOCK_ASSERTIONS)
  10966. - (void)dealloc {
  10967. NSAssert(!_autocreator,
  10968. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10969. [self class], _autocreator);
  10970. [super dealloc];
  10971. }
  10972. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10973. - (instancetype)copyWithZone:(NSZone *)zone {
  10974. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  10975. }
  10976. - (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
  10977. if (self == other) {
  10978. return YES;
  10979. }
  10980. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  10981. return NO;
  10982. }
  10983. if ((_valueSet[0] != other->_valueSet[0]) ||
  10984. (_valueSet[1] != other->_valueSet[1])) {
  10985. return NO;
  10986. }
  10987. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10988. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10989. return NO;
  10990. }
  10991. return YES;
  10992. }
  10993. - (NSUInteger)hash {
  10994. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10995. }
  10996. - (NSString *)description {
  10997. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10998. if (_valueSet[0]) {
  10999. [result appendFormat:@"NO: %lf", _values[0]];
  11000. }
  11001. if (_valueSet[1]) {
  11002. [result appendFormat:@"YES: %lf", _values[1]];
  11003. }
  11004. [result appendString:@" }"];
  11005. return result;
  11006. }
  11007. - (NSUInteger)count {
  11008. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11009. }
  11010. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  11011. int idx = (key ? 1 : 0);
  11012. if (_valueSet[idx]) {
  11013. if (value) {
  11014. *value = _values[idx];
  11015. }
  11016. return YES;
  11017. }
  11018. return NO;
  11019. }
  11020. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11021. forGPBGenericValueKey:(GPBGenericValue *)key {
  11022. int idx = (key->valueBool ? 1 : 0);
  11023. _values[idx] = value->valueDouble;
  11024. _valueSet[idx] = YES;
  11025. }
  11026. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11027. if (_valueSet[0]) {
  11028. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11029. }
  11030. if (_valueSet[1]) {
  11031. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11032. }
  11033. }
  11034. - (void)enumerateKeysAndValuesUsingBlock:
  11035. (void (^)(BOOL key, double value, BOOL *stop))block {
  11036. BOOL stop = NO;
  11037. if (_valueSet[0]) {
  11038. block(NO, _values[0], &stop);
  11039. }
  11040. if (!stop && _valueSet[1]) {
  11041. block(YES, _values[1], &stop);
  11042. }
  11043. }
  11044. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11045. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11046. NSUInteger count = 0;
  11047. size_t result = 0;
  11048. for (int i = 0; i < 2; ++i) {
  11049. if (_valueSet[i]) {
  11050. ++count;
  11051. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11052. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11053. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11054. }
  11055. }
  11056. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11057. result += tagSize * count;
  11058. return result;
  11059. }
  11060. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11061. asField:(GPBFieldDescriptor *)field {
  11062. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11063. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11064. for (int i = 0; i < 2; ++i) {
  11065. if (_valueSet[i]) {
  11066. // Write the tag.
  11067. [outputStream writeInt32NoTag:tag];
  11068. // Write the size of the message.
  11069. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11070. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11071. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11072. // Write the fields.
  11073. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11074. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11075. }
  11076. }
  11077. }
  11078. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11079. if (otherDictionary) {
  11080. for (int i = 0; i < 2; ++i) {
  11081. if (otherDictionary->_valueSet[i]) {
  11082. _valueSet[i] = YES;
  11083. _values[i] = otherDictionary->_values[i];
  11084. }
  11085. }
  11086. if (_autocreator) {
  11087. GPBAutocreatedDictionaryModified(_autocreator, self);
  11088. }
  11089. }
  11090. }
  11091. - (void)setValue:(double)value forKey:(BOOL)key {
  11092. int idx = (key ? 1 : 0);
  11093. _values[idx] = value;
  11094. _valueSet[idx] = YES;
  11095. if (_autocreator) {
  11096. GPBAutocreatedDictionaryModified(_autocreator, self);
  11097. }
  11098. }
  11099. - (void)removeValueForKey:(BOOL)aKey {
  11100. _valueSet[aKey ? 1 : 0] = NO;
  11101. }
  11102. - (void)removeAll {
  11103. _valueSet[0] = NO;
  11104. _valueSet[1] = NO;
  11105. }
  11106. @end
  11107. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11108. // This block of code is generated, do not edit it directly.
  11109. #pragma mark - Bool -> Object
  11110. @implementation GPBBoolObjectDictionary {
  11111. @package
  11112. id _values[2];
  11113. }
  11114. + (instancetype)dictionary {
  11115. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  11116. }
  11117. + (instancetype)dictionaryWithValue:(id)value
  11118. forKey:(BOOL)key {
  11119. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11120. // on to get the type correct.
  11121. return [[(GPBBoolObjectDictionary*)[self alloc] initWithValues:&value
  11122. forKeys:&key
  11123. count:1] autorelease];
  11124. }
  11125. + (instancetype)dictionaryWithValues:(const id [])values
  11126. forKeys:(const BOOL [])keys
  11127. count:(NSUInteger)count {
  11128. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11129. // on to get the type correct.
  11130. return [[(GPBBoolObjectDictionary*)[self alloc] initWithValues:values
  11131. forKeys:keys
  11132. count:count] autorelease];
  11133. }
  11134. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11135. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11136. // on to get the type correct.
  11137. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11138. }
  11139. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11140. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11141. }
  11142. - (instancetype)init {
  11143. return [self initWithValues:NULL forKeys:NULL count:0];
  11144. }
  11145. - (instancetype)initWithValues:(const id [])values
  11146. forKeys:(const BOOL [])keys
  11147. count:(NSUInteger)count {
  11148. self = [super init];
  11149. if (self) {
  11150. for (NSUInteger i = 0; i < count; ++i) {
  11151. int idx = keys[i] ? 1 : 0;
  11152. [_values[idx] release];
  11153. _values[idx] = (id)[values[i] retain];
  11154. }
  11155. }
  11156. return self;
  11157. }
  11158. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11159. self = [self initWithValues:NULL forKeys:NULL count:0];
  11160. if (self) {
  11161. if (dictionary) {
  11162. _values[0] = [dictionary->_values[0] retain];
  11163. _values[1] = [dictionary->_values[1] retain];
  11164. }
  11165. }
  11166. return self;
  11167. }
  11168. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11169. #pragma unused(numItems)
  11170. return [self initWithValues:NULL forKeys:NULL count:0];
  11171. }
  11172. - (void)dealloc {
  11173. NSAssert(!_autocreator,
  11174. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11175. [self class], _autocreator);
  11176. [_values[0] release];
  11177. [_values[1] release];
  11178. [super dealloc];
  11179. }
  11180. - (instancetype)copyWithZone:(NSZone *)zone {
  11181. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11182. }
  11183. - (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
  11184. if (self == other) {
  11185. return YES;
  11186. }
  11187. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11188. return NO;
  11189. }
  11190. if (((_values[0] != nil) != (other->_values[0] != nil)) ||
  11191. ((_values[1] != nil) != (other->_values[1] != nil))) {
  11192. return NO;
  11193. }
  11194. if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
  11195. ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
  11196. return NO;
  11197. }
  11198. return YES;
  11199. }
  11200. - (NSUInteger)hash {
  11201. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11202. }
  11203. - (NSString *)description {
  11204. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11205. if ((_values[0] != nil)) {
  11206. [result appendFormat:@"NO: %@", _values[0]];
  11207. }
  11208. if ((_values[1] != nil)) {
  11209. [result appendFormat:@"YES: %@", _values[1]];
  11210. }
  11211. [result appendString:@" }"];
  11212. return result;
  11213. }
  11214. - (NSUInteger)count {
  11215. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11216. }
  11217. - (id)valueForKey:(BOOL)key {
  11218. return _values[key ? 1 : 0];
  11219. }
  11220. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11221. forGPBGenericValueKey:(GPBGenericValue *)key {
  11222. int idx = (key->valueBool ? 1 : 0);
  11223. [_values[idx] release];
  11224. _values[idx] = [value->valueString retain];
  11225. }
  11226. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11227. if (_values[0] != nil) {
  11228. block(@"false", _values[0]);
  11229. }
  11230. if ((_values[1] != nil)) {
  11231. block(@"true", _values[1]);
  11232. }
  11233. }
  11234. - (void)enumerateKeysAndValuesUsingBlock:
  11235. (void (^)(BOOL key, id value, BOOL *stop))block {
  11236. BOOL stop = NO;
  11237. if (_values[0] != nil) {
  11238. block(NO, _values[0], &stop);
  11239. }
  11240. if (!stop && (_values[1] != nil)) {
  11241. block(YES, _values[1], &stop);
  11242. }
  11243. }
  11244. - (BOOL)isInitialized {
  11245. if (_values[0] && ![_values[0] isInitialized]) {
  11246. return NO;
  11247. }
  11248. if (_values[1] && ![_values[1] isInitialized]) {
  11249. return NO;
  11250. }
  11251. return YES;
  11252. }
  11253. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11254. GPBBoolObjectDictionary *newDict =
  11255. [[GPBBoolObjectDictionary alloc] init];
  11256. for (int i = 0; i < 2; ++i) {
  11257. if (_values[i] != nil) {
  11258. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11259. }
  11260. }
  11261. return newDict;
  11262. }
  11263. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11264. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11265. NSUInteger count = 0;
  11266. size_t result = 0;
  11267. for (int i = 0; i < 2; ++i) {
  11268. if (_values[i] != nil) {
  11269. ++count;
  11270. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11271. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11272. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11273. }
  11274. }
  11275. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11276. result += tagSize * count;
  11277. return result;
  11278. }
  11279. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11280. asField:(GPBFieldDescriptor *)field {
  11281. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11282. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11283. for (int i = 0; i < 2; ++i) {
  11284. if (_values[i] != nil) {
  11285. // Write the tag.
  11286. [outputStream writeInt32NoTag:tag];
  11287. // Write the size of the message.
  11288. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11289. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11290. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11291. // Write the fields.
  11292. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11293. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11294. }
  11295. }
  11296. }
  11297. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11298. if (otherDictionary) {
  11299. for (int i = 0; i < 2; ++i) {
  11300. if (otherDictionary->_values[i] != nil) {
  11301. [_values[i] release];
  11302. _values[i] = [otherDictionary->_values[i] retain];
  11303. }
  11304. }
  11305. if (_autocreator) {
  11306. GPBAutocreatedDictionaryModified(_autocreator, self);
  11307. }
  11308. }
  11309. }
  11310. - (void)setValue:(id)value forKey:(BOOL)key {
  11311. int idx = (key ? 1 : 0);
  11312. [_values[idx] release];
  11313. _values[idx] = [value retain];
  11314. if (_autocreator) {
  11315. GPBAutocreatedDictionaryModified(_autocreator, self);
  11316. }
  11317. }
  11318. - (void)removeValueForKey:(BOOL)aKey {
  11319. int idx = (aKey ? 1 : 0);
  11320. [_values[idx] release];
  11321. _values[idx] = nil;
  11322. }
  11323. - (void)removeAll {
  11324. for (int i = 0; i < 2; ++i) {
  11325. [_values[i] release];
  11326. _values[i] = nil;
  11327. }
  11328. }
  11329. @end
  11330. //%PDDM-EXPAND-END (8 expansions)
  11331. #pragma mark - Bool -> Enum
  11332. @implementation GPBBoolEnumDictionary {
  11333. @package
  11334. GPBEnumValidationFunc _validationFunc;
  11335. int32_t _values[2];
  11336. BOOL _valueSet[2];
  11337. }
  11338. @synthesize validationFunc = _validationFunc;
  11339. + (instancetype)dictionary {
  11340. return [[[self alloc] initWithValidationFunction:NULL
  11341. rawValues:NULL
  11342. forKeys:NULL
  11343. count:0] autorelease];
  11344. }
  11345. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11346. return [[[self alloc] initWithValidationFunction:func
  11347. rawValues:NULL
  11348. forKeys:NULL
  11349. count:0] autorelease];
  11350. }
  11351. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11352. rawValue:(int32_t)rawValue
  11353. forKey:(BOOL)key {
  11354. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11355. // on to get the type correct.
  11356. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11357. rawValues:&rawValue
  11358. forKeys:&key
  11359. count:1] autorelease];
  11360. }
  11361. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11362. rawValues:(const int32_t [])values
  11363. forKeys:(const BOOL [])keys
  11364. count:(NSUInteger)count {
  11365. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11366. // on to get the type correct.
  11367. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11368. rawValues:values
  11369. forKeys:keys
  11370. count:count] autorelease];
  11371. }
  11372. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11373. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11374. // on to get the type correct.
  11375. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11376. }
  11377. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11378. capacity:(NSUInteger)numItems {
  11379. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11380. }
  11381. - (instancetype)init {
  11382. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11383. }
  11384. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11385. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11386. }
  11387. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11388. rawValues:(const int32_t [])rawValues
  11389. forKeys:(const BOOL [])keys
  11390. count:(NSUInteger)count {
  11391. self = [super init];
  11392. if (self) {
  11393. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11394. for (NSUInteger i = 0; i < count; ++i) {
  11395. int idx = keys[i] ? 1 : 0;
  11396. _values[idx] = rawValues[i];
  11397. _valueSet[idx] = YES;
  11398. }
  11399. }
  11400. return self;
  11401. }
  11402. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11403. self = [self initWithValidationFunction:dictionary.validationFunc
  11404. rawValues:NULL
  11405. forKeys:NULL
  11406. count:0];
  11407. if (self) {
  11408. if (dictionary) {
  11409. for (int i = 0; i < 2; ++i) {
  11410. if (dictionary->_valueSet[i]) {
  11411. _values[i] = dictionary->_values[i];
  11412. _valueSet[i] = YES;
  11413. }
  11414. }
  11415. }
  11416. }
  11417. return self;
  11418. }
  11419. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11420. capacity:(NSUInteger)numItems {
  11421. #pragma unused(numItems)
  11422. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11423. }
  11424. #if !defined(NS_BLOCK_ASSERTIONS)
  11425. - (void)dealloc {
  11426. NSAssert(!_autocreator,
  11427. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11428. [self class], _autocreator);
  11429. [super dealloc];
  11430. }
  11431. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11432. - (instancetype)copyWithZone:(NSZone *)zone {
  11433. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11434. }
  11435. - (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
  11436. if (self == other) {
  11437. return YES;
  11438. }
  11439. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11440. return NO;
  11441. }
  11442. if ((_valueSet[0] != other->_valueSet[0]) ||
  11443. (_valueSet[1] != other->_valueSet[1])) {
  11444. return NO;
  11445. }
  11446. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11447. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11448. return NO;
  11449. }
  11450. return YES;
  11451. }
  11452. - (NSUInteger)hash {
  11453. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11454. }
  11455. - (NSString *)description {
  11456. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11457. if (_valueSet[0]) {
  11458. [result appendFormat:@"NO: %d", _values[0]];
  11459. }
  11460. if (_valueSet[1]) {
  11461. [result appendFormat:@"YES: %d", _values[1]];
  11462. }
  11463. [result appendString:@" }"];
  11464. return result;
  11465. }
  11466. - (NSUInteger)count {
  11467. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11468. }
  11469. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  11470. int idx = (key ? 1 : 0);
  11471. if (_valueSet[idx]) {
  11472. if (value) {
  11473. int32_t result = _values[idx];
  11474. if (!_validationFunc(result)) {
  11475. result = kGPBUnrecognizedEnumeratorValue;
  11476. }
  11477. *value = result;
  11478. }
  11479. return YES;
  11480. }
  11481. return NO;
  11482. }
  11483. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  11484. int idx = (key ? 1 : 0);
  11485. if (_valueSet[idx]) {
  11486. if (rawValue) {
  11487. *rawValue = _values[idx];
  11488. }
  11489. return YES;
  11490. }
  11491. return NO;
  11492. }
  11493. - (void)enumerateKeysAndValuesUsingBlock:
  11494. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11495. BOOL stop = NO;
  11496. if (_valueSet[0]) {
  11497. block(NO, _values[0], &stop);
  11498. }
  11499. if (!stop && _valueSet[1]) {
  11500. block(YES, _values[1], &stop);
  11501. }
  11502. }
  11503. - (void)enumerateKeysAndRawValuesUsingBlock:
  11504. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11505. BOOL stop = NO;
  11506. GPBEnumValidationFunc func = _validationFunc;
  11507. int32_t validatedValue;
  11508. if (_valueSet[0]) {
  11509. validatedValue = _values[0];
  11510. if (!func(validatedValue)) {
  11511. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11512. }
  11513. block(NO, validatedValue, &stop);
  11514. }
  11515. if (!stop && _valueSet[1]) {
  11516. validatedValue = _values[1];
  11517. if (!func(validatedValue)) {
  11518. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11519. }
  11520. block(YES, validatedValue, &stop);
  11521. }
  11522. }
  11523. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11524. // This block of code is generated, do not edit it directly.
  11525. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11526. forKey:(GPBGenericValue *)key
  11527. keyDataType:(GPBDataType)keyDataType {
  11528. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11529. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11530. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11531. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11532. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11533. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11534. [outputStream release];
  11535. return data;
  11536. }
  11537. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11538. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11539. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11540. NSUInteger count = 0;
  11541. size_t result = 0;
  11542. for (int i = 0; i < 2; ++i) {
  11543. if (_valueSet[i]) {
  11544. ++count;
  11545. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11546. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11547. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11548. }
  11549. }
  11550. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11551. result += tagSize * count;
  11552. return result;
  11553. }
  11554. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11555. asField:(GPBFieldDescriptor *)field {
  11556. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11557. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11558. for (int i = 0; i < 2; ++i) {
  11559. if (_valueSet[i]) {
  11560. // Write the tag.
  11561. [outputStream writeInt32NoTag:tag];
  11562. // Write the size of the message.
  11563. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11564. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11565. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11566. // Write the fields.
  11567. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11568. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11569. }
  11570. }
  11571. }
  11572. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11573. if (_valueSet[0]) {
  11574. block(@"false", @(_values[0]));
  11575. }
  11576. if (_valueSet[1]) {
  11577. block(@"true", @(_values[1]));
  11578. }
  11579. }
  11580. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11581. forGPBGenericValueKey:(GPBGenericValue *)key {
  11582. int idx = (key->valueBool ? 1 : 0);
  11583. _values[idx] = value->valueInt32;
  11584. _valueSet[idx] = YES;
  11585. }
  11586. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11587. if (otherDictionary) {
  11588. for (int i = 0; i < 2; ++i) {
  11589. if (otherDictionary->_valueSet[i]) {
  11590. _valueSet[i] = YES;
  11591. _values[i] = otherDictionary->_values[i];
  11592. }
  11593. }
  11594. if (_autocreator) {
  11595. GPBAutocreatedDictionaryModified(_autocreator, self);
  11596. }
  11597. }
  11598. }
  11599. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  11600. if (!_validationFunc(value)) {
  11601. [NSException raise:NSInvalidArgumentException
  11602. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11603. value];
  11604. }
  11605. int idx = (key ? 1 : 0);
  11606. _values[idx] = value;
  11607. _valueSet[idx] = YES;
  11608. if (_autocreator) {
  11609. GPBAutocreatedDictionaryModified(_autocreator, self);
  11610. }
  11611. }
  11612. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11613. int idx = (key ? 1 : 0);
  11614. _values[idx] = rawValue;
  11615. _valueSet[idx] = YES;
  11616. if (_autocreator) {
  11617. GPBAutocreatedDictionaryModified(_autocreator, self);
  11618. }
  11619. }
  11620. - (void)removeValueForKey:(BOOL)aKey {
  11621. _valueSet[aKey ? 1 : 0] = NO;
  11622. }
  11623. - (void)removeAll {
  11624. _valueSet[0] = NO;
  11625. _valueSet[1] = NO;
  11626. }
  11627. @end
  11628. #pragma mark - NSDictionary Subclass
  11629. @implementation GPBAutocreatedDictionary {
  11630. NSMutableDictionary *_dictionary;
  11631. }
  11632. - (void)dealloc {
  11633. NSAssert(!_autocreator,
  11634. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11635. [self class], _autocreator);
  11636. [_dictionary release];
  11637. [super dealloc];
  11638. }
  11639. #pragma mark Required NSDictionary overrides
  11640. - (instancetype)initWithObjects:(const id [])objects
  11641. forKeys:(const id<NSCopying> [])keys
  11642. count:(NSUInteger)count {
  11643. self = [super init];
  11644. if (self) {
  11645. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11646. forKeys:keys
  11647. count:count];
  11648. }
  11649. return self;
  11650. }
  11651. - (NSUInteger)count {
  11652. return [_dictionary count];
  11653. }
  11654. - (id)objectForKey:(id)aKey {
  11655. return [_dictionary objectForKey:aKey];
  11656. }
  11657. - (NSEnumerator *)keyEnumerator {
  11658. if (_dictionary == nil) {
  11659. _dictionary = [[NSMutableDictionary alloc] init];
  11660. }
  11661. return [_dictionary keyEnumerator];
  11662. }
  11663. #pragma mark Required NSMutableDictionary overrides
  11664. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11665. // since we only autocreate empty dictionaries.
  11666. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11667. if (_dictionary == nil) {
  11668. _dictionary = [[NSMutableDictionary alloc] init];
  11669. }
  11670. [_dictionary setObject:anObject forKey:aKey];
  11671. if (_autocreator) {
  11672. GPBAutocreatedDictionaryModified(_autocreator, self);
  11673. }
  11674. }
  11675. - (void)removeObjectForKey:(id)aKey {
  11676. [_dictionary removeObjectForKey:aKey];
  11677. }
  11678. #pragma mark Extra things hooked
  11679. - (id)copyWithZone:(NSZone *)zone {
  11680. if (_dictionary == nil) {
  11681. _dictionary = [[NSMutableDictionary alloc] init];
  11682. }
  11683. return [_dictionary copyWithZone:zone];
  11684. }
  11685. - (id)mutableCopyWithZone:(NSZone *)zone {
  11686. if (_dictionary == nil) {
  11687. _dictionary = [[NSMutableDictionary alloc] init];
  11688. }
  11689. return [_dictionary mutableCopyWithZone:zone];
  11690. }
  11691. - (id)objectForKeyedSubscript:(id)key {
  11692. return [_dictionary objectForKeyedSubscript:key];
  11693. }
  11694. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11695. if (_dictionary == nil) {
  11696. _dictionary = [[NSMutableDictionary alloc] init];
  11697. }
  11698. [_dictionary setObject:obj forKeyedSubscript:key];
  11699. if (_autocreator) {
  11700. GPBAutocreatedDictionaryModified(_autocreator, self);
  11701. }
  11702. }
  11703. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11704. id obj,
  11705. BOOL *stop))block {
  11706. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11707. }
  11708. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11709. usingBlock:(void (^)(id key,
  11710. id obj,
  11711. BOOL *stop))block {
  11712. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11713. }
  11714. @end