GPBDictionary.m 467 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623
  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. __block size_t result = 0;
  303. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  304. #pragma unused(stop)
  305. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  306. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  307. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  308. }];
  309. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  310. result += tagSize * dict.count;
  311. return result;
  312. }
  313. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  314. NSDictionary *dict,
  315. GPBFieldDescriptor *field) {
  316. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  317. GPBDataType mapValueType = GPBGetFieldDataType(field);
  318. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  319. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  320. #pragma unused(stop)
  321. // Write the tag.
  322. [outputStream writeInt32NoTag:tag];
  323. // Write the size of the message.
  324. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  325. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  326. // Write the size and fields.
  327. [outputStream writeInt32NoTag:(int32_t)msgSize];
  328. [outputStream writeString:kMapKeyFieldNumber value:key];
  329. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  330. }];
  331. }
  332. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  333. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  334. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  335. #pragma unused(field) // For when asserts are off in release.
  336. for (GPBMessage *msg in [dict objectEnumerator]) {
  337. if (!msg.initialized) {
  338. return NO;
  339. }
  340. }
  341. return YES;
  342. }
  343. // Note: if the type is an object, it the retain pass back to the caller.
  344. static void ReadValue(GPBCodedInputStream *stream,
  345. GPBGenericValue *valueToFill,
  346. GPBDataType type,
  347. GPBExtensionRegistry *registry,
  348. GPBFieldDescriptor *field) {
  349. switch (type) {
  350. case GPBDataTypeBool:
  351. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  352. break;
  353. case GPBDataTypeFixed32:
  354. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  355. break;
  356. case GPBDataTypeSFixed32:
  357. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  358. break;
  359. case GPBDataTypeFloat:
  360. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  361. break;
  362. case GPBDataTypeFixed64:
  363. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  364. break;
  365. case GPBDataTypeSFixed64:
  366. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  367. break;
  368. case GPBDataTypeDouble:
  369. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  370. break;
  371. case GPBDataTypeInt32:
  372. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  373. break;
  374. case GPBDataTypeInt64:
  375. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  376. break;
  377. case GPBDataTypeSInt32:
  378. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  379. break;
  380. case GPBDataTypeSInt64:
  381. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  382. break;
  383. case GPBDataTypeUInt32:
  384. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  385. break;
  386. case GPBDataTypeUInt64:
  387. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  388. break;
  389. case GPBDataTypeBytes:
  390. [valueToFill->valueData release];
  391. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  392. break;
  393. case GPBDataTypeString:
  394. [valueToFill->valueString release];
  395. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  396. break;
  397. case GPBDataTypeMessage: {
  398. GPBMessage *message = [[field.msgClass alloc] init];
  399. [stream readMessage:message extensionRegistry:registry];
  400. [valueToFill->valueMessage release];
  401. valueToFill->valueMessage = message;
  402. break;
  403. }
  404. case GPBDataTypeGroup:
  405. NSCAssert(NO, @"Can't happen");
  406. break;
  407. case GPBDataTypeEnum:
  408. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  409. break;
  410. }
  411. }
  412. void GPBDictionaryReadEntry(id mapDictionary,
  413. GPBCodedInputStream *stream,
  414. GPBExtensionRegistry *registry,
  415. GPBFieldDescriptor *field,
  416. GPBMessage *parentMessage) {
  417. GPBDataType keyDataType = field.mapKeyDataType;
  418. GPBDataType valueDataType = GPBGetFieldDataType(field);
  419. GPBGenericValue key;
  420. GPBGenericValue value;
  421. // Zero them (but pick up any enum default for proto2).
  422. key.valueString = value.valueString = nil;
  423. if (valueDataType == GPBDataTypeEnum) {
  424. value = field.defaultValue;
  425. }
  426. GPBCodedInputStreamState *state = &stream->state_;
  427. uint32_t keyTag =
  428. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  429. uint32_t valueTag =
  430. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  431. BOOL hitError = NO;
  432. while (YES) {
  433. uint32_t tag = GPBCodedInputStreamReadTag(state);
  434. if (tag == keyTag) {
  435. ReadValue(stream, &key, keyDataType, registry, field);
  436. } else if (tag == valueTag) {
  437. ReadValue(stream, &value, valueDataType, registry, field);
  438. } else if (tag == 0) {
  439. // zero signals EOF / limit reached
  440. break;
  441. } else { // Unknown
  442. if (![stream skipField:tag]){
  443. hitError = YES;
  444. break;
  445. }
  446. }
  447. }
  448. if (!hitError) {
  449. // Handle the special defaults and/or missing key/value.
  450. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  451. key.valueString = [@"" retain];
  452. }
  453. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  454. #pragma clang diagnostic push
  455. #pragma clang diagnostic ignored "-Wswitch-enum"
  456. switch (valueDataType) {
  457. case GPBDataTypeString:
  458. value.valueString = [@"" retain];
  459. break;
  460. case GPBDataTypeBytes:
  461. value.valueData = [GPBEmptyNSData() retain];
  462. break;
  463. #if defined(__clang_analyzer__)
  464. case GPBDataTypeGroup:
  465. // Maps can't really have Groups as the value type, but this case is needed
  466. // so the analyzer won't report the posibility of send nil in for the value
  467. // in the NSMutableDictionary case below.
  468. #endif
  469. case GPBDataTypeMessage: {
  470. value.valueMessage = [[field.msgClass alloc] init];
  471. break;
  472. }
  473. default:
  474. // Nothing
  475. break;
  476. }
  477. #pragma clang diagnostic pop
  478. }
  479. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  480. #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
  481. // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
  482. // be raised as needed for new Xcodes.
  483. //
  484. // This is only needed on a "shallow" analyze; on a "deep" analyze, the
  485. // existing code path gets this correct. In shallow, the analyzer decides
  486. // GPBDataTypeIsObject(valueDataType) is both false and true on a single
  487. // path through this function, allowing nil to be used for the
  488. // setObject:forKey:.
  489. if (value.valueString == nil) {
  490. value.valueString = [@"" retain];
  491. }
  492. #endif
  493. // mapDictionary is an NSMutableDictionary
  494. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  495. forKey:key.valueString];
  496. } else {
  497. if (valueDataType == GPBDataTypeEnum) {
  498. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  499. [field isValidEnumValue:value.valueEnum]) {
  500. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  501. } else {
  502. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  503. forKey:&key
  504. keyDataType:keyDataType];
  505. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  506. }
  507. } else {
  508. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  509. }
  510. }
  511. }
  512. if (GPBDataTypeIsObject(keyDataType)) {
  513. [key.valueString release];
  514. }
  515. if (GPBDataTypeIsObject(valueDataType)) {
  516. [value.valueString release];
  517. }
  518. }
  519. //
  520. // Macros for the common basic cases.
  521. //
  522. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  523. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  524. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  525. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  526. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  527. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  528. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  529. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  530. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  531. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  532. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  533. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  534. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  535. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
  536. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  537. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
  538. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
  539. //%#pragma mark - KEY_NAME -> VALUE_NAME
  540. //%
  541. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  542. //% @package
  543. //% NSMutableDictionary *_dictionary;
  544. //%}
  545. //%
  546. //%+ (instancetype)dictionary {
  547. //% return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
  548. //%}
  549. //%
  550. //%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)##VNAME_VAR
  551. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  552. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  553. //% // on to get the type correct.
  554. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
  555. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  556. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  557. //%}
  558. //%
  559. //%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  560. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  561. //% ##VNAME$S## count:(NSUInteger)count {
  562. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  563. //% // on to get the type correct.
  564. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
  565. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  566. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  567. //%}
  568. //%
  569. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  570. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  571. //% // on to get the type correct.
  572. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  573. //%}
  574. //%
  575. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  576. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  577. //%}
  578. //%
  579. //%- (instancetype)init {
  580. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  581. //%}
  582. //%
  583. //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  584. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  585. //% ##VNAME$S## count:(NSUInteger)count {
  586. //% self = [super init];
  587. //% if (self) {
  588. //% _dictionary = [[NSMutableDictionary alloc] init];
  589. //% if (count && VNAME_VAR##s && keys) {
  590. //% for (NSUInteger i = 0; i < count; ++i) {
  591. //%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])];
  592. //% }
  593. //% }
  594. //% }
  595. //% return self;
  596. //%}
  597. //%
  598. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  599. //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  600. //% if (self) {
  601. //% if (dictionary) {
  602. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  603. //% }
  604. //% }
  605. //% return self;
  606. //%}
  607. //%
  608. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  609. //% #pragma unused(numItems)
  610. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  611. //%}
  612. //%
  613. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  614. //%
  615. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  616. //%
  617. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  618. //%
  619. //%@end
  620. //%
  621. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  622. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  623. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  624. //%#pragma mark - KEY_NAME -> VALUE_NAME
  625. //%
  626. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  627. //% @package
  628. //% NSMutableDictionary *_dictionary;
  629. //% GPBEnumValidationFunc _validationFunc;
  630. //%}
  631. //%
  632. //%@synthesize validationFunc = _validationFunc;
  633. //%
  634. //%+ (instancetype)dictionary {
  635. //% return [[[self alloc] initWithValidationFunction:NULL
  636. //% rawValues:NULL
  637. //% forKeys:NULL
  638. //% count:0] autorelease];
  639. //%}
  640. //%
  641. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  642. //% return [[[self alloc] initWithValidationFunction:func
  643. //% rawValues:NULL
  644. //% forKeys:NULL
  645. //% count:0] autorelease];
  646. //%}
  647. //%
  648. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  649. //% rawValue:(VALUE_TYPE)rawValue
  650. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  651. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  652. //% // on to get the type correct.
  653. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  654. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  655. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  656. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  657. //%}
  658. //%
  659. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  660. //% rawValues:(const VALUE_TYPE [])rawValues
  661. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  662. //% count:(NSUInteger)count {
  663. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  664. //% // on to get the type correct.
  665. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  666. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  667. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  668. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  669. //%}
  670. //%
  671. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  672. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  673. //% // on to get the type correct.
  674. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  675. //%}
  676. //%
  677. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  678. //% capacity:(NSUInteger)numItems {
  679. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  680. //%}
  681. //%
  682. //%- (instancetype)init {
  683. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  684. //%}
  685. //%
  686. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  687. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  688. //%}
  689. //%
  690. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  691. //% rawValues:(const VALUE_TYPE [])rawValues
  692. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  693. //% count:(NSUInteger)count {
  694. //% self = [super init];
  695. //% if (self) {
  696. //% _dictionary = [[NSMutableDictionary alloc] init];
  697. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  698. //% if (count && rawValues && keys) {
  699. //% for (NSUInteger i = 0; i < count; ++i) {
  700. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  701. //% }
  702. //% }
  703. //% }
  704. //% return self;
  705. //%}
  706. //%
  707. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  708. //% self = [self initWithValidationFunction:dictionary.validationFunc
  709. //% rawValues:NULL
  710. //% forKeys:NULL
  711. //% count:0];
  712. //% if (self) {
  713. //% if (dictionary) {
  714. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  715. //% }
  716. //% }
  717. //% return self;
  718. //%}
  719. //%
  720. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  721. //% capacity:(NSUInteger)numItems {
  722. //% #pragma unused(numItems)
  723. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  724. //%}
  725. //%
  726. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
  727. //%
  728. //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  729. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  730. //% if (wrapped && value) {
  731. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  732. //% if (!_validationFunc(result)) {
  733. //% result = kGPBUnrecognizedEnumeratorValue;
  734. //% }
  735. //% *value = result;
  736. //% }
  737. //% return (wrapped != NULL);
  738. //%}
  739. //%
  740. //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
  741. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  742. //% if (wrapped && rawValue) {
  743. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  744. //% }
  745. //% return (wrapped != NULL);
  746. //%}
  747. //%
  748. //%- (void)enumerateKeysAndEnumsUsingBlock:
  749. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  750. //% GPBEnumValidationFunc func = _validationFunc;
  751. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  752. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  753. //% BOOL *stop) {
  754. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  755. //% if (!func(unwrapped)) {
  756. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  757. //% }
  758. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  759. //% }];
  760. //%}
  761. //%
  762. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
  763. //%
  764. //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  765. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  766. //% [NSException raise:NSInvalidArgumentException
  767. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  768. //% value];
  769. //% }
  770. //%
  771. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  772. //% if (_autocreator) {
  773. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  774. //% }
  775. //%}
  776. //%
  777. //%@end
  778. //%
  779. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  780. //%- (void)dealloc {
  781. //% NSAssert(!_autocreator,
  782. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  783. //% [self class], _autocreator);
  784. //% [_dictionary release];
  785. //% [super dealloc];
  786. //%}
  787. //%
  788. //%- (instancetype)copyWithZone:(NSZone *)zone {
  789. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  790. //%}
  791. //%
  792. //%- (BOOL)isEqual:(id)other {
  793. //% if (self == other) {
  794. //% return YES;
  795. //% }
  796. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  797. //% return NO;
  798. //% }
  799. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  800. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  801. //%}
  802. //%
  803. //%- (NSUInteger)hash {
  804. //% return _dictionary.count;
  805. //%}
  806. //%
  807. //%- (NSString *)description {
  808. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  809. //%}
  810. //%
  811. //%- (NSUInteger)count {
  812. //% return _dictionary.count;
  813. //%}
  814. //%
  815. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
  816. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  817. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  818. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u,
  819. //% BOOL *stop) {
  820. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), stop);
  821. //% }];
  822. //%}
  823. //%
  824. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  825. //% NSUInteger count = _dictionary.count;
  826. //% if (count == 0) {
  827. //% return 0;
  828. //% }
  829. //%
  830. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  831. //% GPBDataType keyDataType = field.mapKeyDataType;
  832. //% __block size_t result = 0;
  833. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  834. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u##,
  835. //% BOOL *stop) {
  836. //% #pragma unused(stop)
  837. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  838. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  839. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  840. //% }];
  841. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  842. //% result += tagSize * count;
  843. //% return result;
  844. //%}
  845. //%
  846. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  847. //% asField:(GPBFieldDescriptor *)field {
  848. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  849. //% GPBDataType keyDataType = field.mapKeyDataType;
  850. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  851. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  852. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u,
  853. //% BOOL *stop) {
  854. //% #pragma unused(stop)
  855. //% // Write the tag.
  856. //% [outputStream writeInt32NoTag:tag];
  857. //% // Write the size of the message.
  858. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  859. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  860. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  861. //% // Write the fields.
  862. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  863. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  864. //% }];
  865. //%}
  866. //%
  867. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  868. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  869. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  870. //%}
  871. //%
  872. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  873. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) {
  874. //% #pragma unused(stop)
  875. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
  876. //% }];
  877. //%}
  878. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  879. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
  880. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
  881. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  882. //% if (otherDictionary) {
  883. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  884. //% if (_autocreator) {
  885. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  886. //% }
  887. //% }
  888. //%}
  889. //%
  890. //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
  891. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
  892. //% if (_autocreator) {
  893. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  894. //% }
  895. //%}
  896. //%
  897. //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  898. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  899. //%}
  900. //%
  901. //%- (void)removeAll {
  902. //% [_dictionary removeAllObjects];
  903. //%}
  904. //
  905. // Custom Generation for Bool keys
  906. //
  907. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  908. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
  909. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  910. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
  911. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
  912. //%#pragma mark - Bool -> VALUE_NAME
  913. //%
  914. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  915. //% @package
  916. //% VALUE_TYPE _values[2];
  917. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  918. //%
  919. //%+ (instancetype)dictionary {
  920. //% return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
  921. //%}
  922. //%
  923. //%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)VNAME_VAR
  924. //% ##VNAME$S## forKey:(BOOL)key {
  925. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  926. //% // on to get the type correct.
  927. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
  928. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  929. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  930. //%}
  931. //%
  932. //%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  933. //% ##VNAME$S## forKeys:(const BOOL [])keys
  934. //% ##VNAME$S## count:(NSUInteger)count {
  935. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  936. //% // on to get the type correct.
  937. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
  938. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  939. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  940. //%}
  941. //%
  942. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  943. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  944. //% // on to get the type correct.
  945. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  946. //%}
  947. //%
  948. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  949. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  950. //%}
  951. //%
  952. //%- (instancetype)init {
  953. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  954. //%}
  955. //%
  956. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  957. //%
  958. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  959. //% #pragma unused(numItems)
  960. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  961. //%}
  962. //%
  963. //%BOOL_DICT_DEALLOC##HELPER()
  964. //%
  965. //%- (instancetype)copyWithZone:(NSZone *)zone {
  966. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  967. //%}
  968. //%
  969. //%- (BOOL)isEqual:(id)other {
  970. //% if (self == other) {
  971. //% return YES;
  972. //% }
  973. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  974. //% return NO;
  975. //% }
  976. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  977. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  978. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  979. //% return NO;
  980. //% }
  981. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  982. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  983. //% return NO;
  984. //% }
  985. //% return YES;
  986. //%}
  987. //%
  988. //%- (NSUInteger)hash {
  989. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  990. //%}
  991. //%
  992. //%- (NSString *)description {
  993. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  994. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  995. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  996. //% }
  997. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  998. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  999. //% }
  1000. //% [result appendString:@" }"];
  1001. //% return result;
  1002. //%}
  1003. //%
  1004. //%- (NSUInteger)count {
  1005. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  1006. //%}
  1007. //%
  1008. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
  1009. //%
  1010. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  1011. //%
  1012. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1013. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1014. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  1015. //% }
  1016. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  1017. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  1018. //% }
  1019. //%}
  1020. //%
  1021. //%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
  1022. //% (void (^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  1023. //% BOOL stop = NO;
  1024. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1025. //% block(NO, _values[0], &stop);
  1026. //% }
  1027. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  1028. //% block(YES, _values[1], &stop);
  1029. //% }
  1030. //%}
  1031. //%
  1032. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1033. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1034. //% NSUInteger count = 0;
  1035. //% size_t result = 0;
  1036. //% for (int i = 0; i < 2; ++i) {
  1037. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1038. //% ++count;
  1039. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1040. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1041. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1042. //% }
  1043. //% }
  1044. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1045. //% result += tagSize * count;
  1046. //% return result;
  1047. //%}
  1048. //%
  1049. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1050. //% asField:(GPBFieldDescriptor *)field {
  1051. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1052. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1053. //% for (int i = 0; i < 2; ++i) {
  1054. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1055. //% // Write the tag.
  1056. //% [outputStream writeInt32NoTag:tag];
  1057. //% // Write the size of the message.
  1058. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1059. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1060. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1061. //% // Write the fields.
  1062. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1063. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1064. //% }
  1065. //% }
  1066. //%}
  1067. //%
  1068. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1069. //%
  1070. //%@end
  1071. //%
  1072. //
  1073. // Helpers for PODs
  1074. //
  1075. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1076. //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
  1077. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1078. //% if (wrapped && value) {
  1079. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1080. //% }
  1081. //% return (wrapped != NULL);
  1082. //%}
  1083. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1084. //%@(VALUE)
  1085. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1086. //%[VALUE unsignedIntValue]
  1087. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1088. //%[VALUE intValue]
  1089. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1090. //%[VALUE unsignedLongLongValue]
  1091. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1092. //%[VALUE longLongValue]
  1093. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1094. //%[VALUE boolValue]
  1095. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1096. //%[VALUE floatValue]
  1097. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1098. //%[VALUE doubleValue]
  1099. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1100. //%[VALUE intValue]
  1101. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1102. //%[NSString stringWithFormat:@"%u", VALUE]
  1103. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1104. //%[NSString stringWithFormat:@"%d", VALUE]
  1105. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1106. //%[NSString stringWithFormat:@"%llu", VALUE]
  1107. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1108. //%[NSString stringWithFormat:@"%lld", VALUE]
  1109. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1110. //%(VALUE ? @"true" : @"false")
  1111. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1112. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1113. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1114. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1115. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1116. //%@(VALUE)
  1117. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1118. //%NSNumber *
  1119. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1120. //%VAL1 != VAL2
  1121. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1122. // Empty
  1123. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1124. // Empty
  1125. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1126. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1127. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1128. // Empty
  1129. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1130. // Empty
  1131. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1132. // Empty
  1133. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1134. // Empty
  1135. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1136. // Empty
  1137. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1138. // Empty
  1139. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1140. // Empty
  1141. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1142. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1143. //% forKey:(GPBGenericValue *)key
  1144. //% keyDataType:(GPBDataType)keyDataType {
  1145. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1146. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1147. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1148. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1149. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1150. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1151. //% [outputStream release];
  1152. //% return data;
  1153. //%}
  1154. //%
  1155. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1156. //%value##VALUE_NAME
  1157. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1158. // Empty
  1159. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1160. // Empty
  1161. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1162. //% BOOL _valueSet[2];
  1163. //%
  1164. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1165. //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
  1166. //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys
  1167. //% ##VALUE_NAME$S## count:(NSUInteger)count {
  1168. //% self = [super init];
  1169. //% if (self) {
  1170. //% for (NSUInteger i = 0; i < count; ++i) {
  1171. //% int idx = keys[i] ? 1 : 0;
  1172. //% _values[idx] = values[i];
  1173. //% _valueSet[idx] = YES;
  1174. //% }
  1175. //% }
  1176. //% return self;
  1177. //%}
  1178. //%
  1179. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1180. //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
  1181. //% if (self) {
  1182. //% if (dictionary) {
  1183. //% for (int i = 0; i < 2; ++i) {
  1184. //% if (dictionary->_valueSet[i]) {
  1185. //% _values[i] = dictionary->_values[i];
  1186. //% _valueSet[i] = YES;
  1187. //% }
  1188. //% }
  1189. //% }
  1190. //% }
  1191. //% return self;
  1192. //%}
  1193. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1194. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1195. //%- (void)dealloc {
  1196. //% NSAssert(!_autocreator,
  1197. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1198. //% [self class], _autocreator);
  1199. //% [super dealloc];
  1200. //%}
  1201. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1202. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1203. //%BOOL_DICT_HASPOD(IDX, REF)
  1204. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1205. //%REF##_valueSet[IDX]
  1206. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
  1207. //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
  1208. //% int idx = (key ? 1 : 0);
  1209. //% if (_valueSet[idx]) {
  1210. //% if (value) {
  1211. //% *value = _values[idx];
  1212. //% }
  1213. //% return YES;
  1214. //% }
  1215. //% return NO;
  1216. //%}
  1217. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1218. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1219. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1220. //% int idx = (key->valueBool ? 1 : 0);
  1221. //% _values[idx] = value->value##VALUE_NAME;
  1222. //% _valueSet[idx] = YES;
  1223. //%}
  1224. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1225. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1226. //% if (otherDictionary) {
  1227. //% for (int i = 0; i < 2; ++i) {
  1228. //% if (otherDictionary->_valueSet[i]) {
  1229. //% _valueSet[i] = YES;
  1230. //% _values[i] = otherDictionary->_values[i];
  1231. //% }
  1232. //% }
  1233. //% if (_autocreator) {
  1234. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1235. //% }
  1236. //% }
  1237. //%}
  1238. //%
  1239. //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
  1240. //% int idx = (key ? 1 : 0);
  1241. //% _values[idx] = value;
  1242. //% _valueSet[idx] = YES;
  1243. //% if (_autocreator) {
  1244. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1245. //% }
  1246. //%}
  1247. //%
  1248. //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
  1249. //% _valueSet[aKey ? 1 : 0] = NO;
  1250. //%}
  1251. //%
  1252. //%- (void)removeAll {
  1253. //% _valueSet[0] = NO;
  1254. //% _valueSet[1] = NO;
  1255. //%}
  1256. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1257. //%STR_FORMAT_##VALUE_NAME()
  1258. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1259. //%%u
  1260. //%PDDM-DEFINE STR_FORMAT_Int32()
  1261. //%%d
  1262. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1263. //%%llu
  1264. //%PDDM-DEFINE STR_FORMAT_Int64()
  1265. //%%lld
  1266. //%PDDM-DEFINE STR_FORMAT_Bool()
  1267. //%%d
  1268. //%PDDM-DEFINE STR_FORMAT_Float()
  1269. //%%f
  1270. //%PDDM-DEFINE STR_FORMAT_Double()
  1271. //%%lf
  1272. //
  1273. // Helpers for Objects
  1274. //
  1275. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1276. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1277. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1278. //% return result;
  1279. //%}
  1280. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1281. //%VALUE
  1282. //%PDDM-DEFINE UNWRAPString(VALUE)
  1283. //%VALUE
  1284. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1285. //%VALUE
  1286. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1287. //%VALUE
  1288. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1289. //%VALUE
  1290. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1291. //%ENUM_TYPEOBJECT_##TYPE()
  1292. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1293. //%NSString *
  1294. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1295. //%id ##
  1296. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1297. //%![VAL1 isEqual:VAL2]
  1298. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1299. //%- (BOOL)isInitialized {
  1300. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1301. //% if (!msg.initialized) {
  1302. //% return NO;
  1303. //% }
  1304. //% }
  1305. //% return YES;
  1306. //%}
  1307. //%
  1308. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1309. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1310. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1311. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1312. //% GPBMessage *msg,
  1313. //% BOOL *stop) {
  1314. //% #pragma unused(stop)
  1315. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1316. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1317. //% [copiedMsg release];
  1318. //% }];
  1319. //% return newDict;
  1320. //%}
  1321. //%
  1322. //%
  1323. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1324. //%- (BOOL)isInitialized {
  1325. //% if (_values[0] && ![_values[0] isInitialized]) {
  1326. //% return NO;
  1327. //% }
  1328. //% if (_values[1] && ![_values[1] isInitialized]) {
  1329. //% return NO;
  1330. //% }
  1331. //% return YES;
  1332. //%}
  1333. //%
  1334. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1335. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1336. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1337. //% for (int i = 0; i < 2; ++i) {
  1338. //% if (_values[i] != nil) {
  1339. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1340. //% }
  1341. //% }
  1342. //% return newDict;
  1343. //%}
  1344. //%
  1345. //%
  1346. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1347. // Empty
  1348. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1349. //%valueString
  1350. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1351. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1352. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1353. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1354. //%##EXTRA_INDENT$S## }
  1355. //%
  1356. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1357. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1358. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1359. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1360. //%##EXTRA_INDENT$S## }
  1361. //%
  1362. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1363. // Empty
  1364. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1365. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1366. //% forKeys:(const BOOL [])keys
  1367. //% count:(NSUInteger)count {
  1368. //% self = [super init];
  1369. //% if (self) {
  1370. //% for (NSUInteger i = 0; i < count; ++i) {
  1371. //% if (!objects[i]) {
  1372. //% [NSException raise:NSInvalidArgumentException
  1373. //% format:@"Attempting to add nil object to a Dictionary"];
  1374. //% }
  1375. //% int idx = keys[i] ? 1 : 0;
  1376. //% [_values[idx] release];
  1377. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1378. //% }
  1379. //% }
  1380. //% return self;
  1381. //%}
  1382. //%
  1383. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1384. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1385. //% if (self) {
  1386. //% if (dictionary) {
  1387. //% _values[0] = [dictionary->_values[0] retain];
  1388. //% _values[1] = [dictionary->_values[1] retain];
  1389. //% }
  1390. //% }
  1391. //% return self;
  1392. //%}
  1393. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1394. //%- (void)dealloc {
  1395. //% NSAssert(!_autocreator,
  1396. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1397. //% [self class], _autocreator);
  1398. //% [_values[0] release];
  1399. //% [_values[1] release];
  1400. //% [super dealloc];
  1401. //%}
  1402. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1403. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1404. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1405. //%REF##_values[IDX] != nil
  1406. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
  1407. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1408. //% return _values[key ? 1 : 0];
  1409. //%}
  1410. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1411. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1412. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1413. //% int idx = (key->valueBool ? 1 : 0);
  1414. //% [_values[idx] release];
  1415. //% _values[idx] = [value->valueString retain];
  1416. //%}
  1417. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1418. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1419. //% if (otherDictionary) {
  1420. //% for (int i = 0; i < 2; ++i) {
  1421. //% if (otherDictionary->_values[i] != nil) {
  1422. //% [_values[i] release];
  1423. //% _values[i] = [otherDictionary->_values[i] retain];
  1424. //% }
  1425. //% }
  1426. //% if (_autocreator) {
  1427. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1428. //% }
  1429. //% }
  1430. //%}
  1431. //%
  1432. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1433. //% if (!object) {
  1434. //% [NSException raise:NSInvalidArgumentException
  1435. //% format:@"Attempting to add nil object to a Dictionary"];
  1436. //% }
  1437. //% int idx = (key ? 1 : 0);
  1438. //% [_values[idx] release];
  1439. //% _values[idx] = [object retain];
  1440. //% if (_autocreator) {
  1441. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1442. //% }
  1443. //%}
  1444. //%
  1445. //%- (void)removeObjectForKey:(BOOL)aKey {
  1446. //% int idx = (aKey ? 1 : 0);
  1447. //% [_values[idx] release];
  1448. //% _values[idx] = nil;
  1449. //%}
  1450. //%
  1451. //%- (void)removeAll {
  1452. //% for (int i = 0; i < 2; ++i) {
  1453. //% [_values[i] release];
  1454. //% _values[i] = nil;
  1455. //% }
  1456. //%}
  1457. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1458. //%%@
  1459. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1460. // This block of code is generated, do not edit it directly.
  1461. #pragma mark - UInt32 -> UInt32
  1462. @implementation GPBUInt32UInt32Dictionary {
  1463. @package
  1464. NSMutableDictionary *_dictionary;
  1465. }
  1466. + (instancetype)dictionary {
  1467. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  1468. }
  1469. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  1470. forKey:(uint32_t)key {
  1471. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  1472. // on to get the type correct.
  1473. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  1474. forKeys:&key
  1475. count:1] autorelease];
  1476. }
  1477. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  1478. forKeys:(const uint32_t [])keys
  1479. count:(NSUInteger)count {
  1480. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  1481. // on to get the type correct.
  1482. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
  1483. forKeys:keys
  1484. count:count] autorelease];
  1485. }
  1486. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1487. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1488. // on to get the type correct.
  1489. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1490. }
  1491. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1492. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1493. }
  1494. - (instancetype)init {
  1495. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1496. }
  1497. - (instancetype)initWithUInt32s:(const uint32_t [])values
  1498. forKeys:(const uint32_t [])keys
  1499. count:(NSUInteger)count {
  1500. self = [super init];
  1501. if (self) {
  1502. _dictionary = [[NSMutableDictionary alloc] init];
  1503. if (count && values && keys) {
  1504. for (NSUInteger i = 0; i < count; ++i) {
  1505. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1506. }
  1507. }
  1508. }
  1509. return self;
  1510. }
  1511. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1512. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  1513. if (self) {
  1514. if (dictionary) {
  1515. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1516. }
  1517. }
  1518. return self;
  1519. }
  1520. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1521. #pragma unused(numItems)
  1522. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1523. }
  1524. - (void)dealloc {
  1525. NSAssert(!_autocreator,
  1526. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1527. [self class], _autocreator);
  1528. [_dictionary release];
  1529. [super dealloc];
  1530. }
  1531. - (instancetype)copyWithZone:(NSZone *)zone {
  1532. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1533. }
  1534. - (BOOL)isEqual:(id)other {
  1535. if (self == other) {
  1536. return YES;
  1537. }
  1538. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1539. return NO;
  1540. }
  1541. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1542. return [_dictionary isEqual:otherDictionary->_dictionary];
  1543. }
  1544. - (NSUInteger)hash {
  1545. return _dictionary.count;
  1546. }
  1547. - (NSString *)description {
  1548. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1549. }
  1550. - (NSUInteger)count {
  1551. return _dictionary.count;
  1552. }
  1553. - (void)enumerateKeysAndUInt32sUsingBlock:
  1554. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1555. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1556. NSNumber *aValue,
  1557. BOOL *stop) {
  1558. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1559. }];
  1560. }
  1561. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1562. NSUInteger count = _dictionary.count;
  1563. if (count == 0) {
  1564. return 0;
  1565. }
  1566. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1567. GPBDataType keyDataType = field.mapKeyDataType;
  1568. __block size_t result = 0;
  1569. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1570. NSNumber *aValue,
  1571. BOOL *stop) {
  1572. #pragma unused(stop)
  1573. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1574. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1575. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1576. }];
  1577. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1578. result += tagSize * count;
  1579. return result;
  1580. }
  1581. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1582. asField:(GPBFieldDescriptor *)field {
  1583. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1584. GPBDataType keyDataType = field.mapKeyDataType;
  1585. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1586. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1587. NSNumber *aValue,
  1588. BOOL *stop) {
  1589. #pragma unused(stop)
  1590. // Write the tag.
  1591. [outputStream writeInt32NoTag:tag];
  1592. // Write the size of the message.
  1593. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1594. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1595. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1596. // Write the fields.
  1597. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1598. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1599. }];
  1600. }
  1601. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1602. forGPBGenericValueKey:(GPBGenericValue *)key {
  1603. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1604. }
  1605. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1606. [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1607. #pragma unused(stop)
  1608. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1609. }];
  1610. }
  1611. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
  1612. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1613. if (wrapped && value) {
  1614. *value = [wrapped unsignedIntValue];
  1615. }
  1616. return (wrapped != NULL);
  1617. }
  1618. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1619. if (otherDictionary) {
  1620. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1621. if (_autocreator) {
  1622. GPBAutocreatedDictionaryModified(_autocreator, self);
  1623. }
  1624. }
  1625. }
  1626. - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
  1627. [_dictionary setObject:@(value) forKey:@(key)];
  1628. if (_autocreator) {
  1629. GPBAutocreatedDictionaryModified(_autocreator, self);
  1630. }
  1631. }
  1632. - (void)removeUInt32ForKey:(uint32_t)aKey {
  1633. [_dictionary removeObjectForKey:@(aKey)];
  1634. }
  1635. - (void)removeAll {
  1636. [_dictionary removeAllObjects];
  1637. }
  1638. @end
  1639. #pragma mark - UInt32 -> Int32
  1640. @implementation GPBUInt32Int32Dictionary {
  1641. @package
  1642. NSMutableDictionary *_dictionary;
  1643. }
  1644. + (instancetype)dictionary {
  1645. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  1646. }
  1647. + (instancetype)dictionaryWithInt32:(int32_t)value
  1648. forKey:(uint32_t)key {
  1649. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  1650. // on to get the type correct.
  1651. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
  1652. forKeys:&key
  1653. count:1] autorelease];
  1654. }
  1655. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  1656. forKeys:(const uint32_t [])keys
  1657. count:(NSUInteger)count {
  1658. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  1659. // on to get the type correct.
  1660. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:values
  1661. forKeys:keys
  1662. count:count] autorelease];
  1663. }
  1664. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1665. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1666. // on to get the type correct.
  1667. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1668. }
  1669. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1670. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1671. }
  1672. - (instancetype)init {
  1673. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1674. }
  1675. - (instancetype)initWithInt32s:(const int32_t [])values
  1676. forKeys:(const uint32_t [])keys
  1677. count:(NSUInteger)count {
  1678. self = [super init];
  1679. if (self) {
  1680. _dictionary = [[NSMutableDictionary alloc] init];
  1681. if (count && values && keys) {
  1682. for (NSUInteger i = 0; i < count; ++i) {
  1683. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1684. }
  1685. }
  1686. }
  1687. return self;
  1688. }
  1689. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1690. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  1691. if (self) {
  1692. if (dictionary) {
  1693. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1694. }
  1695. }
  1696. return self;
  1697. }
  1698. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1699. #pragma unused(numItems)
  1700. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1701. }
  1702. - (void)dealloc {
  1703. NSAssert(!_autocreator,
  1704. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1705. [self class], _autocreator);
  1706. [_dictionary release];
  1707. [super dealloc];
  1708. }
  1709. - (instancetype)copyWithZone:(NSZone *)zone {
  1710. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1711. }
  1712. - (BOOL)isEqual:(id)other {
  1713. if (self == other) {
  1714. return YES;
  1715. }
  1716. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1717. return NO;
  1718. }
  1719. GPBUInt32Int32Dictionary *otherDictionary = other;
  1720. return [_dictionary isEqual:otherDictionary->_dictionary];
  1721. }
  1722. - (NSUInteger)hash {
  1723. return _dictionary.count;
  1724. }
  1725. - (NSString *)description {
  1726. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1727. }
  1728. - (NSUInteger)count {
  1729. return _dictionary.count;
  1730. }
  1731. - (void)enumerateKeysAndInt32sUsingBlock:
  1732. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1733. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1734. NSNumber *aValue,
  1735. BOOL *stop) {
  1736. block([aKey unsignedIntValue], [aValue intValue], stop);
  1737. }];
  1738. }
  1739. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1740. NSUInteger count = _dictionary.count;
  1741. if (count == 0) {
  1742. return 0;
  1743. }
  1744. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1745. GPBDataType keyDataType = field.mapKeyDataType;
  1746. __block size_t result = 0;
  1747. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1748. NSNumber *aValue,
  1749. BOOL *stop) {
  1750. #pragma unused(stop)
  1751. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1752. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1753. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1754. }];
  1755. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1756. result += tagSize * count;
  1757. return result;
  1758. }
  1759. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1760. asField:(GPBFieldDescriptor *)field {
  1761. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1762. GPBDataType keyDataType = field.mapKeyDataType;
  1763. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1764. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1765. NSNumber *aValue,
  1766. BOOL *stop) {
  1767. #pragma unused(stop)
  1768. // Write the tag.
  1769. [outputStream writeInt32NoTag:tag];
  1770. // Write the size of the message.
  1771. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1772. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1773. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1774. // Write the fields.
  1775. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1776. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1777. }];
  1778. }
  1779. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1780. forGPBGenericValueKey:(GPBGenericValue *)key {
  1781. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1782. }
  1783. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1784. [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1785. #pragma unused(stop)
  1786. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1787. }];
  1788. }
  1789. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
  1790. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1791. if (wrapped && value) {
  1792. *value = [wrapped intValue];
  1793. }
  1794. return (wrapped != NULL);
  1795. }
  1796. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1797. if (otherDictionary) {
  1798. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1799. if (_autocreator) {
  1800. GPBAutocreatedDictionaryModified(_autocreator, self);
  1801. }
  1802. }
  1803. }
  1804. - (void)setInt32:(int32_t)value forKey:(uint32_t)key {
  1805. [_dictionary setObject:@(value) forKey:@(key)];
  1806. if (_autocreator) {
  1807. GPBAutocreatedDictionaryModified(_autocreator, self);
  1808. }
  1809. }
  1810. - (void)removeInt32ForKey:(uint32_t)aKey {
  1811. [_dictionary removeObjectForKey:@(aKey)];
  1812. }
  1813. - (void)removeAll {
  1814. [_dictionary removeAllObjects];
  1815. }
  1816. @end
  1817. #pragma mark - UInt32 -> UInt64
  1818. @implementation GPBUInt32UInt64Dictionary {
  1819. @package
  1820. NSMutableDictionary *_dictionary;
  1821. }
  1822. + (instancetype)dictionary {
  1823. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  1824. }
  1825. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  1826. forKey:(uint32_t)key {
  1827. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  1828. // on to get the type correct.
  1829. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  1830. forKeys:&key
  1831. count:1] autorelease];
  1832. }
  1833. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  1834. forKeys:(const uint32_t [])keys
  1835. count:(NSUInteger)count {
  1836. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  1837. // on to get the type correct.
  1838. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
  1839. forKeys:keys
  1840. count:count] autorelease];
  1841. }
  1842. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1843. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1844. // on to get the type correct.
  1845. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1846. }
  1847. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1848. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1849. }
  1850. - (instancetype)init {
  1851. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1852. }
  1853. - (instancetype)initWithUInt64s:(const uint64_t [])values
  1854. forKeys:(const uint32_t [])keys
  1855. count:(NSUInteger)count {
  1856. self = [super init];
  1857. if (self) {
  1858. _dictionary = [[NSMutableDictionary alloc] init];
  1859. if (count && values && keys) {
  1860. for (NSUInteger i = 0; i < count; ++i) {
  1861. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1862. }
  1863. }
  1864. }
  1865. return self;
  1866. }
  1867. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1868. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  1869. if (self) {
  1870. if (dictionary) {
  1871. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1872. }
  1873. }
  1874. return self;
  1875. }
  1876. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1877. #pragma unused(numItems)
  1878. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1879. }
  1880. - (void)dealloc {
  1881. NSAssert(!_autocreator,
  1882. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1883. [self class], _autocreator);
  1884. [_dictionary release];
  1885. [super dealloc];
  1886. }
  1887. - (instancetype)copyWithZone:(NSZone *)zone {
  1888. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1889. }
  1890. - (BOOL)isEqual:(id)other {
  1891. if (self == other) {
  1892. return YES;
  1893. }
  1894. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1895. return NO;
  1896. }
  1897. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1898. return [_dictionary isEqual:otherDictionary->_dictionary];
  1899. }
  1900. - (NSUInteger)hash {
  1901. return _dictionary.count;
  1902. }
  1903. - (NSString *)description {
  1904. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1905. }
  1906. - (NSUInteger)count {
  1907. return _dictionary.count;
  1908. }
  1909. - (void)enumerateKeysAndUInt64sUsingBlock:
  1910. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1911. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1912. NSNumber *aValue,
  1913. BOOL *stop) {
  1914. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1915. }];
  1916. }
  1917. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1918. NSUInteger count = _dictionary.count;
  1919. if (count == 0) {
  1920. return 0;
  1921. }
  1922. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1923. GPBDataType keyDataType = field.mapKeyDataType;
  1924. __block size_t result = 0;
  1925. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1926. NSNumber *aValue,
  1927. BOOL *stop) {
  1928. #pragma unused(stop)
  1929. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1930. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1931. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1932. }];
  1933. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1934. result += tagSize * count;
  1935. return result;
  1936. }
  1937. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1938. asField:(GPBFieldDescriptor *)field {
  1939. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1940. GPBDataType keyDataType = field.mapKeyDataType;
  1941. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1942. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1943. NSNumber *aValue,
  1944. BOOL *stop) {
  1945. #pragma unused(stop)
  1946. // Write the tag.
  1947. [outputStream writeInt32NoTag:tag];
  1948. // Write the size of the message.
  1949. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1950. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1951. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1952. // Write the fields.
  1953. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1954. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1955. }];
  1956. }
  1957. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1958. forGPBGenericValueKey:(GPBGenericValue *)key {
  1959. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1960. }
  1961. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1962. [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1963. #pragma unused(stop)
  1964. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1965. }];
  1966. }
  1967. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
  1968. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1969. if (wrapped && value) {
  1970. *value = [wrapped unsignedLongLongValue];
  1971. }
  1972. return (wrapped != NULL);
  1973. }
  1974. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1975. if (otherDictionary) {
  1976. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1977. if (_autocreator) {
  1978. GPBAutocreatedDictionaryModified(_autocreator, self);
  1979. }
  1980. }
  1981. }
  1982. - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
  1983. [_dictionary setObject:@(value) forKey:@(key)];
  1984. if (_autocreator) {
  1985. GPBAutocreatedDictionaryModified(_autocreator, self);
  1986. }
  1987. }
  1988. - (void)removeUInt64ForKey:(uint32_t)aKey {
  1989. [_dictionary removeObjectForKey:@(aKey)];
  1990. }
  1991. - (void)removeAll {
  1992. [_dictionary removeAllObjects];
  1993. }
  1994. @end
  1995. #pragma mark - UInt32 -> Int64
  1996. @implementation GPBUInt32Int64Dictionary {
  1997. @package
  1998. NSMutableDictionary *_dictionary;
  1999. }
  2000. + (instancetype)dictionary {
  2001. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  2002. }
  2003. + (instancetype)dictionaryWithInt64:(int64_t)value
  2004. forKey:(uint32_t)key {
  2005. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  2006. // on to get the type correct.
  2007. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
  2008. forKeys:&key
  2009. count:1] autorelease];
  2010. }
  2011. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  2012. forKeys:(const uint32_t [])keys
  2013. count:(NSUInteger)count {
  2014. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  2015. // on to get the type correct.
  2016. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:values
  2017. forKeys:keys
  2018. count:count] autorelease];
  2019. }
  2020. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2021. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2022. // on to get the type correct.
  2023. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2024. }
  2025. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2026. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2027. }
  2028. - (instancetype)init {
  2029. return [self initWithInt64s:NULL forKeys:NULL count:0];
  2030. }
  2031. - (instancetype)initWithInt64s:(const int64_t [])values
  2032. forKeys:(const uint32_t [])keys
  2033. count:(NSUInteger)count {
  2034. self = [super init];
  2035. if (self) {
  2036. _dictionary = [[NSMutableDictionary alloc] init];
  2037. if (count && values && keys) {
  2038. for (NSUInteger i = 0; i < count; ++i) {
  2039. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2040. }
  2041. }
  2042. }
  2043. return self;
  2044. }
  2045. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2046. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  2047. if (self) {
  2048. if (dictionary) {
  2049. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2050. }
  2051. }
  2052. return self;
  2053. }
  2054. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2055. #pragma unused(numItems)
  2056. return [self initWithInt64s:NULL forKeys:NULL count:0];
  2057. }
  2058. - (void)dealloc {
  2059. NSAssert(!_autocreator,
  2060. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2061. [self class], _autocreator);
  2062. [_dictionary release];
  2063. [super dealloc];
  2064. }
  2065. - (instancetype)copyWithZone:(NSZone *)zone {
  2066. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2067. }
  2068. - (BOOL)isEqual:(id)other {
  2069. if (self == other) {
  2070. return YES;
  2071. }
  2072. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2073. return NO;
  2074. }
  2075. GPBUInt32Int64Dictionary *otherDictionary = other;
  2076. return [_dictionary isEqual:otherDictionary->_dictionary];
  2077. }
  2078. - (NSUInteger)hash {
  2079. return _dictionary.count;
  2080. }
  2081. - (NSString *)description {
  2082. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2083. }
  2084. - (NSUInteger)count {
  2085. return _dictionary.count;
  2086. }
  2087. - (void)enumerateKeysAndInt64sUsingBlock:
  2088. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2089. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2090. NSNumber *aValue,
  2091. BOOL *stop) {
  2092. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2093. }];
  2094. }
  2095. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2096. NSUInteger count = _dictionary.count;
  2097. if (count == 0) {
  2098. return 0;
  2099. }
  2100. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2101. GPBDataType keyDataType = field.mapKeyDataType;
  2102. __block size_t result = 0;
  2103. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2104. NSNumber *aValue,
  2105. BOOL *stop) {
  2106. #pragma unused(stop)
  2107. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2108. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2109. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2110. }];
  2111. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2112. result += tagSize * count;
  2113. return result;
  2114. }
  2115. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2116. asField:(GPBFieldDescriptor *)field {
  2117. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2118. GPBDataType keyDataType = field.mapKeyDataType;
  2119. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2120. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2121. NSNumber *aValue,
  2122. BOOL *stop) {
  2123. #pragma unused(stop)
  2124. // Write the tag.
  2125. [outputStream writeInt32NoTag:tag];
  2126. // Write the size of the message.
  2127. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2128. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2129. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2130. // Write the fields.
  2131. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2132. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2133. }];
  2134. }
  2135. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2136. forGPBGenericValueKey:(GPBGenericValue *)key {
  2137. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2138. }
  2139. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2140. [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2141. #pragma unused(stop)
  2142. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2143. }];
  2144. }
  2145. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
  2146. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2147. if (wrapped && value) {
  2148. *value = [wrapped longLongValue];
  2149. }
  2150. return (wrapped != NULL);
  2151. }
  2152. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2153. if (otherDictionary) {
  2154. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2155. if (_autocreator) {
  2156. GPBAutocreatedDictionaryModified(_autocreator, self);
  2157. }
  2158. }
  2159. }
  2160. - (void)setInt64:(int64_t)value forKey:(uint32_t)key {
  2161. [_dictionary setObject:@(value) forKey:@(key)];
  2162. if (_autocreator) {
  2163. GPBAutocreatedDictionaryModified(_autocreator, self);
  2164. }
  2165. }
  2166. - (void)removeInt64ForKey:(uint32_t)aKey {
  2167. [_dictionary removeObjectForKey:@(aKey)];
  2168. }
  2169. - (void)removeAll {
  2170. [_dictionary removeAllObjects];
  2171. }
  2172. @end
  2173. #pragma mark - UInt32 -> Bool
  2174. @implementation GPBUInt32BoolDictionary {
  2175. @package
  2176. NSMutableDictionary *_dictionary;
  2177. }
  2178. + (instancetype)dictionary {
  2179. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  2180. }
  2181. + (instancetype)dictionaryWithBool:(BOOL)value
  2182. forKey:(uint32_t)key {
  2183. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  2184. // on to get the type correct.
  2185. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:&value
  2186. forKeys:&key
  2187. count:1] autorelease];
  2188. }
  2189. + (instancetype)dictionaryWithBools:(const BOOL [])values
  2190. forKeys:(const uint32_t [])keys
  2191. count:(NSUInteger)count {
  2192. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  2193. // on to get the type correct.
  2194. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:values
  2195. forKeys:keys
  2196. count:count] autorelease];
  2197. }
  2198. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2199. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2200. // on to get the type correct.
  2201. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2202. }
  2203. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2204. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2205. }
  2206. - (instancetype)init {
  2207. return [self initWithBools:NULL forKeys:NULL count:0];
  2208. }
  2209. - (instancetype)initWithBools:(const BOOL [])values
  2210. forKeys:(const uint32_t [])keys
  2211. count:(NSUInteger)count {
  2212. self = [super init];
  2213. if (self) {
  2214. _dictionary = [[NSMutableDictionary alloc] init];
  2215. if (count && values && keys) {
  2216. for (NSUInteger i = 0; i < count; ++i) {
  2217. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2218. }
  2219. }
  2220. }
  2221. return self;
  2222. }
  2223. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2224. self = [self initWithBools:NULL forKeys:NULL count:0];
  2225. if (self) {
  2226. if (dictionary) {
  2227. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2228. }
  2229. }
  2230. return self;
  2231. }
  2232. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2233. #pragma unused(numItems)
  2234. return [self initWithBools:NULL forKeys:NULL count:0];
  2235. }
  2236. - (void)dealloc {
  2237. NSAssert(!_autocreator,
  2238. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2239. [self class], _autocreator);
  2240. [_dictionary release];
  2241. [super dealloc];
  2242. }
  2243. - (instancetype)copyWithZone:(NSZone *)zone {
  2244. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2245. }
  2246. - (BOOL)isEqual:(id)other {
  2247. if (self == other) {
  2248. return YES;
  2249. }
  2250. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2251. return NO;
  2252. }
  2253. GPBUInt32BoolDictionary *otherDictionary = other;
  2254. return [_dictionary isEqual:otherDictionary->_dictionary];
  2255. }
  2256. - (NSUInteger)hash {
  2257. return _dictionary.count;
  2258. }
  2259. - (NSString *)description {
  2260. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2261. }
  2262. - (NSUInteger)count {
  2263. return _dictionary.count;
  2264. }
  2265. - (void)enumerateKeysAndBoolsUsingBlock:
  2266. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2267. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2268. NSNumber *aValue,
  2269. BOOL *stop) {
  2270. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2271. }];
  2272. }
  2273. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2274. NSUInteger count = _dictionary.count;
  2275. if (count == 0) {
  2276. return 0;
  2277. }
  2278. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2279. GPBDataType keyDataType = field.mapKeyDataType;
  2280. __block size_t result = 0;
  2281. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2282. NSNumber *aValue,
  2283. BOOL *stop) {
  2284. #pragma unused(stop)
  2285. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2286. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2287. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2288. }];
  2289. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2290. result += tagSize * count;
  2291. return result;
  2292. }
  2293. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2294. asField:(GPBFieldDescriptor *)field {
  2295. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2296. GPBDataType keyDataType = field.mapKeyDataType;
  2297. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2298. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2299. NSNumber *aValue,
  2300. BOOL *stop) {
  2301. #pragma unused(stop)
  2302. // Write the tag.
  2303. [outputStream writeInt32NoTag:tag];
  2304. // Write the size of the message.
  2305. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2306. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2307. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2308. // Write the fields.
  2309. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2310. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2311. }];
  2312. }
  2313. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2314. forGPBGenericValueKey:(GPBGenericValue *)key {
  2315. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2316. }
  2317. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2318. [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2319. #pragma unused(stop)
  2320. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2321. }];
  2322. }
  2323. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
  2324. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2325. if (wrapped && value) {
  2326. *value = [wrapped boolValue];
  2327. }
  2328. return (wrapped != NULL);
  2329. }
  2330. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2331. if (otherDictionary) {
  2332. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2333. if (_autocreator) {
  2334. GPBAutocreatedDictionaryModified(_autocreator, self);
  2335. }
  2336. }
  2337. }
  2338. - (void)setBool:(BOOL)value forKey:(uint32_t)key {
  2339. [_dictionary setObject:@(value) forKey:@(key)];
  2340. if (_autocreator) {
  2341. GPBAutocreatedDictionaryModified(_autocreator, self);
  2342. }
  2343. }
  2344. - (void)removeBoolForKey:(uint32_t)aKey {
  2345. [_dictionary removeObjectForKey:@(aKey)];
  2346. }
  2347. - (void)removeAll {
  2348. [_dictionary removeAllObjects];
  2349. }
  2350. @end
  2351. #pragma mark - UInt32 -> Float
  2352. @implementation GPBUInt32FloatDictionary {
  2353. @package
  2354. NSMutableDictionary *_dictionary;
  2355. }
  2356. + (instancetype)dictionary {
  2357. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  2358. }
  2359. + (instancetype)dictionaryWithFloat:(float)value
  2360. forKey:(uint32_t)key {
  2361. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  2362. // on to get the type correct.
  2363. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:&value
  2364. forKeys:&key
  2365. count:1] autorelease];
  2366. }
  2367. + (instancetype)dictionaryWithFloats:(const float [])values
  2368. forKeys:(const uint32_t [])keys
  2369. count:(NSUInteger)count {
  2370. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  2371. // on to get the type correct.
  2372. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:values
  2373. forKeys:keys
  2374. count:count] autorelease];
  2375. }
  2376. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2377. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2378. // on to get the type correct.
  2379. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2380. }
  2381. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2382. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2383. }
  2384. - (instancetype)init {
  2385. return [self initWithFloats:NULL forKeys:NULL count:0];
  2386. }
  2387. - (instancetype)initWithFloats:(const float [])values
  2388. forKeys:(const uint32_t [])keys
  2389. count:(NSUInteger)count {
  2390. self = [super init];
  2391. if (self) {
  2392. _dictionary = [[NSMutableDictionary alloc] init];
  2393. if (count && values && keys) {
  2394. for (NSUInteger i = 0; i < count; ++i) {
  2395. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2396. }
  2397. }
  2398. }
  2399. return self;
  2400. }
  2401. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2402. self = [self initWithFloats:NULL forKeys:NULL count:0];
  2403. if (self) {
  2404. if (dictionary) {
  2405. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2406. }
  2407. }
  2408. return self;
  2409. }
  2410. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2411. #pragma unused(numItems)
  2412. return [self initWithFloats:NULL forKeys:NULL count:0];
  2413. }
  2414. - (void)dealloc {
  2415. NSAssert(!_autocreator,
  2416. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2417. [self class], _autocreator);
  2418. [_dictionary release];
  2419. [super dealloc];
  2420. }
  2421. - (instancetype)copyWithZone:(NSZone *)zone {
  2422. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2423. }
  2424. - (BOOL)isEqual:(id)other {
  2425. if (self == other) {
  2426. return YES;
  2427. }
  2428. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2429. return NO;
  2430. }
  2431. GPBUInt32FloatDictionary *otherDictionary = other;
  2432. return [_dictionary isEqual:otherDictionary->_dictionary];
  2433. }
  2434. - (NSUInteger)hash {
  2435. return _dictionary.count;
  2436. }
  2437. - (NSString *)description {
  2438. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2439. }
  2440. - (NSUInteger)count {
  2441. return _dictionary.count;
  2442. }
  2443. - (void)enumerateKeysAndFloatsUsingBlock:
  2444. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2445. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2446. NSNumber *aValue,
  2447. BOOL *stop) {
  2448. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2449. }];
  2450. }
  2451. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2452. NSUInteger count = _dictionary.count;
  2453. if (count == 0) {
  2454. return 0;
  2455. }
  2456. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2457. GPBDataType keyDataType = field.mapKeyDataType;
  2458. __block size_t result = 0;
  2459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2460. NSNumber *aValue,
  2461. BOOL *stop) {
  2462. #pragma unused(stop)
  2463. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2464. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2465. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2466. }];
  2467. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2468. result += tagSize * count;
  2469. return result;
  2470. }
  2471. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2472. asField:(GPBFieldDescriptor *)field {
  2473. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2474. GPBDataType keyDataType = field.mapKeyDataType;
  2475. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2476. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2477. NSNumber *aValue,
  2478. BOOL *stop) {
  2479. #pragma unused(stop)
  2480. // Write the tag.
  2481. [outputStream writeInt32NoTag:tag];
  2482. // Write the size of the message.
  2483. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2484. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2485. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2486. // Write the fields.
  2487. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2488. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2489. }];
  2490. }
  2491. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2492. forGPBGenericValueKey:(GPBGenericValue *)key {
  2493. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2494. }
  2495. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2496. [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2497. #pragma unused(stop)
  2498. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2499. }];
  2500. }
  2501. - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
  2502. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2503. if (wrapped && value) {
  2504. *value = [wrapped floatValue];
  2505. }
  2506. return (wrapped != NULL);
  2507. }
  2508. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2509. if (otherDictionary) {
  2510. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2511. if (_autocreator) {
  2512. GPBAutocreatedDictionaryModified(_autocreator, self);
  2513. }
  2514. }
  2515. }
  2516. - (void)setFloat:(float)value forKey:(uint32_t)key {
  2517. [_dictionary setObject:@(value) forKey:@(key)];
  2518. if (_autocreator) {
  2519. GPBAutocreatedDictionaryModified(_autocreator, self);
  2520. }
  2521. }
  2522. - (void)removeFloatForKey:(uint32_t)aKey {
  2523. [_dictionary removeObjectForKey:@(aKey)];
  2524. }
  2525. - (void)removeAll {
  2526. [_dictionary removeAllObjects];
  2527. }
  2528. @end
  2529. #pragma mark - UInt32 -> Double
  2530. @implementation GPBUInt32DoubleDictionary {
  2531. @package
  2532. NSMutableDictionary *_dictionary;
  2533. }
  2534. + (instancetype)dictionary {
  2535. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  2536. }
  2537. + (instancetype)dictionaryWithDouble:(double)value
  2538. forKey:(uint32_t)key {
  2539. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  2540. // on to get the type correct.
  2541. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
  2542. forKeys:&key
  2543. count:1] autorelease];
  2544. }
  2545. + (instancetype)dictionaryWithDoubles:(const double [])values
  2546. forKeys:(const uint32_t [])keys
  2547. count:(NSUInteger)count {
  2548. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  2549. // on to get the type correct.
  2550. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:values
  2551. forKeys:keys
  2552. count:count] autorelease];
  2553. }
  2554. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2555. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2556. // on to get the type correct.
  2557. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2558. }
  2559. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2560. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2561. }
  2562. - (instancetype)init {
  2563. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2564. }
  2565. - (instancetype)initWithDoubles:(const double [])values
  2566. forKeys:(const uint32_t [])keys
  2567. count:(NSUInteger)count {
  2568. self = [super init];
  2569. if (self) {
  2570. _dictionary = [[NSMutableDictionary alloc] init];
  2571. if (count && values && keys) {
  2572. for (NSUInteger i = 0; i < count; ++i) {
  2573. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2574. }
  2575. }
  2576. }
  2577. return self;
  2578. }
  2579. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2580. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  2581. if (self) {
  2582. if (dictionary) {
  2583. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2584. }
  2585. }
  2586. return self;
  2587. }
  2588. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2589. #pragma unused(numItems)
  2590. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2591. }
  2592. - (void)dealloc {
  2593. NSAssert(!_autocreator,
  2594. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2595. [self class], _autocreator);
  2596. [_dictionary release];
  2597. [super dealloc];
  2598. }
  2599. - (instancetype)copyWithZone:(NSZone *)zone {
  2600. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2601. }
  2602. - (BOOL)isEqual:(id)other {
  2603. if (self == other) {
  2604. return YES;
  2605. }
  2606. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2607. return NO;
  2608. }
  2609. GPBUInt32DoubleDictionary *otherDictionary = other;
  2610. return [_dictionary isEqual:otherDictionary->_dictionary];
  2611. }
  2612. - (NSUInteger)hash {
  2613. return _dictionary.count;
  2614. }
  2615. - (NSString *)description {
  2616. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2617. }
  2618. - (NSUInteger)count {
  2619. return _dictionary.count;
  2620. }
  2621. - (void)enumerateKeysAndDoublesUsingBlock:
  2622. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2623. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2624. NSNumber *aValue,
  2625. BOOL *stop) {
  2626. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2627. }];
  2628. }
  2629. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2630. NSUInteger count = _dictionary.count;
  2631. if (count == 0) {
  2632. return 0;
  2633. }
  2634. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2635. GPBDataType keyDataType = field.mapKeyDataType;
  2636. __block size_t result = 0;
  2637. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2638. NSNumber *aValue,
  2639. BOOL *stop) {
  2640. #pragma unused(stop)
  2641. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2642. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2643. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2644. }];
  2645. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2646. result += tagSize * count;
  2647. return result;
  2648. }
  2649. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2650. asField:(GPBFieldDescriptor *)field {
  2651. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2652. GPBDataType keyDataType = field.mapKeyDataType;
  2653. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2654. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2655. NSNumber *aValue,
  2656. BOOL *stop) {
  2657. #pragma unused(stop)
  2658. // Write the tag.
  2659. [outputStream writeInt32NoTag:tag];
  2660. // Write the size of the message.
  2661. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2662. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2663. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2664. // Write the fields.
  2665. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2666. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2667. }];
  2668. }
  2669. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2670. forGPBGenericValueKey:(GPBGenericValue *)key {
  2671. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2672. }
  2673. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2674. [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2675. #pragma unused(stop)
  2676. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2677. }];
  2678. }
  2679. - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
  2680. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2681. if (wrapped && value) {
  2682. *value = [wrapped doubleValue];
  2683. }
  2684. return (wrapped != NULL);
  2685. }
  2686. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2687. if (otherDictionary) {
  2688. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2689. if (_autocreator) {
  2690. GPBAutocreatedDictionaryModified(_autocreator, self);
  2691. }
  2692. }
  2693. }
  2694. - (void)setDouble:(double)value forKey:(uint32_t)key {
  2695. [_dictionary setObject:@(value) forKey:@(key)];
  2696. if (_autocreator) {
  2697. GPBAutocreatedDictionaryModified(_autocreator, self);
  2698. }
  2699. }
  2700. - (void)removeDoubleForKey:(uint32_t)aKey {
  2701. [_dictionary removeObjectForKey:@(aKey)];
  2702. }
  2703. - (void)removeAll {
  2704. [_dictionary removeAllObjects];
  2705. }
  2706. @end
  2707. #pragma mark - UInt32 -> Enum
  2708. @implementation GPBUInt32EnumDictionary {
  2709. @package
  2710. NSMutableDictionary *_dictionary;
  2711. GPBEnumValidationFunc _validationFunc;
  2712. }
  2713. @synthesize validationFunc = _validationFunc;
  2714. + (instancetype)dictionary {
  2715. return [[[self alloc] initWithValidationFunction:NULL
  2716. rawValues:NULL
  2717. forKeys:NULL
  2718. count:0] autorelease];
  2719. }
  2720. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2721. return [[[self alloc] initWithValidationFunction:func
  2722. rawValues:NULL
  2723. forKeys:NULL
  2724. count:0] autorelease];
  2725. }
  2726. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2727. rawValue:(int32_t)rawValue
  2728. forKey:(uint32_t)key {
  2729. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2730. // on to get the type correct.
  2731. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2732. rawValues:&rawValue
  2733. forKeys:&key
  2734. count:1] autorelease];
  2735. }
  2736. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2737. rawValues:(const int32_t [])rawValues
  2738. forKeys:(const uint32_t [])keys
  2739. count:(NSUInteger)count {
  2740. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2741. // on to get the type correct.
  2742. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2743. rawValues:rawValues
  2744. forKeys:keys
  2745. count:count] autorelease];
  2746. }
  2747. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2748. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2749. // on to get the type correct.
  2750. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2751. }
  2752. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2753. capacity:(NSUInteger)numItems {
  2754. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2755. }
  2756. - (instancetype)init {
  2757. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2758. }
  2759. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2760. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2761. }
  2762. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2763. rawValues:(const int32_t [])rawValues
  2764. forKeys:(const uint32_t [])keys
  2765. count:(NSUInteger)count {
  2766. self = [super init];
  2767. if (self) {
  2768. _dictionary = [[NSMutableDictionary alloc] init];
  2769. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2770. if (count && rawValues && keys) {
  2771. for (NSUInteger i = 0; i < count; ++i) {
  2772. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2773. }
  2774. }
  2775. }
  2776. return self;
  2777. }
  2778. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2779. self = [self initWithValidationFunction:dictionary.validationFunc
  2780. rawValues:NULL
  2781. forKeys:NULL
  2782. count:0];
  2783. if (self) {
  2784. if (dictionary) {
  2785. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2786. }
  2787. }
  2788. return self;
  2789. }
  2790. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2791. capacity:(NSUInteger)numItems {
  2792. #pragma unused(numItems)
  2793. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2794. }
  2795. - (void)dealloc {
  2796. NSAssert(!_autocreator,
  2797. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2798. [self class], _autocreator);
  2799. [_dictionary release];
  2800. [super dealloc];
  2801. }
  2802. - (instancetype)copyWithZone:(NSZone *)zone {
  2803. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2804. }
  2805. - (BOOL)isEqual:(id)other {
  2806. if (self == other) {
  2807. return YES;
  2808. }
  2809. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2810. return NO;
  2811. }
  2812. GPBUInt32EnumDictionary *otherDictionary = other;
  2813. return [_dictionary isEqual:otherDictionary->_dictionary];
  2814. }
  2815. - (NSUInteger)hash {
  2816. return _dictionary.count;
  2817. }
  2818. - (NSString *)description {
  2819. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2820. }
  2821. - (NSUInteger)count {
  2822. return _dictionary.count;
  2823. }
  2824. - (void)enumerateKeysAndRawValuesUsingBlock:
  2825. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2826. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2827. NSNumber *aValue,
  2828. BOOL *stop) {
  2829. block([aKey unsignedIntValue], [aValue intValue], stop);
  2830. }];
  2831. }
  2832. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2833. NSUInteger count = _dictionary.count;
  2834. if (count == 0) {
  2835. return 0;
  2836. }
  2837. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2838. GPBDataType keyDataType = field.mapKeyDataType;
  2839. __block size_t result = 0;
  2840. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2841. NSNumber *aValue,
  2842. BOOL *stop) {
  2843. #pragma unused(stop)
  2844. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2845. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2846. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2847. }];
  2848. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2849. result += tagSize * count;
  2850. return result;
  2851. }
  2852. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2853. asField:(GPBFieldDescriptor *)field {
  2854. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2855. GPBDataType keyDataType = field.mapKeyDataType;
  2856. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2857. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2858. NSNumber *aValue,
  2859. BOOL *stop) {
  2860. #pragma unused(stop)
  2861. // Write the tag.
  2862. [outputStream writeInt32NoTag:tag];
  2863. // Write the size of the message.
  2864. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2865. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2866. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2867. // Write the fields.
  2868. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2869. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2870. }];
  2871. }
  2872. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2873. forKey:(GPBGenericValue *)key
  2874. keyDataType:(GPBDataType)keyDataType {
  2875. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2876. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2877. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2878. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2879. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2880. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2881. [outputStream release];
  2882. return data;
  2883. }
  2884. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2885. forGPBGenericValueKey:(GPBGenericValue *)key {
  2886. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2887. }
  2888. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2889. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2890. #pragma unused(stop)
  2891. block([NSString stringWithFormat:@"%u", key], @(value));
  2892. }];
  2893. }
  2894. - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
  2895. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2896. if (wrapped && value) {
  2897. int32_t result = [wrapped intValue];
  2898. if (!_validationFunc(result)) {
  2899. result = kGPBUnrecognizedEnumeratorValue;
  2900. }
  2901. *value = result;
  2902. }
  2903. return (wrapped != NULL);
  2904. }
  2905. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
  2906. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2907. if (wrapped && rawValue) {
  2908. *rawValue = [wrapped intValue];
  2909. }
  2910. return (wrapped != NULL);
  2911. }
  2912. - (void)enumerateKeysAndEnumsUsingBlock:
  2913. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2914. GPBEnumValidationFunc func = _validationFunc;
  2915. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2916. NSNumber *aValue,
  2917. BOOL *stop) {
  2918. int32_t unwrapped = [aValue intValue];
  2919. if (!func(unwrapped)) {
  2920. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2921. }
  2922. block([aKey unsignedIntValue], unwrapped, stop);
  2923. }];
  2924. }
  2925. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2926. if (otherDictionary) {
  2927. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2928. if (_autocreator) {
  2929. GPBAutocreatedDictionaryModified(_autocreator, self);
  2930. }
  2931. }
  2932. }
  2933. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2934. [_dictionary setObject:@(value) forKey:@(key)];
  2935. if (_autocreator) {
  2936. GPBAutocreatedDictionaryModified(_autocreator, self);
  2937. }
  2938. }
  2939. - (void)removeEnumForKey:(uint32_t)aKey {
  2940. [_dictionary removeObjectForKey:@(aKey)];
  2941. }
  2942. - (void)removeAll {
  2943. [_dictionary removeAllObjects];
  2944. }
  2945. - (void)setEnum:(int32_t)value forKey:(uint32_t)key {
  2946. if (!_validationFunc(value)) {
  2947. [NSException raise:NSInvalidArgumentException
  2948. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2949. value];
  2950. }
  2951. [_dictionary setObject:@(value) forKey:@(key)];
  2952. if (_autocreator) {
  2953. GPBAutocreatedDictionaryModified(_autocreator, self);
  2954. }
  2955. }
  2956. @end
  2957. #pragma mark - UInt32 -> Object
  2958. @implementation GPBUInt32ObjectDictionary {
  2959. @package
  2960. NSMutableDictionary *_dictionary;
  2961. }
  2962. + (instancetype)dictionary {
  2963. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  2964. }
  2965. + (instancetype)dictionaryWithObject:(id)object
  2966. forKey:(uint32_t)key {
  2967. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2968. // on to get the type correct.
  2969. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  2970. forKeys:&key
  2971. count:1] autorelease];
  2972. }
  2973. + (instancetype)dictionaryWithObjects:(const id [])objects
  2974. forKeys:(const uint32_t [])keys
  2975. count:(NSUInteger)count {
  2976. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2977. // on to get the type correct.
  2978. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  2979. forKeys:keys
  2980. count:count] autorelease];
  2981. }
  2982. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2983. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2984. // on to get the type correct.
  2985. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2986. }
  2987. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2988. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2989. }
  2990. - (instancetype)init {
  2991. return [self initWithObjects:NULL forKeys:NULL count:0];
  2992. }
  2993. - (instancetype)initWithObjects:(const id [])objects
  2994. forKeys:(const uint32_t [])keys
  2995. count:(NSUInteger)count {
  2996. self = [super init];
  2997. if (self) {
  2998. _dictionary = [[NSMutableDictionary alloc] init];
  2999. if (count && objects && keys) {
  3000. for (NSUInteger i = 0; i < count; ++i) {
  3001. if (!objects[i]) {
  3002. [NSException raise:NSInvalidArgumentException
  3003. format:@"Attempting to add nil object to a Dictionary"];
  3004. }
  3005. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  3006. }
  3007. }
  3008. }
  3009. return self;
  3010. }
  3011. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  3012. self = [self initWithObjects:NULL forKeys:NULL count:0];
  3013. if (self) {
  3014. if (dictionary) {
  3015. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3016. }
  3017. }
  3018. return self;
  3019. }
  3020. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3021. #pragma unused(numItems)
  3022. return [self initWithObjects:NULL forKeys:NULL count:0];
  3023. }
  3024. - (void)dealloc {
  3025. NSAssert(!_autocreator,
  3026. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3027. [self class], _autocreator);
  3028. [_dictionary release];
  3029. [super dealloc];
  3030. }
  3031. - (instancetype)copyWithZone:(NSZone *)zone {
  3032. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  3033. }
  3034. - (BOOL)isEqual:(id)other {
  3035. if (self == other) {
  3036. return YES;
  3037. }
  3038. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  3039. return NO;
  3040. }
  3041. GPBUInt32ObjectDictionary *otherDictionary = other;
  3042. return [_dictionary isEqual:otherDictionary->_dictionary];
  3043. }
  3044. - (NSUInteger)hash {
  3045. return _dictionary.count;
  3046. }
  3047. - (NSString *)description {
  3048. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3049. }
  3050. - (NSUInteger)count {
  3051. return _dictionary.count;
  3052. }
  3053. - (void)enumerateKeysAndObjectsUsingBlock:
  3054. (void (^)(uint32_t key, id object, BOOL *stop))block {
  3055. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3056. id aObject,
  3057. BOOL *stop) {
  3058. block([aKey unsignedIntValue], aObject, stop);
  3059. }];
  3060. }
  3061. - (BOOL)isInitialized {
  3062. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  3063. if (!msg.initialized) {
  3064. return NO;
  3065. }
  3066. }
  3067. return YES;
  3068. }
  3069. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  3070. GPBUInt32ObjectDictionary *newDict =
  3071. [[GPBUInt32ObjectDictionary alloc] init];
  3072. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  3073. GPBMessage *msg,
  3074. BOOL *stop) {
  3075. #pragma unused(stop)
  3076. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3077. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  3078. [copiedMsg release];
  3079. }];
  3080. return newDict;
  3081. }
  3082. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3083. NSUInteger count = _dictionary.count;
  3084. if (count == 0) {
  3085. return 0;
  3086. }
  3087. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3088. GPBDataType keyDataType = field.mapKeyDataType;
  3089. __block size_t result = 0;
  3090. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3091. id aObject,
  3092. BOOL *stop) {
  3093. #pragma unused(stop)
  3094. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3095. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3096. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3097. }];
  3098. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3099. result += tagSize * count;
  3100. return result;
  3101. }
  3102. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3103. asField:(GPBFieldDescriptor *)field {
  3104. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3105. GPBDataType keyDataType = field.mapKeyDataType;
  3106. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3107. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3108. id aObject,
  3109. BOOL *stop) {
  3110. #pragma unused(stop)
  3111. // Write the tag.
  3112. [outputStream writeInt32NoTag:tag];
  3113. // Write the size of the message.
  3114. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3115. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3116. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3117. // Write the fields.
  3118. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3119. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  3120. }];
  3121. }
  3122. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3123. forGPBGenericValueKey:(GPBGenericValue *)key {
  3124. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3125. }
  3126. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3127. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3128. #pragma unused(stop)
  3129. block([NSString stringWithFormat:@"%u", key], object);
  3130. }];
  3131. }
  3132. - (id)objectForKey:(uint32_t)key {
  3133. id result = [_dictionary objectForKey:@(key)];
  3134. return result;
  3135. }
  3136. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3137. if (otherDictionary) {
  3138. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3139. if (_autocreator) {
  3140. GPBAutocreatedDictionaryModified(_autocreator, self);
  3141. }
  3142. }
  3143. }
  3144. - (void)setObject:(id)object forKey:(uint32_t)key {
  3145. if (!object) {
  3146. [NSException raise:NSInvalidArgumentException
  3147. format:@"Attempting to add nil object to a Dictionary"];
  3148. }
  3149. [_dictionary setObject:object forKey:@(key)];
  3150. if (_autocreator) {
  3151. GPBAutocreatedDictionaryModified(_autocreator, self);
  3152. }
  3153. }
  3154. - (void)removeObjectForKey:(uint32_t)aKey {
  3155. [_dictionary removeObjectForKey:@(aKey)];
  3156. }
  3157. - (void)removeAll {
  3158. [_dictionary removeAllObjects];
  3159. }
  3160. @end
  3161. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3162. // This block of code is generated, do not edit it directly.
  3163. #pragma mark - Int32 -> UInt32
  3164. @implementation GPBInt32UInt32Dictionary {
  3165. @package
  3166. NSMutableDictionary *_dictionary;
  3167. }
  3168. + (instancetype)dictionary {
  3169. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  3170. }
  3171. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  3172. forKey:(int32_t)key {
  3173. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  3174. // on to get the type correct.
  3175. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  3176. forKeys:&key
  3177. count:1] autorelease];
  3178. }
  3179. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  3180. forKeys:(const int32_t [])keys
  3181. count:(NSUInteger)count {
  3182. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  3183. // on to get the type correct.
  3184. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
  3185. forKeys:keys
  3186. count:count] autorelease];
  3187. }
  3188. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3189. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3190. // on to get the type correct.
  3191. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3192. }
  3193. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3194. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3195. }
  3196. - (instancetype)init {
  3197. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  3198. }
  3199. - (instancetype)initWithUInt32s:(const uint32_t [])values
  3200. forKeys:(const int32_t [])keys
  3201. count:(NSUInteger)count {
  3202. self = [super init];
  3203. if (self) {
  3204. _dictionary = [[NSMutableDictionary alloc] init];
  3205. if (count && values && keys) {
  3206. for (NSUInteger i = 0; i < count; ++i) {
  3207. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3208. }
  3209. }
  3210. }
  3211. return self;
  3212. }
  3213. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3214. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  3215. if (self) {
  3216. if (dictionary) {
  3217. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3218. }
  3219. }
  3220. return self;
  3221. }
  3222. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3223. #pragma unused(numItems)
  3224. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  3225. }
  3226. - (void)dealloc {
  3227. NSAssert(!_autocreator,
  3228. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3229. [self class], _autocreator);
  3230. [_dictionary release];
  3231. [super dealloc];
  3232. }
  3233. - (instancetype)copyWithZone:(NSZone *)zone {
  3234. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3235. }
  3236. - (BOOL)isEqual:(id)other {
  3237. if (self == other) {
  3238. return YES;
  3239. }
  3240. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3241. return NO;
  3242. }
  3243. GPBInt32UInt32Dictionary *otherDictionary = other;
  3244. return [_dictionary isEqual:otherDictionary->_dictionary];
  3245. }
  3246. - (NSUInteger)hash {
  3247. return _dictionary.count;
  3248. }
  3249. - (NSString *)description {
  3250. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3251. }
  3252. - (NSUInteger)count {
  3253. return _dictionary.count;
  3254. }
  3255. - (void)enumerateKeysAndUInt32sUsingBlock:
  3256. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3258. NSNumber *aValue,
  3259. BOOL *stop) {
  3260. block([aKey intValue], [aValue unsignedIntValue], stop);
  3261. }];
  3262. }
  3263. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3264. NSUInteger count = _dictionary.count;
  3265. if (count == 0) {
  3266. return 0;
  3267. }
  3268. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3269. GPBDataType keyDataType = field.mapKeyDataType;
  3270. __block size_t result = 0;
  3271. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3272. NSNumber *aValue,
  3273. BOOL *stop) {
  3274. #pragma unused(stop)
  3275. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3276. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3277. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3278. }];
  3279. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3280. result += tagSize * count;
  3281. return result;
  3282. }
  3283. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3284. asField:(GPBFieldDescriptor *)field {
  3285. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3286. GPBDataType keyDataType = field.mapKeyDataType;
  3287. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3288. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3289. NSNumber *aValue,
  3290. BOOL *stop) {
  3291. #pragma unused(stop)
  3292. // Write the tag.
  3293. [outputStream writeInt32NoTag:tag];
  3294. // Write the size of the message.
  3295. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3296. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3297. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3298. // Write the fields.
  3299. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3300. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3301. }];
  3302. }
  3303. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3304. forGPBGenericValueKey:(GPBGenericValue *)key {
  3305. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3306. }
  3307. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3308. [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3309. #pragma unused(stop)
  3310. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3311. }];
  3312. }
  3313. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
  3314. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3315. if (wrapped && value) {
  3316. *value = [wrapped unsignedIntValue];
  3317. }
  3318. return (wrapped != NULL);
  3319. }
  3320. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3321. if (otherDictionary) {
  3322. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3323. if (_autocreator) {
  3324. GPBAutocreatedDictionaryModified(_autocreator, self);
  3325. }
  3326. }
  3327. }
  3328. - (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
  3329. [_dictionary setObject:@(value) forKey:@(key)];
  3330. if (_autocreator) {
  3331. GPBAutocreatedDictionaryModified(_autocreator, self);
  3332. }
  3333. }
  3334. - (void)removeUInt32ForKey:(int32_t)aKey {
  3335. [_dictionary removeObjectForKey:@(aKey)];
  3336. }
  3337. - (void)removeAll {
  3338. [_dictionary removeAllObjects];
  3339. }
  3340. @end
  3341. #pragma mark - Int32 -> Int32
  3342. @implementation GPBInt32Int32Dictionary {
  3343. @package
  3344. NSMutableDictionary *_dictionary;
  3345. }
  3346. + (instancetype)dictionary {
  3347. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  3348. }
  3349. + (instancetype)dictionaryWithInt32:(int32_t)value
  3350. forKey:(int32_t)key {
  3351. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  3352. // on to get the type correct.
  3353. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
  3354. forKeys:&key
  3355. count:1] autorelease];
  3356. }
  3357. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  3358. forKeys:(const int32_t [])keys
  3359. count:(NSUInteger)count {
  3360. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  3361. // on to get the type correct.
  3362. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:values
  3363. forKeys:keys
  3364. count:count] autorelease];
  3365. }
  3366. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3367. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3368. // on to get the type correct.
  3369. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3370. }
  3371. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3372. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3373. }
  3374. - (instancetype)init {
  3375. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3376. }
  3377. - (instancetype)initWithInt32s:(const int32_t [])values
  3378. forKeys:(const int32_t [])keys
  3379. count:(NSUInteger)count {
  3380. self = [super init];
  3381. if (self) {
  3382. _dictionary = [[NSMutableDictionary alloc] init];
  3383. if (count && values && keys) {
  3384. for (NSUInteger i = 0; i < count; ++i) {
  3385. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3386. }
  3387. }
  3388. }
  3389. return self;
  3390. }
  3391. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3392. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  3393. if (self) {
  3394. if (dictionary) {
  3395. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3396. }
  3397. }
  3398. return self;
  3399. }
  3400. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3401. #pragma unused(numItems)
  3402. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3403. }
  3404. - (void)dealloc {
  3405. NSAssert(!_autocreator,
  3406. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3407. [self class], _autocreator);
  3408. [_dictionary release];
  3409. [super dealloc];
  3410. }
  3411. - (instancetype)copyWithZone:(NSZone *)zone {
  3412. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3413. }
  3414. - (BOOL)isEqual:(id)other {
  3415. if (self == other) {
  3416. return YES;
  3417. }
  3418. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3419. return NO;
  3420. }
  3421. GPBInt32Int32Dictionary *otherDictionary = other;
  3422. return [_dictionary isEqual:otherDictionary->_dictionary];
  3423. }
  3424. - (NSUInteger)hash {
  3425. return _dictionary.count;
  3426. }
  3427. - (NSString *)description {
  3428. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3429. }
  3430. - (NSUInteger)count {
  3431. return _dictionary.count;
  3432. }
  3433. - (void)enumerateKeysAndInt32sUsingBlock:
  3434. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3435. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3436. NSNumber *aValue,
  3437. BOOL *stop) {
  3438. block([aKey intValue], [aValue intValue], stop);
  3439. }];
  3440. }
  3441. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3442. NSUInteger count = _dictionary.count;
  3443. if (count == 0) {
  3444. return 0;
  3445. }
  3446. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3447. GPBDataType keyDataType = field.mapKeyDataType;
  3448. __block size_t result = 0;
  3449. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3450. NSNumber *aValue,
  3451. BOOL *stop) {
  3452. #pragma unused(stop)
  3453. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3454. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3455. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3456. }];
  3457. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3458. result += tagSize * count;
  3459. return result;
  3460. }
  3461. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3462. asField:(GPBFieldDescriptor *)field {
  3463. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3464. GPBDataType keyDataType = field.mapKeyDataType;
  3465. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3466. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3467. NSNumber *aValue,
  3468. BOOL *stop) {
  3469. #pragma unused(stop)
  3470. // Write the tag.
  3471. [outputStream writeInt32NoTag:tag];
  3472. // Write the size of the message.
  3473. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3474. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3475. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3476. // Write the fields.
  3477. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3478. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3479. }];
  3480. }
  3481. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3482. forGPBGenericValueKey:(GPBGenericValue *)key {
  3483. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3484. }
  3485. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3486. [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3487. #pragma unused(stop)
  3488. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3489. }];
  3490. }
  3491. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
  3492. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3493. if (wrapped && value) {
  3494. *value = [wrapped intValue];
  3495. }
  3496. return (wrapped != NULL);
  3497. }
  3498. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3499. if (otherDictionary) {
  3500. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3501. if (_autocreator) {
  3502. GPBAutocreatedDictionaryModified(_autocreator, self);
  3503. }
  3504. }
  3505. }
  3506. - (void)setInt32:(int32_t)value forKey:(int32_t)key {
  3507. [_dictionary setObject:@(value) forKey:@(key)];
  3508. if (_autocreator) {
  3509. GPBAutocreatedDictionaryModified(_autocreator, self);
  3510. }
  3511. }
  3512. - (void)removeInt32ForKey:(int32_t)aKey {
  3513. [_dictionary removeObjectForKey:@(aKey)];
  3514. }
  3515. - (void)removeAll {
  3516. [_dictionary removeAllObjects];
  3517. }
  3518. @end
  3519. #pragma mark - Int32 -> UInt64
  3520. @implementation GPBInt32UInt64Dictionary {
  3521. @package
  3522. NSMutableDictionary *_dictionary;
  3523. }
  3524. + (instancetype)dictionary {
  3525. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  3526. }
  3527. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  3528. forKey:(int32_t)key {
  3529. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  3530. // on to get the type correct.
  3531. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  3532. forKeys:&key
  3533. count:1] autorelease];
  3534. }
  3535. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  3536. forKeys:(const int32_t [])keys
  3537. count:(NSUInteger)count {
  3538. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  3539. // on to get the type correct.
  3540. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
  3541. forKeys:keys
  3542. count:count] autorelease];
  3543. }
  3544. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3545. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3546. // on to get the type correct.
  3547. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3548. }
  3549. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3550. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3551. }
  3552. - (instancetype)init {
  3553. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3554. }
  3555. - (instancetype)initWithUInt64s:(const uint64_t [])values
  3556. forKeys:(const int32_t [])keys
  3557. count:(NSUInteger)count {
  3558. self = [super init];
  3559. if (self) {
  3560. _dictionary = [[NSMutableDictionary alloc] init];
  3561. if (count && values && keys) {
  3562. for (NSUInteger i = 0; i < count; ++i) {
  3563. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3564. }
  3565. }
  3566. }
  3567. return self;
  3568. }
  3569. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3570. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  3571. if (self) {
  3572. if (dictionary) {
  3573. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3574. }
  3575. }
  3576. return self;
  3577. }
  3578. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3579. #pragma unused(numItems)
  3580. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3581. }
  3582. - (void)dealloc {
  3583. NSAssert(!_autocreator,
  3584. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3585. [self class], _autocreator);
  3586. [_dictionary release];
  3587. [super dealloc];
  3588. }
  3589. - (instancetype)copyWithZone:(NSZone *)zone {
  3590. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3591. }
  3592. - (BOOL)isEqual:(id)other {
  3593. if (self == other) {
  3594. return YES;
  3595. }
  3596. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3597. return NO;
  3598. }
  3599. GPBInt32UInt64Dictionary *otherDictionary = other;
  3600. return [_dictionary isEqual:otherDictionary->_dictionary];
  3601. }
  3602. - (NSUInteger)hash {
  3603. return _dictionary.count;
  3604. }
  3605. - (NSString *)description {
  3606. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3607. }
  3608. - (NSUInteger)count {
  3609. return _dictionary.count;
  3610. }
  3611. - (void)enumerateKeysAndUInt64sUsingBlock:
  3612. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3613. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3614. NSNumber *aValue,
  3615. BOOL *stop) {
  3616. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3617. }];
  3618. }
  3619. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3620. NSUInteger count = _dictionary.count;
  3621. if (count == 0) {
  3622. return 0;
  3623. }
  3624. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3625. GPBDataType keyDataType = field.mapKeyDataType;
  3626. __block size_t result = 0;
  3627. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3628. NSNumber *aValue,
  3629. BOOL *stop) {
  3630. #pragma unused(stop)
  3631. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3632. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3633. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3634. }];
  3635. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3636. result += tagSize * count;
  3637. return result;
  3638. }
  3639. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3640. asField:(GPBFieldDescriptor *)field {
  3641. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3642. GPBDataType keyDataType = field.mapKeyDataType;
  3643. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3644. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3645. NSNumber *aValue,
  3646. BOOL *stop) {
  3647. #pragma unused(stop)
  3648. // Write the tag.
  3649. [outputStream writeInt32NoTag:tag];
  3650. // Write the size of the message.
  3651. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3652. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3653. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3654. // Write the fields.
  3655. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3656. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3657. }];
  3658. }
  3659. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3660. forGPBGenericValueKey:(GPBGenericValue *)key {
  3661. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3662. }
  3663. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3664. [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3665. #pragma unused(stop)
  3666. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3667. }];
  3668. }
  3669. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
  3670. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3671. if (wrapped && value) {
  3672. *value = [wrapped unsignedLongLongValue];
  3673. }
  3674. return (wrapped != NULL);
  3675. }
  3676. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3677. if (otherDictionary) {
  3678. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3679. if (_autocreator) {
  3680. GPBAutocreatedDictionaryModified(_autocreator, self);
  3681. }
  3682. }
  3683. }
  3684. - (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
  3685. [_dictionary setObject:@(value) forKey:@(key)];
  3686. if (_autocreator) {
  3687. GPBAutocreatedDictionaryModified(_autocreator, self);
  3688. }
  3689. }
  3690. - (void)removeUInt64ForKey:(int32_t)aKey {
  3691. [_dictionary removeObjectForKey:@(aKey)];
  3692. }
  3693. - (void)removeAll {
  3694. [_dictionary removeAllObjects];
  3695. }
  3696. @end
  3697. #pragma mark - Int32 -> Int64
  3698. @implementation GPBInt32Int64Dictionary {
  3699. @package
  3700. NSMutableDictionary *_dictionary;
  3701. }
  3702. + (instancetype)dictionary {
  3703. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  3704. }
  3705. + (instancetype)dictionaryWithInt64:(int64_t)value
  3706. forKey:(int32_t)key {
  3707. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  3708. // on to get the type correct.
  3709. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
  3710. forKeys:&key
  3711. count:1] autorelease];
  3712. }
  3713. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  3714. forKeys:(const int32_t [])keys
  3715. count:(NSUInteger)count {
  3716. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  3717. // on to get the type correct.
  3718. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:values
  3719. forKeys:keys
  3720. count:count] autorelease];
  3721. }
  3722. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3723. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3724. // on to get the type correct.
  3725. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3726. }
  3727. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3728. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3729. }
  3730. - (instancetype)init {
  3731. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3732. }
  3733. - (instancetype)initWithInt64s:(const int64_t [])values
  3734. forKeys:(const int32_t [])keys
  3735. count:(NSUInteger)count {
  3736. self = [super init];
  3737. if (self) {
  3738. _dictionary = [[NSMutableDictionary alloc] init];
  3739. if (count && values && keys) {
  3740. for (NSUInteger i = 0; i < count; ++i) {
  3741. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3742. }
  3743. }
  3744. }
  3745. return self;
  3746. }
  3747. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3748. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  3749. if (self) {
  3750. if (dictionary) {
  3751. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3752. }
  3753. }
  3754. return self;
  3755. }
  3756. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3757. #pragma unused(numItems)
  3758. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3759. }
  3760. - (void)dealloc {
  3761. NSAssert(!_autocreator,
  3762. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3763. [self class], _autocreator);
  3764. [_dictionary release];
  3765. [super dealloc];
  3766. }
  3767. - (instancetype)copyWithZone:(NSZone *)zone {
  3768. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3769. }
  3770. - (BOOL)isEqual:(id)other {
  3771. if (self == other) {
  3772. return YES;
  3773. }
  3774. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3775. return NO;
  3776. }
  3777. GPBInt32Int64Dictionary *otherDictionary = other;
  3778. return [_dictionary isEqual:otherDictionary->_dictionary];
  3779. }
  3780. - (NSUInteger)hash {
  3781. return _dictionary.count;
  3782. }
  3783. - (NSString *)description {
  3784. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3785. }
  3786. - (NSUInteger)count {
  3787. return _dictionary.count;
  3788. }
  3789. - (void)enumerateKeysAndInt64sUsingBlock:
  3790. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3791. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3792. NSNumber *aValue,
  3793. BOOL *stop) {
  3794. block([aKey intValue], [aValue longLongValue], stop);
  3795. }];
  3796. }
  3797. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3798. NSUInteger count = _dictionary.count;
  3799. if (count == 0) {
  3800. return 0;
  3801. }
  3802. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3803. GPBDataType keyDataType = field.mapKeyDataType;
  3804. __block size_t result = 0;
  3805. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3806. NSNumber *aValue,
  3807. BOOL *stop) {
  3808. #pragma unused(stop)
  3809. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3810. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3811. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3812. }];
  3813. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3814. result += tagSize * count;
  3815. return result;
  3816. }
  3817. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3818. asField:(GPBFieldDescriptor *)field {
  3819. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3820. GPBDataType keyDataType = field.mapKeyDataType;
  3821. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3822. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3823. NSNumber *aValue,
  3824. BOOL *stop) {
  3825. #pragma unused(stop)
  3826. // Write the tag.
  3827. [outputStream writeInt32NoTag:tag];
  3828. // Write the size of the message.
  3829. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3830. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3831. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3832. // Write the fields.
  3833. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3834. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3835. }];
  3836. }
  3837. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3838. forGPBGenericValueKey:(GPBGenericValue *)key {
  3839. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3840. }
  3841. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3842. [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3843. #pragma unused(stop)
  3844. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3845. }];
  3846. }
  3847. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
  3848. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3849. if (wrapped && value) {
  3850. *value = [wrapped longLongValue];
  3851. }
  3852. return (wrapped != NULL);
  3853. }
  3854. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3855. if (otherDictionary) {
  3856. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3857. if (_autocreator) {
  3858. GPBAutocreatedDictionaryModified(_autocreator, self);
  3859. }
  3860. }
  3861. }
  3862. - (void)setInt64:(int64_t)value forKey:(int32_t)key {
  3863. [_dictionary setObject:@(value) forKey:@(key)];
  3864. if (_autocreator) {
  3865. GPBAutocreatedDictionaryModified(_autocreator, self);
  3866. }
  3867. }
  3868. - (void)removeInt64ForKey:(int32_t)aKey {
  3869. [_dictionary removeObjectForKey:@(aKey)];
  3870. }
  3871. - (void)removeAll {
  3872. [_dictionary removeAllObjects];
  3873. }
  3874. @end
  3875. #pragma mark - Int32 -> Bool
  3876. @implementation GPBInt32BoolDictionary {
  3877. @package
  3878. NSMutableDictionary *_dictionary;
  3879. }
  3880. + (instancetype)dictionary {
  3881. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  3882. }
  3883. + (instancetype)dictionaryWithBool:(BOOL)value
  3884. forKey:(int32_t)key {
  3885. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  3886. // on to get the type correct.
  3887. return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:&value
  3888. forKeys:&key
  3889. count:1] autorelease];
  3890. }
  3891. + (instancetype)dictionaryWithBools:(const BOOL [])values
  3892. forKeys:(const int32_t [])keys
  3893. count:(NSUInteger)count {
  3894. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  3895. // on to get the type correct.
  3896. return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:values
  3897. forKeys:keys
  3898. count:count] autorelease];
  3899. }
  3900. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3901. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3902. // on to get the type correct.
  3903. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3904. }
  3905. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3906. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3907. }
  3908. - (instancetype)init {
  3909. return [self initWithBools:NULL forKeys:NULL count:0];
  3910. }
  3911. - (instancetype)initWithBools:(const BOOL [])values
  3912. forKeys:(const int32_t [])keys
  3913. count:(NSUInteger)count {
  3914. self = [super init];
  3915. if (self) {
  3916. _dictionary = [[NSMutableDictionary alloc] init];
  3917. if (count && values && keys) {
  3918. for (NSUInteger i = 0; i < count; ++i) {
  3919. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3920. }
  3921. }
  3922. }
  3923. return self;
  3924. }
  3925. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3926. self = [self initWithBools:NULL forKeys:NULL count:0];
  3927. if (self) {
  3928. if (dictionary) {
  3929. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3930. }
  3931. }
  3932. return self;
  3933. }
  3934. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3935. #pragma unused(numItems)
  3936. return [self initWithBools:NULL forKeys:NULL count:0];
  3937. }
  3938. - (void)dealloc {
  3939. NSAssert(!_autocreator,
  3940. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3941. [self class], _autocreator);
  3942. [_dictionary release];
  3943. [super dealloc];
  3944. }
  3945. - (instancetype)copyWithZone:(NSZone *)zone {
  3946. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3947. }
  3948. - (BOOL)isEqual:(id)other {
  3949. if (self == other) {
  3950. return YES;
  3951. }
  3952. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3953. return NO;
  3954. }
  3955. GPBInt32BoolDictionary *otherDictionary = other;
  3956. return [_dictionary isEqual:otherDictionary->_dictionary];
  3957. }
  3958. - (NSUInteger)hash {
  3959. return _dictionary.count;
  3960. }
  3961. - (NSString *)description {
  3962. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3963. }
  3964. - (NSUInteger)count {
  3965. return _dictionary.count;
  3966. }
  3967. - (void)enumerateKeysAndBoolsUsingBlock:
  3968. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3969. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3970. NSNumber *aValue,
  3971. BOOL *stop) {
  3972. block([aKey intValue], [aValue boolValue], stop);
  3973. }];
  3974. }
  3975. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3976. NSUInteger count = _dictionary.count;
  3977. if (count == 0) {
  3978. return 0;
  3979. }
  3980. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3981. GPBDataType keyDataType = field.mapKeyDataType;
  3982. __block size_t result = 0;
  3983. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3984. NSNumber *aValue,
  3985. BOOL *stop) {
  3986. #pragma unused(stop)
  3987. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3988. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3989. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3990. }];
  3991. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3992. result += tagSize * count;
  3993. return result;
  3994. }
  3995. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3996. asField:(GPBFieldDescriptor *)field {
  3997. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3998. GPBDataType keyDataType = field.mapKeyDataType;
  3999. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4000. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4001. NSNumber *aValue,
  4002. BOOL *stop) {
  4003. #pragma unused(stop)
  4004. // Write the tag.
  4005. [outputStream writeInt32NoTag:tag];
  4006. // Write the size of the message.
  4007. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4008. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  4009. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4010. // Write the fields.
  4011. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4012. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  4013. }];
  4014. }
  4015. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4016. forGPBGenericValueKey:(GPBGenericValue *)key {
  4017. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  4018. }
  4019. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4020. [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  4021. #pragma unused(stop)
  4022. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  4023. }];
  4024. }
  4025. - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
  4026. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4027. if (wrapped && value) {
  4028. *value = [wrapped boolValue];
  4029. }
  4030. return (wrapped != NULL);
  4031. }
  4032. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  4033. if (otherDictionary) {
  4034. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4035. if (_autocreator) {
  4036. GPBAutocreatedDictionaryModified(_autocreator, self);
  4037. }
  4038. }
  4039. }
  4040. - (void)setBool:(BOOL)value forKey:(int32_t)key {
  4041. [_dictionary setObject:@(value) forKey:@(key)];
  4042. if (_autocreator) {
  4043. GPBAutocreatedDictionaryModified(_autocreator, self);
  4044. }
  4045. }
  4046. - (void)removeBoolForKey:(int32_t)aKey {
  4047. [_dictionary removeObjectForKey:@(aKey)];
  4048. }
  4049. - (void)removeAll {
  4050. [_dictionary removeAllObjects];
  4051. }
  4052. @end
  4053. #pragma mark - Int32 -> Float
  4054. @implementation GPBInt32FloatDictionary {
  4055. @package
  4056. NSMutableDictionary *_dictionary;
  4057. }
  4058. + (instancetype)dictionary {
  4059. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  4060. }
  4061. + (instancetype)dictionaryWithFloat:(float)value
  4062. forKey:(int32_t)key {
  4063. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  4064. // on to get the type correct.
  4065. return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:&value
  4066. forKeys:&key
  4067. count:1] autorelease];
  4068. }
  4069. + (instancetype)dictionaryWithFloats:(const float [])values
  4070. forKeys:(const int32_t [])keys
  4071. count:(NSUInteger)count {
  4072. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  4073. // on to get the type correct.
  4074. return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:values
  4075. forKeys:keys
  4076. count:count] autorelease];
  4077. }
  4078. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4079. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4080. // on to get the type correct.
  4081. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4082. }
  4083. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4084. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4085. }
  4086. - (instancetype)init {
  4087. return [self initWithFloats:NULL forKeys:NULL count:0];
  4088. }
  4089. - (instancetype)initWithFloats:(const float [])values
  4090. forKeys:(const int32_t [])keys
  4091. count:(NSUInteger)count {
  4092. self = [super init];
  4093. if (self) {
  4094. _dictionary = [[NSMutableDictionary alloc] init];
  4095. if (count && values && keys) {
  4096. for (NSUInteger i = 0; i < count; ++i) {
  4097. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4098. }
  4099. }
  4100. }
  4101. return self;
  4102. }
  4103. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4104. self = [self initWithFloats:NULL forKeys:NULL count:0];
  4105. if (self) {
  4106. if (dictionary) {
  4107. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4108. }
  4109. }
  4110. return self;
  4111. }
  4112. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4113. #pragma unused(numItems)
  4114. return [self initWithFloats:NULL forKeys:NULL count:0];
  4115. }
  4116. - (void)dealloc {
  4117. NSAssert(!_autocreator,
  4118. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4119. [self class], _autocreator);
  4120. [_dictionary release];
  4121. [super dealloc];
  4122. }
  4123. - (instancetype)copyWithZone:(NSZone *)zone {
  4124. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4125. }
  4126. - (BOOL)isEqual:(id)other {
  4127. if (self == other) {
  4128. return YES;
  4129. }
  4130. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4131. return NO;
  4132. }
  4133. GPBInt32FloatDictionary *otherDictionary = other;
  4134. return [_dictionary isEqual:otherDictionary->_dictionary];
  4135. }
  4136. - (NSUInteger)hash {
  4137. return _dictionary.count;
  4138. }
  4139. - (NSString *)description {
  4140. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4141. }
  4142. - (NSUInteger)count {
  4143. return _dictionary.count;
  4144. }
  4145. - (void)enumerateKeysAndFloatsUsingBlock:
  4146. (void (^)(int32_t key, float value, BOOL *stop))block {
  4147. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4148. NSNumber *aValue,
  4149. BOOL *stop) {
  4150. block([aKey intValue], [aValue floatValue], stop);
  4151. }];
  4152. }
  4153. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4154. NSUInteger count = _dictionary.count;
  4155. if (count == 0) {
  4156. return 0;
  4157. }
  4158. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4159. GPBDataType keyDataType = field.mapKeyDataType;
  4160. __block size_t result = 0;
  4161. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4162. NSNumber *aValue,
  4163. BOOL *stop) {
  4164. #pragma unused(stop)
  4165. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4166. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4167. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4168. }];
  4169. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4170. result += tagSize * count;
  4171. return result;
  4172. }
  4173. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4174. asField:(GPBFieldDescriptor *)field {
  4175. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4176. GPBDataType keyDataType = field.mapKeyDataType;
  4177. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4178. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4179. NSNumber *aValue,
  4180. BOOL *stop) {
  4181. #pragma unused(stop)
  4182. // Write the tag.
  4183. [outputStream writeInt32NoTag:tag];
  4184. // Write the size of the message.
  4185. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4186. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4187. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4188. // Write the fields.
  4189. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4190. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4191. }];
  4192. }
  4193. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4194. forGPBGenericValueKey:(GPBGenericValue *)key {
  4195. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4196. }
  4197. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4198. [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4199. #pragma unused(stop)
  4200. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4201. }];
  4202. }
  4203. - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
  4204. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4205. if (wrapped && value) {
  4206. *value = [wrapped floatValue];
  4207. }
  4208. return (wrapped != NULL);
  4209. }
  4210. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4211. if (otherDictionary) {
  4212. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4213. if (_autocreator) {
  4214. GPBAutocreatedDictionaryModified(_autocreator, self);
  4215. }
  4216. }
  4217. }
  4218. - (void)setFloat:(float)value forKey:(int32_t)key {
  4219. [_dictionary setObject:@(value) forKey:@(key)];
  4220. if (_autocreator) {
  4221. GPBAutocreatedDictionaryModified(_autocreator, self);
  4222. }
  4223. }
  4224. - (void)removeFloatForKey:(int32_t)aKey {
  4225. [_dictionary removeObjectForKey:@(aKey)];
  4226. }
  4227. - (void)removeAll {
  4228. [_dictionary removeAllObjects];
  4229. }
  4230. @end
  4231. #pragma mark - Int32 -> Double
  4232. @implementation GPBInt32DoubleDictionary {
  4233. @package
  4234. NSMutableDictionary *_dictionary;
  4235. }
  4236. + (instancetype)dictionary {
  4237. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  4238. }
  4239. + (instancetype)dictionaryWithDouble:(double)value
  4240. forKey:(int32_t)key {
  4241. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  4242. // on to get the type correct.
  4243. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
  4244. forKeys:&key
  4245. count:1] autorelease];
  4246. }
  4247. + (instancetype)dictionaryWithDoubles:(const double [])values
  4248. forKeys:(const int32_t [])keys
  4249. count:(NSUInteger)count {
  4250. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  4251. // on to get the type correct.
  4252. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:values
  4253. forKeys:keys
  4254. count:count] autorelease];
  4255. }
  4256. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4257. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4258. // on to get the type correct.
  4259. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4260. }
  4261. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4262. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4263. }
  4264. - (instancetype)init {
  4265. return [self initWithDoubles:NULL forKeys:NULL count:0];
  4266. }
  4267. - (instancetype)initWithDoubles:(const double [])values
  4268. forKeys:(const int32_t [])keys
  4269. count:(NSUInteger)count {
  4270. self = [super init];
  4271. if (self) {
  4272. _dictionary = [[NSMutableDictionary alloc] init];
  4273. if (count && values && keys) {
  4274. for (NSUInteger i = 0; i < count; ++i) {
  4275. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4276. }
  4277. }
  4278. }
  4279. return self;
  4280. }
  4281. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4282. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  4283. if (self) {
  4284. if (dictionary) {
  4285. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4286. }
  4287. }
  4288. return self;
  4289. }
  4290. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4291. #pragma unused(numItems)
  4292. return [self initWithDoubles:NULL forKeys:NULL count:0];
  4293. }
  4294. - (void)dealloc {
  4295. NSAssert(!_autocreator,
  4296. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4297. [self class], _autocreator);
  4298. [_dictionary release];
  4299. [super dealloc];
  4300. }
  4301. - (instancetype)copyWithZone:(NSZone *)zone {
  4302. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4303. }
  4304. - (BOOL)isEqual:(id)other {
  4305. if (self == other) {
  4306. return YES;
  4307. }
  4308. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4309. return NO;
  4310. }
  4311. GPBInt32DoubleDictionary *otherDictionary = other;
  4312. return [_dictionary isEqual:otherDictionary->_dictionary];
  4313. }
  4314. - (NSUInteger)hash {
  4315. return _dictionary.count;
  4316. }
  4317. - (NSString *)description {
  4318. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4319. }
  4320. - (NSUInteger)count {
  4321. return _dictionary.count;
  4322. }
  4323. - (void)enumerateKeysAndDoublesUsingBlock:
  4324. (void (^)(int32_t key, double value, BOOL *stop))block {
  4325. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4326. NSNumber *aValue,
  4327. BOOL *stop) {
  4328. block([aKey intValue], [aValue doubleValue], stop);
  4329. }];
  4330. }
  4331. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4332. NSUInteger count = _dictionary.count;
  4333. if (count == 0) {
  4334. return 0;
  4335. }
  4336. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4337. GPBDataType keyDataType = field.mapKeyDataType;
  4338. __block size_t result = 0;
  4339. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4340. NSNumber *aValue,
  4341. BOOL *stop) {
  4342. #pragma unused(stop)
  4343. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4344. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4345. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4346. }];
  4347. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4348. result += tagSize * count;
  4349. return result;
  4350. }
  4351. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4352. asField:(GPBFieldDescriptor *)field {
  4353. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4354. GPBDataType keyDataType = field.mapKeyDataType;
  4355. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4356. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4357. NSNumber *aValue,
  4358. BOOL *stop) {
  4359. #pragma unused(stop)
  4360. // Write the tag.
  4361. [outputStream writeInt32NoTag:tag];
  4362. // Write the size of the message.
  4363. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4364. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4365. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4366. // Write the fields.
  4367. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4368. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4369. }];
  4370. }
  4371. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4372. forGPBGenericValueKey:(GPBGenericValue *)key {
  4373. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4374. }
  4375. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4376. [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4377. #pragma unused(stop)
  4378. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4379. }];
  4380. }
  4381. - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
  4382. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4383. if (wrapped && value) {
  4384. *value = [wrapped doubleValue];
  4385. }
  4386. return (wrapped != NULL);
  4387. }
  4388. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4389. if (otherDictionary) {
  4390. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4391. if (_autocreator) {
  4392. GPBAutocreatedDictionaryModified(_autocreator, self);
  4393. }
  4394. }
  4395. }
  4396. - (void)setDouble:(double)value forKey:(int32_t)key {
  4397. [_dictionary setObject:@(value) forKey:@(key)];
  4398. if (_autocreator) {
  4399. GPBAutocreatedDictionaryModified(_autocreator, self);
  4400. }
  4401. }
  4402. - (void)removeDoubleForKey:(int32_t)aKey {
  4403. [_dictionary removeObjectForKey:@(aKey)];
  4404. }
  4405. - (void)removeAll {
  4406. [_dictionary removeAllObjects];
  4407. }
  4408. @end
  4409. #pragma mark - Int32 -> Enum
  4410. @implementation GPBInt32EnumDictionary {
  4411. @package
  4412. NSMutableDictionary *_dictionary;
  4413. GPBEnumValidationFunc _validationFunc;
  4414. }
  4415. @synthesize validationFunc = _validationFunc;
  4416. + (instancetype)dictionary {
  4417. return [[[self alloc] initWithValidationFunction:NULL
  4418. rawValues:NULL
  4419. forKeys:NULL
  4420. count:0] autorelease];
  4421. }
  4422. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4423. return [[[self alloc] initWithValidationFunction:func
  4424. rawValues:NULL
  4425. forKeys:NULL
  4426. count:0] autorelease];
  4427. }
  4428. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4429. rawValue:(int32_t)rawValue
  4430. forKey:(int32_t)key {
  4431. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4432. // on to get the type correct.
  4433. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4434. rawValues:&rawValue
  4435. forKeys:&key
  4436. count:1] autorelease];
  4437. }
  4438. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4439. rawValues:(const int32_t [])rawValues
  4440. forKeys:(const int32_t [])keys
  4441. count:(NSUInteger)count {
  4442. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4443. // on to get the type correct.
  4444. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4445. rawValues:rawValues
  4446. forKeys:keys
  4447. count:count] autorelease];
  4448. }
  4449. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4450. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4451. // on to get the type correct.
  4452. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4453. }
  4454. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4455. capacity:(NSUInteger)numItems {
  4456. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4457. }
  4458. - (instancetype)init {
  4459. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4460. }
  4461. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4462. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4463. }
  4464. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4465. rawValues:(const int32_t [])rawValues
  4466. forKeys:(const int32_t [])keys
  4467. count:(NSUInteger)count {
  4468. self = [super init];
  4469. if (self) {
  4470. _dictionary = [[NSMutableDictionary alloc] init];
  4471. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4472. if (count && rawValues && keys) {
  4473. for (NSUInteger i = 0; i < count; ++i) {
  4474. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4475. }
  4476. }
  4477. }
  4478. return self;
  4479. }
  4480. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4481. self = [self initWithValidationFunction:dictionary.validationFunc
  4482. rawValues:NULL
  4483. forKeys:NULL
  4484. count:0];
  4485. if (self) {
  4486. if (dictionary) {
  4487. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4488. }
  4489. }
  4490. return self;
  4491. }
  4492. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4493. capacity:(NSUInteger)numItems {
  4494. #pragma unused(numItems)
  4495. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4496. }
  4497. - (void)dealloc {
  4498. NSAssert(!_autocreator,
  4499. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4500. [self class], _autocreator);
  4501. [_dictionary release];
  4502. [super dealloc];
  4503. }
  4504. - (instancetype)copyWithZone:(NSZone *)zone {
  4505. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4506. }
  4507. - (BOOL)isEqual:(id)other {
  4508. if (self == other) {
  4509. return YES;
  4510. }
  4511. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4512. return NO;
  4513. }
  4514. GPBInt32EnumDictionary *otherDictionary = other;
  4515. return [_dictionary isEqual:otherDictionary->_dictionary];
  4516. }
  4517. - (NSUInteger)hash {
  4518. return _dictionary.count;
  4519. }
  4520. - (NSString *)description {
  4521. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4522. }
  4523. - (NSUInteger)count {
  4524. return _dictionary.count;
  4525. }
  4526. - (void)enumerateKeysAndRawValuesUsingBlock:
  4527. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4528. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4529. NSNumber *aValue,
  4530. BOOL *stop) {
  4531. block([aKey intValue], [aValue intValue], stop);
  4532. }];
  4533. }
  4534. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4535. NSUInteger count = _dictionary.count;
  4536. if (count == 0) {
  4537. return 0;
  4538. }
  4539. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4540. GPBDataType keyDataType = field.mapKeyDataType;
  4541. __block size_t result = 0;
  4542. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4543. NSNumber *aValue,
  4544. BOOL *stop) {
  4545. #pragma unused(stop)
  4546. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4547. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4548. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4549. }];
  4550. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4551. result += tagSize * count;
  4552. return result;
  4553. }
  4554. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4555. asField:(GPBFieldDescriptor *)field {
  4556. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4557. GPBDataType keyDataType = field.mapKeyDataType;
  4558. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4559. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4560. NSNumber *aValue,
  4561. BOOL *stop) {
  4562. #pragma unused(stop)
  4563. // Write the tag.
  4564. [outputStream writeInt32NoTag:tag];
  4565. // Write the size of the message.
  4566. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4567. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4568. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4569. // Write the fields.
  4570. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4571. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4572. }];
  4573. }
  4574. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4575. forKey:(GPBGenericValue *)key
  4576. keyDataType:(GPBDataType)keyDataType {
  4577. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4578. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4579. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4580. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4581. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4582. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4583. [outputStream release];
  4584. return data;
  4585. }
  4586. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4587. forGPBGenericValueKey:(GPBGenericValue *)key {
  4588. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4589. }
  4590. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4591. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4592. #pragma unused(stop)
  4593. block([NSString stringWithFormat:@"%d", key], @(value));
  4594. }];
  4595. }
  4596. - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
  4597. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4598. if (wrapped && value) {
  4599. int32_t result = [wrapped intValue];
  4600. if (!_validationFunc(result)) {
  4601. result = kGPBUnrecognizedEnumeratorValue;
  4602. }
  4603. *value = result;
  4604. }
  4605. return (wrapped != NULL);
  4606. }
  4607. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
  4608. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4609. if (wrapped && rawValue) {
  4610. *rawValue = [wrapped intValue];
  4611. }
  4612. return (wrapped != NULL);
  4613. }
  4614. - (void)enumerateKeysAndEnumsUsingBlock:
  4615. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4616. GPBEnumValidationFunc func = _validationFunc;
  4617. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4618. NSNumber *aValue,
  4619. BOOL *stop) {
  4620. int32_t unwrapped = [aValue intValue];
  4621. if (!func(unwrapped)) {
  4622. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4623. }
  4624. block([aKey intValue], unwrapped, stop);
  4625. }];
  4626. }
  4627. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4628. if (otherDictionary) {
  4629. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4630. if (_autocreator) {
  4631. GPBAutocreatedDictionaryModified(_autocreator, self);
  4632. }
  4633. }
  4634. }
  4635. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4636. [_dictionary setObject:@(value) forKey:@(key)];
  4637. if (_autocreator) {
  4638. GPBAutocreatedDictionaryModified(_autocreator, self);
  4639. }
  4640. }
  4641. - (void)removeEnumForKey:(int32_t)aKey {
  4642. [_dictionary removeObjectForKey:@(aKey)];
  4643. }
  4644. - (void)removeAll {
  4645. [_dictionary removeAllObjects];
  4646. }
  4647. - (void)setEnum:(int32_t)value forKey:(int32_t)key {
  4648. if (!_validationFunc(value)) {
  4649. [NSException raise:NSInvalidArgumentException
  4650. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4651. value];
  4652. }
  4653. [_dictionary setObject:@(value) forKey:@(key)];
  4654. if (_autocreator) {
  4655. GPBAutocreatedDictionaryModified(_autocreator, self);
  4656. }
  4657. }
  4658. @end
  4659. #pragma mark - Int32 -> Object
  4660. @implementation GPBInt32ObjectDictionary {
  4661. @package
  4662. NSMutableDictionary *_dictionary;
  4663. }
  4664. + (instancetype)dictionary {
  4665. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4666. }
  4667. + (instancetype)dictionaryWithObject:(id)object
  4668. forKey:(int32_t)key {
  4669. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4670. // on to get the type correct.
  4671. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4672. forKeys:&key
  4673. count:1] autorelease];
  4674. }
  4675. + (instancetype)dictionaryWithObjects:(const id [])objects
  4676. forKeys:(const int32_t [])keys
  4677. count:(NSUInteger)count {
  4678. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4679. // on to get the type correct.
  4680. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4681. forKeys:keys
  4682. count:count] autorelease];
  4683. }
  4684. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4685. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4686. // on to get the type correct.
  4687. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4688. }
  4689. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4690. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4691. }
  4692. - (instancetype)init {
  4693. return [self initWithObjects:NULL forKeys:NULL count:0];
  4694. }
  4695. - (instancetype)initWithObjects:(const id [])objects
  4696. forKeys:(const int32_t [])keys
  4697. count:(NSUInteger)count {
  4698. self = [super init];
  4699. if (self) {
  4700. _dictionary = [[NSMutableDictionary alloc] init];
  4701. if (count && objects && keys) {
  4702. for (NSUInteger i = 0; i < count; ++i) {
  4703. if (!objects[i]) {
  4704. [NSException raise:NSInvalidArgumentException
  4705. format:@"Attempting to add nil object to a Dictionary"];
  4706. }
  4707. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4708. }
  4709. }
  4710. }
  4711. return self;
  4712. }
  4713. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4714. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4715. if (self) {
  4716. if (dictionary) {
  4717. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4718. }
  4719. }
  4720. return self;
  4721. }
  4722. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4723. #pragma unused(numItems)
  4724. return [self initWithObjects:NULL forKeys:NULL count:0];
  4725. }
  4726. - (void)dealloc {
  4727. NSAssert(!_autocreator,
  4728. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4729. [self class], _autocreator);
  4730. [_dictionary release];
  4731. [super dealloc];
  4732. }
  4733. - (instancetype)copyWithZone:(NSZone *)zone {
  4734. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4735. }
  4736. - (BOOL)isEqual:(id)other {
  4737. if (self == other) {
  4738. return YES;
  4739. }
  4740. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4741. return NO;
  4742. }
  4743. GPBInt32ObjectDictionary *otherDictionary = other;
  4744. return [_dictionary isEqual:otherDictionary->_dictionary];
  4745. }
  4746. - (NSUInteger)hash {
  4747. return _dictionary.count;
  4748. }
  4749. - (NSString *)description {
  4750. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4751. }
  4752. - (NSUInteger)count {
  4753. return _dictionary.count;
  4754. }
  4755. - (void)enumerateKeysAndObjectsUsingBlock:
  4756. (void (^)(int32_t key, id object, BOOL *stop))block {
  4757. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4758. id aObject,
  4759. BOOL *stop) {
  4760. block([aKey intValue], aObject, stop);
  4761. }];
  4762. }
  4763. - (BOOL)isInitialized {
  4764. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4765. if (!msg.initialized) {
  4766. return NO;
  4767. }
  4768. }
  4769. return YES;
  4770. }
  4771. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4772. GPBInt32ObjectDictionary *newDict =
  4773. [[GPBInt32ObjectDictionary alloc] init];
  4774. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4775. GPBMessage *msg,
  4776. BOOL *stop) {
  4777. #pragma unused(stop)
  4778. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4779. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4780. [copiedMsg release];
  4781. }];
  4782. return newDict;
  4783. }
  4784. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4785. NSUInteger count = _dictionary.count;
  4786. if (count == 0) {
  4787. return 0;
  4788. }
  4789. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4790. GPBDataType keyDataType = field.mapKeyDataType;
  4791. __block size_t result = 0;
  4792. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4793. id aObject,
  4794. BOOL *stop) {
  4795. #pragma unused(stop)
  4796. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4797. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4798. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4799. }];
  4800. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4801. result += tagSize * count;
  4802. return result;
  4803. }
  4804. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4805. asField:(GPBFieldDescriptor *)field {
  4806. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4807. GPBDataType keyDataType = field.mapKeyDataType;
  4808. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4809. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4810. id aObject,
  4811. BOOL *stop) {
  4812. #pragma unused(stop)
  4813. // Write the tag.
  4814. [outputStream writeInt32NoTag:tag];
  4815. // Write the size of the message.
  4816. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4817. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4818. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4819. // Write the fields.
  4820. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4821. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  4822. }];
  4823. }
  4824. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4825. forGPBGenericValueKey:(GPBGenericValue *)key {
  4826. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4827. }
  4828. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4829. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4830. #pragma unused(stop)
  4831. block([NSString stringWithFormat:@"%d", key], object);
  4832. }];
  4833. }
  4834. - (id)objectForKey:(int32_t)key {
  4835. id result = [_dictionary objectForKey:@(key)];
  4836. return result;
  4837. }
  4838. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4839. if (otherDictionary) {
  4840. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4841. if (_autocreator) {
  4842. GPBAutocreatedDictionaryModified(_autocreator, self);
  4843. }
  4844. }
  4845. }
  4846. - (void)setObject:(id)object forKey:(int32_t)key {
  4847. if (!object) {
  4848. [NSException raise:NSInvalidArgumentException
  4849. format:@"Attempting to add nil object to a Dictionary"];
  4850. }
  4851. [_dictionary setObject:object forKey:@(key)];
  4852. if (_autocreator) {
  4853. GPBAutocreatedDictionaryModified(_autocreator, self);
  4854. }
  4855. }
  4856. - (void)removeObjectForKey:(int32_t)aKey {
  4857. [_dictionary removeObjectForKey:@(aKey)];
  4858. }
  4859. - (void)removeAll {
  4860. [_dictionary removeAllObjects];
  4861. }
  4862. @end
  4863. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4864. // This block of code is generated, do not edit it directly.
  4865. #pragma mark - UInt64 -> UInt32
  4866. @implementation GPBUInt64UInt32Dictionary {
  4867. @package
  4868. NSMutableDictionary *_dictionary;
  4869. }
  4870. + (instancetype)dictionary {
  4871. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  4872. }
  4873. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  4874. forKey:(uint64_t)key {
  4875. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  4876. // on to get the type correct.
  4877. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  4878. forKeys:&key
  4879. count:1] autorelease];
  4880. }
  4881. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  4882. forKeys:(const uint64_t [])keys
  4883. count:(NSUInteger)count {
  4884. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  4885. // on to get the type correct.
  4886. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
  4887. forKeys:keys
  4888. count:count] autorelease];
  4889. }
  4890. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4891. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4892. // on to get the type correct.
  4893. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4894. }
  4895. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4896. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4897. }
  4898. - (instancetype)init {
  4899. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4900. }
  4901. - (instancetype)initWithUInt32s:(const uint32_t [])values
  4902. forKeys:(const uint64_t [])keys
  4903. count:(NSUInteger)count {
  4904. self = [super init];
  4905. if (self) {
  4906. _dictionary = [[NSMutableDictionary alloc] init];
  4907. if (count && values && keys) {
  4908. for (NSUInteger i = 0; i < count; ++i) {
  4909. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4910. }
  4911. }
  4912. }
  4913. return self;
  4914. }
  4915. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4916. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  4917. if (self) {
  4918. if (dictionary) {
  4919. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4920. }
  4921. }
  4922. return self;
  4923. }
  4924. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4925. #pragma unused(numItems)
  4926. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4927. }
  4928. - (void)dealloc {
  4929. NSAssert(!_autocreator,
  4930. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4931. [self class], _autocreator);
  4932. [_dictionary release];
  4933. [super dealloc];
  4934. }
  4935. - (instancetype)copyWithZone:(NSZone *)zone {
  4936. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4937. }
  4938. - (BOOL)isEqual:(id)other {
  4939. if (self == other) {
  4940. return YES;
  4941. }
  4942. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4943. return NO;
  4944. }
  4945. GPBUInt64UInt32Dictionary *otherDictionary = other;
  4946. return [_dictionary isEqual:otherDictionary->_dictionary];
  4947. }
  4948. - (NSUInteger)hash {
  4949. return _dictionary.count;
  4950. }
  4951. - (NSString *)description {
  4952. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4953. }
  4954. - (NSUInteger)count {
  4955. return _dictionary.count;
  4956. }
  4957. - (void)enumerateKeysAndUInt32sUsingBlock:
  4958. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4959. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4960. NSNumber *aValue,
  4961. BOOL *stop) {
  4962. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4963. }];
  4964. }
  4965. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4966. NSUInteger count = _dictionary.count;
  4967. if (count == 0) {
  4968. return 0;
  4969. }
  4970. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4971. GPBDataType keyDataType = field.mapKeyDataType;
  4972. __block size_t result = 0;
  4973. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4974. NSNumber *aValue,
  4975. BOOL *stop) {
  4976. #pragma unused(stop)
  4977. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4978. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4979. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4980. }];
  4981. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4982. result += tagSize * count;
  4983. return result;
  4984. }
  4985. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4986. asField:(GPBFieldDescriptor *)field {
  4987. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4988. GPBDataType keyDataType = field.mapKeyDataType;
  4989. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4990. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4991. NSNumber *aValue,
  4992. BOOL *stop) {
  4993. #pragma unused(stop)
  4994. // Write the tag.
  4995. [outputStream writeInt32NoTag:tag];
  4996. // Write the size of the message.
  4997. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4998. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4999. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5000. // Write the fields.
  5001. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5002. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  5003. }];
  5004. }
  5005. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5006. forGPBGenericValueKey:(GPBGenericValue *)key {
  5007. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  5008. }
  5009. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5010. [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  5011. #pragma unused(stop)
  5012. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  5013. }];
  5014. }
  5015. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
  5016. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5017. if (wrapped && value) {
  5018. *value = [wrapped unsignedIntValue];
  5019. }
  5020. return (wrapped != NULL);
  5021. }
  5022. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  5023. if (otherDictionary) {
  5024. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5025. if (_autocreator) {
  5026. GPBAutocreatedDictionaryModified(_autocreator, self);
  5027. }
  5028. }
  5029. }
  5030. - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
  5031. [_dictionary setObject:@(value) forKey:@(key)];
  5032. if (_autocreator) {
  5033. GPBAutocreatedDictionaryModified(_autocreator, self);
  5034. }
  5035. }
  5036. - (void)removeUInt32ForKey:(uint64_t)aKey {
  5037. [_dictionary removeObjectForKey:@(aKey)];
  5038. }
  5039. - (void)removeAll {
  5040. [_dictionary removeAllObjects];
  5041. }
  5042. @end
  5043. #pragma mark - UInt64 -> Int32
  5044. @implementation GPBUInt64Int32Dictionary {
  5045. @package
  5046. NSMutableDictionary *_dictionary;
  5047. }
  5048. + (instancetype)dictionary {
  5049. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  5050. }
  5051. + (instancetype)dictionaryWithInt32:(int32_t)value
  5052. forKey:(uint64_t)key {
  5053. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  5054. // on to get the type correct.
  5055. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
  5056. forKeys:&key
  5057. count:1] autorelease];
  5058. }
  5059. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  5060. forKeys:(const uint64_t [])keys
  5061. count:(NSUInteger)count {
  5062. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  5063. // on to get the type correct.
  5064. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:values
  5065. forKeys:keys
  5066. count:count] autorelease];
  5067. }
  5068. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5069. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5070. // on to get the type correct.
  5071. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5072. }
  5073. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5074. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5075. }
  5076. - (instancetype)init {
  5077. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5078. }
  5079. - (instancetype)initWithInt32s:(const int32_t [])values
  5080. forKeys:(const uint64_t [])keys
  5081. count:(NSUInteger)count {
  5082. self = [super init];
  5083. if (self) {
  5084. _dictionary = [[NSMutableDictionary alloc] init];
  5085. if (count && values && keys) {
  5086. for (NSUInteger i = 0; i < count; ++i) {
  5087. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5088. }
  5089. }
  5090. }
  5091. return self;
  5092. }
  5093. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5094. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  5095. if (self) {
  5096. if (dictionary) {
  5097. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5098. }
  5099. }
  5100. return self;
  5101. }
  5102. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5103. #pragma unused(numItems)
  5104. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5105. }
  5106. - (void)dealloc {
  5107. NSAssert(!_autocreator,
  5108. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5109. [self class], _autocreator);
  5110. [_dictionary release];
  5111. [super dealloc];
  5112. }
  5113. - (instancetype)copyWithZone:(NSZone *)zone {
  5114. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5115. }
  5116. - (BOOL)isEqual:(id)other {
  5117. if (self == other) {
  5118. return YES;
  5119. }
  5120. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5121. return NO;
  5122. }
  5123. GPBUInt64Int32Dictionary *otherDictionary = other;
  5124. return [_dictionary isEqual:otherDictionary->_dictionary];
  5125. }
  5126. - (NSUInteger)hash {
  5127. return _dictionary.count;
  5128. }
  5129. - (NSString *)description {
  5130. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5131. }
  5132. - (NSUInteger)count {
  5133. return _dictionary.count;
  5134. }
  5135. - (void)enumerateKeysAndInt32sUsingBlock:
  5136. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5137. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5138. NSNumber *aValue,
  5139. BOOL *stop) {
  5140. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5141. }];
  5142. }
  5143. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5144. NSUInteger count = _dictionary.count;
  5145. if (count == 0) {
  5146. return 0;
  5147. }
  5148. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5149. GPBDataType keyDataType = field.mapKeyDataType;
  5150. __block size_t result = 0;
  5151. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5152. NSNumber *aValue,
  5153. BOOL *stop) {
  5154. #pragma unused(stop)
  5155. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5156. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5157. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5158. }];
  5159. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5160. result += tagSize * count;
  5161. return result;
  5162. }
  5163. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5164. asField:(GPBFieldDescriptor *)field {
  5165. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5166. GPBDataType keyDataType = field.mapKeyDataType;
  5167. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5168. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5169. NSNumber *aValue,
  5170. BOOL *stop) {
  5171. #pragma unused(stop)
  5172. // Write the tag.
  5173. [outputStream writeInt32NoTag:tag];
  5174. // Write the size of the message.
  5175. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5176. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5177. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5178. // Write the fields.
  5179. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5180. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5181. }];
  5182. }
  5183. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5184. forGPBGenericValueKey:(GPBGenericValue *)key {
  5185. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5186. }
  5187. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5188. [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5189. #pragma unused(stop)
  5190. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5191. }];
  5192. }
  5193. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
  5194. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5195. if (wrapped && value) {
  5196. *value = [wrapped intValue];
  5197. }
  5198. return (wrapped != NULL);
  5199. }
  5200. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5201. if (otherDictionary) {
  5202. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5203. if (_autocreator) {
  5204. GPBAutocreatedDictionaryModified(_autocreator, self);
  5205. }
  5206. }
  5207. }
  5208. - (void)setInt32:(int32_t)value forKey:(uint64_t)key {
  5209. [_dictionary setObject:@(value) forKey:@(key)];
  5210. if (_autocreator) {
  5211. GPBAutocreatedDictionaryModified(_autocreator, self);
  5212. }
  5213. }
  5214. - (void)removeInt32ForKey:(uint64_t)aKey {
  5215. [_dictionary removeObjectForKey:@(aKey)];
  5216. }
  5217. - (void)removeAll {
  5218. [_dictionary removeAllObjects];
  5219. }
  5220. @end
  5221. #pragma mark - UInt64 -> UInt64
  5222. @implementation GPBUInt64UInt64Dictionary {
  5223. @package
  5224. NSMutableDictionary *_dictionary;
  5225. }
  5226. + (instancetype)dictionary {
  5227. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  5228. }
  5229. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  5230. forKey:(uint64_t)key {
  5231. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  5232. // on to get the type correct.
  5233. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  5234. forKeys:&key
  5235. count:1] autorelease];
  5236. }
  5237. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  5238. forKeys:(const uint64_t [])keys
  5239. count:(NSUInteger)count {
  5240. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  5241. // on to get the type correct.
  5242. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
  5243. forKeys:keys
  5244. count:count] autorelease];
  5245. }
  5246. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5247. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5248. // on to get the type correct.
  5249. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5250. }
  5251. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5252. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5253. }
  5254. - (instancetype)init {
  5255. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  5256. }
  5257. - (instancetype)initWithUInt64s:(const uint64_t [])values
  5258. forKeys:(const uint64_t [])keys
  5259. count:(NSUInteger)count {
  5260. self = [super init];
  5261. if (self) {
  5262. _dictionary = [[NSMutableDictionary alloc] init];
  5263. if (count && values && keys) {
  5264. for (NSUInteger i = 0; i < count; ++i) {
  5265. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5266. }
  5267. }
  5268. }
  5269. return self;
  5270. }
  5271. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5272. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  5273. if (self) {
  5274. if (dictionary) {
  5275. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5276. }
  5277. }
  5278. return self;
  5279. }
  5280. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5281. #pragma unused(numItems)
  5282. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  5283. }
  5284. - (void)dealloc {
  5285. NSAssert(!_autocreator,
  5286. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5287. [self class], _autocreator);
  5288. [_dictionary release];
  5289. [super dealloc];
  5290. }
  5291. - (instancetype)copyWithZone:(NSZone *)zone {
  5292. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5293. }
  5294. - (BOOL)isEqual:(id)other {
  5295. if (self == other) {
  5296. return YES;
  5297. }
  5298. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5299. return NO;
  5300. }
  5301. GPBUInt64UInt64Dictionary *otherDictionary = other;
  5302. return [_dictionary isEqual:otherDictionary->_dictionary];
  5303. }
  5304. - (NSUInteger)hash {
  5305. return _dictionary.count;
  5306. }
  5307. - (NSString *)description {
  5308. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5309. }
  5310. - (NSUInteger)count {
  5311. return _dictionary.count;
  5312. }
  5313. - (void)enumerateKeysAndUInt64sUsingBlock:
  5314. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5315. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5316. NSNumber *aValue,
  5317. BOOL *stop) {
  5318. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5319. }];
  5320. }
  5321. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5322. NSUInteger count = _dictionary.count;
  5323. if (count == 0) {
  5324. return 0;
  5325. }
  5326. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5327. GPBDataType keyDataType = field.mapKeyDataType;
  5328. __block size_t result = 0;
  5329. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5330. NSNumber *aValue,
  5331. BOOL *stop) {
  5332. #pragma unused(stop)
  5333. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5334. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5335. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5336. }];
  5337. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5338. result += tagSize * count;
  5339. return result;
  5340. }
  5341. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5342. asField:(GPBFieldDescriptor *)field {
  5343. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5344. GPBDataType keyDataType = field.mapKeyDataType;
  5345. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5346. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5347. NSNumber *aValue,
  5348. BOOL *stop) {
  5349. #pragma unused(stop)
  5350. // Write the tag.
  5351. [outputStream writeInt32NoTag:tag];
  5352. // Write the size of the message.
  5353. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5354. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5355. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5356. // Write the fields.
  5357. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5358. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5359. }];
  5360. }
  5361. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5362. forGPBGenericValueKey:(GPBGenericValue *)key {
  5363. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5364. }
  5365. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5366. [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5367. #pragma unused(stop)
  5368. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5369. }];
  5370. }
  5371. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
  5372. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5373. if (wrapped && value) {
  5374. *value = [wrapped unsignedLongLongValue];
  5375. }
  5376. return (wrapped != NULL);
  5377. }
  5378. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5379. if (otherDictionary) {
  5380. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5381. if (_autocreator) {
  5382. GPBAutocreatedDictionaryModified(_autocreator, self);
  5383. }
  5384. }
  5385. }
  5386. - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
  5387. [_dictionary setObject:@(value) forKey:@(key)];
  5388. if (_autocreator) {
  5389. GPBAutocreatedDictionaryModified(_autocreator, self);
  5390. }
  5391. }
  5392. - (void)removeUInt64ForKey:(uint64_t)aKey {
  5393. [_dictionary removeObjectForKey:@(aKey)];
  5394. }
  5395. - (void)removeAll {
  5396. [_dictionary removeAllObjects];
  5397. }
  5398. @end
  5399. #pragma mark - UInt64 -> Int64
  5400. @implementation GPBUInt64Int64Dictionary {
  5401. @package
  5402. NSMutableDictionary *_dictionary;
  5403. }
  5404. + (instancetype)dictionary {
  5405. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  5406. }
  5407. + (instancetype)dictionaryWithInt64:(int64_t)value
  5408. forKey:(uint64_t)key {
  5409. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  5410. // on to get the type correct.
  5411. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
  5412. forKeys:&key
  5413. count:1] autorelease];
  5414. }
  5415. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  5416. forKeys:(const uint64_t [])keys
  5417. count:(NSUInteger)count {
  5418. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  5419. // on to get the type correct.
  5420. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:values
  5421. forKeys:keys
  5422. count:count] autorelease];
  5423. }
  5424. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5425. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5426. // on to get the type correct.
  5427. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5428. }
  5429. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5430. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5431. }
  5432. - (instancetype)init {
  5433. return [self initWithInt64s:NULL forKeys:NULL count:0];
  5434. }
  5435. - (instancetype)initWithInt64s:(const int64_t [])values
  5436. forKeys:(const uint64_t [])keys
  5437. count:(NSUInteger)count {
  5438. self = [super init];
  5439. if (self) {
  5440. _dictionary = [[NSMutableDictionary alloc] init];
  5441. if (count && values && keys) {
  5442. for (NSUInteger i = 0; i < count; ++i) {
  5443. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5444. }
  5445. }
  5446. }
  5447. return self;
  5448. }
  5449. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5450. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  5451. if (self) {
  5452. if (dictionary) {
  5453. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5454. }
  5455. }
  5456. return self;
  5457. }
  5458. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5459. #pragma unused(numItems)
  5460. return [self initWithInt64s:NULL forKeys:NULL count:0];
  5461. }
  5462. - (void)dealloc {
  5463. NSAssert(!_autocreator,
  5464. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5465. [self class], _autocreator);
  5466. [_dictionary release];
  5467. [super dealloc];
  5468. }
  5469. - (instancetype)copyWithZone:(NSZone *)zone {
  5470. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5471. }
  5472. - (BOOL)isEqual:(id)other {
  5473. if (self == other) {
  5474. return YES;
  5475. }
  5476. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5477. return NO;
  5478. }
  5479. GPBUInt64Int64Dictionary *otherDictionary = other;
  5480. return [_dictionary isEqual:otherDictionary->_dictionary];
  5481. }
  5482. - (NSUInteger)hash {
  5483. return _dictionary.count;
  5484. }
  5485. - (NSString *)description {
  5486. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5487. }
  5488. - (NSUInteger)count {
  5489. return _dictionary.count;
  5490. }
  5491. - (void)enumerateKeysAndInt64sUsingBlock:
  5492. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5493. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5494. NSNumber *aValue,
  5495. BOOL *stop) {
  5496. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5497. }];
  5498. }
  5499. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5500. NSUInteger count = _dictionary.count;
  5501. if (count == 0) {
  5502. return 0;
  5503. }
  5504. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5505. GPBDataType keyDataType = field.mapKeyDataType;
  5506. __block size_t result = 0;
  5507. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5508. NSNumber *aValue,
  5509. BOOL *stop) {
  5510. #pragma unused(stop)
  5511. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5512. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5513. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5514. }];
  5515. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5516. result += tagSize * count;
  5517. return result;
  5518. }
  5519. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5520. asField:(GPBFieldDescriptor *)field {
  5521. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5522. GPBDataType keyDataType = field.mapKeyDataType;
  5523. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5524. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5525. NSNumber *aValue,
  5526. BOOL *stop) {
  5527. #pragma unused(stop)
  5528. // Write the tag.
  5529. [outputStream writeInt32NoTag:tag];
  5530. // Write the size of the message.
  5531. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5532. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5533. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5534. // Write the fields.
  5535. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5536. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5537. }];
  5538. }
  5539. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5540. forGPBGenericValueKey:(GPBGenericValue *)key {
  5541. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5542. }
  5543. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5544. [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5545. #pragma unused(stop)
  5546. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5547. }];
  5548. }
  5549. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
  5550. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5551. if (wrapped && value) {
  5552. *value = [wrapped longLongValue];
  5553. }
  5554. return (wrapped != NULL);
  5555. }
  5556. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5557. if (otherDictionary) {
  5558. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5559. if (_autocreator) {
  5560. GPBAutocreatedDictionaryModified(_autocreator, self);
  5561. }
  5562. }
  5563. }
  5564. - (void)setInt64:(int64_t)value forKey:(uint64_t)key {
  5565. [_dictionary setObject:@(value) forKey:@(key)];
  5566. if (_autocreator) {
  5567. GPBAutocreatedDictionaryModified(_autocreator, self);
  5568. }
  5569. }
  5570. - (void)removeInt64ForKey:(uint64_t)aKey {
  5571. [_dictionary removeObjectForKey:@(aKey)];
  5572. }
  5573. - (void)removeAll {
  5574. [_dictionary removeAllObjects];
  5575. }
  5576. @end
  5577. #pragma mark - UInt64 -> Bool
  5578. @implementation GPBUInt64BoolDictionary {
  5579. @package
  5580. NSMutableDictionary *_dictionary;
  5581. }
  5582. + (instancetype)dictionary {
  5583. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  5584. }
  5585. + (instancetype)dictionaryWithBool:(BOOL)value
  5586. forKey:(uint64_t)key {
  5587. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  5588. // on to get the type correct.
  5589. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:&value
  5590. forKeys:&key
  5591. count:1] autorelease];
  5592. }
  5593. + (instancetype)dictionaryWithBools:(const BOOL [])values
  5594. forKeys:(const uint64_t [])keys
  5595. count:(NSUInteger)count {
  5596. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  5597. // on to get the type correct.
  5598. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:values
  5599. forKeys:keys
  5600. count:count] autorelease];
  5601. }
  5602. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5603. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5604. // on to get the type correct.
  5605. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5606. }
  5607. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5608. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5609. }
  5610. - (instancetype)init {
  5611. return [self initWithBools:NULL forKeys:NULL count:0];
  5612. }
  5613. - (instancetype)initWithBools:(const BOOL [])values
  5614. forKeys:(const uint64_t [])keys
  5615. count:(NSUInteger)count {
  5616. self = [super init];
  5617. if (self) {
  5618. _dictionary = [[NSMutableDictionary alloc] init];
  5619. if (count && values && keys) {
  5620. for (NSUInteger i = 0; i < count; ++i) {
  5621. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5622. }
  5623. }
  5624. }
  5625. return self;
  5626. }
  5627. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5628. self = [self initWithBools:NULL forKeys:NULL count:0];
  5629. if (self) {
  5630. if (dictionary) {
  5631. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5632. }
  5633. }
  5634. return self;
  5635. }
  5636. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5637. #pragma unused(numItems)
  5638. return [self initWithBools:NULL forKeys:NULL count:0];
  5639. }
  5640. - (void)dealloc {
  5641. NSAssert(!_autocreator,
  5642. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5643. [self class], _autocreator);
  5644. [_dictionary release];
  5645. [super dealloc];
  5646. }
  5647. - (instancetype)copyWithZone:(NSZone *)zone {
  5648. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5649. }
  5650. - (BOOL)isEqual:(id)other {
  5651. if (self == other) {
  5652. return YES;
  5653. }
  5654. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5655. return NO;
  5656. }
  5657. GPBUInt64BoolDictionary *otherDictionary = other;
  5658. return [_dictionary isEqual:otherDictionary->_dictionary];
  5659. }
  5660. - (NSUInteger)hash {
  5661. return _dictionary.count;
  5662. }
  5663. - (NSString *)description {
  5664. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5665. }
  5666. - (NSUInteger)count {
  5667. return _dictionary.count;
  5668. }
  5669. - (void)enumerateKeysAndBoolsUsingBlock:
  5670. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5671. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5672. NSNumber *aValue,
  5673. BOOL *stop) {
  5674. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5675. }];
  5676. }
  5677. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5678. NSUInteger count = _dictionary.count;
  5679. if (count == 0) {
  5680. return 0;
  5681. }
  5682. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5683. GPBDataType keyDataType = field.mapKeyDataType;
  5684. __block size_t result = 0;
  5685. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5686. NSNumber *aValue,
  5687. BOOL *stop) {
  5688. #pragma unused(stop)
  5689. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5690. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5691. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5692. }];
  5693. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5694. result += tagSize * count;
  5695. return result;
  5696. }
  5697. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5698. asField:(GPBFieldDescriptor *)field {
  5699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5700. GPBDataType keyDataType = field.mapKeyDataType;
  5701. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5703. NSNumber *aValue,
  5704. BOOL *stop) {
  5705. #pragma unused(stop)
  5706. // Write the tag.
  5707. [outputStream writeInt32NoTag:tag];
  5708. // Write the size of the message.
  5709. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5710. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5711. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5712. // Write the fields.
  5713. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5714. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5715. }];
  5716. }
  5717. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5718. forGPBGenericValueKey:(GPBGenericValue *)key {
  5719. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5720. }
  5721. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5722. [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5723. #pragma unused(stop)
  5724. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5725. }];
  5726. }
  5727. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
  5728. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5729. if (wrapped && value) {
  5730. *value = [wrapped boolValue];
  5731. }
  5732. return (wrapped != NULL);
  5733. }
  5734. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5735. if (otherDictionary) {
  5736. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5737. if (_autocreator) {
  5738. GPBAutocreatedDictionaryModified(_autocreator, self);
  5739. }
  5740. }
  5741. }
  5742. - (void)setBool:(BOOL)value forKey:(uint64_t)key {
  5743. [_dictionary setObject:@(value) forKey:@(key)];
  5744. if (_autocreator) {
  5745. GPBAutocreatedDictionaryModified(_autocreator, self);
  5746. }
  5747. }
  5748. - (void)removeBoolForKey:(uint64_t)aKey {
  5749. [_dictionary removeObjectForKey:@(aKey)];
  5750. }
  5751. - (void)removeAll {
  5752. [_dictionary removeAllObjects];
  5753. }
  5754. @end
  5755. #pragma mark - UInt64 -> Float
  5756. @implementation GPBUInt64FloatDictionary {
  5757. @package
  5758. NSMutableDictionary *_dictionary;
  5759. }
  5760. + (instancetype)dictionary {
  5761. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  5762. }
  5763. + (instancetype)dictionaryWithFloat:(float)value
  5764. forKey:(uint64_t)key {
  5765. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  5766. // on to get the type correct.
  5767. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:&value
  5768. forKeys:&key
  5769. count:1] autorelease];
  5770. }
  5771. + (instancetype)dictionaryWithFloats:(const float [])values
  5772. forKeys:(const uint64_t [])keys
  5773. count:(NSUInteger)count {
  5774. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  5775. // on to get the type correct.
  5776. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:values
  5777. forKeys:keys
  5778. count:count] autorelease];
  5779. }
  5780. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5781. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5782. // on to get the type correct.
  5783. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5784. }
  5785. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5786. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5787. }
  5788. - (instancetype)init {
  5789. return [self initWithFloats:NULL forKeys:NULL count:0];
  5790. }
  5791. - (instancetype)initWithFloats:(const float [])values
  5792. forKeys:(const uint64_t [])keys
  5793. count:(NSUInteger)count {
  5794. self = [super init];
  5795. if (self) {
  5796. _dictionary = [[NSMutableDictionary alloc] init];
  5797. if (count && values && keys) {
  5798. for (NSUInteger i = 0; i < count; ++i) {
  5799. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5800. }
  5801. }
  5802. }
  5803. return self;
  5804. }
  5805. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5806. self = [self initWithFloats:NULL forKeys:NULL count:0];
  5807. if (self) {
  5808. if (dictionary) {
  5809. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5810. }
  5811. }
  5812. return self;
  5813. }
  5814. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5815. #pragma unused(numItems)
  5816. return [self initWithFloats:NULL forKeys:NULL count:0];
  5817. }
  5818. - (void)dealloc {
  5819. NSAssert(!_autocreator,
  5820. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5821. [self class], _autocreator);
  5822. [_dictionary release];
  5823. [super dealloc];
  5824. }
  5825. - (instancetype)copyWithZone:(NSZone *)zone {
  5826. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5827. }
  5828. - (BOOL)isEqual:(id)other {
  5829. if (self == other) {
  5830. return YES;
  5831. }
  5832. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5833. return NO;
  5834. }
  5835. GPBUInt64FloatDictionary *otherDictionary = other;
  5836. return [_dictionary isEqual:otherDictionary->_dictionary];
  5837. }
  5838. - (NSUInteger)hash {
  5839. return _dictionary.count;
  5840. }
  5841. - (NSString *)description {
  5842. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5843. }
  5844. - (NSUInteger)count {
  5845. return _dictionary.count;
  5846. }
  5847. - (void)enumerateKeysAndFloatsUsingBlock:
  5848. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5849. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5850. NSNumber *aValue,
  5851. BOOL *stop) {
  5852. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5853. }];
  5854. }
  5855. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5856. NSUInteger count = _dictionary.count;
  5857. if (count == 0) {
  5858. return 0;
  5859. }
  5860. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5861. GPBDataType keyDataType = field.mapKeyDataType;
  5862. __block size_t result = 0;
  5863. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5864. NSNumber *aValue,
  5865. BOOL *stop) {
  5866. #pragma unused(stop)
  5867. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5868. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5869. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5870. }];
  5871. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5872. result += tagSize * count;
  5873. return result;
  5874. }
  5875. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5876. asField:(GPBFieldDescriptor *)field {
  5877. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5878. GPBDataType keyDataType = field.mapKeyDataType;
  5879. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5880. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5881. NSNumber *aValue,
  5882. BOOL *stop) {
  5883. #pragma unused(stop)
  5884. // Write the tag.
  5885. [outputStream writeInt32NoTag:tag];
  5886. // Write the size of the message.
  5887. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5888. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5889. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5890. // Write the fields.
  5891. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5892. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5893. }];
  5894. }
  5895. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5896. forGPBGenericValueKey:(GPBGenericValue *)key {
  5897. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5898. }
  5899. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5900. [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5901. #pragma unused(stop)
  5902. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5903. }];
  5904. }
  5905. - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
  5906. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5907. if (wrapped && value) {
  5908. *value = [wrapped floatValue];
  5909. }
  5910. return (wrapped != NULL);
  5911. }
  5912. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5913. if (otherDictionary) {
  5914. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5915. if (_autocreator) {
  5916. GPBAutocreatedDictionaryModified(_autocreator, self);
  5917. }
  5918. }
  5919. }
  5920. - (void)setFloat:(float)value forKey:(uint64_t)key {
  5921. [_dictionary setObject:@(value) forKey:@(key)];
  5922. if (_autocreator) {
  5923. GPBAutocreatedDictionaryModified(_autocreator, self);
  5924. }
  5925. }
  5926. - (void)removeFloatForKey:(uint64_t)aKey {
  5927. [_dictionary removeObjectForKey:@(aKey)];
  5928. }
  5929. - (void)removeAll {
  5930. [_dictionary removeAllObjects];
  5931. }
  5932. @end
  5933. #pragma mark - UInt64 -> Double
  5934. @implementation GPBUInt64DoubleDictionary {
  5935. @package
  5936. NSMutableDictionary *_dictionary;
  5937. }
  5938. + (instancetype)dictionary {
  5939. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  5940. }
  5941. + (instancetype)dictionaryWithDouble:(double)value
  5942. forKey:(uint64_t)key {
  5943. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  5944. // on to get the type correct.
  5945. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
  5946. forKeys:&key
  5947. count:1] autorelease];
  5948. }
  5949. + (instancetype)dictionaryWithDoubles:(const double [])values
  5950. forKeys:(const uint64_t [])keys
  5951. count:(NSUInteger)count {
  5952. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  5953. // on to get the type correct.
  5954. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:values
  5955. forKeys:keys
  5956. count:count] autorelease];
  5957. }
  5958. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5959. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5960. // on to get the type correct.
  5961. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5962. }
  5963. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5964. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5965. }
  5966. - (instancetype)init {
  5967. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5968. }
  5969. - (instancetype)initWithDoubles:(const double [])values
  5970. forKeys:(const uint64_t [])keys
  5971. count:(NSUInteger)count {
  5972. self = [super init];
  5973. if (self) {
  5974. _dictionary = [[NSMutableDictionary alloc] init];
  5975. if (count && values && keys) {
  5976. for (NSUInteger i = 0; i < count; ++i) {
  5977. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5978. }
  5979. }
  5980. }
  5981. return self;
  5982. }
  5983. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5984. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  5985. if (self) {
  5986. if (dictionary) {
  5987. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5988. }
  5989. }
  5990. return self;
  5991. }
  5992. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5993. #pragma unused(numItems)
  5994. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5995. }
  5996. - (void)dealloc {
  5997. NSAssert(!_autocreator,
  5998. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5999. [self class], _autocreator);
  6000. [_dictionary release];
  6001. [super dealloc];
  6002. }
  6003. - (instancetype)copyWithZone:(NSZone *)zone {
  6004. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6005. }
  6006. - (BOOL)isEqual:(id)other {
  6007. if (self == other) {
  6008. return YES;
  6009. }
  6010. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  6011. return NO;
  6012. }
  6013. GPBUInt64DoubleDictionary *otherDictionary = other;
  6014. return [_dictionary isEqual:otherDictionary->_dictionary];
  6015. }
  6016. - (NSUInteger)hash {
  6017. return _dictionary.count;
  6018. }
  6019. - (NSString *)description {
  6020. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6021. }
  6022. - (NSUInteger)count {
  6023. return _dictionary.count;
  6024. }
  6025. - (void)enumerateKeysAndDoublesUsingBlock:
  6026. (void (^)(uint64_t key, double value, BOOL *stop))block {
  6027. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6028. NSNumber *aValue,
  6029. BOOL *stop) {
  6030. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  6031. }];
  6032. }
  6033. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6034. NSUInteger count = _dictionary.count;
  6035. if (count == 0) {
  6036. return 0;
  6037. }
  6038. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6039. GPBDataType keyDataType = field.mapKeyDataType;
  6040. __block size_t result = 0;
  6041. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6042. NSNumber *aValue,
  6043. BOOL *stop) {
  6044. #pragma unused(stop)
  6045. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6046. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6047. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6048. }];
  6049. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6050. result += tagSize * count;
  6051. return result;
  6052. }
  6053. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6054. asField:(GPBFieldDescriptor *)field {
  6055. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6056. GPBDataType keyDataType = field.mapKeyDataType;
  6057. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6058. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6059. NSNumber *aValue,
  6060. BOOL *stop) {
  6061. #pragma unused(stop)
  6062. // Write the tag.
  6063. [outputStream writeInt32NoTag:tag];
  6064. // Write the size of the message.
  6065. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6066. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6067. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6068. // Write the fields.
  6069. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6070. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6071. }];
  6072. }
  6073. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6074. forGPBGenericValueKey:(GPBGenericValue *)key {
  6075. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  6076. }
  6077. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6078. [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  6079. #pragma unused(stop)
  6080. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6081. }];
  6082. }
  6083. - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
  6084. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6085. if (wrapped && value) {
  6086. *value = [wrapped doubleValue];
  6087. }
  6088. return (wrapped != NULL);
  6089. }
  6090. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  6091. if (otherDictionary) {
  6092. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6093. if (_autocreator) {
  6094. GPBAutocreatedDictionaryModified(_autocreator, self);
  6095. }
  6096. }
  6097. }
  6098. - (void)setDouble:(double)value forKey:(uint64_t)key {
  6099. [_dictionary setObject:@(value) forKey:@(key)];
  6100. if (_autocreator) {
  6101. GPBAutocreatedDictionaryModified(_autocreator, self);
  6102. }
  6103. }
  6104. - (void)removeDoubleForKey:(uint64_t)aKey {
  6105. [_dictionary removeObjectForKey:@(aKey)];
  6106. }
  6107. - (void)removeAll {
  6108. [_dictionary removeAllObjects];
  6109. }
  6110. @end
  6111. #pragma mark - UInt64 -> Enum
  6112. @implementation GPBUInt64EnumDictionary {
  6113. @package
  6114. NSMutableDictionary *_dictionary;
  6115. GPBEnumValidationFunc _validationFunc;
  6116. }
  6117. @synthesize validationFunc = _validationFunc;
  6118. + (instancetype)dictionary {
  6119. return [[[self alloc] initWithValidationFunction:NULL
  6120. rawValues:NULL
  6121. forKeys:NULL
  6122. count:0] autorelease];
  6123. }
  6124. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6125. return [[[self alloc] initWithValidationFunction:func
  6126. rawValues:NULL
  6127. forKeys:NULL
  6128. count:0] autorelease];
  6129. }
  6130. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6131. rawValue:(int32_t)rawValue
  6132. forKey:(uint64_t)key {
  6133. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6134. // on to get the type correct.
  6135. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6136. rawValues:&rawValue
  6137. forKeys:&key
  6138. count:1] autorelease];
  6139. }
  6140. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6141. rawValues:(const int32_t [])rawValues
  6142. forKeys:(const uint64_t [])keys
  6143. count:(NSUInteger)count {
  6144. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6145. // on to get the type correct.
  6146. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6147. rawValues:rawValues
  6148. forKeys:keys
  6149. count:count] autorelease];
  6150. }
  6151. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6152. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6153. // on to get the type correct.
  6154. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6155. }
  6156. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6157. capacity:(NSUInteger)numItems {
  6158. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6159. }
  6160. - (instancetype)init {
  6161. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6162. }
  6163. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6164. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6165. }
  6166. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6167. rawValues:(const int32_t [])rawValues
  6168. forKeys:(const uint64_t [])keys
  6169. count:(NSUInteger)count {
  6170. self = [super init];
  6171. if (self) {
  6172. _dictionary = [[NSMutableDictionary alloc] init];
  6173. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6174. if (count && rawValues && keys) {
  6175. for (NSUInteger i = 0; i < count; ++i) {
  6176. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6177. }
  6178. }
  6179. }
  6180. return self;
  6181. }
  6182. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6183. self = [self initWithValidationFunction:dictionary.validationFunc
  6184. rawValues:NULL
  6185. forKeys:NULL
  6186. count:0];
  6187. if (self) {
  6188. if (dictionary) {
  6189. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6190. }
  6191. }
  6192. return self;
  6193. }
  6194. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6195. capacity:(NSUInteger)numItems {
  6196. #pragma unused(numItems)
  6197. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6198. }
  6199. - (void)dealloc {
  6200. NSAssert(!_autocreator,
  6201. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6202. [self class], _autocreator);
  6203. [_dictionary release];
  6204. [super dealloc];
  6205. }
  6206. - (instancetype)copyWithZone:(NSZone *)zone {
  6207. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6208. }
  6209. - (BOOL)isEqual:(id)other {
  6210. if (self == other) {
  6211. return YES;
  6212. }
  6213. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6214. return NO;
  6215. }
  6216. GPBUInt64EnumDictionary *otherDictionary = other;
  6217. return [_dictionary isEqual:otherDictionary->_dictionary];
  6218. }
  6219. - (NSUInteger)hash {
  6220. return _dictionary.count;
  6221. }
  6222. - (NSString *)description {
  6223. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6224. }
  6225. - (NSUInteger)count {
  6226. return _dictionary.count;
  6227. }
  6228. - (void)enumerateKeysAndRawValuesUsingBlock:
  6229. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6230. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6231. NSNumber *aValue,
  6232. BOOL *stop) {
  6233. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6234. }];
  6235. }
  6236. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6237. NSUInteger count = _dictionary.count;
  6238. if (count == 0) {
  6239. return 0;
  6240. }
  6241. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6242. GPBDataType keyDataType = field.mapKeyDataType;
  6243. __block size_t result = 0;
  6244. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6245. NSNumber *aValue,
  6246. BOOL *stop) {
  6247. #pragma unused(stop)
  6248. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6249. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6250. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6251. }];
  6252. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6253. result += tagSize * count;
  6254. return result;
  6255. }
  6256. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6257. asField:(GPBFieldDescriptor *)field {
  6258. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6259. GPBDataType keyDataType = field.mapKeyDataType;
  6260. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6261. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6262. NSNumber *aValue,
  6263. BOOL *stop) {
  6264. #pragma unused(stop)
  6265. // Write the tag.
  6266. [outputStream writeInt32NoTag:tag];
  6267. // Write the size of the message.
  6268. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6269. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6270. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6271. // Write the fields.
  6272. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6273. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6274. }];
  6275. }
  6276. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6277. forKey:(GPBGenericValue *)key
  6278. keyDataType:(GPBDataType)keyDataType {
  6279. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6280. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6281. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6282. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6283. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6284. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6285. [outputStream release];
  6286. return data;
  6287. }
  6288. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6289. forGPBGenericValueKey:(GPBGenericValue *)key {
  6290. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6291. }
  6292. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6293. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6294. #pragma unused(stop)
  6295. block([NSString stringWithFormat:@"%llu", key], @(value));
  6296. }];
  6297. }
  6298. - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
  6299. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6300. if (wrapped && value) {
  6301. int32_t result = [wrapped intValue];
  6302. if (!_validationFunc(result)) {
  6303. result = kGPBUnrecognizedEnumeratorValue;
  6304. }
  6305. *value = result;
  6306. }
  6307. return (wrapped != NULL);
  6308. }
  6309. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
  6310. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6311. if (wrapped && rawValue) {
  6312. *rawValue = [wrapped intValue];
  6313. }
  6314. return (wrapped != NULL);
  6315. }
  6316. - (void)enumerateKeysAndEnumsUsingBlock:
  6317. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6318. GPBEnumValidationFunc func = _validationFunc;
  6319. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6320. NSNumber *aValue,
  6321. BOOL *stop) {
  6322. int32_t unwrapped = [aValue intValue];
  6323. if (!func(unwrapped)) {
  6324. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6325. }
  6326. block([aKey unsignedLongLongValue], unwrapped, stop);
  6327. }];
  6328. }
  6329. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6330. if (otherDictionary) {
  6331. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6332. if (_autocreator) {
  6333. GPBAutocreatedDictionaryModified(_autocreator, self);
  6334. }
  6335. }
  6336. }
  6337. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6338. [_dictionary setObject:@(value) forKey:@(key)];
  6339. if (_autocreator) {
  6340. GPBAutocreatedDictionaryModified(_autocreator, self);
  6341. }
  6342. }
  6343. - (void)removeEnumForKey:(uint64_t)aKey {
  6344. [_dictionary removeObjectForKey:@(aKey)];
  6345. }
  6346. - (void)removeAll {
  6347. [_dictionary removeAllObjects];
  6348. }
  6349. - (void)setEnum:(int32_t)value forKey:(uint64_t)key {
  6350. if (!_validationFunc(value)) {
  6351. [NSException raise:NSInvalidArgumentException
  6352. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6353. value];
  6354. }
  6355. [_dictionary setObject:@(value) forKey:@(key)];
  6356. if (_autocreator) {
  6357. GPBAutocreatedDictionaryModified(_autocreator, self);
  6358. }
  6359. }
  6360. @end
  6361. #pragma mark - UInt64 -> Object
  6362. @implementation GPBUInt64ObjectDictionary {
  6363. @package
  6364. NSMutableDictionary *_dictionary;
  6365. }
  6366. + (instancetype)dictionary {
  6367. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6368. }
  6369. + (instancetype)dictionaryWithObject:(id)object
  6370. forKey:(uint64_t)key {
  6371. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6372. // on to get the type correct.
  6373. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6374. forKeys:&key
  6375. count:1] autorelease];
  6376. }
  6377. + (instancetype)dictionaryWithObjects:(const id [])objects
  6378. forKeys:(const uint64_t [])keys
  6379. count:(NSUInteger)count {
  6380. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6381. // on to get the type correct.
  6382. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6383. forKeys:keys
  6384. count:count] autorelease];
  6385. }
  6386. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6387. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6388. // on to get the type correct.
  6389. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6390. }
  6391. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6392. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6393. }
  6394. - (instancetype)init {
  6395. return [self initWithObjects:NULL forKeys:NULL count:0];
  6396. }
  6397. - (instancetype)initWithObjects:(const id [])objects
  6398. forKeys:(const uint64_t [])keys
  6399. count:(NSUInteger)count {
  6400. self = [super init];
  6401. if (self) {
  6402. _dictionary = [[NSMutableDictionary alloc] init];
  6403. if (count && objects && keys) {
  6404. for (NSUInteger i = 0; i < count; ++i) {
  6405. if (!objects[i]) {
  6406. [NSException raise:NSInvalidArgumentException
  6407. format:@"Attempting to add nil object to a Dictionary"];
  6408. }
  6409. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6410. }
  6411. }
  6412. }
  6413. return self;
  6414. }
  6415. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6416. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6417. if (self) {
  6418. if (dictionary) {
  6419. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6420. }
  6421. }
  6422. return self;
  6423. }
  6424. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6425. #pragma unused(numItems)
  6426. return [self initWithObjects:NULL forKeys:NULL count:0];
  6427. }
  6428. - (void)dealloc {
  6429. NSAssert(!_autocreator,
  6430. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6431. [self class], _autocreator);
  6432. [_dictionary release];
  6433. [super dealloc];
  6434. }
  6435. - (instancetype)copyWithZone:(NSZone *)zone {
  6436. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6437. }
  6438. - (BOOL)isEqual:(id)other {
  6439. if (self == other) {
  6440. return YES;
  6441. }
  6442. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6443. return NO;
  6444. }
  6445. GPBUInt64ObjectDictionary *otherDictionary = other;
  6446. return [_dictionary isEqual:otherDictionary->_dictionary];
  6447. }
  6448. - (NSUInteger)hash {
  6449. return _dictionary.count;
  6450. }
  6451. - (NSString *)description {
  6452. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6453. }
  6454. - (NSUInteger)count {
  6455. return _dictionary.count;
  6456. }
  6457. - (void)enumerateKeysAndObjectsUsingBlock:
  6458. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6460. id aObject,
  6461. BOOL *stop) {
  6462. block([aKey unsignedLongLongValue], aObject, stop);
  6463. }];
  6464. }
  6465. - (BOOL)isInitialized {
  6466. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6467. if (!msg.initialized) {
  6468. return NO;
  6469. }
  6470. }
  6471. return YES;
  6472. }
  6473. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6474. GPBUInt64ObjectDictionary *newDict =
  6475. [[GPBUInt64ObjectDictionary alloc] init];
  6476. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6477. GPBMessage *msg,
  6478. BOOL *stop) {
  6479. #pragma unused(stop)
  6480. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6481. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6482. [copiedMsg release];
  6483. }];
  6484. return newDict;
  6485. }
  6486. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6487. NSUInteger count = _dictionary.count;
  6488. if (count == 0) {
  6489. return 0;
  6490. }
  6491. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6492. GPBDataType keyDataType = field.mapKeyDataType;
  6493. __block size_t result = 0;
  6494. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6495. id aObject,
  6496. BOOL *stop) {
  6497. #pragma unused(stop)
  6498. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6499. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6500. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6501. }];
  6502. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6503. result += tagSize * count;
  6504. return result;
  6505. }
  6506. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6507. asField:(GPBFieldDescriptor *)field {
  6508. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6509. GPBDataType keyDataType = field.mapKeyDataType;
  6510. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6511. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6512. id aObject,
  6513. BOOL *stop) {
  6514. #pragma unused(stop)
  6515. // Write the tag.
  6516. [outputStream writeInt32NoTag:tag];
  6517. // Write the size of the message.
  6518. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6519. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6520. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6521. // Write the fields.
  6522. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6523. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  6524. }];
  6525. }
  6526. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6527. forGPBGenericValueKey:(GPBGenericValue *)key {
  6528. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6529. }
  6530. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6531. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6532. #pragma unused(stop)
  6533. block([NSString stringWithFormat:@"%llu", key], object);
  6534. }];
  6535. }
  6536. - (id)objectForKey:(uint64_t)key {
  6537. id result = [_dictionary objectForKey:@(key)];
  6538. return result;
  6539. }
  6540. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6541. if (otherDictionary) {
  6542. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6543. if (_autocreator) {
  6544. GPBAutocreatedDictionaryModified(_autocreator, self);
  6545. }
  6546. }
  6547. }
  6548. - (void)setObject:(id)object forKey:(uint64_t)key {
  6549. if (!object) {
  6550. [NSException raise:NSInvalidArgumentException
  6551. format:@"Attempting to add nil object to a Dictionary"];
  6552. }
  6553. [_dictionary setObject:object forKey:@(key)];
  6554. if (_autocreator) {
  6555. GPBAutocreatedDictionaryModified(_autocreator, self);
  6556. }
  6557. }
  6558. - (void)removeObjectForKey:(uint64_t)aKey {
  6559. [_dictionary removeObjectForKey:@(aKey)];
  6560. }
  6561. - (void)removeAll {
  6562. [_dictionary removeAllObjects];
  6563. }
  6564. @end
  6565. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6566. // This block of code is generated, do not edit it directly.
  6567. #pragma mark - Int64 -> UInt32
  6568. @implementation GPBInt64UInt32Dictionary {
  6569. @package
  6570. NSMutableDictionary *_dictionary;
  6571. }
  6572. + (instancetype)dictionary {
  6573. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  6574. }
  6575. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  6576. forKey:(int64_t)key {
  6577. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  6578. // on to get the type correct.
  6579. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  6580. forKeys:&key
  6581. count:1] autorelease];
  6582. }
  6583. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  6584. forKeys:(const int64_t [])keys
  6585. count:(NSUInteger)count {
  6586. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  6587. // on to get the type correct.
  6588. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
  6589. forKeys:keys
  6590. count:count] autorelease];
  6591. }
  6592. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6593. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6594. // on to get the type correct.
  6595. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6596. }
  6597. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6598. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6599. }
  6600. - (instancetype)init {
  6601. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  6602. }
  6603. - (instancetype)initWithUInt32s:(const uint32_t [])values
  6604. forKeys:(const int64_t [])keys
  6605. count:(NSUInteger)count {
  6606. self = [super init];
  6607. if (self) {
  6608. _dictionary = [[NSMutableDictionary alloc] init];
  6609. if (count && values && keys) {
  6610. for (NSUInteger i = 0; i < count; ++i) {
  6611. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6612. }
  6613. }
  6614. }
  6615. return self;
  6616. }
  6617. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6618. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  6619. if (self) {
  6620. if (dictionary) {
  6621. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6622. }
  6623. }
  6624. return self;
  6625. }
  6626. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6627. #pragma unused(numItems)
  6628. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  6629. }
  6630. - (void)dealloc {
  6631. NSAssert(!_autocreator,
  6632. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6633. [self class], _autocreator);
  6634. [_dictionary release];
  6635. [super dealloc];
  6636. }
  6637. - (instancetype)copyWithZone:(NSZone *)zone {
  6638. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6639. }
  6640. - (BOOL)isEqual:(id)other {
  6641. if (self == other) {
  6642. return YES;
  6643. }
  6644. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6645. return NO;
  6646. }
  6647. GPBInt64UInt32Dictionary *otherDictionary = other;
  6648. return [_dictionary isEqual:otherDictionary->_dictionary];
  6649. }
  6650. - (NSUInteger)hash {
  6651. return _dictionary.count;
  6652. }
  6653. - (NSString *)description {
  6654. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6655. }
  6656. - (NSUInteger)count {
  6657. return _dictionary.count;
  6658. }
  6659. - (void)enumerateKeysAndUInt32sUsingBlock:
  6660. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6661. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6662. NSNumber *aValue,
  6663. BOOL *stop) {
  6664. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6665. }];
  6666. }
  6667. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6668. NSUInteger count = _dictionary.count;
  6669. if (count == 0) {
  6670. return 0;
  6671. }
  6672. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6673. GPBDataType keyDataType = field.mapKeyDataType;
  6674. __block size_t result = 0;
  6675. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6676. NSNumber *aValue,
  6677. BOOL *stop) {
  6678. #pragma unused(stop)
  6679. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6680. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6681. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6682. }];
  6683. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6684. result += tagSize * count;
  6685. return result;
  6686. }
  6687. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6688. asField:(GPBFieldDescriptor *)field {
  6689. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6690. GPBDataType keyDataType = field.mapKeyDataType;
  6691. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6692. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6693. NSNumber *aValue,
  6694. BOOL *stop) {
  6695. #pragma unused(stop)
  6696. // Write the tag.
  6697. [outputStream writeInt32NoTag:tag];
  6698. // Write the size of the message.
  6699. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6700. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6701. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6702. // Write the fields.
  6703. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6704. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6705. }];
  6706. }
  6707. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6708. forGPBGenericValueKey:(GPBGenericValue *)key {
  6709. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6710. }
  6711. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6712. [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6713. #pragma unused(stop)
  6714. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6715. }];
  6716. }
  6717. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
  6718. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6719. if (wrapped && value) {
  6720. *value = [wrapped unsignedIntValue];
  6721. }
  6722. return (wrapped != NULL);
  6723. }
  6724. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6725. if (otherDictionary) {
  6726. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6727. if (_autocreator) {
  6728. GPBAutocreatedDictionaryModified(_autocreator, self);
  6729. }
  6730. }
  6731. }
  6732. - (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
  6733. [_dictionary setObject:@(value) forKey:@(key)];
  6734. if (_autocreator) {
  6735. GPBAutocreatedDictionaryModified(_autocreator, self);
  6736. }
  6737. }
  6738. - (void)removeUInt32ForKey:(int64_t)aKey {
  6739. [_dictionary removeObjectForKey:@(aKey)];
  6740. }
  6741. - (void)removeAll {
  6742. [_dictionary removeAllObjects];
  6743. }
  6744. @end
  6745. #pragma mark - Int64 -> Int32
  6746. @implementation GPBInt64Int32Dictionary {
  6747. @package
  6748. NSMutableDictionary *_dictionary;
  6749. }
  6750. + (instancetype)dictionary {
  6751. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  6752. }
  6753. + (instancetype)dictionaryWithInt32:(int32_t)value
  6754. forKey:(int64_t)key {
  6755. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  6756. // on to get the type correct.
  6757. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
  6758. forKeys:&key
  6759. count:1] autorelease];
  6760. }
  6761. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  6762. forKeys:(const int64_t [])keys
  6763. count:(NSUInteger)count {
  6764. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  6765. // on to get the type correct.
  6766. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:values
  6767. forKeys:keys
  6768. count:count] autorelease];
  6769. }
  6770. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6771. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6772. // on to get the type correct.
  6773. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6774. }
  6775. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6776. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6777. }
  6778. - (instancetype)init {
  6779. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6780. }
  6781. - (instancetype)initWithInt32s:(const int32_t [])values
  6782. forKeys:(const int64_t [])keys
  6783. count:(NSUInteger)count {
  6784. self = [super init];
  6785. if (self) {
  6786. _dictionary = [[NSMutableDictionary alloc] init];
  6787. if (count && values && keys) {
  6788. for (NSUInteger i = 0; i < count; ++i) {
  6789. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6790. }
  6791. }
  6792. }
  6793. return self;
  6794. }
  6795. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6796. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  6797. if (self) {
  6798. if (dictionary) {
  6799. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6800. }
  6801. }
  6802. return self;
  6803. }
  6804. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6805. #pragma unused(numItems)
  6806. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6807. }
  6808. - (void)dealloc {
  6809. NSAssert(!_autocreator,
  6810. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6811. [self class], _autocreator);
  6812. [_dictionary release];
  6813. [super dealloc];
  6814. }
  6815. - (instancetype)copyWithZone:(NSZone *)zone {
  6816. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6817. }
  6818. - (BOOL)isEqual:(id)other {
  6819. if (self == other) {
  6820. return YES;
  6821. }
  6822. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6823. return NO;
  6824. }
  6825. GPBInt64Int32Dictionary *otherDictionary = other;
  6826. return [_dictionary isEqual:otherDictionary->_dictionary];
  6827. }
  6828. - (NSUInteger)hash {
  6829. return _dictionary.count;
  6830. }
  6831. - (NSString *)description {
  6832. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6833. }
  6834. - (NSUInteger)count {
  6835. return _dictionary.count;
  6836. }
  6837. - (void)enumerateKeysAndInt32sUsingBlock:
  6838. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6839. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6840. NSNumber *aValue,
  6841. BOOL *stop) {
  6842. block([aKey longLongValue], [aValue intValue], stop);
  6843. }];
  6844. }
  6845. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6846. NSUInteger count = _dictionary.count;
  6847. if (count == 0) {
  6848. return 0;
  6849. }
  6850. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6851. GPBDataType keyDataType = field.mapKeyDataType;
  6852. __block size_t result = 0;
  6853. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6854. NSNumber *aValue,
  6855. BOOL *stop) {
  6856. #pragma unused(stop)
  6857. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6858. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6859. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6860. }];
  6861. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6862. result += tagSize * count;
  6863. return result;
  6864. }
  6865. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6866. asField:(GPBFieldDescriptor *)field {
  6867. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6868. GPBDataType keyDataType = field.mapKeyDataType;
  6869. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6870. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6871. NSNumber *aValue,
  6872. BOOL *stop) {
  6873. #pragma unused(stop)
  6874. // Write the tag.
  6875. [outputStream writeInt32NoTag:tag];
  6876. // Write the size of the message.
  6877. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6878. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6879. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6880. // Write the fields.
  6881. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6882. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6883. }];
  6884. }
  6885. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6886. forGPBGenericValueKey:(GPBGenericValue *)key {
  6887. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6888. }
  6889. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6890. [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6891. #pragma unused(stop)
  6892. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6893. }];
  6894. }
  6895. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
  6896. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6897. if (wrapped && value) {
  6898. *value = [wrapped intValue];
  6899. }
  6900. return (wrapped != NULL);
  6901. }
  6902. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6903. if (otherDictionary) {
  6904. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6905. if (_autocreator) {
  6906. GPBAutocreatedDictionaryModified(_autocreator, self);
  6907. }
  6908. }
  6909. }
  6910. - (void)setInt32:(int32_t)value forKey:(int64_t)key {
  6911. [_dictionary setObject:@(value) forKey:@(key)];
  6912. if (_autocreator) {
  6913. GPBAutocreatedDictionaryModified(_autocreator, self);
  6914. }
  6915. }
  6916. - (void)removeInt32ForKey:(int64_t)aKey {
  6917. [_dictionary removeObjectForKey:@(aKey)];
  6918. }
  6919. - (void)removeAll {
  6920. [_dictionary removeAllObjects];
  6921. }
  6922. @end
  6923. #pragma mark - Int64 -> UInt64
  6924. @implementation GPBInt64UInt64Dictionary {
  6925. @package
  6926. NSMutableDictionary *_dictionary;
  6927. }
  6928. + (instancetype)dictionary {
  6929. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  6930. }
  6931. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  6932. forKey:(int64_t)key {
  6933. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  6934. // on to get the type correct.
  6935. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  6936. forKeys:&key
  6937. count:1] autorelease];
  6938. }
  6939. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  6940. forKeys:(const int64_t [])keys
  6941. count:(NSUInteger)count {
  6942. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  6943. // on to get the type correct.
  6944. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
  6945. forKeys:keys
  6946. count:count] autorelease];
  6947. }
  6948. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6949. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6950. // on to get the type correct.
  6951. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6952. }
  6953. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6954. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6955. }
  6956. - (instancetype)init {
  6957. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6958. }
  6959. - (instancetype)initWithUInt64s:(const uint64_t [])values
  6960. forKeys:(const int64_t [])keys
  6961. count:(NSUInteger)count {
  6962. self = [super init];
  6963. if (self) {
  6964. _dictionary = [[NSMutableDictionary alloc] init];
  6965. if (count && values && keys) {
  6966. for (NSUInteger i = 0; i < count; ++i) {
  6967. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6968. }
  6969. }
  6970. }
  6971. return self;
  6972. }
  6973. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6974. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  6975. if (self) {
  6976. if (dictionary) {
  6977. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6978. }
  6979. }
  6980. return self;
  6981. }
  6982. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6983. #pragma unused(numItems)
  6984. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6985. }
  6986. - (void)dealloc {
  6987. NSAssert(!_autocreator,
  6988. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6989. [self class], _autocreator);
  6990. [_dictionary release];
  6991. [super dealloc];
  6992. }
  6993. - (instancetype)copyWithZone:(NSZone *)zone {
  6994. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6995. }
  6996. - (BOOL)isEqual:(id)other {
  6997. if (self == other) {
  6998. return YES;
  6999. }
  7000. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  7001. return NO;
  7002. }
  7003. GPBInt64UInt64Dictionary *otherDictionary = other;
  7004. return [_dictionary isEqual:otherDictionary->_dictionary];
  7005. }
  7006. - (NSUInteger)hash {
  7007. return _dictionary.count;
  7008. }
  7009. - (NSString *)description {
  7010. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7011. }
  7012. - (NSUInteger)count {
  7013. return _dictionary.count;
  7014. }
  7015. - (void)enumerateKeysAndUInt64sUsingBlock:
  7016. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  7017. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7018. NSNumber *aValue,
  7019. BOOL *stop) {
  7020. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  7021. }];
  7022. }
  7023. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7024. NSUInteger count = _dictionary.count;
  7025. if (count == 0) {
  7026. return 0;
  7027. }
  7028. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7029. GPBDataType keyDataType = field.mapKeyDataType;
  7030. __block size_t result = 0;
  7031. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7032. NSNumber *aValue,
  7033. BOOL *stop) {
  7034. #pragma unused(stop)
  7035. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7036. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7037. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7038. }];
  7039. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7040. result += tagSize * count;
  7041. return result;
  7042. }
  7043. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7044. asField:(GPBFieldDescriptor *)field {
  7045. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7046. GPBDataType keyDataType = field.mapKeyDataType;
  7047. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7048. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7049. NSNumber *aValue,
  7050. BOOL *stop) {
  7051. #pragma unused(stop)
  7052. // Write the tag.
  7053. [outputStream writeInt32NoTag:tag];
  7054. // Write the size of the message.
  7055. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7056. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7057. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7058. // Write the fields.
  7059. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7060. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7061. }];
  7062. }
  7063. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7064. forGPBGenericValueKey:(GPBGenericValue *)key {
  7065. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  7066. }
  7067. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7068. [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  7069. #pragma unused(stop)
  7070. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  7071. }];
  7072. }
  7073. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
  7074. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7075. if (wrapped && value) {
  7076. *value = [wrapped unsignedLongLongValue];
  7077. }
  7078. return (wrapped != NULL);
  7079. }
  7080. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  7081. if (otherDictionary) {
  7082. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7083. if (_autocreator) {
  7084. GPBAutocreatedDictionaryModified(_autocreator, self);
  7085. }
  7086. }
  7087. }
  7088. - (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
  7089. [_dictionary setObject:@(value) forKey:@(key)];
  7090. if (_autocreator) {
  7091. GPBAutocreatedDictionaryModified(_autocreator, self);
  7092. }
  7093. }
  7094. - (void)removeUInt64ForKey:(int64_t)aKey {
  7095. [_dictionary removeObjectForKey:@(aKey)];
  7096. }
  7097. - (void)removeAll {
  7098. [_dictionary removeAllObjects];
  7099. }
  7100. @end
  7101. #pragma mark - Int64 -> Int64
  7102. @implementation GPBInt64Int64Dictionary {
  7103. @package
  7104. NSMutableDictionary *_dictionary;
  7105. }
  7106. + (instancetype)dictionary {
  7107. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  7108. }
  7109. + (instancetype)dictionaryWithInt64:(int64_t)value
  7110. forKey:(int64_t)key {
  7111. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  7112. // on to get the type correct.
  7113. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
  7114. forKeys:&key
  7115. count:1] autorelease];
  7116. }
  7117. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  7118. forKeys:(const int64_t [])keys
  7119. count:(NSUInteger)count {
  7120. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  7121. // on to get the type correct.
  7122. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:values
  7123. forKeys:keys
  7124. count:count] autorelease];
  7125. }
  7126. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7127. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7128. // on to get the type correct.
  7129. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7130. }
  7131. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7132. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7133. }
  7134. - (instancetype)init {
  7135. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7136. }
  7137. - (instancetype)initWithInt64s:(const int64_t [])values
  7138. forKeys:(const int64_t [])keys
  7139. count:(NSUInteger)count {
  7140. self = [super init];
  7141. if (self) {
  7142. _dictionary = [[NSMutableDictionary alloc] init];
  7143. if (count && values && keys) {
  7144. for (NSUInteger i = 0; i < count; ++i) {
  7145. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7146. }
  7147. }
  7148. }
  7149. return self;
  7150. }
  7151. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7152. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  7153. if (self) {
  7154. if (dictionary) {
  7155. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7156. }
  7157. }
  7158. return self;
  7159. }
  7160. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7161. #pragma unused(numItems)
  7162. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7163. }
  7164. - (void)dealloc {
  7165. NSAssert(!_autocreator,
  7166. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7167. [self class], _autocreator);
  7168. [_dictionary release];
  7169. [super dealloc];
  7170. }
  7171. - (instancetype)copyWithZone:(NSZone *)zone {
  7172. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7173. }
  7174. - (BOOL)isEqual:(id)other {
  7175. if (self == other) {
  7176. return YES;
  7177. }
  7178. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7179. return NO;
  7180. }
  7181. GPBInt64Int64Dictionary *otherDictionary = other;
  7182. return [_dictionary isEqual:otherDictionary->_dictionary];
  7183. }
  7184. - (NSUInteger)hash {
  7185. return _dictionary.count;
  7186. }
  7187. - (NSString *)description {
  7188. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7189. }
  7190. - (NSUInteger)count {
  7191. return _dictionary.count;
  7192. }
  7193. - (void)enumerateKeysAndInt64sUsingBlock:
  7194. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7195. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7196. NSNumber *aValue,
  7197. BOOL *stop) {
  7198. block([aKey longLongValue], [aValue longLongValue], stop);
  7199. }];
  7200. }
  7201. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7202. NSUInteger count = _dictionary.count;
  7203. if (count == 0) {
  7204. return 0;
  7205. }
  7206. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7207. GPBDataType keyDataType = field.mapKeyDataType;
  7208. __block size_t result = 0;
  7209. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7210. NSNumber *aValue,
  7211. BOOL *stop) {
  7212. #pragma unused(stop)
  7213. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7214. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7215. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7216. }];
  7217. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7218. result += tagSize * count;
  7219. return result;
  7220. }
  7221. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7222. asField:(GPBFieldDescriptor *)field {
  7223. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7224. GPBDataType keyDataType = field.mapKeyDataType;
  7225. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7226. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7227. NSNumber *aValue,
  7228. BOOL *stop) {
  7229. #pragma unused(stop)
  7230. // Write the tag.
  7231. [outputStream writeInt32NoTag:tag];
  7232. // Write the size of the message.
  7233. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7234. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7235. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7236. // Write the fields.
  7237. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7238. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7239. }];
  7240. }
  7241. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7242. forGPBGenericValueKey:(GPBGenericValue *)key {
  7243. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7244. }
  7245. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7246. [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7247. #pragma unused(stop)
  7248. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7249. }];
  7250. }
  7251. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
  7252. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7253. if (wrapped && value) {
  7254. *value = [wrapped longLongValue];
  7255. }
  7256. return (wrapped != NULL);
  7257. }
  7258. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7259. if (otherDictionary) {
  7260. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7261. if (_autocreator) {
  7262. GPBAutocreatedDictionaryModified(_autocreator, self);
  7263. }
  7264. }
  7265. }
  7266. - (void)setInt64:(int64_t)value forKey:(int64_t)key {
  7267. [_dictionary setObject:@(value) forKey:@(key)];
  7268. if (_autocreator) {
  7269. GPBAutocreatedDictionaryModified(_autocreator, self);
  7270. }
  7271. }
  7272. - (void)removeInt64ForKey:(int64_t)aKey {
  7273. [_dictionary removeObjectForKey:@(aKey)];
  7274. }
  7275. - (void)removeAll {
  7276. [_dictionary removeAllObjects];
  7277. }
  7278. @end
  7279. #pragma mark - Int64 -> Bool
  7280. @implementation GPBInt64BoolDictionary {
  7281. @package
  7282. NSMutableDictionary *_dictionary;
  7283. }
  7284. + (instancetype)dictionary {
  7285. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  7286. }
  7287. + (instancetype)dictionaryWithBool:(BOOL)value
  7288. forKey:(int64_t)key {
  7289. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  7290. // on to get the type correct.
  7291. return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:&value
  7292. forKeys:&key
  7293. count:1] autorelease];
  7294. }
  7295. + (instancetype)dictionaryWithBools:(const BOOL [])values
  7296. forKeys:(const int64_t [])keys
  7297. count:(NSUInteger)count {
  7298. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  7299. // on to get the type correct.
  7300. return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:values
  7301. forKeys:keys
  7302. count:count] autorelease];
  7303. }
  7304. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7305. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7306. // on to get the type correct.
  7307. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7308. }
  7309. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7310. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7311. }
  7312. - (instancetype)init {
  7313. return [self initWithBools:NULL forKeys:NULL count:0];
  7314. }
  7315. - (instancetype)initWithBools:(const BOOL [])values
  7316. forKeys:(const int64_t [])keys
  7317. count:(NSUInteger)count {
  7318. self = [super init];
  7319. if (self) {
  7320. _dictionary = [[NSMutableDictionary alloc] init];
  7321. if (count && values && keys) {
  7322. for (NSUInteger i = 0; i < count; ++i) {
  7323. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7324. }
  7325. }
  7326. }
  7327. return self;
  7328. }
  7329. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7330. self = [self initWithBools:NULL forKeys:NULL count:0];
  7331. if (self) {
  7332. if (dictionary) {
  7333. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7334. }
  7335. }
  7336. return self;
  7337. }
  7338. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7339. #pragma unused(numItems)
  7340. return [self initWithBools:NULL forKeys:NULL count:0];
  7341. }
  7342. - (void)dealloc {
  7343. NSAssert(!_autocreator,
  7344. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7345. [self class], _autocreator);
  7346. [_dictionary release];
  7347. [super dealloc];
  7348. }
  7349. - (instancetype)copyWithZone:(NSZone *)zone {
  7350. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7351. }
  7352. - (BOOL)isEqual:(id)other {
  7353. if (self == other) {
  7354. return YES;
  7355. }
  7356. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7357. return NO;
  7358. }
  7359. GPBInt64BoolDictionary *otherDictionary = other;
  7360. return [_dictionary isEqual:otherDictionary->_dictionary];
  7361. }
  7362. - (NSUInteger)hash {
  7363. return _dictionary.count;
  7364. }
  7365. - (NSString *)description {
  7366. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7367. }
  7368. - (NSUInteger)count {
  7369. return _dictionary.count;
  7370. }
  7371. - (void)enumerateKeysAndBoolsUsingBlock:
  7372. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7373. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7374. NSNumber *aValue,
  7375. BOOL *stop) {
  7376. block([aKey longLongValue], [aValue boolValue], stop);
  7377. }];
  7378. }
  7379. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7380. NSUInteger count = _dictionary.count;
  7381. if (count == 0) {
  7382. return 0;
  7383. }
  7384. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7385. GPBDataType keyDataType = field.mapKeyDataType;
  7386. __block size_t result = 0;
  7387. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7388. NSNumber *aValue,
  7389. BOOL *stop) {
  7390. #pragma unused(stop)
  7391. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7392. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7393. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7394. }];
  7395. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7396. result += tagSize * count;
  7397. return result;
  7398. }
  7399. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7400. asField:(GPBFieldDescriptor *)field {
  7401. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7402. GPBDataType keyDataType = field.mapKeyDataType;
  7403. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7404. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7405. NSNumber *aValue,
  7406. BOOL *stop) {
  7407. #pragma unused(stop)
  7408. // Write the tag.
  7409. [outputStream writeInt32NoTag:tag];
  7410. // Write the size of the message.
  7411. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7412. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7413. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7414. // Write the fields.
  7415. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7416. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7417. }];
  7418. }
  7419. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7420. forGPBGenericValueKey:(GPBGenericValue *)key {
  7421. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7422. }
  7423. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7424. [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7425. #pragma unused(stop)
  7426. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7427. }];
  7428. }
  7429. - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
  7430. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7431. if (wrapped && value) {
  7432. *value = [wrapped boolValue];
  7433. }
  7434. return (wrapped != NULL);
  7435. }
  7436. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7437. if (otherDictionary) {
  7438. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7439. if (_autocreator) {
  7440. GPBAutocreatedDictionaryModified(_autocreator, self);
  7441. }
  7442. }
  7443. }
  7444. - (void)setBool:(BOOL)value forKey:(int64_t)key {
  7445. [_dictionary setObject:@(value) forKey:@(key)];
  7446. if (_autocreator) {
  7447. GPBAutocreatedDictionaryModified(_autocreator, self);
  7448. }
  7449. }
  7450. - (void)removeBoolForKey:(int64_t)aKey {
  7451. [_dictionary removeObjectForKey:@(aKey)];
  7452. }
  7453. - (void)removeAll {
  7454. [_dictionary removeAllObjects];
  7455. }
  7456. @end
  7457. #pragma mark - Int64 -> Float
  7458. @implementation GPBInt64FloatDictionary {
  7459. @package
  7460. NSMutableDictionary *_dictionary;
  7461. }
  7462. + (instancetype)dictionary {
  7463. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  7464. }
  7465. + (instancetype)dictionaryWithFloat:(float)value
  7466. forKey:(int64_t)key {
  7467. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  7468. // on to get the type correct.
  7469. return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:&value
  7470. forKeys:&key
  7471. count:1] autorelease];
  7472. }
  7473. + (instancetype)dictionaryWithFloats:(const float [])values
  7474. forKeys:(const int64_t [])keys
  7475. count:(NSUInteger)count {
  7476. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  7477. // on to get the type correct.
  7478. return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:values
  7479. forKeys:keys
  7480. count:count] autorelease];
  7481. }
  7482. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7483. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7484. // on to get the type correct.
  7485. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7486. }
  7487. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7488. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7489. }
  7490. - (instancetype)init {
  7491. return [self initWithFloats:NULL forKeys:NULL count:0];
  7492. }
  7493. - (instancetype)initWithFloats:(const float [])values
  7494. forKeys:(const int64_t [])keys
  7495. count:(NSUInteger)count {
  7496. self = [super init];
  7497. if (self) {
  7498. _dictionary = [[NSMutableDictionary alloc] init];
  7499. if (count && values && keys) {
  7500. for (NSUInteger i = 0; i < count; ++i) {
  7501. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7502. }
  7503. }
  7504. }
  7505. return self;
  7506. }
  7507. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7508. self = [self initWithFloats:NULL forKeys:NULL count:0];
  7509. if (self) {
  7510. if (dictionary) {
  7511. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7512. }
  7513. }
  7514. return self;
  7515. }
  7516. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7517. #pragma unused(numItems)
  7518. return [self initWithFloats:NULL forKeys:NULL count:0];
  7519. }
  7520. - (void)dealloc {
  7521. NSAssert(!_autocreator,
  7522. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7523. [self class], _autocreator);
  7524. [_dictionary release];
  7525. [super dealloc];
  7526. }
  7527. - (instancetype)copyWithZone:(NSZone *)zone {
  7528. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7529. }
  7530. - (BOOL)isEqual:(id)other {
  7531. if (self == other) {
  7532. return YES;
  7533. }
  7534. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7535. return NO;
  7536. }
  7537. GPBInt64FloatDictionary *otherDictionary = other;
  7538. return [_dictionary isEqual:otherDictionary->_dictionary];
  7539. }
  7540. - (NSUInteger)hash {
  7541. return _dictionary.count;
  7542. }
  7543. - (NSString *)description {
  7544. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7545. }
  7546. - (NSUInteger)count {
  7547. return _dictionary.count;
  7548. }
  7549. - (void)enumerateKeysAndFloatsUsingBlock:
  7550. (void (^)(int64_t key, float value, BOOL *stop))block {
  7551. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7552. NSNumber *aValue,
  7553. BOOL *stop) {
  7554. block([aKey longLongValue], [aValue floatValue], stop);
  7555. }];
  7556. }
  7557. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7558. NSUInteger count = _dictionary.count;
  7559. if (count == 0) {
  7560. return 0;
  7561. }
  7562. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7563. GPBDataType keyDataType = field.mapKeyDataType;
  7564. __block size_t result = 0;
  7565. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7566. NSNumber *aValue,
  7567. BOOL *stop) {
  7568. #pragma unused(stop)
  7569. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7570. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7571. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7572. }];
  7573. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7574. result += tagSize * count;
  7575. return result;
  7576. }
  7577. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7578. asField:(GPBFieldDescriptor *)field {
  7579. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7580. GPBDataType keyDataType = field.mapKeyDataType;
  7581. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7582. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7583. NSNumber *aValue,
  7584. BOOL *stop) {
  7585. #pragma unused(stop)
  7586. // Write the tag.
  7587. [outputStream writeInt32NoTag:tag];
  7588. // Write the size of the message.
  7589. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7590. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7591. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7592. // Write the fields.
  7593. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7594. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7595. }];
  7596. }
  7597. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7598. forGPBGenericValueKey:(GPBGenericValue *)key {
  7599. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7600. }
  7601. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7602. [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7603. #pragma unused(stop)
  7604. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7605. }];
  7606. }
  7607. - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
  7608. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7609. if (wrapped && value) {
  7610. *value = [wrapped floatValue];
  7611. }
  7612. return (wrapped != NULL);
  7613. }
  7614. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7615. if (otherDictionary) {
  7616. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7617. if (_autocreator) {
  7618. GPBAutocreatedDictionaryModified(_autocreator, self);
  7619. }
  7620. }
  7621. }
  7622. - (void)setFloat:(float)value forKey:(int64_t)key {
  7623. [_dictionary setObject:@(value) forKey:@(key)];
  7624. if (_autocreator) {
  7625. GPBAutocreatedDictionaryModified(_autocreator, self);
  7626. }
  7627. }
  7628. - (void)removeFloatForKey:(int64_t)aKey {
  7629. [_dictionary removeObjectForKey:@(aKey)];
  7630. }
  7631. - (void)removeAll {
  7632. [_dictionary removeAllObjects];
  7633. }
  7634. @end
  7635. #pragma mark - Int64 -> Double
  7636. @implementation GPBInt64DoubleDictionary {
  7637. @package
  7638. NSMutableDictionary *_dictionary;
  7639. }
  7640. + (instancetype)dictionary {
  7641. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  7642. }
  7643. + (instancetype)dictionaryWithDouble:(double)value
  7644. forKey:(int64_t)key {
  7645. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  7646. // on to get the type correct.
  7647. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
  7648. forKeys:&key
  7649. count:1] autorelease];
  7650. }
  7651. + (instancetype)dictionaryWithDoubles:(const double [])values
  7652. forKeys:(const int64_t [])keys
  7653. count:(NSUInteger)count {
  7654. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  7655. // on to get the type correct.
  7656. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:values
  7657. forKeys:keys
  7658. count:count] autorelease];
  7659. }
  7660. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7661. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7662. // on to get the type correct.
  7663. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7664. }
  7665. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7666. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7667. }
  7668. - (instancetype)init {
  7669. return [self initWithDoubles:NULL forKeys:NULL count:0];
  7670. }
  7671. - (instancetype)initWithDoubles:(const double [])values
  7672. forKeys:(const int64_t [])keys
  7673. count:(NSUInteger)count {
  7674. self = [super init];
  7675. if (self) {
  7676. _dictionary = [[NSMutableDictionary alloc] init];
  7677. if (count && values && keys) {
  7678. for (NSUInteger i = 0; i < count; ++i) {
  7679. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7680. }
  7681. }
  7682. }
  7683. return self;
  7684. }
  7685. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7686. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  7687. if (self) {
  7688. if (dictionary) {
  7689. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7690. }
  7691. }
  7692. return self;
  7693. }
  7694. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7695. #pragma unused(numItems)
  7696. return [self initWithDoubles:NULL forKeys:NULL count:0];
  7697. }
  7698. - (void)dealloc {
  7699. NSAssert(!_autocreator,
  7700. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7701. [self class], _autocreator);
  7702. [_dictionary release];
  7703. [super dealloc];
  7704. }
  7705. - (instancetype)copyWithZone:(NSZone *)zone {
  7706. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7707. }
  7708. - (BOOL)isEqual:(id)other {
  7709. if (self == other) {
  7710. return YES;
  7711. }
  7712. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7713. return NO;
  7714. }
  7715. GPBInt64DoubleDictionary *otherDictionary = other;
  7716. return [_dictionary isEqual:otherDictionary->_dictionary];
  7717. }
  7718. - (NSUInteger)hash {
  7719. return _dictionary.count;
  7720. }
  7721. - (NSString *)description {
  7722. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7723. }
  7724. - (NSUInteger)count {
  7725. return _dictionary.count;
  7726. }
  7727. - (void)enumerateKeysAndDoublesUsingBlock:
  7728. (void (^)(int64_t key, double value, BOOL *stop))block {
  7729. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7730. NSNumber *aValue,
  7731. BOOL *stop) {
  7732. block([aKey longLongValue], [aValue doubleValue], stop);
  7733. }];
  7734. }
  7735. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7736. NSUInteger count = _dictionary.count;
  7737. if (count == 0) {
  7738. return 0;
  7739. }
  7740. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7741. GPBDataType keyDataType = field.mapKeyDataType;
  7742. __block size_t result = 0;
  7743. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7744. NSNumber *aValue,
  7745. BOOL *stop) {
  7746. #pragma unused(stop)
  7747. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7748. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7749. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7750. }];
  7751. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7752. result += tagSize * count;
  7753. return result;
  7754. }
  7755. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7756. asField:(GPBFieldDescriptor *)field {
  7757. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7758. GPBDataType keyDataType = field.mapKeyDataType;
  7759. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7760. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7761. NSNumber *aValue,
  7762. BOOL *stop) {
  7763. #pragma unused(stop)
  7764. // Write the tag.
  7765. [outputStream writeInt32NoTag:tag];
  7766. // Write the size of the message.
  7767. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7768. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7769. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7770. // Write the fields.
  7771. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7772. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7773. }];
  7774. }
  7775. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7776. forGPBGenericValueKey:(GPBGenericValue *)key {
  7777. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7778. }
  7779. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7780. [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7781. #pragma unused(stop)
  7782. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7783. }];
  7784. }
  7785. - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
  7786. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7787. if (wrapped && value) {
  7788. *value = [wrapped doubleValue];
  7789. }
  7790. return (wrapped != NULL);
  7791. }
  7792. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7793. if (otherDictionary) {
  7794. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7795. if (_autocreator) {
  7796. GPBAutocreatedDictionaryModified(_autocreator, self);
  7797. }
  7798. }
  7799. }
  7800. - (void)setDouble:(double)value forKey:(int64_t)key {
  7801. [_dictionary setObject:@(value) forKey:@(key)];
  7802. if (_autocreator) {
  7803. GPBAutocreatedDictionaryModified(_autocreator, self);
  7804. }
  7805. }
  7806. - (void)removeDoubleForKey:(int64_t)aKey {
  7807. [_dictionary removeObjectForKey:@(aKey)];
  7808. }
  7809. - (void)removeAll {
  7810. [_dictionary removeAllObjects];
  7811. }
  7812. @end
  7813. #pragma mark - Int64 -> Enum
  7814. @implementation GPBInt64EnumDictionary {
  7815. @package
  7816. NSMutableDictionary *_dictionary;
  7817. GPBEnumValidationFunc _validationFunc;
  7818. }
  7819. @synthesize validationFunc = _validationFunc;
  7820. + (instancetype)dictionary {
  7821. return [[[self alloc] initWithValidationFunction:NULL
  7822. rawValues:NULL
  7823. forKeys:NULL
  7824. count:0] autorelease];
  7825. }
  7826. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7827. return [[[self alloc] initWithValidationFunction:func
  7828. rawValues:NULL
  7829. forKeys:NULL
  7830. count:0] autorelease];
  7831. }
  7832. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7833. rawValue:(int32_t)rawValue
  7834. forKey:(int64_t)key {
  7835. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7836. // on to get the type correct.
  7837. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7838. rawValues:&rawValue
  7839. forKeys:&key
  7840. count:1] autorelease];
  7841. }
  7842. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7843. rawValues:(const int32_t [])rawValues
  7844. forKeys:(const int64_t [])keys
  7845. count:(NSUInteger)count {
  7846. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7847. // on to get the type correct.
  7848. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7849. rawValues:rawValues
  7850. forKeys:keys
  7851. count:count] autorelease];
  7852. }
  7853. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7854. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7855. // on to get the type correct.
  7856. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7857. }
  7858. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7859. capacity:(NSUInteger)numItems {
  7860. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7861. }
  7862. - (instancetype)init {
  7863. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7864. }
  7865. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7866. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7867. }
  7868. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7869. rawValues:(const int32_t [])rawValues
  7870. forKeys:(const int64_t [])keys
  7871. count:(NSUInteger)count {
  7872. self = [super init];
  7873. if (self) {
  7874. _dictionary = [[NSMutableDictionary alloc] init];
  7875. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7876. if (count && rawValues && keys) {
  7877. for (NSUInteger i = 0; i < count; ++i) {
  7878. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7879. }
  7880. }
  7881. }
  7882. return self;
  7883. }
  7884. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7885. self = [self initWithValidationFunction:dictionary.validationFunc
  7886. rawValues:NULL
  7887. forKeys:NULL
  7888. count:0];
  7889. if (self) {
  7890. if (dictionary) {
  7891. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7892. }
  7893. }
  7894. return self;
  7895. }
  7896. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7897. capacity:(NSUInteger)numItems {
  7898. #pragma unused(numItems)
  7899. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7900. }
  7901. - (void)dealloc {
  7902. NSAssert(!_autocreator,
  7903. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7904. [self class], _autocreator);
  7905. [_dictionary release];
  7906. [super dealloc];
  7907. }
  7908. - (instancetype)copyWithZone:(NSZone *)zone {
  7909. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7910. }
  7911. - (BOOL)isEqual:(id)other {
  7912. if (self == other) {
  7913. return YES;
  7914. }
  7915. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7916. return NO;
  7917. }
  7918. GPBInt64EnumDictionary *otherDictionary = other;
  7919. return [_dictionary isEqual:otherDictionary->_dictionary];
  7920. }
  7921. - (NSUInteger)hash {
  7922. return _dictionary.count;
  7923. }
  7924. - (NSString *)description {
  7925. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7926. }
  7927. - (NSUInteger)count {
  7928. return _dictionary.count;
  7929. }
  7930. - (void)enumerateKeysAndRawValuesUsingBlock:
  7931. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7932. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7933. NSNumber *aValue,
  7934. BOOL *stop) {
  7935. block([aKey longLongValue], [aValue intValue], stop);
  7936. }];
  7937. }
  7938. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7939. NSUInteger count = _dictionary.count;
  7940. if (count == 0) {
  7941. return 0;
  7942. }
  7943. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7944. GPBDataType keyDataType = field.mapKeyDataType;
  7945. __block size_t result = 0;
  7946. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7947. NSNumber *aValue,
  7948. BOOL *stop) {
  7949. #pragma unused(stop)
  7950. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7951. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7952. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7953. }];
  7954. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7955. result += tagSize * count;
  7956. return result;
  7957. }
  7958. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7959. asField:(GPBFieldDescriptor *)field {
  7960. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7961. GPBDataType keyDataType = field.mapKeyDataType;
  7962. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7963. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7964. NSNumber *aValue,
  7965. BOOL *stop) {
  7966. #pragma unused(stop)
  7967. // Write the tag.
  7968. [outputStream writeInt32NoTag:tag];
  7969. // Write the size of the message.
  7970. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7971. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7972. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7973. // Write the fields.
  7974. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7975. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7976. }];
  7977. }
  7978. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7979. forKey:(GPBGenericValue *)key
  7980. keyDataType:(GPBDataType)keyDataType {
  7981. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7982. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7983. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7984. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7985. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7986. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7987. [outputStream release];
  7988. return data;
  7989. }
  7990. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7991. forGPBGenericValueKey:(GPBGenericValue *)key {
  7992. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7993. }
  7994. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7995. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7996. #pragma unused(stop)
  7997. block([NSString stringWithFormat:@"%lld", key], @(value));
  7998. }];
  7999. }
  8000. - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
  8001. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8002. if (wrapped && value) {
  8003. int32_t result = [wrapped intValue];
  8004. if (!_validationFunc(result)) {
  8005. result = kGPBUnrecognizedEnumeratorValue;
  8006. }
  8007. *value = result;
  8008. }
  8009. return (wrapped != NULL);
  8010. }
  8011. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
  8012. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8013. if (wrapped && rawValue) {
  8014. *rawValue = [wrapped intValue];
  8015. }
  8016. return (wrapped != NULL);
  8017. }
  8018. - (void)enumerateKeysAndEnumsUsingBlock:
  8019. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  8020. GPBEnumValidationFunc func = _validationFunc;
  8021. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8022. NSNumber *aValue,
  8023. BOOL *stop) {
  8024. int32_t unwrapped = [aValue intValue];
  8025. if (!func(unwrapped)) {
  8026. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8027. }
  8028. block([aKey longLongValue], unwrapped, stop);
  8029. }];
  8030. }
  8031. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  8032. if (otherDictionary) {
  8033. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8034. if (_autocreator) {
  8035. GPBAutocreatedDictionaryModified(_autocreator, self);
  8036. }
  8037. }
  8038. }
  8039. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  8040. [_dictionary setObject:@(value) forKey:@(key)];
  8041. if (_autocreator) {
  8042. GPBAutocreatedDictionaryModified(_autocreator, self);
  8043. }
  8044. }
  8045. - (void)removeEnumForKey:(int64_t)aKey {
  8046. [_dictionary removeObjectForKey:@(aKey)];
  8047. }
  8048. - (void)removeAll {
  8049. [_dictionary removeAllObjects];
  8050. }
  8051. - (void)setEnum:(int32_t)value forKey:(int64_t)key {
  8052. if (!_validationFunc(value)) {
  8053. [NSException raise:NSInvalidArgumentException
  8054. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  8055. value];
  8056. }
  8057. [_dictionary setObject:@(value) forKey:@(key)];
  8058. if (_autocreator) {
  8059. GPBAutocreatedDictionaryModified(_autocreator, self);
  8060. }
  8061. }
  8062. @end
  8063. #pragma mark - Int64 -> Object
  8064. @implementation GPBInt64ObjectDictionary {
  8065. @package
  8066. NSMutableDictionary *_dictionary;
  8067. }
  8068. + (instancetype)dictionary {
  8069. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  8070. }
  8071. + (instancetype)dictionaryWithObject:(id)object
  8072. forKey:(int64_t)key {
  8073. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8074. // on to get the type correct.
  8075. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  8076. forKeys:&key
  8077. count:1] autorelease];
  8078. }
  8079. + (instancetype)dictionaryWithObjects:(const id [])objects
  8080. forKeys:(const int64_t [])keys
  8081. count:(NSUInteger)count {
  8082. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8083. // on to get the type correct.
  8084. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  8085. forKeys:keys
  8086. count:count] autorelease];
  8087. }
  8088. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8089. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8090. // on to get the type correct.
  8091. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8092. }
  8093. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8094. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8095. }
  8096. - (instancetype)init {
  8097. return [self initWithObjects:NULL forKeys:NULL count:0];
  8098. }
  8099. - (instancetype)initWithObjects:(const id [])objects
  8100. forKeys:(const int64_t [])keys
  8101. count:(NSUInteger)count {
  8102. self = [super init];
  8103. if (self) {
  8104. _dictionary = [[NSMutableDictionary alloc] init];
  8105. if (count && objects && keys) {
  8106. for (NSUInteger i = 0; i < count; ++i) {
  8107. if (!objects[i]) {
  8108. [NSException raise:NSInvalidArgumentException
  8109. format:@"Attempting to add nil object to a Dictionary"];
  8110. }
  8111. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  8112. }
  8113. }
  8114. }
  8115. return self;
  8116. }
  8117. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8118. self = [self initWithObjects:NULL forKeys:NULL count:0];
  8119. if (self) {
  8120. if (dictionary) {
  8121. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8122. }
  8123. }
  8124. return self;
  8125. }
  8126. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8127. #pragma unused(numItems)
  8128. return [self initWithObjects:NULL forKeys:NULL count:0];
  8129. }
  8130. - (void)dealloc {
  8131. NSAssert(!_autocreator,
  8132. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8133. [self class], _autocreator);
  8134. [_dictionary release];
  8135. [super dealloc];
  8136. }
  8137. - (instancetype)copyWithZone:(NSZone *)zone {
  8138. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8139. }
  8140. - (BOOL)isEqual:(id)other {
  8141. if (self == other) {
  8142. return YES;
  8143. }
  8144. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8145. return NO;
  8146. }
  8147. GPBInt64ObjectDictionary *otherDictionary = other;
  8148. return [_dictionary isEqual:otherDictionary->_dictionary];
  8149. }
  8150. - (NSUInteger)hash {
  8151. return _dictionary.count;
  8152. }
  8153. - (NSString *)description {
  8154. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8155. }
  8156. - (NSUInteger)count {
  8157. return _dictionary.count;
  8158. }
  8159. - (void)enumerateKeysAndObjectsUsingBlock:
  8160. (void (^)(int64_t key, id object, BOOL *stop))block {
  8161. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8162. id aObject,
  8163. BOOL *stop) {
  8164. block([aKey longLongValue], aObject, stop);
  8165. }];
  8166. }
  8167. - (BOOL)isInitialized {
  8168. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8169. if (!msg.initialized) {
  8170. return NO;
  8171. }
  8172. }
  8173. return YES;
  8174. }
  8175. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8176. GPBInt64ObjectDictionary *newDict =
  8177. [[GPBInt64ObjectDictionary alloc] init];
  8178. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8179. GPBMessage *msg,
  8180. BOOL *stop) {
  8181. #pragma unused(stop)
  8182. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8183. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8184. [copiedMsg release];
  8185. }];
  8186. return newDict;
  8187. }
  8188. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8189. NSUInteger count = _dictionary.count;
  8190. if (count == 0) {
  8191. return 0;
  8192. }
  8193. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8194. GPBDataType keyDataType = field.mapKeyDataType;
  8195. __block size_t result = 0;
  8196. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8197. id aObject,
  8198. BOOL *stop) {
  8199. #pragma unused(stop)
  8200. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8201. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8202. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8203. }];
  8204. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8205. result += tagSize * count;
  8206. return result;
  8207. }
  8208. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8209. asField:(GPBFieldDescriptor *)field {
  8210. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8211. GPBDataType keyDataType = field.mapKeyDataType;
  8212. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8213. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8214. id aObject,
  8215. BOOL *stop) {
  8216. #pragma unused(stop)
  8217. // Write the tag.
  8218. [outputStream writeInt32NoTag:tag];
  8219. // Write the size of the message.
  8220. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8221. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8222. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8223. // Write the fields.
  8224. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8225. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  8226. }];
  8227. }
  8228. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8229. forGPBGenericValueKey:(GPBGenericValue *)key {
  8230. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8231. }
  8232. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8233. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8234. #pragma unused(stop)
  8235. block([NSString stringWithFormat:@"%lld", key], object);
  8236. }];
  8237. }
  8238. - (id)objectForKey:(int64_t)key {
  8239. id result = [_dictionary objectForKey:@(key)];
  8240. return result;
  8241. }
  8242. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8243. if (otherDictionary) {
  8244. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8245. if (_autocreator) {
  8246. GPBAutocreatedDictionaryModified(_autocreator, self);
  8247. }
  8248. }
  8249. }
  8250. - (void)setObject:(id)object forKey:(int64_t)key {
  8251. if (!object) {
  8252. [NSException raise:NSInvalidArgumentException
  8253. format:@"Attempting to add nil object to a Dictionary"];
  8254. }
  8255. [_dictionary setObject:object forKey:@(key)];
  8256. if (_autocreator) {
  8257. GPBAutocreatedDictionaryModified(_autocreator, self);
  8258. }
  8259. }
  8260. - (void)removeObjectForKey:(int64_t)aKey {
  8261. [_dictionary removeObjectForKey:@(aKey)];
  8262. }
  8263. - (void)removeAll {
  8264. [_dictionary removeAllObjects];
  8265. }
  8266. @end
  8267. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8268. // This block of code is generated, do not edit it directly.
  8269. #pragma mark - String -> UInt32
  8270. @implementation GPBStringUInt32Dictionary {
  8271. @package
  8272. NSMutableDictionary *_dictionary;
  8273. }
  8274. + (instancetype)dictionary {
  8275. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  8276. }
  8277. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  8278. forKey:(NSString *)key {
  8279. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  8280. // on to get the type correct.
  8281. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:&value
  8282. forKeys:&key
  8283. count:1] autorelease];
  8284. }
  8285. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  8286. forKeys:(const NSString * [])keys
  8287. count:(NSUInteger)count {
  8288. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  8289. // on to get the type correct.
  8290. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:values
  8291. forKeys:keys
  8292. count:count] autorelease];
  8293. }
  8294. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8295. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8296. // on to get the type correct.
  8297. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8298. }
  8299. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8300. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8301. }
  8302. - (instancetype)init {
  8303. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8304. }
  8305. - (instancetype)initWithUInt32s:(const uint32_t [])values
  8306. forKeys:(const NSString * [])keys
  8307. count:(NSUInteger)count {
  8308. self = [super init];
  8309. if (self) {
  8310. _dictionary = [[NSMutableDictionary alloc] init];
  8311. if (count && values && keys) {
  8312. for (NSUInteger i = 0; i < count; ++i) {
  8313. if (!keys[i]) {
  8314. [NSException raise:NSInvalidArgumentException
  8315. format:@"Attempting to add nil key to a Dictionary"];
  8316. }
  8317. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8318. }
  8319. }
  8320. }
  8321. return self;
  8322. }
  8323. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8324. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  8325. if (self) {
  8326. if (dictionary) {
  8327. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8328. }
  8329. }
  8330. return self;
  8331. }
  8332. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8333. #pragma unused(numItems)
  8334. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8335. }
  8336. - (void)dealloc {
  8337. NSAssert(!_autocreator,
  8338. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8339. [self class], _autocreator);
  8340. [_dictionary release];
  8341. [super dealloc];
  8342. }
  8343. - (instancetype)copyWithZone:(NSZone *)zone {
  8344. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8345. }
  8346. - (BOOL)isEqual:(id)other {
  8347. if (self == other) {
  8348. return YES;
  8349. }
  8350. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8351. return NO;
  8352. }
  8353. GPBStringUInt32Dictionary *otherDictionary = other;
  8354. return [_dictionary isEqual:otherDictionary->_dictionary];
  8355. }
  8356. - (NSUInteger)hash {
  8357. return _dictionary.count;
  8358. }
  8359. - (NSString *)description {
  8360. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8361. }
  8362. - (NSUInteger)count {
  8363. return _dictionary.count;
  8364. }
  8365. - (void)enumerateKeysAndUInt32sUsingBlock:
  8366. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8367. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8368. NSNumber *aValue,
  8369. BOOL *stop) {
  8370. block(aKey, [aValue unsignedIntValue], stop);
  8371. }];
  8372. }
  8373. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8374. NSUInteger count = _dictionary.count;
  8375. if (count == 0) {
  8376. return 0;
  8377. }
  8378. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8379. GPBDataType keyDataType = field.mapKeyDataType;
  8380. __block size_t result = 0;
  8381. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8382. NSNumber *aValue,
  8383. BOOL *stop) {
  8384. #pragma unused(stop)
  8385. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8386. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8387. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8388. }];
  8389. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8390. result += tagSize * count;
  8391. return result;
  8392. }
  8393. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8394. asField:(GPBFieldDescriptor *)field {
  8395. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8396. GPBDataType keyDataType = field.mapKeyDataType;
  8397. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8398. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8399. NSNumber *aValue,
  8400. BOOL *stop) {
  8401. #pragma unused(stop)
  8402. // Write the tag.
  8403. [outputStream writeInt32NoTag:tag];
  8404. // Write the size of the message.
  8405. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8406. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8407. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8408. // Write the fields.
  8409. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8410. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8411. }];
  8412. }
  8413. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8414. forGPBGenericValueKey:(GPBGenericValue *)key {
  8415. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8416. }
  8417. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8418. [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8419. #pragma unused(stop)
  8420. block(key, [NSString stringWithFormat:@"%u", value]);
  8421. }];
  8422. }
  8423. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
  8424. NSNumber *wrapped = [_dictionary objectForKey:key];
  8425. if (wrapped && value) {
  8426. *value = [wrapped unsignedIntValue];
  8427. }
  8428. return (wrapped != NULL);
  8429. }
  8430. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8431. if (otherDictionary) {
  8432. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8433. if (_autocreator) {
  8434. GPBAutocreatedDictionaryModified(_autocreator, self);
  8435. }
  8436. }
  8437. }
  8438. - (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
  8439. if (!key) {
  8440. [NSException raise:NSInvalidArgumentException
  8441. format:@"Attempting to add nil key to a Dictionary"];
  8442. }
  8443. [_dictionary setObject:@(value) forKey:key];
  8444. if (_autocreator) {
  8445. GPBAutocreatedDictionaryModified(_autocreator, self);
  8446. }
  8447. }
  8448. - (void)removeUInt32ForKey:(NSString *)aKey {
  8449. [_dictionary removeObjectForKey:aKey];
  8450. }
  8451. - (void)removeAll {
  8452. [_dictionary removeAllObjects];
  8453. }
  8454. @end
  8455. #pragma mark - String -> Int32
  8456. @implementation GPBStringInt32Dictionary {
  8457. @package
  8458. NSMutableDictionary *_dictionary;
  8459. }
  8460. + (instancetype)dictionary {
  8461. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  8462. }
  8463. + (instancetype)dictionaryWithInt32:(int32_t)value
  8464. forKey:(NSString *)key {
  8465. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  8466. // on to get the type correct.
  8467. return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:&value
  8468. forKeys:&key
  8469. count:1] autorelease];
  8470. }
  8471. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  8472. forKeys:(const NSString * [])keys
  8473. count:(NSUInteger)count {
  8474. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  8475. // on to get the type correct.
  8476. return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:values
  8477. forKeys:keys
  8478. count:count] autorelease];
  8479. }
  8480. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8481. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8482. // on to get the type correct.
  8483. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8484. }
  8485. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8486. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8487. }
  8488. - (instancetype)init {
  8489. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8490. }
  8491. - (instancetype)initWithInt32s:(const int32_t [])values
  8492. forKeys:(const NSString * [])keys
  8493. count:(NSUInteger)count {
  8494. self = [super init];
  8495. if (self) {
  8496. _dictionary = [[NSMutableDictionary alloc] init];
  8497. if (count && values && keys) {
  8498. for (NSUInteger i = 0; i < count; ++i) {
  8499. if (!keys[i]) {
  8500. [NSException raise:NSInvalidArgumentException
  8501. format:@"Attempting to add nil key to a Dictionary"];
  8502. }
  8503. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8504. }
  8505. }
  8506. }
  8507. return self;
  8508. }
  8509. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8510. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  8511. if (self) {
  8512. if (dictionary) {
  8513. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8514. }
  8515. }
  8516. return self;
  8517. }
  8518. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8519. #pragma unused(numItems)
  8520. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8521. }
  8522. - (void)dealloc {
  8523. NSAssert(!_autocreator,
  8524. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8525. [self class], _autocreator);
  8526. [_dictionary release];
  8527. [super dealloc];
  8528. }
  8529. - (instancetype)copyWithZone:(NSZone *)zone {
  8530. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8531. }
  8532. - (BOOL)isEqual:(id)other {
  8533. if (self == other) {
  8534. return YES;
  8535. }
  8536. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8537. return NO;
  8538. }
  8539. GPBStringInt32Dictionary *otherDictionary = other;
  8540. return [_dictionary isEqual:otherDictionary->_dictionary];
  8541. }
  8542. - (NSUInteger)hash {
  8543. return _dictionary.count;
  8544. }
  8545. - (NSString *)description {
  8546. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8547. }
  8548. - (NSUInteger)count {
  8549. return _dictionary.count;
  8550. }
  8551. - (void)enumerateKeysAndInt32sUsingBlock:
  8552. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8553. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8554. NSNumber *aValue,
  8555. BOOL *stop) {
  8556. block(aKey, [aValue intValue], stop);
  8557. }];
  8558. }
  8559. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8560. NSUInteger count = _dictionary.count;
  8561. if (count == 0) {
  8562. return 0;
  8563. }
  8564. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8565. GPBDataType keyDataType = field.mapKeyDataType;
  8566. __block size_t result = 0;
  8567. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8568. NSNumber *aValue,
  8569. BOOL *stop) {
  8570. #pragma unused(stop)
  8571. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8572. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8573. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8574. }];
  8575. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8576. result += tagSize * count;
  8577. return result;
  8578. }
  8579. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8580. asField:(GPBFieldDescriptor *)field {
  8581. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8582. GPBDataType keyDataType = field.mapKeyDataType;
  8583. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8584. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8585. NSNumber *aValue,
  8586. BOOL *stop) {
  8587. #pragma unused(stop)
  8588. // Write the tag.
  8589. [outputStream writeInt32NoTag:tag];
  8590. // Write the size of the message.
  8591. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8592. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8593. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8594. // Write the fields.
  8595. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8596. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8597. }];
  8598. }
  8599. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8600. forGPBGenericValueKey:(GPBGenericValue *)key {
  8601. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8602. }
  8603. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8604. [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8605. #pragma unused(stop)
  8606. block(key, [NSString stringWithFormat:@"%d", value]);
  8607. }];
  8608. }
  8609. - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
  8610. NSNumber *wrapped = [_dictionary objectForKey:key];
  8611. if (wrapped && value) {
  8612. *value = [wrapped intValue];
  8613. }
  8614. return (wrapped != NULL);
  8615. }
  8616. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8617. if (otherDictionary) {
  8618. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8619. if (_autocreator) {
  8620. GPBAutocreatedDictionaryModified(_autocreator, self);
  8621. }
  8622. }
  8623. }
  8624. - (void)setInt32:(int32_t)value forKey:(NSString *)key {
  8625. if (!key) {
  8626. [NSException raise:NSInvalidArgumentException
  8627. format:@"Attempting to add nil key to a Dictionary"];
  8628. }
  8629. [_dictionary setObject:@(value) forKey:key];
  8630. if (_autocreator) {
  8631. GPBAutocreatedDictionaryModified(_autocreator, self);
  8632. }
  8633. }
  8634. - (void)removeInt32ForKey:(NSString *)aKey {
  8635. [_dictionary removeObjectForKey:aKey];
  8636. }
  8637. - (void)removeAll {
  8638. [_dictionary removeAllObjects];
  8639. }
  8640. @end
  8641. #pragma mark - String -> UInt64
  8642. @implementation GPBStringUInt64Dictionary {
  8643. @package
  8644. NSMutableDictionary *_dictionary;
  8645. }
  8646. + (instancetype)dictionary {
  8647. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  8648. }
  8649. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  8650. forKey:(NSString *)key {
  8651. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  8652. // on to get the type correct.
  8653. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:&value
  8654. forKeys:&key
  8655. count:1] autorelease];
  8656. }
  8657. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  8658. forKeys:(const NSString * [])keys
  8659. count:(NSUInteger)count {
  8660. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  8661. // on to get the type correct.
  8662. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:values
  8663. forKeys:keys
  8664. count:count] autorelease];
  8665. }
  8666. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8667. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8668. // on to get the type correct.
  8669. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8670. }
  8671. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8672. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8673. }
  8674. - (instancetype)init {
  8675. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  8676. }
  8677. - (instancetype)initWithUInt64s:(const uint64_t [])values
  8678. forKeys:(const NSString * [])keys
  8679. count:(NSUInteger)count {
  8680. self = [super init];
  8681. if (self) {
  8682. _dictionary = [[NSMutableDictionary alloc] init];
  8683. if (count && values && keys) {
  8684. for (NSUInteger i = 0; i < count; ++i) {
  8685. if (!keys[i]) {
  8686. [NSException raise:NSInvalidArgumentException
  8687. format:@"Attempting to add nil key to a Dictionary"];
  8688. }
  8689. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8690. }
  8691. }
  8692. }
  8693. return self;
  8694. }
  8695. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8696. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  8697. if (self) {
  8698. if (dictionary) {
  8699. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8700. }
  8701. }
  8702. return self;
  8703. }
  8704. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8705. #pragma unused(numItems)
  8706. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  8707. }
  8708. - (void)dealloc {
  8709. NSAssert(!_autocreator,
  8710. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8711. [self class], _autocreator);
  8712. [_dictionary release];
  8713. [super dealloc];
  8714. }
  8715. - (instancetype)copyWithZone:(NSZone *)zone {
  8716. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8717. }
  8718. - (BOOL)isEqual:(id)other {
  8719. if (self == other) {
  8720. return YES;
  8721. }
  8722. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8723. return NO;
  8724. }
  8725. GPBStringUInt64Dictionary *otherDictionary = other;
  8726. return [_dictionary isEqual:otherDictionary->_dictionary];
  8727. }
  8728. - (NSUInteger)hash {
  8729. return _dictionary.count;
  8730. }
  8731. - (NSString *)description {
  8732. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8733. }
  8734. - (NSUInteger)count {
  8735. return _dictionary.count;
  8736. }
  8737. - (void)enumerateKeysAndUInt64sUsingBlock:
  8738. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8739. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8740. NSNumber *aValue,
  8741. BOOL *stop) {
  8742. block(aKey, [aValue unsignedLongLongValue], stop);
  8743. }];
  8744. }
  8745. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8746. NSUInteger count = _dictionary.count;
  8747. if (count == 0) {
  8748. return 0;
  8749. }
  8750. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8751. GPBDataType keyDataType = field.mapKeyDataType;
  8752. __block size_t result = 0;
  8753. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8754. NSNumber *aValue,
  8755. BOOL *stop) {
  8756. #pragma unused(stop)
  8757. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8758. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8759. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8760. }];
  8761. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8762. result += tagSize * count;
  8763. return result;
  8764. }
  8765. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8766. asField:(GPBFieldDescriptor *)field {
  8767. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8768. GPBDataType keyDataType = field.mapKeyDataType;
  8769. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8770. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8771. NSNumber *aValue,
  8772. BOOL *stop) {
  8773. #pragma unused(stop)
  8774. // Write the tag.
  8775. [outputStream writeInt32NoTag:tag];
  8776. // Write the size of the message.
  8777. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8778. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8779. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8780. // Write the fields.
  8781. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8782. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8783. }];
  8784. }
  8785. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8786. forGPBGenericValueKey:(GPBGenericValue *)key {
  8787. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8788. }
  8789. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8790. [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8791. #pragma unused(stop)
  8792. block(key, [NSString stringWithFormat:@"%llu", value]);
  8793. }];
  8794. }
  8795. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
  8796. NSNumber *wrapped = [_dictionary objectForKey:key];
  8797. if (wrapped && value) {
  8798. *value = [wrapped unsignedLongLongValue];
  8799. }
  8800. return (wrapped != NULL);
  8801. }
  8802. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8803. if (otherDictionary) {
  8804. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8805. if (_autocreator) {
  8806. GPBAutocreatedDictionaryModified(_autocreator, self);
  8807. }
  8808. }
  8809. }
  8810. - (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
  8811. if (!key) {
  8812. [NSException raise:NSInvalidArgumentException
  8813. format:@"Attempting to add nil key to a Dictionary"];
  8814. }
  8815. [_dictionary setObject:@(value) forKey:key];
  8816. if (_autocreator) {
  8817. GPBAutocreatedDictionaryModified(_autocreator, self);
  8818. }
  8819. }
  8820. - (void)removeUInt64ForKey:(NSString *)aKey {
  8821. [_dictionary removeObjectForKey:aKey];
  8822. }
  8823. - (void)removeAll {
  8824. [_dictionary removeAllObjects];
  8825. }
  8826. @end
  8827. #pragma mark - String -> Int64
  8828. @implementation GPBStringInt64Dictionary {
  8829. @package
  8830. NSMutableDictionary *_dictionary;
  8831. }
  8832. + (instancetype)dictionary {
  8833. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  8834. }
  8835. + (instancetype)dictionaryWithInt64:(int64_t)value
  8836. forKey:(NSString *)key {
  8837. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  8838. // on to get the type correct.
  8839. return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:&value
  8840. forKeys:&key
  8841. count:1] autorelease];
  8842. }
  8843. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  8844. forKeys:(const NSString * [])keys
  8845. count:(NSUInteger)count {
  8846. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  8847. // on to get the type correct.
  8848. return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:values
  8849. forKeys:keys
  8850. count:count] autorelease];
  8851. }
  8852. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8853. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8854. // on to get the type correct.
  8855. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8856. }
  8857. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8858. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8859. }
  8860. - (instancetype)init {
  8861. return [self initWithInt64s:NULL forKeys:NULL count:0];
  8862. }
  8863. - (instancetype)initWithInt64s:(const int64_t [])values
  8864. forKeys:(const NSString * [])keys
  8865. count:(NSUInteger)count {
  8866. self = [super init];
  8867. if (self) {
  8868. _dictionary = [[NSMutableDictionary alloc] init];
  8869. if (count && values && keys) {
  8870. for (NSUInteger i = 0; i < count; ++i) {
  8871. if (!keys[i]) {
  8872. [NSException raise:NSInvalidArgumentException
  8873. format:@"Attempting to add nil key to a Dictionary"];
  8874. }
  8875. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8876. }
  8877. }
  8878. }
  8879. return self;
  8880. }
  8881. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8882. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  8883. if (self) {
  8884. if (dictionary) {
  8885. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8886. }
  8887. }
  8888. return self;
  8889. }
  8890. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8891. #pragma unused(numItems)
  8892. return [self initWithInt64s:NULL forKeys:NULL count:0];
  8893. }
  8894. - (void)dealloc {
  8895. NSAssert(!_autocreator,
  8896. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8897. [self class], _autocreator);
  8898. [_dictionary release];
  8899. [super dealloc];
  8900. }
  8901. - (instancetype)copyWithZone:(NSZone *)zone {
  8902. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8903. }
  8904. - (BOOL)isEqual:(id)other {
  8905. if (self == other) {
  8906. return YES;
  8907. }
  8908. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8909. return NO;
  8910. }
  8911. GPBStringInt64Dictionary *otherDictionary = other;
  8912. return [_dictionary isEqual:otherDictionary->_dictionary];
  8913. }
  8914. - (NSUInteger)hash {
  8915. return _dictionary.count;
  8916. }
  8917. - (NSString *)description {
  8918. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8919. }
  8920. - (NSUInteger)count {
  8921. return _dictionary.count;
  8922. }
  8923. - (void)enumerateKeysAndInt64sUsingBlock:
  8924. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8925. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8926. NSNumber *aValue,
  8927. BOOL *stop) {
  8928. block(aKey, [aValue longLongValue], stop);
  8929. }];
  8930. }
  8931. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8932. NSUInteger count = _dictionary.count;
  8933. if (count == 0) {
  8934. return 0;
  8935. }
  8936. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8937. GPBDataType keyDataType = field.mapKeyDataType;
  8938. __block size_t result = 0;
  8939. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8940. NSNumber *aValue,
  8941. BOOL *stop) {
  8942. #pragma unused(stop)
  8943. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8944. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8945. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8946. }];
  8947. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8948. result += tagSize * count;
  8949. return result;
  8950. }
  8951. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8952. asField:(GPBFieldDescriptor *)field {
  8953. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8954. GPBDataType keyDataType = field.mapKeyDataType;
  8955. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8956. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8957. NSNumber *aValue,
  8958. BOOL *stop) {
  8959. #pragma unused(stop)
  8960. // Write the tag.
  8961. [outputStream writeInt32NoTag:tag];
  8962. // Write the size of the message.
  8963. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8964. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8965. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8966. // Write the fields.
  8967. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8968. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8969. }];
  8970. }
  8971. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8972. forGPBGenericValueKey:(GPBGenericValue *)key {
  8973. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8974. }
  8975. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8976. [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8977. #pragma unused(stop)
  8978. block(key, [NSString stringWithFormat:@"%lld", value]);
  8979. }];
  8980. }
  8981. - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
  8982. NSNumber *wrapped = [_dictionary objectForKey:key];
  8983. if (wrapped && value) {
  8984. *value = [wrapped longLongValue];
  8985. }
  8986. return (wrapped != NULL);
  8987. }
  8988. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8989. if (otherDictionary) {
  8990. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8991. if (_autocreator) {
  8992. GPBAutocreatedDictionaryModified(_autocreator, self);
  8993. }
  8994. }
  8995. }
  8996. - (void)setInt64:(int64_t)value forKey:(NSString *)key {
  8997. if (!key) {
  8998. [NSException raise:NSInvalidArgumentException
  8999. format:@"Attempting to add nil key to a Dictionary"];
  9000. }
  9001. [_dictionary setObject:@(value) forKey:key];
  9002. if (_autocreator) {
  9003. GPBAutocreatedDictionaryModified(_autocreator, self);
  9004. }
  9005. }
  9006. - (void)removeInt64ForKey:(NSString *)aKey {
  9007. [_dictionary removeObjectForKey:aKey];
  9008. }
  9009. - (void)removeAll {
  9010. [_dictionary removeAllObjects];
  9011. }
  9012. @end
  9013. #pragma mark - String -> Bool
  9014. @implementation GPBStringBoolDictionary {
  9015. @package
  9016. NSMutableDictionary *_dictionary;
  9017. }
  9018. + (instancetype)dictionary {
  9019. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  9020. }
  9021. + (instancetype)dictionaryWithBool:(BOOL)value
  9022. forKey:(NSString *)key {
  9023. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  9024. // on to get the type correct.
  9025. return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:&value
  9026. forKeys:&key
  9027. count:1] autorelease];
  9028. }
  9029. + (instancetype)dictionaryWithBools:(const BOOL [])values
  9030. forKeys:(const NSString * [])keys
  9031. count:(NSUInteger)count {
  9032. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  9033. // on to get the type correct.
  9034. return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:values
  9035. forKeys:keys
  9036. count:count] autorelease];
  9037. }
  9038. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9039. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9040. // on to get the type correct.
  9041. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9042. }
  9043. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9044. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9045. }
  9046. - (instancetype)init {
  9047. return [self initWithBools:NULL forKeys:NULL count:0];
  9048. }
  9049. - (instancetype)initWithBools:(const BOOL [])values
  9050. forKeys:(const NSString * [])keys
  9051. count:(NSUInteger)count {
  9052. self = [super init];
  9053. if (self) {
  9054. _dictionary = [[NSMutableDictionary alloc] init];
  9055. if (count && values && keys) {
  9056. for (NSUInteger i = 0; i < count; ++i) {
  9057. if (!keys[i]) {
  9058. [NSException raise:NSInvalidArgumentException
  9059. format:@"Attempting to add nil key to a Dictionary"];
  9060. }
  9061. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9062. }
  9063. }
  9064. }
  9065. return self;
  9066. }
  9067. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9068. self = [self initWithBools:NULL forKeys:NULL count:0];
  9069. if (self) {
  9070. if (dictionary) {
  9071. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9072. }
  9073. }
  9074. return self;
  9075. }
  9076. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9077. #pragma unused(numItems)
  9078. return [self initWithBools:NULL forKeys:NULL count:0];
  9079. }
  9080. - (void)dealloc {
  9081. NSAssert(!_autocreator,
  9082. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9083. [self class], _autocreator);
  9084. [_dictionary release];
  9085. [super dealloc];
  9086. }
  9087. - (instancetype)copyWithZone:(NSZone *)zone {
  9088. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9089. }
  9090. - (BOOL)isEqual:(id)other {
  9091. if (self == other) {
  9092. return YES;
  9093. }
  9094. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  9095. return NO;
  9096. }
  9097. GPBStringBoolDictionary *otherDictionary = other;
  9098. return [_dictionary isEqual:otherDictionary->_dictionary];
  9099. }
  9100. - (NSUInteger)hash {
  9101. return _dictionary.count;
  9102. }
  9103. - (NSString *)description {
  9104. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9105. }
  9106. - (NSUInteger)count {
  9107. return _dictionary.count;
  9108. }
  9109. - (void)enumerateKeysAndBoolsUsingBlock:
  9110. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  9111. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9112. NSNumber *aValue,
  9113. BOOL *stop) {
  9114. block(aKey, [aValue boolValue], stop);
  9115. }];
  9116. }
  9117. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9118. NSUInteger count = _dictionary.count;
  9119. if (count == 0) {
  9120. return 0;
  9121. }
  9122. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9123. GPBDataType keyDataType = field.mapKeyDataType;
  9124. __block size_t result = 0;
  9125. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9126. NSNumber *aValue,
  9127. BOOL *stop) {
  9128. #pragma unused(stop)
  9129. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9130. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9131. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9132. }];
  9133. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9134. result += tagSize * count;
  9135. return result;
  9136. }
  9137. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9138. asField:(GPBFieldDescriptor *)field {
  9139. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9140. GPBDataType keyDataType = field.mapKeyDataType;
  9141. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9142. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9143. NSNumber *aValue,
  9144. BOOL *stop) {
  9145. #pragma unused(stop)
  9146. // Write the tag.
  9147. [outputStream writeInt32NoTag:tag];
  9148. // Write the size of the message.
  9149. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9150. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9151. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9152. // Write the fields.
  9153. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9154. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  9155. }];
  9156. }
  9157. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9158. forGPBGenericValueKey:(GPBGenericValue *)key {
  9159. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  9160. }
  9161. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9162. [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  9163. #pragma unused(stop)
  9164. block(key, (value ? @"true" : @"false"));
  9165. }];
  9166. }
  9167. - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
  9168. NSNumber *wrapped = [_dictionary objectForKey:key];
  9169. if (wrapped && value) {
  9170. *value = [wrapped boolValue];
  9171. }
  9172. return (wrapped != NULL);
  9173. }
  9174. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9175. if (otherDictionary) {
  9176. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9177. if (_autocreator) {
  9178. GPBAutocreatedDictionaryModified(_autocreator, self);
  9179. }
  9180. }
  9181. }
  9182. - (void)setBool:(BOOL)value forKey:(NSString *)key {
  9183. if (!key) {
  9184. [NSException raise:NSInvalidArgumentException
  9185. format:@"Attempting to add nil key to a Dictionary"];
  9186. }
  9187. [_dictionary setObject:@(value) forKey:key];
  9188. if (_autocreator) {
  9189. GPBAutocreatedDictionaryModified(_autocreator, self);
  9190. }
  9191. }
  9192. - (void)removeBoolForKey:(NSString *)aKey {
  9193. [_dictionary removeObjectForKey:aKey];
  9194. }
  9195. - (void)removeAll {
  9196. [_dictionary removeAllObjects];
  9197. }
  9198. @end
  9199. #pragma mark - String -> Float
  9200. @implementation GPBStringFloatDictionary {
  9201. @package
  9202. NSMutableDictionary *_dictionary;
  9203. }
  9204. + (instancetype)dictionary {
  9205. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  9206. }
  9207. + (instancetype)dictionaryWithFloat:(float)value
  9208. forKey:(NSString *)key {
  9209. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  9210. // on to get the type correct.
  9211. return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:&value
  9212. forKeys:&key
  9213. count:1] autorelease];
  9214. }
  9215. + (instancetype)dictionaryWithFloats:(const float [])values
  9216. forKeys:(const NSString * [])keys
  9217. count:(NSUInteger)count {
  9218. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  9219. // on to get the type correct.
  9220. return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:values
  9221. forKeys:keys
  9222. count:count] autorelease];
  9223. }
  9224. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9225. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9226. // on to get the type correct.
  9227. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9228. }
  9229. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9230. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9231. }
  9232. - (instancetype)init {
  9233. return [self initWithFloats:NULL forKeys:NULL count:0];
  9234. }
  9235. - (instancetype)initWithFloats:(const float [])values
  9236. forKeys:(const NSString * [])keys
  9237. count:(NSUInteger)count {
  9238. self = [super init];
  9239. if (self) {
  9240. _dictionary = [[NSMutableDictionary alloc] init];
  9241. if (count && values && keys) {
  9242. for (NSUInteger i = 0; i < count; ++i) {
  9243. if (!keys[i]) {
  9244. [NSException raise:NSInvalidArgumentException
  9245. format:@"Attempting to add nil key to a Dictionary"];
  9246. }
  9247. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9248. }
  9249. }
  9250. }
  9251. return self;
  9252. }
  9253. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9254. self = [self initWithFloats:NULL forKeys:NULL count:0];
  9255. if (self) {
  9256. if (dictionary) {
  9257. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9258. }
  9259. }
  9260. return self;
  9261. }
  9262. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9263. #pragma unused(numItems)
  9264. return [self initWithFloats:NULL forKeys:NULL count:0];
  9265. }
  9266. - (void)dealloc {
  9267. NSAssert(!_autocreator,
  9268. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9269. [self class], _autocreator);
  9270. [_dictionary release];
  9271. [super dealloc];
  9272. }
  9273. - (instancetype)copyWithZone:(NSZone *)zone {
  9274. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9275. }
  9276. - (BOOL)isEqual:(id)other {
  9277. if (self == other) {
  9278. return YES;
  9279. }
  9280. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9281. return NO;
  9282. }
  9283. GPBStringFloatDictionary *otherDictionary = other;
  9284. return [_dictionary isEqual:otherDictionary->_dictionary];
  9285. }
  9286. - (NSUInteger)hash {
  9287. return _dictionary.count;
  9288. }
  9289. - (NSString *)description {
  9290. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9291. }
  9292. - (NSUInteger)count {
  9293. return _dictionary.count;
  9294. }
  9295. - (void)enumerateKeysAndFloatsUsingBlock:
  9296. (void (^)(NSString *key, float value, BOOL *stop))block {
  9297. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9298. NSNumber *aValue,
  9299. BOOL *stop) {
  9300. block(aKey, [aValue floatValue], stop);
  9301. }];
  9302. }
  9303. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9304. NSUInteger count = _dictionary.count;
  9305. if (count == 0) {
  9306. return 0;
  9307. }
  9308. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9309. GPBDataType keyDataType = field.mapKeyDataType;
  9310. __block size_t result = 0;
  9311. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9312. NSNumber *aValue,
  9313. BOOL *stop) {
  9314. #pragma unused(stop)
  9315. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9316. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9317. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9318. }];
  9319. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9320. result += tagSize * count;
  9321. return result;
  9322. }
  9323. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9324. asField:(GPBFieldDescriptor *)field {
  9325. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9326. GPBDataType keyDataType = field.mapKeyDataType;
  9327. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9328. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9329. NSNumber *aValue,
  9330. BOOL *stop) {
  9331. #pragma unused(stop)
  9332. // Write the tag.
  9333. [outputStream writeInt32NoTag:tag];
  9334. // Write the size of the message.
  9335. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9336. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9337. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9338. // Write the fields.
  9339. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9340. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9341. }];
  9342. }
  9343. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9344. forGPBGenericValueKey:(GPBGenericValue *)key {
  9345. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9346. }
  9347. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9348. [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9349. #pragma unused(stop)
  9350. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9351. }];
  9352. }
  9353. - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
  9354. NSNumber *wrapped = [_dictionary objectForKey:key];
  9355. if (wrapped && value) {
  9356. *value = [wrapped floatValue];
  9357. }
  9358. return (wrapped != NULL);
  9359. }
  9360. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9361. if (otherDictionary) {
  9362. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9363. if (_autocreator) {
  9364. GPBAutocreatedDictionaryModified(_autocreator, self);
  9365. }
  9366. }
  9367. }
  9368. - (void)setFloat:(float)value forKey:(NSString *)key {
  9369. if (!key) {
  9370. [NSException raise:NSInvalidArgumentException
  9371. format:@"Attempting to add nil key to a Dictionary"];
  9372. }
  9373. [_dictionary setObject:@(value) forKey:key];
  9374. if (_autocreator) {
  9375. GPBAutocreatedDictionaryModified(_autocreator, self);
  9376. }
  9377. }
  9378. - (void)removeFloatForKey:(NSString *)aKey {
  9379. [_dictionary removeObjectForKey:aKey];
  9380. }
  9381. - (void)removeAll {
  9382. [_dictionary removeAllObjects];
  9383. }
  9384. @end
  9385. #pragma mark - String -> Double
  9386. @implementation GPBStringDoubleDictionary {
  9387. @package
  9388. NSMutableDictionary *_dictionary;
  9389. }
  9390. + (instancetype)dictionary {
  9391. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  9392. }
  9393. + (instancetype)dictionaryWithDouble:(double)value
  9394. forKey:(NSString *)key {
  9395. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  9396. // on to get the type correct.
  9397. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:&value
  9398. forKeys:&key
  9399. count:1] autorelease];
  9400. }
  9401. + (instancetype)dictionaryWithDoubles:(const double [])values
  9402. forKeys:(const NSString * [])keys
  9403. count:(NSUInteger)count {
  9404. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  9405. // on to get the type correct.
  9406. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:values
  9407. forKeys:keys
  9408. count:count] autorelease];
  9409. }
  9410. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9411. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9412. // on to get the type correct.
  9413. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9414. }
  9415. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9416. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9417. }
  9418. - (instancetype)init {
  9419. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9420. }
  9421. - (instancetype)initWithDoubles:(const double [])values
  9422. forKeys:(const NSString * [])keys
  9423. count:(NSUInteger)count {
  9424. self = [super init];
  9425. if (self) {
  9426. _dictionary = [[NSMutableDictionary alloc] init];
  9427. if (count && values && keys) {
  9428. for (NSUInteger i = 0; i < count; ++i) {
  9429. if (!keys[i]) {
  9430. [NSException raise:NSInvalidArgumentException
  9431. format:@"Attempting to add nil key to a Dictionary"];
  9432. }
  9433. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9434. }
  9435. }
  9436. }
  9437. return self;
  9438. }
  9439. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9440. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  9441. if (self) {
  9442. if (dictionary) {
  9443. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9444. }
  9445. }
  9446. return self;
  9447. }
  9448. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9449. #pragma unused(numItems)
  9450. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9451. }
  9452. - (void)dealloc {
  9453. NSAssert(!_autocreator,
  9454. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9455. [self class], _autocreator);
  9456. [_dictionary release];
  9457. [super dealloc];
  9458. }
  9459. - (instancetype)copyWithZone:(NSZone *)zone {
  9460. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9461. }
  9462. - (BOOL)isEqual:(id)other {
  9463. if (self == other) {
  9464. return YES;
  9465. }
  9466. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9467. return NO;
  9468. }
  9469. GPBStringDoubleDictionary *otherDictionary = other;
  9470. return [_dictionary isEqual:otherDictionary->_dictionary];
  9471. }
  9472. - (NSUInteger)hash {
  9473. return _dictionary.count;
  9474. }
  9475. - (NSString *)description {
  9476. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9477. }
  9478. - (NSUInteger)count {
  9479. return _dictionary.count;
  9480. }
  9481. - (void)enumerateKeysAndDoublesUsingBlock:
  9482. (void (^)(NSString *key, double value, BOOL *stop))block {
  9483. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9484. NSNumber *aValue,
  9485. BOOL *stop) {
  9486. block(aKey, [aValue doubleValue], stop);
  9487. }];
  9488. }
  9489. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9490. NSUInteger count = _dictionary.count;
  9491. if (count == 0) {
  9492. return 0;
  9493. }
  9494. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9495. GPBDataType keyDataType = field.mapKeyDataType;
  9496. __block size_t result = 0;
  9497. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9498. NSNumber *aValue,
  9499. BOOL *stop) {
  9500. #pragma unused(stop)
  9501. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9502. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9503. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9504. }];
  9505. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9506. result += tagSize * count;
  9507. return result;
  9508. }
  9509. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9510. asField:(GPBFieldDescriptor *)field {
  9511. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9512. GPBDataType keyDataType = field.mapKeyDataType;
  9513. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9514. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9515. NSNumber *aValue,
  9516. BOOL *stop) {
  9517. #pragma unused(stop)
  9518. // Write the tag.
  9519. [outputStream writeInt32NoTag:tag];
  9520. // Write the size of the message.
  9521. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9522. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9523. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9524. // Write the fields.
  9525. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9526. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9527. }];
  9528. }
  9529. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9530. forGPBGenericValueKey:(GPBGenericValue *)key {
  9531. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9532. }
  9533. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9534. [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9535. #pragma unused(stop)
  9536. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9537. }];
  9538. }
  9539. - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
  9540. NSNumber *wrapped = [_dictionary objectForKey:key];
  9541. if (wrapped && value) {
  9542. *value = [wrapped doubleValue];
  9543. }
  9544. return (wrapped != NULL);
  9545. }
  9546. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9547. if (otherDictionary) {
  9548. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9549. if (_autocreator) {
  9550. GPBAutocreatedDictionaryModified(_autocreator, self);
  9551. }
  9552. }
  9553. }
  9554. - (void)setDouble:(double)value forKey:(NSString *)key {
  9555. if (!key) {
  9556. [NSException raise:NSInvalidArgumentException
  9557. format:@"Attempting to add nil key to a Dictionary"];
  9558. }
  9559. [_dictionary setObject:@(value) forKey:key];
  9560. if (_autocreator) {
  9561. GPBAutocreatedDictionaryModified(_autocreator, self);
  9562. }
  9563. }
  9564. - (void)removeDoubleForKey:(NSString *)aKey {
  9565. [_dictionary removeObjectForKey:aKey];
  9566. }
  9567. - (void)removeAll {
  9568. [_dictionary removeAllObjects];
  9569. }
  9570. @end
  9571. #pragma mark - String -> Enum
  9572. @implementation GPBStringEnumDictionary {
  9573. @package
  9574. NSMutableDictionary *_dictionary;
  9575. GPBEnumValidationFunc _validationFunc;
  9576. }
  9577. @synthesize validationFunc = _validationFunc;
  9578. + (instancetype)dictionary {
  9579. return [[[self alloc] initWithValidationFunction:NULL
  9580. rawValues:NULL
  9581. forKeys:NULL
  9582. count:0] autorelease];
  9583. }
  9584. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9585. return [[[self alloc] initWithValidationFunction:func
  9586. rawValues:NULL
  9587. forKeys:NULL
  9588. count:0] autorelease];
  9589. }
  9590. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9591. rawValue:(int32_t)rawValue
  9592. forKey:(NSString *)key {
  9593. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9594. // on to get the type correct.
  9595. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9596. rawValues:&rawValue
  9597. forKeys:&key
  9598. count:1] autorelease];
  9599. }
  9600. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9601. rawValues:(const int32_t [])rawValues
  9602. forKeys:(const NSString * [])keys
  9603. count:(NSUInteger)count {
  9604. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9605. // on to get the type correct.
  9606. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9607. rawValues:rawValues
  9608. forKeys:keys
  9609. count:count] autorelease];
  9610. }
  9611. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9612. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9613. // on to get the type correct.
  9614. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9615. }
  9616. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9617. capacity:(NSUInteger)numItems {
  9618. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9619. }
  9620. - (instancetype)init {
  9621. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9622. }
  9623. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9624. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9625. }
  9626. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9627. rawValues:(const int32_t [])rawValues
  9628. forKeys:(const NSString * [])keys
  9629. count:(NSUInteger)count {
  9630. self = [super init];
  9631. if (self) {
  9632. _dictionary = [[NSMutableDictionary alloc] init];
  9633. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9634. if (count && rawValues && keys) {
  9635. for (NSUInteger i = 0; i < count; ++i) {
  9636. if (!keys[i]) {
  9637. [NSException raise:NSInvalidArgumentException
  9638. format:@"Attempting to add nil key to a Dictionary"];
  9639. }
  9640. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9641. }
  9642. }
  9643. }
  9644. return self;
  9645. }
  9646. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9647. self = [self initWithValidationFunction:dictionary.validationFunc
  9648. rawValues:NULL
  9649. forKeys:NULL
  9650. count:0];
  9651. if (self) {
  9652. if (dictionary) {
  9653. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9654. }
  9655. }
  9656. return self;
  9657. }
  9658. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9659. capacity:(NSUInteger)numItems {
  9660. #pragma unused(numItems)
  9661. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9662. }
  9663. - (void)dealloc {
  9664. NSAssert(!_autocreator,
  9665. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9666. [self class], _autocreator);
  9667. [_dictionary release];
  9668. [super dealloc];
  9669. }
  9670. - (instancetype)copyWithZone:(NSZone *)zone {
  9671. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9672. }
  9673. - (BOOL)isEqual:(id)other {
  9674. if (self == other) {
  9675. return YES;
  9676. }
  9677. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9678. return NO;
  9679. }
  9680. GPBStringEnumDictionary *otherDictionary = other;
  9681. return [_dictionary isEqual:otherDictionary->_dictionary];
  9682. }
  9683. - (NSUInteger)hash {
  9684. return _dictionary.count;
  9685. }
  9686. - (NSString *)description {
  9687. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9688. }
  9689. - (NSUInteger)count {
  9690. return _dictionary.count;
  9691. }
  9692. - (void)enumerateKeysAndRawValuesUsingBlock:
  9693. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9694. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9695. NSNumber *aValue,
  9696. BOOL *stop) {
  9697. block(aKey, [aValue intValue], stop);
  9698. }];
  9699. }
  9700. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9701. NSUInteger count = _dictionary.count;
  9702. if (count == 0) {
  9703. return 0;
  9704. }
  9705. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9706. GPBDataType keyDataType = field.mapKeyDataType;
  9707. __block size_t result = 0;
  9708. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9709. NSNumber *aValue,
  9710. BOOL *stop) {
  9711. #pragma unused(stop)
  9712. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9713. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9714. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9715. }];
  9716. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9717. result += tagSize * count;
  9718. return result;
  9719. }
  9720. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9721. asField:(GPBFieldDescriptor *)field {
  9722. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9723. GPBDataType keyDataType = field.mapKeyDataType;
  9724. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9725. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9726. NSNumber *aValue,
  9727. BOOL *stop) {
  9728. #pragma unused(stop)
  9729. // Write the tag.
  9730. [outputStream writeInt32NoTag:tag];
  9731. // Write the size of the message.
  9732. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9733. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9734. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9735. // Write the fields.
  9736. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9737. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9738. }];
  9739. }
  9740. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9741. forKey:(GPBGenericValue *)key
  9742. keyDataType:(GPBDataType)keyDataType {
  9743. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9744. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9745. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9746. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9747. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9748. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9749. [outputStream release];
  9750. return data;
  9751. }
  9752. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9753. forGPBGenericValueKey:(GPBGenericValue *)key {
  9754. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9755. }
  9756. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9757. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9758. #pragma unused(stop)
  9759. block(key, @(value));
  9760. }];
  9761. }
  9762. - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
  9763. NSNumber *wrapped = [_dictionary objectForKey:key];
  9764. if (wrapped && value) {
  9765. int32_t result = [wrapped intValue];
  9766. if (!_validationFunc(result)) {
  9767. result = kGPBUnrecognizedEnumeratorValue;
  9768. }
  9769. *value = result;
  9770. }
  9771. return (wrapped != NULL);
  9772. }
  9773. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
  9774. NSNumber *wrapped = [_dictionary objectForKey:key];
  9775. if (wrapped && rawValue) {
  9776. *rawValue = [wrapped intValue];
  9777. }
  9778. return (wrapped != NULL);
  9779. }
  9780. - (void)enumerateKeysAndEnumsUsingBlock:
  9781. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9782. GPBEnumValidationFunc func = _validationFunc;
  9783. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9784. NSNumber *aValue,
  9785. BOOL *stop) {
  9786. int32_t unwrapped = [aValue intValue];
  9787. if (!func(unwrapped)) {
  9788. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9789. }
  9790. block(aKey, unwrapped, stop);
  9791. }];
  9792. }
  9793. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9794. if (otherDictionary) {
  9795. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9796. if (_autocreator) {
  9797. GPBAutocreatedDictionaryModified(_autocreator, self);
  9798. }
  9799. }
  9800. }
  9801. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9802. if (!key) {
  9803. [NSException raise:NSInvalidArgumentException
  9804. format:@"Attempting to add nil key to a Dictionary"];
  9805. }
  9806. [_dictionary setObject:@(value) forKey:key];
  9807. if (_autocreator) {
  9808. GPBAutocreatedDictionaryModified(_autocreator, self);
  9809. }
  9810. }
  9811. - (void)removeEnumForKey:(NSString *)aKey {
  9812. [_dictionary removeObjectForKey:aKey];
  9813. }
  9814. - (void)removeAll {
  9815. [_dictionary removeAllObjects];
  9816. }
  9817. - (void)setEnum:(int32_t)value forKey:(NSString *)key {
  9818. if (!key) {
  9819. [NSException raise:NSInvalidArgumentException
  9820. format:@"Attempting to add nil key to a Dictionary"];
  9821. }
  9822. if (!_validationFunc(value)) {
  9823. [NSException raise:NSInvalidArgumentException
  9824. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9825. value];
  9826. }
  9827. [_dictionary setObject:@(value) forKey:key];
  9828. if (_autocreator) {
  9829. GPBAutocreatedDictionaryModified(_autocreator, self);
  9830. }
  9831. }
  9832. @end
  9833. //%PDDM-EXPAND-END (5 expansions)
  9834. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9835. // This block of code is generated, do not edit it directly.
  9836. #pragma mark - Bool -> UInt32
  9837. @implementation GPBBoolUInt32Dictionary {
  9838. @package
  9839. uint32_t _values[2];
  9840. BOOL _valueSet[2];
  9841. }
  9842. + (instancetype)dictionary {
  9843. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  9844. }
  9845. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  9846. forKey:(BOOL)key {
  9847. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  9848. // on to get the type correct.
  9849. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:&value
  9850. forKeys:&key
  9851. count:1] autorelease];
  9852. }
  9853. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  9854. forKeys:(const BOOL [])keys
  9855. count:(NSUInteger)count {
  9856. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  9857. // on to get the type correct.
  9858. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:values
  9859. forKeys:keys
  9860. count:count] autorelease];
  9861. }
  9862. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9863. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9864. // on to get the type correct.
  9865. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9866. }
  9867. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9868. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9869. }
  9870. - (instancetype)init {
  9871. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  9872. }
  9873. - (instancetype)initWithUInt32s:(const uint32_t [])values
  9874. forKeys:(const BOOL [])keys
  9875. count:(NSUInteger)count {
  9876. self = [super init];
  9877. if (self) {
  9878. for (NSUInteger i = 0; i < count; ++i) {
  9879. int idx = keys[i] ? 1 : 0;
  9880. _values[idx] = values[i];
  9881. _valueSet[idx] = YES;
  9882. }
  9883. }
  9884. return self;
  9885. }
  9886. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9887. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  9888. if (self) {
  9889. if (dictionary) {
  9890. for (int i = 0; i < 2; ++i) {
  9891. if (dictionary->_valueSet[i]) {
  9892. _values[i] = dictionary->_values[i];
  9893. _valueSet[i] = YES;
  9894. }
  9895. }
  9896. }
  9897. }
  9898. return self;
  9899. }
  9900. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9901. #pragma unused(numItems)
  9902. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  9903. }
  9904. #if !defined(NS_BLOCK_ASSERTIONS)
  9905. - (void)dealloc {
  9906. NSAssert(!_autocreator,
  9907. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9908. [self class], _autocreator);
  9909. [super dealloc];
  9910. }
  9911. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9912. - (instancetype)copyWithZone:(NSZone *)zone {
  9913. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9914. }
  9915. - (BOOL)isEqual:(id)other {
  9916. if (self == other) {
  9917. return YES;
  9918. }
  9919. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9920. return NO;
  9921. }
  9922. GPBBoolUInt32Dictionary *otherDictionary = other;
  9923. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9924. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9925. return NO;
  9926. }
  9927. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9928. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9929. return NO;
  9930. }
  9931. return YES;
  9932. }
  9933. - (NSUInteger)hash {
  9934. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9935. }
  9936. - (NSString *)description {
  9937. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9938. if (_valueSet[0]) {
  9939. [result appendFormat:@"NO: %u", _values[0]];
  9940. }
  9941. if (_valueSet[1]) {
  9942. [result appendFormat:@"YES: %u", _values[1]];
  9943. }
  9944. [result appendString:@" }"];
  9945. return result;
  9946. }
  9947. - (NSUInteger)count {
  9948. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9949. }
  9950. - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
  9951. int idx = (key ? 1 : 0);
  9952. if (_valueSet[idx]) {
  9953. if (value) {
  9954. *value = _values[idx];
  9955. }
  9956. return YES;
  9957. }
  9958. return NO;
  9959. }
  9960. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9961. forGPBGenericValueKey:(GPBGenericValue *)key {
  9962. int idx = (key->valueBool ? 1 : 0);
  9963. _values[idx] = value->valueUInt32;
  9964. _valueSet[idx] = YES;
  9965. }
  9966. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9967. if (_valueSet[0]) {
  9968. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9969. }
  9970. if (_valueSet[1]) {
  9971. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9972. }
  9973. }
  9974. - (void)enumerateKeysAndUInt32sUsingBlock:
  9975. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9976. BOOL stop = NO;
  9977. if (_valueSet[0]) {
  9978. block(NO, _values[0], &stop);
  9979. }
  9980. if (!stop && _valueSet[1]) {
  9981. block(YES, _values[1], &stop);
  9982. }
  9983. }
  9984. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9985. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9986. NSUInteger count = 0;
  9987. size_t result = 0;
  9988. for (int i = 0; i < 2; ++i) {
  9989. if (_valueSet[i]) {
  9990. ++count;
  9991. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9992. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9993. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9994. }
  9995. }
  9996. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9997. result += tagSize * count;
  9998. return result;
  9999. }
  10000. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10001. asField:(GPBFieldDescriptor *)field {
  10002. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10003. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10004. for (int i = 0; i < 2; ++i) {
  10005. if (_valueSet[i]) {
  10006. // Write the tag.
  10007. [outputStream writeInt32NoTag:tag];
  10008. // Write the size of the message.
  10009. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10010. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10011. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10012. // Write the fields.
  10013. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10014. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10015. }
  10016. }
  10017. }
  10018. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  10019. if (otherDictionary) {
  10020. for (int i = 0; i < 2; ++i) {
  10021. if (otherDictionary->_valueSet[i]) {
  10022. _valueSet[i] = YES;
  10023. _values[i] = otherDictionary->_values[i];
  10024. }
  10025. }
  10026. if (_autocreator) {
  10027. GPBAutocreatedDictionaryModified(_autocreator, self);
  10028. }
  10029. }
  10030. }
  10031. - (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
  10032. int idx = (key ? 1 : 0);
  10033. _values[idx] = value;
  10034. _valueSet[idx] = YES;
  10035. if (_autocreator) {
  10036. GPBAutocreatedDictionaryModified(_autocreator, self);
  10037. }
  10038. }
  10039. - (void)removeUInt32ForKey:(BOOL)aKey {
  10040. _valueSet[aKey ? 1 : 0] = NO;
  10041. }
  10042. - (void)removeAll {
  10043. _valueSet[0] = NO;
  10044. _valueSet[1] = NO;
  10045. }
  10046. @end
  10047. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  10048. // This block of code is generated, do not edit it directly.
  10049. #pragma mark - Bool -> Int32
  10050. @implementation GPBBoolInt32Dictionary {
  10051. @package
  10052. int32_t _values[2];
  10053. BOOL _valueSet[2];
  10054. }
  10055. + (instancetype)dictionary {
  10056. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  10057. }
  10058. + (instancetype)dictionaryWithInt32:(int32_t)value
  10059. forKey:(BOOL)key {
  10060. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  10061. // on to get the type correct.
  10062. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:&value
  10063. forKeys:&key
  10064. count:1] autorelease];
  10065. }
  10066. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  10067. forKeys:(const BOOL [])keys
  10068. count:(NSUInteger)count {
  10069. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  10070. // on to get the type correct.
  10071. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:values
  10072. forKeys:keys
  10073. count:count] autorelease];
  10074. }
  10075. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10076. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10077. // on to get the type correct.
  10078. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10079. }
  10080. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10081. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10082. }
  10083. - (instancetype)init {
  10084. return [self initWithInt32s:NULL forKeys:NULL count:0];
  10085. }
  10086. - (instancetype)initWithInt32s:(const int32_t [])values
  10087. forKeys:(const BOOL [])keys
  10088. count:(NSUInteger)count {
  10089. self = [super init];
  10090. if (self) {
  10091. for (NSUInteger i = 0; i < count; ++i) {
  10092. int idx = keys[i] ? 1 : 0;
  10093. _values[idx] = values[i];
  10094. _valueSet[idx] = YES;
  10095. }
  10096. }
  10097. return self;
  10098. }
  10099. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10100. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  10101. if (self) {
  10102. if (dictionary) {
  10103. for (int i = 0; i < 2; ++i) {
  10104. if (dictionary->_valueSet[i]) {
  10105. _values[i] = dictionary->_values[i];
  10106. _valueSet[i] = YES;
  10107. }
  10108. }
  10109. }
  10110. }
  10111. return self;
  10112. }
  10113. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10114. #pragma unused(numItems)
  10115. return [self initWithInt32s:NULL forKeys:NULL count:0];
  10116. }
  10117. #if !defined(NS_BLOCK_ASSERTIONS)
  10118. - (void)dealloc {
  10119. NSAssert(!_autocreator,
  10120. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10121. [self class], _autocreator);
  10122. [super dealloc];
  10123. }
  10124. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10125. - (instancetype)copyWithZone:(NSZone *)zone {
  10126. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10127. }
  10128. - (BOOL)isEqual:(id)other {
  10129. if (self == other) {
  10130. return YES;
  10131. }
  10132. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  10133. return NO;
  10134. }
  10135. GPBBoolInt32Dictionary *otherDictionary = other;
  10136. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10137. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10138. return NO;
  10139. }
  10140. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10141. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10142. return NO;
  10143. }
  10144. return YES;
  10145. }
  10146. - (NSUInteger)hash {
  10147. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10148. }
  10149. - (NSString *)description {
  10150. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10151. if (_valueSet[0]) {
  10152. [result appendFormat:@"NO: %d", _values[0]];
  10153. }
  10154. if (_valueSet[1]) {
  10155. [result appendFormat:@"YES: %d", _values[1]];
  10156. }
  10157. [result appendString:@" }"];
  10158. return result;
  10159. }
  10160. - (NSUInteger)count {
  10161. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10162. }
  10163. - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
  10164. int idx = (key ? 1 : 0);
  10165. if (_valueSet[idx]) {
  10166. if (value) {
  10167. *value = _values[idx];
  10168. }
  10169. return YES;
  10170. }
  10171. return NO;
  10172. }
  10173. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10174. forGPBGenericValueKey:(GPBGenericValue *)key {
  10175. int idx = (key->valueBool ? 1 : 0);
  10176. _values[idx] = value->valueInt32;
  10177. _valueSet[idx] = YES;
  10178. }
  10179. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10180. if (_valueSet[0]) {
  10181. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  10182. }
  10183. if (_valueSet[1]) {
  10184. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  10185. }
  10186. }
  10187. - (void)enumerateKeysAndInt32sUsingBlock:
  10188. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10189. BOOL stop = NO;
  10190. if (_valueSet[0]) {
  10191. block(NO, _values[0], &stop);
  10192. }
  10193. if (!stop && _valueSet[1]) {
  10194. block(YES, _values[1], &stop);
  10195. }
  10196. }
  10197. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10198. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10199. NSUInteger count = 0;
  10200. size_t result = 0;
  10201. for (int i = 0; i < 2; ++i) {
  10202. if (_valueSet[i]) {
  10203. ++count;
  10204. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10205. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10206. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10207. }
  10208. }
  10209. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10210. result += tagSize * count;
  10211. return result;
  10212. }
  10213. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10214. asField:(GPBFieldDescriptor *)field {
  10215. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10216. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10217. for (int i = 0; i < 2; ++i) {
  10218. if (_valueSet[i]) {
  10219. // Write the tag.
  10220. [outputStream writeInt32NoTag:tag];
  10221. // Write the size of the message.
  10222. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10223. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10224. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10225. // Write the fields.
  10226. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10227. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10228. }
  10229. }
  10230. }
  10231. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10232. if (otherDictionary) {
  10233. for (int i = 0; i < 2; ++i) {
  10234. if (otherDictionary->_valueSet[i]) {
  10235. _valueSet[i] = YES;
  10236. _values[i] = otherDictionary->_values[i];
  10237. }
  10238. }
  10239. if (_autocreator) {
  10240. GPBAutocreatedDictionaryModified(_autocreator, self);
  10241. }
  10242. }
  10243. }
  10244. - (void)setInt32:(int32_t)value forKey:(BOOL)key {
  10245. int idx = (key ? 1 : 0);
  10246. _values[idx] = value;
  10247. _valueSet[idx] = YES;
  10248. if (_autocreator) {
  10249. GPBAutocreatedDictionaryModified(_autocreator, self);
  10250. }
  10251. }
  10252. - (void)removeInt32ForKey:(BOOL)aKey {
  10253. _valueSet[aKey ? 1 : 0] = NO;
  10254. }
  10255. - (void)removeAll {
  10256. _valueSet[0] = NO;
  10257. _valueSet[1] = NO;
  10258. }
  10259. @end
  10260. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10261. // This block of code is generated, do not edit it directly.
  10262. #pragma mark - Bool -> UInt64
  10263. @implementation GPBBoolUInt64Dictionary {
  10264. @package
  10265. uint64_t _values[2];
  10266. BOOL _valueSet[2];
  10267. }
  10268. + (instancetype)dictionary {
  10269. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  10270. }
  10271. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  10272. forKey:(BOOL)key {
  10273. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  10274. // on to get the type correct.
  10275. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:&value
  10276. forKeys:&key
  10277. count:1] autorelease];
  10278. }
  10279. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  10280. forKeys:(const BOOL [])keys
  10281. count:(NSUInteger)count {
  10282. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  10283. // on to get the type correct.
  10284. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:values
  10285. forKeys:keys
  10286. count:count] autorelease];
  10287. }
  10288. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10289. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10290. // on to get the type correct.
  10291. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10292. }
  10293. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10294. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10295. }
  10296. - (instancetype)init {
  10297. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  10298. }
  10299. - (instancetype)initWithUInt64s:(const uint64_t [])values
  10300. forKeys:(const BOOL [])keys
  10301. count:(NSUInteger)count {
  10302. self = [super init];
  10303. if (self) {
  10304. for (NSUInteger i = 0; i < count; ++i) {
  10305. int idx = keys[i] ? 1 : 0;
  10306. _values[idx] = values[i];
  10307. _valueSet[idx] = YES;
  10308. }
  10309. }
  10310. return self;
  10311. }
  10312. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10313. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  10314. if (self) {
  10315. if (dictionary) {
  10316. for (int i = 0; i < 2; ++i) {
  10317. if (dictionary->_valueSet[i]) {
  10318. _values[i] = dictionary->_values[i];
  10319. _valueSet[i] = YES;
  10320. }
  10321. }
  10322. }
  10323. }
  10324. return self;
  10325. }
  10326. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10327. #pragma unused(numItems)
  10328. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  10329. }
  10330. #if !defined(NS_BLOCK_ASSERTIONS)
  10331. - (void)dealloc {
  10332. NSAssert(!_autocreator,
  10333. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10334. [self class], _autocreator);
  10335. [super dealloc];
  10336. }
  10337. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10338. - (instancetype)copyWithZone:(NSZone *)zone {
  10339. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10340. }
  10341. - (BOOL)isEqual:(id)other {
  10342. if (self == other) {
  10343. return YES;
  10344. }
  10345. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10346. return NO;
  10347. }
  10348. GPBBoolUInt64Dictionary *otherDictionary = other;
  10349. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10350. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10351. return NO;
  10352. }
  10353. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10354. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10355. return NO;
  10356. }
  10357. return YES;
  10358. }
  10359. - (NSUInteger)hash {
  10360. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10361. }
  10362. - (NSString *)description {
  10363. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10364. if (_valueSet[0]) {
  10365. [result appendFormat:@"NO: %llu", _values[0]];
  10366. }
  10367. if (_valueSet[1]) {
  10368. [result appendFormat:@"YES: %llu", _values[1]];
  10369. }
  10370. [result appendString:@" }"];
  10371. return result;
  10372. }
  10373. - (NSUInteger)count {
  10374. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10375. }
  10376. - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
  10377. int idx = (key ? 1 : 0);
  10378. if (_valueSet[idx]) {
  10379. if (value) {
  10380. *value = _values[idx];
  10381. }
  10382. return YES;
  10383. }
  10384. return NO;
  10385. }
  10386. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10387. forGPBGenericValueKey:(GPBGenericValue *)key {
  10388. int idx = (key->valueBool ? 1 : 0);
  10389. _values[idx] = value->valueUInt64;
  10390. _valueSet[idx] = YES;
  10391. }
  10392. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10393. if (_valueSet[0]) {
  10394. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10395. }
  10396. if (_valueSet[1]) {
  10397. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10398. }
  10399. }
  10400. - (void)enumerateKeysAndUInt64sUsingBlock:
  10401. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10402. BOOL stop = NO;
  10403. if (_valueSet[0]) {
  10404. block(NO, _values[0], &stop);
  10405. }
  10406. if (!stop && _valueSet[1]) {
  10407. block(YES, _values[1], &stop);
  10408. }
  10409. }
  10410. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10411. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10412. NSUInteger count = 0;
  10413. size_t result = 0;
  10414. for (int i = 0; i < 2; ++i) {
  10415. if (_valueSet[i]) {
  10416. ++count;
  10417. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10418. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10419. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10420. }
  10421. }
  10422. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10423. result += tagSize * count;
  10424. return result;
  10425. }
  10426. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10427. asField:(GPBFieldDescriptor *)field {
  10428. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10429. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10430. for (int i = 0; i < 2; ++i) {
  10431. if (_valueSet[i]) {
  10432. // Write the tag.
  10433. [outputStream writeInt32NoTag:tag];
  10434. // Write the size of the message.
  10435. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10436. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10437. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10438. // Write the fields.
  10439. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10440. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10441. }
  10442. }
  10443. }
  10444. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10445. if (otherDictionary) {
  10446. for (int i = 0; i < 2; ++i) {
  10447. if (otherDictionary->_valueSet[i]) {
  10448. _valueSet[i] = YES;
  10449. _values[i] = otherDictionary->_values[i];
  10450. }
  10451. }
  10452. if (_autocreator) {
  10453. GPBAutocreatedDictionaryModified(_autocreator, self);
  10454. }
  10455. }
  10456. }
  10457. - (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
  10458. int idx = (key ? 1 : 0);
  10459. _values[idx] = value;
  10460. _valueSet[idx] = YES;
  10461. if (_autocreator) {
  10462. GPBAutocreatedDictionaryModified(_autocreator, self);
  10463. }
  10464. }
  10465. - (void)removeUInt64ForKey:(BOOL)aKey {
  10466. _valueSet[aKey ? 1 : 0] = NO;
  10467. }
  10468. - (void)removeAll {
  10469. _valueSet[0] = NO;
  10470. _valueSet[1] = NO;
  10471. }
  10472. @end
  10473. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10474. // This block of code is generated, do not edit it directly.
  10475. #pragma mark - Bool -> Int64
  10476. @implementation GPBBoolInt64Dictionary {
  10477. @package
  10478. int64_t _values[2];
  10479. BOOL _valueSet[2];
  10480. }
  10481. + (instancetype)dictionary {
  10482. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  10483. }
  10484. + (instancetype)dictionaryWithInt64:(int64_t)value
  10485. forKey:(BOOL)key {
  10486. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  10487. // on to get the type correct.
  10488. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:&value
  10489. forKeys:&key
  10490. count:1] autorelease];
  10491. }
  10492. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  10493. forKeys:(const BOOL [])keys
  10494. count:(NSUInteger)count {
  10495. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  10496. // on to get the type correct.
  10497. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:values
  10498. forKeys:keys
  10499. count:count] autorelease];
  10500. }
  10501. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10502. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10503. // on to get the type correct.
  10504. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10505. }
  10506. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10507. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10508. }
  10509. - (instancetype)init {
  10510. return [self initWithInt64s:NULL forKeys:NULL count:0];
  10511. }
  10512. - (instancetype)initWithInt64s:(const int64_t [])values
  10513. forKeys:(const BOOL [])keys
  10514. count:(NSUInteger)count {
  10515. self = [super init];
  10516. if (self) {
  10517. for (NSUInteger i = 0; i < count; ++i) {
  10518. int idx = keys[i] ? 1 : 0;
  10519. _values[idx] = values[i];
  10520. _valueSet[idx] = YES;
  10521. }
  10522. }
  10523. return self;
  10524. }
  10525. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10526. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  10527. if (self) {
  10528. if (dictionary) {
  10529. for (int i = 0; i < 2; ++i) {
  10530. if (dictionary->_valueSet[i]) {
  10531. _values[i] = dictionary->_values[i];
  10532. _valueSet[i] = YES;
  10533. }
  10534. }
  10535. }
  10536. }
  10537. return self;
  10538. }
  10539. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10540. #pragma unused(numItems)
  10541. return [self initWithInt64s:NULL forKeys:NULL count:0];
  10542. }
  10543. #if !defined(NS_BLOCK_ASSERTIONS)
  10544. - (void)dealloc {
  10545. NSAssert(!_autocreator,
  10546. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10547. [self class], _autocreator);
  10548. [super dealloc];
  10549. }
  10550. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10551. - (instancetype)copyWithZone:(NSZone *)zone {
  10552. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10553. }
  10554. - (BOOL)isEqual:(id)other {
  10555. if (self == other) {
  10556. return YES;
  10557. }
  10558. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10559. return NO;
  10560. }
  10561. GPBBoolInt64Dictionary *otherDictionary = other;
  10562. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10563. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10564. return NO;
  10565. }
  10566. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10567. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10568. return NO;
  10569. }
  10570. return YES;
  10571. }
  10572. - (NSUInteger)hash {
  10573. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10574. }
  10575. - (NSString *)description {
  10576. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10577. if (_valueSet[0]) {
  10578. [result appendFormat:@"NO: %lld", _values[0]];
  10579. }
  10580. if (_valueSet[1]) {
  10581. [result appendFormat:@"YES: %lld", _values[1]];
  10582. }
  10583. [result appendString:@" }"];
  10584. return result;
  10585. }
  10586. - (NSUInteger)count {
  10587. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10588. }
  10589. - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
  10590. int idx = (key ? 1 : 0);
  10591. if (_valueSet[idx]) {
  10592. if (value) {
  10593. *value = _values[idx];
  10594. }
  10595. return YES;
  10596. }
  10597. return NO;
  10598. }
  10599. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10600. forGPBGenericValueKey:(GPBGenericValue *)key {
  10601. int idx = (key->valueBool ? 1 : 0);
  10602. _values[idx] = value->valueInt64;
  10603. _valueSet[idx] = YES;
  10604. }
  10605. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10606. if (_valueSet[0]) {
  10607. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10608. }
  10609. if (_valueSet[1]) {
  10610. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10611. }
  10612. }
  10613. - (void)enumerateKeysAndInt64sUsingBlock:
  10614. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10615. BOOL stop = NO;
  10616. if (_valueSet[0]) {
  10617. block(NO, _values[0], &stop);
  10618. }
  10619. if (!stop && _valueSet[1]) {
  10620. block(YES, _values[1], &stop);
  10621. }
  10622. }
  10623. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10624. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10625. NSUInteger count = 0;
  10626. size_t result = 0;
  10627. for (int i = 0; i < 2; ++i) {
  10628. if (_valueSet[i]) {
  10629. ++count;
  10630. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10631. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10632. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10633. }
  10634. }
  10635. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10636. result += tagSize * count;
  10637. return result;
  10638. }
  10639. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10640. asField:(GPBFieldDescriptor *)field {
  10641. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10642. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10643. for (int i = 0; i < 2; ++i) {
  10644. if (_valueSet[i]) {
  10645. // Write the tag.
  10646. [outputStream writeInt32NoTag:tag];
  10647. // Write the size of the message.
  10648. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10649. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10650. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10651. // Write the fields.
  10652. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10653. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10654. }
  10655. }
  10656. }
  10657. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10658. if (otherDictionary) {
  10659. for (int i = 0; i < 2; ++i) {
  10660. if (otherDictionary->_valueSet[i]) {
  10661. _valueSet[i] = YES;
  10662. _values[i] = otherDictionary->_values[i];
  10663. }
  10664. }
  10665. if (_autocreator) {
  10666. GPBAutocreatedDictionaryModified(_autocreator, self);
  10667. }
  10668. }
  10669. }
  10670. - (void)setInt64:(int64_t)value forKey:(BOOL)key {
  10671. int idx = (key ? 1 : 0);
  10672. _values[idx] = value;
  10673. _valueSet[idx] = YES;
  10674. if (_autocreator) {
  10675. GPBAutocreatedDictionaryModified(_autocreator, self);
  10676. }
  10677. }
  10678. - (void)removeInt64ForKey:(BOOL)aKey {
  10679. _valueSet[aKey ? 1 : 0] = NO;
  10680. }
  10681. - (void)removeAll {
  10682. _valueSet[0] = NO;
  10683. _valueSet[1] = NO;
  10684. }
  10685. @end
  10686. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10687. // This block of code is generated, do not edit it directly.
  10688. #pragma mark - Bool -> Bool
  10689. @implementation GPBBoolBoolDictionary {
  10690. @package
  10691. BOOL _values[2];
  10692. BOOL _valueSet[2];
  10693. }
  10694. + (instancetype)dictionary {
  10695. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  10696. }
  10697. + (instancetype)dictionaryWithBool:(BOOL)value
  10698. forKey:(BOOL)key {
  10699. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  10700. // on to get the type correct.
  10701. return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:&value
  10702. forKeys:&key
  10703. count:1] autorelease];
  10704. }
  10705. + (instancetype)dictionaryWithBools:(const BOOL [])values
  10706. forKeys:(const BOOL [])keys
  10707. count:(NSUInteger)count {
  10708. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  10709. // on to get the type correct.
  10710. return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:values
  10711. forKeys:keys
  10712. count:count] autorelease];
  10713. }
  10714. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10715. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10716. // on to get the type correct.
  10717. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10718. }
  10719. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10720. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10721. }
  10722. - (instancetype)init {
  10723. return [self initWithBools:NULL forKeys:NULL count:0];
  10724. }
  10725. - (instancetype)initWithBools:(const BOOL [])values
  10726. forKeys:(const BOOL [])keys
  10727. count:(NSUInteger)count {
  10728. self = [super init];
  10729. if (self) {
  10730. for (NSUInteger i = 0; i < count; ++i) {
  10731. int idx = keys[i] ? 1 : 0;
  10732. _values[idx] = values[i];
  10733. _valueSet[idx] = YES;
  10734. }
  10735. }
  10736. return self;
  10737. }
  10738. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10739. self = [self initWithBools:NULL forKeys:NULL count:0];
  10740. if (self) {
  10741. if (dictionary) {
  10742. for (int i = 0; i < 2; ++i) {
  10743. if (dictionary->_valueSet[i]) {
  10744. _values[i] = dictionary->_values[i];
  10745. _valueSet[i] = YES;
  10746. }
  10747. }
  10748. }
  10749. }
  10750. return self;
  10751. }
  10752. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10753. #pragma unused(numItems)
  10754. return [self initWithBools:NULL forKeys:NULL count:0];
  10755. }
  10756. #if !defined(NS_BLOCK_ASSERTIONS)
  10757. - (void)dealloc {
  10758. NSAssert(!_autocreator,
  10759. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10760. [self class], _autocreator);
  10761. [super dealloc];
  10762. }
  10763. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10764. - (instancetype)copyWithZone:(NSZone *)zone {
  10765. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10766. }
  10767. - (BOOL)isEqual:(id)other {
  10768. if (self == other) {
  10769. return YES;
  10770. }
  10771. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10772. return NO;
  10773. }
  10774. GPBBoolBoolDictionary *otherDictionary = other;
  10775. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10776. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10777. return NO;
  10778. }
  10779. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10780. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10781. return NO;
  10782. }
  10783. return YES;
  10784. }
  10785. - (NSUInteger)hash {
  10786. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10787. }
  10788. - (NSString *)description {
  10789. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10790. if (_valueSet[0]) {
  10791. [result appendFormat:@"NO: %d", _values[0]];
  10792. }
  10793. if (_valueSet[1]) {
  10794. [result appendFormat:@"YES: %d", _values[1]];
  10795. }
  10796. [result appendString:@" }"];
  10797. return result;
  10798. }
  10799. - (NSUInteger)count {
  10800. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10801. }
  10802. - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
  10803. int idx = (key ? 1 : 0);
  10804. if (_valueSet[idx]) {
  10805. if (value) {
  10806. *value = _values[idx];
  10807. }
  10808. return YES;
  10809. }
  10810. return NO;
  10811. }
  10812. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10813. forGPBGenericValueKey:(GPBGenericValue *)key {
  10814. int idx = (key->valueBool ? 1 : 0);
  10815. _values[idx] = value->valueBool;
  10816. _valueSet[idx] = YES;
  10817. }
  10818. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10819. if (_valueSet[0]) {
  10820. block(@"false", (_values[0] ? @"true" : @"false"));
  10821. }
  10822. if (_valueSet[1]) {
  10823. block(@"true", (_values[1] ? @"true" : @"false"));
  10824. }
  10825. }
  10826. - (void)enumerateKeysAndBoolsUsingBlock:
  10827. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10828. BOOL stop = NO;
  10829. if (_valueSet[0]) {
  10830. block(NO, _values[0], &stop);
  10831. }
  10832. if (!stop && _valueSet[1]) {
  10833. block(YES, _values[1], &stop);
  10834. }
  10835. }
  10836. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10837. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10838. NSUInteger count = 0;
  10839. size_t result = 0;
  10840. for (int i = 0; i < 2; ++i) {
  10841. if (_valueSet[i]) {
  10842. ++count;
  10843. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10844. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10845. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10846. }
  10847. }
  10848. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10849. result += tagSize * count;
  10850. return result;
  10851. }
  10852. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10853. asField:(GPBFieldDescriptor *)field {
  10854. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10855. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10856. for (int i = 0; i < 2; ++i) {
  10857. if (_valueSet[i]) {
  10858. // Write the tag.
  10859. [outputStream writeInt32NoTag:tag];
  10860. // Write the size of the message.
  10861. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10862. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10863. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10864. // Write the fields.
  10865. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10866. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10867. }
  10868. }
  10869. }
  10870. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10871. if (otherDictionary) {
  10872. for (int i = 0; i < 2; ++i) {
  10873. if (otherDictionary->_valueSet[i]) {
  10874. _valueSet[i] = YES;
  10875. _values[i] = otherDictionary->_values[i];
  10876. }
  10877. }
  10878. if (_autocreator) {
  10879. GPBAutocreatedDictionaryModified(_autocreator, self);
  10880. }
  10881. }
  10882. }
  10883. - (void)setBool:(BOOL)value forKey:(BOOL)key {
  10884. int idx = (key ? 1 : 0);
  10885. _values[idx] = value;
  10886. _valueSet[idx] = YES;
  10887. if (_autocreator) {
  10888. GPBAutocreatedDictionaryModified(_autocreator, self);
  10889. }
  10890. }
  10891. - (void)removeBoolForKey:(BOOL)aKey {
  10892. _valueSet[aKey ? 1 : 0] = NO;
  10893. }
  10894. - (void)removeAll {
  10895. _valueSet[0] = NO;
  10896. _valueSet[1] = NO;
  10897. }
  10898. @end
  10899. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10900. // This block of code is generated, do not edit it directly.
  10901. #pragma mark - Bool -> Float
  10902. @implementation GPBBoolFloatDictionary {
  10903. @package
  10904. float _values[2];
  10905. BOOL _valueSet[2];
  10906. }
  10907. + (instancetype)dictionary {
  10908. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  10909. }
  10910. + (instancetype)dictionaryWithFloat:(float)value
  10911. forKey:(BOOL)key {
  10912. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  10913. // on to get the type correct.
  10914. return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:&value
  10915. forKeys:&key
  10916. count:1] autorelease];
  10917. }
  10918. + (instancetype)dictionaryWithFloats:(const float [])values
  10919. forKeys:(const BOOL [])keys
  10920. count:(NSUInteger)count {
  10921. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  10922. // on to get the type correct.
  10923. return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:values
  10924. forKeys:keys
  10925. count:count] autorelease];
  10926. }
  10927. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10928. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10929. // on to get the type correct.
  10930. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10931. }
  10932. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10933. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10934. }
  10935. - (instancetype)init {
  10936. return [self initWithFloats:NULL forKeys:NULL count:0];
  10937. }
  10938. - (instancetype)initWithFloats:(const float [])values
  10939. forKeys:(const BOOL [])keys
  10940. count:(NSUInteger)count {
  10941. self = [super init];
  10942. if (self) {
  10943. for (NSUInteger i = 0; i < count; ++i) {
  10944. int idx = keys[i] ? 1 : 0;
  10945. _values[idx] = values[i];
  10946. _valueSet[idx] = YES;
  10947. }
  10948. }
  10949. return self;
  10950. }
  10951. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10952. self = [self initWithFloats:NULL forKeys:NULL count:0];
  10953. if (self) {
  10954. if (dictionary) {
  10955. for (int i = 0; i < 2; ++i) {
  10956. if (dictionary->_valueSet[i]) {
  10957. _values[i] = dictionary->_values[i];
  10958. _valueSet[i] = YES;
  10959. }
  10960. }
  10961. }
  10962. }
  10963. return self;
  10964. }
  10965. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10966. #pragma unused(numItems)
  10967. return [self initWithFloats:NULL forKeys:NULL count:0];
  10968. }
  10969. #if !defined(NS_BLOCK_ASSERTIONS)
  10970. - (void)dealloc {
  10971. NSAssert(!_autocreator,
  10972. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10973. [self class], _autocreator);
  10974. [super dealloc];
  10975. }
  10976. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10977. - (instancetype)copyWithZone:(NSZone *)zone {
  10978. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10979. }
  10980. - (BOOL)isEqual:(id)other {
  10981. if (self == other) {
  10982. return YES;
  10983. }
  10984. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10985. return NO;
  10986. }
  10987. GPBBoolFloatDictionary *otherDictionary = other;
  10988. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10989. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10990. return NO;
  10991. }
  10992. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10993. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10994. return NO;
  10995. }
  10996. return YES;
  10997. }
  10998. - (NSUInteger)hash {
  10999. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11000. }
  11001. - (NSString *)description {
  11002. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11003. if (_valueSet[0]) {
  11004. [result appendFormat:@"NO: %f", _values[0]];
  11005. }
  11006. if (_valueSet[1]) {
  11007. [result appendFormat:@"YES: %f", _values[1]];
  11008. }
  11009. [result appendString:@" }"];
  11010. return result;
  11011. }
  11012. - (NSUInteger)count {
  11013. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11014. }
  11015. - (BOOL)getFloat:(float *)value forKey:(BOOL)key {
  11016. int idx = (key ? 1 : 0);
  11017. if (_valueSet[idx]) {
  11018. if (value) {
  11019. *value = _values[idx];
  11020. }
  11021. return YES;
  11022. }
  11023. return NO;
  11024. }
  11025. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11026. forGPBGenericValueKey:(GPBGenericValue *)key {
  11027. int idx = (key->valueBool ? 1 : 0);
  11028. _values[idx] = value->valueFloat;
  11029. _valueSet[idx] = YES;
  11030. }
  11031. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11032. if (_valueSet[0]) {
  11033. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  11034. }
  11035. if (_valueSet[1]) {
  11036. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  11037. }
  11038. }
  11039. - (void)enumerateKeysAndFloatsUsingBlock:
  11040. (void (^)(BOOL key, float value, BOOL *stop))block {
  11041. BOOL stop = NO;
  11042. if (_valueSet[0]) {
  11043. block(NO, _values[0], &stop);
  11044. }
  11045. if (!stop && _valueSet[1]) {
  11046. block(YES, _values[1], &stop);
  11047. }
  11048. }
  11049. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11050. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11051. NSUInteger count = 0;
  11052. size_t result = 0;
  11053. for (int i = 0; i < 2; ++i) {
  11054. if (_valueSet[i]) {
  11055. ++count;
  11056. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11057. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11058. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11059. }
  11060. }
  11061. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11062. result += tagSize * count;
  11063. return result;
  11064. }
  11065. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11066. asField:(GPBFieldDescriptor *)field {
  11067. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11068. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11069. for (int i = 0; i < 2; ++i) {
  11070. if (_valueSet[i]) {
  11071. // Write the tag.
  11072. [outputStream writeInt32NoTag:tag];
  11073. // Write the size of the message.
  11074. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11075. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11076. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11077. // Write the fields.
  11078. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11079. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11080. }
  11081. }
  11082. }
  11083. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  11084. if (otherDictionary) {
  11085. for (int i = 0; i < 2; ++i) {
  11086. if (otherDictionary->_valueSet[i]) {
  11087. _valueSet[i] = YES;
  11088. _values[i] = otherDictionary->_values[i];
  11089. }
  11090. }
  11091. if (_autocreator) {
  11092. GPBAutocreatedDictionaryModified(_autocreator, self);
  11093. }
  11094. }
  11095. }
  11096. - (void)setFloat:(float)value forKey:(BOOL)key {
  11097. int idx = (key ? 1 : 0);
  11098. _values[idx] = value;
  11099. _valueSet[idx] = YES;
  11100. if (_autocreator) {
  11101. GPBAutocreatedDictionaryModified(_autocreator, self);
  11102. }
  11103. }
  11104. - (void)removeFloatForKey:(BOOL)aKey {
  11105. _valueSet[aKey ? 1 : 0] = NO;
  11106. }
  11107. - (void)removeAll {
  11108. _valueSet[0] = NO;
  11109. _valueSet[1] = NO;
  11110. }
  11111. @end
  11112. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  11113. // This block of code is generated, do not edit it directly.
  11114. #pragma mark - Bool -> Double
  11115. @implementation GPBBoolDoubleDictionary {
  11116. @package
  11117. double _values[2];
  11118. BOOL _valueSet[2];
  11119. }
  11120. + (instancetype)dictionary {
  11121. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  11122. }
  11123. + (instancetype)dictionaryWithDouble:(double)value
  11124. forKey:(BOOL)key {
  11125. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  11126. // on to get the type correct.
  11127. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:&value
  11128. forKeys:&key
  11129. count:1] autorelease];
  11130. }
  11131. + (instancetype)dictionaryWithDoubles:(const double [])values
  11132. forKeys:(const BOOL [])keys
  11133. count:(NSUInteger)count {
  11134. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  11135. // on to get the type correct.
  11136. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:values
  11137. forKeys:keys
  11138. count:count] autorelease];
  11139. }
  11140. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11141. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11142. // on to get the type correct.
  11143. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11144. }
  11145. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11146. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11147. }
  11148. - (instancetype)init {
  11149. return [self initWithDoubles:NULL forKeys:NULL count:0];
  11150. }
  11151. - (instancetype)initWithDoubles:(const double [])values
  11152. forKeys:(const BOOL [])keys
  11153. count:(NSUInteger)count {
  11154. self = [super init];
  11155. if (self) {
  11156. for (NSUInteger i = 0; i < count; ++i) {
  11157. int idx = keys[i] ? 1 : 0;
  11158. _values[idx] = values[i];
  11159. _valueSet[idx] = YES;
  11160. }
  11161. }
  11162. return self;
  11163. }
  11164. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11165. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  11166. if (self) {
  11167. if (dictionary) {
  11168. for (int i = 0; i < 2; ++i) {
  11169. if (dictionary->_valueSet[i]) {
  11170. _values[i] = dictionary->_values[i];
  11171. _valueSet[i] = YES;
  11172. }
  11173. }
  11174. }
  11175. }
  11176. return self;
  11177. }
  11178. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11179. #pragma unused(numItems)
  11180. return [self initWithDoubles:NULL forKeys:NULL count:0];
  11181. }
  11182. #if !defined(NS_BLOCK_ASSERTIONS)
  11183. - (void)dealloc {
  11184. NSAssert(!_autocreator,
  11185. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11186. [self class], _autocreator);
  11187. [super dealloc];
  11188. }
  11189. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11190. - (instancetype)copyWithZone:(NSZone *)zone {
  11191. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  11192. }
  11193. - (BOOL)isEqual:(id)other {
  11194. if (self == other) {
  11195. return YES;
  11196. }
  11197. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  11198. return NO;
  11199. }
  11200. GPBBoolDoubleDictionary *otherDictionary = other;
  11201. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11202. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11203. return NO;
  11204. }
  11205. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11206. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11207. return NO;
  11208. }
  11209. return YES;
  11210. }
  11211. - (NSUInteger)hash {
  11212. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11213. }
  11214. - (NSString *)description {
  11215. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11216. if (_valueSet[0]) {
  11217. [result appendFormat:@"NO: %lf", _values[0]];
  11218. }
  11219. if (_valueSet[1]) {
  11220. [result appendFormat:@"YES: %lf", _values[1]];
  11221. }
  11222. [result appendString:@" }"];
  11223. return result;
  11224. }
  11225. - (NSUInteger)count {
  11226. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11227. }
  11228. - (BOOL)getDouble:(double *)value forKey:(BOOL)key {
  11229. int idx = (key ? 1 : 0);
  11230. if (_valueSet[idx]) {
  11231. if (value) {
  11232. *value = _values[idx];
  11233. }
  11234. return YES;
  11235. }
  11236. return NO;
  11237. }
  11238. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11239. forGPBGenericValueKey:(GPBGenericValue *)key {
  11240. int idx = (key->valueBool ? 1 : 0);
  11241. _values[idx] = value->valueDouble;
  11242. _valueSet[idx] = YES;
  11243. }
  11244. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11245. if (_valueSet[0]) {
  11246. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11247. }
  11248. if (_valueSet[1]) {
  11249. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11250. }
  11251. }
  11252. - (void)enumerateKeysAndDoublesUsingBlock:
  11253. (void (^)(BOOL key, double value, BOOL *stop))block {
  11254. BOOL stop = NO;
  11255. if (_valueSet[0]) {
  11256. block(NO, _values[0], &stop);
  11257. }
  11258. if (!stop && _valueSet[1]) {
  11259. block(YES, _values[1], &stop);
  11260. }
  11261. }
  11262. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11263. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11264. NSUInteger count = 0;
  11265. size_t result = 0;
  11266. for (int i = 0; i < 2; ++i) {
  11267. if (_valueSet[i]) {
  11268. ++count;
  11269. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11270. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11271. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11272. }
  11273. }
  11274. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11275. result += tagSize * count;
  11276. return result;
  11277. }
  11278. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11279. asField:(GPBFieldDescriptor *)field {
  11280. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11281. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11282. for (int i = 0; i < 2; ++i) {
  11283. if (_valueSet[i]) {
  11284. // Write the tag.
  11285. [outputStream writeInt32NoTag:tag];
  11286. // Write the size of the message.
  11287. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11288. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11289. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11290. // Write the fields.
  11291. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11292. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11293. }
  11294. }
  11295. }
  11296. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11297. if (otherDictionary) {
  11298. for (int i = 0; i < 2; ++i) {
  11299. if (otherDictionary->_valueSet[i]) {
  11300. _valueSet[i] = YES;
  11301. _values[i] = otherDictionary->_values[i];
  11302. }
  11303. }
  11304. if (_autocreator) {
  11305. GPBAutocreatedDictionaryModified(_autocreator, self);
  11306. }
  11307. }
  11308. }
  11309. - (void)setDouble:(double)value forKey:(BOOL)key {
  11310. int idx = (key ? 1 : 0);
  11311. _values[idx] = value;
  11312. _valueSet[idx] = YES;
  11313. if (_autocreator) {
  11314. GPBAutocreatedDictionaryModified(_autocreator, self);
  11315. }
  11316. }
  11317. - (void)removeDoubleForKey:(BOOL)aKey {
  11318. _valueSet[aKey ? 1 : 0] = NO;
  11319. }
  11320. - (void)removeAll {
  11321. _valueSet[0] = NO;
  11322. _valueSet[1] = NO;
  11323. }
  11324. @end
  11325. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11326. // This block of code is generated, do not edit it directly.
  11327. #pragma mark - Bool -> Object
  11328. @implementation GPBBoolObjectDictionary {
  11329. @package
  11330. id _values[2];
  11331. }
  11332. + (instancetype)dictionary {
  11333. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11334. }
  11335. + (instancetype)dictionaryWithObject:(id)object
  11336. forKey:(BOOL)key {
  11337. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11338. // on to get the type correct.
  11339. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11340. forKeys:&key
  11341. count:1] autorelease];
  11342. }
  11343. + (instancetype)dictionaryWithObjects:(const id [])objects
  11344. forKeys:(const BOOL [])keys
  11345. count:(NSUInteger)count {
  11346. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11347. // on to get the type correct.
  11348. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11349. forKeys:keys
  11350. count:count] autorelease];
  11351. }
  11352. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11353. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11354. // on to get the type correct.
  11355. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11356. }
  11357. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11358. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11359. }
  11360. - (instancetype)init {
  11361. return [self initWithObjects:NULL forKeys:NULL count:0];
  11362. }
  11363. - (instancetype)initWithObjects:(const id [])objects
  11364. forKeys:(const BOOL [])keys
  11365. count:(NSUInteger)count {
  11366. self = [super init];
  11367. if (self) {
  11368. for (NSUInteger i = 0; i < count; ++i) {
  11369. if (!objects[i]) {
  11370. [NSException raise:NSInvalidArgumentException
  11371. format:@"Attempting to add nil object to a Dictionary"];
  11372. }
  11373. int idx = keys[i] ? 1 : 0;
  11374. [_values[idx] release];
  11375. _values[idx] = (id)[objects[i] retain];
  11376. }
  11377. }
  11378. return self;
  11379. }
  11380. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11381. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11382. if (self) {
  11383. if (dictionary) {
  11384. _values[0] = [dictionary->_values[0] retain];
  11385. _values[1] = [dictionary->_values[1] retain];
  11386. }
  11387. }
  11388. return self;
  11389. }
  11390. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11391. #pragma unused(numItems)
  11392. return [self initWithObjects:NULL forKeys:NULL count:0];
  11393. }
  11394. - (void)dealloc {
  11395. NSAssert(!_autocreator,
  11396. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11397. [self class], _autocreator);
  11398. [_values[0] release];
  11399. [_values[1] release];
  11400. [super dealloc];
  11401. }
  11402. - (instancetype)copyWithZone:(NSZone *)zone {
  11403. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11404. }
  11405. - (BOOL)isEqual:(id)other {
  11406. if (self == other) {
  11407. return YES;
  11408. }
  11409. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11410. return NO;
  11411. }
  11412. GPBBoolObjectDictionary *otherDictionary = other;
  11413. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  11414. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  11415. return NO;
  11416. }
  11417. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  11418. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  11419. return NO;
  11420. }
  11421. return YES;
  11422. }
  11423. - (NSUInteger)hash {
  11424. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11425. }
  11426. - (NSString *)description {
  11427. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11428. if ((_values[0] != nil)) {
  11429. [result appendFormat:@"NO: %@", _values[0]];
  11430. }
  11431. if ((_values[1] != nil)) {
  11432. [result appendFormat:@"YES: %@", _values[1]];
  11433. }
  11434. [result appendString:@" }"];
  11435. return result;
  11436. }
  11437. - (NSUInteger)count {
  11438. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11439. }
  11440. - (id)objectForKey:(BOOL)key {
  11441. return _values[key ? 1 : 0];
  11442. }
  11443. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11444. forGPBGenericValueKey:(GPBGenericValue *)key {
  11445. int idx = (key->valueBool ? 1 : 0);
  11446. [_values[idx] release];
  11447. _values[idx] = [value->valueString retain];
  11448. }
  11449. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11450. if (_values[0] != nil) {
  11451. block(@"false", _values[0]);
  11452. }
  11453. if ((_values[1] != nil)) {
  11454. block(@"true", _values[1]);
  11455. }
  11456. }
  11457. - (void)enumerateKeysAndObjectsUsingBlock:
  11458. (void (^)(BOOL key, id object, BOOL *stop))block {
  11459. BOOL stop = NO;
  11460. if (_values[0] != nil) {
  11461. block(NO, _values[0], &stop);
  11462. }
  11463. if (!stop && (_values[1] != nil)) {
  11464. block(YES, _values[1], &stop);
  11465. }
  11466. }
  11467. - (BOOL)isInitialized {
  11468. if (_values[0] && ![_values[0] isInitialized]) {
  11469. return NO;
  11470. }
  11471. if (_values[1] && ![_values[1] isInitialized]) {
  11472. return NO;
  11473. }
  11474. return YES;
  11475. }
  11476. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11477. GPBBoolObjectDictionary *newDict =
  11478. [[GPBBoolObjectDictionary alloc] init];
  11479. for (int i = 0; i < 2; ++i) {
  11480. if (_values[i] != nil) {
  11481. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11482. }
  11483. }
  11484. return newDict;
  11485. }
  11486. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11487. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11488. NSUInteger count = 0;
  11489. size_t result = 0;
  11490. for (int i = 0; i < 2; ++i) {
  11491. if (_values[i] != nil) {
  11492. ++count;
  11493. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11494. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11495. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11496. }
  11497. }
  11498. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11499. result += tagSize * count;
  11500. return result;
  11501. }
  11502. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11503. asField:(GPBFieldDescriptor *)field {
  11504. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11505. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11506. for (int i = 0; i < 2; ++i) {
  11507. if (_values[i] != nil) {
  11508. // Write the tag.
  11509. [outputStream writeInt32NoTag:tag];
  11510. // Write the size of the message.
  11511. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11512. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11513. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11514. // Write the fields.
  11515. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11516. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11517. }
  11518. }
  11519. }
  11520. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11521. if (otherDictionary) {
  11522. for (int i = 0; i < 2; ++i) {
  11523. if (otherDictionary->_values[i] != nil) {
  11524. [_values[i] release];
  11525. _values[i] = [otherDictionary->_values[i] retain];
  11526. }
  11527. }
  11528. if (_autocreator) {
  11529. GPBAutocreatedDictionaryModified(_autocreator, self);
  11530. }
  11531. }
  11532. }
  11533. - (void)setObject:(id)object forKey:(BOOL)key {
  11534. if (!object) {
  11535. [NSException raise:NSInvalidArgumentException
  11536. format:@"Attempting to add nil object to a Dictionary"];
  11537. }
  11538. int idx = (key ? 1 : 0);
  11539. [_values[idx] release];
  11540. _values[idx] = [object retain];
  11541. if (_autocreator) {
  11542. GPBAutocreatedDictionaryModified(_autocreator, self);
  11543. }
  11544. }
  11545. - (void)removeObjectForKey:(BOOL)aKey {
  11546. int idx = (aKey ? 1 : 0);
  11547. [_values[idx] release];
  11548. _values[idx] = nil;
  11549. }
  11550. - (void)removeAll {
  11551. for (int i = 0; i < 2; ++i) {
  11552. [_values[i] release];
  11553. _values[i] = nil;
  11554. }
  11555. }
  11556. @end
  11557. //%PDDM-EXPAND-END (8 expansions)
  11558. #pragma mark - Bool -> Enum
  11559. @implementation GPBBoolEnumDictionary {
  11560. @package
  11561. GPBEnumValidationFunc _validationFunc;
  11562. int32_t _values[2];
  11563. BOOL _valueSet[2];
  11564. }
  11565. @synthesize validationFunc = _validationFunc;
  11566. + (instancetype)dictionary {
  11567. return [[[self alloc] initWithValidationFunction:NULL
  11568. rawValues:NULL
  11569. forKeys:NULL
  11570. count:0] autorelease];
  11571. }
  11572. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11573. return [[[self alloc] initWithValidationFunction:func
  11574. rawValues:NULL
  11575. forKeys:NULL
  11576. count:0] autorelease];
  11577. }
  11578. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11579. rawValue:(int32_t)rawValue
  11580. forKey:(BOOL)key {
  11581. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11582. // on to get the type correct.
  11583. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11584. rawValues:&rawValue
  11585. forKeys:&key
  11586. count:1] autorelease];
  11587. }
  11588. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11589. rawValues:(const int32_t [])values
  11590. forKeys:(const BOOL [])keys
  11591. count:(NSUInteger)count {
  11592. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11593. // on to get the type correct.
  11594. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11595. rawValues:values
  11596. forKeys:keys
  11597. count:count] autorelease];
  11598. }
  11599. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11600. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11601. // on to get the type correct.
  11602. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11603. }
  11604. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11605. capacity:(NSUInteger)numItems {
  11606. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11607. }
  11608. - (instancetype)init {
  11609. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11610. }
  11611. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11612. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11613. }
  11614. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11615. rawValues:(const int32_t [])rawValues
  11616. forKeys:(const BOOL [])keys
  11617. count:(NSUInteger)count {
  11618. self = [super init];
  11619. if (self) {
  11620. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11621. for (NSUInteger i = 0; i < count; ++i) {
  11622. int idx = keys[i] ? 1 : 0;
  11623. _values[idx] = rawValues[i];
  11624. _valueSet[idx] = YES;
  11625. }
  11626. }
  11627. return self;
  11628. }
  11629. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11630. self = [self initWithValidationFunction:dictionary.validationFunc
  11631. rawValues:NULL
  11632. forKeys:NULL
  11633. count:0];
  11634. if (self) {
  11635. if (dictionary) {
  11636. for (int i = 0; i < 2; ++i) {
  11637. if (dictionary->_valueSet[i]) {
  11638. _values[i] = dictionary->_values[i];
  11639. _valueSet[i] = YES;
  11640. }
  11641. }
  11642. }
  11643. }
  11644. return self;
  11645. }
  11646. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11647. capacity:(NSUInteger)numItems {
  11648. #pragma unused(numItems)
  11649. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11650. }
  11651. #if !defined(NS_BLOCK_ASSERTIONS)
  11652. - (void)dealloc {
  11653. NSAssert(!_autocreator,
  11654. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11655. [self class], _autocreator);
  11656. [super dealloc];
  11657. }
  11658. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11659. - (instancetype)copyWithZone:(NSZone *)zone {
  11660. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11661. }
  11662. - (BOOL)isEqual:(id)other {
  11663. if (self == other) {
  11664. return YES;
  11665. }
  11666. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11667. return NO;
  11668. }
  11669. GPBBoolEnumDictionary *otherDictionary = other;
  11670. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11671. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11672. return NO;
  11673. }
  11674. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11675. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11676. return NO;
  11677. }
  11678. return YES;
  11679. }
  11680. - (NSUInteger)hash {
  11681. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11682. }
  11683. - (NSString *)description {
  11684. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11685. if (_valueSet[0]) {
  11686. [result appendFormat:@"NO: %d", _values[0]];
  11687. }
  11688. if (_valueSet[1]) {
  11689. [result appendFormat:@"YES: %d", _values[1]];
  11690. }
  11691. [result appendString:@" }"];
  11692. return result;
  11693. }
  11694. - (NSUInteger)count {
  11695. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11696. }
  11697. - (BOOL)getEnum:(int32_t*)value forKey:(BOOL)key {
  11698. int idx = (key ? 1 : 0);
  11699. if (_valueSet[idx]) {
  11700. if (value) {
  11701. int32_t result = _values[idx];
  11702. if (!_validationFunc(result)) {
  11703. result = kGPBUnrecognizedEnumeratorValue;
  11704. }
  11705. *value = result;
  11706. }
  11707. return YES;
  11708. }
  11709. return NO;
  11710. }
  11711. - (BOOL)getRawValue:(int32_t*)rawValue forKey:(BOOL)key {
  11712. int idx = (key ? 1 : 0);
  11713. if (_valueSet[idx]) {
  11714. if (rawValue) {
  11715. *rawValue = _values[idx];
  11716. }
  11717. return YES;
  11718. }
  11719. return NO;
  11720. }
  11721. - (void)enumerateKeysAndRawValuesUsingBlock:
  11722. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11723. BOOL stop = NO;
  11724. if (_valueSet[0]) {
  11725. block(NO, _values[0], &stop);
  11726. }
  11727. if (!stop && _valueSet[1]) {
  11728. block(YES, _values[1], &stop);
  11729. }
  11730. }
  11731. - (void)enumerateKeysAndEnumsUsingBlock:
  11732. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11733. BOOL stop = NO;
  11734. GPBEnumValidationFunc func = _validationFunc;
  11735. int32_t validatedValue;
  11736. if (_valueSet[0]) {
  11737. validatedValue = _values[0];
  11738. if (!func(validatedValue)) {
  11739. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11740. }
  11741. block(NO, validatedValue, &stop);
  11742. }
  11743. if (!stop && _valueSet[1]) {
  11744. validatedValue = _values[1];
  11745. if (!func(validatedValue)) {
  11746. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11747. }
  11748. block(YES, validatedValue, &stop);
  11749. }
  11750. }
  11751. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11752. // This block of code is generated, do not edit it directly.
  11753. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11754. forKey:(GPBGenericValue *)key
  11755. keyDataType:(GPBDataType)keyDataType {
  11756. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11757. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11758. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11759. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11760. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11761. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11762. [outputStream release];
  11763. return data;
  11764. }
  11765. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11766. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11767. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11768. NSUInteger count = 0;
  11769. size_t result = 0;
  11770. for (int i = 0; i < 2; ++i) {
  11771. if (_valueSet[i]) {
  11772. ++count;
  11773. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11774. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11775. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11776. }
  11777. }
  11778. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11779. result += tagSize * count;
  11780. return result;
  11781. }
  11782. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11783. asField:(GPBFieldDescriptor *)field {
  11784. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11785. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11786. for (int i = 0; i < 2; ++i) {
  11787. if (_valueSet[i]) {
  11788. // Write the tag.
  11789. [outputStream writeInt32NoTag:tag];
  11790. // Write the size of the message.
  11791. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11792. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11793. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11794. // Write the fields.
  11795. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11796. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11797. }
  11798. }
  11799. }
  11800. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11801. if (_valueSet[0]) {
  11802. block(@"false", @(_values[0]));
  11803. }
  11804. if (_valueSet[1]) {
  11805. block(@"true", @(_values[1]));
  11806. }
  11807. }
  11808. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11809. forGPBGenericValueKey:(GPBGenericValue *)key {
  11810. int idx = (key->valueBool ? 1 : 0);
  11811. _values[idx] = value->valueInt32;
  11812. _valueSet[idx] = YES;
  11813. }
  11814. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11815. if (otherDictionary) {
  11816. for (int i = 0; i < 2; ++i) {
  11817. if (otherDictionary->_valueSet[i]) {
  11818. _valueSet[i] = YES;
  11819. _values[i] = otherDictionary->_values[i];
  11820. }
  11821. }
  11822. if (_autocreator) {
  11823. GPBAutocreatedDictionaryModified(_autocreator, self);
  11824. }
  11825. }
  11826. }
  11827. - (void)setEnum:(int32_t)value forKey:(BOOL)key {
  11828. if (!_validationFunc(value)) {
  11829. [NSException raise:NSInvalidArgumentException
  11830. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11831. value];
  11832. }
  11833. int idx = (key ? 1 : 0);
  11834. _values[idx] = value;
  11835. _valueSet[idx] = YES;
  11836. if (_autocreator) {
  11837. GPBAutocreatedDictionaryModified(_autocreator, self);
  11838. }
  11839. }
  11840. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11841. int idx = (key ? 1 : 0);
  11842. _values[idx] = rawValue;
  11843. _valueSet[idx] = YES;
  11844. if (_autocreator) {
  11845. GPBAutocreatedDictionaryModified(_autocreator, self);
  11846. }
  11847. }
  11848. - (void)removeEnumForKey:(BOOL)aKey {
  11849. _valueSet[aKey ? 1 : 0] = NO;
  11850. }
  11851. - (void)removeAll {
  11852. _valueSet[0] = NO;
  11853. _valueSet[1] = NO;
  11854. }
  11855. @end
  11856. #pragma mark - NSDictionary Subclass
  11857. @implementation GPBAutocreatedDictionary {
  11858. NSMutableDictionary *_dictionary;
  11859. }
  11860. - (void)dealloc {
  11861. NSAssert(!_autocreator,
  11862. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11863. [self class], _autocreator);
  11864. [_dictionary release];
  11865. [super dealloc];
  11866. }
  11867. #pragma mark Required NSDictionary overrides
  11868. - (instancetype)initWithObjects:(const id [])objects
  11869. forKeys:(const id<NSCopying> [])keys
  11870. count:(NSUInteger)count {
  11871. self = [super init];
  11872. if (self) {
  11873. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11874. forKeys:keys
  11875. count:count];
  11876. }
  11877. return self;
  11878. }
  11879. - (NSUInteger)count {
  11880. return [_dictionary count];
  11881. }
  11882. - (id)objectForKey:(id)aKey {
  11883. return [_dictionary objectForKey:aKey];
  11884. }
  11885. - (NSEnumerator *)keyEnumerator {
  11886. if (_dictionary == nil) {
  11887. _dictionary = [[NSMutableDictionary alloc] init];
  11888. }
  11889. return [_dictionary keyEnumerator];
  11890. }
  11891. #pragma mark Required NSMutableDictionary overrides
  11892. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11893. // since we only autocreate empty dictionaries.
  11894. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11895. if (_dictionary == nil) {
  11896. _dictionary = [[NSMutableDictionary alloc] init];
  11897. }
  11898. [_dictionary setObject:anObject forKey:aKey];
  11899. if (_autocreator) {
  11900. GPBAutocreatedDictionaryModified(_autocreator, self);
  11901. }
  11902. }
  11903. - (void)removeObjectForKey:(id)aKey {
  11904. [_dictionary removeObjectForKey:aKey];
  11905. }
  11906. #pragma mark Extra things hooked
  11907. - (id)copyWithZone:(NSZone *)zone {
  11908. if (_dictionary == nil) {
  11909. _dictionary = [[NSMutableDictionary alloc] init];
  11910. }
  11911. return [_dictionary copyWithZone:zone];
  11912. }
  11913. - (id)mutableCopyWithZone:(NSZone *)zone {
  11914. if (_dictionary == nil) {
  11915. _dictionary = [[NSMutableDictionary alloc] init];
  11916. }
  11917. return [_dictionary mutableCopyWithZone:zone];
  11918. }
  11919. - (id)objectForKeyedSubscript:(id)key {
  11920. return [_dictionary objectForKeyedSubscript:key];
  11921. }
  11922. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11923. if (_dictionary == nil) {
  11924. _dictionary = [[NSMutableDictionary alloc] init];
  11925. }
  11926. [_dictionary setObject:obj forKeyedSubscript:key];
  11927. if (_autocreator) {
  11928. GPBAutocreatedDictionaryModified(_autocreator, self);
  11929. }
  11930. }
  11931. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11932. id obj,
  11933. BOOL *stop))block {
  11934. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11935. }
  11936. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11937. usingBlock:(void (^)(id key,
  11938. id obj,
  11939. BOOL *stop))block {
  11940. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11941. }
  11942. @end
  11943. #pragma clang diagnostic pop