GPBDictionary.m 467 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073
  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_PackagePrivate.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. // Direct access is use for speed, to avoid even internally declaring things
  45. // read/write, etc. The warning is enabled in the project to ensure code calling
  46. // protos can turn on -Wdirect-ivar-access without issues.
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  49. // Used to include code only visible to specific versions of the static
  50. // analyzer. Useful for wrapping code that only exists to silence the analyzer.
  51. // Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
  52. // END_APPLE_BUILD_VERSION using:
  53. // xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
  54. // Example usage:
  55. // #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
  56. #define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
  57. (defined(__clang_analyzer__) && \
  58. (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
  59. __apple_build_version__ <= END_APPLE_BUILD_VERSION))
  60. enum {
  61. kMapKeyFieldNumber = 1,
  62. kMapValueFieldNumber = 2,
  63. };
  64. static BOOL DictDefault_IsValidValue(int32_t value) {
  65. // Anything but the bad value marker is allowed.
  66. return (value != kGPBUnrecognizedEnumeratorValue);
  67. }
  68. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  69. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  70. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  71. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  72. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  73. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  74. //% } else {
  75. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  76. //% return 0;
  77. //% }
  78. //%}
  79. //%
  80. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  81. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  82. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  83. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  84. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  85. //% } else {
  86. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  87. //% }
  88. //%}
  89. //%
  90. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  91. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  92. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  93. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  94. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  95. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  96. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  97. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  98. //% } else {
  99. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  100. //% return 0;
  101. //% }
  102. //%}
  103. //%
  104. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  105. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  106. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  107. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  108. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  109. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  110. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  111. //% } else {
  112. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  113. //% }
  114. //%}
  115. //%
  116. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  117. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  118. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  119. //% #pragma unused(dataType) // For when asserts are off in release.
  120. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  121. //%}
  122. //%
  123. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  124. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  125. //% #pragma unused(dataType) // For when asserts are off in release.
  126. //% [stream write##VALUE_NAME##:fieldNum value:value];
  127. //%}
  128. //%
  129. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  130. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  131. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  132. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  133. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  134. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  135. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  136. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  137. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  138. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  139. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  140. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  141. // This block of code is generated, do not edit it directly.
  142. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  143. if (dataType == GPBDataTypeInt32) {
  144. return GPBComputeInt32Size(fieldNum, value);
  145. } else if (dataType == GPBDataTypeSInt32) {
  146. return GPBComputeSInt32Size(fieldNum, value);
  147. } else if (dataType == GPBDataTypeSFixed32) {
  148. return GPBComputeSFixed32Size(fieldNum, value);
  149. } else {
  150. NSCAssert(NO, @"Unexpected type %d", dataType);
  151. return 0;
  152. }
  153. }
  154. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  155. if (dataType == GPBDataTypeInt32) {
  156. [stream writeInt32:fieldNum value:value];
  157. } else if (dataType == GPBDataTypeSInt32) {
  158. [stream writeSInt32:fieldNum value:value];
  159. } else if (dataType == GPBDataTypeSFixed32) {
  160. [stream writeSFixed32:fieldNum value:value];
  161. } else {
  162. NSCAssert(NO, @"Unexpected type %d", dataType);
  163. }
  164. }
  165. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  166. if (dataType == GPBDataTypeUInt32) {
  167. return GPBComputeUInt32Size(fieldNum, value);
  168. } else if (dataType == GPBDataTypeFixed32) {
  169. return GPBComputeFixed32Size(fieldNum, value);
  170. } else {
  171. NSCAssert(NO, @"Unexpected type %d", dataType);
  172. return 0;
  173. }
  174. }
  175. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  176. if (dataType == GPBDataTypeUInt32) {
  177. [stream writeUInt32:fieldNum value:value];
  178. } else if (dataType == GPBDataTypeFixed32) {
  179. [stream writeFixed32:fieldNum value:value];
  180. } else {
  181. NSCAssert(NO, @"Unexpected type %d", dataType);
  182. }
  183. }
  184. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  185. if (dataType == GPBDataTypeInt64) {
  186. return GPBComputeInt64Size(fieldNum, value);
  187. } else if (dataType == GPBDataTypeSInt64) {
  188. return GPBComputeSInt64Size(fieldNum, value);
  189. } else if (dataType == GPBDataTypeSFixed64) {
  190. return GPBComputeSFixed64Size(fieldNum, value);
  191. } else {
  192. NSCAssert(NO, @"Unexpected type %d", dataType);
  193. return 0;
  194. }
  195. }
  196. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  197. if (dataType == GPBDataTypeInt64) {
  198. [stream writeInt64:fieldNum value:value];
  199. } else if (dataType == GPBDataTypeSInt64) {
  200. [stream writeSInt64:fieldNum value:value];
  201. } else if (dataType == GPBDataTypeSFixed64) {
  202. [stream writeSFixed64:fieldNum value:value];
  203. } else {
  204. NSCAssert(NO, @"Unexpected type %d", dataType);
  205. }
  206. }
  207. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  208. if (dataType == GPBDataTypeUInt64) {
  209. return GPBComputeUInt64Size(fieldNum, value);
  210. } else if (dataType == GPBDataTypeFixed64) {
  211. return GPBComputeFixed64Size(fieldNum, value);
  212. } else {
  213. NSCAssert(NO, @"Unexpected type %d", dataType);
  214. return 0;
  215. }
  216. }
  217. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  218. if (dataType == GPBDataTypeUInt64) {
  219. [stream writeUInt64:fieldNum value:value];
  220. } else if (dataType == GPBDataTypeFixed64) {
  221. [stream writeFixed64:fieldNum value:value];
  222. } else {
  223. NSCAssert(NO, @"Unexpected type %d", dataType);
  224. }
  225. }
  226. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  227. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  228. #pragma unused(dataType) // For when asserts are off in release.
  229. return GPBComputeBoolSize(fieldNum, value);
  230. }
  231. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  232. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  233. #pragma unused(dataType) // For when asserts are off in release.
  234. [stream writeBool:fieldNum value:value];
  235. }
  236. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  237. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  238. #pragma unused(dataType) // For when asserts are off in release.
  239. return GPBComputeEnumSize(fieldNum, value);
  240. }
  241. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  242. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  243. #pragma unused(dataType) // For when asserts are off in release.
  244. [stream writeEnum:fieldNum value:value];
  245. }
  246. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  247. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  248. #pragma unused(dataType) // For when asserts are off in release.
  249. return GPBComputeFloatSize(fieldNum, value);
  250. }
  251. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
  252. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  253. #pragma unused(dataType) // For when asserts are off in release.
  254. [stream writeFloat:fieldNum value:value];
  255. }
  256. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  257. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  258. #pragma unused(dataType) // For when asserts are off in release.
  259. return GPBComputeDoubleSize(fieldNum, value);
  260. }
  261. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
  262. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  263. #pragma unused(dataType) // For when asserts are off in release.
  264. [stream writeDouble:fieldNum value:value];
  265. }
  266. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  267. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  268. #pragma unused(dataType) // For when asserts are off in release.
  269. return GPBComputeStringSize(fieldNum, value);
  270. }
  271. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  272. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  273. #pragma unused(dataType) // For when asserts are off in release.
  274. [stream writeString:fieldNum value:value];
  275. }
  276. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  277. if (dataType == GPBDataTypeMessage) {
  278. return GPBComputeMessageSize(fieldNum, value);
  279. } else if (dataType == GPBDataTypeString) {
  280. return GPBComputeStringSize(fieldNum, value);
  281. } else if (dataType == GPBDataTypeBytes) {
  282. return GPBComputeBytesSize(fieldNum, value);
  283. } else {
  284. NSCAssert(NO, @"Unexpected type %d", dataType);
  285. return 0;
  286. }
  287. }
  288. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  289. if (dataType == GPBDataTypeMessage) {
  290. [stream writeMessage:fieldNum value:value];
  291. } else if (dataType == GPBDataTypeString) {
  292. [stream writeString:fieldNum value:value];
  293. } else if (dataType == GPBDataTypeBytes) {
  294. [stream writeBytes:fieldNum value:value];
  295. } else {
  296. NSCAssert(NO, @"Unexpected type %d", dataType);
  297. }
  298. }
  299. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  300. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  301. GPBDataType mapValueType = GPBGetFieldDataType(field);
  302. size_t result = 0;
  303. NSString *key;
  304. NSEnumerator *keys = [dict keyEnumerator];
  305. while ((key = [keys nextObject])) {
  306. id obj = dict[key];
  307. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  308. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  309. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  310. }
  311. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  312. result += tagSize * dict.count;
  313. return result;
  314. }
  315. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  316. NSDictionary *dict,
  317. GPBFieldDescriptor *field) {
  318. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  319. GPBDataType mapValueType = GPBGetFieldDataType(field);
  320. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  321. NSString *key;
  322. NSEnumerator *keys = [dict keyEnumerator];
  323. while ((key = [keys nextObject])) {
  324. id obj = dict[key];
  325. // Write the tag.
  326. [outputStream writeInt32NoTag:tag];
  327. // Write the size of the message.
  328. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  329. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  330. // Write the size and fields.
  331. [outputStream writeInt32NoTag:(int32_t)msgSize];
  332. [outputStream writeString:kMapKeyFieldNumber value:key];
  333. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  334. }
  335. }
  336. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  337. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  338. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  339. #pragma unused(field) // For when asserts are off in release.
  340. GPBMessage *msg;
  341. NSEnumerator *objects = [dict objectEnumerator];
  342. while ((msg = [objects nextObject])) {
  343. if (!msg.initialized) {
  344. return NO;
  345. }
  346. }
  347. return YES;
  348. }
  349. // Note: if the type is an object, it the retain pass back to the caller.
  350. static void ReadValue(GPBCodedInputStream *stream,
  351. GPBGenericValue *valueToFill,
  352. GPBDataType type,
  353. GPBExtensionRegistry *registry,
  354. GPBFieldDescriptor *field) {
  355. switch (type) {
  356. case GPBDataTypeBool:
  357. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  358. break;
  359. case GPBDataTypeFixed32:
  360. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  361. break;
  362. case GPBDataTypeSFixed32:
  363. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  364. break;
  365. case GPBDataTypeFloat:
  366. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  367. break;
  368. case GPBDataTypeFixed64:
  369. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  370. break;
  371. case GPBDataTypeSFixed64:
  372. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  373. break;
  374. case GPBDataTypeDouble:
  375. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  376. break;
  377. case GPBDataTypeInt32:
  378. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  379. break;
  380. case GPBDataTypeInt64:
  381. valueToFill->valueInt64 = GPBCodedInputStreamReadInt64(&stream->state_);
  382. break;
  383. case GPBDataTypeSInt32:
  384. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  385. break;
  386. case GPBDataTypeSInt64:
  387. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  388. break;
  389. case GPBDataTypeUInt32:
  390. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  391. break;
  392. case GPBDataTypeUInt64:
  393. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  394. break;
  395. case GPBDataTypeBytes:
  396. [valueToFill->valueData release];
  397. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  398. break;
  399. case GPBDataTypeString:
  400. [valueToFill->valueString release];
  401. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  402. break;
  403. case GPBDataTypeMessage: {
  404. GPBMessage *message = [[field.msgClass alloc] init];
  405. [stream readMessage:message extensionRegistry:registry];
  406. [valueToFill->valueMessage release];
  407. valueToFill->valueMessage = message;
  408. break;
  409. }
  410. case GPBDataTypeGroup:
  411. NSCAssert(NO, @"Can't happen");
  412. break;
  413. case GPBDataTypeEnum:
  414. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  415. break;
  416. }
  417. }
  418. void GPBDictionaryReadEntry(id mapDictionary,
  419. GPBCodedInputStream *stream,
  420. GPBExtensionRegistry *registry,
  421. GPBFieldDescriptor *field,
  422. GPBMessage *parentMessage) {
  423. GPBDataType keyDataType = field.mapKeyDataType;
  424. GPBDataType valueDataType = GPBGetFieldDataType(field);
  425. GPBGenericValue key;
  426. GPBGenericValue value;
  427. // Zero them (but pick up any enum default for proto2).
  428. key.valueString = value.valueString = nil;
  429. if (valueDataType == GPBDataTypeEnum) {
  430. value = field.defaultValue;
  431. }
  432. GPBCodedInputStreamState *state = &stream->state_;
  433. uint32_t keyTag =
  434. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  435. uint32_t valueTag =
  436. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  437. BOOL hitError = NO;
  438. while (YES) {
  439. uint32_t tag = GPBCodedInputStreamReadTag(state);
  440. if (tag == keyTag) {
  441. ReadValue(stream, &key, keyDataType, registry, field);
  442. } else if (tag == valueTag) {
  443. ReadValue(stream, &value, valueDataType, registry, field);
  444. } else if (tag == 0) {
  445. // zero signals EOF / limit reached
  446. break;
  447. } else { // Unknown
  448. if (![stream skipField:tag]){
  449. hitError = YES;
  450. break;
  451. }
  452. }
  453. }
  454. if (!hitError) {
  455. // Handle the special defaults and/or missing key/value.
  456. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  457. key.valueString = [@"" retain];
  458. }
  459. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  460. #pragma clang diagnostic push
  461. #pragma clang diagnostic ignored "-Wswitch-enum"
  462. switch (valueDataType) {
  463. case GPBDataTypeString:
  464. value.valueString = [@"" retain];
  465. break;
  466. case GPBDataTypeBytes:
  467. value.valueData = [GPBEmptyNSData() retain];
  468. break;
  469. #if defined(__clang_analyzer__)
  470. case GPBDataTypeGroup:
  471. // Maps can't really have Groups as the value type, but this case is needed
  472. // so the analyzer won't report the posibility of send nil in for the value
  473. // in the NSMutableDictionary case below.
  474. #endif
  475. case GPBDataTypeMessage: {
  476. value.valueMessage = [[field.msgClass alloc] init];
  477. break;
  478. }
  479. default:
  480. // Nothing
  481. break;
  482. }
  483. #pragma clang diagnostic pop
  484. }
  485. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  486. #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
  487. // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
  488. // be raised as needed for new Xcodes.
  489. //
  490. // This is only needed on a "shallow" analyze; on a "deep" analyze, the
  491. // existing code path gets this correct. In shallow, the analyzer decides
  492. // GPBDataTypeIsObject(valueDataType) is both false and true on a single
  493. // path through this function, allowing nil to be used for the
  494. // setObject:forKey:.
  495. if (value.valueString == nil) {
  496. value.valueString = [@"" retain];
  497. }
  498. #endif
  499. // mapDictionary is an NSMutableDictionary
  500. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  501. forKey:key.valueString];
  502. } else {
  503. if (valueDataType == GPBDataTypeEnum) {
  504. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  505. [field isValidEnumValue:value.valueEnum]) {
  506. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  507. } else {
  508. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  509. forKey:&key
  510. keyDataType:keyDataType];
  511. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  512. }
  513. } else {
  514. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  515. }
  516. }
  517. }
  518. if (GPBDataTypeIsObject(keyDataType)) {
  519. [key.valueString release];
  520. }
  521. if (GPBDataTypeIsObject(valueDataType)) {
  522. [value.valueString release];
  523. }
  524. }
  525. //
  526. // Macros for the common basic cases.
  527. //
  528. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  529. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  530. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  531. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  532. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  533. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  534. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  535. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  536. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  537. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  538. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  539. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  540. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  541. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
  542. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  543. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
  544. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
  545. //%#pragma mark - KEY_NAME -> VALUE_NAME
  546. //%
  547. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  548. //% @package
  549. //% NSMutableDictionary *_dictionary;
  550. //%}
  551. //%
  552. //%+ (instancetype)dictionary {
  553. //% return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
  554. //%}
  555. //%
  556. //%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)##VNAME_VAR
  557. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  558. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  559. //% // on to get the type correct.
  560. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
  561. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  562. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  563. //%}
  564. //%
  565. //%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  566. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  567. //% ##VNAME$S## count:(NSUInteger)count {
  568. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  569. //% // on to get the type correct.
  570. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
  571. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  572. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  573. //%}
  574. //%
  575. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  576. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  577. //% // on to get the type correct.
  578. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  579. //%}
  580. //%
  581. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  582. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  583. //%}
  584. //%
  585. //%- (instancetype)init {
  586. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  587. //%}
  588. //%
  589. //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  590. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  591. //% ##VNAME$S## count:(NSUInteger)count {
  592. //% self = [super init];
  593. //% if (self) {
  594. //% _dictionary = [[NSMutableDictionary alloc] init];
  595. //% if (count && VNAME_VAR##s && keys) {
  596. //% for (NSUInteger i = 0; i < count; ++i) {
  597. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
  598. //% }
  599. //% }
  600. //% }
  601. //% return self;
  602. //%}
  603. //%
  604. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  605. //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  606. //% if (self) {
  607. //% if (dictionary) {
  608. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  609. //% }
  610. //% }
  611. //% return self;
  612. //%}
  613. //%
  614. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  615. //% #pragma unused(numItems)
  616. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  617. //%}
  618. //%
  619. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  620. //%
  621. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  622. //%
  623. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  624. //%
  625. //%@end
  626. //%
  627. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  628. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  629. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  630. //%#pragma mark - KEY_NAME -> VALUE_NAME
  631. //%
  632. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  633. //% @package
  634. //% NSMutableDictionary *_dictionary;
  635. //% GPBEnumValidationFunc _validationFunc;
  636. //%}
  637. //%
  638. //%@synthesize validationFunc = _validationFunc;
  639. //%
  640. //%+ (instancetype)dictionary {
  641. //% return [[[self alloc] initWithValidationFunction:NULL
  642. //% rawValues:NULL
  643. //% forKeys:NULL
  644. //% count:0] autorelease];
  645. //%}
  646. //%
  647. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  648. //% return [[[self alloc] initWithValidationFunction:func
  649. //% rawValues:NULL
  650. //% forKeys:NULL
  651. //% count:0] autorelease];
  652. //%}
  653. //%
  654. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  655. //% rawValue:(VALUE_TYPE)rawValue
  656. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  657. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  658. //% // on to get the type correct.
  659. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  660. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  661. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  662. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  663. //%}
  664. //%
  665. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  666. //% rawValues:(const VALUE_TYPE [])rawValues
  667. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  668. //% count:(NSUInteger)count {
  669. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  670. //% // on to get the type correct.
  671. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  672. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  673. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  674. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  675. //%}
  676. //%
  677. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  678. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  679. //% // on to get the type correct.
  680. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  681. //%}
  682. //%
  683. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  684. //% capacity:(NSUInteger)numItems {
  685. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  686. //%}
  687. //%
  688. //%- (instancetype)init {
  689. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  690. //%}
  691. //%
  692. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  693. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  694. //%}
  695. //%
  696. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  697. //% rawValues:(const VALUE_TYPE [])rawValues
  698. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  699. //% count:(NSUInteger)count {
  700. //% self = [super init];
  701. //% if (self) {
  702. //% _dictionary = [[NSMutableDictionary alloc] init];
  703. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  704. //% if (count && rawValues && keys) {
  705. //% for (NSUInteger i = 0; i < count; ++i) {
  706. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  707. //% }
  708. //% }
  709. //% }
  710. //% return self;
  711. //%}
  712. //%
  713. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  714. //% self = [self initWithValidationFunction:dictionary.validationFunc
  715. //% rawValues:NULL
  716. //% forKeys:NULL
  717. //% count:0];
  718. //% if (self) {
  719. //% if (dictionary) {
  720. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  721. //% }
  722. //% }
  723. //% return self;
  724. //%}
  725. //%
  726. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  727. //% capacity:(NSUInteger)numItems {
  728. //% #pragma unused(numItems)
  729. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  730. //%}
  731. //%
  732. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
  733. //%
  734. //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  735. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  736. //% if (wrapped && value) {
  737. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  738. //% if (!_validationFunc(result)) {
  739. //% result = kGPBUnrecognizedEnumeratorValue;
  740. //% }
  741. //% *value = result;
  742. //% }
  743. //% return (wrapped != NULL);
  744. //%}
  745. //%
  746. //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
  747. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  748. //% if (wrapped && rawValue) {
  749. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  750. //% }
  751. //% return (wrapped != NULL);
  752. //%}
  753. //%
  754. //%- (void)enumerateKeysAndEnumsUsingBlock:
  755. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  756. //% GPBEnumValidationFunc func = _validationFunc;
  757. //% BOOL stop = NO;
  758. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  759. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  760. //% while ((aKey = [keys nextObject])) {
  761. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue = _dictionary[aKey];
  762. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  763. //% if (!func(unwrapped)) {
  764. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  765. //% }
  766. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, &stop);
  767. //% if (stop) {
  768. //% break;
  769. //% }
  770. //% }
  771. //%}
  772. //%
  773. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
  774. //%
  775. //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  776. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  777. //% [NSException raise:NSInvalidArgumentException
  778. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  779. //% value];
  780. //% }
  781. //%
  782. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  783. //% if (_autocreator) {
  784. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  785. //% }
  786. //%}
  787. //%
  788. //%@end
  789. //%
  790. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  791. //%- (void)dealloc {
  792. //% NSAssert(!_autocreator,
  793. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  794. //% [self class], _autocreator);
  795. //% [_dictionary release];
  796. //% [super dealloc];
  797. //%}
  798. //%
  799. //%- (instancetype)copyWithZone:(NSZone *)zone {
  800. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  801. //%}
  802. //%
  803. //%- (BOOL)isEqual:(id)other {
  804. //% if (self == other) {
  805. //% return YES;
  806. //% }
  807. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  808. //% return NO;
  809. //% }
  810. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  811. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  812. //%}
  813. //%
  814. //%- (NSUInteger)hash {
  815. //% return _dictionary.count;
  816. //%}
  817. //%
  818. //%- (NSString *)description {
  819. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  820. //%}
  821. //%
  822. //%- (NSUInteger)count {
  823. //% return _dictionary.count;
  824. //%}
  825. //%
  826. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
  827. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  828. //% BOOL stop = NO;
  829. //% NSDictionary *internal = _dictionary;
  830. //% NSEnumerator *keys = [internal keyEnumerator];
  831. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  832. //% while ((aKey = [keys nextObject])) {
  833. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  834. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), &stop);
  835. //% if (stop) {
  836. //% break;
  837. //% }
  838. //% }
  839. //%}
  840. //%
  841. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  842. //% NSDictionary *internal = _dictionary;
  843. //% NSUInteger count = internal.count;
  844. //% if (count == 0) {
  845. //% return 0;
  846. //% }
  847. //%
  848. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  849. //% GPBDataType keyDataType = field.mapKeyDataType;
  850. //% size_t result = 0;
  851. //% NSEnumerator *keys = [internal keyEnumerator];
  852. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  853. //% while ((aKey = [keys nextObject])) {
  854. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  855. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  856. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  857. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  858. //% }
  859. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  860. //% result += tagSize * count;
  861. //% return result;
  862. //%}
  863. //%
  864. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  865. //% asField:(GPBFieldDescriptor *)field {
  866. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  867. //% GPBDataType keyDataType = field.mapKeyDataType;
  868. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  869. //% NSDictionary *internal = _dictionary;
  870. //% NSEnumerator *keys = [internal keyEnumerator];
  871. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  872. //% while ((aKey = [keys nextObject])) {
  873. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  874. //% [outputStream writeInt32NoTag:tag];
  875. //% // Write the size of the message.
  876. //% KEY_TYPE KisP##unwrappedKey = UNWRAP##KEY_NAME(aKey);
  877. //% VALUE_TYPE unwrappedValue = UNWRAP##VALUE_NAME(a##VNAME_VAR$u);
  878. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  879. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  880. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  881. //% // Write the fields.
  882. //% WriteDict##KEY_NAME##Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  883. //% WriteDict##VALUE_NAME##Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  884. //% }
  885. //%}
  886. //%
  887. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  888. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  889. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  890. //%}
  891. //%
  892. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  893. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) {
  894. //% #pragma unused(stop)
  895. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
  896. //% }];
  897. //%}
  898. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  899. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
  900. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
  901. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  902. //% if (otherDictionary) {
  903. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  904. //% if (_autocreator) {
  905. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  906. //% }
  907. //% }
  908. //%}
  909. //%
  910. //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
  911. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
  912. //% if (_autocreator) {
  913. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  914. //% }
  915. //%}
  916. //%
  917. //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  918. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  919. //%}
  920. //%
  921. //%- (void)removeAll {
  922. //% [_dictionary removeAllObjects];
  923. //%}
  924. //
  925. // Custom Generation for Bool keys
  926. //
  927. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  928. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
  929. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  930. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
  931. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
  932. //%#pragma mark - Bool -> VALUE_NAME
  933. //%
  934. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  935. //% @package
  936. //% VALUE_TYPE _values[2];
  937. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  938. //%
  939. //%+ (instancetype)dictionary {
  940. //% return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
  941. //%}
  942. //%
  943. //%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)VNAME_VAR
  944. //% ##VNAME$S## forKey:(BOOL)key {
  945. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  946. //% // on to get the type correct.
  947. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
  948. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  949. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  950. //%}
  951. //%
  952. //%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  953. //% ##VNAME$S## forKeys:(const BOOL [])keys
  954. //% ##VNAME$S## count:(NSUInteger)count {
  955. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  956. //% // on to get the type correct.
  957. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
  958. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  959. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  960. //%}
  961. //%
  962. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  963. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  964. //% // on to get the type correct.
  965. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  966. //%}
  967. //%
  968. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  969. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  970. //%}
  971. //%
  972. //%- (instancetype)init {
  973. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  974. //%}
  975. //%
  976. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  977. //%
  978. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  979. //% #pragma unused(numItems)
  980. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  981. //%}
  982. //%
  983. //%BOOL_DICT_DEALLOC##HELPER()
  984. //%
  985. //%- (instancetype)copyWithZone:(NSZone *)zone {
  986. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  987. //%}
  988. //%
  989. //%- (BOOL)isEqual:(id)other {
  990. //% if (self == other) {
  991. //% return YES;
  992. //% }
  993. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  994. //% return NO;
  995. //% }
  996. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  997. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  998. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  999. //% return NO;
  1000. //% }
  1001. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  1002. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  1003. //% return NO;
  1004. //% }
  1005. //% return YES;
  1006. //%}
  1007. //%
  1008. //%- (NSUInteger)hash {
  1009. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  1010. //%}
  1011. //%
  1012. //%- (NSString *)description {
  1013. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  1014. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  1015. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  1016. //% }
  1017. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  1018. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  1019. //% }
  1020. //% [result appendString:@" }"];
  1021. //% return result;
  1022. //%}
  1023. //%
  1024. //%- (NSUInteger)count {
  1025. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  1026. //%}
  1027. //%
  1028. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
  1029. //%
  1030. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  1031. //%
  1032. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1033. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1034. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  1035. //% }
  1036. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  1037. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  1038. //% }
  1039. //%}
  1040. //%
  1041. //%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
  1042. //% (void (^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  1043. //% BOOL stop = NO;
  1044. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1045. //% block(NO, _values[0], &stop);
  1046. //% }
  1047. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  1048. //% block(YES, _values[1], &stop);
  1049. //% }
  1050. //%}
  1051. //%
  1052. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1053. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1054. //% NSUInteger count = 0;
  1055. //% size_t result = 0;
  1056. //% for (int i = 0; i < 2; ++i) {
  1057. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1058. //% ++count;
  1059. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1060. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1061. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1062. //% }
  1063. //% }
  1064. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1065. //% result += tagSize * count;
  1066. //% return result;
  1067. //%}
  1068. //%
  1069. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1070. //% asField:(GPBFieldDescriptor *)field {
  1071. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1072. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1073. //% for (int i = 0; i < 2; ++i) {
  1074. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1075. //% // Write the tag.
  1076. //% [outputStream writeInt32NoTag:tag];
  1077. //% // Write the size of the message.
  1078. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1079. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1080. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1081. //% // Write the fields.
  1082. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1083. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1084. //% }
  1085. //% }
  1086. //%}
  1087. //%
  1088. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1089. //%
  1090. //%@end
  1091. //%
  1092. //
  1093. // Helpers for PODs
  1094. //
  1095. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1096. //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
  1097. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1098. //% if (wrapped && value) {
  1099. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1100. //% }
  1101. //% return (wrapped != NULL);
  1102. //%}
  1103. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1104. //%@(VALUE)
  1105. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1106. //%[VALUE unsignedIntValue]
  1107. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1108. //%[VALUE intValue]
  1109. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1110. //%[VALUE unsignedLongLongValue]
  1111. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1112. //%[VALUE longLongValue]
  1113. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1114. //%[VALUE boolValue]
  1115. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1116. //%[VALUE floatValue]
  1117. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1118. //%[VALUE doubleValue]
  1119. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1120. //%[VALUE intValue]
  1121. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1122. //%[NSString stringWithFormat:@"%u", VALUE]
  1123. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1124. //%[NSString stringWithFormat:@"%d", VALUE]
  1125. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1126. //%[NSString stringWithFormat:@"%llu", VALUE]
  1127. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1128. //%[NSString stringWithFormat:@"%lld", VALUE]
  1129. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1130. //%(VALUE ? @"true" : @"false")
  1131. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1132. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1133. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1134. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1135. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1136. //%@(VALUE)
  1137. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1138. //%NSNumber *
  1139. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1140. //%VAL1 != VAL2
  1141. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1142. // Empty
  1143. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1144. // Empty
  1145. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1146. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1147. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1148. // Empty
  1149. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1150. // Empty
  1151. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1152. // Empty
  1153. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1154. // Empty
  1155. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1156. // Empty
  1157. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1158. // Empty
  1159. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1160. // Empty
  1161. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1162. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1163. //% forKey:(GPBGenericValue *)key
  1164. //% keyDataType:(GPBDataType)keyDataType {
  1165. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1166. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1167. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1168. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1169. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1170. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1171. //% [outputStream release];
  1172. //% return data;
  1173. //%}
  1174. //%
  1175. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1176. //%value##VALUE_NAME
  1177. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1178. // Empty
  1179. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1180. // Empty
  1181. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1182. //% BOOL _valueSet[2];
  1183. //%
  1184. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1185. //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
  1186. //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys
  1187. //% ##VALUE_NAME$S## count:(NSUInteger)count {
  1188. //% self = [super init];
  1189. //% if (self) {
  1190. //% for (NSUInteger i = 0; i < count; ++i) {
  1191. //% int idx = keys[i] ? 1 : 0;
  1192. //% _values[idx] = values[i];
  1193. //% _valueSet[idx] = YES;
  1194. //% }
  1195. //% }
  1196. //% return self;
  1197. //%}
  1198. //%
  1199. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1200. //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
  1201. //% if (self) {
  1202. //% if (dictionary) {
  1203. //% for (int i = 0; i < 2; ++i) {
  1204. //% if (dictionary->_valueSet[i]) {
  1205. //% _values[i] = dictionary->_values[i];
  1206. //% _valueSet[i] = YES;
  1207. //% }
  1208. //% }
  1209. //% }
  1210. //% }
  1211. //% return self;
  1212. //%}
  1213. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1214. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1215. //%- (void)dealloc {
  1216. //% NSAssert(!_autocreator,
  1217. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1218. //% [self class], _autocreator);
  1219. //% [super dealloc];
  1220. //%}
  1221. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1222. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1223. //%BOOL_DICT_HASPOD(IDX, REF)
  1224. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1225. //%REF##_valueSet[IDX]
  1226. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
  1227. //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
  1228. //% int idx = (key ? 1 : 0);
  1229. //% if (_valueSet[idx]) {
  1230. //% if (value) {
  1231. //% *value = _values[idx];
  1232. //% }
  1233. //% return YES;
  1234. //% }
  1235. //% return NO;
  1236. //%}
  1237. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1238. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1239. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1240. //% int idx = (key->valueBool ? 1 : 0);
  1241. //% _values[idx] = value->value##VALUE_NAME;
  1242. //% _valueSet[idx] = YES;
  1243. //%}
  1244. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1245. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1246. //% if (otherDictionary) {
  1247. //% for (int i = 0; i < 2; ++i) {
  1248. //% if (otherDictionary->_valueSet[i]) {
  1249. //% _valueSet[i] = YES;
  1250. //% _values[i] = otherDictionary->_values[i];
  1251. //% }
  1252. //% }
  1253. //% if (_autocreator) {
  1254. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1255. //% }
  1256. //% }
  1257. //%}
  1258. //%
  1259. //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
  1260. //% int idx = (key ? 1 : 0);
  1261. //% _values[idx] = value;
  1262. //% _valueSet[idx] = YES;
  1263. //% if (_autocreator) {
  1264. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1265. //% }
  1266. //%}
  1267. //%
  1268. //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
  1269. //% _valueSet[aKey ? 1 : 0] = NO;
  1270. //%}
  1271. //%
  1272. //%- (void)removeAll {
  1273. //% _valueSet[0] = NO;
  1274. //% _valueSet[1] = NO;
  1275. //%}
  1276. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1277. //%STR_FORMAT_##VALUE_NAME()
  1278. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1279. //%%u
  1280. //%PDDM-DEFINE STR_FORMAT_Int32()
  1281. //%%d
  1282. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1283. //%%llu
  1284. //%PDDM-DEFINE STR_FORMAT_Int64()
  1285. //%%lld
  1286. //%PDDM-DEFINE STR_FORMAT_Bool()
  1287. //%%d
  1288. //%PDDM-DEFINE STR_FORMAT_Float()
  1289. //%%f
  1290. //%PDDM-DEFINE STR_FORMAT_Double()
  1291. //%%lf
  1292. //
  1293. // Helpers for Objects
  1294. //
  1295. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1296. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1297. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1298. //% return result;
  1299. //%}
  1300. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1301. //%VALUE
  1302. //%PDDM-DEFINE UNWRAPString(VALUE)
  1303. //%VALUE
  1304. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1305. //%VALUE
  1306. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1307. //%VALUE
  1308. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1309. //%VALUE
  1310. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1311. //%ENUM_TYPEOBJECT_##TYPE()
  1312. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1313. //%NSString *
  1314. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1315. //%id ##
  1316. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1317. //%![VAL1 isEqual:VAL2]
  1318. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1319. //%- (BOOL)isInitialized {
  1320. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1321. //% if (!msg.initialized) {
  1322. //% return NO;
  1323. //% }
  1324. //% }
  1325. //% return YES;
  1326. //%}
  1327. //%
  1328. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1329. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1330. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1331. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  1332. //% id aKey;
  1333. //% NSMutableDictionary *internalDict = newDict->_dictionary;
  1334. //% while ((aKey = [keys nextObject])) {
  1335. //% GPBMessage *msg = _dictionary[aKey];
  1336. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1337. //% [internalDict setObject:copiedMsg forKey:aKey];
  1338. //% [copiedMsg release];
  1339. //% }
  1340. //% return newDict;
  1341. //%}
  1342. //%
  1343. //%
  1344. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1345. //%- (BOOL)isInitialized {
  1346. //% if (_values[0] && ![_values[0] isInitialized]) {
  1347. //% return NO;
  1348. //% }
  1349. //% if (_values[1] && ![_values[1] isInitialized]) {
  1350. //% return NO;
  1351. //% }
  1352. //% return YES;
  1353. //%}
  1354. //%
  1355. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1356. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1357. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1358. //% for (int i = 0; i < 2; ++i) {
  1359. //% if (_values[i] != nil) {
  1360. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1361. //% }
  1362. //% }
  1363. //% return newDict;
  1364. //%}
  1365. //%
  1366. //%
  1367. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1368. // Empty
  1369. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1370. //%valueString
  1371. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1372. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1373. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1374. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1375. //%##EXTRA_INDENT$S## }
  1376. //%
  1377. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1378. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1379. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1380. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1381. //%##EXTRA_INDENT$S## }
  1382. //%
  1383. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1384. // Empty
  1385. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1386. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1387. //% forKeys:(const BOOL [])keys
  1388. //% count:(NSUInteger)count {
  1389. //% self = [super init];
  1390. //% if (self) {
  1391. //% for (NSUInteger i = 0; i < count; ++i) {
  1392. //% if (!objects[i]) {
  1393. //% [NSException raise:NSInvalidArgumentException
  1394. //% format:@"Attempting to add nil object to a Dictionary"];
  1395. //% }
  1396. //% int idx = keys[i] ? 1 : 0;
  1397. //% [_values[idx] release];
  1398. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1399. //% }
  1400. //% }
  1401. //% return self;
  1402. //%}
  1403. //%
  1404. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1405. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1406. //% if (self) {
  1407. //% if (dictionary) {
  1408. //% _values[0] = [dictionary->_values[0] retain];
  1409. //% _values[1] = [dictionary->_values[1] retain];
  1410. //% }
  1411. //% }
  1412. //% return self;
  1413. //%}
  1414. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1415. //%- (void)dealloc {
  1416. //% NSAssert(!_autocreator,
  1417. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1418. //% [self class], _autocreator);
  1419. //% [_values[0] release];
  1420. //% [_values[1] release];
  1421. //% [super dealloc];
  1422. //%}
  1423. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1424. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1425. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1426. //%REF##_values[IDX] != nil
  1427. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
  1428. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1429. //% return _values[key ? 1 : 0];
  1430. //%}
  1431. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1432. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1433. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1434. //% int idx = (key->valueBool ? 1 : 0);
  1435. //% [_values[idx] release];
  1436. //% _values[idx] = [value->valueString retain];
  1437. //%}
  1438. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1439. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1440. //% if (otherDictionary) {
  1441. //% for (int i = 0; i < 2; ++i) {
  1442. //% if (otherDictionary->_values[i] != nil) {
  1443. //% [_values[i] release];
  1444. //% _values[i] = [otherDictionary->_values[i] retain];
  1445. //% }
  1446. //% }
  1447. //% if (_autocreator) {
  1448. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1449. //% }
  1450. //% }
  1451. //%}
  1452. //%
  1453. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1454. //% if (!object) {
  1455. //% [NSException raise:NSInvalidArgumentException
  1456. //% format:@"Attempting to add nil object to a Dictionary"];
  1457. //% }
  1458. //% int idx = (key ? 1 : 0);
  1459. //% [_values[idx] release];
  1460. //% _values[idx] = [object retain];
  1461. //% if (_autocreator) {
  1462. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1463. //% }
  1464. //%}
  1465. //%
  1466. //%- (void)removeObjectForKey:(BOOL)aKey {
  1467. //% int idx = (aKey ? 1 : 0);
  1468. //% [_values[idx] release];
  1469. //% _values[idx] = nil;
  1470. //%}
  1471. //%
  1472. //%- (void)removeAll {
  1473. //% for (int i = 0; i < 2; ++i) {
  1474. //% [_values[i] release];
  1475. //% _values[i] = nil;
  1476. //% }
  1477. //%}
  1478. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1479. //%%@
  1480. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1481. // This block of code is generated, do not edit it directly.
  1482. #pragma mark - UInt32 -> UInt32
  1483. @implementation GPBUInt32UInt32Dictionary {
  1484. @package
  1485. NSMutableDictionary *_dictionary;
  1486. }
  1487. + (instancetype)dictionary {
  1488. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  1489. }
  1490. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  1491. forKey:(uint32_t)key {
  1492. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  1493. // on to get the type correct.
  1494. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  1495. forKeys:&key
  1496. count:1] autorelease];
  1497. }
  1498. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  1499. forKeys:(const uint32_t [])keys
  1500. count:(NSUInteger)count {
  1501. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  1502. // on to get the type correct.
  1503. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
  1504. forKeys:keys
  1505. count:count] autorelease];
  1506. }
  1507. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1508. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1509. // on to get the type correct.
  1510. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1511. }
  1512. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1513. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1514. }
  1515. - (instancetype)init {
  1516. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1517. }
  1518. - (instancetype)initWithUInt32s:(const uint32_t [])values
  1519. forKeys:(const uint32_t [])keys
  1520. count:(NSUInteger)count {
  1521. self = [super init];
  1522. if (self) {
  1523. _dictionary = [[NSMutableDictionary alloc] init];
  1524. if (count && values && keys) {
  1525. for (NSUInteger i = 0; i < count; ++i) {
  1526. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1527. }
  1528. }
  1529. }
  1530. return self;
  1531. }
  1532. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1533. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  1534. if (self) {
  1535. if (dictionary) {
  1536. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1537. }
  1538. }
  1539. return self;
  1540. }
  1541. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1542. #pragma unused(numItems)
  1543. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1544. }
  1545. - (void)dealloc {
  1546. NSAssert(!_autocreator,
  1547. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1548. [self class], _autocreator);
  1549. [_dictionary release];
  1550. [super dealloc];
  1551. }
  1552. - (instancetype)copyWithZone:(NSZone *)zone {
  1553. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1554. }
  1555. - (BOOL)isEqual:(id)other {
  1556. if (self == other) {
  1557. return YES;
  1558. }
  1559. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1560. return NO;
  1561. }
  1562. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1563. return [_dictionary isEqual:otherDictionary->_dictionary];
  1564. }
  1565. - (NSUInteger)hash {
  1566. return _dictionary.count;
  1567. }
  1568. - (NSString *)description {
  1569. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1570. }
  1571. - (NSUInteger)count {
  1572. return _dictionary.count;
  1573. }
  1574. - (void)enumerateKeysAndUInt32sUsingBlock:
  1575. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1576. BOOL stop = NO;
  1577. NSDictionary *internal = _dictionary;
  1578. NSEnumerator *keys = [internal keyEnumerator];
  1579. NSNumber *aKey;
  1580. while ((aKey = [keys nextObject])) {
  1581. NSNumber *aValue = internal[aKey];
  1582. block([aKey unsignedIntValue], [aValue unsignedIntValue], &stop);
  1583. if (stop) {
  1584. break;
  1585. }
  1586. }
  1587. }
  1588. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1589. NSDictionary *internal = _dictionary;
  1590. NSUInteger count = internal.count;
  1591. if (count == 0) {
  1592. return 0;
  1593. }
  1594. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1595. GPBDataType keyDataType = field.mapKeyDataType;
  1596. size_t result = 0;
  1597. NSEnumerator *keys = [internal keyEnumerator];
  1598. NSNumber *aKey;
  1599. while ((aKey = [keys nextObject])) {
  1600. NSNumber *aValue = internal[aKey];
  1601. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1602. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1603. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1604. }
  1605. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1606. result += tagSize * count;
  1607. return result;
  1608. }
  1609. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1610. asField:(GPBFieldDescriptor *)field {
  1611. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1612. GPBDataType keyDataType = field.mapKeyDataType;
  1613. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1614. NSDictionary *internal = _dictionary;
  1615. NSEnumerator *keys = [internal keyEnumerator];
  1616. NSNumber *aKey;
  1617. while ((aKey = [keys nextObject])) {
  1618. NSNumber *aValue = internal[aKey];
  1619. [outputStream writeInt32NoTag:tag];
  1620. // Write the size of the message.
  1621. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1622. uint32_t unwrappedValue = [aValue unsignedIntValue];
  1623. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1624. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1625. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1626. // Write the fields.
  1627. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1628. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1629. }
  1630. }
  1631. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1632. forGPBGenericValueKey:(GPBGenericValue *)key {
  1633. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1634. }
  1635. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1636. [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1637. #pragma unused(stop)
  1638. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1639. }];
  1640. }
  1641. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
  1642. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1643. if (wrapped && value) {
  1644. *value = [wrapped unsignedIntValue];
  1645. }
  1646. return (wrapped != NULL);
  1647. }
  1648. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1649. if (otherDictionary) {
  1650. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1651. if (_autocreator) {
  1652. GPBAutocreatedDictionaryModified(_autocreator, self);
  1653. }
  1654. }
  1655. }
  1656. - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
  1657. [_dictionary setObject:@(value) forKey:@(key)];
  1658. if (_autocreator) {
  1659. GPBAutocreatedDictionaryModified(_autocreator, self);
  1660. }
  1661. }
  1662. - (void)removeUInt32ForKey:(uint32_t)aKey {
  1663. [_dictionary removeObjectForKey:@(aKey)];
  1664. }
  1665. - (void)removeAll {
  1666. [_dictionary removeAllObjects];
  1667. }
  1668. @end
  1669. #pragma mark - UInt32 -> Int32
  1670. @implementation GPBUInt32Int32Dictionary {
  1671. @package
  1672. NSMutableDictionary *_dictionary;
  1673. }
  1674. + (instancetype)dictionary {
  1675. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  1676. }
  1677. + (instancetype)dictionaryWithInt32:(int32_t)value
  1678. forKey:(uint32_t)key {
  1679. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  1680. // on to get the type correct.
  1681. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
  1682. forKeys:&key
  1683. count:1] autorelease];
  1684. }
  1685. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  1686. forKeys:(const uint32_t [])keys
  1687. count:(NSUInteger)count {
  1688. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  1689. // on to get the type correct.
  1690. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:values
  1691. forKeys:keys
  1692. count:count] autorelease];
  1693. }
  1694. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1695. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1696. // on to get the type correct.
  1697. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1698. }
  1699. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1700. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1701. }
  1702. - (instancetype)init {
  1703. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1704. }
  1705. - (instancetype)initWithInt32s:(const int32_t [])values
  1706. forKeys:(const uint32_t [])keys
  1707. count:(NSUInteger)count {
  1708. self = [super init];
  1709. if (self) {
  1710. _dictionary = [[NSMutableDictionary alloc] init];
  1711. if (count && values && keys) {
  1712. for (NSUInteger i = 0; i < count; ++i) {
  1713. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1714. }
  1715. }
  1716. }
  1717. return self;
  1718. }
  1719. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1720. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  1721. if (self) {
  1722. if (dictionary) {
  1723. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1724. }
  1725. }
  1726. return self;
  1727. }
  1728. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1729. #pragma unused(numItems)
  1730. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1731. }
  1732. - (void)dealloc {
  1733. NSAssert(!_autocreator,
  1734. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1735. [self class], _autocreator);
  1736. [_dictionary release];
  1737. [super dealloc];
  1738. }
  1739. - (instancetype)copyWithZone:(NSZone *)zone {
  1740. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1741. }
  1742. - (BOOL)isEqual:(id)other {
  1743. if (self == other) {
  1744. return YES;
  1745. }
  1746. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1747. return NO;
  1748. }
  1749. GPBUInt32Int32Dictionary *otherDictionary = other;
  1750. return [_dictionary isEqual:otherDictionary->_dictionary];
  1751. }
  1752. - (NSUInteger)hash {
  1753. return _dictionary.count;
  1754. }
  1755. - (NSString *)description {
  1756. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1757. }
  1758. - (NSUInteger)count {
  1759. return _dictionary.count;
  1760. }
  1761. - (void)enumerateKeysAndInt32sUsingBlock:
  1762. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1763. BOOL stop = NO;
  1764. NSDictionary *internal = _dictionary;
  1765. NSEnumerator *keys = [internal keyEnumerator];
  1766. NSNumber *aKey;
  1767. while ((aKey = [keys nextObject])) {
  1768. NSNumber *aValue = internal[aKey];
  1769. block([aKey unsignedIntValue], [aValue intValue], &stop);
  1770. if (stop) {
  1771. break;
  1772. }
  1773. }
  1774. }
  1775. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1776. NSDictionary *internal = _dictionary;
  1777. NSUInteger count = internal.count;
  1778. if (count == 0) {
  1779. return 0;
  1780. }
  1781. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1782. GPBDataType keyDataType = field.mapKeyDataType;
  1783. size_t result = 0;
  1784. NSEnumerator *keys = [internal keyEnumerator];
  1785. NSNumber *aKey;
  1786. while ((aKey = [keys nextObject])) {
  1787. NSNumber *aValue = internal[aKey];
  1788. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1789. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1790. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1791. }
  1792. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1793. result += tagSize * count;
  1794. return result;
  1795. }
  1796. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1797. asField:(GPBFieldDescriptor *)field {
  1798. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1799. GPBDataType keyDataType = field.mapKeyDataType;
  1800. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1801. NSDictionary *internal = _dictionary;
  1802. NSEnumerator *keys = [internal keyEnumerator];
  1803. NSNumber *aKey;
  1804. while ((aKey = [keys nextObject])) {
  1805. NSNumber *aValue = internal[aKey];
  1806. [outputStream writeInt32NoTag:tag];
  1807. // Write the size of the message.
  1808. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1809. int32_t unwrappedValue = [aValue intValue];
  1810. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1811. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1812. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1813. // Write the fields.
  1814. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1815. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1816. }
  1817. }
  1818. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1819. forGPBGenericValueKey:(GPBGenericValue *)key {
  1820. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1821. }
  1822. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1823. [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1824. #pragma unused(stop)
  1825. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1826. }];
  1827. }
  1828. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
  1829. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1830. if (wrapped && value) {
  1831. *value = [wrapped intValue];
  1832. }
  1833. return (wrapped != NULL);
  1834. }
  1835. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1836. if (otherDictionary) {
  1837. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1838. if (_autocreator) {
  1839. GPBAutocreatedDictionaryModified(_autocreator, self);
  1840. }
  1841. }
  1842. }
  1843. - (void)setInt32:(int32_t)value forKey:(uint32_t)key {
  1844. [_dictionary setObject:@(value) forKey:@(key)];
  1845. if (_autocreator) {
  1846. GPBAutocreatedDictionaryModified(_autocreator, self);
  1847. }
  1848. }
  1849. - (void)removeInt32ForKey:(uint32_t)aKey {
  1850. [_dictionary removeObjectForKey:@(aKey)];
  1851. }
  1852. - (void)removeAll {
  1853. [_dictionary removeAllObjects];
  1854. }
  1855. @end
  1856. #pragma mark - UInt32 -> UInt64
  1857. @implementation GPBUInt32UInt64Dictionary {
  1858. @package
  1859. NSMutableDictionary *_dictionary;
  1860. }
  1861. + (instancetype)dictionary {
  1862. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  1863. }
  1864. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  1865. forKey:(uint32_t)key {
  1866. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  1867. // on to get the type correct.
  1868. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  1869. forKeys:&key
  1870. count:1] autorelease];
  1871. }
  1872. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  1873. forKeys:(const uint32_t [])keys
  1874. count:(NSUInteger)count {
  1875. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  1876. // on to get the type correct.
  1877. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
  1878. forKeys:keys
  1879. count:count] autorelease];
  1880. }
  1881. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1882. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1883. // on to get the type correct.
  1884. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1885. }
  1886. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1887. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1888. }
  1889. - (instancetype)init {
  1890. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1891. }
  1892. - (instancetype)initWithUInt64s:(const uint64_t [])values
  1893. forKeys:(const uint32_t [])keys
  1894. count:(NSUInteger)count {
  1895. self = [super init];
  1896. if (self) {
  1897. _dictionary = [[NSMutableDictionary alloc] init];
  1898. if (count && values && keys) {
  1899. for (NSUInteger i = 0; i < count; ++i) {
  1900. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1901. }
  1902. }
  1903. }
  1904. return self;
  1905. }
  1906. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1907. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  1908. if (self) {
  1909. if (dictionary) {
  1910. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1911. }
  1912. }
  1913. return self;
  1914. }
  1915. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1916. #pragma unused(numItems)
  1917. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1918. }
  1919. - (void)dealloc {
  1920. NSAssert(!_autocreator,
  1921. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1922. [self class], _autocreator);
  1923. [_dictionary release];
  1924. [super dealloc];
  1925. }
  1926. - (instancetype)copyWithZone:(NSZone *)zone {
  1927. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1928. }
  1929. - (BOOL)isEqual:(id)other {
  1930. if (self == other) {
  1931. return YES;
  1932. }
  1933. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1934. return NO;
  1935. }
  1936. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1937. return [_dictionary isEqual:otherDictionary->_dictionary];
  1938. }
  1939. - (NSUInteger)hash {
  1940. return _dictionary.count;
  1941. }
  1942. - (NSString *)description {
  1943. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1944. }
  1945. - (NSUInteger)count {
  1946. return _dictionary.count;
  1947. }
  1948. - (void)enumerateKeysAndUInt64sUsingBlock:
  1949. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1950. BOOL stop = NO;
  1951. NSDictionary *internal = _dictionary;
  1952. NSEnumerator *keys = [internal keyEnumerator];
  1953. NSNumber *aKey;
  1954. while ((aKey = [keys nextObject])) {
  1955. NSNumber *aValue = internal[aKey];
  1956. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], &stop);
  1957. if (stop) {
  1958. break;
  1959. }
  1960. }
  1961. }
  1962. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1963. NSDictionary *internal = _dictionary;
  1964. NSUInteger count = internal.count;
  1965. if (count == 0) {
  1966. return 0;
  1967. }
  1968. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1969. GPBDataType keyDataType = field.mapKeyDataType;
  1970. size_t result = 0;
  1971. NSEnumerator *keys = [internal keyEnumerator];
  1972. NSNumber *aKey;
  1973. while ((aKey = [keys nextObject])) {
  1974. NSNumber *aValue = internal[aKey];
  1975. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1976. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1977. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1978. }
  1979. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1980. result += tagSize * count;
  1981. return result;
  1982. }
  1983. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1984. asField:(GPBFieldDescriptor *)field {
  1985. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1986. GPBDataType keyDataType = field.mapKeyDataType;
  1987. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1988. NSDictionary *internal = _dictionary;
  1989. NSEnumerator *keys = [internal keyEnumerator];
  1990. NSNumber *aKey;
  1991. while ((aKey = [keys nextObject])) {
  1992. NSNumber *aValue = internal[aKey];
  1993. [outputStream writeInt32NoTag:tag];
  1994. // Write the size of the message.
  1995. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1996. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  1997. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1998. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1999. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2000. // Write the fields.
  2001. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2002. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2003. }
  2004. }
  2005. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2006. forGPBGenericValueKey:(GPBGenericValue *)key {
  2007. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  2008. }
  2009. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2010. [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  2011. #pragma unused(stop)
  2012. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  2013. }];
  2014. }
  2015. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
  2016. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2017. if (wrapped && value) {
  2018. *value = [wrapped unsignedLongLongValue];
  2019. }
  2020. return (wrapped != NULL);
  2021. }
  2022. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  2023. if (otherDictionary) {
  2024. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2025. if (_autocreator) {
  2026. GPBAutocreatedDictionaryModified(_autocreator, self);
  2027. }
  2028. }
  2029. }
  2030. - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
  2031. [_dictionary setObject:@(value) forKey:@(key)];
  2032. if (_autocreator) {
  2033. GPBAutocreatedDictionaryModified(_autocreator, self);
  2034. }
  2035. }
  2036. - (void)removeUInt64ForKey:(uint32_t)aKey {
  2037. [_dictionary removeObjectForKey:@(aKey)];
  2038. }
  2039. - (void)removeAll {
  2040. [_dictionary removeAllObjects];
  2041. }
  2042. @end
  2043. #pragma mark - UInt32 -> Int64
  2044. @implementation GPBUInt32Int64Dictionary {
  2045. @package
  2046. NSMutableDictionary *_dictionary;
  2047. }
  2048. + (instancetype)dictionary {
  2049. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  2050. }
  2051. + (instancetype)dictionaryWithInt64:(int64_t)value
  2052. forKey:(uint32_t)key {
  2053. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  2054. // on to get the type correct.
  2055. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
  2056. forKeys:&key
  2057. count:1] autorelease];
  2058. }
  2059. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  2060. forKeys:(const uint32_t [])keys
  2061. count:(NSUInteger)count {
  2062. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  2063. // on to get the type correct.
  2064. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:values
  2065. forKeys:keys
  2066. count:count] autorelease];
  2067. }
  2068. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2069. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2070. // on to get the type correct.
  2071. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2072. }
  2073. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2074. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2075. }
  2076. - (instancetype)init {
  2077. return [self initWithInt64s:NULL forKeys:NULL count:0];
  2078. }
  2079. - (instancetype)initWithInt64s:(const int64_t [])values
  2080. forKeys:(const uint32_t [])keys
  2081. count:(NSUInteger)count {
  2082. self = [super init];
  2083. if (self) {
  2084. _dictionary = [[NSMutableDictionary alloc] init];
  2085. if (count && values && keys) {
  2086. for (NSUInteger i = 0; i < count; ++i) {
  2087. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2088. }
  2089. }
  2090. }
  2091. return self;
  2092. }
  2093. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2094. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  2095. if (self) {
  2096. if (dictionary) {
  2097. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2098. }
  2099. }
  2100. return self;
  2101. }
  2102. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2103. #pragma unused(numItems)
  2104. return [self initWithInt64s:NULL forKeys:NULL count:0];
  2105. }
  2106. - (void)dealloc {
  2107. NSAssert(!_autocreator,
  2108. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2109. [self class], _autocreator);
  2110. [_dictionary release];
  2111. [super dealloc];
  2112. }
  2113. - (instancetype)copyWithZone:(NSZone *)zone {
  2114. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2115. }
  2116. - (BOOL)isEqual:(id)other {
  2117. if (self == other) {
  2118. return YES;
  2119. }
  2120. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2121. return NO;
  2122. }
  2123. GPBUInt32Int64Dictionary *otherDictionary = other;
  2124. return [_dictionary isEqual:otherDictionary->_dictionary];
  2125. }
  2126. - (NSUInteger)hash {
  2127. return _dictionary.count;
  2128. }
  2129. - (NSString *)description {
  2130. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2131. }
  2132. - (NSUInteger)count {
  2133. return _dictionary.count;
  2134. }
  2135. - (void)enumerateKeysAndInt64sUsingBlock:
  2136. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2137. BOOL stop = NO;
  2138. NSDictionary *internal = _dictionary;
  2139. NSEnumerator *keys = [internal keyEnumerator];
  2140. NSNumber *aKey;
  2141. while ((aKey = [keys nextObject])) {
  2142. NSNumber *aValue = internal[aKey];
  2143. block([aKey unsignedIntValue], [aValue longLongValue], &stop);
  2144. if (stop) {
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2150. NSDictionary *internal = _dictionary;
  2151. NSUInteger count = internal.count;
  2152. if (count == 0) {
  2153. return 0;
  2154. }
  2155. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2156. GPBDataType keyDataType = field.mapKeyDataType;
  2157. size_t result = 0;
  2158. NSEnumerator *keys = [internal keyEnumerator];
  2159. NSNumber *aKey;
  2160. while ((aKey = [keys nextObject])) {
  2161. NSNumber *aValue = internal[aKey];
  2162. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2163. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2164. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2165. }
  2166. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2167. result += tagSize * count;
  2168. return result;
  2169. }
  2170. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2171. asField:(GPBFieldDescriptor *)field {
  2172. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2173. GPBDataType keyDataType = field.mapKeyDataType;
  2174. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2175. NSDictionary *internal = _dictionary;
  2176. NSEnumerator *keys = [internal keyEnumerator];
  2177. NSNumber *aKey;
  2178. while ((aKey = [keys nextObject])) {
  2179. NSNumber *aValue = internal[aKey];
  2180. [outputStream writeInt32NoTag:tag];
  2181. // Write the size of the message.
  2182. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2183. int64_t unwrappedValue = [aValue longLongValue];
  2184. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2185. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2186. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2187. // Write the fields.
  2188. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2189. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2190. }
  2191. }
  2192. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2193. forGPBGenericValueKey:(GPBGenericValue *)key {
  2194. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2195. }
  2196. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2197. [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2198. #pragma unused(stop)
  2199. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2200. }];
  2201. }
  2202. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
  2203. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2204. if (wrapped && value) {
  2205. *value = [wrapped longLongValue];
  2206. }
  2207. return (wrapped != NULL);
  2208. }
  2209. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2210. if (otherDictionary) {
  2211. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2212. if (_autocreator) {
  2213. GPBAutocreatedDictionaryModified(_autocreator, self);
  2214. }
  2215. }
  2216. }
  2217. - (void)setInt64:(int64_t)value forKey:(uint32_t)key {
  2218. [_dictionary setObject:@(value) forKey:@(key)];
  2219. if (_autocreator) {
  2220. GPBAutocreatedDictionaryModified(_autocreator, self);
  2221. }
  2222. }
  2223. - (void)removeInt64ForKey:(uint32_t)aKey {
  2224. [_dictionary removeObjectForKey:@(aKey)];
  2225. }
  2226. - (void)removeAll {
  2227. [_dictionary removeAllObjects];
  2228. }
  2229. @end
  2230. #pragma mark - UInt32 -> Bool
  2231. @implementation GPBUInt32BoolDictionary {
  2232. @package
  2233. NSMutableDictionary *_dictionary;
  2234. }
  2235. + (instancetype)dictionary {
  2236. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  2237. }
  2238. + (instancetype)dictionaryWithBool:(BOOL)value
  2239. forKey:(uint32_t)key {
  2240. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  2241. // on to get the type correct.
  2242. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:&value
  2243. forKeys:&key
  2244. count:1] autorelease];
  2245. }
  2246. + (instancetype)dictionaryWithBools:(const BOOL [])values
  2247. forKeys:(const uint32_t [])keys
  2248. count:(NSUInteger)count {
  2249. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  2250. // on to get the type correct.
  2251. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:values
  2252. forKeys:keys
  2253. count:count] autorelease];
  2254. }
  2255. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2256. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2257. // on to get the type correct.
  2258. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2259. }
  2260. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2261. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2262. }
  2263. - (instancetype)init {
  2264. return [self initWithBools:NULL forKeys:NULL count:0];
  2265. }
  2266. - (instancetype)initWithBools:(const BOOL [])values
  2267. forKeys:(const uint32_t [])keys
  2268. count:(NSUInteger)count {
  2269. self = [super init];
  2270. if (self) {
  2271. _dictionary = [[NSMutableDictionary alloc] init];
  2272. if (count && values && keys) {
  2273. for (NSUInteger i = 0; i < count; ++i) {
  2274. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2275. }
  2276. }
  2277. }
  2278. return self;
  2279. }
  2280. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2281. self = [self initWithBools:NULL forKeys:NULL count:0];
  2282. if (self) {
  2283. if (dictionary) {
  2284. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2285. }
  2286. }
  2287. return self;
  2288. }
  2289. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2290. #pragma unused(numItems)
  2291. return [self initWithBools:NULL forKeys:NULL count:0];
  2292. }
  2293. - (void)dealloc {
  2294. NSAssert(!_autocreator,
  2295. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2296. [self class], _autocreator);
  2297. [_dictionary release];
  2298. [super dealloc];
  2299. }
  2300. - (instancetype)copyWithZone:(NSZone *)zone {
  2301. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2302. }
  2303. - (BOOL)isEqual:(id)other {
  2304. if (self == other) {
  2305. return YES;
  2306. }
  2307. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2308. return NO;
  2309. }
  2310. GPBUInt32BoolDictionary *otherDictionary = other;
  2311. return [_dictionary isEqual:otherDictionary->_dictionary];
  2312. }
  2313. - (NSUInteger)hash {
  2314. return _dictionary.count;
  2315. }
  2316. - (NSString *)description {
  2317. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2318. }
  2319. - (NSUInteger)count {
  2320. return _dictionary.count;
  2321. }
  2322. - (void)enumerateKeysAndBoolsUsingBlock:
  2323. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2324. BOOL stop = NO;
  2325. NSDictionary *internal = _dictionary;
  2326. NSEnumerator *keys = [internal keyEnumerator];
  2327. NSNumber *aKey;
  2328. while ((aKey = [keys nextObject])) {
  2329. NSNumber *aValue = internal[aKey];
  2330. block([aKey unsignedIntValue], [aValue boolValue], &stop);
  2331. if (stop) {
  2332. break;
  2333. }
  2334. }
  2335. }
  2336. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2337. NSDictionary *internal = _dictionary;
  2338. NSUInteger count = internal.count;
  2339. if (count == 0) {
  2340. return 0;
  2341. }
  2342. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2343. GPBDataType keyDataType = field.mapKeyDataType;
  2344. size_t result = 0;
  2345. NSEnumerator *keys = [internal keyEnumerator];
  2346. NSNumber *aKey;
  2347. while ((aKey = [keys nextObject])) {
  2348. NSNumber *aValue = internal[aKey];
  2349. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2350. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2351. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2352. }
  2353. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2354. result += tagSize * count;
  2355. return result;
  2356. }
  2357. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2358. asField:(GPBFieldDescriptor *)field {
  2359. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2360. GPBDataType keyDataType = field.mapKeyDataType;
  2361. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2362. NSDictionary *internal = _dictionary;
  2363. NSEnumerator *keys = [internal keyEnumerator];
  2364. NSNumber *aKey;
  2365. while ((aKey = [keys nextObject])) {
  2366. NSNumber *aValue = internal[aKey];
  2367. [outputStream writeInt32NoTag:tag];
  2368. // Write the size of the message.
  2369. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2370. BOOL unwrappedValue = [aValue boolValue];
  2371. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2372. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2373. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2374. // Write the fields.
  2375. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2376. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2377. }
  2378. }
  2379. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2380. forGPBGenericValueKey:(GPBGenericValue *)key {
  2381. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2382. }
  2383. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2384. [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2385. #pragma unused(stop)
  2386. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2387. }];
  2388. }
  2389. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
  2390. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2391. if (wrapped && value) {
  2392. *value = [wrapped boolValue];
  2393. }
  2394. return (wrapped != NULL);
  2395. }
  2396. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2397. if (otherDictionary) {
  2398. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2399. if (_autocreator) {
  2400. GPBAutocreatedDictionaryModified(_autocreator, self);
  2401. }
  2402. }
  2403. }
  2404. - (void)setBool:(BOOL)value forKey:(uint32_t)key {
  2405. [_dictionary setObject:@(value) forKey:@(key)];
  2406. if (_autocreator) {
  2407. GPBAutocreatedDictionaryModified(_autocreator, self);
  2408. }
  2409. }
  2410. - (void)removeBoolForKey:(uint32_t)aKey {
  2411. [_dictionary removeObjectForKey:@(aKey)];
  2412. }
  2413. - (void)removeAll {
  2414. [_dictionary removeAllObjects];
  2415. }
  2416. @end
  2417. #pragma mark - UInt32 -> Float
  2418. @implementation GPBUInt32FloatDictionary {
  2419. @package
  2420. NSMutableDictionary *_dictionary;
  2421. }
  2422. + (instancetype)dictionary {
  2423. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  2424. }
  2425. + (instancetype)dictionaryWithFloat:(float)value
  2426. forKey:(uint32_t)key {
  2427. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  2428. // on to get the type correct.
  2429. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:&value
  2430. forKeys:&key
  2431. count:1] autorelease];
  2432. }
  2433. + (instancetype)dictionaryWithFloats:(const float [])values
  2434. forKeys:(const uint32_t [])keys
  2435. count:(NSUInteger)count {
  2436. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  2437. // on to get the type correct.
  2438. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:values
  2439. forKeys:keys
  2440. count:count] autorelease];
  2441. }
  2442. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2443. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2444. // on to get the type correct.
  2445. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2446. }
  2447. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2448. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2449. }
  2450. - (instancetype)init {
  2451. return [self initWithFloats:NULL forKeys:NULL count:0];
  2452. }
  2453. - (instancetype)initWithFloats:(const float [])values
  2454. forKeys:(const uint32_t [])keys
  2455. count:(NSUInteger)count {
  2456. self = [super init];
  2457. if (self) {
  2458. _dictionary = [[NSMutableDictionary alloc] init];
  2459. if (count && values && keys) {
  2460. for (NSUInteger i = 0; i < count; ++i) {
  2461. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2462. }
  2463. }
  2464. }
  2465. return self;
  2466. }
  2467. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2468. self = [self initWithFloats:NULL forKeys:NULL count:0];
  2469. if (self) {
  2470. if (dictionary) {
  2471. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2472. }
  2473. }
  2474. return self;
  2475. }
  2476. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2477. #pragma unused(numItems)
  2478. return [self initWithFloats:NULL forKeys:NULL count:0];
  2479. }
  2480. - (void)dealloc {
  2481. NSAssert(!_autocreator,
  2482. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2483. [self class], _autocreator);
  2484. [_dictionary release];
  2485. [super dealloc];
  2486. }
  2487. - (instancetype)copyWithZone:(NSZone *)zone {
  2488. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2489. }
  2490. - (BOOL)isEqual:(id)other {
  2491. if (self == other) {
  2492. return YES;
  2493. }
  2494. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2495. return NO;
  2496. }
  2497. GPBUInt32FloatDictionary *otherDictionary = other;
  2498. return [_dictionary isEqual:otherDictionary->_dictionary];
  2499. }
  2500. - (NSUInteger)hash {
  2501. return _dictionary.count;
  2502. }
  2503. - (NSString *)description {
  2504. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2505. }
  2506. - (NSUInteger)count {
  2507. return _dictionary.count;
  2508. }
  2509. - (void)enumerateKeysAndFloatsUsingBlock:
  2510. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2511. BOOL stop = NO;
  2512. NSDictionary *internal = _dictionary;
  2513. NSEnumerator *keys = [internal keyEnumerator];
  2514. NSNumber *aKey;
  2515. while ((aKey = [keys nextObject])) {
  2516. NSNumber *aValue = internal[aKey];
  2517. block([aKey unsignedIntValue], [aValue floatValue], &stop);
  2518. if (stop) {
  2519. break;
  2520. }
  2521. }
  2522. }
  2523. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2524. NSDictionary *internal = _dictionary;
  2525. NSUInteger count = internal.count;
  2526. if (count == 0) {
  2527. return 0;
  2528. }
  2529. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2530. GPBDataType keyDataType = field.mapKeyDataType;
  2531. size_t result = 0;
  2532. NSEnumerator *keys = [internal keyEnumerator];
  2533. NSNumber *aKey;
  2534. while ((aKey = [keys nextObject])) {
  2535. NSNumber *aValue = internal[aKey];
  2536. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2537. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2538. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2539. }
  2540. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2541. result += tagSize * count;
  2542. return result;
  2543. }
  2544. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2545. asField:(GPBFieldDescriptor *)field {
  2546. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2547. GPBDataType keyDataType = field.mapKeyDataType;
  2548. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2549. NSDictionary *internal = _dictionary;
  2550. NSEnumerator *keys = [internal keyEnumerator];
  2551. NSNumber *aKey;
  2552. while ((aKey = [keys nextObject])) {
  2553. NSNumber *aValue = internal[aKey];
  2554. [outputStream writeInt32NoTag:tag];
  2555. // Write the size of the message.
  2556. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2557. float unwrappedValue = [aValue floatValue];
  2558. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2559. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2560. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2561. // Write the fields.
  2562. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2563. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2564. }
  2565. }
  2566. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2567. forGPBGenericValueKey:(GPBGenericValue *)key {
  2568. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2569. }
  2570. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2571. [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2572. #pragma unused(stop)
  2573. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2574. }];
  2575. }
  2576. - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
  2577. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2578. if (wrapped && value) {
  2579. *value = [wrapped floatValue];
  2580. }
  2581. return (wrapped != NULL);
  2582. }
  2583. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2584. if (otherDictionary) {
  2585. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2586. if (_autocreator) {
  2587. GPBAutocreatedDictionaryModified(_autocreator, self);
  2588. }
  2589. }
  2590. }
  2591. - (void)setFloat:(float)value forKey:(uint32_t)key {
  2592. [_dictionary setObject:@(value) forKey:@(key)];
  2593. if (_autocreator) {
  2594. GPBAutocreatedDictionaryModified(_autocreator, self);
  2595. }
  2596. }
  2597. - (void)removeFloatForKey:(uint32_t)aKey {
  2598. [_dictionary removeObjectForKey:@(aKey)];
  2599. }
  2600. - (void)removeAll {
  2601. [_dictionary removeAllObjects];
  2602. }
  2603. @end
  2604. #pragma mark - UInt32 -> Double
  2605. @implementation GPBUInt32DoubleDictionary {
  2606. @package
  2607. NSMutableDictionary *_dictionary;
  2608. }
  2609. + (instancetype)dictionary {
  2610. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  2611. }
  2612. + (instancetype)dictionaryWithDouble:(double)value
  2613. forKey:(uint32_t)key {
  2614. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  2615. // on to get the type correct.
  2616. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
  2617. forKeys:&key
  2618. count:1] autorelease];
  2619. }
  2620. + (instancetype)dictionaryWithDoubles:(const double [])values
  2621. forKeys:(const uint32_t [])keys
  2622. count:(NSUInteger)count {
  2623. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  2624. // on to get the type correct.
  2625. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:values
  2626. forKeys:keys
  2627. count:count] autorelease];
  2628. }
  2629. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2630. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2631. // on to get the type correct.
  2632. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2633. }
  2634. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2635. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2636. }
  2637. - (instancetype)init {
  2638. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2639. }
  2640. - (instancetype)initWithDoubles:(const double [])values
  2641. forKeys:(const uint32_t [])keys
  2642. count:(NSUInteger)count {
  2643. self = [super init];
  2644. if (self) {
  2645. _dictionary = [[NSMutableDictionary alloc] init];
  2646. if (count && values && keys) {
  2647. for (NSUInteger i = 0; i < count; ++i) {
  2648. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2649. }
  2650. }
  2651. }
  2652. return self;
  2653. }
  2654. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2655. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  2656. if (self) {
  2657. if (dictionary) {
  2658. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2659. }
  2660. }
  2661. return self;
  2662. }
  2663. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2664. #pragma unused(numItems)
  2665. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2666. }
  2667. - (void)dealloc {
  2668. NSAssert(!_autocreator,
  2669. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2670. [self class], _autocreator);
  2671. [_dictionary release];
  2672. [super dealloc];
  2673. }
  2674. - (instancetype)copyWithZone:(NSZone *)zone {
  2675. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2676. }
  2677. - (BOOL)isEqual:(id)other {
  2678. if (self == other) {
  2679. return YES;
  2680. }
  2681. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2682. return NO;
  2683. }
  2684. GPBUInt32DoubleDictionary *otherDictionary = other;
  2685. return [_dictionary isEqual:otherDictionary->_dictionary];
  2686. }
  2687. - (NSUInteger)hash {
  2688. return _dictionary.count;
  2689. }
  2690. - (NSString *)description {
  2691. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2692. }
  2693. - (NSUInteger)count {
  2694. return _dictionary.count;
  2695. }
  2696. - (void)enumerateKeysAndDoublesUsingBlock:
  2697. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2698. BOOL stop = NO;
  2699. NSDictionary *internal = _dictionary;
  2700. NSEnumerator *keys = [internal keyEnumerator];
  2701. NSNumber *aKey;
  2702. while ((aKey = [keys nextObject])) {
  2703. NSNumber *aValue = internal[aKey];
  2704. block([aKey unsignedIntValue], [aValue doubleValue], &stop);
  2705. if (stop) {
  2706. break;
  2707. }
  2708. }
  2709. }
  2710. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2711. NSDictionary *internal = _dictionary;
  2712. NSUInteger count = internal.count;
  2713. if (count == 0) {
  2714. return 0;
  2715. }
  2716. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2717. GPBDataType keyDataType = field.mapKeyDataType;
  2718. size_t result = 0;
  2719. NSEnumerator *keys = [internal keyEnumerator];
  2720. NSNumber *aKey;
  2721. while ((aKey = [keys nextObject])) {
  2722. NSNumber *aValue = internal[aKey];
  2723. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2724. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2725. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2726. }
  2727. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2728. result += tagSize * count;
  2729. return result;
  2730. }
  2731. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2732. asField:(GPBFieldDescriptor *)field {
  2733. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2734. GPBDataType keyDataType = field.mapKeyDataType;
  2735. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2736. NSDictionary *internal = _dictionary;
  2737. NSEnumerator *keys = [internal keyEnumerator];
  2738. NSNumber *aKey;
  2739. while ((aKey = [keys nextObject])) {
  2740. NSNumber *aValue = internal[aKey];
  2741. [outputStream writeInt32NoTag:tag];
  2742. // Write the size of the message.
  2743. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2744. double unwrappedValue = [aValue doubleValue];
  2745. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2746. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2747. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2748. // Write the fields.
  2749. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2750. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2751. }
  2752. }
  2753. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2754. forGPBGenericValueKey:(GPBGenericValue *)key {
  2755. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2756. }
  2757. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2758. [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2759. #pragma unused(stop)
  2760. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2761. }];
  2762. }
  2763. - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
  2764. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2765. if (wrapped && value) {
  2766. *value = [wrapped doubleValue];
  2767. }
  2768. return (wrapped != NULL);
  2769. }
  2770. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2771. if (otherDictionary) {
  2772. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2773. if (_autocreator) {
  2774. GPBAutocreatedDictionaryModified(_autocreator, self);
  2775. }
  2776. }
  2777. }
  2778. - (void)setDouble:(double)value forKey:(uint32_t)key {
  2779. [_dictionary setObject:@(value) forKey:@(key)];
  2780. if (_autocreator) {
  2781. GPBAutocreatedDictionaryModified(_autocreator, self);
  2782. }
  2783. }
  2784. - (void)removeDoubleForKey:(uint32_t)aKey {
  2785. [_dictionary removeObjectForKey:@(aKey)];
  2786. }
  2787. - (void)removeAll {
  2788. [_dictionary removeAllObjects];
  2789. }
  2790. @end
  2791. #pragma mark - UInt32 -> Enum
  2792. @implementation GPBUInt32EnumDictionary {
  2793. @package
  2794. NSMutableDictionary *_dictionary;
  2795. GPBEnumValidationFunc _validationFunc;
  2796. }
  2797. @synthesize validationFunc = _validationFunc;
  2798. + (instancetype)dictionary {
  2799. return [[[self alloc] initWithValidationFunction:NULL
  2800. rawValues:NULL
  2801. forKeys:NULL
  2802. count:0] autorelease];
  2803. }
  2804. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2805. return [[[self alloc] initWithValidationFunction:func
  2806. rawValues:NULL
  2807. forKeys:NULL
  2808. count:0] autorelease];
  2809. }
  2810. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2811. rawValue:(int32_t)rawValue
  2812. forKey:(uint32_t)key {
  2813. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2814. // on to get the type correct.
  2815. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2816. rawValues:&rawValue
  2817. forKeys:&key
  2818. count:1] autorelease];
  2819. }
  2820. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2821. rawValues:(const int32_t [])rawValues
  2822. forKeys:(const uint32_t [])keys
  2823. count:(NSUInteger)count {
  2824. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2825. // on to get the type correct.
  2826. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2827. rawValues:rawValues
  2828. forKeys:keys
  2829. count:count] autorelease];
  2830. }
  2831. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2832. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2833. // on to get the type correct.
  2834. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2835. }
  2836. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2837. capacity:(NSUInteger)numItems {
  2838. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2839. }
  2840. - (instancetype)init {
  2841. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2842. }
  2843. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2844. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2845. }
  2846. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2847. rawValues:(const int32_t [])rawValues
  2848. forKeys:(const uint32_t [])keys
  2849. count:(NSUInteger)count {
  2850. self = [super init];
  2851. if (self) {
  2852. _dictionary = [[NSMutableDictionary alloc] init];
  2853. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2854. if (count && rawValues && keys) {
  2855. for (NSUInteger i = 0; i < count; ++i) {
  2856. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2857. }
  2858. }
  2859. }
  2860. return self;
  2861. }
  2862. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2863. self = [self initWithValidationFunction:dictionary.validationFunc
  2864. rawValues:NULL
  2865. forKeys:NULL
  2866. count:0];
  2867. if (self) {
  2868. if (dictionary) {
  2869. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2870. }
  2871. }
  2872. return self;
  2873. }
  2874. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2875. capacity:(NSUInteger)numItems {
  2876. #pragma unused(numItems)
  2877. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2878. }
  2879. - (void)dealloc {
  2880. NSAssert(!_autocreator,
  2881. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2882. [self class], _autocreator);
  2883. [_dictionary release];
  2884. [super dealloc];
  2885. }
  2886. - (instancetype)copyWithZone:(NSZone *)zone {
  2887. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2888. }
  2889. - (BOOL)isEqual:(id)other {
  2890. if (self == other) {
  2891. return YES;
  2892. }
  2893. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2894. return NO;
  2895. }
  2896. GPBUInt32EnumDictionary *otherDictionary = other;
  2897. return [_dictionary isEqual:otherDictionary->_dictionary];
  2898. }
  2899. - (NSUInteger)hash {
  2900. return _dictionary.count;
  2901. }
  2902. - (NSString *)description {
  2903. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2904. }
  2905. - (NSUInteger)count {
  2906. return _dictionary.count;
  2907. }
  2908. - (void)enumerateKeysAndRawValuesUsingBlock:
  2909. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2910. BOOL stop = NO;
  2911. NSDictionary *internal = _dictionary;
  2912. NSEnumerator *keys = [internal keyEnumerator];
  2913. NSNumber *aKey;
  2914. while ((aKey = [keys nextObject])) {
  2915. NSNumber *aValue = internal[aKey];
  2916. block([aKey unsignedIntValue], [aValue intValue], &stop);
  2917. if (stop) {
  2918. break;
  2919. }
  2920. }
  2921. }
  2922. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2923. NSDictionary *internal = _dictionary;
  2924. NSUInteger count = internal.count;
  2925. if (count == 0) {
  2926. return 0;
  2927. }
  2928. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2929. GPBDataType keyDataType = field.mapKeyDataType;
  2930. size_t result = 0;
  2931. NSEnumerator *keys = [internal keyEnumerator];
  2932. NSNumber *aKey;
  2933. while ((aKey = [keys nextObject])) {
  2934. NSNumber *aValue = internal[aKey];
  2935. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2936. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2937. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2938. }
  2939. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2940. result += tagSize * count;
  2941. return result;
  2942. }
  2943. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2944. asField:(GPBFieldDescriptor *)field {
  2945. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2946. GPBDataType keyDataType = field.mapKeyDataType;
  2947. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2948. NSDictionary *internal = _dictionary;
  2949. NSEnumerator *keys = [internal keyEnumerator];
  2950. NSNumber *aKey;
  2951. while ((aKey = [keys nextObject])) {
  2952. NSNumber *aValue = internal[aKey];
  2953. [outputStream writeInt32NoTag:tag];
  2954. // Write the size of the message.
  2955. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2956. int32_t unwrappedValue = [aValue intValue];
  2957. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2958. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2959. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2960. // Write the fields.
  2961. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2962. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2963. }
  2964. }
  2965. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2966. forKey:(GPBGenericValue *)key
  2967. keyDataType:(GPBDataType)keyDataType {
  2968. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2969. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2970. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2971. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2972. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2973. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2974. [outputStream release];
  2975. return data;
  2976. }
  2977. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2978. forGPBGenericValueKey:(GPBGenericValue *)key {
  2979. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2980. }
  2981. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2982. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2983. #pragma unused(stop)
  2984. block([NSString stringWithFormat:@"%u", key], @(value));
  2985. }];
  2986. }
  2987. - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
  2988. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2989. if (wrapped && value) {
  2990. int32_t result = [wrapped intValue];
  2991. if (!_validationFunc(result)) {
  2992. result = kGPBUnrecognizedEnumeratorValue;
  2993. }
  2994. *value = result;
  2995. }
  2996. return (wrapped != NULL);
  2997. }
  2998. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
  2999. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3000. if (wrapped && rawValue) {
  3001. *rawValue = [wrapped intValue];
  3002. }
  3003. return (wrapped != NULL);
  3004. }
  3005. - (void)enumerateKeysAndEnumsUsingBlock:
  3006. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  3007. GPBEnumValidationFunc func = _validationFunc;
  3008. BOOL stop = NO;
  3009. NSEnumerator *keys = [_dictionary keyEnumerator];
  3010. NSNumber *aKey;
  3011. while ((aKey = [keys nextObject])) {
  3012. NSNumber *aValue = _dictionary[aKey];
  3013. int32_t unwrapped = [aValue intValue];
  3014. if (!func(unwrapped)) {
  3015. unwrapped = kGPBUnrecognizedEnumeratorValue;
  3016. }
  3017. block([aKey unsignedIntValue], unwrapped, &stop);
  3018. if (stop) {
  3019. break;
  3020. }
  3021. }
  3022. }
  3023. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  3024. if (otherDictionary) {
  3025. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3026. if (_autocreator) {
  3027. GPBAutocreatedDictionaryModified(_autocreator, self);
  3028. }
  3029. }
  3030. }
  3031. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  3032. [_dictionary setObject:@(value) forKey:@(key)];
  3033. if (_autocreator) {
  3034. GPBAutocreatedDictionaryModified(_autocreator, self);
  3035. }
  3036. }
  3037. - (void)removeEnumForKey:(uint32_t)aKey {
  3038. [_dictionary removeObjectForKey:@(aKey)];
  3039. }
  3040. - (void)removeAll {
  3041. [_dictionary removeAllObjects];
  3042. }
  3043. - (void)setEnum:(int32_t)value forKey:(uint32_t)key {
  3044. if (!_validationFunc(value)) {
  3045. [NSException raise:NSInvalidArgumentException
  3046. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  3047. value];
  3048. }
  3049. [_dictionary setObject:@(value) forKey:@(key)];
  3050. if (_autocreator) {
  3051. GPBAutocreatedDictionaryModified(_autocreator, self);
  3052. }
  3053. }
  3054. @end
  3055. #pragma mark - UInt32 -> Object
  3056. @implementation GPBUInt32ObjectDictionary {
  3057. @package
  3058. NSMutableDictionary *_dictionary;
  3059. }
  3060. + (instancetype)dictionary {
  3061. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  3062. }
  3063. + (instancetype)dictionaryWithObject:(id)object
  3064. forKey:(uint32_t)key {
  3065. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  3066. // on to get the type correct.
  3067. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  3068. forKeys:&key
  3069. count:1] autorelease];
  3070. }
  3071. + (instancetype)dictionaryWithObjects:(const id [])objects
  3072. forKeys:(const uint32_t [])keys
  3073. count:(NSUInteger)count {
  3074. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  3075. // on to get the type correct.
  3076. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  3077. forKeys:keys
  3078. count:count] autorelease];
  3079. }
  3080. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  3081. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3082. // on to get the type correct.
  3083. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3084. }
  3085. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3086. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3087. }
  3088. - (instancetype)init {
  3089. return [self initWithObjects:NULL forKeys:NULL count:0];
  3090. }
  3091. - (instancetype)initWithObjects:(const id [])objects
  3092. forKeys:(const uint32_t [])keys
  3093. count:(NSUInteger)count {
  3094. self = [super init];
  3095. if (self) {
  3096. _dictionary = [[NSMutableDictionary alloc] init];
  3097. if (count && objects && keys) {
  3098. for (NSUInteger i = 0; i < count; ++i) {
  3099. if (!objects[i]) {
  3100. [NSException raise:NSInvalidArgumentException
  3101. format:@"Attempting to add nil object to a Dictionary"];
  3102. }
  3103. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  3104. }
  3105. }
  3106. }
  3107. return self;
  3108. }
  3109. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  3110. self = [self initWithObjects:NULL forKeys:NULL count:0];
  3111. if (self) {
  3112. if (dictionary) {
  3113. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3114. }
  3115. }
  3116. return self;
  3117. }
  3118. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3119. #pragma unused(numItems)
  3120. return [self initWithObjects:NULL forKeys:NULL count:0];
  3121. }
  3122. - (void)dealloc {
  3123. NSAssert(!_autocreator,
  3124. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3125. [self class], _autocreator);
  3126. [_dictionary release];
  3127. [super dealloc];
  3128. }
  3129. - (instancetype)copyWithZone:(NSZone *)zone {
  3130. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  3131. }
  3132. - (BOOL)isEqual:(id)other {
  3133. if (self == other) {
  3134. return YES;
  3135. }
  3136. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  3137. return NO;
  3138. }
  3139. GPBUInt32ObjectDictionary *otherDictionary = other;
  3140. return [_dictionary isEqual:otherDictionary->_dictionary];
  3141. }
  3142. - (NSUInteger)hash {
  3143. return _dictionary.count;
  3144. }
  3145. - (NSString *)description {
  3146. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3147. }
  3148. - (NSUInteger)count {
  3149. return _dictionary.count;
  3150. }
  3151. - (void)enumerateKeysAndObjectsUsingBlock:
  3152. (void (^)(uint32_t key, id object, BOOL *stop))block {
  3153. BOOL stop = NO;
  3154. NSDictionary *internal = _dictionary;
  3155. NSEnumerator *keys = [internal keyEnumerator];
  3156. NSNumber *aKey;
  3157. while ((aKey = [keys nextObject])) {
  3158. id aObject = internal[aKey];
  3159. block([aKey unsignedIntValue], aObject, &stop);
  3160. if (stop) {
  3161. break;
  3162. }
  3163. }
  3164. }
  3165. - (BOOL)isInitialized {
  3166. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  3167. if (!msg.initialized) {
  3168. return NO;
  3169. }
  3170. }
  3171. return YES;
  3172. }
  3173. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  3174. GPBUInt32ObjectDictionary *newDict =
  3175. [[GPBUInt32ObjectDictionary alloc] init];
  3176. NSEnumerator *keys = [_dictionary keyEnumerator];
  3177. id aKey;
  3178. NSMutableDictionary *internalDict = newDict->_dictionary;
  3179. while ((aKey = [keys nextObject])) {
  3180. GPBMessage *msg = _dictionary[aKey];
  3181. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3182. [internalDict setObject:copiedMsg forKey:aKey];
  3183. [copiedMsg release];
  3184. }
  3185. return newDict;
  3186. }
  3187. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3188. NSDictionary *internal = _dictionary;
  3189. NSUInteger count = internal.count;
  3190. if (count == 0) {
  3191. return 0;
  3192. }
  3193. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3194. GPBDataType keyDataType = field.mapKeyDataType;
  3195. size_t result = 0;
  3196. NSEnumerator *keys = [internal keyEnumerator];
  3197. NSNumber *aKey;
  3198. while ((aKey = [keys nextObject])) {
  3199. id aObject = internal[aKey];
  3200. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3201. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3202. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3203. }
  3204. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3205. result += tagSize * count;
  3206. return result;
  3207. }
  3208. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3209. asField:(GPBFieldDescriptor *)field {
  3210. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3211. GPBDataType keyDataType = field.mapKeyDataType;
  3212. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3213. NSDictionary *internal = _dictionary;
  3214. NSEnumerator *keys = [internal keyEnumerator];
  3215. NSNumber *aKey;
  3216. while ((aKey = [keys nextObject])) {
  3217. id aObject = internal[aKey];
  3218. [outputStream writeInt32NoTag:tag];
  3219. // Write the size of the message.
  3220. uint32_t unwrappedKey = [aKey unsignedIntValue];
  3221. id unwrappedValue = aObject;
  3222. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3223. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3224. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3225. // Write the fields.
  3226. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3227. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3228. }
  3229. }
  3230. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3231. forGPBGenericValueKey:(GPBGenericValue *)key {
  3232. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3233. }
  3234. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3235. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3236. #pragma unused(stop)
  3237. block([NSString stringWithFormat:@"%u", key], object);
  3238. }];
  3239. }
  3240. - (id)objectForKey:(uint32_t)key {
  3241. id result = [_dictionary objectForKey:@(key)];
  3242. return result;
  3243. }
  3244. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3245. if (otherDictionary) {
  3246. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3247. if (_autocreator) {
  3248. GPBAutocreatedDictionaryModified(_autocreator, self);
  3249. }
  3250. }
  3251. }
  3252. - (void)setObject:(id)object forKey:(uint32_t)key {
  3253. if (!object) {
  3254. [NSException raise:NSInvalidArgumentException
  3255. format:@"Attempting to add nil object to a Dictionary"];
  3256. }
  3257. [_dictionary setObject:object forKey:@(key)];
  3258. if (_autocreator) {
  3259. GPBAutocreatedDictionaryModified(_autocreator, self);
  3260. }
  3261. }
  3262. - (void)removeObjectForKey:(uint32_t)aKey {
  3263. [_dictionary removeObjectForKey:@(aKey)];
  3264. }
  3265. - (void)removeAll {
  3266. [_dictionary removeAllObjects];
  3267. }
  3268. @end
  3269. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3270. // This block of code is generated, do not edit it directly.
  3271. #pragma mark - Int32 -> UInt32
  3272. @implementation GPBInt32UInt32Dictionary {
  3273. @package
  3274. NSMutableDictionary *_dictionary;
  3275. }
  3276. + (instancetype)dictionary {
  3277. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  3278. }
  3279. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  3280. forKey:(int32_t)key {
  3281. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  3282. // on to get the type correct.
  3283. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  3284. forKeys:&key
  3285. count:1] autorelease];
  3286. }
  3287. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  3288. forKeys:(const int32_t [])keys
  3289. count:(NSUInteger)count {
  3290. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  3291. // on to get the type correct.
  3292. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
  3293. forKeys:keys
  3294. count:count] autorelease];
  3295. }
  3296. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3297. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3298. // on to get the type correct.
  3299. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3300. }
  3301. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3302. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3303. }
  3304. - (instancetype)init {
  3305. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  3306. }
  3307. - (instancetype)initWithUInt32s:(const uint32_t [])values
  3308. forKeys:(const int32_t [])keys
  3309. count:(NSUInteger)count {
  3310. self = [super init];
  3311. if (self) {
  3312. _dictionary = [[NSMutableDictionary alloc] init];
  3313. if (count && values && keys) {
  3314. for (NSUInteger i = 0; i < count; ++i) {
  3315. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3316. }
  3317. }
  3318. }
  3319. return self;
  3320. }
  3321. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3322. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  3323. if (self) {
  3324. if (dictionary) {
  3325. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3326. }
  3327. }
  3328. return self;
  3329. }
  3330. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3331. #pragma unused(numItems)
  3332. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  3333. }
  3334. - (void)dealloc {
  3335. NSAssert(!_autocreator,
  3336. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3337. [self class], _autocreator);
  3338. [_dictionary release];
  3339. [super dealloc];
  3340. }
  3341. - (instancetype)copyWithZone:(NSZone *)zone {
  3342. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3343. }
  3344. - (BOOL)isEqual:(id)other {
  3345. if (self == other) {
  3346. return YES;
  3347. }
  3348. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3349. return NO;
  3350. }
  3351. GPBInt32UInt32Dictionary *otherDictionary = other;
  3352. return [_dictionary isEqual:otherDictionary->_dictionary];
  3353. }
  3354. - (NSUInteger)hash {
  3355. return _dictionary.count;
  3356. }
  3357. - (NSString *)description {
  3358. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3359. }
  3360. - (NSUInteger)count {
  3361. return _dictionary.count;
  3362. }
  3363. - (void)enumerateKeysAndUInt32sUsingBlock:
  3364. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3365. BOOL stop = NO;
  3366. NSDictionary *internal = _dictionary;
  3367. NSEnumerator *keys = [internal keyEnumerator];
  3368. NSNumber *aKey;
  3369. while ((aKey = [keys nextObject])) {
  3370. NSNumber *aValue = internal[aKey];
  3371. block([aKey intValue], [aValue unsignedIntValue], &stop);
  3372. if (stop) {
  3373. break;
  3374. }
  3375. }
  3376. }
  3377. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3378. NSDictionary *internal = _dictionary;
  3379. NSUInteger count = internal.count;
  3380. if (count == 0) {
  3381. return 0;
  3382. }
  3383. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3384. GPBDataType keyDataType = field.mapKeyDataType;
  3385. size_t result = 0;
  3386. NSEnumerator *keys = [internal keyEnumerator];
  3387. NSNumber *aKey;
  3388. while ((aKey = [keys nextObject])) {
  3389. NSNumber *aValue = internal[aKey];
  3390. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3391. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3392. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3393. }
  3394. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3395. result += tagSize * count;
  3396. return result;
  3397. }
  3398. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3399. asField:(GPBFieldDescriptor *)field {
  3400. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3401. GPBDataType keyDataType = field.mapKeyDataType;
  3402. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3403. NSDictionary *internal = _dictionary;
  3404. NSEnumerator *keys = [internal keyEnumerator];
  3405. NSNumber *aKey;
  3406. while ((aKey = [keys nextObject])) {
  3407. NSNumber *aValue = internal[aKey];
  3408. [outputStream writeInt32NoTag:tag];
  3409. // Write the size of the message.
  3410. int32_t unwrappedKey = [aKey intValue];
  3411. uint32_t unwrappedValue = [aValue unsignedIntValue];
  3412. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3413. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3414. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3415. // Write the fields.
  3416. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3417. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3418. }
  3419. }
  3420. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3421. forGPBGenericValueKey:(GPBGenericValue *)key {
  3422. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3423. }
  3424. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3425. [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3426. #pragma unused(stop)
  3427. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3428. }];
  3429. }
  3430. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
  3431. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3432. if (wrapped && value) {
  3433. *value = [wrapped unsignedIntValue];
  3434. }
  3435. return (wrapped != NULL);
  3436. }
  3437. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3438. if (otherDictionary) {
  3439. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3440. if (_autocreator) {
  3441. GPBAutocreatedDictionaryModified(_autocreator, self);
  3442. }
  3443. }
  3444. }
  3445. - (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
  3446. [_dictionary setObject:@(value) forKey:@(key)];
  3447. if (_autocreator) {
  3448. GPBAutocreatedDictionaryModified(_autocreator, self);
  3449. }
  3450. }
  3451. - (void)removeUInt32ForKey:(int32_t)aKey {
  3452. [_dictionary removeObjectForKey:@(aKey)];
  3453. }
  3454. - (void)removeAll {
  3455. [_dictionary removeAllObjects];
  3456. }
  3457. @end
  3458. #pragma mark - Int32 -> Int32
  3459. @implementation GPBInt32Int32Dictionary {
  3460. @package
  3461. NSMutableDictionary *_dictionary;
  3462. }
  3463. + (instancetype)dictionary {
  3464. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  3465. }
  3466. + (instancetype)dictionaryWithInt32:(int32_t)value
  3467. forKey:(int32_t)key {
  3468. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  3469. // on to get the type correct.
  3470. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
  3471. forKeys:&key
  3472. count:1] autorelease];
  3473. }
  3474. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  3475. forKeys:(const int32_t [])keys
  3476. count:(NSUInteger)count {
  3477. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  3478. // on to get the type correct.
  3479. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:values
  3480. forKeys:keys
  3481. count:count] autorelease];
  3482. }
  3483. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3484. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3485. // on to get the type correct.
  3486. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3487. }
  3488. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3489. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3490. }
  3491. - (instancetype)init {
  3492. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3493. }
  3494. - (instancetype)initWithInt32s:(const int32_t [])values
  3495. forKeys:(const int32_t [])keys
  3496. count:(NSUInteger)count {
  3497. self = [super init];
  3498. if (self) {
  3499. _dictionary = [[NSMutableDictionary alloc] init];
  3500. if (count && values && keys) {
  3501. for (NSUInteger i = 0; i < count; ++i) {
  3502. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3503. }
  3504. }
  3505. }
  3506. return self;
  3507. }
  3508. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3509. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  3510. if (self) {
  3511. if (dictionary) {
  3512. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3513. }
  3514. }
  3515. return self;
  3516. }
  3517. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3518. #pragma unused(numItems)
  3519. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3520. }
  3521. - (void)dealloc {
  3522. NSAssert(!_autocreator,
  3523. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3524. [self class], _autocreator);
  3525. [_dictionary release];
  3526. [super dealloc];
  3527. }
  3528. - (instancetype)copyWithZone:(NSZone *)zone {
  3529. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3530. }
  3531. - (BOOL)isEqual:(id)other {
  3532. if (self == other) {
  3533. return YES;
  3534. }
  3535. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3536. return NO;
  3537. }
  3538. GPBInt32Int32Dictionary *otherDictionary = other;
  3539. return [_dictionary isEqual:otherDictionary->_dictionary];
  3540. }
  3541. - (NSUInteger)hash {
  3542. return _dictionary.count;
  3543. }
  3544. - (NSString *)description {
  3545. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3546. }
  3547. - (NSUInteger)count {
  3548. return _dictionary.count;
  3549. }
  3550. - (void)enumerateKeysAndInt32sUsingBlock:
  3551. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3552. BOOL stop = NO;
  3553. NSDictionary *internal = _dictionary;
  3554. NSEnumerator *keys = [internal keyEnumerator];
  3555. NSNumber *aKey;
  3556. while ((aKey = [keys nextObject])) {
  3557. NSNumber *aValue = internal[aKey];
  3558. block([aKey intValue], [aValue intValue], &stop);
  3559. if (stop) {
  3560. break;
  3561. }
  3562. }
  3563. }
  3564. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3565. NSDictionary *internal = _dictionary;
  3566. NSUInteger count = internal.count;
  3567. if (count == 0) {
  3568. return 0;
  3569. }
  3570. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3571. GPBDataType keyDataType = field.mapKeyDataType;
  3572. size_t result = 0;
  3573. NSEnumerator *keys = [internal keyEnumerator];
  3574. NSNumber *aKey;
  3575. while ((aKey = [keys nextObject])) {
  3576. NSNumber *aValue = internal[aKey];
  3577. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3578. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3579. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3580. }
  3581. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3582. result += tagSize * count;
  3583. return result;
  3584. }
  3585. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3586. asField:(GPBFieldDescriptor *)field {
  3587. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3588. GPBDataType keyDataType = field.mapKeyDataType;
  3589. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3590. NSDictionary *internal = _dictionary;
  3591. NSEnumerator *keys = [internal keyEnumerator];
  3592. NSNumber *aKey;
  3593. while ((aKey = [keys nextObject])) {
  3594. NSNumber *aValue = internal[aKey];
  3595. [outputStream writeInt32NoTag:tag];
  3596. // Write the size of the message.
  3597. int32_t unwrappedKey = [aKey intValue];
  3598. int32_t unwrappedValue = [aValue intValue];
  3599. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3600. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3601. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3602. // Write the fields.
  3603. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3604. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3605. }
  3606. }
  3607. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3608. forGPBGenericValueKey:(GPBGenericValue *)key {
  3609. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3610. }
  3611. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3612. [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3613. #pragma unused(stop)
  3614. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3615. }];
  3616. }
  3617. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
  3618. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3619. if (wrapped && value) {
  3620. *value = [wrapped intValue];
  3621. }
  3622. return (wrapped != NULL);
  3623. }
  3624. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3625. if (otherDictionary) {
  3626. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3627. if (_autocreator) {
  3628. GPBAutocreatedDictionaryModified(_autocreator, self);
  3629. }
  3630. }
  3631. }
  3632. - (void)setInt32:(int32_t)value forKey:(int32_t)key {
  3633. [_dictionary setObject:@(value) forKey:@(key)];
  3634. if (_autocreator) {
  3635. GPBAutocreatedDictionaryModified(_autocreator, self);
  3636. }
  3637. }
  3638. - (void)removeInt32ForKey:(int32_t)aKey {
  3639. [_dictionary removeObjectForKey:@(aKey)];
  3640. }
  3641. - (void)removeAll {
  3642. [_dictionary removeAllObjects];
  3643. }
  3644. @end
  3645. #pragma mark - Int32 -> UInt64
  3646. @implementation GPBInt32UInt64Dictionary {
  3647. @package
  3648. NSMutableDictionary *_dictionary;
  3649. }
  3650. + (instancetype)dictionary {
  3651. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  3652. }
  3653. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  3654. forKey:(int32_t)key {
  3655. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  3656. // on to get the type correct.
  3657. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  3658. forKeys:&key
  3659. count:1] autorelease];
  3660. }
  3661. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  3662. forKeys:(const int32_t [])keys
  3663. count:(NSUInteger)count {
  3664. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  3665. // on to get the type correct.
  3666. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
  3667. forKeys:keys
  3668. count:count] autorelease];
  3669. }
  3670. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3671. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3672. // on to get the type correct.
  3673. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3674. }
  3675. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3676. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3677. }
  3678. - (instancetype)init {
  3679. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3680. }
  3681. - (instancetype)initWithUInt64s:(const uint64_t [])values
  3682. forKeys:(const int32_t [])keys
  3683. count:(NSUInteger)count {
  3684. self = [super init];
  3685. if (self) {
  3686. _dictionary = [[NSMutableDictionary alloc] init];
  3687. if (count && values && keys) {
  3688. for (NSUInteger i = 0; i < count; ++i) {
  3689. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3690. }
  3691. }
  3692. }
  3693. return self;
  3694. }
  3695. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3696. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  3697. if (self) {
  3698. if (dictionary) {
  3699. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3700. }
  3701. }
  3702. return self;
  3703. }
  3704. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3705. #pragma unused(numItems)
  3706. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3707. }
  3708. - (void)dealloc {
  3709. NSAssert(!_autocreator,
  3710. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3711. [self class], _autocreator);
  3712. [_dictionary release];
  3713. [super dealloc];
  3714. }
  3715. - (instancetype)copyWithZone:(NSZone *)zone {
  3716. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3717. }
  3718. - (BOOL)isEqual:(id)other {
  3719. if (self == other) {
  3720. return YES;
  3721. }
  3722. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3723. return NO;
  3724. }
  3725. GPBInt32UInt64Dictionary *otherDictionary = other;
  3726. return [_dictionary isEqual:otherDictionary->_dictionary];
  3727. }
  3728. - (NSUInteger)hash {
  3729. return _dictionary.count;
  3730. }
  3731. - (NSString *)description {
  3732. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3733. }
  3734. - (NSUInteger)count {
  3735. return _dictionary.count;
  3736. }
  3737. - (void)enumerateKeysAndUInt64sUsingBlock:
  3738. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3739. BOOL stop = NO;
  3740. NSDictionary *internal = _dictionary;
  3741. NSEnumerator *keys = [internal keyEnumerator];
  3742. NSNumber *aKey;
  3743. while ((aKey = [keys nextObject])) {
  3744. NSNumber *aValue = internal[aKey];
  3745. block([aKey intValue], [aValue unsignedLongLongValue], &stop);
  3746. if (stop) {
  3747. break;
  3748. }
  3749. }
  3750. }
  3751. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3752. NSDictionary *internal = _dictionary;
  3753. NSUInteger count = internal.count;
  3754. if (count == 0) {
  3755. return 0;
  3756. }
  3757. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3758. GPBDataType keyDataType = field.mapKeyDataType;
  3759. size_t result = 0;
  3760. NSEnumerator *keys = [internal keyEnumerator];
  3761. NSNumber *aKey;
  3762. while ((aKey = [keys nextObject])) {
  3763. NSNumber *aValue = internal[aKey];
  3764. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3765. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3766. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3767. }
  3768. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3769. result += tagSize * count;
  3770. return result;
  3771. }
  3772. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3773. asField:(GPBFieldDescriptor *)field {
  3774. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3775. GPBDataType keyDataType = field.mapKeyDataType;
  3776. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3777. NSDictionary *internal = _dictionary;
  3778. NSEnumerator *keys = [internal keyEnumerator];
  3779. NSNumber *aKey;
  3780. while ((aKey = [keys nextObject])) {
  3781. NSNumber *aValue = internal[aKey];
  3782. [outputStream writeInt32NoTag:tag];
  3783. // Write the size of the message.
  3784. int32_t unwrappedKey = [aKey intValue];
  3785. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  3786. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3787. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3788. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3789. // Write the fields.
  3790. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3791. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3792. }
  3793. }
  3794. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3795. forGPBGenericValueKey:(GPBGenericValue *)key {
  3796. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3797. }
  3798. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3799. [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3800. #pragma unused(stop)
  3801. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3802. }];
  3803. }
  3804. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
  3805. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3806. if (wrapped && value) {
  3807. *value = [wrapped unsignedLongLongValue];
  3808. }
  3809. return (wrapped != NULL);
  3810. }
  3811. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3812. if (otherDictionary) {
  3813. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3814. if (_autocreator) {
  3815. GPBAutocreatedDictionaryModified(_autocreator, self);
  3816. }
  3817. }
  3818. }
  3819. - (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
  3820. [_dictionary setObject:@(value) forKey:@(key)];
  3821. if (_autocreator) {
  3822. GPBAutocreatedDictionaryModified(_autocreator, self);
  3823. }
  3824. }
  3825. - (void)removeUInt64ForKey:(int32_t)aKey {
  3826. [_dictionary removeObjectForKey:@(aKey)];
  3827. }
  3828. - (void)removeAll {
  3829. [_dictionary removeAllObjects];
  3830. }
  3831. @end
  3832. #pragma mark - Int32 -> Int64
  3833. @implementation GPBInt32Int64Dictionary {
  3834. @package
  3835. NSMutableDictionary *_dictionary;
  3836. }
  3837. + (instancetype)dictionary {
  3838. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  3839. }
  3840. + (instancetype)dictionaryWithInt64:(int64_t)value
  3841. forKey:(int32_t)key {
  3842. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  3843. // on to get the type correct.
  3844. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
  3845. forKeys:&key
  3846. count:1] autorelease];
  3847. }
  3848. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  3849. forKeys:(const int32_t [])keys
  3850. count:(NSUInteger)count {
  3851. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  3852. // on to get the type correct.
  3853. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:values
  3854. forKeys:keys
  3855. count:count] autorelease];
  3856. }
  3857. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3858. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3859. // on to get the type correct.
  3860. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3861. }
  3862. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3863. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3864. }
  3865. - (instancetype)init {
  3866. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3867. }
  3868. - (instancetype)initWithInt64s:(const int64_t [])values
  3869. forKeys:(const int32_t [])keys
  3870. count:(NSUInteger)count {
  3871. self = [super init];
  3872. if (self) {
  3873. _dictionary = [[NSMutableDictionary alloc] init];
  3874. if (count && values && keys) {
  3875. for (NSUInteger i = 0; i < count; ++i) {
  3876. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3877. }
  3878. }
  3879. }
  3880. return self;
  3881. }
  3882. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3883. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  3884. if (self) {
  3885. if (dictionary) {
  3886. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3887. }
  3888. }
  3889. return self;
  3890. }
  3891. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3892. #pragma unused(numItems)
  3893. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3894. }
  3895. - (void)dealloc {
  3896. NSAssert(!_autocreator,
  3897. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3898. [self class], _autocreator);
  3899. [_dictionary release];
  3900. [super dealloc];
  3901. }
  3902. - (instancetype)copyWithZone:(NSZone *)zone {
  3903. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3904. }
  3905. - (BOOL)isEqual:(id)other {
  3906. if (self == other) {
  3907. return YES;
  3908. }
  3909. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3910. return NO;
  3911. }
  3912. GPBInt32Int64Dictionary *otherDictionary = other;
  3913. return [_dictionary isEqual:otherDictionary->_dictionary];
  3914. }
  3915. - (NSUInteger)hash {
  3916. return _dictionary.count;
  3917. }
  3918. - (NSString *)description {
  3919. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3920. }
  3921. - (NSUInteger)count {
  3922. return _dictionary.count;
  3923. }
  3924. - (void)enumerateKeysAndInt64sUsingBlock:
  3925. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3926. BOOL stop = NO;
  3927. NSDictionary *internal = _dictionary;
  3928. NSEnumerator *keys = [internal keyEnumerator];
  3929. NSNumber *aKey;
  3930. while ((aKey = [keys nextObject])) {
  3931. NSNumber *aValue = internal[aKey];
  3932. block([aKey intValue], [aValue longLongValue], &stop);
  3933. if (stop) {
  3934. break;
  3935. }
  3936. }
  3937. }
  3938. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3939. NSDictionary *internal = _dictionary;
  3940. NSUInteger count = internal.count;
  3941. if (count == 0) {
  3942. return 0;
  3943. }
  3944. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3945. GPBDataType keyDataType = field.mapKeyDataType;
  3946. size_t result = 0;
  3947. NSEnumerator *keys = [internal keyEnumerator];
  3948. NSNumber *aKey;
  3949. while ((aKey = [keys nextObject])) {
  3950. NSNumber *aValue = internal[aKey];
  3951. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3952. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3953. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3954. }
  3955. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3956. result += tagSize * count;
  3957. return result;
  3958. }
  3959. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3960. asField:(GPBFieldDescriptor *)field {
  3961. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3962. GPBDataType keyDataType = field.mapKeyDataType;
  3963. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3964. NSDictionary *internal = _dictionary;
  3965. NSEnumerator *keys = [internal keyEnumerator];
  3966. NSNumber *aKey;
  3967. while ((aKey = [keys nextObject])) {
  3968. NSNumber *aValue = internal[aKey];
  3969. [outputStream writeInt32NoTag:tag];
  3970. // Write the size of the message.
  3971. int32_t unwrappedKey = [aKey intValue];
  3972. int64_t unwrappedValue = [aValue longLongValue];
  3973. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3974. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3975. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3976. // Write the fields.
  3977. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3978. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3979. }
  3980. }
  3981. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3982. forGPBGenericValueKey:(GPBGenericValue *)key {
  3983. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3984. }
  3985. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3986. [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3987. #pragma unused(stop)
  3988. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3989. }];
  3990. }
  3991. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
  3992. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3993. if (wrapped && value) {
  3994. *value = [wrapped longLongValue];
  3995. }
  3996. return (wrapped != NULL);
  3997. }
  3998. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3999. if (otherDictionary) {
  4000. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4001. if (_autocreator) {
  4002. GPBAutocreatedDictionaryModified(_autocreator, self);
  4003. }
  4004. }
  4005. }
  4006. - (void)setInt64:(int64_t)value forKey:(int32_t)key {
  4007. [_dictionary setObject:@(value) forKey:@(key)];
  4008. if (_autocreator) {
  4009. GPBAutocreatedDictionaryModified(_autocreator, self);
  4010. }
  4011. }
  4012. - (void)removeInt64ForKey:(int32_t)aKey {
  4013. [_dictionary removeObjectForKey:@(aKey)];
  4014. }
  4015. - (void)removeAll {
  4016. [_dictionary removeAllObjects];
  4017. }
  4018. @end
  4019. #pragma mark - Int32 -> Bool
  4020. @implementation GPBInt32BoolDictionary {
  4021. @package
  4022. NSMutableDictionary *_dictionary;
  4023. }
  4024. + (instancetype)dictionary {
  4025. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  4026. }
  4027. + (instancetype)dictionaryWithBool:(BOOL)value
  4028. forKey:(int32_t)key {
  4029. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  4030. // on to get the type correct.
  4031. return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:&value
  4032. forKeys:&key
  4033. count:1] autorelease];
  4034. }
  4035. + (instancetype)dictionaryWithBools:(const BOOL [])values
  4036. forKeys:(const int32_t [])keys
  4037. count:(NSUInteger)count {
  4038. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  4039. // on to get the type correct.
  4040. return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:values
  4041. forKeys:keys
  4042. count:count] autorelease];
  4043. }
  4044. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  4045. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4046. // on to get the type correct.
  4047. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4048. }
  4049. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4050. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4051. }
  4052. - (instancetype)init {
  4053. return [self initWithBools:NULL forKeys:NULL count:0];
  4054. }
  4055. - (instancetype)initWithBools:(const BOOL [])values
  4056. forKeys:(const int32_t [])keys
  4057. count:(NSUInteger)count {
  4058. self = [super init];
  4059. if (self) {
  4060. _dictionary = [[NSMutableDictionary alloc] init];
  4061. if (count && values && keys) {
  4062. for (NSUInteger i = 0; i < count; ++i) {
  4063. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4064. }
  4065. }
  4066. }
  4067. return self;
  4068. }
  4069. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  4070. self = [self initWithBools:NULL forKeys:NULL count:0];
  4071. if (self) {
  4072. if (dictionary) {
  4073. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4074. }
  4075. }
  4076. return self;
  4077. }
  4078. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4079. #pragma unused(numItems)
  4080. return [self initWithBools:NULL forKeys:NULL count:0];
  4081. }
  4082. - (void)dealloc {
  4083. NSAssert(!_autocreator,
  4084. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4085. [self class], _autocreator);
  4086. [_dictionary release];
  4087. [super dealloc];
  4088. }
  4089. - (instancetype)copyWithZone:(NSZone *)zone {
  4090. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  4091. }
  4092. - (BOOL)isEqual:(id)other {
  4093. if (self == other) {
  4094. return YES;
  4095. }
  4096. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  4097. return NO;
  4098. }
  4099. GPBInt32BoolDictionary *otherDictionary = other;
  4100. return [_dictionary isEqual:otherDictionary->_dictionary];
  4101. }
  4102. - (NSUInteger)hash {
  4103. return _dictionary.count;
  4104. }
  4105. - (NSString *)description {
  4106. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4107. }
  4108. - (NSUInteger)count {
  4109. return _dictionary.count;
  4110. }
  4111. - (void)enumerateKeysAndBoolsUsingBlock:
  4112. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  4113. BOOL stop = NO;
  4114. NSDictionary *internal = _dictionary;
  4115. NSEnumerator *keys = [internal keyEnumerator];
  4116. NSNumber *aKey;
  4117. while ((aKey = [keys nextObject])) {
  4118. NSNumber *aValue = internal[aKey];
  4119. block([aKey intValue], [aValue boolValue], &stop);
  4120. if (stop) {
  4121. break;
  4122. }
  4123. }
  4124. }
  4125. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4126. NSDictionary *internal = _dictionary;
  4127. NSUInteger count = internal.count;
  4128. if (count == 0) {
  4129. return 0;
  4130. }
  4131. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4132. GPBDataType keyDataType = field.mapKeyDataType;
  4133. size_t result = 0;
  4134. NSEnumerator *keys = [internal keyEnumerator];
  4135. NSNumber *aKey;
  4136. while ((aKey = [keys nextObject])) {
  4137. NSNumber *aValue = internal[aKey];
  4138. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4139. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  4140. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4141. }
  4142. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4143. result += tagSize * count;
  4144. return result;
  4145. }
  4146. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4147. asField:(GPBFieldDescriptor *)field {
  4148. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4149. GPBDataType keyDataType = field.mapKeyDataType;
  4150. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4151. NSDictionary *internal = _dictionary;
  4152. NSEnumerator *keys = [internal keyEnumerator];
  4153. NSNumber *aKey;
  4154. while ((aKey = [keys nextObject])) {
  4155. NSNumber *aValue = internal[aKey];
  4156. [outputStream writeInt32NoTag:tag];
  4157. // Write the size of the message.
  4158. int32_t unwrappedKey = [aKey intValue];
  4159. BOOL unwrappedValue = [aValue boolValue];
  4160. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4161. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4162. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4163. // Write the fields.
  4164. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4165. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4166. }
  4167. }
  4168. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4169. forGPBGenericValueKey:(GPBGenericValue *)key {
  4170. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  4171. }
  4172. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4173. [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  4174. #pragma unused(stop)
  4175. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  4176. }];
  4177. }
  4178. - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
  4179. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4180. if (wrapped && value) {
  4181. *value = [wrapped boolValue];
  4182. }
  4183. return (wrapped != NULL);
  4184. }
  4185. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  4186. if (otherDictionary) {
  4187. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4188. if (_autocreator) {
  4189. GPBAutocreatedDictionaryModified(_autocreator, self);
  4190. }
  4191. }
  4192. }
  4193. - (void)setBool:(BOOL)value forKey:(int32_t)key {
  4194. [_dictionary setObject:@(value) forKey:@(key)];
  4195. if (_autocreator) {
  4196. GPBAutocreatedDictionaryModified(_autocreator, self);
  4197. }
  4198. }
  4199. - (void)removeBoolForKey:(int32_t)aKey {
  4200. [_dictionary removeObjectForKey:@(aKey)];
  4201. }
  4202. - (void)removeAll {
  4203. [_dictionary removeAllObjects];
  4204. }
  4205. @end
  4206. #pragma mark - Int32 -> Float
  4207. @implementation GPBInt32FloatDictionary {
  4208. @package
  4209. NSMutableDictionary *_dictionary;
  4210. }
  4211. + (instancetype)dictionary {
  4212. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  4213. }
  4214. + (instancetype)dictionaryWithFloat:(float)value
  4215. forKey:(int32_t)key {
  4216. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  4217. // on to get the type correct.
  4218. return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:&value
  4219. forKeys:&key
  4220. count:1] autorelease];
  4221. }
  4222. + (instancetype)dictionaryWithFloats:(const float [])values
  4223. forKeys:(const int32_t [])keys
  4224. count:(NSUInteger)count {
  4225. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  4226. // on to get the type correct.
  4227. return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:values
  4228. forKeys:keys
  4229. count:count] autorelease];
  4230. }
  4231. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4232. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4233. // on to get the type correct.
  4234. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4235. }
  4236. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4237. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4238. }
  4239. - (instancetype)init {
  4240. return [self initWithFloats:NULL forKeys:NULL count:0];
  4241. }
  4242. - (instancetype)initWithFloats:(const float [])values
  4243. forKeys:(const int32_t [])keys
  4244. count:(NSUInteger)count {
  4245. self = [super init];
  4246. if (self) {
  4247. _dictionary = [[NSMutableDictionary alloc] init];
  4248. if (count && values && keys) {
  4249. for (NSUInteger i = 0; i < count; ++i) {
  4250. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4251. }
  4252. }
  4253. }
  4254. return self;
  4255. }
  4256. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4257. self = [self initWithFloats:NULL forKeys:NULL count:0];
  4258. if (self) {
  4259. if (dictionary) {
  4260. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4261. }
  4262. }
  4263. return self;
  4264. }
  4265. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4266. #pragma unused(numItems)
  4267. return [self initWithFloats:NULL forKeys:NULL count:0];
  4268. }
  4269. - (void)dealloc {
  4270. NSAssert(!_autocreator,
  4271. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4272. [self class], _autocreator);
  4273. [_dictionary release];
  4274. [super dealloc];
  4275. }
  4276. - (instancetype)copyWithZone:(NSZone *)zone {
  4277. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4278. }
  4279. - (BOOL)isEqual:(id)other {
  4280. if (self == other) {
  4281. return YES;
  4282. }
  4283. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4284. return NO;
  4285. }
  4286. GPBInt32FloatDictionary *otherDictionary = other;
  4287. return [_dictionary isEqual:otherDictionary->_dictionary];
  4288. }
  4289. - (NSUInteger)hash {
  4290. return _dictionary.count;
  4291. }
  4292. - (NSString *)description {
  4293. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4294. }
  4295. - (NSUInteger)count {
  4296. return _dictionary.count;
  4297. }
  4298. - (void)enumerateKeysAndFloatsUsingBlock:
  4299. (void (^)(int32_t key, float value, BOOL *stop))block {
  4300. BOOL stop = NO;
  4301. NSDictionary *internal = _dictionary;
  4302. NSEnumerator *keys = [internal keyEnumerator];
  4303. NSNumber *aKey;
  4304. while ((aKey = [keys nextObject])) {
  4305. NSNumber *aValue = internal[aKey];
  4306. block([aKey intValue], [aValue floatValue], &stop);
  4307. if (stop) {
  4308. break;
  4309. }
  4310. }
  4311. }
  4312. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4313. NSDictionary *internal = _dictionary;
  4314. NSUInteger count = internal.count;
  4315. if (count == 0) {
  4316. return 0;
  4317. }
  4318. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4319. GPBDataType keyDataType = field.mapKeyDataType;
  4320. size_t result = 0;
  4321. NSEnumerator *keys = [internal keyEnumerator];
  4322. NSNumber *aKey;
  4323. while ((aKey = [keys nextObject])) {
  4324. NSNumber *aValue = internal[aKey];
  4325. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4326. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4327. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4328. }
  4329. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4330. result += tagSize * count;
  4331. return result;
  4332. }
  4333. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4334. asField:(GPBFieldDescriptor *)field {
  4335. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4336. GPBDataType keyDataType = field.mapKeyDataType;
  4337. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4338. NSDictionary *internal = _dictionary;
  4339. NSEnumerator *keys = [internal keyEnumerator];
  4340. NSNumber *aKey;
  4341. while ((aKey = [keys nextObject])) {
  4342. NSNumber *aValue = internal[aKey];
  4343. [outputStream writeInt32NoTag:tag];
  4344. // Write the size of the message.
  4345. int32_t unwrappedKey = [aKey intValue];
  4346. float unwrappedValue = [aValue floatValue];
  4347. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4348. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4349. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4350. // Write the fields.
  4351. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4352. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4353. }
  4354. }
  4355. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4356. forGPBGenericValueKey:(GPBGenericValue *)key {
  4357. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4358. }
  4359. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4360. [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4361. #pragma unused(stop)
  4362. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4363. }];
  4364. }
  4365. - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
  4366. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4367. if (wrapped && value) {
  4368. *value = [wrapped floatValue];
  4369. }
  4370. return (wrapped != NULL);
  4371. }
  4372. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4373. if (otherDictionary) {
  4374. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4375. if (_autocreator) {
  4376. GPBAutocreatedDictionaryModified(_autocreator, self);
  4377. }
  4378. }
  4379. }
  4380. - (void)setFloat:(float)value forKey:(int32_t)key {
  4381. [_dictionary setObject:@(value) forKey:@(key)];
  4382. if (_autocreator) {
  4383. GPBAutocreatedDictionaryModified(_autocreator, self);
  4384. }
  4385. }
  4386. - (void)removeFloatForKey:(int32_t)aKey {
  4387. [_dictionary removeObjectForKey:@(aKey)];
  4388. }
  4389. - (void)removeAll {
  4390. [_dictionary removeAllObjects];
  4391. }
  4392. @end
  4393. #pragma mark - Int32 -> Double
  4394. @implementation GPBInt32DoubleDictionary {
  4395. @package
  4396. NSMutableDictionary *_dictionary;
  4397. }
  4398. + (instancetype)dictionary {
  4399. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  4400. }
  4401. + (instancetype)dictionaryWithDouble:(double)value
  4402. forKey:(int32_t)key {
  4403. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  4404. // on to get the type correct.
  4405. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
  4406. forKeys:&key
  4407. count:1] autorelease];
  4408. }
  4409. + (instancetype)dictionaryWithDoubles:(const double [])values
  4410. forKeys:(const int32_t [])keys
  4411. count:(NSUInteger)count {
  4412. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  4413. // on to get the type correct.
  4414. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:values
  4415. forKeys:keys
  4416. count:count] autorelease];
  4417. }
  4418. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4419. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4420. // on to get the type correct.
  4421. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4422. }
  4423. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4424. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4425. }
  4426. - (instancetype)init {
  4427. return [self initWithDoubles:NULL forKeys:NULL count:0];
  4428. }
  4429. - (instancetype)initWithDoubles:(const double [])values
  4430. forKeys:(const int32_t [])keys
  4431. count:(NSUInteger)count {
  4432. self = [super init];
  4433. if (self) {
  4434. _dictionary = [[NSMutableDictionary alloc] init];
  4435. if (count && values && keys) {
  4436. for (NSUInteger i = 0; i < count; ++i) {
  4437. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4438. }
  4439. }
  4440. }
  4441. return self;
  4442. }
  4443. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4444. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  4445. if (self) {
  4446. if (dictionary) {
  4447. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4448. }
  4449. }
  4450. return self;
  4451. }
  4452. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4453. #pragma unused(numItems)
  4454. return [self initWithDoubles:NULL forKeys:NULL count:0];
  4455. }
  4456. - (void)dealloc {
  4457. NSAssert(!_autocreator,
  4458. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4459. [self class], _autocreator);
  4460. [_dictionary release];
  4461. [super dealloc];
  4462. }
  4463. - (instancetype)copyWithZone:(NSZone *)zone {
  4464. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4465. }
  4466. - (BOOL)isEqual:(id)other {
  4467. if (self == other) {
  4468. return YES;
  4469. }
  4470. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4471. return NO;
  4472. }
  4473. GPBInt32DoubleDictionary *otherDictionary = other;
  4474. return [_dictionary isEqual:otherDictionary->_dictionary];
  4475. }
  4476. - (NSUInteger)hash {
  4477. return _dictionary.count;
  4478. }
  4479. - (NSString *)description {
  4480. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4481. }
  4482. - (NSUInteger)count {
  4483. return _dictionary.count;
  4484. }
  4485. - (void)enumerateKeysAndDoublesUsingBlock:
  4486. (void (^)(int32_t key, double value, BOOL *stop))block {
  4487. BOOL stop = NO;
  4488. NSDictionary *internal = _dictionary;
  4489. NSEnumerator *keys = [internal keyEnumerator];
  4490. NSNumber *aKey;
  4491. while ((aKey = [keys nextObject])) {
  4492. NSNumber *aValue = internal[aKey];
  4493. block([aKey intValue], [aValue doubleValue], &stop);
  4494. if (stop) {
  4495. break;
  4496. }
  4497. }
  4498. }
  4499. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4500. NSDictionary *internal = _dictionary;
  4501. NSUInteger count = internal.count;
  4502. if (count == 0) {
  4503. return 0;
  4504. }
  4505. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4506. GPBDataType keyDataType = field.mapKeyDataType;
  4507. size_t result = 0;
  4508. NSEnumerator *keys = [internal keyEnumerator];
  4509. NSNumber *aKey;
  4510. while ((aKey = [keys nextObject])) {
  4511. NSNumber *aValue = internal[aKey];
  4512. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4513. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4514. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4515. }
  4516. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4517. result += tagSize * count;
  4518. return result;
  4519. }
  4520. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4521. asField:(GPBFieldDescriptor *)field {
  4522. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4523. GPBDataType keyDataType = field.mapKeyDataType;
  4524. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4525. NSDictionary *internal = _dictionary;
  4526. NSEnumerator *keys = [internal keyEnumerator];
  4527. NSNumber *aKey;
  4528. while ((aKey = [keys nextObject])) {
  4529. NSNumber *aValue = internal[aKey];
  4530. [outputStream writeInt32NoTag:tag];
  4531. // Write the size of the message.
  4532. int32_t unwrappedKey = [aKey intValue];
  4533. double unwrappedValue = [aValue doubleValue];
  4534. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4535. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4536. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4537. // Write the fields.
  4538. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4539. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4540. }
  4541. }
  4542. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4543. forGPBGenericValueKey:(GPBGenericValue *)key {
  4544. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4545. }
  4546. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4547. [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4548. #pragma unused(stop)
  4549. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4550. }];
  4551. }
  4552. - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
  4553. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4554. if (wrapped && value) {
  4555. *value = [wrapped doubleValue];
  4556. }
  4557. return (wrapped != NULL);
  4558. }
  4559. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4560. if (otherDictionary) {
  4561. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4562. if (_autocreator) {
  4563. GPBAutocreatedDictionaryModified(_autocreator, self);
  4564. }
  4565. }
  4566. }
  4567. - (void)setDouble:(double)value forKey:(int32_t)key {
  4568. [_dictionary setObject:@(value) forKey:@(key)];
  4569. if (_autocreator) {
  4570. GPBAutocreatedDictionaryModified(_autocreator, self);
  4571. }
  4572. }
  4573. - (void)removeDoubleForKey:(int32_t)aKey {
  4574. [_dictionary removeObjectForKey:@(aKey)];
  4575. }
  4576. - (void)removeAll {
  4577. [_dictionary removeAllObjects];
  4578. }
  4579. @end
  4580. #pragma mark - Int32 -> Enum
  4581. @implementation GPBInt32EnumDictionary {
  4582. @package
  4583. NSMutableDictionary *_dictionary;
  4584. GPBEnumValidationFunc _validationFunc;
  4585. }
  4586. @synthesize validationFunc = _validationFunc;
  4587. + (instancetype)dictionary {
  4588. return [[[self alloc] initWithValidationFunction:NULL
  4589. rawValues:NULL
  4590. forKeys:NULL
  4591. count:0] autorelease];
  4592. }
  4593. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4594. return [[[self alloc] initWithValidationFunction:func
  4595. rawValues:NULL
  4596. forKeys:NULL
  4597. count:0] autorelease];
  4598. }
  4599. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4600. rawValue:(int32_t)rawValue
  4601. forKey:(int32_t)key {
  4602. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4603. // on to get the type correct.
  4604. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4605. rawValues:&rawValue
  4606. forKeys:&key
  4607. count:1] autorelease];
  4608. }
  4609. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4610. rawValues:(const int32_t [])rawValues
  4611. forKeys:(const int32_t [])keys
  4612. count:(NSUInteger)count {
  4613. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4614. // on to get the type correct.
  4615. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4616. rawValues:rawValues
  4617. forKeys:keys
  4618. count:count] autorelease];
  4619. }
  4620. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4621. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4622. // on to get the type correct.
  4623. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4624. }
  4625. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4626. capacity:(NSUInteger)numItems {
  4627. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4628. }
  4629. - (instancetype)init {
  4630. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4631. }
  4632. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4633. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4634. }
  4635. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4636. rawValues:(const int32_t [])rawValues
  4637. forKeys:(const int32_t [])keys
  4638. count:(NSUInteger)count {
  4639. self = [super init];
  4640. if (self) {
  4641. _dictionary = [[NSMutableDictionary alloc] init];
  4642. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4643. if (count && rawValues && keys) {
  4644. for (NSUInteger i = 0; i < count; ++i) {
  4645. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4646. }
  4647. }
  4648. }
  4649. return self;
  4650. }
  4651. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4652. self = [self initWithValidationFunction:dictionary.validationFunc
  4653. rawValues:NULL
  4654. forKeys:NULL
  4655. count:0];
  4656. if (self) {
  4657. if (dictionary) {
  4658. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4659. }
  4660. }
  4661. return self;
  4662. }
  4663. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4664. capacity:(NSUInteger)numItems {
  4665. #pragma unused(numItems)
  4666. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4667. }
  4668. - (void)dealloc {
  4669. NSAssert(!_autocreator,
  4670. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4671. [self class], _autocreator);
  4672. [_dictionary release];
  4673. [super dealloc];
  4674. }
  4675. - (instancetype)copyWithZone:(NSZone *)zone {
  4676. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4677. }
  4678. - (BOOL)isEqual:(id)other {
  4679. if (self == other) {
  4680. return YES;
  4681. }
  4682. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4683. return NO;
  4684. }
  4685. GPBInt32EnumDictionary *otherDictionary = other;
  4686. return [_dictionary isEqual:otherDictionary->_dictionary];
  4687. }
  4688. - (NSUInteger)hash {
  4689. return _dictionary.count;
  4690. }
  4691. - (NSString *)description {
  4692. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4693. }
  4694. - (NSUInteger)count {
  4695. return _dictionary.count;
  4696. }
  4697. - (void)enumerateKeysAndRawValuesUsingBlock:
  4698. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4699. BOOL stop = NO;
  4700. NSDictionary *internal = _dictionary;
  4701. NSEnumerator *keys = [internal keyEnumerator];
  4702. NSNumber *aKey;
  4703. while ((aKey = [keys nextObject])) {
  4704. NSNumber *aValue = internal[aKey];
  4705. block([aKey intValue], [aValue intValue], &stop);
  4706. if (stop) {
  4707. break;
  4708. }
  4709. }
  4710. }
  4711. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4712. NSDictionary *internal = _dictionary;
  4713. NSUInteger count = internal.count;
  4714. if (count == 0) {
  4715. return 0;
  4716. }
  4717. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4718. GPBDataType keyDataType = field.mapKeyDataType;
  4719. size_t result = 0;
  4720. NSEnumerator *keys = [internal keyEnumerator];
  4721. NSNumber *aKey;
  4722. while ((aKey = [keys nextObject])) {
  4723. NSNumber *aValue = internal[aKey];
  4724. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4725. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4726. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4727. }
  4728. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4729. result += tagSize * count;
  4730. return result;
  4731. }
  4732. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4733. asField:(GPBFieldDescriptor *)field {
  4734. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4735. GPBDataType keyDataType = field.mapKeyDataType;
  4736. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4737. NSDictionary *internal = _dictionary;
  4738. NSEnumerator *keys = [internal keyEnumerator];
  4739. NSNumber *aKey;
  4740. while ((aKey = [keys nextObject])) {
  4741. NSNumber *aValue = internal[aKey];
  4742. [outputStream writeInt32NoTag:tag];
  4743. // Write the size of the message.
  4744. int32_t unwrappedKey = [aKey intValue];
  4745. int32_t unwrappedValue = [aValue intValue];
  4746. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4747. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4748. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4749. // Write the fields.
  4750. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4751. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4752. }
  4753. }
  4754. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4755. forKey:(GPBGenericValue *)key
  4756. keyDataType:(GPBDataType)keyDataType {
  4757. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4758. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4759. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4760. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4761. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4762. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4763. [outputStream release];
  4764. return data;
  4765. }
  4766. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4767. forGPBGenericValueKey:(GPBGenericValue *)key {
  4768. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4769. }
  4770. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4771. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4772. #pragma unused(stop)
  4773. block([NSString stringWithFormat:@"%d", key], @(value));
  4774. }];
  4775. }
  4776. - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
  4777. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4778. if (wrapped && value) {
  4779. int32_t result = [wrapped intValue];
  4780. if (!_validationFunc(result)) {
  4781. result = kGPBUnrecognizedEnumeratorValue;
  4782. }
  4783. *value = result;
  4784. }
  4785. return (wrapped != NULL);
  4786. }
  4787. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
  4788. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4789. if (wrapped && rawValue) {
  4790. *rawValue = [wrapped intValue];
  4791. }
  4792. return (wrapped != NULL);
  4793. }
  4794. - (void)enumerateKeysAndEnumsUsingBlock:
  4795. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4796. GPBEnumValidationFunc func = _validationFunc;
  4797. BOOL stop = NO;
  4798. NSEnumerator *keys = [_dictionary keyEnumerator];
  4799. NSNumber *aKey;
  4800. while ((aKey = [keys nextObject])) {
  4801. NSNumber *aValue = _dictionary[aKey];
  4802. int32_t unwrapped = [aValue intValue];
  4803. if (!func(unwrapped)) {
  4804. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4805. }
  4806. block([aKey intValue], unwrapped, &stop);
  4807. if (stop) {
  4808. break;
  4809. }
  4810. }
  4811. }
  4812. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4813. if (otherDictionary) {
  4814. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4815. if (_autocreator) {
  4816. GPBAutocreatedDictionaryModified(_autocreator, self);
  4817. }
  4818. }
  4819. }
  4820. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4821. [_dictionary setObject:@(value) forKey:@(key)];
  4822. if (_autocreator) {
  4823. GPBAutocreatedDictionaryModified(_autocreator, self);
  4824. }
  4825. }
  4826. - (void)removeEnumForKey:(int32_t)aKey {
  4827. [_dictionary removeObjectForKey:@(aKey)];
  4828. }
  4829. - (void)removeAll {
  4830. [_dictionary removeAllObjects];
  4831. }
  4832. - (void)setEnum:(int32_t)value forKey:(int32_t)key {
  4833. if (!_validationFunc(value)) {
  4834. [NSException raise:NSInvalidArgumentException
  4835. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4836. value];
  4837. }
  4838. [_dictionary setObject:@(value) forKey:@(key)];
  4839. if (_autocreator) {
  4840. GPBAutocreatedDictionaryModified(_autocreator, self);
  4841. }
  4842. }
  4843. @end
  4844. #pragma mark - Int32 -> Object
  4845. @implementation GPBInt32ObjectDictionary {
  4846. @package
  4847. NSMutableDictionary *_dictionary;
  4848. }
  4849. + (instancetype)dictionary {
  4850. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4851. }
  4852. + (instancetype)dictionaryWithObject:(id)object
  4853. forKey:(int32_t)key {
  4854. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4855. // on to get the type correct.
  4856. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4857. forKeys:&key
  4858. count:1] autorelease];
  4859. }
  4860. + (instancetype)dictionaryWithObjects:(const id [])objects
  4861. forKeys:(const int32_t [])keys
  4862. count:(NSUInteger)count {
  4863. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4864. // on to get the type correct.
  4865. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4866. forKeys:keys
  4867. count:count] autorelease];
  4868. }
  4869. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4870. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4871. // on to get the type correct.
  4872. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4873. }
  4874. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4875. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4876. }
  4877. - (instancetype)init {
  4878. return [self initWithObjects:NULL forKeys:NULL count:0];
  4879. }
  4880. - (instancetype)initWithObjects:(const id [])objects
  4881. forKeys:(const int32_t [])keys
  4882. count:(NSUInteger)count {
  4883. self = [super init];
  4884. if (self) {
  4885. _dictionary = [[NSMutableDictionary alloc] init];
  4886. if (count && objects && keys) {
  4887. for (NSUInteger i = 0; i < count; ++i) {
  4888. if (!objects[i]) {
  4889. [NSException raise:NSInvalidArgumentException
  4890. format:@"Attempting to add nil object to a Dictionary"];
  4891. }
  4892. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4893. }
  4894. }
  4895. }
  4896. return self;
  4897. }
  4898. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4899. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4900. if (self) {
  4901. if (dictionary) {
  4902. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4903. }
  4904. }
  4905. return self;
  4906. }
  4907. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4908. #pragma unused(numItems)
  4909. return [self initWithObjects:NULL forKeys:NULL count:0];
  4910. }
  4911. - (void)dealloc {
  4912. NSAssert(!_autocreator,
  4913. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4914. [self class], _autocreator);
  4915. [_dictionary release];
  4916. [super dealloc];
  4917. }
  4918. - (instancetype)copyWithZone:(NSZone *)zone {
  4919. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4920. }
  4921. - (BOOL)isEqual:(id)other {
  4922. if (self == other) {
  4923. return YES;
  4924. }
  4925. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4926. return NO;
  4927. }
  4928. GPBInt32ObjectDictionary *otherDictionary = other;
  4929. return [_dictionary isEqual:otherDictionary->_dictionary];
  4930. }
  4931. - (NSUInteger)hash {
  4932. return _dictionary.count;
  4933. }
  4934. - (NSString *)description {
  4935. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4936. }
  4937. - (NSUInteger)count {
  4938. return _dictionary.count;
  4939. }
  4940. - (void)enumerateKeysAndObjectsUsingBlock:
  4941. (void (^)(int32_t key, id object, BOOL *stop))block {
  4942. BOOL stop = NO;
  4943. NSDictionary *internal = _dictionary;
  4944. NSEnumerator *keys = [internal keyEnumerator];
  4945. NSNumber *aKey;
  4946. while ((aKey = [keys nextObject])) {
  4947. id aObject = internal[aKey];
  4948. block([aKey intValue], aObject, &stop);
  4949. if (stop) {
  4950. break;
  4951. }
  4952. }
  4953. }
  4954. - (BOOL)isInitialized {
  4955. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4956. if (!msg.initialized) {
  4957. return NO;
  4958. }
  4959. }
  4960. return YES;
  4961. }
  4962. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4963. GPBInt32ObjectDictionary *newDict =
  4964. [[GPBInt32ObjectDictionary alloc] init];
  4965. NSEnumerator *keys = [_dictionary keyEnumerator];
  4966. id aKey;
  4967. NSMutableDictionary *internalDict = newDict->_dictionary;
  4968. while ((aKey = [keys nextObject])) {
  4969. GPBMessage *msg = _dictionary[aKey];
  4970. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4971. [internalDict setObject:copiedMsg forKey:aKey];
  4972. [copiedMsg release];
  4973. }
  4974. return newDict;
  4975. }
  4976. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4977. NSDictionary *internal = _dictionary;
  4978. NSUInteger count = internal.count;
  4979. if (count == 0) {
  4980. return 0;
  4981. }
  4982. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4983. GPBDataType keyDataType = field.mapKeyDataType;
  4984. size_t result = 0;
  4985. NSEnumerator *keys = [internal keyEnumerator];
  4986. NSNumber *aKey;
  4987. while ((aKey = [keys nextObject])) {
  4988. id aObject = internal[aKey];
  4989. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4990. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4991. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4992. }
  4993. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4994. result += tagSize * count;
  4995. return result;
  4996. }
  4997. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4998. asField:(GPBFieldDescriptor *)field {
  4999. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5000. GPBDataType keyDataType = field.mapKeyDataType;
  5001. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5002. NSDictionary *internal = _dictionary;
  5003. NSEnumerator *keys = [internal keyEnumerator];
  5004. NSNumber *aKey;
  5005. while ((aKey = [keys nextObject])) {
  5006. id aObject = internal[aKey];
  5007. [outputStream writeInt32NoTag:tag];
  5008. // Write the size of the message.
  5009. int32_t unwrappedKey = [aKey intValue];
  5010. id unwrappedValue = aObject;
  5011. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5012. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5013. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5014. // Write the fields.
  5015. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5016. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5017. }
  5018. }
  5019. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5020. forGPBGenericValueKey:(GPBGenericValue *)key {
  5021. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  5022. }
  5023. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5024. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  5025. #pragma unused(stop)
  5026. block([NSString stringWithFormat:@"%d", key], object);
  5027. }];
  5028. }
  5029. - (id)objectForKey:(int32_t)key {
  5030. id result = [_dictionary objectForKey:@(key)];
  5031. return result;
  5032. }
  5033. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  5034. if (otherDictionary) {
  5035. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5036. if (_autocreator) {
  5037. GPBAutocreatedDictionaryModified(_autocreator, self);
  5038. }
  5039. }
  5040. }
  5041. - (void)setObject:(id)object forKey:(int32_t)key {
  5042. if (!object) {
  5043. [NSException raise:NSInvalidArgumentException
  5044. format:@"Attempting to add nil object to a Dictionary"];
  5045. }
  5046. [_dictionary setObject:object forKey:@(key)];
  5047. if (_autocreator) {
  5048. GPBAutocreatedDictionaryModified(_autocreator, self);
  5049. }
  5050. }
  5051. - (void)removeObjectForKey:(int32_t)aKey {
  5052. [_dictionary removeObjectForKey:@(aKey)];
  5053. }
  5054. - (void)removeAll {
  5055. [_dictionary removeAllObjects];
  5056. }
  5057. @end
  5058. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  5059. // This block of code is generated, do not edit it directly.
  5060. #pragma mark - UInt64 -> UInt32
  5061. @implementation GPBUInt64UInt32Dictionary {
  5062. @package
  5063. NSMutableDictionary *_dictionary;
  5064. }
  5065. + (instancetype)dictionary {
  5066. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  5067. }
  5068. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  5069. forKey:(uint64_t)key {
  5070. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  5071. // on to get the type correct.
  5072. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  5073. forKeys:&key
  5074. count:1] autorelease];
  5075. }
  5076. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  5077. forKeys:(const uint64_t [])keys
  5078. count:(NSUInteger)count {
  5079. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  5080. // on to get the type correct.
  5081. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
  5082. forKeys:keys
  5083. count:count] autorelease];
  5084. }
  5085. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  5086. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5087. // on to get the type correct.
  5088. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5089. }
  5090. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5091. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5092. }
  5093. - (instancetype)init {
  5094. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5095. }
  5096. - (instancetype)initWithUInt32s:(const uint32_t [])values
  5097. forKeys:(const uint64_t [])keys
  5098. count:(NSUInteger)count {
  5099. self = [super init];
  5100. if (self) {
  5101. _dictionary = [[NSMutableDictionary alloc] init];
  5102. if (count && values && keys) {
  5103. for (NSUInteger i = 0; i < count; ++i) {
  5104. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5105. }
  5106. }
  5107. }
  5108. return self;
  5109. }
  5110. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  5111. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  5112. if (self) {
  5113. if (dictionary) {
  5114. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5115. }
  5116. }
  5117. return self;
  5118. }
  5119. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5120. #pragma unused(numItems)
  5121. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5122. }
  5123. - (void)dealloc {
  5124. NSAssert(!_autocreator,
  5125. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5126. [self class], _autocreator);
  5127. [_dictionary release];
  5128. [super dealloc];
  5129. }
  5130. - (instancetype)copyWithZone:(NSZone *)zone {
  5131. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  5132. }
  5133. - (BOOL)isEqual:(id)other {
  5134. if (self == other) {
  5135. return YES;
  5136. }
  5137. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  5138. return NO;
  5139. }
  5140. GPBUInt64UInt32Dictionary *otherDictionary = other;
  5141. return [_dictionary isEqual:otherDictionary->_dictionary];
  5142. }
  5143. - (NSUInteger)hash {
  5144. return _dictionary.count;
  5145. }
  5146. - (NSString *)description {
  5147. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5148. }
  5149. - (NSUInteger)count {
  5150. return _dictionary.count;
  5151. }
  5152. - (void)enumerateKeysAndUInt32sUsingBlock:
  5153. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  5154. BOOL stop = NO;
  5155. NSDictionary *internal = _dictionary;
  5156. NSEnumerator *keys = [internal keyEnumerator];
  5157. NSNumber *aKey;
  5158. while ((aKey = [keys nextObject])) {
  5159. NSNumber *aValue = internal[aKey];
  5160. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], &stop);
  5161. if (stop) {
  5162. break;
  5163. }
  5164. }
  5165. }
  5166. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5167. NSDictionary *internal = _dictionary;
  5168. NSUInteger count = internal.count;
  5169. if (count == 0) {
  5170. return 0;
  5171. }
  5172. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5173. GPBDataType keyDataType = field.mapKeyDataType;
  5174. size_t result = 0;
  5175. NSEnumerator *keys = [internal keyEnumerator];
  5176. NSNumber *aKey;
  5177. while ((aKey = [keys nextObject])) {
  5178. NSNumber *aValue = internal[aKey];
  5179. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5180. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  5181. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5182. }
  5183. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5184. result += tagSize * count;
  5185. return result;
  5186. }
  5187. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5188. asField:(GPBFieldDescriptor *)field {
  5189. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5190. GPBDataType keyDataType = field.mapKeyDataType;
  5191. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5192. NSDictionary *internal = _dictionary;
  5193. NSEnumerator *keys = [internal keyEnumerator];
  5194. NSNumber *aKey;
  5195. while ((aKey = [keys nextObject])) {
  5196. NSNumber *aValue = internal[aKey];
  5197. [outputStream writeInt32NoTag:tag];
  5198. // Write the size of the message.
  5199. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5200. uint32_t unwrappedValue = [aValue unsignedIntValue];
  5201. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5202. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5203. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5204. // Write the fields.
  5205. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5206. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5207. }
  5208. }
  5209. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5210. forGPBGenericValueKey:(GPBGenericValue *)key {
  5211. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  5212. }
  5213. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5214. [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  5215. #pragma unused(stop)
  5216. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  5217. }];
  5218. }
  5219. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
  5220. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5221. if (wrapped && value) {
  5222. *value = [wrapped unsignedIntValue];
  5223. }
  5224. return (wrapped != NULL);
  5225. }
  5226. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  5227. if (otherDictionary) {
  5228. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5229. if (_autocreator) {
  5230. GPBAutocreatedDictionaryModified(_autocreator, self);
  5231. }
  5232. }
  5233. }
  5234. - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
  5235. [_dictionary setObject:@(value) forKey:@(key)];
  5236. if (_autocreator) {
  5237. GPBAutocreatedDictionaryModified(_autocreator, self);
  5238. }
  5239. }
  5240. - (void)removeUInt32ForKey:(uint64_t)aKey {
  5241. [_dictionary removeObjectForKey:@(aKey)];
  5242. }
  5243. - (void)removeAll {
  5244. [_dictionary removeAllObjects];
  5245. }
  5246. @end
  5247. #pragma mark - UInt64 -> Int32
  5248. @implementation GPBUInt64Int32Dictionary {
  5249. @package
  5250. NSMutableDictionary *_dictionary;
  5251. }
  5252. + (instancetype)dictionary {
  5253. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  5254. }
  5255. + (instancetype)dictionaryWithInt32:(int32_t)value
  5256. forKey:(uint64_t)key {
  5257. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  5258. // on to get the type correct.
  5259. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
  5260. forKeys:&key
  5261. count:1] autorelease];
  5262. }
  5263. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  5264. forKeys:(const uint64_t [])keys
  5265. count:(NSUInteger)count {
  5266. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  5267. // on to get the type correct.
  5268. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:values
  5269. forKeys:keys
  5270. count:count] autorelease];
  5271. }
  5272. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5273. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5274. // on to get the type correct.
  5275. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5276. }
  5277. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5278. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5279. }
  5280. - (instancetype)init {
  5281. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5282. }
  5283. - (instancetype)initWithInt32s:(const int32_t [])values
  5284. forKeys:(const uint64_t [])keys
  5285. count:(NSUInteger)count {
  5286. self = [super init];
  5287. if (self) {
  5288. _dictionary = [[NSMutableDictionary alloc] init];
  5289. if (count && values && keys) {
  5290. for (NSUInteger i = 0; i < count; ++i) {
  5291. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5292. }
  5293. }
  5294. }
  5295. return self;
  5296. }
  5297. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5298. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  5299. if (self) {
  5300. if (dictionary) {
  5301. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5302. }
  5303. }
  5304. return self;
  5305. }
  5306. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5307. #pragma unused(numItems)
  5308. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5309. }
  5310. - (void)dealloc {
  5311. NSAssert(!_autocreator,
  5312. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5313. [self class], _autocreator);
  5314. [_dictionary release];
  5315. [super dealloc];
  5316. }
  5317. - (instancetype)copyWithZone:(NSZone *)zone {
  5318. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5319. }
  5320. - (BOOL)isEqual:(id)other {
  5321. if (self == other) {
  5322. return YES;
  5323. }
  5324. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5325. return NO;
  5326. }
  5327. GPBUInt64Int32Dictionary *otherDictionary = other;
  5328. return [_dictionary isEqual:otherDictionary->_dictionary];
  5329. }
  5330. - (NSUInteger)hash {
  5331. return _dictionary.count;
  5332. }
  5333. - (NSString *)description {
  5334. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5335. }
  5336. - (NSUInteger)count {
  5337. return _dictionary.count;
  5338. }
  5339. - (void)enumerateKeysAndInt32sUsingBlock:
  5340. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5341. BOOL stop = NO;
  5342. NSDictionary *internal = _dictionary;
  5343. NSEnumerator *keys = [internal keyEnumerator];
  5344. NSNumber *aKey;
  5345. while ((aKey = [keys nextObject])) {
  5346. NSNumber *aValue = internal[aKey];
  5347. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  5348. if (stop) {
  5349. break;
  5350. }
  5351. }
  5352. }
  5353. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5354. NSDictionary *internal = _dictionary;
  5355. NSUInteger count = internal.count;
  5356. if (count == 0) {
  5357. return 0;
  5358. }
  5359. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5360. GPBDataType keyDataType = field.mapKeyDataType;
  5361. size_t result = 0;
  5362. NSEnumerator *keys = [internal keyEnumerator];
  5363. NSNumber *aKey;
  5364. while ((aKey = [keys nextObject])) {
  5365. NSNumber *aValue = internal[aKey];
  5366. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5367. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5368. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5369. }
  5370. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5371. result += tagSize * count;
  5372. return result;
  5373. }
  5374. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5375. asField:(GPBFieldDescriptor *)field {
  5376. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5377. GPBDataType keyDataType = field.mapKeyDataType;
  5378. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5379. NSDictionary *internal = _dictionary;
  5380. NSEnumerator *keys = [internal keyEnumerator];
  5381. NSNumber *aKey;
  5382. while ((aKey = [keys nextObject])) {
  5383. NSNumber *aValue = internal[aKey];
  5384. [outputStream writeInt32NoTag:tag];
  5385. // Write the size of the message.
  5386. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5387. int32_t unwrappedValue = [aValue intValue];
  5388. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5389. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5390. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5391. // Write the fields.
  5392. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5393. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5394. }
  5395. }
  5396. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5397. forGPBGenericValueKey:(GPBGenericValue *)key {
  5398. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5399. }
  5400. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5401. [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5402. #pragma unused(stop)
  5403. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5404. }];
  5405. }
  5406. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
  5407. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5408. if (wrapped && value) {
  5409. *value = [wrapped intValue];
  5410. }
  5411. return (wrapped != NULL);
  5412. }
  5413. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5414. if (otherDictionary) {
  5415. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5416. if (_autocreator) {
  5417. GPBAutocreatedDictionaryModified(_autocreator, self);
  5418. }
  5419. }
  5420. }
  5421. - (void)setInt32:(int32_t)value forKey:(uint64_t)key {
  5422. [_dictionary setObject:@(value) forKey:@(key)];
  5423. if (_autocreator) {
  5424. GPBAutocreatedDictionaryModified(_autocreator, self);
  5425. }
  5426. }
  5427. - (void)removeInt32ForKey:(uint64_t)aKey {
  5428. [_dictionary removeObjectForKey:@(aKey)];
  5429. }
  5430. - (void)removeAll {
  5431. [_dictionary removeAllObjects];
  5432. }
  5433. @end
  5434. #pragma mark - UInt64 -> UInt64
  5435. @implementation GPBUInt64UInt64Dictionary {
  5436. @package
  5437. NSMutableDictionary *_dictionary;
  5438. }
  5439. + (instancetype)dictionary {
  5440. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  5441. }
  5442. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  5443. forKey:(uint64_t)key {
  5444. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  5445. // on to get the type correct.
  5446. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  5447. forKeys:&key
  5448. count:1] autorelease];
  5449. }
  5450. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  5451. forKeys:(const uint64_t [])keys
  5452. count:(NSUInteger)count {
  5453. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  5454. // on to get the type correct.
  5455. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
  5456. forKeys:keys
  5457. count:count] autorelease];
  5458. }
  5459. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5460. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5461. // on to get the type correct.
  5462. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5463. }
  5464. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5465. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5466. }
  5467. - (instancetype)init {
  5468. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  5469. }
  5470. - (instancetype)initWithUInt64s:(const uint64_t [])values
  5471. forKeys:(const uint64_t [])keys
  5472. count:(NSUInteger)count {
  5473. self = [super init];
  5474. if (self) {
  5475. _dictionary = [[NSMutableDictionary alloc] init];
  5476. if (count && values && keys) {
  5477. for (NSUInteger i = 0; i < count; ++i) {
  5478. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5479. }
  5480. }
  5481. }
  5482. return self;
  5483. }
  5484. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5485. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  5486. if (self) {
  5487. if (dictionary) {
  5488. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5489. }
  5490. }
  5491. return self;
  5492. }
  5493. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5494. #pragma unused(numItems)
  5495. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  5496. }
  5497. - (void)dealloc {
  5498. NSAssert(!_autocreator,
  5499. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5500. [self class], _autocreator);
  5501. [_dictionary release];
  5502. [super dealloc];
  5503. }
  5504. - (instancetype)copyWithZone:(NSZone *)zone {
  5505. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5506. }
  5507. - (BOOL)isEqual:(id)other {
  5508. if (self == other) {
  5509. return YES;
  5510. }
  5511. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5512. return NO;
  5513. }
  5514. GPBUInt64UInt64Dictionary *otherDictionary = other;
  5515. return [_dictionary isEqual:otherDictionary->_dictionary];
  5516. }
  5517. - (NSUInteger)hash {
  5518. return _dictionary.count;
  5519. }
  5520. - (NSString *)description {
  5521. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5522. }
  5523. - (NSUInteger)count {
  5524. return _dictionary.count;
  5525. }
  5526. - (void)enumerateKeysAndUInt64sUsingBlock:
  5527. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5528. BOOL stop = NO;
  5529. NSDictionary *internal = _dictionary;
  5530. NSEnumerator *keys = [internal keyEnumerator];
  5531. NSNumber *aKey;
  5532. while ((aKey = [keys nextObject])) {
  5533. NSNumber *aValue = internal[aKey];
  5534. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], &stop);
  5535. if (stop) {
  5536. break;
  5537. }
  5538. }
  5539. }
  5540. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5541. NSDictionary *internal = _dictionary;
  5542. NSUInteger count = internal.count;
  5543. if (count == 0) {
  5544. return 0;
  5545. }
  5546. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5547. GPBDataType keyDataType = field.mapKeyDataType;
  5548. size_t result = 0;
  5549. NSEnumerator *keys = [internal keyEnumerator];
  5550. NSNumber *aKey;
  5551. while ((aKey = [keys nextObject])) {
  5552. NSNumber *aValue = internal[aKey];
  5553. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5554. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5555. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5556. }
  5557. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5558. result += tagSize * count;
  5559. return result;
  5560. }
  5561. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5562. asField:(GPBFieldDescriptor *)field {
  5563. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5564. GPBDataType keyDataType = field.mapKeyDataType;
  5565. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5566. NSDictionary *internal = _dictionary;
  5567. NSEnumerator *keys = [internal keyEnumerator];
  5568. NSNumber *aKey;
  5569. while ((aKey = [keys nextObject])) {
  5570. NSNumber *aValue = internal[aKey];
  5571. [outputStream writeInt32NoTag:tag];
  5572. // Write the size of the message.
  5573. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5574. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  5575. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5576. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5577. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5578. // Write the fields.
  5579. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5580. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5581. }
  5582. }
  5583. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5584. forGPBGenericValueKey:(GPBGenericValue *)key {
  5585. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5586. }
  5587. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5588. [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5589. #pragma unused(stop)
  5590. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5591. }];
  5592. }
  5593. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
  5594. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5595. if (wrapped && value) {
  5596. *value = [wrapped unsignedLongLongValue];
  5597. }
  5598. return (wrapped != NULL);
  5599. }
  5600. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5601. if (otherDictionary) {
  5602. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5603. if (_autocreator) {
  5604. GPBAutocreatedDictionaryModified(_autocreator, self);
  5605. }
  5606. }
  5607. }
  5608. - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
  5609. [_dictionary setObject:@(value) forKey:@(key)];
  5610. if (_autocreator) {
  5611. GPBAutocreatedDictionaryModified(_autocreator, self);
  5612. }
  5613. }
  5614. - (void)removeUInt64ForKey:(uint64_t)aKey {
  5615. [_dictionary removeObjectForKey:@(aKey)];
  5616. }
  5617. - (void)removeAll {
  5618. [_dictionary removeAllObjects];
  5619. }
  5620. @end
  5621. #pragma mark - UInt64 -> Int64
  5622. @implementation GPBUInt64Int64Dictionary {
  5623. @package
  5624. NSMutableDictionary *_dictionary;
  5625. }
  5626. + (instancetype)dictionary {
  5627. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  5628. }
  5629. + (instancetype)dictionaryWithInt64:(int64_t)value
  5630. forKey:(uint64_t)key {
  5631. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  5632. // on to get the type correct.
  5633. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
  5634. forKeys:&key
  5635. count:1] autorelease];
  5636. }
  5637. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  5638. forKeys:(const uint64_t [])keys
  5639. count:(NSUInteger)count {
  5640. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  5641. // on to get the type correct.
  5642. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:values
  5643. forKeys:keys
  5644. count:count] autorelease];
  5645. }
  5646. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5647. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5648. // on to get the type correct.
  5649. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5650. }
  5651. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5652. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5653. }
  5654. - (instancetype)init {
  5655. return [self initWithInt64s:NULL forKeys:NULL count:0];
  5656. }
  5657. - (instancetype)initWithInt64s:(const int64_t [])values
  5658. forKeys:(const uint64_t [])keys
  5659. count:(NSUInteger)count {
  5660. self = [super init];
  5661. if (self) {
  5662. _dictionary = [[NSMutableDictionary alloc] init];
  5663. if (count && values && keys) {
  5664. for (NSUInteger i = 0; i < count; ++i) {
  5665. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5666. }
  5667. }
  5668. }
  5669. return self;
  5670. }
  5671. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5672. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  5673. if (self) {
  5674. if (dictionary) {
  5675. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5676. }
  5677. }
  5678. return self;
  5679. }
  5680. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5681. #pragma unused(numItems)
  5682. return [self initWithInt64s:NULL forKeys:NULL count:0];
  5683. }
  5684. - (void)dealloc {
  5685. NSAssert(!_autocreator,
  5686. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5687. [self class], _autocreator);
  5688. [_dictionary release];
  5689. [super dealloc];
  5690. }
  5691. - (instancetype)copyWithZone:(NSZone *)zone {
  5692. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5693. }
  5694. - (BOOL)isEqual:(id)other {
  5695. if (self == other) {
  5696. return YES;
  5697. }
  5698. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5699. return NO;
  5700. }
  5701. GPBUInt64Int64Dictionary *otherDictionary = other;
  5702. return [_dictionary isEqual:otherDictionary->_dictionary];
  5703. }
  5704. - (NSUInteger)hash {
  5705. return _dictionary.count;
  5706. }
  5707. - (NSString *)description {
  5708. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5709. }
  5710. - (NSUInteger)count {
  5711. return _dictionary.count;
  5712. }
  5713. - (void)enumerateKeysAndInt64sUsingBlock:
  5714. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5715. BOOL stop = NO;
  5716. NSDictionary *internal = _dictionary;
  5717. NSEnumerator *keys = [internal keyEnumerator];
  5718. NSNumber *aKey;
  5719. while ((aKey = [keys nextObject])) {
  5720. NSNumber *aValue = internal[aKey];
  5721. block([aKey unsignedLongLongValue], [aValue longLongValue], &stop);
  5722. if (stop) {
  5723. break;
  5724. }
  5725. }
  5726. }
  5727. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5728. NSDictionary *internal = _dictionary;
  5729. NSUInteger count = internal.count;
  5730. if (count == 0) {
  5731. return 0;
  5732. }
  5733. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5734. GPBDataType keyDataType = field.mapKeyDataType;
  5735. size_t result = 0;
  5736. NSEnumerator *keys = [internal keyEnumerator];
  5737. NSNumber *aKey;
  5738. while ((aKey = [keys nextObject])) {
  5739. NSNumber *aValue = internal[aKey];
  5740. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5741. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5742. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5743. }
  5744. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5745. result += tagSize * count;
  5746. return result;
  5747. }
  5748. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5749. asField:(GPBFieldDescriptor *)field {
  5750. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5751. GPBDataType keyDataType = field.mapKeyDataType;
  5752. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5753. NSDictionary *internal = _dictionary;
  5754. NSEnumerator *keys = [internal keyEnumerator];
  5755. NSNumber *aKey;
  5756. while ((aKey = [keys nextObject])) {
  5757. NSNumber *aValue = internal[aKey];
  5758. [outputStream writeInt32NoTag:tag];
  5759. // Write the size of the message.
  5760. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5761. int64_t unwrappedValue = [aValue longLongValue];
  5762. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5763. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5764. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5765. // Write the fields.
  5766. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5767. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5768. }
  5769. }
  5770. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5771. forGPBGenericValueKey:(GPBGenericValue *)key {
  5772. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5773. }
  5774. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5775. [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5776. #pragma unused(stop)
  5777. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5778. }];
  5779. }
  5780. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
  5781. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5782. if (wrapped && value) {
  5783. *value = [wrapped longLongValue];
  5784. }
  5785. return (wrapped != NULL);
  5786. }
  5787. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5788. if (otherDictionary) {
  5789. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5790. if (_autocreator) {
  5791. GPBAutocreatedDictionaryModified(_autocreator, self);
  5792. }
  5793. }
  5794. }
  5795. - (void)setInt64:(int64_t)value forKey:(uint64_t)key {
  5796. [_dictionary setObject:@(value) forKey:@(key)];
  5797. if (_autocreator) {
  5798. GPBAutocreatedDictionaryModified(_autocreator, self);
  5799. }
  5800. }
  5801. - (void)removeInt64ForKey:(uint64_t)aKey {
  5802. [_dictionary removeObjectForKey:@(aKey)];
  5803. }
  5804. - (void)removeAll {
  5805. [_dictionary removeAllObjects];
  5806. }
  5807. @end
  5808. #pragma mark - UInt64 -> Bool
  5809. @implementation GPBUInt64BoolDictionary {
  5810. @package
  5811. NSMutableDictionary *_dictionary;
  5812. }
  5813. + (instancetype)dictionary {
  5814. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  5815. }
  5816. + (instancetype)dictionaryWithBool:(BOOL)value
  5817. forKey:(uint64_t)key {
  5818. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  5819. // on to get the type correct.
  5820. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:&value
  5821. forKeys:&key
  5822. count:1] autorelease];
  5823. }
  5824. + (instancetype)dictionaryWithBools:(const BOOL [])values
  5825. forKeys:(const uint64_t [])keys
  5826. count:(NSUInteger)count {
  5827. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  5828. // on to get the type correct.
  5829. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:values
  5830. forKeys:keys
  5831. count:count] autorelease];
  5832. }
  5833. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5834. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5835. // on to get the type correct.
  5836. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5837. }
  5838. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5839. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5840. }
  5841. - (instancetype)init {
  5842. return [self initWithBools:NULL forKeys:NULL count:0];
  5843. }
  5844. - (instancetype)initWithBools:(const BOOL [])values
  5845. forKeys:(const uint64_t [])keys
  5846. count:(NSUInteger)count {
  5847. self = [super init];
  5848. if (self) {
  5849. _dictionary = [[NSMutableDictionary alloc] init];
  5850. if (count && values && keys) {
  5851. for (NSUInteger i = 0; i < count; ++i) {
  5852. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5853. }
  5854. }
  5855. }
  5856. return self;
  5857. }
  5858. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5859. self = [self initWithBools:NULL forKeys:NULL count:0];
  5860. if (self) {
  5861. if (dictionary) {
  5862. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5863. }
  5864. }
  5865. return self;
  5866. }
  5867. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5868. #pragma unused(numItems)
  5869. return [self initWithBools:NULL forKeys:NULL count:0];
  5870. }
  5871. - (void)dealloc {
  5872. NSAssert(!_autocreator,
  5873. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5874. [self class], _autocreator);
  5875. [_dictionary release];
  5876. [super dealloc];
  5877. }
  5878. - (instancetype)copyWithZone:(NSZone *)zone {
  5879. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5880. }
  5881. - (BOOL)isEqual:(id)other {
  5882. if (self == other) {
  5883. return YES;
  5884. }
  5885. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5886. return NO;
  5887. }
  5888. GPBUInt64BoolDictionary *otherDictionary = other;
  5889. return [_dictionary isEqual:otherDictionary->_dictionary];
  5890. }
  5891. - (NSUInteger)hash {
  5892. return _dictionary.count;
  5893. }
  5894. - (NSString *)description {
  5895. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5896. }
  5897. - (NSUInteger)count {
  5898. return _dictionary.count;
  5899. }
  5900. - (void)enumerateKeysAndBoolsUsingBlock:
  5901. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5902. BOOL stop = NO;
  5903. NSDictionary *internal = _dictionary;
  5904. NSEnumerator *keys = [internal keyEnumerator];
  5905. NSNumber *aKey;
  5906. while ((aKey = [keys nextObject])) {
  5907. NSNumber *aValue = internal[aKey];
  5908. block([aKey unsignedLongLongValue], [aValue boolValue], &stop);
  5909. if (stop) {
  5910. break;
  5911. }
  5912. }
  5913. }
  5914. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5915. NSDictionary *internal = _dictionary;
  5916. NSUInteger count = internal.count;
  5917. if (count == 0) {
  5918. return 0;
  5919. }
  5920. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5921. GPBDataType keyDataType = field.mapKeyDataType;
  5922. size_t result = 0;
  5923. NSEnumerator *keys = [internal keyEnumerator];
  5924. NSNumber *aKey;
  5925. while ((aKey = [keys nextObject])) {
  5926. NSNumber *aValue = internal[aKey];
  5927. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5928. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5929. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5930. }
  5931. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5932. result += tagSize * count;
  5933. return result;
  5934. }
  5935. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5936. asField:(GPBFieldDescriptor *)field {
  5937. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5938. GPBDataType keyDataType = field.mapKeyDataType;
  5939. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5940. NSDictionary *internal = _dictionary;
  5941. NSEnumerator *keys = [internal keyEnumerator];
  5942. NSNumber *aKey;
  5943. while ((aKey = [keys nextObject])) {
  5944. NSNumber *aValue = internal[aKey];
  5945. [outputStream writeInt32NoTag:tag];
  5946. // Write the size of the message.
  5947. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5948. BOOL unwrappedValue = [aValue boolValue];
  5949. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5950. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5951. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5952. // Write the fields.
  5953. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5954. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5955. }
  5956. }
  5957. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5958. forGPBGenericValueKey:(GPBGenericValue *)key {
  5959. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5960. }
  5961. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5962. [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5963. #pragma unused(stop)
  5964. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5965. }];
  5966. }
  5967. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
  5968. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5969. if (wrapped && value) {
  5970. *value = [wrapped boolValue];
  5971. }
  5972. return (wrapped != NULL);
  5973. }
  5974. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5975. if (otherDictionary) {
  5976. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5977. if (_autocreator) {
  5978. GPBAutocreatedDictionaryModified(_autocreator, self);
  5979. }
  5980. }
  5981. }
  5982. - (void)setBool:(BOOL)value forKey:(uint64_t)key {
  5983. [_dictionary setObject:@(value) forKey:@(key)];
  5984. if (_autocreator) {
  5985. GPBAutocreatedDictionaryModified(_autocreator, self);
  5986. }
  5987. }
  5988. - (void)removeBoolForKey:(uint64_t)aKey {
  5989. [_dictionary removeObjectForKey:@(aKey)];
  5990. }
  5991. - (void)removeAll {
  5992. [_dictionary removeAllObjects];
  5993. }
  5994. @end
  5995. #pragma mark - UInt64 -> Float
  5996. @implementation GPBUInt64FloatDictionary {
  5997. @package
  5998. NSMutableDictionary *_dictionary;
  5999. }
  6000. + (instancetype)dictionary {
  6001. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  6002. }
  6003. + (instancetype)dictionaryWithFloat:(float)value
  6004. forKey:(uint64_t)key {
  6005. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  6006. // on to get the type correct.
  6007. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:&value
  6008. forKeys:&key
  6009. count:1] autorelease];
  6010. }
  6011. + (instancetype)dictionaryWithFloats:(const float [])values
  6012. forKeys:(const uint64_t [])keys
  6013. count:(NSUInteger)count {
  6014. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  6015. // on to get the type correct.
  6016. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:values
  6017. forKeys:keys
  6018. count:count] autorelease];
  6019. }
  6020. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  6021. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6022. // on to get the type correct.
  6023. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6024. }
  6025. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6026. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6027. }
  6028. - (instancetype)init {
  6029. return [self initWithFloats:NULL forKeys:NULL count:0];
  6030. }
  6031. - (instancetype)initWithFloats:(const float [])values
  6032. forKeys:(const uint64_t [])keys
  6033. count:(NSUInteger)count {
  6034. self = [super init];
  6035. if (self) {
  6036. _dictionary = [[NSMutableDictionary alloc] init];
  6037. if (count && values && keys) {
  6038. for (NSUInteger i = 0; i < count; ++i) {
  6039. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6040. }
  6041. }
  6042. }
  6043. return self;
  6044. }
  6045. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  6046. self = [self initWithFloats:NULL forKeys:NULL count:0];
  6047. if (self) {
  6048. if (dictionary) {
  6049. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6050. }
  6051. }
  6052. return self;
  6053. }
  6054. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6055. #pragma unused(numItems)
  6056. return [self initWithFloats:NULL forKeys:NULL count:0];
  6057. }
  6058. - (void)dealloc {
  6059. NSAssert(!_autocreator,
  6060. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6061. [self class], _autocreator);
  6062. [_dictionary release];
  6063. [super dealloc];
  6064. }
  6065. - (instancetype)copyWithZone:(NSZone *)zone {
  6066. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  6067. }
  6068. - (BOOL)isEqual:(id)other {
  6069. if (self == other) {
  6070. return YES;
  6071. }
  6072. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  6073. return NO;
  6074. }
  6075. GPBUInt64FloatDictionary *otherDictionary = other;
  6076. return [_dictionary isEqual:otherDictionary->_dictionary];
  6077. }
  6078. - (NSUInteger)hash {
  6079. return _dictionary.count;
  6080. }
  6081. - (NSString *)description {
  6082. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6083. }
  6084. - (NSUInteger)count {
  6085. return _dictionary.count;
  6086. }
  6087. - (void)enumerateKeysAndFloatsUsingBlock:
  6088. (void (^)(uint64_t key, float value, BOOL *stop))block {
  6089. BOOL stop = NO;
  6090. NSDictionary *internal = _dictionary;
  6091. NSEnumerator *keys = [internal keyEnumerator];
  6092. NSNumber *aKey;
  6093. while ((aKey = [keys nextObject])) {
  6094. NSNumber *aValue = internal[aKey];
  6095. block([aKey unsignedLongLongValue], [aValue floatValue], &stop);
  6096. if (stop) {
  6097. break;
  6098. }
  6099. }
  6100. }
  6101. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6102. NSDictionary *internal = _dictionary;
  6103. NSUInteger count = internal.count;
  6104. if (count == 0) {
  6105. return 0;
  6106. }
  6107. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6108. GPBDataType keyDataType = field.mapKeyDataType;
  6109. size_t result = 0;
  6110. NSEnumerator *keys = [internal keyEnumerator];
  6111. NSNumber *aKey;
  6112. while ((aKey = [keys nextObject])) {
  6113. NSNumber *aValue = internal[aKey];
  6114. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6115. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  6116. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6117. }
  6118. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6119. result += tagSize * count;
  6120. return result;
  6121. }
  6122. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6123. asField:(GPBFieldDescriptor *)field {
  6124. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6125. GPBDataType keyDataType = field.mapKeyDataType;
  6126. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6127. NSDictionary *internal = _dictionary;
  6128. NSEnumerator *keys = [internal keyEnumerator];
  6129. NSNumber *aKey;
  6130. while ((aKey = [keys nextObject])) {
  6131. NSNumber *aValue = internal[aKey];
  6132. [outputStream writeInt32NoTag:tag];
  6133. // Write the size of the message.
  6134. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  6135. float unwrappedValue = [aValue floatValue];
  6136. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6137. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6138. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6139. // Write the fields.
  6140. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6141. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6142. }
  6143. }
  6144. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6145. forGPBGenericValueKey:(GPBGenericValue *)key {
  6146. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  6147. }
  6148. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6149. [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  6150. #pragma unused(stop)
  6151. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  6152. }];
  6153. }
  6154. - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
  6155. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6156. if (wrapped && value) {
  6157. *value = [wrapped floatValue];
  6158. }
  6159. return (wrapped != NULL);
  6160. }
  6161. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  6162. if (otherDictionary) {
  6163. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6164. if (_autocreator) {
  6165. GPBAutocreatedDictionaryModified(_autocreator, self);
  6166. }
  6167. }
  6168. }
  6169. - (void)setFloat:(float)value forKey:(uint64_t)key {
  6170. [_dictionary setObject:@(value) forKey:@(key)];
  6171. if (_autocreator) {
  6172. GPBAutocreatedDictionaryModified(_autocreator, self);
  6173. }
  6174. }
  6175. - (void)removeFloatForKey:(uint64_t)aKey {
  6176. [_dictionary removeObjectForKey:@(aKey)];
  6177. }
  6178. - (void)removeAll {
  6179. [_dictionary removeAllObjects];
  6180. }
  6181. @end
  6182. #pragma mark - UInt64 -> Double
  6183. @implementation GPBUInt64DoubleDictionary {
  6184. @package
  6185. NSMutableDictionary *_dictionary;
  6186. }
  6187. + (instancetype)dictionary {
  6188. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  6189. }
  6190. + (instancetype)dictionaryWithDouble:(double)value
  6191. forKey:(uint64_t)key {
  6192. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  6193. // on to get the type correct.
  6194. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
  6195. forKeys:&key
  6196. count:1] autorelease];
  6197. }
  6198. + (instancetype)dictionaryWithDoubles:(const double [])values
  6199. forKeys:(const uint64_t [])keys
  6200. count:(NSUInteger)count {
  6201. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  6202. // on to get the type correct.
  6203. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:values
  6204. forKeys:keys
  6205. count:count] autorelease];
  6206. }
  6207. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  6208. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6209. // on to get the type correct.
  6210. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6211. }
  6212. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6213. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6214. }
  6215. - (instancetype)init {
  6216. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6217. }
  6218. - (instancetype)initWithDoubles:(const double [])values
  6219. forKeys:(const uint64_t [])keys
  6220. count:(NSUInteger)count {
  6221. self = [super init];
  6222. if (self) {
  6223. _dictionary = [[NSMutableDictionary alloc] init];
  6224. if (count && values && keys) {
  6225. for (NSUInteger i = 0; i < count; ++i) {
  6226. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6227. }
  6228. }
  6229. }
  6230. return self;
  6231. }
  6232. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  6233. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  6234. if (self) {
  6235. if (dictionary) {
  6236. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6237. }
  6238. }
  6239. return self;
  6240. }
  6241. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6242. #pragma unused(numItems)
  6243. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6244. }
  6245. - (void)dealloc {
  6246. NSAssert(!_autocreator,
  6247. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6248. [self class], _autocreator);
  6249. [_dictionary release];
  6250. [super dealloc];
  6251. }
  6252. - (instancetype)copyWithZone:(NSZone *)zone {
  6253. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6254. }
  6255. - (BOOL)isEqual:(id)other {
  6256. if (self == other) {
  6257. return YES;
  6258. }
  6259. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  6260. return NO;
  6261. }
  6262. GPBUInt64DoubleDictionary *otherDictionary = other;
  6263. return [_dictionary isEqual:otherDictionary->_dictionary];
  6264. }
  6265. - (NSUInteger)hash {
  6266. return _dictionary.count;
  6267. }
  6268. - (NSString *)description {
  6269. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6270. }
  6271. - (NSUInteger)count {
  6272. return _dictionary.count;
  6273. }
  6274. - (void)enumerateKeysAndDoublesUsingBlock:
  6275. (void (^)(uint64_t key, double value, BOOL *stop))block {
  6276. BOOL stop = NO;
  6277. NSDictionary *internal = _dictionary;
  6278. NSEnumerator *keys = [internal keyEnumerator];
  6279. NSNumber *aKey;
  6280. while ((aKey = [keys nextObject])) {
  6281. NSNumber *aValue = internal[aKey];
  6282. block([aKey unsignedLongLongValue], [aValue doubleValue], &stop);
  6283. if (stop) {
  6284. break;
  6285. }
  6286. }
  6287. }
  6288. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6289. NSDictionary *internal = _dictionary;
  6290. NSUInteger count = internal.count;
  6291. if (count == 0) {
  6292. return 0;
  6293. }
  6294. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6295. GPBDataType keyDataType = field.mapKeyDataType;
  6296. size_t result = 0;
  6297. NSEnumerator *keys = [internal keyEnumerator];
  6298. NSNumber *aKey;
  6299. while ((aKey = [keys nextObject])) {
  6300. NSNumber *aValue = internal[aKey];
  6301. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6302. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6303. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6304. }
  6305. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6306. result += tagSize * count;
  6307. return result;
  6308. }
  6309. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6310. asField:(GPBFieldDescriptor *)field {
  6311. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6312. GPBDataType keyDataType = field.mapKeyDataType;
  6313. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6314. NSDictionary *internal = _dictionary;
  6315. NSEnumerator *keys = [internal keyEnumerator];
  6316. NSNumber *aKey;
  6317. while ((aKey = [keys nextObject])) {
  6318. NSNumber *aValue = internal[aKey];
  6319. [outputStream writeInt32NoTag:tag];
  6320. // Write the size of the message.
  6321. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  6322. double unwrappedValue = [aValue doubleValue];
  6323. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6324. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6325. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6326. // Write the fields.
  6327. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6328. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6329. }
  6330. }
  6331. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6332. forGPBGenericValueKey:(GPBGenericValue *)key {
  6333. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  6334. }
  6335. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6336. [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  6337. #pragma unused(stop)
  6338. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6339. }];
  6340. }
  6341. - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
  6342. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6343. if (wrapped && value) {
  6344. *value = [wrapped doubleValue];
  6345. }
  6346. return (wrapped != NULL);
  6347. }
  6348. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  6349. if (otherDictionary) {
  6350. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6351. if (_autocreator) {
  6352. GPBAutocreatedDictionaryModified(_autocreator, self);
  6353. }
  6354. }
  6355. }
  6356. - (void)setDouble:(double)value forKey:(uint64_t)key {
  6357. [_dictionary setObject:@(value) forKey:@(key)];
  6358. if (_autocreator) {
  6359. GPBAutocreatedDictionaryModified(_autocreator, self);
  6360. }
  6361. }
  6362. - (void)removeDoubleForKey:(uint64_t)aKey {
  6363. [_dictionary removeObjectForKey:@(aKey)];
  6364. }
  6365. - (void)removeAll {
  6366. [_dictionary removeAllObjects];
  6367. }
  6368. @end
  6369. #pragma mark - UInt64 -> Enum
  6370. @implementation GPBUInt64EnumDictionary {
  6371. @package
  6372. NSMutableDictionary *_dictionary;
  6373. GPBEnumValidationFunc _validationFunc;
  6374. }
  6375. @synthesize validationFunc = _validationFunc;
  6376. + (instancetype)dictionary {
  6377. return [[[self alloc] initWithValidationFunction:NULL
  6378. rawValues:NULL
  6379. forKeys:NULL
  6380. count:0] autorelease];
  6381. }
  6382. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6383. return [[[self alloc] initWithValidationFunction:func
  6384. rawValues:NULL
  6385. forKeys:NULL
  6386. count:0] autorelease];
  6387. }
  6388. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6389. rawValue:(int32_t)rawValue
  6390. forKey:(uint64_t)key {
  6391. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6392. // on to get the type correct.
  6393. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6394. rawValues:&rawValue
  6395. forKeys:&key
  6396. count:1] autorelease];
  6397. }
  6398. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6399. rawValues:(const int32_t [])rawValues
  6400. forKeys:(const uint64_t [])keys
  6401. count:(NSUInteger)count {
  6402. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6403. // on to get the type correct.
  6404. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6405. rawValues:rawValues
  6406. forKeys:keys
  6407. count:count] autorelease];
  6408. }
  6409. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6410. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6411. // on to get the type correct.
  6412. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6413. }
  6414. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6415. capacity:(NSUInteger)numItems {
  6416. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6417. }
  6418. - (instancetype)init {
  6419. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6420. }
  6421. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6422. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6423. }
  6424. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6425. rawValues:(const int32_t [])rawValues
  6426. forKeys:(const uint64_t [])keys
  6427. count:(NSUInteger)count {
  6428. self = [super init];
  6429. if (self) {
  6430. _dictionary = [[NSMutableDictionary alloc] init];
  6431. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6432. if (count && rawValues && keys) {
  6433. for (NSUInteger i = 0; i < count; ++i) {
  6434. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6435. }
  6436. }
  6437. }
  6438. return self;
  6439. }
  6440. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6441. self = [self initWithValidationFunction:dictionary.validationFunc
  6442. rawValues:NULL
  6443. forKeys:NULL
  6444. count:0];
  6445. if (self) {
  6446. if (dictionary) {
  6447. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6448. }
  6449. }
  6450. return self;
  6451. }
  6452. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6453. capacity:(NSUInteger)numItems {
  6454. #pragma unused(numItems)
  6455. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6456. }
  6457. - (void)dealloc {
  6458. NSAssert(!_autocreator,
  6459. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6460. [self class], _autocreator);
  6461. [_dictionary release];
  6462. [super dealloc];
  6463. }
  6464. - (instancetype)copyWithZone:(NSZone *)zone {
  6465. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6466. }
  6467. - (BOOL)isEqual:(id)other {
  6468. if (self == other) {
  6469. return YES;
  6470. }
  6471. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6472. return NO;
  6473. }
  6474. GPBUInt64EnumDictionary *otherDictionary = other;
  6475. return [_dictionary isEqual:otherDictionary->_dictionary];
  6476. }
  6477. - (NSUInteger)hash {
  6478. return _dictionary.count;
  6479. }
  6480. - (NSString *)description {
  6481. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6482. }
  6483. - (NSUInteger)count {
  6484. return _dictionary.count;
  6485. }
  6486. - (void)enumerateKeysAndRawValuesUsingBlock:
  6487. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6488. BOOL stop = NO;
  6489. NSDictionary *internal = _dictionary;
  6490. NSEnumerator *keys = [internal keyEnumerator];
  6491. NSNumber *aKey;
  6492. while ((aKey = [keys nextObject])) {
  6493. NSNumber *aValue = internal[aKey];
  6494. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  6495. if (stop) {
  6496. break;
  6497. }
  6498. }
  6499. }
  6500. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6501. NSDictionary *internal = _dictionary;
  6502. NSUInteger count = internal.count;
  6503. if (count == 0) {
  6504. return 0;
  6505. }
  6506. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6507. GPBDataType keyDataType = field.mapKeyDataType;
  6508. size_t result = 0;
  6509. NSEnumerator *keys = [internal keyEnumerator];
  6510. NSNumber *aKey;
  6511. while ((aKey = [keys nextObject])) {
  6512. NSNumber *aValue = internal[aKey];
  6513. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6514. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6515. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6516. }
  6517. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6518. result += tagSize * count;
  6519. return result;
  6520. }
  6521. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6522. asField:(GPBFieldDescriptor *)field {
  6523. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6524. GPBDataType keyDataType = field.mapKeyDataType;
  6525. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6526. NSDictionary *internal = _dictionary;
  6527. NSEnumerator *keys = [internal keyEnumerator];
  6528. NSNumber *aKey;
  6529. while ((aKey = [keys nextObject])) {
  6530. NSNumber *aValue = internal[aKey];
  6531. [outputStream writeInt32NoTag:tag];
  6532. // Write the size of the message.
  6533. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  6534. int32_t unwrappedValue = [aValue intValue];
  6535. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6536. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6537. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6538. // Write the fields.
  6539. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6540. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6541. }
  6542. }
  6543. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6544. forKey:(GPBGenericValue *)key
  6545. keyDataType:(GPBDataType)keyDataType {
  6546. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6547. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6548. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6549. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6550. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6551. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6552. [outputStream release];
  6553. return data;
  6554. }
  6555. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6556. forGPBGenericValueKey:(GPBGenericValue *)key {
  6557. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6558. }
  6559. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6560. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6561. #pragma unused(stop)
  6562. block([NSString stringWithFormat:@"%llu", key], @(value));
  6563. }];
  6564. }
  6565. - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
  6566. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6567. if (wrapped && value) {
  6568. int32_t result = [wrapped intValue];
  6569. if (!_validationFunc(result)) {
  6570. result = kGPBUnrecognizedEnumeratorValue;
  6571. }
  6572. *value = result;
  6573. }
  6574. return (wrapped != NULL);
  6575. }
  6576. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
  6577. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6578. if (wrapped && rawValue) {
  6579. *rawValue = [wrapped intValue];
  6580. }
  6581. return (wrapped != NULL);
  6582. }
  6583. - (void)enumerateKeysAndEnumsUsingBlock:
  6584. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6585. GPBEnumValidationFunc func = _validationFunc;
  6586. BOOL stop = NO;
  6587. NSEnumerator *keys = [_dictionary keyEnumerator];
  6588. NSNumber *aKey;
  6589. while ((aKey = [keys nextObject])) {
  6590. NSNumber *aValue = _dictionary[aKey];
  6591. int32_t unwrapped = [aValue intValue];
  6592. if (!func(unwrapped)) {
  6593. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6594. }
  6595. block([aKey unsignedLongLongValue], unwrapped, &stop);
  6596. if (stop) {
  6597. break;
  6598. }
  6599. }
  6600. }
  6601. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6602. if (otherDictionary) {
  6603. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6604. if (_autocreator) {
  6605. GPBAutocreatedDictionaryModified(_autocreator, self);
  6606. }
  6607. }
  6608. }
  6609. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6610. [_dictionary setObject:@(value) forKey:@(key)];
  6611. if (_autocreator) {
  6612. GPBAutocreatedDictionaryModified(_autocreator, self);
  6613. }
  6614. }
  6615. - (void)removeEnumForKey:(uint64_t)aKey {
  6616. [_dictionary removeObjectForKey:@(aKey)];
  6617. }
  6618. - (void)removeAll {
  6619. [_dictionary removeAllObjects];
  6620. }
  6621. - (void)setEnum:(int32_t)value forKey:(uint64_t)key {
  6622. if (!_validationFunc(value)) {
  6623. [NSException raise:NSInvalidArgumentException
  6624. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6625. value];
  6626. }
  6627. [_dictionary setObject:@(value) forKey:@(key)];
  6628. if (_autocreator) {
  6629. GPBAutocreatedDictionaryModified(_autocreator, self);
  6630. }
  6631. }
  6632. @end
  6633. #pragma mark - UInt64 -> Object
  6634. @implementation GPBUInt64ObjectDictionary {
  6635. @package
  6636. NSMutableDictionary *_dictionary;
  6637. }
  6638. + (instancetype)dictionary {
  6639. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6640. }
  6641. + (instancetype)dictionaryWithObject:(id)object
  6642. forKey:(uint64_t)key {
  6643. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6644. // on to get the type correct.
  6645. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6646. forKeys:&key
  6647. count:1] autorelease];
  6648. }
  6649. + (instancetype)dictionaryWithObjects:(const id [])objects
  6650. forKeys:(const uint64_t [])keys
  6651. count:(NSUInteger)count {
  6652. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6653. // on to get the type correct.
  6654. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6655. forKeys:keys
  6656. count:count] autorelease];
  6657. }
  6658. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6659. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6660. // on to get the type correct.
  6661. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6662. }
  6663. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6664. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6665. }
  6666. - (instancetype)init {
  6667. return [self initWithObjects:NULL forKeys:NULL count:0];
  6668. }
  6669. - (instancetype)initWithObjects:(const id [])objects
  6670. forKeys:(const uint64_t [])keys
  6671. count:(NSUInteger)count {
  6672. self = [super init];
  6673. if (self) {
  6674. _dictionary = [[NSMutableDictionary alloc] init];
  6675. if (count && objects && keys) {
  6676. for (NSUInteger i = 0; i < count; ++i) {
  6677. if (!objects[i]) {
  6678. [NSException raise:NSInvalidArgumentException
  6679. format:@"Attempting to add nil object to a Dictionary"];
  6680. }
  6681. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6682. }
  6683. }
  6684. }
  6685. return self;
  6686. }
  6687. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6688. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6689. if (self) {
  6690. if (dictionary) {
  6691. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6692. }
  6693. }
  6694. return self;
  6695. }
  6696. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6697. #pragma unused(numItems)
  6698. return [self initWithObjects:NULL forKeys:NULL count:0];
  6699. }
  6700. - (void)dealloc {
  6701. NSAssert(!_autocreator,
  6702. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6703. [self class], _autocreator);
  6704. [_dictionary release];
  6705. [super dealloc];
  6706. }
  6707. - (instancetype)copyWithZone:(NSZone *)zone {
  6708. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6709. }
  6710. - (BOOL)isEqual:(id)other {
  6711. if (self == other) {
  6712. return YES;
  6713. }
  6714. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6715. return NO;
  6716. }
  6717. GPBUInt64ObjectDictionary *otherDictionary = other;
  6718. return [_dictionary isEqual:otherDictionary->_dictionary];
  6719. }
  6720. - (NSUInteger)hash {
  6721. return _dictionary.count;
  6722. }
  6723. - (NSString *)description {
  6724. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6725. }
  6726. - (NSUInteger)count {
  6727. return _dictionary.count;
  6728. }
  6729. - (void)enumerateKeysAndObjectsUsingBlock:
  6730. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6731. BOOL stop = NO;
  6732. NSDictionary *internal = _dictionary;
  6733. NSEnumerator *keys = [internal keyEnumerator];
  6734. NSNumber *aKey;
  6735. while ((aKey = [keys nextObject])) {
  6736. id aObject = internal[aKey];
  6737. block([aKey unsignedLongLongValue], aObject, &stop);
  6738. if (stop) {
  6739. break;
  6740. }
  6741. }
  6742. }
  6743. - (BOOL)isInitialized {
  6744. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6745. if (!msg.initialized) {
  6746. return NO;
  6747. }
  6748. }
  6749. return YES;
  6750. }
  6751. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6752. GPBUInt64ObjectDictionary *newDict =
  6753. [[GPBUInt64ObjectDictionary alloc] init];
  6754. NSEnumerator *keys = [_dictionary keyEnumerator];
  6755. id aKey;
  6756. NSMutableDictionary *internalDict = newDict->_dictionary;
  6757. while ((aKey = [keys nextObject])) {
  6758. GPBMessage *msg = _dictionary[aKey];
  6759. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6760. [internalDict setObject:copiedMsg forKey:aKey];
  6761. [copiedMsg release];
  6762. }
  6763. return newDict;
  6764. }
  6765. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6766. NSDictionary *internal = _dictionary;
  6767. NSUInteger count = internal.count;
  6768. if (count == 0) {
  6769. return 0;
  6770. }
  6771. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6772. GPBDataType keyDataType = field.mapKeyDataType;
  6773. size_t result = 0;
  6774. NSEnumerator *keys = [internal keyEnumerator];
  6775. NSNumber *aKey;
  6776. while ((aKey = [keys nextObject])) {
  6777. id aObject = internal[aKey];
  6778. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6779. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6780. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6781. }
  6782. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6783. result += tagSize * count;
  6784. return result;
  6785. }
  6786. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6787. asField:(GPBFieldDescriptor *)field {
  6788. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6789. GPBDataType keyDataType = field.mapKeyDataType;
  6790. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6791. NSDictionary *internal = _dictionary;
  6792. NSEnumerator *keys = [internal keyEnumerator];
  6793. NSNumber *aKey;
  6794. while ((aKey = [keys nextObject])) {
  6795. id aObject = internal[aKey];
  6796. [outputStream writeInt32NoTag:tag];
  6797. // Write the size of the message.
  6798. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  6799. id unwrappedValue = aObject;
  6800. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6801. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6802. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6803. // Write the fields.
  6804. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6805. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6806. }
  6807. }
  6808. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6809. forGPBGenericValueKey:(GPBGenericValue *)key {
  6810. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6811. }
  6812. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6813. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6814. #pragma unused(stop)
  6815. block([NSString stringWithFormat:@"%llu", key], object);
  6816. }];
  6817. }
  6818. - (id)objectForKey:(uint64_t)key {
  6819. id result = [_dictionary objectForKey:@(key)];
  6820. return result;
  6821. }
  6822. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6823. if (otherDictionary) {
  6824. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6825. if (_autocreator) {
  6826. GPBAutocreatedDictionaryModified(_autocreator, self);
  6827. }
  6828. }
  6829. }
  6830. - (void)setObject:(id)object forKey:(uint64_t)key {
  6831. if (!object) {
  6832. [NSException raise:NSInvalidArgumentException
  6833. format:@"Attempting to add nil object to a Dictionary"];
  6834. }
  6835. [_dictionary setObject:object forKey:@(key)];
  6836. if (_autocreator) {
  6837. GPBAutocreatedDictionaryModified(_autocreator, self);
  6838. }
  6839. }
  6840. - (void)removeObjectForKey:(uint64_t)aKey {
  6841. [_dictionary removeObjectForKey:@(aKey)];
  6842. }
  6843. - (void)removeAll {
  6844. [_dictionary removeAllObjects];
  6845. }
  6846. @end
  6847. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6848. // This block of code is generated, do not edit it directly.
  6849. #pragma mark - Int64 -> UInt32
  6850. @implementation GPBInt64UInt32Dictionary {
  6851. @package
  6852. NSMutableDictionary *_dictionary;
  6853. }
  6854. + (instancetype)dictionary {
  6855. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  6856. }
  6857. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  6858. forKey:(int64_t)key {
  6859. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  6860. // on to get the type correct.
  6861. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  6862. forKeys:&key
  6863. count:1] autorelease];
  6864. }
  6865. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  6866. forKeys:(const int64_t [])keys
  6867. count:(NSUInteger)count {
  6868. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  6869. // on to get the type correct.
  6870. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
  6871. forKeys:keys
  6872. count:count] autorelease];
  6873. }
  6874. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6875. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6876. // on to get the type correct.
  6877. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6878. }
  6879. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6880. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6881. }
  6882. - (instancetype)init {
  6883. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  6884. }
  6885. - (instancetype)initWithUInt32s:(const uint32_t [])values
  6886. forKeys:(const int64_t [])keys
  6887. count:(NSUInteger)count {
  6888. self = [super init];
  6889. if (self) {
  6890. _dictionary = [[NSMutableDictionary alloc] init];
  6891. if (count && values && keys) {
  6892. for (NSUInteger i = 0; i < count; ++i) {
  6893. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6894. }
  6895. }
  6896. }
  6897. return self;
  6898. }
  6899. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6900. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  6901. if (self) {
  6902. if (dictionary) {
  6903. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6904. }
  6905. }
  6906. return self;
  6907. }
  6908. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6909. #pragma unused(numItems)
  6910. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  6911. }
  6912. - (void)dealloc {
  6913. NSAssert(!_autocreator,
  6914. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6915. [self class], _autocreator);
  6916. [_dictionary release];
  6917. [super dealloc];
  6918. }
  6919. - (instancetype)copyWithZone:(NSZone *)zone {
  6920. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6921. }
  6922. - (BOOL)isEqual:(id)other {
  6923. if (self == other) {
  6924. return YES;
  6925. }
  6926. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6927. return NO;
  6928. }
  6929. GPBInt64UInt32Dictionary *otherDictionary = other;
  6930. return [_dictionary isEqual:otherDictionary->_dictionary];
  6931. }
  6932. - (NSUInteger)hash {
  6933. return _dictionary.count;
  6934. }
  6935. - (NSString *)description {
  6936. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6937. }
  6938. - (NSUInteger)count {
  6939. return _dictionary.count;
  6940. }
  6941. - (void)enumerateKeysAndUInt32sUsingBlock:
  6942. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6943. BOOL stop = NO;
  6944. NSDictionary *internal = _dictionary;
  6945. NSEnumerator *keys = [internal keyEnumerator];
  6946. NSNumber *aKey;
  6947. while ((aKey = [keys nextObject])) {
  6948. NSNumber *aValue = internal[aKey];
  6949. block([aKey longLongValue], [aValue unsignedIntValue], &stop);
  6950. if (stop) {
  6951. break;
  6952. }
  6953. }
  6954. }
  6955. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6956. NSDictionary *internal = _dictionary;
  6957. NSUInteger count = internal.count;
  6958. if (count == 0) {
  6959. return 0;
  6960. }
  6961. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6962. GPBDataType keyDataType = field.mapKeyDataType;
  6963. size_t result = 0;
  6964. NSEnumerator *keys = [internal keyEnumerator];
  6965. NSNumber *aKey;
  6966. while ((aKey = [keys nextObject])) {
  6967. NSNumber *aValue = internal[aKey];
  6968. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6969. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6970. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6971. }
  6972. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6973. result += tagSize * count;
  6974. return result;
  6975. }
  6976. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6977. asField:(GPBFieldDescriptor *)field {
  6978. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6979. GPBDataType keyDataType = field.mapKeyDataType;
  6980. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6981. NSDictionary *internal = _dictionary;
  6982. NSEnumerator *keys = [internal keyEnumerator];
  6983. NSNumber *aKey;
  6984. while ((aKey = [keys nextObject])) {
  6985. NSNumber *aValue = internal[aKey];
  6986. [outputStream writeInt32NoTag:tag];
  6987. // Write the size of the message.
  6988. int64_t unwrappedKey = [aKey longLongValue];
  6989. uint32_t unwrappedValue = [aValue unsignedIntValue];
  6990. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6991. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6992. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6993. // Write the fields.
  6994. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6995. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6996. }
  6997. }
  6998. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6999. forGPBGenericValueKey:(GPBGenericValue *)key {
  7000. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  7001. }
  7002. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7003. [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  7004. #pragma unused(stop)
  7005. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  7006. }];
  7007. }
  7008. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
  7009. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7010. if (wrapped && value) {
  7011. *value = [wrapped unsignedIntValue];
  7012. }
  7013. return (wrapped != NULL);
  7014. }
  7015. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  7016. if (otherDictionary) {
  7017. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7018. if (_autocreator) {
  7019. GPBAutocreatedDictionaryModified(_autocreator, self);
  7020. }
  7021. }
  7022. }
  7023. - (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
  7024. [_dictionary setObject:@(value) forKey:@(key)];
  7025. if (_autocreator) {
  7026. GPBAutocreatedDictionaryModified(_autocreator, self);
  7027. }
  7028. }
  7029. - (void)removeUInt32ForKey:(int64_t)aKey {
  7030. [_dictionary removeObjectForKey:@(aKey)];
  7031. }
  7032. - (void)removeAll {
  7033. [_dictionary removeAllObjects];
  7034. }
  7035. @end
  7036. #pragma mark - Int64 -> Int32
  7037. @implementation GPBInt64Int32Dictionary {
  7038. @package
  7039. NSMutableDictionary *_dictionary;
  7040. }
  7041. + (instancetype)dictionary {
  7042. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  7043. }
  7044. + (instancetype)dictionaryWithInt32:(int32_t)value
  7045. forKey:(int64_t)key {
  7046. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  7047. // on to get the type correct.
  7048. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
  7049. forKeys:&key
  7050. count:1] autorelease];
  7051. }
  7052. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  7053. forKeys:(const int64_t [])keys
  7054. count:(NSUInteger)count {
  7055. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  7056. // on to get the type correct.
  7057. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:values
  7058. forKeys:keys
  7059. count:count] autorelease];
  7060. }
  7061. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  7062. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7063. // on to get the type correct.
  7064. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7065. }
  7066. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7067. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7068. }
  7069. - (instancetype)init {
  7070. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7071. }
  7072. - (instancetype)initWithInt32s:(const int32_t [])values
  7073. forKeys:(const int64_t [])keys
  7074. count:(NSUInteger)count {
  7075. self = [super init];
  7076. if (self) {
  7077. _dictionary = [[NSMutableDictionary alloc] init];
  7078. if (count && values && keys) {
  7079. for (NSUInteger i = 0; i < count; ++i) {
  7080. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7081. }
  7082. }
  7083. }
  7084. return self;
  7085. }
  7086. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  7087. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  7088. if (self) {
  7089. if (dictionary) {
  7090. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7091. }
  7092. }
  7093. return self;
  7094. }
  7095. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7096. #pragma unused(numItems)
  7097. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7098. }
  7099. - (void)dealloc {
  7100. NSAssert(!_autocreator,
  7101. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7102. [self class], _autocreator);
  7103. [_dictionary release];
  7104. [super dealloc];
  7105. }
  7106. - (instancetype)copyWithZone:(NSZone *)zone {
  7107. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  7108. }
  7109. - (BOOL)isEqual:(id)other {
  7110. if (self == other) {
  7111. return YES;
  7112. }
  7113. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  7114. return NO;
  7115. }
  7116. GPBInt64Int32Dictionary *otherDictionary = other;
  7117. return [_dictionary isEqual:otherDictionary->_dictionary];
  7118. }
  7119. - (NSUInteger)hash {
  7120. return _dictionary.count;
  7121. }
  7122. - (NSString *)description {
  7123. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7124. }
  7125. - (NSUInteger)count {
  7126. return _dictionary.count;
  7127. }
  7128. - (void)enumerateKeysAndInt32sUsingBlock:
  7129. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7130. BOOL stop = NO;
  7131. NSDictionary *internal = _dictionary;
  7132. NSEnumerator *keys = [internal keyEnumerator];
  7133. NSNumber *aKey;
  7134. while ((aKey = [keys nextObject])) {
  7135. NSNumber *aValue = internal[aKey];
  7136. block([aKey longLongValue], [aValue intValue], &stop);
  7137. if (stop) {
  7138. break;
  7139. }
  7140. }
  7141. }
  7142. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7143. NSDictionary *internal = _dictionary;
  7144. NSUInteger count = internal.count;
  7145. if (count == 0) {
  7146. return 0;
  7147. }
  7148. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7149. GPBDataType keyDataType = field.mapKeyDataType;
  7150. size_t result = 0;
  7151. NSEnumerator *keys = [internal keyEnumerator];
  7152. NSNumber *aKey;
  7153. while ((aKey = [keys nextObject])) {
  7154. NSNumber *aValue = internal[aKey];
  7155. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7156. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7157. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7158. }
  7159. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7160. result += tagSize * count;
  7161. return result;
  7162. }
  7163. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7164. asField:(GPBFieldDescriptor *)field {
  7165. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7166. GPBDataType keyDataType = field.mapKeyDataType;
  7167. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7168. NSDictionary *internal = _dictionary;
  7169. NSEnumerator *keys = [internal keyEnumerator];
  7170. NSNumber *aKey;
  7171. while ((aKey = [keys nextObject])) {
  7172. NSNumber *aValue = internal[aKey];
  7173. [outputStream writeInt32NoTag:tag];
  7174. // Write the size of the message.
  7175. int64_t unwrappedKey = [aKey longLongValue];
  7176. int32_t unwrappedValue = [aValue intValue];
  7177. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7178. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7179. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7180. // Write the fields.
  7181. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7182. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7183. }
  7184. }
  7185. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7186. forGPBGenericValueKey:(GPBGenericValue *)key {
  7187. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  7188. }
  7189. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7190. [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7191. #pragma unused(stop)
  7192. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  7193. }];
  7194. }
  7195. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
  7196. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7197. if (wrapped && value) {
  7198. *value = [wrapped intValue];
  7199. }
  7200. return (wrapped != NULL);
  7201. }
  7202. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  7203. if (otherDictionary) {
  7204. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7205. if (_autocreator) {
  7206. GPBAutocreatedDictionaryModified(_autocreator, self);
  7207. }
  7208. }
  7209. }
  7210. - (void)setInt32:(int32_t)value forKey:(int64_t)key {
  7211. [_dictionary setObject:@(value) forKey:@(key)];
  7212. if (_autocreator) {
  7213. GPBAutocreatedDictionaryModified(_autocreator, self);
  7214. }
  7215. }
  7216. - (void)removeInt32ForKey:(int64_t)aKey {
  7217. [_dictionary removeObjectForKey:@(aKey)];
  7218. }
  7219. - (void)removeAll {
  7220. [_dictionary removeAllObjects];
  7221. }
  7222. @end
  7223. #pragma mark - Int64 -> UInt64
  7224. @implementation GPBInt64UInt64Dictionary {
  7225. @package
  7226. NSMutableDictionary *_dictionary;
  7227. }
  7228. + (instancetype)dictionary {
  7229. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  7230. }
  7231. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  7232. forKey:(int64_t)key {
  7233. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  7234. // on to get the type correct.
  7235. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  7236. forKeys:&key
  7237. count:1] autorelease];
  7238. }
  7239. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  7240. forKeys:(const int64_t [])keys
  7241. count:(NSUInteger)count {
  7242. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  7243. // on to get the type correct.
  7244. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
  7245. forKeys:keys
  7246. count:count] autorelease];
  7247. }
  7248. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  7249. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7250. // on to get the type correct.
  7251. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7252. }
  7253. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7254. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7255. }
  7256. - (instancetype)init {
  7257. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7258. }
  7259. - (instancetype)initWithUInt64s:(const uint64_t [])values
  7260. forKeys:(const int64_t [])keys
  7261. count:(NSUInteger)count {
  7262. self = [super init];
  7263. if (self) {
  7264. _dictionary = [[NSMutableDictionary alloc] init];
  7265. if (count && values && keys) {
  7266. for (NSUInteger i = 0; i < count; ++i) {
  7267. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7268. }
  7269. }
  7270. }
  7271. return self;
  7272. }
  7273. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  7274. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  7275. if (self) {
  7276. if (dictionary) {
  7277. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7278. }
  7279. }
  7280. return self;
  7281. }
  7282. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7283. #pragma unused(numItems)
  7284. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7285. }
  7286. - (void)dealloc {
  7287. NSAssert(!_autocreator,
  7288. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7289. [self class], _autocreator);
  7290. [_dictionary release];
  7291. [super dealloc];
  7292. }
  7293. - (instancetype)copyWithZone:(NSZone *)zone {
  7294. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  7295. }
  7296. - (BOOL)isEqual:(id)other {
  7297. if (self == other) {
  7298. return YES;
  7299. }
  7300. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  7301. return NO;
  7302. }
  7303. GPBInt64UInt64Dictionary *otherDictionary = other;
  7304. return [_dictionary isEqual:otherDictionary->_dictionary];
  7305. }
  7306. - (NSUInteger)hash {
  7307. return _dictionary.count;
  7308. }
  7309. - (NSString *)description {
  7310. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7311. }
  7312. - (NSUInteger)count {
  7313. return _dictionary.count;
  7314. }
  7315. - (void)enumerateKeysAndUInt64sUsingBlock:
  7316. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  7317. BOOL stop = NO;
  7318. NSDictionary *internal = _dictionary;
  7319. NSEnumerator *keys = [internal keyEnumerator];
  7320. NSNumber *aKey;
  7321. while ((aKey = [keys nextObject])) {
  7322. NSNumber *aValue = internal[aKey];
  7323. block([aKey longLongValue], [aValue unsignedLongLongValue], &stop);
  7324. if (stop) {
  7325. break;
  7326. }
  7327. }
  7328. }
  7329. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7330. NSDictionary *internal = _dictionary;
  7331. NSUInteger count = internal.count;
  7332. if (count == 0) {
  7333. return 0;
  7334. }
  7335. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7336. GPBDataType keyDataType = field.mapKeyDataType;
  7337. size_t result = 0;
  7338. NSEnumerator *keys = [internal keyEnumerator];
  7339. NSNumber *aKey;
  7340. while ((aKey = [keys nextObject])) {
  7341. NSNumber *aValue = internal[aKey];
  7342. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7343. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7344. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7345. }
  7346. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7347. result += tagSize * count;
  7348. return result;
  7349. }
  7350. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7351. asField:(GPBFieldDescriptor *)field {
  7352. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7353. GPBDataType keyDataType = field.mapKeyDataType;
  7354. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7355. NSDictionary *internal = _dictionary;
  7356. NSEnumerator *keys = [internal keyEnumerator];
  7357. NSNumber *aKey;
  7358. while ((aKey = [keys nextObject])) {
  7359. NSNumber *aValue = internal[aKey];
  7360. [outputStream writeInt32NoTag:tag];
  7361. // Write the size of the message.
  7362. int64_t unwrappedKey = [aKey longLongValue];
  7363. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  7364. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7365. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7366. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7367. // Write the fields.
  7368. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7369. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7370. }
  7371. }
  7372. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7373. forGPBGenericValueKey:(GPBGenericValue *)key {
  7374. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  7375. }
  7376. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7377. [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  7378. #pragma unused(stop)
  7379. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  7380. }];
  7381. }
  7382. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
  7383. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7384. if (wrapped && value) {
  7385. *value = [wrapped unsignedLongLongValue];
  7386. }
  7387. return (wrapped != NULL);
  7388. }
  7389. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  7390. if (otherDictionary) {
  7391. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7392. if (_autocreator) {
  7393. GPBAutocreatedDictionaryModified(_autocreator, self);
  7394. }
  7395. }
  7396. }
  7397. - (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
  7398. [_dictionary setObject:@(value) forKey:@(key)];
  7399. if (_autocreator) {
  7400. GPBAutocreatedDictionaryModified(_autocreator, self);
  7401. }
  7402. }
  7403. - (void)removeUInt64ForKey:(int64_t)aKey {
  7404. [_dictionary removeObjectForKey:@(aKey)];
  7405. }
  7406. - (void)removeAll {
  7407. [_dictionary removeAllObjects];
  7408. }
  7409. @end
  7410. #pragma mark - Int64 -> Int64
  7411. @implementation GPBInt64Int64Dictionary {
  7412. @package
  7413. NSMutableDictionary *_dictionary;
  7414. }
  7415. + (instancetype)dictionary {
  7416. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  7417. }
  7418. + (instancetype)dictionaryWithInt64:(int64_t)value
  7419. forKey:(int64_t)key {
  7420. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  7421. // on to get the type correct.
  7422. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
  7423. forKeys:&key
  7424. count:1] autorelease];
  7425. }
  7426. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  7427. forKeys:(const int64_t [])keys
  7428. count:(NSUInteger)count {
  7429. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  7430. // on to get the type correct.
  7431. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:values
  7432. forKeys:keys
  7433. count:count] autorelease];
  7434. }
  7435. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7436. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7437. // on to get the type correct.
  7438. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7439. }
  7440. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7441. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7442. }
  7443. - (instancetype)init {
  7444. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7445. }
  7446. - (instancetype)initWithInt64s:(const int64_t [])values
  7447. forKeys:(const int64_t [])keys
  7448. count:(NSUInteger)count {
  7449. self = [super init];
  7450. if (self) {
  7451. _dictionary = [[NSMutableDictionary alloc] init];
  7452. if (count && values && keys) {
  7453. for (NSUInteger i = 0; i < count; ++i) {
  7454. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7455. }
  7456. }
  7457. }
  7458. return self;
  7459. }
  7460. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7461. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  7462. if (self) {
  7463. if (dictionary) {
  7464. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7465. }
  7466. }
  7467. return self;
  7468. }
  7469. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7470. #pragma unused(numItems)
  7471. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7472. }
  7473. - (void)dealloc {
  7474. NSAssert(!_autocreator,
  7475. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7476. [self class], _autocreator);
  7477. [_dictionary release];
  7478. [super dealloc];
  7479. }
  7480. - (instancetype)copyWithZone:(NSZone *)zone {
  7481. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7482. }
  7483. - (BOOL)isEqual:(id)other {
  7484. if (self == other) {
  7485. return YES;
  7486. }
  7487. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7488. return NO;
  7489. }
  7490. GPBInt64Int64Dictionary *otherDictionary = other;
  7491. return [_dictionary isEqual:otherDictionary->_dictionary];
  7492. }
  7493. - (NSUInteger)hash {
  7494. return _dictionary.count;
  7495. }
  7496. - (NSString *)description {
  7497. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7498. }
  7499. - (NSUInteger)count {
  7500. return _dictionary.count;
  7501. }
  7502. - (void)enumerateKeysAndInt64sUsingBlock:
  7503. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7504. BOOL stop = NO;
  7505. NSDictionary *internal = _dictionary;
  7506. NSEnumerator *keys = [internal keyEnumerator];
  7507. NSNumber *aKey;
  7508. while ((aKey = [keys nextObject])) {
  7509. NSNumber *aValue = internal[aKey];
  7510. block([aKey longLongValue], [aValue longLongValue], &stop);
  7511. if (stop) {
  7512. break;
  7513. }
  7514. }
  7515. }
  7516. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7517. NSDictionary *internal = _dictionary;
  7518. NSUInteger count = internal.count;
  7519. if (count == 0) {
  7520. return 0;
  7521. }
  7522. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7523. GPBDataType keyDataType = field.mapKeyDataType;
  7524. size_t result = 0;
  7525. NSEnumerator *keys = [internal keyEnumerator];
  7526. NSNumber *aKey;
  7527. while ((aKey = [keys nextObject])) {
  7528. NSNumber *aValue = internal[aKey];
  7529. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7530. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7531. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7532. }
  7533. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7534. result += tagSize * count;
  7535. return result;
  7536. }
  7537. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7538. asField:(GPBFieldDescriptor *)field {
  7539. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7540. GPBDataType keyDataType = field.mapKeyDataType;
  7541. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7542. NSDictionary *internal = _dictionary;
  7543. NSEnumerator *keys = [internal keyEnumerator];
  7544. NSNumber *aKey;
  7545. while ((aKey = [keys nextObject])) {
  7546. NSNumber *aValue = internal[aKey];
  7547. [outputStream writeInt32NoTag:tag];
  7548. // Write the size of the message.
  7549. int64_t unwrappedKey = [aKey longLongValue];
  7550. int64_t unwrappedValue = [aValue longLongValue];
  7551. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7552. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7553. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7554. // Write the fields.
  7555. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7556. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7557. }
  7558. }
  7559. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7560. forGPBGenericValueKey:(GPBGenericValue *)key {
  7561. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7562. }
  7563. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7564. [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7565. #pragma unused(stop)
  7566. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7567. }];
  7568. }
  7569. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
  7570. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7571. if (wrapped && value) {
  7572. *value = [wrapped longLongValue];
  7573. }
  7574. return (wrapped != NULL);
  7575. }
  7576. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7577. if (otherDictionary) {
  7578. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7579. if (_autocreator) {
  7580. GPBAutocreatedDictionaryModified(_autocreator, self);
  7581. }
  7582. }
  7583. }
  7584. - (void)setInt64:(int64_t)value forKey:(int64_t)key {
  7585. [_dictionary setObject:@(value) forKey:@(key)];
  7586. if (_autocreator) {
  7587. GPBAutocreatedDictionaryModified(_autocreator, self);
  7588. }
  7589. }
  7590. - (void)removeInt64ForKey:(int64_t)aKey {
  7591. [_dictionary removeObjectForKey:@(aKey)];
  7592. }
  7593. - (void)removeAll {
  7594. [_dictionary removeAllObjects];
  7595. }
  7596. @end
  7597. #pragma mark - Int64 -> Bool
  7598. @implementation GPBInt64BoolDictionary {
  7599. @package
  7600. NSMutableDictionary *_dictionary;
  7601. }
  7602. + (instancetype)dictionary {
  7603. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  7604. }
  7605. + (instancetype)dictionaryWithBool:(BOOL)value
  7606. forKey:(int64_t)key {
  7607. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  7608. // on to get the type correct.
  7609. return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:&value
  7610. forKeys:&key
  7611. count:1] autorelease];
  7612. }
  7613. + (instancetype)dictionaryWithBools:(const BOOL [])values
  7614. forKeys:(const int64_t [])keys
  7615. count:(NSUInteger)count {
  7616. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  7617. // on to get the type correct.
  7618. return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:values
  7619. forKeys:keys
  7620. count:count] autorelease];
  7621. }
  7622. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7623. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7624. // on to get the type correct.
  7625. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7626. }
  7627. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7628. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7629. }
  7630. - (instancetype)init {
  7631. return [self initWithBools:NULL forKeys:NULL count:0];
  7632. }
  7633. - (instancetype)initWithBools:(const BOOL [])values
  7634. forKeys:(const int64_t [])keys
  7635. count:(NSUInteger)count {
  7636. self = [super init];
  7637. if (self) {
  7638. _dictionary = [[NSMutableDictionary alloc] init];
  7639. if (count && values && keys) {
  7640. for (NSUInteger i = 0; i < count; ++i) {
  7641. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7642. }
  7643. }
  7644. }
  7645. return self;
  7646. }
  7647. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7648. self = [self initWithBools:NULL forKeys:NULL count:0];
  7649. if (self) {
  7650. if (dictionary) {
  7651. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7652. }
  7653. }
  7654. return self;
  7655. }
  7656. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7657. #pragma unused(numItems)
  7658. return [self initWithBools:NULL forKeys:NULL count:0];
  7659. }
  7660. - (void)dealloc {
  7661. NSAssert(!_autocreator,
  7662. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7663. [self class], _autocreator);
  7664. [_dictionary release];
  7665. [super dealloc];
  7666. }
  7667. - (instancetype)copyWithZone:(NSZone *)zone {
  7668. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7669. }
  7670. - (BOOL)isEqual:(id)other {
  7671. if (self == other) {
  7672. return YES;
  7673. }
  7674. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7675. return NO;
  7676. }
  7677. GPBInt64BoolDictionary *otherDictionary = other;
  7678. return [_dictionary isEqual:otherDictionary->_dictionary];
  7679. }
  7680. - (NSUInteger)hash {
  7681. return _dictionary.count;
  7682. }
  7683. - (NSString *)description {
  7684. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7685. }
  7686. - (NSUInteger)count {
  7687. return _dictionary.count;
  7688. }
  7689. - (void)enumerateKeysAndBoolsUsingBlock:
  7690. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7691. BOOL stop = NO;
  7692. NSDictionary *internal = _dictionary;
  7693. NSEnumerator *keys = [internal keyEnumerator];
  7694. NSNumber *aKey;
  7695. while ((aKey = [keys nextObject])) {
  7696. NSNumber *aValue = internal[aKey];
  7697. block([aKey longLongValue], [aValue boolValue], &stop);
  7698. if (stop) {
  7699. break;
  7700. }
  7701. }
  7702. }
  7703. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7704. NSDictionary *internal = _dictionary;
  7705. NSUInteger count = internal.count;
  7706. if (count == 0) {
  7707. return 0;
  7708. }
  7709. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7710. GPBDataType keyDataType = field.mapKeyDataType;
  7711. size_t result = 0;
  7712. NSEnumerator *keys = [internal keyEnumerator];
  7713. NSNumber *aKey;
  7714. while ((aKey = [keys nextObject])) {
  7715. NSNumber *aValue = internal[aKey];
  7716. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7717. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7718. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7719. }
  7720. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7721. result += tagSize * count;
  7722. return result;
  7723. }
  7724. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7725. asField:(GPBFieldDescriptor *)field {
  7726. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7727. GPBDataType keyDataType = field.mapKeyDataType;
  7728. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7729. NSDictionary *internal = _dictionary;
  7730. NSEnumerator *keys = [internal keyEnumerator];
  7731. NSNumber *aKey;
  7732. while ((aKey = [keys nextObject])) {
  7733. NSNumber *aValue = internal[aKey];
  7734. [outputStream writeInt32NoTag:tag];
  7735. // Write the size of the message.
  7736. int64_t unwrappedKey = [aKey longLongValue];
  7737. BOOL unwrappedValue = [aValue boolValue];
  7738. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7739. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7740. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7741. // Write the fields.
  7742. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7743. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7744. }
  7745. }
  7746. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7747. forGPBGenericValueKey:(GPBGenericValue *)key {
  7748. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7749. }
  7750. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7751. [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7752. #pragma unused(stop)
  7753. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7754. }];
  7755. }
  7756. - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
  7757. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7758. if (wrapped && value) {
  7759. *value = [wrapped boolValue];
  7760. }
  7761. return (wrapped != NULL);
  7762. }
  7763. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7764. if (otherDictionary) {
  7765. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7766. if (_autocreator) {
  7767. GPBAutocreatedDictionaryModified(_autocreator, self);
  7768. }
  7769. }
  7770. }
  7771. - (void)setBool:(BOOL)value forKey:(int64_t)key {
  7772. [_dictionary setObject:@(value) forKey:@(key)];
  7773. if (_autocreator) {
  7774. GPBAutocreatedDictionaryModified(_autocreator, self);
  7775. }
  7776. }
  7777. - (void)removeBoolForKey:(int64_t)aKey {
  7778. [_dictionary removeObjectForKey:@(aKey)];
  7779. }
  7780. - (void)removeAll {
  7781. [_dictionary removeAllObjects];
  7782. }
  7783. @end
  7784. #pragma mark - Int64 -> Float
  7785. @implementation GPBInt64FloatDictionary {
  7786. @package
  7787. NSMutableDictionary *_dictionary;
  7788. }
  7789. + (instancetype)dictionary {
  7790. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  7791. }
  7792. + (instancetype)dictionaryWithFloat:(float)value
  7793. forKey:(int64_t)key {
  7794. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  7795. // on to get the type correct.
  7796. return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:&value
  7797. forKeys:&key
  7798. count:1] autorelease];
  7799. }
  7800. + (instancetype)dictionaryWithFloats:(const float [])values
  7801. forKeys:(const int64_t [])keys
  7802. count:(NSUInteger)count {
  7803. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  7804. // on to get the type correct.
  7805. return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:values
  7806. forKeys:keys
  7807. count:count] autorelease];
  7808. }
  7809. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7810. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7811. // on to get the type correct.
  7812. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7813. }
  7814. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7815. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7816. }
  7817. - (instancetype)init {
  7818. return [self initWithFloats:NULL forKeys:NULL count:0];
  7819. }
  7820. - (instancetype)initWithFloats:(const float [])values
  7821. forKeys:(const int64_t [])keys
  7822. count:(NSUInteger)count {
  7823. self = [super init];
  7824. if (self) {
  7825. _dictionary = [[NSMutableDictionary alloc] init];
  7826. if (count && values && keys) {
  7827. for (NSUInteger i = 0; i < count; ++i) {
  7828. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7829. }
  7830. }
  7831. }
  7832. return self;
  7833. }
  7834. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7835. self = [self initWithFloats:NULL forKeys:NULL count:0];
  7836. if (self) {
  7837. if (dictionary) {
  7838. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7839. }
  7840. }
  7841. return self;
  7842. }
  7843. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7844. #pragma unused(numItems)
  7845. return [self initWithFloats:NULL forKeys:NULL count:0];
  7846. }
  7847. - (void)dealloc {
  7848. NSAssert(!_autocreator,
  7849. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7850. [self class], _autocreator);
  7851. [_dictionary release];
  7852. [super dealloc];
  7853. }
  7854. - (instancetype)copyWithZone:(NSZone *)zone {
  7855. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7856. }
  7857. - (BOOL)isEqual:(id)other {
  7858. if (self == other) {
  7859. return YES;
  7860. }
  7861. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7862. return NO;
  7863. }
  7864. GPBInt64FloatDictionary *otherDictionary = other;
  7865. return [_dictionary isEqual:otherDictionary->_dictionary];
  7866. }
  7867. - (NSUInteger)hash {
  7868. return _dictionary.count;
  7869. }
  7870. - (NSString *)description {
  7871. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7872. }
  7873. - (NSUInteger)count {
  7874. return _dictionary.count;
  7875. }
  7876. - (void)enumerateKeysAndFloatsUsingBlock:
  7877. (void (^)(int64_t key, float value, BOOL *stop))block {
  7878. BOOL stop = NO;
  7879. NSDictionary *internal = _dictionary;
  7880. NSEnumerator *keys = [internal keyEnumerator];
  7881. NSNumber *aKey;
  7882. while ((aKey = [keys nextObject])) {
  7883. NSNumber *aValue = internal[aKey];
  7884. block([aKey longLongValue], [aValue floatValue], &stop);
  7885. if (stop) {
  7886. break;
  7887. }
  7888. }
  7889. }
  7890. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7891. NSDictionary *internal = _dictionary;
  7892. NSUInteger count = internal.count;
  7893. if (count == 0) {
  7894. return 0;
  7895. }
  7896. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7897. GPBDataType keyDataType = field.mapKeyDataType;
  7898. size_t result = 0;
  7899. NSEnumerator *keys = [internal keyEnumerator];
  7900. NSNumber *aKey;
  7901. while ((aKey = [keys nextObject])) {
  7902. NSNumber *aValue = internal[aKey];
  7903. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7904. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7905. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7906. }
  7907. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7908. result += tagSize * count;
  7909. return result;
  7910. }
  7911. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7912. asField:(GPBFieldDescriptor *)field {
  7913. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7914. GPBDataType keyDataType = field.mapKeyDataType;
  7915. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7916. NSDictionary *internal = _dictionary;
  7917. NSEnumerator *keys = [internal keyEnumerator];
  7918. NSNumber *aKey;
  7919. while ((aKey = [keys nextObject])) {
  7920. NSNumber *aValue = internal[aKey];
  7921. [outputStream writeInt32NoTag:tag];
  7922. // Write the size of the message.
  7923. int64_t unwrappedKey = [aKey longLongValue];
  7924. float unwrappedValue = [aValue floatValue];
  7925. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7926. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7927. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7928. // Write the fields.
  7929. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7930. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7931. }
  7932. }
  7933. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7934. forGPBGenericValueKey:(GPBGenericValue *)key {
  7935. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7936. }
  7937. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7938. [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7939. #pragma unused(stop)
  7940. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7941. }];
  7942. }
  7943. - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
  7944. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7945. if (wrapped && value) {
  7946. *value = [wrapped floatValue];
  7947. }
  7948. return (wrapped != NULL);
  7949. }
  7950. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7951. if (otherDictionary) {
  7952. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7953. if (_autocreator) {
  7954. GPBAutocreatedDictionaryModified(_autocreator, self);
  7955. }
  7956. }
  7957. }
  7958. - (void)setFloat:(float)value forKey:(int64_t)key {
  7959. [_dictionary setObject:@(value) forKey:@(key)];
  7960. if (_autocreator) {
  7961. GPBAutocreatedDictionaryModified(_autocreator, self);
  7962. }
  7963. }
  7964. - (void)removeFloatForKey:(int64_t)aKey {
  7965. [_dictionary removeObjectForKey:@(aKey)];
  7966. }
  7967. - (void)removeAll {
  7968. [_dictionary removeAllObjects];
  7969. }
  7970. @end
  7971. #pragma mark - Int64 -> Double
  7972. @implementation GPBInt64DoubleDictionary {
  7973. @package
  7974. NSMutableDictionary *_dictionary;
  7975. }
  7976. + (instancetype)dictionary {
  7977. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  7978. }
  7979. + (instancetype)dictionaryWithDouble:(double)value
  7980. forKey:(int64_t)key {
  7981. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  7982. // on to get the type correct.
  7983. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
  7984. forKeys:&key
  7985. count:1] autorelease];
  7986. }
  7987. + (instancetype)dictionaryWithDoubles:(const double [])values
  7988. forKeys:(const int64_t [])keys
  7989. count:(NSUInteger)count {
  7990. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  7991. // on to get the type correct.
  7992. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:values
  7993. forKeys:keys
  7994. count:count] autorelease];
  7995. }
  7996. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7997. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7998. // on to get the type correct.
  7999. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8000. }
  8001. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8002. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8003. }
  8004. - (instancetype)init {
  8005. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8006. }
  8007. - (instancetype)initWithDoubles:(const double [])values
  8008. forKeys:(const int64_t [])keys
  8009. count:(NSUInteger)count {
  8010. self = [super init];
  8011. if (self) {
  8012. _dictionary = [[NSMutableDictionary alloc] init];
  8013. if (count && values && keys) {
  8014. for (NSUInteger i = 0; i < count; ++i) {
  8015. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  8016. }
  8017. }
  8018. }
  8019. return self;
  8020. }
  8021. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  8022. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  8023. if (self) {
  8024. if (dictionary) {
  8025. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8026. }
  8027. }
  8028. return self;
  8029. }
  8030. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8031. #pragma unused(numItems)
  8032. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8033. }
  8034. - (void)dealloc {
  8035. NSAssert(!_autocreator,
  8036. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8037. [self class], _autocreator);
  8038. [_dictionary release];
  8039. [super dealloc];
  8040. }
  8041. - (instancetype)copyWithZone:(NSZone *)zone {
  8042. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  8043. }
  8044. - (BOOL)isEqual:(id)other {
  8045. if (self == other) {
  8046. return YES;
  8047. }
  8048. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  8049. return NO;
  8050. }
  8051. GPBInt64DoubleDictionary *otherDictionary = other;
  8052. return [_dictionary isEqual:otherDictionary->_dictionary];
  8053. }
  8054. - (NSUInteger)hash {
  8055. return _dictionary.count;
  8056. }
  8057. - (NSString *)description {
  8058. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8059. }
  8060. - (NSUInteger)count {
  8061. return _dictionary.count;
  8062. }
  8063. - (void)enumerateKeysAndDoublesUsingBlock:
  8064. (void (^)(int64_t key, double value, BOOL *stop))block {
  8065. BOOL stop = NO;
  8066. NSDictionary *internal = _dictionary;
  8067. NSEnumerator *keys = [internal keyEnumerator];
  8068. NSNumber *aKey;
  8069. while ((aKey = [keys nextObject])) {
  8070. NSNumber *aValue = internal[aKey];
  8071. block([aKey longLongValue], [aValue doubleValue], &stop);
  8072. if (stop) {
  8073. break;
  8074. }
  8075. }
  8076. }
  8077. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8078. NSDictionary *internal = _dictionary;
  8079. NSUInteger count = internal.count;
  8080. if (count == 0) {
  8081. return 0;
  8082. }
  8083. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8084. GPBDataType keyDataType = field.mapKeyDataType;
  8085. size_t result = 0;
  8086. NSEnumerator *keys = [internal keyEnumerator];
  8087. NSNumber *aKey;
  8088. while ((aKey = [keys nextObject])) {
  8089. NSNumber *aValue = internal[aKey];
  8090. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8091. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  8092. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8093. }
  8094. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8095. result += tagSize * count;
  8096. return result;
  8097. }
  8098. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8099. asField:(GPBFieldDescriptor *)field {
  8100. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8101. GPBDataType keyDataType = field.mapKeyDataType;
  8102. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8103. NSDictionary *internal = _dictionary;
  8104. NSEnumerator *keys = [internal keyEnumerator];
  8105. NSNumber *aKey;
  8106. while ((aKey = [keys nextObject])) {
  8107. NSNumber *aValue = internal[aKey];
  8108. [outputStream writeInt32NoTag:tag];
  8109. // Write the size of the message.
  8110. int64_t unwrappedKey = [aKey longLongValue];
  8111. double unwrappedValue = [aValue doubleValue];
  8112. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8113. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8114. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8115. // Write the fields.
  8116. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8117. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8118. }
  8119. }
  8120. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8121. forGPBGenericValueKey:(GPBGenericValue *)key {
  8122. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  8123. }
  8124. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8125. [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  8126. #pragma unused(stop)
  8127. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  8128. }];
  8129. }
  8130. - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
  8131. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8132. if (wrapped && value) {
  8133. *value = [wrapped doubleValue];
  8134. }
  8135. return (wrapped != NULL);
  8136. }
  8137. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  8138. if (otherDictionary) {
  8139. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8140. if (_autocreator) {
  8141. GPBAutocreatedDictionaryModified(_autocreator, self);
  8142. }
  8143. }
  8144. }
  8145. - (void)setDouble:(double)value forKey:(int64_t)key {
  8146. [_dictionary setObject:@(value) forKey:@(key)];
  8147. if (_autocreator) {
  8148. GPBAutocreatedDictionaryModified(_autocreator, self);
  8149. }
  8150. }
  8151. - (void)removeDoubleForKey:(int64_t)aKey {
  8152. [_dictionary removeObjectForKey:@(aKey)];
  8153. }
  8154. - (void)removeAll {
  8155. [_dictionary removeAllObjects];
  8156. }
  8157. @end
  8158. #pragma mark - Int64 -> Enum
  8159. @implementation GPBInt64EnumDictionary {
  8160. @package
  8161. NSMutableDictionary *_dictionary;
  8162. GPBEnumValidationFunc _validationFunc;
  8163. }
  8164. @synthesize validationFunc = _validationFunc;
  8165. + (instancetype)dictionary {
  8166. return [[[self alloc] initWithValidationFunction:NULL
  8167. rawValues:NULL
  8168. forKeys:NULL
  8169. count:0] autorelease];
  8170. }
  8171. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  8172. return [[[self alloc] initWithValidationFunction:func
  8173. rawValues:NULL
  8174. forKeys:NULL
  8175. count:0] autorelease];
  8176. }
  8177. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  8178. rawValue:(int32_t)rawValue
  8179. forKey:(int64_t)key {
  8180. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8181. // on to get the type correct.
  8182. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  8183. rawValues:&rawValue
  8184. forKeys:&key
  8185. count:1] autorelease];
  8186. }
  8187. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  8188. rawValues:(const int32_t [])rawValues
  8189. forKeys:(const int64_t [])keys
  8190. count:(NSUInteger)count {
  8191. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8192. // on to get the type correct.
  8193. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  8194. rawValues:rawValues
  8195. forKeys:keys
  8196. count:count] autorelease];
  8197. }
  8198. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  8199. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8200. // on to get the type correct.
  8201. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8202. }
  8203. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  8204. capacity:(NSUInteger)numItems {
  8205. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  8206. }
  8207. - (instancetype)init {
  8208. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  8209. }
  8210. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  8211. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8212. }
  8213. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8214. rawValues:(const int32_t [])rawValues
  8215. forKeys:(const int64_t [])keys
  8216. count:(NSUInteger)count {
  8217. self = [super init];
  8218. if (self) {
  8219. _dictionary = [[NSMutableDictionary alloc] init];
  8220. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  8221. if (count && rawValues && keys) {
  8222. for (NSUInteger i = 0; i < count; ++i) {
  8223. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  8224. }
  8225. }
  8226. }
  8227. return self;
  8228. }
  8229. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  8230. self = [self initWithValidationFunction:dictionary.validationFunc
  8231. rawValues:NULL
  8232. forKeys:NULL
  8233. count:0];
  8234. if (self) {
  8235. if (dictionary) {
  8236. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8237. }
  8238. }
  8239. return self;
  8240. }
  8241. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8242. capacity:(NSUInteger)numItems {
  8243. #pragma unused(numItems)
  8244. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8245. }
  8246. - (void)dealloc {
  8247. NSAssert(!_autocreator,
  8248. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8249. [self class], _autocreator);
  8250. [_dictionary release];
  8251. [super dealloc];
  8252. }
  8253. - (instancetype)copyWithZone:(NSZone *)zone {
  8254. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  8255. }
  8256. - (BOOL)isEqual:(id)other {
  8257. if (self == other) {
  8258. return YES;
  8259. }
  8260. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  8261. return NO;
  8262. }
  8263. GPBInt64EnumDictionary *otherDictionary = other;
  8264. return [_dictionary isEqual:otherDictionary->_dictionary];
  8265. }
  8266. - (NSUInteger)hash {
  8267. return _dictionary.count;
  8268. }
  8269. - (NSString *)description {
  8270. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8271. }
  8272. - (NSUInteger)count {
  8273. return _dictionary.count;
  8274. }
  8275. - (void)enumerateKeysAndRawValuesUsingBlock:
  8276. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  8277. BOOL stop = NO;
  8278. NSDictionary *internal = _dictionary;
  8279. NSEnumerator *keys = [internal keyEnumerator];
  8280. NSNumber *aKey;
  8281. while ((aKey = [keys nextObject])) {
  8282. NSNumber *aValue = internal[aKey];
  8283. block([aKey longLongValue], [aValue intValue], &stop);
  8284. if (stop) {
  8285. break;
  8286. }
  8287. }
  8288. }
  8289. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8290. NSDictionary *internal = _dictionary;
  8291. NSUInteger count = internal.count;
  8292. if (count == 0) {
  8293. return 0;
  8294. }
  8295. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8296. GPBDataType keyDataType = field.mapKeyDataType;
  8297. size_t result = 0;
  8298. NSEnumerator *keys = [internal keyEnumerator];
  8299. NSNumber *aKey;
  8300. while ((aKey = [keys nextObject])) {
  8301. NSNumber *aValue = internal[aKey];
  8302. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8303. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8304. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8305. }
  8306. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8307. result += tagSize * count;
  8308. return result;
  8309. }
  8310. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8311. asField:(GPBFieldDescriptor *)field {
  8312. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8313. GPBDataType keyDataType = field.mapKeyDataType;
  8314. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8315. NSDictionary *internal = _dictionary;
  8316. NSEnumerator *keys = [internal keyEnumerator];
  8317. NSNumber *aKey;
  8318. while ((aKey = [keys nextObject])) {
  8319. NSNumber *aValue = internal[aKey];
  8320. [outputStream writeInt32NoTag:tag];
  8321. // Write the size of the message.
  8322. int64_t unwrappedKey = [aKey longLongValue];
  8323. int32_t unwrappedValue = [aValue intValue];
  8324. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8325. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8326. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8327. // Write the fields.
  8328. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8329. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8330. }
  8331. }
  8332. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  8333. forKey:(GPBGenericValue *)key
  8334. keyDataType:(GPBDataType)keyDataType {
  8335. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  8336. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  8337. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  8338. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  8339. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  8340. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  8341. [outputStream release];
  8342. return data;
  8343. }
  8344. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8345. forGPBGenericValueKey:(GPBGenericValue *)key {
  8346. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  8347. }
  8348. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8349. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  8350. #pragma unused(stop)
  8351. block([NSString stringWithFormat:@"%lld", key], @(value));
  8352. }];
  8353. }
  8354. - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
  8355. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8356. if (wrapped && value) {
  8357. int32_t result = [wrapped intValue];
  8358. if (!_validationFunc(result)) {
  8359. result = kGPBUnrecognizedEnumeratorValue;
  8360. }
  8361. *value = result;
  8362. }
  8363. return (wrapped != NULL);
  8364. }
  8365. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
  8366. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8367. if (wrapped && rawValue) {
  8368. *rawValue = [wrapped intValue];
  8369. }
  8370. return (wrapped != NULL);
  8371. }
  8372. - (void)enumerateKeysAndEnumsUsingBlock:
  8373. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  8374. GPBEnumValidationFunc func = _validationFunc;
  8375. BOOL stop = NO;
  8376. NSEnumerator *keys = [_dictionary keyEnumerator];
  8377. NSNumber *aKey;
  8378. while ((aKey = [keys nextObject])) {
  8379. NSNumber *aValue = _dictionary[aKey];
  8380. int32_t unwrapped = [aValue intValue];
  8381. if (!func(unwrapped)) {
  8382. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8383. }
  8384. block([aKey longLongValue], unwrapped, &stop);
  8385. if (stop) {
  8386. break;
  8387. }
  8388. }
  8389. }
  8390. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  8391. if (otherDictionary) {
  8392. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8393. if (_autocreator) {
  8394. GPBAutocreatedDictionaryModified(_autocreator, self);
  8395. }
  8396. }
  8397. }
  8398. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  8399. [_dictionary setObject:@(value) forKey:@(key)];
  8400. if (_autocreator) {
  8401. GPBAutocreatedDictionaryModified(_autocreator, self);
  8402. }
  8403. }
  8404. - (void)removeEnumForKey:(int64_t)aKey {
  8405. [_dictionary removeObjectForKey:@(aKey)];
  8406. }
  8407. - (void)removeAll {
  8408. [_dictionary removeAllObjects];
  8409. }
  8410. - (void)setEnum:(int32_t)value forKey:(int64_t)key {
  8411. if (!_validationFunc(value)) {
  8412. [NSException raise:NSInvalidArgumentException
  8413. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  8414. value];
  8415. }
  8416. [_dictionary setObject:@(value) forKey:@(key)];
  8417. if (_autocreator) {
  8418. GPBAutocreatedDictionaryModified(_autocreator, self);
  8419. }
  8420. }
  8421. @end
  8422. #pragma mark - Int64 -> Object
  8423. @implementation GPBInt64ObjectDictionary {
  8424. @package
  8425. NSMutableDictionary *_dictionary;
  8426. }
  8427. + (instancetype)dictionary {
  8428. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  8429. }
  8430. + (instancetype)dictionaryWithObject:(id)object
  8431. forKey:(int64_t)key {
  8432. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8433. // on to get the type correct.
  8434. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  8435. forKeys:&key
  8436. count:1] autorelease];
  8437. }
  8438. + (instancetype)dictionaryWithObjects:(const id [])objects
  8439. forKeys:(const int64_t [])keys
  8440. count:(NSUInteger)count {
  8441. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8442. // on to get the type correct.
  8443. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  8444. forKeys:keys
  8445. count:count] autorelease];
  8446. }
  8447. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8448. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8449. // on to get the type correct.
  8450. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8451. }
  8452. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8453. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8454. }
  8455. - (instancetype)init {
  8456. return [self initWithObjects:NULL forKeys:NULL count:0];
  8457. }
  8458. - (instancetype)initWithObjects:(const id [])objects
  8459. forKeys:(const int64_t [])keys
  8460. count:(NSUInteger)count {
  8461. self = [super init];
  8462. if (self) {
  8463. _dictionary = [[NSMutableDictionary alloc] init];
  8464. if (count && objects && keys) {
  8465. for (NSUInteger i = 0; i < count; ++i) {
  8466. if (!objects[i]) {
  8467. [NSException raise:NSInvalidArgumentException
  8468. format:@"Attempting to add nil object to a Dictionary"];
  8469. }
  8470. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  8471. }
  8472. }
  8473. }
  8474. return self;
  8475. }
  8476. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8477. self = [self initWithObjects:NULL forKeys:NULL count:0];
  8478. if (self) {
  8479. if (dictionary) {
  8480. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8481. }
  8482. }
  8483. return self;
  8484. }
  8485. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8486. #pragma unused(numItems)
  8487. return [self initWithObjects:NULL forKeys:NULL count:0];
  8488. }
  8489. - (void)dealloc {
  8490. NSAssert(!_autocreator,
  8491. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8492. [self class], _autocreator);
  8493. [_dictionary release];
  8494. [super dealloc];
  8495. }
  8496. - (instancetype)copyWithZone:(NSZone *)zone {
  8497. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8498. }
  8499. - (BOOL)isEqual:(id)other {
  8500. if (self == other) {
  8501. return YES;
  8502. }
  8503. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8504. return NO;
  8505. }
  8506. GPBInt64ObjectDictionary *otherDictionary = other;
  8507. return [_dictionary isEqual:otherDictionary->_dictionary];
  8508. }
  8509. - (NSUInteger)hash {
  8510. return _dictionary.count;
  8511. }
  8512. - (NSString *)description {
  8513. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8514. }
  8515. - (NSUInteger)count {
  8516. return _dictionary.count;
  8517. }
  8518. - (void)enumerateKeysAndObjectsUsingBlock:
  8519. (void (^)(int64_t key, id object, BOOL *stop))block {
  8520. BOOL stop = NO;
  8521. NSDictionary *internal = _dictionary;
  8522. NSEnumerator *keys = [internal keyEnumerator];
  8523. NSNumber *aKey;
  8524. while ((aKey = [keys nextObject])) {
  8525. id aObject = internal[aKey];
  8526. block([aKey longLongValue], aObject, &stop);
  8527. if (stop) {
  8528. break;
  8529. }
  8530. }
  8531. }
  8532. - (BOOL)isInitialized {
  8533. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8534. if (!msg.initialized) {
  8535. return NO;
  8536. }
  8537. }
  8538. return YES;
  8539. }
  8540. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8541. GPBInt64ObjectDictionary *newDict =
  8542. [[GPBInt64ObjectDictionary alloc] init];
  8543. NSEnumerator *keys = [_dictionary keyEnumerator];
  8544. id aKey;
  8545. NSMutableDictionary *internalDict = newDict->_dictionary;
  8546. while ((aKey = [keys nextObject])) {
  8547. GPBMessage *msg = _dictionary[aKey];
  8548. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8549. [internalDict setObject:copiedMsg forKey:aKey];
  8550. [copiedMsg release];
  8551. }
  8552. return newDict;
  8553. }
  8554. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8555. NSDictionary *internal = _dictionary;
  8556. NSUInteger count = internal.count;
  8557. if (count == 0) {
  8558. return 0;
  8559. }
  8560. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8561. GPBDataType keyDataType = field.mapKeyDataType;
  8562. size_t result = 0;
  8563. NSEnumerator *keys = [internal keyEnumerator];
  8564. NSNumber *aKey;
  8565. while ((aKey = [keys nextObject])) {
  8566. id aObject = internal[aKey];
  8567. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8568. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8570. }
  8571. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8572. result += tagSize * count;
  8573. return result;
  8574. }
  8575. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8576. asField:(GPBFieldDescriptor *)field {
  8577. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8578. GPBDataType keyDataType = field.mapKeyDataType;
  8579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8580. NSDictionary *internal = _dictionary;
  8581. NSEnumerator *keys = [internal keyEnumerator];
  8582. NSNumber *aKey;
  8583. while ((aKey = [keys nextObject])) {
  8584. id aObject = internal[aKey];
  8585. [outputStream writeInt32NoTag:tag];
  8586. // Write the size of the message.
  8587. int64_t unwrappedKey = [aKey longLongValue];
  8588. id unwrappedValue = aObject;
  8589. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8590. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8591. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8592. // Write the fields.
  8593. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8594. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8595. }
  8596. }
  8597. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8598. forGPBGenericValueKey:(GPBGenericValue *)key {
  8599. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8600. }
  8601. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8602. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8603. #pragma unused(stop)
  8604. block([NSString stringWithFormat:@"%lld", key], object);
  8605. }];
  8606. }
  8607. - (id)objectForKey:(int64_t)key {
  8608. id result = [_dictionary objectForKey:@(key)];
  8609. return result;
  8610. }
  8611. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8612. if (otherDictionary) {
  8613. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8614. if (_autocreator) {
  8615. GPBAutocreatedDictionaryModified(_autocreator, self);
  8616. }
  8617. }
  8618. }
  8619. - (void)setObject:(id)object forKey:(int64_t)key {
  8620. if (!object) {
  8621. [NSException raise:NSInvalidArgumentException
  8622. format:@"Attempting to add nil object to a Dictionary"];
  8623. }
  8624. [_dictionary setObject:object forKey:@(key)];
  8625. if (_autocreator) {
  8626. GPBAutocreatedDictionaryModified(_autocreator, self);
  8627. }
  8628. }
  8629. - (void)removeObjectForKey:(int64_t)aKey {
  8630. [_dictionary removeObjectForKey:@(aKey)];
  8631. }
  8632. - (void)removeAll {
  8633. [_dictionary removeAllObjects];
  8634. }
  8635. @end
  8636. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8637. // This block of code is generated, do not edit it directly.
  8638. #pragma mark - String -> UInt32
  8639. @implementation GPBStringUInt32Dictionary {
  8640. @package
  8641. NSMutableDictionary *_dictionary;
  8642. }
  8643. + (instancetype)dictionary {
  8644. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  8645. }
  8646. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  8647. forKey:(NSString *)key {
  8648. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  8649. // on to get the type correct.
  8650. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:&value
  8651. forKeys:&key
  8652. count:1] autorelease];
  8653. }
  8654. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  8655. forKeys:(const NSString * [])keys
  8656. count:(NSUInteger)count {
  8657. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  8658. // on to get the type correct.
  8659. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:values
  8660. forKeys:keys
  8661. count:count] autorelease];
  8662. }
  8663. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8664. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8665. // on to get the type correct.
  8666. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8667. }
  8668. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8669. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8670. }
  8671. - (instancetype)init {
  8672. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8673. }
  8674. - (instancetype)initWithUInt32s:(const uint32_t [])values
  8675. forKeys:(const NSString * [])keys
  8676. count:(NSUInteger)count {
  8677. self = [super init];
  8678. if (self) {
  8679. _dictionary = [[NSMutableDictionary alloc] init];
  8680. if (count && values && keys) {
  8681. for (NSUInteger i = 0; i < count; ++i) {
  8682. if (!keys[i]) {
  8683. [NSException raise:NSInvalidArgumentException
  8684. format:@"Attempting to add nil key to a Dictionary"];
  8685. }
  8686. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8687. }
  8688. }
  8689. }
  8690. return self;
  8691. }
  8692. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8693. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  8694. if (self) {
  8695. if (dictionary) {
  8696. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8697. }
  8698. }
  8699. return self;
  8700. }
  8701. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8702. #pragma unused(numItems)
  8703. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8704. }
  8705. - (void)dealloc {
  8706. NSAssert(!_autocreator,
  8707. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8708. [self class], _autocreator);
  8709. [_dictionary release];
  8710. [super dealloc];
  8711. }
  8712. - (instancetype)copyWithZone:(NSZone *)zone {
  8713. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8714. }
  8715. - (BOOL)isEqual:(id)other {
  8716. if (self == other) {
  8717. return YES;
  8718. }
  8719. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8720. return NO;
  8721. }
  8722. GPBStringUInt32Dictionary *otherDictionary = other;
  8723. return [_dictionary isEqual:otherDictionary->_dictionary];
  8724. }
  8725. - (NSUInteger)hash {
  8726. return _dictionary.count;
  8727. }
  8728. - (NSString *)description {
  8729. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8730. }
  8731. - (NSUInteger)count {
  8732. return _dictionary.count;
  8733. }
  8734. - (void)enumerateKeysAndUInt32sUsingBlock:
  8735. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8736. BOOL stop = NO;
  8737. NSDictionary *internal = _dictionary;
  8738. NSEnumerator *keys = [internal keyEnumerator];
  8739. NSString *aKey;
  8740. while ((aKey = [keys nextObject])) {
  8741. NSNumber *aValue = internal[aKey];
  8742. block(aKey, [aValue unsignedIntValue], &stop);
  8743. if (stop) {
  8744. break;
  8745. }
  8746. }
  8747. }
  8748. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8749. NSDictionary *internal = _dictionary;
  8750. NSUInteger count = internal.count;
  8751. if (count == 0) {
  8752. return 0;
  8753. }
  8754. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8755. GPBDataType keyDataType = field.mapKeyDataType;
  8756. size_t result = 0;
  8757. NSEnumerator *keys = [internal keyEnumerator];
  8758. NSString *aKey;
  8759. while ((aKey = [keys nextObject])) {
  8760. NSNumber *aValue = internal[aKey];
  8761. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8762. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8763. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8764. }
  8765. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8766. result += tagSize * count;
  8767. return result;
  8768. }
  8769. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8770. asField:(GPBFieldDescriptor *)field {
  8771. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8772. GPBDataType keyDataType = field.mapKeyDataType;
  8773. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8774. NSDictionary *internal = _dictionary;
  8775. NSEnumerator *keys = [internal keyEnumerator];
  8776. NSString *aKey;
  8777. while ((aKey = [keys nextObject])) {
  8778. NSNumber *aValue = internal[aKey];
  8779. [outputStream writeInt32NoTag:tag];
  8780. // Write the size of the message.
  8781. NSString *unwrappedKey = aKey;
  8782. uint32_t unwrappedValue = [aValue unsignedIntValue];
  8783. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8784. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8785. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8786. // Write the fields.
  8787. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8788. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8789. }
  8790. }
  8791. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8792. forGPBGenericValueKey:(GPBGenericValue *)key {
  8793. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8794. }
  8795. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8796. [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8797. #pragma unused(stop)
  8798. block(key, [NSString stringWithFormat:@"%u", value]);
  8799. }];
  8800. }
  8801. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
  8802. NSNumber *wrapped = [_dictionary objectForKey:key];
  8803. if (wrapped && value) {
  8804. *value = [wrapped unsignedIntValue];
  8805. }
  8806. return (wrapped != NULL);
  8807. }
  8808. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8809. if (otherDictionary) {
  8810. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8811. if (_autocreator) {
  8812. GPBAutocreatedDictionaryModified(_autocreator, self);
  8813. }
  8814. }
  8815. }
  8816. - (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
  8817. if (!key) {
  8818. [NSException raise:NSInvalidArgumentException
  8819. format:@"Attempting to add nil key to a Dictionary"];
  8820. }
  8821. [_dictionary setObject:@(value) forKey:key];
  8822. if (_autocreator) {
  8823. GPBAutocreatedDictionaryModified(_autocreator, self);
  8824. }
  8825. }
  8826. - (void)removeUInt32ForKey:(NSString *)aKey {
  8827. [_dictionary removeObjectForKey:aKey];
  8828. }
  8829. - (void)removeAll {
  8830. [_dictionary removeAllObjects];
  8831. }
  8832. @end
  8833. #pragma mark - String -> Int32
  8834. @implementation GPBStringInt32Dictionary {
  8835. @package
  8836. NSMutableDictionary *_dictionary;
  8837. }
  8838. + (instancetype)dictionary {
  8839. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  8840. }
  8841. + (instancetype)dictionaryWithInt32:(int32_t)value
  8842. forKey:(NSString *)key {
  8843. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  8844. // on to get the type correct.
  8845. return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:&value
  8846. forKeys:&key
  8847. count:1] autorelease];
  8848. }
  8849. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  8850. forKeys:(const NSString * [])keys
  8851. count:(NSUInteger)count {
  8852. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  8853. // on to get the type correct.
  8854. return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:values
  8855. forKeys:keys
  8856. count:count] autorelease];
  8857. }
  8858. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8859. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8860. // on to get the type correct.
  8861. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8862. }
  8863. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8864. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8865. }
  8866. - (instancetype)init {
  8867. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8868. }
  8869. - (instancetype)initWithInt32s:(const int32_t [])values
  8870. forKeys:(const NSString * [])keys
  8871. count:(NSUInteger)count {
  8872. self = [super init];
  8873. if (self) {
  8874. _dictionary = [[NSMutableDictionary alloc] init];
  8875. if (count && values && keys) {
  8876. for (NSUInteger i = 0; i < count; ++i) {
  8877. if (!keys[i]) {
  8878. [NSException raise:NSInvalidArgumentException
  8879. format:@"Attempting to add nil key to a Dictionary"];
  8880. }
  8881. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8882. }
  8883. }
  8884. }
  8885. return self;
  8886. }
  8887. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8888. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  8889. if (self) {
  8890. if (dictionary) {
  8891. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8892. }
  8893. }
  8894. return self;
  8895. }
  8896. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8897. #pragma unused(numItems)
  8898. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8899. }
  8900. - (void)dealloc {
  8901. NSAssert(!_autocreator,
  8902. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8903. [self class], _autocreator);
  8904. [_dictionary release];
  8905. [super dealloc];
  8906. }
  8907. - (instancetype)copyWithZone:(NSZone *)zone {
  8908. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8909. }
  8910. - (BOOL)isEqual:(id)other {
  8911. if (self == other) {
  8912. return YES;
  8913. }
  8914. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8915. return NO;
  8916. }
  8917. GPBStringInt32Dictionary *otherDictionary = other;
  8918. return [_dictionary isEqual:otherDictionary->_dictionary];
  8919. }
  8920. - (NSUInteger)hash {
  8921. return _dictionary.count;
  8922. }
  8923. - (NSString *)description {
  8924. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8925. }
  8926. - (NSUInteger)count {
  8927. return _dictionary.count;
  8928. }
  8929. - (void)enumerateKeysAndInt32sUsingBlock:
  8930. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8931. BOOL stop = NO;
  8932. NSDictionary *internal = _dictionary;
  8933. NSEnumerator *keys = [internal keyEnumerator];
  8934. NSString *aKey;
  8935. while ((aKey = [keys nextObject])) {
  8936. NSNumber *aValue = internal[aKey];
  8937. block(aKey, [aValue intValue], &stop);
  8938. if (stop) {
  8939. break;
  8940. }
  8941. }
  8942. }
  8943. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8944. NSDictionary *internal = _dictionary;
  8945. NSUInteger count = internal.count;
  8946. if (count == 0) {
  8947. return 0;
  8948. }
  8949. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8950. GPBDataType keyDataType = field.mapKeyDataType;
  8951. size_t result = 0;
  8952. NSEnumerator *keys = [internal keyEnumerator];
  8953. NSString *aKey;
  8954. while ((aKey = [keys nextObject])) {
  8955. NSNumber *aValue = internal[aKey];
  8956. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8957. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8958. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8959. }
  8960. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8961. result += tagSize * count;
  8962. return result;
  8963. }
  8964. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8965. asField:(GPBFieldDescriptor *)field {
  8966. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8967. GPBDataType keyDataType = field.mapKeyDataType;
  8968. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8969. NSDictionary *internal = _dictionary;
  8970. NSEnumerator *keys = [internal keyEnumerator];
  8971. NSString *aKey;
  8972. while ((aKey = [keys nextObject])) {
  8973. NSNumber *aValue = internal[aKey];
  8974. [outputStream writeInt32NoTag:tag];
  8975. // Write the size of the message.
  8976. NSString *unwrappedKey = aKey;
  8977. int32_t unwrappedValue = [aValue intValue];
  8978. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8979. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8980. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8981. // Write the fields.
  8982. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8983. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8984. }
  8985. }
  8986. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8987. forGPBGenericValueKey:(GPBGenericValue *)key {
  8988. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8989. }
  8990. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8991. [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8992. #pragma unused(stop)
  8993. block(key, [NSString stringWithFormat:@"%d", value]);
  8994. }];
  8995. }
  8996. - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
  8997. NSNumber *wrapped = [_dictionary objectForKey:key];
  8998. if (wrapped && value) {
  8999. *value = [wrapped intValue];
  9000. }
  9001. return (wrapped != NULL);
  9002. }
  9003. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  9004. if (otherDictionary) {
  9005. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9006. if (_autocreator) {
  9007. GPBAutocreatedDictionaryModified(_autocreator, self);
  9008. }
  9009. }
  9010. }
  9011. - (void)setInt32:(int32_t)value forKey:(NSString *)key {
  9012. if (!key) {
  9013. [NSException raise:NSInvalidArgumentException
  9014. format:@"Attempting to add nil key to a Dictionary"];
  9015. }
  9016. [_dictionary setObject:@(value) forKey:key];
  9017. if (_autocreator) {
  9018. GPBAutocreatedDictionaryModified(_autocreator, self);
  9019. }
  9020. }
  9021. - (void)removeInt32ForKey:(NSString *)aKey {
  9022. [_dictionary removeObjectForKey:aKey];
  9023. }
  9024. - (void)removeAll {
  9025. [_dictionary removeAllObjects];
  9026. }
  9027. @end
  9028. #pragma mark - String -> UInt64
  9029. @implementation GPBStringUInt64Dictionary {
  9030. @package
  9031. NSMutableDictionary *_dictionary;
  9032. }
  9033. + (instancetype)dictionary {
  9034. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  9035. }
  9036. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  9037. forKey:(NSString *)key {
  9038. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  9039. // on to get the type correct.
  9040. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:&value
  9041. forKeys:&key
  9042. count:1] autorelease];
  9043. }
  9044. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  9045. forKeys:(const NSString * [])keys
  9046. count:(NSUInteger)count {
  9047. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  9048. // on to get the type correct.
  9049. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:values
  9050. forKeys:keys
  9051. count:count] autorelease];
  9052. }
  9053. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  9054. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9055. // on to get the type correct.
  9056. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9057. }
  9058. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9059. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9060. }
  9061. - (instancetype)init {
  9062. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9063. }
  9064. - (instancetype)initWithUInt64s:(const uint64_t [])values
  9065. forKeys:(const NSString * [])keys
  9066. count:(NSUInteger)count {
  9067. self = [super init];
  9068. if (self) {
  9069. _dictionary = [[NSMutableDictionary alloc] init];
  9070. if (count && values && keys) {
  9071. for (NSUInteger i = 0; i < count; ++i) {
  9072. if (!keys[i]) {
  9073. [NSException raise:NSInvalidArgumentException
  9074. format:@"Attempting to add nil key to a Dictionary"];
  9075. }
  9076. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9077. }
  9078. }
  9079. }
  9080. return self;
  9081. }
  9082. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  9083. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  9084. if (self) {
  9085. if (dictionary) {
  9086. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9087. }
  9088. }
  9089. return self;
  9090. }
  9091. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9092. #pragma unused(numItems)
  9093. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9094. }
  9095. - (void)dealloc {
  9096. NSAssert(!_autocreator,
  9097. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9098. [self class], _autocreator);
  9099. [_dictionary release];
  9100. [super dealloc];
  9101. }
  9102. - (instancetype)copyWithZone:(NSZone *)zone {
  9103. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9104. }
  9105. - (BOOL)isEqual:(id)other {
  9106. if (self == other) {
  9107. return YES;
  9108. }
  9109. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  9110. return NO;
  9111. }
  9112. GPBStringUInt64Dictionary *otherDictionary = other;
  9113. return [_dictionary isEqual:otherDictionary->_dictionary];
  9114. }
  9115. - (NSUInteger)hash {
  9116. return _dictionary.count;
  9117. }
  9118. - (NSString *)description {
  9119. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9120. }
  9121. - (NSUInteger)count {
  9122. return _dictionary.count;
  9123. }
  9124. - (void)enumerateKeysAndUInt64sUsingBlock:
  9125. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  9126. BOOL stop = NO;
  9127. NSDictionary *internal = _dictionary;
  9128. NSEnumerator *keys = [internal keyEnumerator];
  9129. NSString *aKey;
  9130. while ((aKey = [keys nextObject])) {
  9131. NSNumber *aValue = internal[aKey];
  9132. block(aKey, [aValue unsignedLongLongValue], &stop);
  9133. if (stop) {
  9134. break;
  9135. }
  9136. }
  9137. }
  9138. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9139. NSDictionary *internal = _dictionary;
  9140. NSUInteger count = internal.count;
  9141. if (count == 0) {
  9142. return 0;
  9143. }
  9144. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9145. GPBDataType keyDataType = field.mapKeyDataType;
  9146. size_t result = 0;
  9147. NSEnumerator *keys = [internal keyEnumerator];
  9148. NSString *aKey;
  9149. while ((aKey = [keys nextObject])) {
  9150. NSNumber *aValue = internal[aKey];
  9151. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9152. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  9153. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9154. }
  9155. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9156. result += tagSize * count;
  9157. return result;
  9158. }
  9159. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9160. asField:(GPBFieldDescriptor *)field {
  9161. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9162. GPBDataType keyDataType = field.mapKeyDataType;
  9163. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9164. NSDictionary *internal = _dictionary;
  9165. NSEnumerator *keys = [internal keyEnumerator];
  9166. NSString *aKey;
  9167. while ((aKey = [keys nextObject])) {
  9168. NSNumber *aValue = internal[aKey];
  9169. [outputStream writeInt32NoTag:tag];
  9170. // Write the size of the message.
  9171. NSString *unwrappedKey = aKey;
  9172. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  9173. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9174. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  9175. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9176. // Write the fields.
  9177. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9178. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  9179. }
  9180. }
  9181. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9182. forGPBGenericValueKey:(GPBGenericValue *)key {
  9183. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  9184. }
  9185. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9186. [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  9187. #pragma unused(stop)
  9188. block(key, [NSString stringWithFormat:@"%llu", value]);
  9189. }];
  9190. }
  9191. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
  9192. NSNumber *wrapped = [_dictionary objectForKey:key];
  9193. if (wrapped && value) {
  9194. *value = [wrapped unsignedLongLongValue];
  9195. }
  9196. return (wrapped != NULL);
  9197. }
  9198. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  9199. if (otherDictionary) {
  9200. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9201. if (_autocreator) {
  9202. GPBAutocreatedDictionaryModified(_autocreator, self);
  9203. }
  9204. }
  9205. }
  9206. - (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
  9207. if (!key) {
  9208. [NSException raise:NSInvalidArgumentException
  9209. format:@"Attempting to add nil key to a Dictionary"];
  9210. }
  9211. [_dictionary setObject:@(value) forKey:key];
  9212. if (_autocreator) {
  9213. GPBAutocreatedDictionaryModified(_autocreator, self);
  9214. }
  9215. }
  9216. - (void)removeUInt64ForKey:(NSString *)aKey {
  9217. [_dictionary removeObjectForKey:aKey];
  9218. }
  9219. - (void)removeAll {
  9220. [_dictionary removeAllObjects];
  9221. }
  9222. @end
  9223. #pragma mark - String -> Int64
  9224. @implementation GPBStringInt64Dictionary {
  9225. @package
  9226. NSMutableDictionary *_dictionary;
  9227. }
  9228. + (instancetype)dictionary {
  9229. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  9230. }
  9231. + (instancetype)dictionaryWithInt64:(int64_t)value
  9232. forKey:(NSString *)key {
  9233. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  9234. // on to get the type correct.
  9235. return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:&value
  9236. forKeys:&key
  9237. count:1] autorelease];
  9238. }
  9239. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  9240. forKeys:(const NSString * [])keys
  9241. count:(NSUInteger)count {
  9242. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  9243. // on to get the type correct.
  9244. return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:values
  9245. forKeys:keys
  9246. count:count] autorelease];
  9247. }
  9248. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  9249. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9250. // on to get the type correct.
  9251. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9252. }
  9253. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9254. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9255. }
  9256. - (instancetype)init {
  9257. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9258. }
  9259. - (instancetype)initWithInt64s:(const int64_t [])values
  9260. forKeys:(const NSString * [])keys
  9261. count:(NSUInteger)count {
  9262. self = [super init];
  9263. if (self) {
  9264. _dictionary = [[NSMutableDictionary alloc] init];
  9265. if (count && values && keys) {
  9266. for (NSUInteger i = 0; i < count; ++i) {
  9267. if (!keys[i]) {
  9268. [NSException raise:NSInvalidArgumentException
  9269. format:@"Attempting to add nil key to a Dictionary"];
  9270. }
  9271. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9272. }
  9273. }
  9274. }
  9275. return self;
  9276. }
  9277. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  9278. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  9279. if (self) {
  9280. if (dictionary) {
  9281. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9282. }
  9283. }
  9284. return self;
  9285. }
  9286. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9287. #pragma unused(numItems)
  9288. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9289. }
  9290. - (void)dealloc {
  9291. NSAssert(!_autocreator,
  9292. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9293. [self class], _autocreator);
  9294. [_dictionary release];
  9295. [super dealloc];
  9296. }
  9297. - (instancetype)copyWithZone:(NSZone *)zone {
  9298. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9299. }
  9300. - (BOOL)isEqual:(id)other {
  9301. if (self == other) {
  9302. return YES;
  9303. }
  9304. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  9305. return NO;
  9306. }
  9307. GPBStringInt64Dictionary *otherDictionary = other;
  9308. return [_dictionary isEqual:otherDictionary->_dictionary];
  9309. }
  9310. - (NSUInteger)hash {
  9311. return _dictionary.count;
  9312. }
  9313. - (NSString *)description {
  9314. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9315. }
  9316. - (NSUInteger)count {
  9317. return _dictionary.count;
  9318. }
  9319. - (void)enumerateKeysAndInt64sUsingBlock:
  9320. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  9321. BOOL stop = NO;
  9322. NSDictionary *internal = _dictionary;
  9323. NSEnumerator *keys = [internal keyEnumerator];
  9324. NSString *aKey;
  9325. while ((aKey = [keys nextObject])) {
  9326. NSNumber *aValue = internal[aKey];
  9327. block(aKey, [aValue longLongValue], &stop);
  9328. if (stop) {
  9329. break;
  9330. }
  9331. }
  9332. }
  9333. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9334. NSDictionary *internal = _dictionary;
  9335. NSUInteger count = internal.count;
  9336. if (count == 0) {
  9337. return 0;
  9338. }
  9339. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9340. GPBDataType keyDataType = field.mapKeyDataType;
  9341. size_t result = 0;
  9342. NSEnumerator *keys = [internal keyEnumerator];
  9343. NSString *aKey;
  9344. while ((aKey = [keys nextObject])) {
  9345. NSNumber *aValue = internal[aKey];
  9346. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9347. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  9348. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9349. }
  9350. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9351. result += tagSize * count;
  9352. return result;
  9353. }
  9354. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9355. asField:(GPBFieldDescriptor *)field {
  9356. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9357. GPBDataType keyDataType = field.mapKeyDataType;
  9358. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9359. NSDictionary *internal = _dictionary;
  9360. NSEnumerator *keys = [internal keyEnumerator];
  9361. NSString *aKey;
  9362. while ((aKey = [keys nextObject])) {
  9363. NSNumber *aValue = internal[aKey];
  9364. [outputStream writeInt32NoTag:tag];
  9365. // Write the size of the message.
  9366. NSString *unwrappedKey = aKey;
  9367. int64_t unwrappedValue = [aValue longLongValue];
  9368. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9369. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  9370. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9371. // Write the fields.
  9372. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9373. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  9374. }
  9375. }
  9376. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9377. forGPBGenericValueKey:(GPBGenericValue *)key {
  9378. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  9379. }
  9380. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9381. [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  9382. #pragma unused(stop)
  9383. block(key, [NSString stringWithFormat:@"%lld", value]);
  9384. }];
  9385. }
  9386. - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
  9387. NSNumber *wrapped = [_dictionary objectForKey:key];
  9388. if (wrapped && value) {
  9389. *value = [wrapped longLongValue];
  9390. }
  9391. return (wrapped != NULL);
  9392. }
  9393. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  9394. if (otherDictionary) {
  9395. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9396. if (_autocreator) {
  9397. GPBAutocreatedDictionaryModified(_autocreator, self);
  9398. }
  9399. }
  9400. }
  9401. - (void)setInt64:(int64_t)value forKey:(NSString *)key {
  9402. if (!key) {
  9403. [NSException raise:NSInvalidArgumentException
  9404. format:@"Attempting to add nil key to a Dictionary"];
  9405. }
  9406. [_dictionary setObject:@(value) forKey:key];
  9407. if (_autocreator) {
  9408. GPBAutocreatedDictionaryModified(_autocreator, self);
  9409. }
  9410. }
  9411. - (void)removeInt64ForKey:(NSString *)aKey {
  9412. [_dictionary removeObjectForKey:aKey];
  9413. }
  9414. - (void)removeAll {
  9415. [_dictionary removeAllObjects];
  9416. }
  9417. @end
  9418. #pragma mark - String -> Bool
  9419. @implementation GPBStringBoolDictionary {
  9420. @package
  9421. NSMutableDictionary *_dictionary;
  9422. }
  9423. + (instancetype)dictionary {
  9424. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  9425. }
  9426. + (instancetype)dictionaryWithBool:(BOOL)value
  9427. forKey:(NSString *)key {
  9428. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  9429. // on to get the type correct.
  9430. return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:&value
  9431. forKeys:&key
  9432. count:1] autorelease];
  9433. }
  9434. + (instancetype)dictionaryWithBools:(const BOOL [])values
  9435. forKeys:(const NSString * [])keys
  9436. count:(NSUInteger)count {
  9437. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  9438. // on to get the type correct.
  9439. return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:values
  9440. forKeys:keys
  9441. count:count] autorelease];
  9442. }
  9443. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9444. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9445. // on to get the type correct.
  9446. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9447. }
  9448. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9449. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9450. }
  9451. - (instancetype)init {
  9452. return [self initWithBools:NULL forKeys:NULL count:0];
  9453. }
  9454. - (instancetype)initWithBools:(const BOOL [])values
  9455. forKeys:(const NSString * [])keys
  9456. count:(NSUInteger)count {
  9457. self = [super init];
  9458. if (self) {
  9459. _dictionary = [[NSMutableDictionary alloc] init];
  9460. if (count && values && keys) {
  9461. for (NSUInteger i = 0; i < count; ++i) {
  9462. if (!keys[i]) {
  9463. [NSException raise:NSInvalidArgumentException
  9464. format:@"Attempting to add nil key to a Dictionary"];
  9465. }
  9466. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9467. }
  9468. }
  9469. }
  9470. return self;
  9471. }
  9472. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9473. self = [self initWithBools:NULL forKeys:NULL count:0];
  9474. if (self) {
  9475. if (dictionary) {
  9476. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9477. }
  9478. }
  9479. return self;
  9480. }
  9481. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9482. #pragma unused(numItems)
  9483. return [self initWithBools:NULL forKeys:NULL count:0];
  9484. }
  9485. - (void)dealloc {
  9486. NSAssert(!_autocreator,
  9487. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9488. [self class], _autocreator);
  9489. [_dictionary release];
  9490. [super dealloc];
  9491. }
  9492. - (instancetype)copyWithZone:(NSZone *)zone {
  9493. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9494. }
  9495. - (BOOL)isEqual:(id)other {
  9496. if (self == other) {
  9497. return YES;
  9498. }
  9499. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  9500. return NO;
  9501. }
  9502. GPBStringBoolDictionary *otherDictionary = other;
  9503. return [_dictionary isEqual:otherDictionary->_dictionary];
  9504. }
  9505. - (NSUInteger)hash {
  9506. return _dictionary.count;
  9507. }
  9508. - (NSString *)description {
  9509. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9510. }
  9511. - (NSUInteger)count {
  9512. return _dictionary.count;
  9513. }
  9514. - (void)enumerateKeysAndBoolsUsingBlock:
  9515. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  9516. BOOL stop = NO;
  9517. NSDictionary *internal = _dictionary;
  9518. NSEnumerator *keys = [internal keyEnumerator];
  9519. NSString *aKey;
  9520. while ((aKey = [keys nextObject])) {
  9521. NSNumber *aValue = internal[aKey];
  9522. block(aKey, [aValue boolValue], &stop);
  9523. if (stop) {
  9524. break;
  9525. }
  9526. }
  9527. }
  9528. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9529. NSDictionary *internal = _dictionary;
  9530. NSUInteger count = internal.count;
  9531. if (count == 0) {
  9532. return 0;
  9533. }
  9534. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9535. GPBDataType keyDataType = field.mapKeyDataType;
  9536. size_t result = 0;
  9537. NSEnumerator *keys = [internal keyEnumerator];
  9538. NSString *aKey;
  9539. while ((aKey = [keys nextObject])) {
  9540. NSNumber *aValue = internal[aKey];
  9541. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9542. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9543. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9544. }
  9545. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9546. result += tagSize * count;
  9547. return result;
  9548. }
  9549. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9550. asField:(GPBFieldDescriptor *)field {
  9551. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9552. GPBDataType keyDataType = field.mapKeyDataType;
  9553. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9554. NSDictionary *internal = _dictionary;
  9555. NSEnumerator *keys = [internal keyEnumerator];
  9556. NSString *aKey;
  9557. while ((aKey = [keys nextObject])) {
  9558. NSNumber *aValue = internal[aKey];
  9559. [outputStream writeInt32NoTag:tag];
  9560. // Write the size of the message.
  9561. NSString *unwrappedKey = aKey;
  9562. BOOL unwrappedValue = [aValue boolValue];
  9563. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9564. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  9565. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9566. // Write the fields.
  9567. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9568. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  9569. }
  9570. }
  9571. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9572. forGPBGenericValueKey:(GPBGenericValue *)key {
  9573. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  9574. }
  9575. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9576. [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  9577. #pragma unused(stop)
  9578. block(key, (value ? @"true" : @"false"));
  9579. }];
  9580. }
  9581. - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
  9582. NSNumber *wrapped = [_dictionary objectForKey:key];
  9583. if (wrapped && value) {
  9584. *value = [wrapped boolValue];
  9585. }
  9586. return (wrapped != NULL);
  9587. }
  9588. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9589. if (otherDictionary) {
  9590. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9591. if (_autocreator) {
  9592. GPBAutocreatedDictionaryModified(_autocreator, self);
  9593. }
  9594. }
  9595. }
  9596. - (void)setBool:(BOOL)value forKey:(NSString *)key {
  9597. if (!key) {
  9598. [NSException raise:NSInvalidArgumentException
  9599. format:@"Attempting to add nil key to a Dictionary"];
  9600. }
  9601. [_dictionary setObject:@(value) forKey:key];
  9602. if (_autocreator) {
  9603. GPBAutocreatedDictionaryModified(_autocreator, self);
  9604. }
  9605. }
  9606. - (void)removeBoolForKey:(NSString *)aKey {
  9607. [_dictionary removeObjectForKey:aKey];
  9608. }
  9609. - (void)removeAll {
  9610. [_dictionary removeAllObjects];
  9611. }
  9612. @end
  9613. #pragma mark - String -> Float
  9614. @implementation GPBStringFloatDictionary {
  9615. @package
  9616. NSMutableDictionary *_dictionary;
  9617. }
  9618. + (instancetype)dictionary {
  9619. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  9620. }
  9621. + (instancetype)dictionaryWithFloat:(float)value
  9622. forKey:(NSString *)key {
  9623. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  9624. // on to get the type correct.
  9625. return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:&value
  9626. forKeys:&key
  9627. count:1] autorelease];
  9628. }
  9629. + (instancetype)dictionaryWithFloats:(const float [])values
  9630. forKeys:(const NSString * [])keys
  9631. count:(NSUInteger)count {
  9632. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  9633. // on to get the type correct.
  9634. return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:values
  9635. forKeys:keys
  9636. count:count] autorelease];
  9637. }
  9638. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9639. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9640. // on to get the type correct.
  9641. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9642. }
  9643. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9644. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9645. }
  9646. - (instancetype)init {
  9647. return [self initWithFloats:NULL forKeys:NULL count:0];
  9648. }
  9649. - (instancetype)initWithFloats:(const float [])values
  9650. forKeys:(const NSString * [])keys
  9651. count:(NSUInteger)count {
  9652. self = [super init];
  9653. if (self) {
  9654. _dictionary = [[NSMutableDictionary alloc] init];
  9655. if (count && values && keys) {
  9656. for (NSUInteger i = 0; i < count; ++i) {
  9657. if (!keys[i]) {
  9658. [NSException raise:NSInvalidArgumentException
  9659. format:@"Attempting to add nil key to a Dictionary"];
  9660. }
  9661. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9662. }
  9663. }
  9664. }
  9665. return self;
  9666. }
  9667. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9668. self = [self initWithFloats:NULL forKeys:NULL count:0];
  9669. if (self) {
  9670. if (dictionary) {
  9671. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9672. }
  9673. }
  9674. return self;
  9675. }
  9676. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9677. #pragma unused(numItems)
  9678. return [self initWithFloats:NULL forKeys:NULL count:0];
  9679. }
  9680. - (void)dealloc {
  9681. NSAssert(!_autocreator,
  9682. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9683. [self class], _autocreator);
  9684. [_dictionary release];
  9685. [super dealloc];
  9686. }
  9687. - (instancetype)copyWithZone:(NSZone *)zone {
  9688. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9689. }
  9690. - (BOOL)isEqual:(id)other {
  9691. if (self == other) {
  9692. return YES;
  9693. }
  9694. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9695. return NO;
  9696. }
  9697. GPBStringFloatDictionary *otherDictionary = other;
  9698. return [_dictionary isEqual:otherDictionary->_dictionary];
  9699. }
  9700. - (NSUInteger)hash {
  9701. return _dictionary.count;
  9702. }
  9703. - (NSString *)description {
  9704. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9705. }
  9706. - (NSUInteger)count {
  9707. return _dictionary.count;
  9708. }
  9709. - (void)enumerateKeysAndFloatsUsingBlock:
  9710. (void (^)(NSString *key, float value, BOOL *stop))block {
  9711. BOOL stop = NO;
  9712. NSDictionary *internal = _dictionary;
  9713. NSEnumerator *keys = [internal keyEnumerator];
  9714. NSString *aKey;
  9715. while ((aKey = [keys nextObject])) {
  9716. NSNumber *aValue = internal[aKey];
  9717. block(aKey, [aValue floatValue], &stop);
  9718. if (stop) {
  9719. break;
  9720. }
  9721. }
  9722. }
  9723. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9724. NSDictionary *internal = _dictionary;
  9725. NSUInteger count = internal.count;
  9726. if (count == 0) {
  9727. return 0;
  9728. }
  9729. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9730. GPBDataType keyDataType = field.mapKeyDataType;
  9731. size_t result = 0;
  9732. NSEnumerator *keys = [internal keyEnumerator];
  9733. NSString *aKey;
  9734. while ((aKey = [keys nextObject])) {
  9735. NSNumber *aValue = internal[aKey];
  9736. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9737. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9738. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9739. }
  9740. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9741. result += tagSize * count;
  9742. return result;
  9743. }
  9744. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9745. asField:(GPBFieldDescriptor *)field {
  9746. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9747. GPBDataType keyDataType = field.mapKeyDataType;
  9748. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9749. NSDictionary *internal = _dictionary;
  9750. NSEnumerator *keys = [internal keyEnumerator];
  9751. NSString *aKey;
  9752. while ((aKey = [keys nextObject])) {
  9753. NSNumber *aValue = internal[aKey];
  9754. [outputStream writeInt32NoTag:tag];
  9755. // Write the size of the message.
  9756. NSString *unwrappedKey = aKey;
  9757. float unwrappedValue = [aValue floatValue];
  9758. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9759. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  9760. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9761. // Write the fields.
  9762. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9763. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  9764. }
  9765. }
  9766. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9767. forGPBGenericValueKey:(GPBGenericValue *)key {
  9768. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9769. }
  9770. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9771. [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9772. #pragma unused(stop)
  9773. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9774. }];
  9775. }
  9776. - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
  9777. NSNumber *wrapped = [_dictionary objectForKey:key];
  9778. if (wrapped && value) {
  9779. *value = [wrapped floatValue];
  9780. }
  9781. return (wrapped != NULL);
  9782. }
  9783. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9784. if (otherDictionary) {
  9785. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9786. if (_autocreator) {
  9787. GPBAutocreatedDictionaryModified(_autocreator, self);
  9788. }
  9789. }
  9790. }
  9791. - (void)setFloat:(float)value forKey:(NSString *)key {
  9792. if (!key) {
  9793. [NSException raise:NSInvalidArgumentException
  9794. format:@"Attempting to add nil key to a Dictionary"];
  9795. }
  9796. [_dictionary setObject:@(value) forKey:key];
  9797. if (_autocreator) {
  9798. GPBAutocreatedDictionaryModified(_autocreator, self);
  9799. }
  9800. }
  9801. - (void)removeFloatForKey:(NSString *)aKey {
  9802. [_dictionary removeObjectForKey:aKey];
  9803. }
  9804. - (void)removeAll {
  9805. [_dictionary removeAllObjects];
  9806. }
  9807. @end
  9808. #pragma mark - String -> Double
  9809. @implementation GPBStringDoubleDictionary {
  9810. @package
  9811. NSMutableDictionary *_dictionary;
  9812. }
  9813. + (instancetype)dictionary {
  9814. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  9815. }
  9816. + (instancetype)dictionaryWithDouble:(double)value
  9817. forKey:(NSString *)key {
  9818. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  9819. // on to get the type correct.
  9820. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:&value
  9821. forKeys:&key
  9822. count:1] autorelease];
  9823. }
  9824. + (instancetype)dictionaryWithDoubles:(const double [])values
  9825. forKeys:(const NSString * [])keys
  9826. count:(NSUInteger)count {
  9827. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  9828. // on to get the type correct.
  9829. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:values
  9830. forKeys:keys
  9831. count:count] autorelease];
  9832. }
  9833. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9834. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9835. // on to get the type correct.
  9836. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9837. }
  9838. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9839. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9840. }
  9841. - (instancetype)init {
  9842. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9843. }
  9844. - (instancetype)initWithDoubles:(const double [])values
  9845. forKeys:(const NSString * [])keys
  9846. count:(NSUInteger)count {
  9847. self = [super init];
  9848. if (self) {
  9849. _dictionary = [[NSMutableDictionary alloc] init];
  9850. if (count && values && keys) {
  9851. for (NSUInteger i = 0; i < count; ++i) {
  9852. if (!keys[i]) {
  9853. [NSException raise:NSInvalidArgumentException
  9854. format:@"Attempting to add nil key to a Dictionary"];
  9855. }
  9856. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9857. }
  9858. }
  9859. }
  9860. return self;
  9861. }
  9862. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9863. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  9864. if (self) {
  9865. if (dictionary) {
  9866. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9867. }
  9868. }
  9869. return self;
  9870. }
  9871. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9872. #pragma unused(numItems)
  9873. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9874. }
  9875. - (void)dealloc {
  9876. NSAssert(!_autocreator,
  9877. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9878. [self class], _autocreator);
  9879. [_dictionary release];
  9880. [super dealloc];
  9881. }
  9882. - (instancetype)copyWithZone:(NSZone *)zone {
  9883. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9884. }
  9885. - (BOOL)isEqual:(id)other {
  9886. if (self == other) {
  9887. return YES;
  9888. }
  9889. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9890. return NO;
  9891. }
  9892. GPBStringDoubleDictionary *otherDictionary = other;
  9893. return [_dictionary isEqual:otherDictionary->_dictionary];
  9894. }
  9895. - (NSUInteger)hash {
  9896. return _dictionary.count;
  9897. }
  9898. - (NSString *)description {
  9899. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9900. }
  9901. - (NSUInteger)count {
  9902. return _dictionary.count;
  9903. }
  9904. - (void)enumerateKeysAndDoublesUsingBlock:
  9905. (void (^)(NSString *key, double value, BOOL *stop))block {
  9906. BOOL stop = NO;
  9907. NSDictionary *internal = _dictionary;
  9908. NSEnumerator *keys = [internal keyEnumerator];
  9909. NSString *aKey;
  9910. while ((aKey = [keys nextObject])) {
  9911. NSNumber *aValue = internal[aKey];
  9912. block(aKey, [aValue doubleValue], &stop);
  9913. if (stop) {
  9914. break;
  9915. }
  9916. }
  9917. }
  9918. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9919. NSDictionary *internal = _dictionary;
  9920. NSUInteger count = internal.count;
  9921. if (count == 0) {
  9922. return 0;
  9923. }
  9924. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9925. GPBDataType keyDataType = field.mapKeyDataType;
  9926. size_t result = 0;
  9927. NSEnumerator *keys = [internal keyEnumerator];
  9928. NSString *aKey;
  9929. while ((aKey = [keys nextObject])) {
  9930. NSNumber *aValue = internal[aKey];
  9931. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9932. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9933. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9934. }
  9935. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9936. result += tagSize * count;
  9937. return result;
  9938. }
  9939. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9940. asField:(GPBFieldDescriptor *)field {
  9941. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9942. GPBDataType keyDataType = field.mapKeyDataType;
  9943. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9944. NSDictionary *internal = _dictionary;
  9945. NSEnumerator *keys = [internal keyEnumerator];
  9946. NSString *aKey;
  9947. while ((aKey = [keys nextObject])) {
  9948. NSNumber *aValue = internal[aKey];
  9949. [outputStream writeInt32NoTag:tag];
  9950. // Write the size of the message.
  9951. NSString *unwrappedKey = aKey;
  9952. double unwrappedValue = [aValue doubleValue];
  9953. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9954. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  9955. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9956. // Write the fields.
  9957. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  9958. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  9959. }
  9960. }
  9961. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9962. forGPBGenericValueKey:(GPBGenericValue *)key {
  9963. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9964. }
  9965. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9966. [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9967. #pragma unused(stop)
  9968. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9969. }];
  9970. }
  9971. - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
  9972. NSNumber *wrapped = [_dictionary objectForKey:key];
  9973. if (wrapped && value) {
  9974. *value = [wrapped doubleValue];
  9975. }
  9976. return (wrapped != NULL);
  9977. }
  9978. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9979. if (otherDictionary) {
  9980. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9981. if (_autocreator) {
  9982. GPBAutocreatedDictionaryModified(_autocreator, self);
  9983. }
  9984. }
  9985. }
  9986. - (void)setDouble:(double)value forKey:(NSString *)key {
  9987. if (!key) {
  9988. [NSException raise:NSInvalidArgumentException
  9989. format:@"Attempting to add nil key to a Dictionary"];
  9990. }
  9991. [_dictionary setObject:@(value) forKey:key];
  9992. if (_autocreator) {
  9993. GPBAutocreatedDictionaryModified(_autocreator, self);
  9994. }
  9995. }
  9996. - (void)removeDoubleForKey:(NSString *)aKey {
  9997. [_dictionary removeObjectForKey:aKey];
  9998. }
  9999. - (void)removeAll {
  10000. [_dictionary removeAllObjects];
  10001. }
  10002. @end
  10003. #pragma mark - String -> Enum
  10004. @implementation GPBStringEnumDictionary {
  10005. @package
  10006. NSMutableDictionary *_dictionary;
  10007. GPBEnumValidationFunc _validationFunc;
  10008. }
  10009. @synthesize validationFunc = _validationFunc;
  10010. + (instancetype)dictionary {
  10011. return [[[self alloc] initWithValidationFunction:NULL
  10012. rawValues:NULL
  10013. forKeys:NULL
  10014. count:0] autorelease];
  10015. }
  10016. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  10017. return [[[self alloc] initWithValidationFunction:func
  10018. rawValues:NULL
  10019. forKeys:NULL
  10020. count:0] autorelease];
  10021. }
  10022. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  10023. rawValue:(int32_t)rawValue
  10024. forKey:(NSString *)key {
  10025. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10026. // on to get the type correct.
  10027. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  10028. rawValues:&rawValue
  10029. forKeys:&key
  10030. count:1] autorelease];
  10031. }
  10032. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  10033. rawValues:(const int32_t [])rawValues
  10034. forKeys:(const NSString * [])keys
  10035. count:(NSUInteger)count {
  10036. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10037. // on to get the type correct.
  10038. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  10039. rawValues:rawValues
  10040. forKeys:keys
  10041. count:count] autorelease];
  10042. }
  10043. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  10044. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10045. // on to get the type correct.
  10046. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10047. }
  10048. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  10049. capacity:(NSUInteger)numItems {
  10050. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  10051. }
  10052. - (instancetype)init {
  10053. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  10054. }
  10055. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  10056. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10057. }
  10058. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10059. rawValues:(const int32_t [])rawValues
  10060. forKeys:(const NSString * [])keys
  10061. count:(NSUInteger)count {
  10062. self = [super init];
  10063. if (self) {
  10064. _dictionary = [[NSMutableDictionary alloc] init];
  10065. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  10066. if (count && rawValues && keys) {
  10067. for (NSUInteger i = 0; i < count; ++i) {
  10068. if (!keys[i]) {
  10069. [NSException raise:NSInvalidArgumentException
  10070. format:@"Attempting to add nil key to a Dictionary"];
  10071. }
  10072. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  10073. }
  10074. }
  10075. }
  10076. return self;
  10077. }
  10078. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  10079. self = [self initWithValidationFunction:dictionary.validationFunc
  10080. rawValues:NULL
  10081. forKeys:NULL
  10082. count:0];
  10083. if (self) {
  10084. if (dictionary) {
  10085. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  10086. }
  10087. }
  10088. return self;
  10089. }
  10090. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10091. capacity:(NSUInteger)numItems {
  10092. #pragma unused(numItems)
  10093. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10094. }
  10095. - (void)dealloc {
  10096. NSAssert(!_autocreator,
  10097. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10098. [self class], _autocreator);
  10099. [_dictionary release];
  10100. [super dealloc];
  10101. }
  10102. - (instancetype)copyWithZone:(NSZone *)zone {
  10103. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  10104. }
  10105. - (BOOL)isEqual:(id)other {
  10106. if (self == other) {
  10107. return YES;
  10108. }
  10109. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  10110. return NO;
  10111. }
  10112. GPBStringEnumDictionary *otherDictionary = other;
  10113. return [_dictionary isEqual:otherDictionary->_dictionary];
  10114. }
  10115. - (NSUInteger)hash {
  10116. return _dictionary.count;
  10117. }
  10118. - (NSString *)description {
  10119. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  10120. }
  10121. - (NSUInteger)count {
  10122. return _dictionary.count;
  10123. }
  10124. - (void)enumerateKeysAndRawValuesUsingBlock:
  10125. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  10126. BOOL stop = NO;
  10127. NSDictionary *internal = _dictionary;
  10128. NSEnumerator *keys = [internal keyEnumerator];
  10129. NSString *aKey;
  10130. while ((aKey = [keys nextObject])) {
  10131. NSNumber *aValue = internal[aKey];
  10132. block(aKey, [aValue intValue], &stop);
  10133. if (stop) {
  10134. break;
  10135. }
  10136. }
  10137. }
  10138. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10139. NSDictionary *internal = _dictionary;
  10140. NSUInteger count = internal.count;
  10141. if (count == 0) {
  10142. return 0;
  10143. }
  10144. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10145. GPBDataType keyDataType = field.mapKeyDataType;
  10146. size_t result = 0;
  10147. NSEnumerator *keys = [internal keyEnumerator];
  10148. NSString *aKey;
  10149. while ((aKey = [keys nextObject])) {
  10150. NSNumber *aValue = internal[aKey];
  10151. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  10152. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  10153. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10154. }
  10155. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10156. result += tagSize * count;
  10157. return result;
  10158. }
  10159. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10160. asField:(GPBFieldDescriptor *)field {
  10161. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10162. GPBDataType keyDataType = field.mapKeyDataType;
  10163. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10164. NSDictionary *internal = _dictionary;
  10165. NSEnumerator *keys = [internal keyEnumerator];
  10166. NSString *aKey;
  10167. while ((aKey = [keys nextObject])) {
  10168. NSNumber *aValue = internal[aKey];
  10169. [outputStream writeInt32NoTag:tag];
  10170. // Write the size of the message.
  10171. NSString *unwrappedKey = aKey;
  10172. int32_t unwrappedValue = [aValue intValue];
  10173. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  10174. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  10175. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10176. // Write the fields.
  10177. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  10178. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  10179. }
  10180. }
  10181. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  10182. forKey:(GPBGenericValue *)key
  10183. keyDataType:(GPBDataType)keyDataType {
  10184. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  10185. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  10186. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  10187. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  10188. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  10189. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  10190. [outputStream release];
  10191. return data;
  10192. }
  10193. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10194. forGPBGenericValueKey:(GPBGenericValue *)key {
  10195. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  10196. }
  10197. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10198. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  10199. #pragma unused(stop)
  10200. block(key, @(value));
  10201. }];
  10202. }
  10203. - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
  10204. NSNumber *wrapped = [_dictionary objectForKey:key];
  10205. if (wrapped && value) {
  10206. int32_t result = [wrapped intValue];
  10207. if (!_validationFunc(result)) {
  10208. result = kGPBUnrecognizedEnumeratorValue;
  10209. }
  10210. *value = result;
  10211. }
  10212. return (wrapped != NULL);
  10213. }
  10214. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
  10215. NSNumber *wrapped = [_dictionary objectForKey:key];
  10216. if (wrapped && rawValue) {
  10217. *rawValue = [wrapped intValue];
  10218. }
  10219. return (wrapped != NULL);
  10220. }
  10221. - (void)enumerateKeysAndEnumsUsingBlock:
  10222. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  10223. GPBEnumValidationFunc func = _validationFunc;
  10224. BOOL stop = NO;
  10225. NSEnumerator *keys = [_dictionary keyEnumerator];
  10226. NSString *aKey;
  10227. while ((aKey = [keys nextObject])) {
  10228. NSNumber *aValue = _dictionary[aKey];
  10229. int32_t unwrapped = [aValue intValue];
  10230. if (!func(unwrapped)) {
  10231. unwrapped = kGPBUnrecognizedEnumeratorValue;
  10232. }
  10233. block(aKey, unwrapped, &stop);
  10234. if (stop) {
  10235. break;
  10236. }
  10237. }
  10238. }
  10239. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  10240. if (otherDictionary) {
  10241. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  10242. if (_autocreator) {
  10243. GPBAutocreatedDictionaryModified(_autocreator, self);
  10244. }
  10245. }
  10246. }
  10247. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  10248. if (!key) {
  10249. [NSException raise:NSInvalidArgumentException
  10250. format:@"Attempting to add nil key to a Dictionary"];
  10251. }
  10252. [_dictionary setObject:@(value) forKey:key];
  10253. if (_autocreator) {
  10254. GPBAutocreatedDictionaryModified(_autocreator, self);
  10255. }
  10256. }
  10257. - (void)removeEnumForKey:(NSString *)aKey {
  10258. [_dictionary removeObjectForKey:aKey];
  10259. }
  10260. - (void)removeAll {
  10261. [_dictionary removeAllObjects];
  10262. }
  10263. - (void)setEnum:(int32_t)value forKey:(NSString *)key {
  10264. if (!key) {
  10265. [NSException raise:NSInvalidArgumentException
  10266. format:@"Attempting to add nil key to a Dictionary"];
  10267. }
  10268. if (!_validationFunc(value)) {
  10269. [NSException raise:NSInvalidArgumentException
  10270. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  10271. value];
  10272. }
  10273. [_dictionary setObject:@(value) forKey:key];
  10274. if (_autocreator) {
  10275. GPBAutocreatedDictionaryModified(_autocreator, self);
  10276. }
  10277. }
  10278. @end
  10279. //%PDDM-EXPAND-END (5 expansions)
  10280. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  10281. // This block of code is generated, do not edit it directly.
  10282. #pragma mark - Bool -> UInt32
  10283. @implementation GPBBoolUInt32Dictionary {
  10284. @package
  10285. uint32_t _values[2];
  10286. BOOL _valueSet[2];
  10287. }
  10288. + (instancetype)dictionary {
  10289. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  10290. }
  10291. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  10292. forKey:(BOOL)key {
  10293. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  10294. // on to get the type correct.
  10295. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:&value
  10296. forKeys:&key
  10297. count:1] autorelease];
  10298. }
  10299. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  10300. forKeys:(const BOOL [])keys
  10301. count:(NSUInteger)count {
  10302. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  10303. // on to get the type correct.
  10304. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:values
  10305. forKeys:keys
  10306. count:count] autorelease];
  10307. }
  10308. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  10309. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10310. // on to get the type correct.
  10311. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10312. }
  10313. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10314. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10315. }
  10316. - (instancetype)init {
  10317. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  10318. }
  10319. - (instancetype)initWithUInt32s:(const uint32_t [])values
  10320. forKeys:(const BOOL [])keys
  10321. count:(NSUInteger)count {
  10322. self = [super init];
  10323. if (self) {
  10324. for (NSUInteger i = 0; i < count; ++i) {
  10325. int idx = keys[i] ? 1 : 0;
  10326. _values[idx] = values[i];
  10327. _valueSet[idx] = YES;
  10328. }
  10329. }
  10330. return self;
  10331. }
  10332. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  10333. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  10334. if (self) {
  10335. if (dictionary) {
  10336. for (int i = 0; i < 2; ++i) {
  10337. if (dictionary->_valueSet[i]) {
  10338. _values[i] = dictionary->_values[i];
  10339. _valueSet[i] = YES;
  10340. }
  10341. }
  10342. }
  10343. }
  10344. return self;
  10345. }
  10346. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10347. #pragma unused(numItems)
  10348. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  10349. }
  10350. #if !defined(NS_BLOCK_ASSERTIONS)
  10351. - (void)dealloc {
  10352. NSAssert(!_autocreator,
  10353. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10354. [self class], _autocreator);
  10355. [super dealloc];
  10356. }
  10357. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10358. - (instancetype)copyWithZone:(NSZone *)zone {
  10359. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10360. }
  10361. - (BOOL)isEqual:(id)other {
  10362. if (self == other) {
  10363. return YES;
  10364. }
  10365. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  10366. return NO;
  10367. }
  10368. GPBBoolUInt32Dictionary *otherDictionary = other;
  10369. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10370. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10371. return NO;
  10372. }
  10373. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10374. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10375. return NO;
  10376. }
  10377. return YES;
  10378. }
  10379. - (NSUInteger)hash {
  10380. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10381. }
  10382. - (NSString *)description {
  10383. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10384. if (_valueSet[0]) {
  10385. [result appendFormat:@"NO: %u", _values[0]];
  10386. }
  10387. if (_valueSet[1]) {
  10388. [result appendFormat:@"YES: %u", _values[1]];
  10389. }
  10390. [result appendString:@" }"];
  10391. return result;
  10392. }
  10393. - (NSUInteger)count {
  10394. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10395. }
  10396. - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
  10397. int idx = (key ? 1 : 0);
  10398. if (_valueSet[idx]) {
  10399. if (value) {
  10400. *value = _values[idx];
  10401. }
  10402. return YES;
  10403. }
  10404. return NO;
  10405. }
  10406. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10407. forGPBGenericValueKey:(GPBGenericValue *)key {
  10408. int idx = (key->valueBool ? 1 : 0);
  10409. _values[idx] = value->valueUInt32;
  10410. _valueSet[idx] = YES;
  10411. }
  10412. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10413. if (_valueSet[0]) {
  10414. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  10415. }
  10416. if (_valueSet[1]) {
  10417. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  10418. }
  10419. }
  10420. - (void)enumerateKeysAndUInt32sUsingBlock:
  10421. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  10422. BOOL stop = NO;
  10423. if (_valueSet[0]) {
  10424. block(NO, _values[0], &stop);
  10425. }
  10426. if (!stop && _valueSet[1]) {
  10427. block(YES, _values[1], &stop);
  10428. }
  10429. }
  10430. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10431. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10432. NSUInteger count = 0;
  10433. size_t result = 0;
  10434. for (int i = 0; i < 2; ++i) {
  10435. if (_valueSet[i]) {
  10436. ++count;
  10437. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10438. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10439. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10440. }
  10441. }
  10442. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10443. result += tagSize * count;
  10444. return result;
  10445. }
  10446. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10447. asField:(GPBFieldDescriptor *)field {
  10448. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10449. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10450. for (int i = 0; i < 2; ++i) {
  10451. if (_valueSet[i]) {
  10452. // Write the tag.
  10453. [outputStream writeInt32NoTag:tag];
  10454. // Write the size of the message.
  10455. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10456. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10457. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10458. // Write the fields.
  10459. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10460. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10461. }
  10462. }
  10463. }
  10464. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  10465. if (otherDictionary) {
  10466. for (int i = 0; i < 2; ++i) {
  10467. if (otherDictionary->_valueSet[i]) {
  10468. _valueSet[i] = YES;
  10469. _values[i] = otherDictionary->_values[i];
  10470. }
  10471. }
  10472. if (_autocreator) {
  10473. GPBAutocreatedDictionaryModified(_autocreator, self);
  10474. }
  10475. }
  10476. }
  10477. - (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
  10478. int idx = (key ? 1 : 0);
  10479. _values[idx] = value;
  10480. _valueSet[idx] = YES;
  10481. if (_autocreator) {
  10482. GPBAutocreatedDictionaryModified(_autocreator, self);
  10483. }
  10484. }
  10485. - (void)removeUInt32ForKey:(BOOL)aKey {
  10486. _valueSet[aKey ? 1 : 0] = NO;
  10487. }
  10488. - (void)removeAll {
  10489. _valueSet[0] = NO;
  10490. _valueSet[1] = NO;
  10491. }
  10492. @end
  10493. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  10494. // This block of code is generated, do not edit it directly.
  10495. #pragma mark - Bool -> Int32
  10496. @implementation GPBBoolInt32Dictionary {
  10497. @package
  10498. int32_t _values[2];
  10499. BOOL _valueSet[2];
  10500. }
  10501. + (instancetype)dictionary {
  10502. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  10503. }
  10504. + (instancetype)dictionaryWithInt32:(int32_t)value
  10505. forKey:(BOOL)key {
  10506. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  10507. // on to get the type correct.
  10508. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:&value
  10509. forKeys:&key
  10510. count:1] autorelease];
  10511. }
  10512. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  10513. forKeys:(const BOOL [])keys
  10514. count:(NSUInteger)count {
  10515. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  10516. // on to get the type correct.
  10517. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:values
  10518. forKeys:keys
  10519. count:count] autorelease];
  10520. }
  10521. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10522. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10523. // on to get the type correct.
  10524. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10525. }
  10526. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10527. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10528. }
  10529. - (instancetype)init {
  10530. return [self initWithInt32s:NULL forKeys:NULL count:0];
  10531. }
  10532. - (instancetype)initWithInt32s:(const int32_t [])values
  10533. forKeys:(const BOOL [])keys
  10534. count:(NSUInteger)count {
  10535. self = [super init];
  10536. if (self) {
  10537. for (NSUInteger i = 0; i < count; ++i) {
  10538. int idx = keys[i] ? 1 : 0;
  10539. _values[idx] = values[i];
  10540. _valueSet[idx] = YES;
  10541. }
  10542. }
  10543. return self;
  10544. }
  10545. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10546. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  10547. if (self) {
  10548. if (dictionary) {
  10549. for (int i = 0; i < 2; ++i) {
  10550. if (dictionary->_valueSet[i]) {
  10551. _values[i] = dictionary->_values[i];
  10552. _valueSet[i] = YES;
  10553. }
  10554. }
  10555. }
  10556. }
  10557. return self;
  10558. }
  10559. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10560. #pragma unused(numItems)
  10561. return [self initWithInt32s:NULL forKeys:NULL count:0];
  10562. }
  10563. #if !defined(NS_BLOCK_ASSERTIONS)
  10564. - (void)dealloc {
  10565. NSAssert(!_autocreator,
  10566. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10567. [self class], _autocreator);
  10568. [super dealloc];
  10569. }
  10570. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10571. - (instancetype)copyWithZone:(NSZone *)zone {
  10572. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10573. }
  10574. - (BOOL)isEqual:(id)other {
  10575. if (self == other) {
  10576. return YES;
  10577. }
  10578. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  10579. return NO;
  10580. }
  10581. GPBBoolInt32Dictionary *otherDictionary = other;
  10582. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10583. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10584. return NO;
  10585. }
  10586. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10587. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10588. return NO;
  10589. }
  10590. return YES;
  10591. }
  10592. - (NSUInteger)hash {
  10593. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10594. }
  10595. - (NSString *)description {
  10596. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10597. if (_valueSet[0]) {
  10598. [result appendFormat:@"NO: %d", _values[0]];
  10599. }
  10600. if (_valueSet[1]) {
  10601. [result appendFormat:@"YES: %d", _values[1]];
  10602. }
  10603. [result appendString:@" }"];
  10604. return result;
  10605. }
  10606. - (NSUInteger)count {
  10607. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10608. }
  10609. - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
  10610. int idx = (key ? 1 : 0);
  10611. if (_valueSet[idx]) {
  10612. if (value) {
  10613. *value = _values[idx];
  10614. }
  10615. return YES;
  10616. }
  10617. return NO;
  10618. }
  10619. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10620. forGPBGenericValueKey:(GPBGenericValue *)key {
  10621. int idx = (key->valueBool ? 1 : 0);
  10622. _values[idx] = value->valueInt32;
  10623. _valueSet[idx] = YES;
  10624. }
  10625. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10626. if (_valueSet[0]) {
  10627. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  10628. }
  10629. if (_valueSet[1]) {
  10630. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  10631. }
  10632. }
  10633. - (void)enumerateKeysAndInt32sUsingBlock:
  10634. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10635. BOOL stop = NO;
  10636. if (_valueSet[0]) {
  10637. block(NO, _values[0], &stop);
  10638. }
  10639. if (!stop && _valueSet[1]) {
  10640. block(YES, _values[1], &stop);
  10641. }
  10642. }
  10643. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10644. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10645. NSUInteger count = 0;
  10646. size_t result = 0;
  10647. for (int i = 0; i < 2; ++i) {
  10648. if (_valueSet[i]) {
  10649. ++count;
  10650. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10651. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10652. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10653. }
  10654. }
  10655. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10656. result += tagSize * count;
  10657. return result;
  10658. }
  10659. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10660. asField:(GPBFieldDescriptor *)field {
  10661. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10662. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10663. for (int i = 0; i < 2; ++i) {
  10664. if (_valueSet[i]) {
  10665. // Write the tag.
  10666. [outputStream writeInt32NoTag:tag];
  10667. // Write the size of the message.
  10668. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10669. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10670. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10671. // Write the fields.
  10672. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10673. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10674. }
  10675. }
  10676. }
  10677. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10678. if (otherDictionary) {
  10679. for (int i = 0; i < 2; ++i) {
  10680. if (otherDictionary->_valueSet[i]) {
  10681. _valueSet[i] = YES;
  10682. _values[i] = otherDictionary->_values[i];
  10683. }
  10684. }
  10685. if (_autocreator) {
  10686. GPBAutocreatedDictionaryModified(_autocreator, self);
  10687. }
  10688. }
  10689. }
  10690. - (void)setInt32:(int32_t)value forKey:(BOOL)key {
  10691. int idx = (key ? 1 : 0);
  10692. _values[idx] = value;
  10693. _valueSet[idx] = YES;
  10694. if (_autocreator) {
  10695. GPBAutocreatedDictionaryModified(_autocreator, self);
  10696. }
  10697. }
  10698. - (void)removeInt32ForKey:(BOOL)aKey {
  10699. _valueSet[aKey ? 1 : 0] = NO;
  10700. }
  10701. - (void)removeAll {
  10702. _valueSet[0] = NO;
  10703. _valueSet[1] = NO;
  10704. }
  10705. @end
  10706. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10707. // This block of code is generated, do not edit it directly.
  10708. #pragma mark - Bool -> UInt64
  10709. @implementation GPBBoolUInt64Dictionary {
  10710. @package
  10711. uint64_t _values[2];
  10712. BOOL _valueSet[2];
  10713. }
  10714. + (instancetype)dictionary {
  10715. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  10716. }
  10717. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  10718. forKey:(BOOL)key {
  10719. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  10720. // on to get the type correct.
  10721. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:&value
  10722. forKeys:&key
  10723. count:1] autorelease];
  10724. }
  10725. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  10726. forKeys:(const BOOL [])keys
  10727. count:(NSUInteger)count {
  10728. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  10729. // on to get the type correct.
  10730. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:values
  10731. forKeys:keys
  10732. count:count] autorelease];
  10733. }
  10734. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10735. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10736. // on to get the type correct.
  10737. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10738. }
  10739. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10740. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10741. }
  10742. - (instancetype)init {
  10743. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  10744. }
  10745. - (instancetype)initWithUInt64s:(const uint64_t [])values
  10746. forKeys:(const BOOL [])keys
  10747. count:(NSUInteger)count {
  10748. self = [super init];
  10749. if (self) {
  10750. for (NSUInteger i = 0; i < count; ++i) {
  10751. int idx = keys[i] ? 1 : 0;
  10752. _values[idx] = values[i];
  10753. _valueSet[idx] = YES;
  10754. }
  10755. }
  10756. return self;
  10757. }
  10758. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10759. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  10760. if (self) {
  10761. if (dictionary) {
  10762. for (int i = 0; i < 2; ++i) {
  10763. if (dictionary->_valueSet[i]) {
  10764. _values[i] = dictionary->_values[i];
  10765. _valueSet[i] = YES;
  10766. }
  10767. }
  10768. }
  10769. }
  10770. return self;
  10771. }
  10772. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10773. #pragma unused(numItems)
  10774. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  10775. }
  10776. #if !defined(NS_BLOCK_ASSERTIONS)
  10777. - (void)dealloc {
  10778. NSAssert(!_autocreator,
  10779. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10780. [self class], _autocreator);
  10781. [super dealloc];
  10782. }
  10783. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10784. - (instancetype)copyWithZone:(NSZone *)zone {
  10785. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10786. }
  10787. - (BOOL)isEqual:(id)other {
  10788. if (self == other) {
  10789. return YES;
  10790. }
  10791. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10792. return NO;
  10793. }
  10794. GPBBoolUInt64Dictionary *otherDictionary = other;
  10795. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10796. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10797. return NO;
  10798. }
  10799. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10800. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10801. return NO;
  10802. }
  10803. return YES;
  10804. }
  10805. - (NSUInteger)hash {
  10806. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10807. }
  10808. - (NSString *)description {
  10809. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10810. if (_valueSet[0]) {
  10811. [result appendFormat:@"NO: %llu", _values[0]];
  10812. }
  10813. if (_valueSet[1]) {
  10814. [result appendFormat:@"YES: %llu", _values[1]];
  10815. }
  10816. [result appendString:@" }"];
  10817. return result;
  10818. }
  10819. - (NSUInteger)count {
  10820. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10821. }
  10822. - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
  10823. int idx = (key ? 1 : 0);
  10824. if (_valueSet[idx]) {
  10825. if (value) {
  10826. *value = _values[idx];
  10827. }
  10828. return YES;
  10829. }
  10830. return NO;
  10831. }
  10832. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10833. forGPBGenericValueKey:(GPBGenericValue *)key {
  10834. int idx = (key->valueBool ? 1 : 0);
  10835. _values[idx] = value->valueUInt64;
  10836. _valueSet[idx] = YES;
  10837. }
  10838. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10839. if (_valueSet[0]) {
  10840. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10841. }
  10842. if (_valueSet[1]) {
  10843. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10844. }
  10845. }
  10846. - (void)enumerateKeysAndUInt64sUsingBlock:
  10847. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10848. BOOL stop = NO;
  10849. if (_valueSet[0]) {
  10850. block(NO, _values[0], &stop);
  10851. }
  10852. if (!stop && _valueSet[1]) {
  10853. block(YES, _values[1], &stop);
  10854. }
  10855. }
  10856. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10857. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10858. NSUInteger count = 0;
  10859. size_t result = 0;
  10860. for (int i = 0; i < 2; ++i) {
  10861. if (_valueSet[i]) {
  10862. ++count;
  10863. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10864. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10865. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10866. }
  10867. }
  10868. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10869. result += tagSize * count;
  10870. return result;
  10871. }
  10872. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10873. asField:(GPBFieldDescriptor *)field {
  10874. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10875. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10876. for (int i = 0; i < 2; ++i) {
  10877. if (_valueSet[i]) {
  10878. // Write the tag.
  10879. [outputStream writeInt32NoTag:tag];
  10880. // Write the size of the message.
  10881. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10882. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10883. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10884. // Write the fields.
  10885. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10886. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10887. }
  10888. }
  10889. }
  10890. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10891. if (otherDictionary) {
  10892. for (int i = 0; i < 2; ++i) {
  10893. if (otherDictionary->_valueSet[i]) {
  10894. _valueSet[i] = YES;
  10895. _values[i] = otherDictionary->_values[i];
  10896. }
  10897. }
  10898. if (_autocreator) {
  10899. GPBAutocreatedDictionaryModified(_autocreator, self);
  10900. }
  10901. }
  10902. }
  10903. - (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
  10904. int idx = (key ? 1 : 0);
  10905. _values[idx] = value;
  10906. _valueSet[idx] = YES;
  10907. if (_autocreator) {
  10908. GPBAutocreatedDictionaryModified(_autocreator, self);
  10909. }
  10910. }
  10911. - (void)removeUInt64ForKey:(BOOL)aKey {
  10912. _valueSet[aKey ? 1 : 0] = NO;
  10913. }
  10914. - (void)removeAll {
  10915. _valueSet[0] = NO;
  10916. _valueSet[1] = NO;
  10917. }
  10918. @end
  10919. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10920. // This block of code is generated, do not edit it directly.
  10921. #pragma mark - Bool -> Int64
  10922. @implementation GPBBoolInt64Dictionary {
  10923. @package
  10924. int64_t _values[2];
  10925. BOOL _valueSet[2];
  10926. }
  10927. + (instancetype)dictionary {
  10928. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  10929. }
  10930. + (instancetype)dictionaryWithInt64:(int64_t)value
  10931. forKey:(BOOL)key {
  10932. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  10933. // on to get the type correct.
  10934. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:&value
  10935. forKeys:&key
  10936. count:1] autorelease];
  10937. }
  10938. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  10939. forKeys:(const BOOL [])keys
  10940. count:(NSUInteger)count {
  10941. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  10942. // on to get the type correct.
  10943. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:values
  10944. forKeys:keys
  10945. count:count] autorelease];
  10946. }
  10947. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10948. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10949. // on to get the type correct.
  10950. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10951. }
  10952. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10953. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10954. }
  10955. - (instancetype)init {
  10956. return [self initWithInt64s:NULL forKeys:NULL count:0];
  10957. }
  10958. - (instancetype)initWithInt64s:(const int64_t [])values
  10959. forKeys:(const BOOL [])keys
  10960. count:(NSUInteger)count {
  10961. self = [super init];
  10962. if (self) {
  10963. for (NSUInteger i = 0; i < count; ++i) {
  10964. int idx = keys[i] ? 1 : 0;
  10965. _values[idx] = values[i];
  10966. _valueSet[idx] = YES;
  10967. }
  10968. }
  10969. return self;
  10970. }
  10971. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10972. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  10973. if (self) {
  10974. if (dictionary) {
  10975. for (int i = 0; i < 2; ++i) {
  10976. if (dictionary->_valueSet[i]) {
  10977. _values[i] = dictionary->_values[i];
  10978. _valueSet[i] = YES;
  10979. }
  10980. }
  10981. }
  10982. }
  10983. return self;
  10984. }
  10985. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10986. #pragma unused(numItems)
  10987. return [self initWithInt64s:NULL forKeys:NULL count:0];
  10988. }
  10989. #if !defined(NS_BLOCK_ASSERTIONS)
  10990. - (void)dealloc {
  10991. NSAssert(!_autocreator,
  10992. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10993. [self class], _autocreator);
  10994. [super dealloc];
  10995. }
  10996. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10997. - (instancetype)copyWithZone:(NSZone *)zone {
  10998. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10999. }
  11000. - (BOOL)isEqual:(id)other {
  11001. if (self == other) {
  11002. return YES;
  11003. }
  11004. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  11005. return NO;
  11006. }
  11007. GPBBoolInt64Dictionary *otherDictionary = other;
  11008. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11009. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11010. return NO;
  11011. }
  11012. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11013. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11014. return NO;
  11015. }
  11016. return YES;
  11017. }
  11018. - (NSUInteger)hash {
  11019. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11020. }
  11021. - (NSString *)description {
  11022. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11023. if (_valueSet[0]) {
  11024. [result appendFormat:@"NO: %lld", _values[0]];
  11025. }
  11026. if (_valueSet[1]) {
  11027. [result appendFormat:@"YES: %lld", _values[1]];
  11028. }
  11029. [result appendString:@" }"];
  11030. return result;
  11031. }
  11032. - (NSUInteger)count {
  11033. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11034. }
  11035. - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
  11036. int idx = (key ? 1 : 0);
  11037. if (_valueSet[idx]) {
  11038. if (value) {
  11039. *value = _values[idx];
  11040. }
  11041. return YES;
  11042. }
  11043. return NO;
  11044. }
  11045. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11046. forGPBGenericValueKey:(GPBGenericValue *)key {
  11047. int idx = (key->valueBool ? 1 : 0);
  11048. _values[idx] = value->valueInt64;
  11049. _valueSet[idx] = YES;
  11050. }
  11051. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11052. if (_valueSet[0]) {
  11053. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  11054. }
  11055. if (_valueSet[1]) {
  11056. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  11057. }
  11058. }
  11059. - (void)enumerateKeysAndInt64sUsingBlock:
  11060. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  11061. BOOL stop = NO;
  11062. if (_valueSet[0]) {
  11063. block(NO, _values[0], &stop);
  11064. }
  11065. if (!stop && _valueSet[1]) {
  11066. block(YES, _values[1], &stop);
  11067. }
  11068. }
  11069. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11070. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11071. NSUInteger count = 0;
  11072. size_t result = 0;
  11073. for (int i = 0; i < 2; ++i) {
  11074. if (_valueSet[i]) {
  11075. ++count;
  11076. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11077. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11078. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11079. }
  11080. }
  11081. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11082. result += tagSize * count;
  11083. return result;
  11084. }
  11085. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11086. asField:(GPBFieldDescriptor *)field {
  11087. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11088. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11089. for (int i = 0; i < 2; ++i) {
  11090. if (_valueSet[i]) {
  11091. // Write the tag.
  11092. [outputStream writeInt32NoTag:tag];
  11093. // Write the size of the message.
  11094. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11095. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11096. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11097. // Write the fields.
  11098. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11099. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11100. }
  11101. }
  11102. }
  11103. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  11104. if (otherDictionary) {
  11105. for (int i = 0; i < 2; ++i) {
  11106. if (otherDictionary->_valueSet[i]) {
  11107. _valueSet[i] = YES;
  11108. _values[i] = otherDictionary->_values[i];
  11109. }
  11110. }
  11111. if (_autocreator) {
  11112. GPBAutocreatedDictionaryModified(_autocreator, self);
  11113. }
  11114. }
  11115. }
  11116. - (void)setInt64:(int64_t)value forKey:(BOOL)key {
  11117. int idx = (key ? 1 : 0);
  11118. _values[idx] = value;
  11119. _valueSet[idx] = YES;
  11120. if (_autocreator) {
  11121. GPBAutocreatedDictionaryModified(_autocreator, self);
  11122. }
  11123. }
  11124. - (void)removeInt64ForKey:(BOOL)aKey {
  11125. _valueSet[aKey ? 1 : 0] = NO;
  11126. }
  11127. - (void)removeAll {
  11128. _valueSet[0] = NO;
  11129. _valueSet[1] = NO;
  11130. }
  11131. @end
  11132. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  11133. // This block of code is generated, do not edit it directly.
  11134. #pragma mark - Bool -> Bool
  11135. @implementation GPBBoolBoolDictionary {
  11136. @package
  11137. BOOL _values[2];
  11138. BOOL _valueSet[2];
  11139. }
  11140. + (instancetype)dictionary {
  11141. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  11142. }
  11143. + (instancetype)dictionaryWithBool:(BOOL)value
  11144. forKey:(BOOL)key {
  11145. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  11146. // on to get the type correct.
  11147. return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:&value
  11148. forKeys:&key
  11149. count:1] autorelease];
  11150. }
  11151. + (instancetype)dictionaryWithBools:(const BOOL [])values
  11152. forKeys:(const BOOL [])keys
  11153. count:(NSUInteger)count {
  11154. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  11155. // on to get the type correct.
  11156. return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:values
  11157. forKeys:keys
  11158. count:count] autorelease];
  11159. }
  11160. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  11161. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11162. // on to get the type correct.
  11163. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11164. }
  11165. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11166. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11167. }
  11168. - (instancetype)init {
  11169. return [self initWithBools:NULL forKeys:NULL count:0];
  11170. }
  11171. - (instancetype)initWithBools:(const BOOL [])values
  11172. forKeys:(const BOOL [])keys
  11173. count:(NSUInteger)count {
  11174. self = [super init];
  11175. if (self) {
  11176. for (NSUInteger i = 0; i < count; ++i) {
  11177. int idx = keys[i] ? 1 : 0;
  11178. _values[idx] = values[i];
  11179. _valueSet[idx] = YES;
  11180. }
  11181. }
  11182. return self;
  11183. }
  11184. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  11185. self = [self initWithBools:NULL forKeys:NULL count:0];
  11186. if (self) {
  11187. if (dictionary) {
  11188. for (int i = 0; i < 2; ++i) {
  11189. if (dictionary->_valueSet[i]) {
  11190. _values[i] = dictionary->_values[i];
  11191. _valueSet[i] = YES;
  11192. }
  11193. }
  11194. }
  11195. }
  11196. return self;
  11197. }
  11198. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11199. #pragma unused(numItems)
  11200. return [self initWithBools:NULL forKeys:NULL count:0];
  11201. }
  11202. #if !defined(NS_BLOCK_ASSERTIONS)
  11203. - (void)dealloc {
  11204. NSAssert(!_autocreator,
  11205. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11206. [self class], _autocreator);
  11207. [super dealloc];
  11208. }
  11209. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11210. - (instancetype)copyWithZone:(NSZone *)zone {
  11211. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  11212. }
  11213. - (BOOL)isEqual:(id)other {
  11214. if (self == other) {
  11215. return YES;
  11216. }
  11217. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  11218. return NO;
  11219. }
  11220. GPBBoolBoolDictionary *otherDictionary = other;
  11221. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11222. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11223. return NO;
  11224. }
  11225. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11226. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11227. return NO;
  11228. }
  11229. return YES;
  11230. }
  11231. - (NSUInteger)hash {
  11232. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11233. }
  11234. - (NSString *)description {
  11235. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11236. if (_valueSet[0]) {
  11237. [result appendFormat:@"NO: %d", _values[0]];
  11238. }
  11239. if (_valueSet[1]) {
  11240. [result appendFormat:@"YES: %d", _values[1]];
  11241. }
  11242. [result appendString:@" }"];
  11243. return result;
  11244. }
  11245. - (NSUInteger)count {
  11246. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11247. }
  11248. - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
  11249. int idx = (key ? 1 : 0);
  11250. if (_valueSet[idx]) {
  11251. if (value) {
  11252. *value = _values[idx];
  11253. }
  11254. return YES;
  11255. }
  11256. return NO;
  11257. }
  11258. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11259. forGPBGenericValueKey:(GPBGenericValue *)key {
  11260. int idx = (key->valueBool ? 1 : 0);
  11261. _values[idx] = value->valueBool;
  11262. _valueSet[idx] = YES;
  11263. }
  11264. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11265. if (_valueSet[0]) {
  11266. block(@"false", (_values[0] ? @"true" : @"false"));
  11267. }
  11268. if (_valueSet[1]) {
  11269. block(@"true", (_values[1] ? @"true" : @"false"));
  11270. }
  11271. }
  11272. - (void)enumerateKeysAndBoolsUsingBlock:
  11273. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  11274. BOOL stop = NO;
  11275. if (_valueSet[0]) {
  11276. block(NO, _values[0], &stop);
  11277. }
  11278. if (!stop && _valueSet[1]) {
  11279. block(YES, _values[1], &stop);
  11280. }
  11281. }
  11282. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11283. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11284. NSUInteger count = 0;
  11285. size_t result = 0;
  11286. for (int i = 0; i < 2; ++i) {
  11287. if (_valueSet[i]) {
  11288. ++count;
  11289. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11290. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11291. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11292. }
  11293. }
  11294. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11295. result += tagSize * count;
  11296. return result;
  11297. }
  11298. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11299. asField:(GPBFieldDescriptor *)field {
  11300. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11301. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11302. for (int i = 0; i < 2; ++i) {
  11303. if (_valueSet[i]) {
  11304. // Write the tag.
  11305. [outputStream writeInt32NoTag:tag];
  11306. // Write the size of the message.
  11307. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11308. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11309. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11310. // Write the fields.
  11311. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11312. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11313. }
  11314. }
  11315. }
  11316. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  11317. if (otherDictionary) {
  11318. for (int i = 0; i < 2; ++i) {
  11319. if (otherDictionary->_valueSet[i]) {
  11320. _valueSet[i] = YES;
  11321. _values[i] = otherDictionary->_values[i];
  11322. }
  11323. }
  11324. if (_autocreator) {
  11325. GPBAutocreatedDictionaryModified(_autocreator, self);
  11326. }
  11327. }
  11328. }
  11329. - (void)setBool:(BOOL)value forKey:(BOOL)key {
  11330. int idx = (key ? 1 : 0);
  11331. _values[idx] = value;
  11332. _valueSet[idx] = YES;
  11333. if (_autocreator) {
  11334. GPBAutocreatedDictionaryModified(_autocreator, self);
  11335. }
  11336. }
  11337. - (void)removeBoolForKey:(BOOL)aKey {
  11338. _valueSet[aKey ? 1 : 0] = NO;
  11339. }
  11340. - (void)removeAll {
  11341. _valueSet[0] = NO;
  11342. _valueSet[1] = NO;
  11343. }
  11344. @end
  11345. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  11346. // This block of code is generated, do not edit it directly.
  11347. #pragma mark - Bool -> Float
  11348. @implementation GPBBoolFloatDictionary {
  11349. @package
  11350. float _values[2];
  11351. BOOL _valueSet[2];
  11352. }
  11353. + (instancetype)dictionary {
  11354. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  11355. }
  11356. + (instancetype)dictionaryWithFloat:(float)value
  11357. forKey:(BOOL)key {
  11358. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  11359. // on to get the type correct.
  11360. return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:&value
  11361. forKeys:&key
  11362. count:1] autorelease];
  11363. }
  11364. + (instancetype)dictionaryWithFloats:(const float [])values
  11365. forKeys:(const BOOL [])keys
  11366. count:(NSUInteger)count {
  11367. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  11368. // on to get the type correct.
  11369. return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:values
  11370. forKeys:keys
  11371. count:count] autorelease];
  11372. }
  11373. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  11374. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11375. // on to get the type correct.
  11376. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11377. }
  11378. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11379. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11380. }
  11381. - (instancetype)init {
  11382. return [self initWithFloats:NULL forKeys:NULL count:0];
  11383. }
  11384. - (instancetype)initWithFloats:(const float [])values
  11385. forKeys:(const BOOL [])keys
  11386. count:(NSUInteger)count {
  11387. self = [super init];
  11388. if (self) {
  11389. for (NSUInteger i = 0; i < count; ++i) {
  11390. int idx = keys[i] ? 1 : 0;
  11391. _values[idx] = values[i];
  11392. _valueSet[idx] = YES;
  11393. }
  11394. }
  11395. return self;
  11396. }
  11397. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  11398. self = [self initWithFloats:NULL forKeys:NULL count:0];
  11399. if (self) {
  11400. if (dictionary) {
  11401. for (int i = 0; i < 2; ++i) {
  11402. if (dictionary->_valueSet[i]) {
  11403. _values[i] = dictionary->_values[i];
  11404. _valueSet[i] = YES;
  11405. }
  11406. }
  11407. }
  11408. }
  11409. return self;
  11410. }
  11411. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11412. #pragma unused(numItems)
  11413. return [self initWithFloats:NULL forKeys:NULL count:0];
  11414. }
  11415. #if !defined(NS_BLOCK_ASSERTIONS)
  11416. - (void)dealloc {
  11417. NSAssert(!_autocreator,
  11418. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11419. [self class], _autocreator);
  11420. [super dealloc];
  11421. }
  11422. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11423. - (instancetype)copyWithZone:(NSZone *)zone {
  11424. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  11425. }
  11426. - (BOOL)isEqual:(id)other {
  11427. if (self == other) {
  11428. return YES;
  11429. }
  11430. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  11431. return NO;
  11432. }
  11433. GPBBoolFloatDictionary *otherDictionary = other;
  11434. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11435. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11436. return NO;
  11437. }
  11438. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11439. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11440. return NO;
  11441. }
  11442. return YES;
  11443. }
  11444. - (NSUInteger)hash {
  11445. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11446. }
  11447. - (NSString *)description {
  11448. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11449. if (_valueSet[0]) {
  11450. [result appendFormat:@"NO: %f", _values[0]];
  11451. }
  11452. if (_valueSet[1]) {
  11453. [result appendFormat:@"YES: %f", _values[1]];
  11454. }
  11455. [result appendString:@" }"];
  11456. return result;
  11457. }
  11458. - (NSUInteger)count {
  11459. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11460. }
  11461. - (BOOL)getFloat:(float *)value forKey:(BOOL)key {
  11462. int idx = (key ? 1 : 0);
  11463. if (_valueSet[idx]) {
  11464. if (value) {
  11465. *value = _values[idx];
  11466. }
  11467. return YES;
  11468. }
  11469. return NO;
  11470. }
  11471. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11472. forGPBGenericValueKey:(GPBGenericValue *)key {
  11473. int idx = (key->valueBool ? 1 : 0);
  11474. _values[idx] = value->valueFloat;
  11475. _valueSet[idx] = YES;
  11476. }
  11477. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11478. if (_valueSet[0]) {
  11479. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  11480. }
  11481. if (_valueSet[1]) {
  11482. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  11483. }
  11484. }
  11485. - (void)enumerateKeysAndFloatsUsingBlock:
  11486. (void (^)(BOOL key, float value, BOOL *stop))block {
  11487. BOOL stop = NO;
  11488. if (_valueSet[0]) {
  11489. block(NO, _values[0], &stop);
  11490. }
  11491. if (!stop && _valueSet[1]) {
  11492. block(YES, _values[1], &stop);
  11493. }
  11494. }
  11495. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11496. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11497. NSUInteger count = 0;
  11498. size_t result = 0;
  11499. for (int i = 0; i < 2; ++i) {
  11500. if (_valueSet[i]) {
  11501. ++count;
  11502. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11503. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11504. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11505. }
  11506. }
  11507. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11508. result += tagSize * count;
  11509. return result;
  11510. }
  11511. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11512. asField:(GPBFieldDescriptor *)field {
  11513. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11514. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11515. for (int i = 0; i < 2; ++i) {
  11516. if (_valueSet[i]) {
  11517. // Write the tag.
  11518. [outputStream writeInt32NoTag:tag];
  11519. // Write the size of the message.
  11520. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11521. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11522. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11523. // Write the fields.
  11524. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11525. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11526. }
  11527. }
  11528. }
  11529. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  11530. if (otherDictionary) {
  11531. for (int i = 0; i < 2; ++i) {
  11532. if (otherDictionary->_valueSet[i]) {
  11533. _valueSet[i] = YES;
  11534. _values[i] = otherDictionary->_values[i];
  11535. }
  11536. }
  11537. if (_autocreator) {
  11538. GPBAutocreatedDictionaryModified(_autocreator, self);
  11539. }
  11540. }
  11541. }
  11542. - (void)setFloat:(float)value forKey:(BOOL)key {
  11543. int idx = (key ? 1 : 0);
  11544. _values[idx] = value;
  11545. _valueSet[idx] = YES;
  11546. if (_autocreator) {
  11547. GPBAutocreatedDictionaryModified(_autocreator, self);
  11548. }
  11549. }
  11550. - (void)removeFloatForKey:(BOOL)aKey {
  11551. _valueSet[aKey ? 1 : 0] = NO;
  11552. }
  11553. - (void)removeAll {
  11554. _valueSet[0] = NO;
  11555. _valueSet[1] = NO;
  11556. }
  11557. @end
  11558. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  11559. // This block of code is generated, do not edit it directly.
  11560. #pragma mark - Bool -> Double
  11561. @implementation GPBBoolDoubleDictionary {
  11562. @package
  11563. double _values[2];
  11564. BOOL _valueSet[2];
  11565. }
  11566. + (instancetype)dictionary {
  11567. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  11568. }
  11569. + (instancetype)dictionaryWithDouble:(double)value
  11570. forKey:(BOOL)key {
  11571. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  11572. // on to get the type correct.
  11573. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:&value
  11574. forKeys:&key
  11575. count:1] autorelease];
  11576. }
  11577. + (instancetype)dictionaryWithDoubles:(const double [])values
  11578. forKeys:(const BOOL [])keys
  11579. count:(NSUInteger)count {
  11580. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  11581. // on to get the type correct.
  11582. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:values
  11583. forKeys:keys
  11584. count:count] autorelease];
  11585. }
  11586. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11587. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11588. // on to get the type correct.
  11589. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11590. }
  11591. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11592. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11593. }
  11594. - (instancetype)init {
  11595. return [self initWithDoubles:NULL forKeys:NULL count:0];
  11596. }
  11597. - (instancetype)initWithDoubles:(const double [])values
  11598. forKeys:(const BOOL [])keys
  11599. count:(NSUInteger)count {
  11600. self = [super init];
  11601. if (self) {
  11602. for (NSUInteger i = 0; i < count; ++i) {
  11603. int idx = keys[i] ? 1 : 0;
  11604. _values[idx] = values[i];
  11605. _valueSet[idx] = YES;
  11606. }
  11607. }
  11608. return self;
  11609. }
  11610. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11611. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  11612. if (self) {
  11613. if (dictionary) {
  11614. for (int i = 0; i < 2; ++i) {
  11615. if (dictionary->_valueSet[i]) {
  11616. _values[i] = dictionary->_values[i];
  11617. _valueSet[i] = YES;
  11618. }
  11619. }
  11620. }
  11621. }
  11622. return self;
  11623. }
  11624. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11625. #pragma unused(numItems)
  11626. return [self initWithDoubles:NULL forKeys:NULL count:0];
  11627. }
  11628. #if !defined(NS_BLOCK_ASSERTIONS)
  11629. - (void)dealloc {
  11630. NSAssert(!_autocreator,
  11631. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11632. [self class], _autocreator);
  11633. [super dealloc];
  11634. }
  11635. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11636. - (instancetype)copyWithZone:(NSZone *)zone {
  11637. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  11638. }
  11639. - (BOOL)isEqual:(id)other {
  11640. if (self == other) {
  11641. return YES;
  11642. }
  11643. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  11644. return NO;
  11645. }
  11646. GPBBoolDoubleDictionary *otherDictionary = other;
  11647. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11648. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11649. return NO;
  11650. }
  11651. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11652. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11653. return NO;
  11654. }
  11655. return YES;
  11656. }
  11657. - (NSUInteger)hash {
  11658. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11659. }
  11660. - (NSString *)description {
  11661. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11662. if (_valueSet[0]) {
  11663. [result appendFormat:@"NO: %lf", _values[0]];
  11664. }
  11665. if (_valueSet[1]) {
  11666. [result appendFormat:@"YES: %lf", _values[1]];
  11667. }
  11668. [result appendString:@" }"];
  11669. return result;
  11670. }
  11671. - (NSUInteger)count {
  11672. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11673. }
  11674. - (BOOL)getDouble:(double *)value forKey:(BOOL)key {
  11675. int idx = (key ? 1 : 0);
  11676. if (_valueSet[idx]) {
  11677. if (value) {
  11678. *value = _values[idx];
  11679. }
  11680. return YES;
  11681. }
  11682. return NO;
  11683. }
  11684. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11685. forGPBGenericValueKey:(GPBGenericValue *)key {
  11686. int idx = (key->valueBool ? 1 : 0);
  11687. _values[idx] = value->valueDouble;
  11688. _valueSet[idx] = YES;
  11689. }
  11690. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11691. if (_valueSet[0]) {
  11692. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11693. }
  11694. if (_valueSet[1]) {
  11695. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11696. }
  11697. }
  11698. - (void)enumerateKeysAndDoublesUsingBlock:
  11699. (void (^)(BOOL key, double value, BOOL *stop))block {
  11700. BOOL stop = NO;
  11701. if (_valueSet[0]) {
  11702. block(NO, _values[0], &stop);
  11703. }
  11704. if (!stop && _valueSet[1]) {
  11705. block(YES, _values[1], &stop);
  11706. }
  11707. }
  11708. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11709. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11710. NSUInteger count = 0;
  11711. size_t result = 0;
  11712. for (int i = 0; i < 2; ++i) {
  11713. if (_valueSet[i]) {
  11714. ++count;
  11715. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11716. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11717. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11718. }
  11719. }
  11720. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11721. result += tagSize * count;
  11722. return result;
  11723. }
  11724. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11725. asField:(GPBFieldDescriptor *)field {
  11726. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11727. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11728. for (int i = 0; i < 2; ++i) {
  11729. if (_valueSet[i]) {
  11730. // Write the tag.
  11731. [outputStream writeInt32NoTag:tag];
  11732. // Write the size of the message.
  11733. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11734. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11735. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11736. // Write the fields.
  11737. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11738. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11739. }
  11740. }
  11741. }
  11742. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11743. if (otherDictionary) {
  11744. for (int i = 0; i < 2; ++i) {
  11745. if (otherDictionary->_valueSet[i]) {
  11746. _valueSet[i] = YES;
  11747. _values[i] = otherDictionary->_values[i];
  11748. }
  11749. }
  11750. if (_autocreator) {
  11751. GPBAutocreatedDictionaryModified(_autocreator, self);
  11752. }
  11753. }
  11754. }
  11755. - (void)setDouble:(double)value forKey:(BOOL)key {
  11756. int idx = (key ? 1 : 0);
  11757. _values[idx] = value;
  11758. _valueSet[idx] = YES;
  11759. if (_autocreator) {
  11760. GPBAutocreatedDictionaryModified(_autocreator, self);
  11761. }
  11762. }
  11763. - (void)removeDoubleForKey:(BOOL)aKey {
  11764. _valueSet[aKey ? 1 : 0] = NO;
  11765. }
  11766. - (void)removeAll {
  11767. _valueSet[0] = NO;
  11768. _valueSet[1] = NO;
  11769. }
  11770. @end
  11771. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11772. // This block of code is generated, do not edit it directly.
  11773. #pragma mark - Bool -> Object
  11774. @implementation GPBBoolObjectDictionary {
  11775. @package
  11776. id _values[2];
  11777. }
  11778. + (instancetype)dictionary {
  11779. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11780. }
  11781. + (instancetype)dictionaryWithObject:(id)object
  11782. forKey:(BOOL)key {
  11783. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11784. // on to get the type correct.
  11785. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11786. forKeys:&key
  11787. count:1] autorelease];
  11788. }
  11789. + (instancetype)dictionaryWithObjects:(const id [])objects
  11790. forKeys:(const BOOL [])keys
  11791. count:(NSUInteger)count {
  11792. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11793. // on to get the type correct.
  11794. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11795. forKeys:keys
  11796. count:count] autorelease];
  11797. }
  11798. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11799. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11800. // on to get the type correct.
  11801. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11802. }
  11803. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11804. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11805. }
  11806. - (instancetype)init {
  11807. return [self initWithObjects:NULL forKeys:NULL count:0];
  11808. }
  11809. - (instancetype)initWithObjects:(const id [])objects
  11810. forKeys:(const BOOL [])keys
  11811. count:(NSUInteger)count {
  11812. self = [super init];
  11813. if (self) {
  11814. for (NSUInteger i = 0; i < count; ++i) {
  11815. if (!objects[i]) {
  11816. [NSException raise:NSInvalidArgumentException
  11817. format:@"Attempting to add nil object to a Dictionary"];
  11818. }
  11819. int idx = keys[i] ? 1 : 0;
  11820. [_values[idx] release];
  11821. _values[idx] = (id)[objects[i] retain];
  11822. }
  11823. }
  11824. return self;
  11825. }
  11826. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11827. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11828. if (self) {
  11829. if (dictionary) {
  11830. _values[0] = [dictionary->_values[0] retain];
  11831. _values[1] = [dictionary->_values[1] retain];
  11832. }
  11833. }
  11834. return self;
  11835. }
  11836. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11837. #pragma unused(numItems)
  11838. return [self initWithObjects:NULL forKeys:NULL count:0];
  11839. }
  11840. - (void)dealloc {
  11841. NSAssert(!_autocreator,
  11842. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11843. [self class], _autocreator);
  11844. [_values[0] release];
  11845. [_values[1] release];
  11846. [super dealloc];
  11847. }
  11848. - (instancetype)copyWithZone:(NSZone *)zone {
  11849. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11850. }
  11851. - (BOOL)isEqual:(id)other {
  11852. if (self == other) {
  11853. return YES;
  11854. }
  11855. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11856. return NO;
  11857. }
  11858. GPBBoolObjectDictionary *otherDictionary = other;
  11859. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  11860. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  11861. return NO;
  11862. }
  11863. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  11864. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  11865. return NO;
  11866. }
  11867. return YES;
  11868. }
  11869. - (NSUInteger)hash {
  11870. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11871. }
  11872. - (NSString *)description {
  11873. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11874. if ((_values[0] != nil)) {
  11875. [result appendFormat:@"NO: %@", _values[0]];
  11876. }
  11877. if ((_values[1] != nil)) {
  11878. [result appendFormat:@"YES: %@", _values[1]];
  11879. }
  11880. [result appendString:@" }"];
  11881. return result;
  11882. }
  11883. - (NSUInteger)count {
  11884. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11885. }
  11886. - (id)objectForKey:(BOOL)key {
  11887. return _values[key ? 1 : 0];
  11888. }
  11889. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11890. forGPBGenericValueKey:(GPBGenericValue *)key {
  11891. int idx = (key->valueBool ? 1 : 0);
  11892. [_values[idx] release];
  11893. _values[idx] = [value->valueString retain];
  11894. }
  11895. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11896. if (_values[0] != nil) {
  11897. block(@"false", _values[0]);
  11898. }
  11899. if ((_values[1] != nil)) {
  11900. block(@"true", _values[1]);
  11901. }
  11902. }
  11903. - (void)enumerateKeysAndObjectsUsingBlock:
  11904. (void (^)(BOOL key, id object, BOOL *stop))block {
  11905. BOOL stop = NO;
  11906. if (_values[0] != nil) {
  11907. block(NO, _values[0], &stop);
  11908. }
  11909. if (!stop && (_values[1] != nil)) {
  11910. block(YES, _values[1], &stop);
  11911. }
  11912. }
  11913. - (BOOL)isInitialized {
  11914. if (_values[0] && ![_values[0] isInitialized]) {
  11915. return NO;
  11916. }
  11917. if (_values[1] && ![_values[1] isInitialized]) {
  11918. return NO;
  11919. }
  11920. return YES;
  11921. }
  11922. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11923. GPBBoolObjectDictionary *newDict =
  11924. [[GPBBoolObjectDictionary alloc] init];
  11925. for (int i = 0; i < 2; ++i) {
  11926. if (_values[i] != nil) {
  11927. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11928. }
  11929. }
  11930. return newDict;
  11931. }
  11932. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11933. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11934. NSUInteger count = 0;
  11935. size_t result = 0;
  11936. for (int i = 0; i < 2; ++i) {
  11937. if (_values[i] != nil) {
  11938. ++count;
  11939. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11940. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11941. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11942. }
  11943. }
  11944. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11945. result += tagSize * count;
  11946. return result;
  11947. }
  11948. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11949. asField:(GPBFieldDescriptor *)field {
  11950. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11951. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11952. for (int i = 0; i < 2; ++i) {
  11953. if (_values[i] != nil) {
  11954. // Write the tag.
  11955. [outputStream writeInt32NoTag:tag];
  11956. // Write the size of the message.
  11957. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11958. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11959. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11960. // Write the fields.
  11961. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11962. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11963. }
  11964. }
  11965. }
  11966. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11967. if (otherDictionary) {
  11968. for (int i = 0; i < 2; ++i) {
  11969. if (otherDictionary->_values[i] != nil) {
  11970. [_values[i] release];
  11971. _values[i] = [otherDictionary->_values[i] retain];
  11972. }
  11973. }
  11974. if (_autocreator) {
  11975. GPBAutocreatedDictionaryModified(_autocreator, self);
  11976. }
  11977. }
  11978. }
  11979. - (void)setObject:(id)object forKey:(BOOL)key {
  11980. if (!object) {
  11981. [NSException raise:NSInvalidArgumentException
  11982. format:@"Attempting to add nil object to a Dictionary"];
  11983. }
  11984. int idx = (key ? 1 : 0);
  11985. [_values[idx] release];
  11986. _values[idx] = [object retain];
  11987. if (_autocreator) {
  11988. GPBAutocreatedDictionaryModified(_autocreator, self);
  11989. }
  11990. }
  11991. - (void)removeObjectForKey:(BOOL)aKey {
  11992. int idx = (aKey ? 1 : 0);
  11993. [_values[idx] release];
  11994. _values[idx] = nil;
  11995. }
  11996. - (void)removeAll {
  11997. for (int i = 0; i < 2; ++i) {
  11998. [_values[i] release];
  11999. _values[i] = nil;
  12000. }
  12001. }
  12002. @end
  12003. //%PDDM-EXPAND-END (8 expansions)
  12004. #pragma mark - Bool -> Enum
  12005. @implementation GPBBoolEnumDictionary {
  12006. @package
  12007. GPBEnumValidationFunc _validationFunc;
  12008. int32_t _values[2];
  12009. BOOL _valueSet[2];
  12010. }
  12011. @synthesize validationFunc = _validationFunc;
  12012. + (instancetype)dictionary {
  12013. return [[[self alloc] initWithValidationFunction:NULL
  12014. rawValues:NULL
  12015. forKeys:NULL
  12016. count:0] autorelease];
  12017. }
  12018. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  12019. return [[[self alloc] initWithValidationFunction:func
  12020. rawValues:NULL
  12021. forKeys:NULL
  12022. count:0] autorelease];
  12023. }
  12024. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  12025. rawValue:(int32_t)rawValue
  12026. forKey:(BOOL)key {
  12027. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  12028. // on to get the type correct.
  12029. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  12030. rawValues:&rawValue
  12031. forKeys:&key
  12032. count:1] autorelease];
  12033. }
  12034. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  12035. rawValues:(const int32_t [])values
  12036. forKeys:(const BOOL [])keys
  12037. count:(NSUInteger)count {
  12038. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  12039. // on to get the type correct.
  12040. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  12041. rawValues:values
  12042. forKeys:keys
  12043. count:count] autorelease];
  12044. }
  12045. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  12046. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  12047. // on to get the type correct.
  12048. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  12049. }
  12050. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  12051. capacity:(NSUInteger)numItems {
  12052. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  12053. }
  12054. - (instancetype)init {
  12055. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  12056. }
  12057. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  12058. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  12059. }
  12060. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  12061. rawValues:(const int32_t [])rawValues
  12062. forKeys:(const BOOL [])keys
  12063. count:(NSUInteger)count {
  12064. self = [super init];
  12065. if (self) {
  12066. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  12067. for (NSUInteger i = 0; i < count; ++i) {
  12068. int idx = keys[i] ? 1 : 0;
  12069. _values[idx] = rawValues[i];
  12070. _valueSet[idx] = YES;
  12071. }
  12072. }
  12073. return self;
  12074. }
  12075. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  12076. self = [self initWithValidationFunction:dictionary.validationFunc
  12077. rawValues:NULL
  12078. forKeys:NULL
  12079. count:0];
  12080. if (self) {
  12081. if (dictionary) {
  12082. for (int i = 0; i < 2; ++i) {
  12083. if (dictionary->_valueSet[i]) {
  12084. _values[i] = dictionary->_values[i];
  12085. _valueSet[i] = YES;
  12086. }
  12087. }
  12088. }
  12089. }
  12090. return self;
  12091. }
  12092. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  12093. capacity:(NSUInteger)numItems {
  12094. #pragma unused(numItems)
  12095. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  12096. }
  12097. #if !defined(NS_BLOCK_ASSERTIONS)
  12098. - (void)dealloc {
  12099. NSAssert(!_autocreator,
  12100. @"%@: Autocreator must be cleared before release, autocreator: %@",
  12101. [self class], _autocreator);
  12102. [super dealloc];
  12103. }
  12104. #endif // !defined(NS_BLOCK_ASSERTIONS)
  12105. - (instancetype)copyWithZone:(NSZone *)zone {
  12106. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  12107. }
  12108. - (BOOL)isEqual:(id)other {
  12109. if (self == other) {
  12110. return YES;
  12111. }
  12112. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  12113. return NO;
  12114. }
  12115. GPBBoolEnumDictionary *otherDictionary = other;
  12116. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  12117. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  12118. return NO;
  12119. }
  12120. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  12121. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  12122. return NO;
  12123. }
  12124. return YES;
  12125. }
  12126. - (NSUInteger)hash {
  12127. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  12128. }
  12129. - (NSString *)description {
  12130. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  12131. if (_valueSet[0]) {
  12132. [result appendFormat:@"NO: %d", _values[0]];
  12133. }
  12134. if (_valueSet[1]) {
  12135. [result appendFormat:@"YES: %d", _values[1]];
  12136. }
  12137. [result appendString:@" }"];
  12138. return result;
  12139. }
  12140. - (NSUInteger)count {
  12141. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  12142. }
  12143. - (BOOL)getEnum:(int32_t*)value forKey:(BOOL)key {
  12144. int idx = (key ? 1 : 0);
  12145. if (_valueSet[idx]) {
  12146. if (value) {
  12147. int32_t result = _values[idx];
  12148. if (!_validationFunc(result)) {
  12149. result = kGPBUnrecognizedEnumeratorValue;
  12150. }
  12151. *value = result;
  12152. }
  12153. return YES;
  12154. }
  12155. return NO;
  12156. }
  12157. - (BOOL)getRawValue:(int32_t*)rawValue forKey:(BOOL)key {
  12158. int idx = (key ? 1 : 0);
  12159. if (_valueSet[idx]) {
  12160. if (rawValue) {
  12161. *rawValue = _values[idx];
  12162. }
  12163. return YES;
  12164. }
  12165. return NO;
  12166. }
  12167. - (void)enumerateKeysAndRawValuesUsingBlock:
  12168. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  12169. BOOL stop = NO;
  12170. if (_valueSet[0]) {
  12171. block(NO, _values[0], &stop);
  12172. }
  12173. if (!stop && _valueSet[1]) {
  12174. block(YES, _values[1], &stop);
  12175. }
  12176. }
  12177. - (void)enumerateKeysAndEnumsUsingBlock:
  12178. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  12179. BOOL stop = NO;
  12180. GPBEnumValidationFunc func = _validationFunc;
  12181. int32_t validatedValue;
  12182. if (_valueSet[0]) {
  12183. validatedValue = _values[0];
  12184. if (!func(validatedValue)) {
  12185. validatedValue = kGPBUnrecognizedEnumeratorValue;
  12186. }
  12187. block(NO, validatedValue, &stop);
  12188. }
  12189. if (!stop && _valueSet[1]) {
  12190. validatedValue = _values[1];
  12191. if (!func(validatedValue)) {
  12192. validatedValue = kGPBUnrecognizedEnumeratorValue;
  12193. }
  12194. block(YES, validatedValue, &stop);
  12195. }
  12196. }
  12197. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  12198. // This block of code is generated, do not edit it directly.
  12199. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  12200. forKey:(GPBGenericValue *)key
  12201. keyDataType:(GPBDataType)keyDataType {
  12202. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  12203. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  12204. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  12205. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  12206. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  12207. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  12208. [outputStream release];
  12209. return data;
  12210. }
  12211. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  12212. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  12213. GPBDataType valueDataType = GPBGetFieldDataType(field);
  12214. NSUInteger count = 0;
  12215. size_t result = 0;
  12216. for (int i = 0; i < 2; ++i) {
  12217. if (_valueSet[i]) {
  12218. ++count;
  12219. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  12220. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  12221. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  12222. }
  12223. }
  12224. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  12225. result += tagSize * count;
  12226. return result;
  12227. }
  12228. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  12229. asField:(GPBFieldDescriptor *)field {
  12230. GPBDataType valueDataType = GPBGetFieldDataType(field);
  12231. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  12232. for (int i = 0; i < 2; ++i) {
  12233. if (_valueSet[i]) {
  12234. // Write the tag.
  12235. [outputStream writeInt32NoTag:tag];
  12236. // Write the size of the message.
  12237. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  12238. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  12239. [outputStream writeInt32NoTag:(int32_t)msgSize];
  12240. // Write the fields.
  12241. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  12242. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  12243. }
  12244. }
  12245. }
  12246. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  12247. if (_valueSet[0]) {
  12248. block(@"false", @(_values[0]));
  12249. }
  12250. if (_valueSet[1]) {
  12251. block(@"true", @(_values[1]));
  12252. }
  12253. }
  12254. - (void)setGPBGenericValue:(GPBGenericValue *)value
  12255. forGPBGenericValueKey:(GPBGenericValue *)key {
  12256. int idx = (key->valueBool ? 1 : 0);
  12257. _values[idx] = value->valueInt32;
  12258. _valueSet[idx] = YES;
  12259. }
  12260. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  12261. if (otherDictionary) {
  12262. for (int i = 0; i < 2; ++i) {
  12263. if (otherDictionary->_valueSet[i]) {
  12264. _valueSet[i] = YES;
  12265. _values[i] = otherDictionary->_values[i];
  12266. }
  12267. }
  12268. if (_autocreator) {
  12269. GPBAutocreatedDictionaryModified(_autocreator, self);
  12270. }
  12271. }
  12272. }
  12273. - (void)setEnum:(int32_t)value forKey:(BOOL)key {
  12274. if (!_validationFunc(value)) {
  12275. [NSException raise:NSInvalidArgumentException
  12276. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  12277. value];
  12278. }
  12279. int idx = (key ? 1 : 0);
  12280. _values[idx] = value;
  12281. _valueSet[idx] = YES;
  12282. if (_autocreator) {
  12283. GPBAutocreatedDictionaryModified(_autocreator, self);
  12284. }
  12285. }
  12286. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  12287. int idx = (key ? 1 : 0);
  12288. _values[idx] = rawValue;
  12289. _valueSet[idx] = YES;
  12290. if (_autocreator) {
  12291. GPBAutocreatedDictionaryModified(_autocreator, self);
  12292. }
  12293. }
  12294. - (void)removeEnumForKey:(BOOL)aKey {
  12295. _valueSet[aKey ? 1 : 0] = NO;
  12296. }
  12297. - (void)removeAll {
  12298. _valueSet[0] = NO;
  12299. _valueSet[1] = NO;
  12300. }
  12301. @end
  12302. #pragma mark - NSDictionary Subclass
  12303. @implementation GPBAutocreatedDictionary {
  12304. NSMutableDictionary *_dictionary;
  12305. }
  12306. - (void)dealloc {
  12307. NSAssert(!_autocreator,
  12308. @"%@: Autocreator must be cleared before release, autocreator: %@",
  12309. [self class], _autocreator);
  12310. [_dictionary release];
  12311. [super dealloc];
  12312. }
  12313. #pragma mark Required NSDictionary overrides
  12314. - (instancetype)initWithObjects:(const id [])objects
  12315. forKeys:(const id<NSCopying> [])keys
  12316. count:(NSUInteger)count {
  12317. self = [super init];
  12318. if (self) {
  12319. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  12320. forKeys:keys
  12321. count:count];
  12322. }
  12323. return self;
  12324. }
  12325. - (NSUInteger)count {
  12326. return [_dictionary count];
  12327. }
  12328. - (id)objectForKey:(id)aKey {
  12329. return [_dictionary objectForKey:aKey];
  12330. }
  12331. - (NSEnumerator *)keyEnumerator {
  12332. if (_dictionary == nil) {
  12333. _dictionary = [[NSMutableDictionary alloc] init];
  12334. }
  12335. return [_dictionary keyEnumerator];
  12336. }
  12337. #pragma mark Required NSMutableDictionary overrides
  12338. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  12339. // since we only autocreate empty dictionaries.
  12340. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  12341. if (_dictionary == nil) {
  12342. _dictionary = [[NSMutableDictionary alloc] init];
  12343. }
  12344. [_dictionary setObject:anObject forKey:aKey];
  12345. if (_autocreator) {
  12346. GPBAutocreatedDictionaryModified(_autocreator, self);
  12347. }
  12348. }
  12349. - (void)removeObjectForKey:(id)aKey {
  12350. [_dictionary removeObjectForKey:aKey];
  12351. }
  12352. #pragma mark Extra things hooked
  12353. - (id)copyWithZone:(NSZone *)zone {
  12354. if (_dictionary == nil) {
  12355. return [[NSMutableDictionary allocWithZone:zone] init];
  12356. }
  12357. return [_dictionary copyWithZone:zone];
  12358. }
  12359. - (id)mutableCopyWithZone:(NSZone *)zone {
  12360. if (_dictionary == nil) {
  12361. return [[NSMutableDictionary allocWithZone:zone] init];
  12362. }
  12363. return [_dictionary mutableCopyWithZone:zone];
  12364. }
  12365. // Not really needed, but subscripting is likely common enough it doesn't hurt
  12366. // to ensure it goes directly to the real NSMutableDictionary.
  12367. - (id)objectForKeyedSubscript:(id)key {
  12368. return [_dictionary objectForKeyedSubscript:key];
  12369. }
  12370. // Not really needed, but subscripting is likely common enough it doesn't hurt
  12371. // to ensure it goes directly to the real NSMutableDictionary.
  12372. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  12373. if (_dictionary == nil) {
  12374. _dictionary = [[NSMutableDictionary alloc] init];
  12375. }
  12376. [_dictionary setObject:obj forKeyedSubscript:key];
  12377. if (_autocreator) {
  12378. GPBAutocreatedDictionaryModified(_autocreator, self);
  12379. }
  12380. }
  12381. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  12382. id obj,
  12383. BOOL *stop))block {
  12384. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  12385. }
  12386. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  12387. usingBlock:(void (^)(id key,
  12388. id obj,
  12389. BOOL *stop))block {
  12390. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  12391. }
  12392. @end
  12393. #pragma clang diagnostic pop