GPBDictionary.m 456 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBDictionary_PackagePrivate.h"
  31. #import "GPBCodedInputStream_PackagePrivate.h"
  32. #import "GPBCodedOutputStream.h"
  33. #import "GPBDescriptor_PackagePrivate.h"
  34. #import "GPBMessage_PackagePrivate.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. // ------------------------------ NOTE ------------------------------
  37. // At the moment, this is all using NSNumbers in NSDictionaries under
  38. // the hood, but it is all hidden so we can come back and optimize
  39. // with direct CFDictionary usage later. The reason that wasn't
  40. // done yet is needing to support 32bit iOS builds. Otherwise
  41. // it would be pretty simple to store all this data in CFDictionaries
  42. // directly.
  43. // ------------------------------------------------------------------
  44. enum {
  45. kMapKeyFieldNumber = 1,
  46. kMapValueFieldNumber = 2,
  47. };
  48. static BOOL DictDefault_IsValidValue(int32_t value) {
  49. // Anything but the bad value marker is allowed.
  50. return (value != kGPBUnrecognizedEnumeratorValue);
  51. }
  52. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  53. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  54. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  55. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  56. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  57. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  58. //% } else {
  59. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  60. //% return 0;
  61. //% }
  62. //%}
  63. //%
  64. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  65. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  66. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  67. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  68. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  69. //% } else {
  70. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  71. //% }
  72. //%}
  73. //%
  74. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  75. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  76. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  77. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  78. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  79. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  80. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  81. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  82. //% } else {
  83. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  84. //% return 0;
  85. //% }
  86. //%}
  87. //%
  88. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  89. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  90. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  91. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  92. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  93. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  94. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  95. //% } else {
  96. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  97. //% }
  98. //%}
  99. //%
  100. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  101. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  102. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  103. //% #pragma unused(dataType) // For when asserts are off in release.
  104. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  105. //%}
  106. //%
  107. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  108. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  109. //% #pragma unused(dataType) // For when asserts are off in release.
  110. //% [stream write##VALUE_NAME##:fieldNum value:value];
  111. //%}
  112. //%
  113. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  114. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  115. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  116. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  117. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  118. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  119. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  120. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  121. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  122. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  123. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  124. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  125. // This block of code is generated, do not edit it directly.
  126. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  127. if (dataType == GPBDataTypeInt32) {
  128. return GPBComputeInt32Size(fieldNum, value);
  129. } else if (dataType == GPBDataTypeSInt32) {
  130. return GPBComputeSInt32Size(fieldNum, value);
  131. } else if (dataType == GPBDataTypeSFixed32) {
  132. return GPBComputeSFixed32Size(fieldNum, value);
  133. } else {
  134. NSCAssert(NO, @"Unexpected type %d", dataType);
  135. return 0;
  136. }
  137. }
  138. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  139. if (dataType == GPBDataTypeInt32) {
  140. [stream writeInt32:fieldNum value:value];
  141. } else if (dataType == GPBDataTypeSInt32) {
  142. [stream writeSInt32:fieldNum value:value];
  143. } else if (dataType == GPBDataTypeSFixed32) {
  144. [stream writeSFixed32:fieldNum value:value];
  145. } else {
  146. NSCAssert(NO, @"Unexpected type %d", dataType);
  147. }
  148. }
  149. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  150. if (dataType == GPBDataTypeUInt32) {
  151. return GPBComputeUInt32Size(fieldNum, value);
  152. } else if (dataType == GPBDataTypeFixed32) {
  153. return GPBComputeFixed32Size(fieldNum, value);
  154. } else {
  155. NSCAssert(NO, @"Unexpected type %d", dataType);
  156. return 0;
  157. }
  158. }
  159. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  160. if (dataType == GPBDataTypeUInt32) {
  161. [stream writeUInt32:fieldNum value:value];
  162. } else if (dataType == GPBDataTypeFixed32) {
  163. [stream writeFixed32:fieldNum value:value];
  164. } else {
  165. NSCAssert(NO, @"Unexpected type %d", dataType);
  166. }
  167. }
  168. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  169. if (dataType == GPBDataTypeInt64) {
  170. return GPBComputeInt64Size(fieldNum, value);
  171. } else if (dataType == GPBDataTypeSInt64) {
  172. return GPBComputeSInt64Size(fieldNum, value);
  173. } else if (dataType == GPBDataTypeSFixed64) {
  174. return GPBComputeSFixed64Size(fieldNum, value);
  175. } else {
  176. NSCAssert(NO, @"Unexpected type %d", dataType);
  177. return 0;
  178. }
  179. }
  180. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  181. if (dataType == GPBDataTypeInt64) {
  182. [stream writeInt64:fieldNum value:value];
  183. } else if (dataType == GPBDataTypeSInt64) {
  184. [stream writeSInt64:fieldNum value:value];
  185. } else if (dataType == GPBDataTypeSFixed64) {
  186. [stream writeSFixed64:fieldNum value:value];
  187. } else {
  188. NSCAssert(NO, @"Unexpected type %d", dataType);
  189. }
  190. }
  191. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  192. if (dataType == GPBDataTypeUInt64) {
  193. return GPBComputeUInt64Size(fieldNum, value);
  194. } else if (dataType == GPBDataTypeFixed64) {
  195. return GPBComputeFixed64Size(fieldNum, value);
  196. } else {
  197. NSCAssert(NO, @"Unexpected type %d", dataType);
  198. return 0;
  199. }
  200. }
  201. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  202. if (dataType == GPBDataTypeUInt64) {
  203. [stream writeUInt64:fieldNum value:value];
  204. } else if (dataType == GPBDataTypeFixed64) {
  205. [stream writeFixed64:fieldNum value:value];
  206. } else {
  207. NSCAssert(NO, @"Unexpected type %d", dataType);
  208. }
  209. }
  210. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  211. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  212. #pragma unused(dataType) // For when asserts are off in release.
  213. return GPBComputeBoolSize(fieldNum, value);
  214. }
  215. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  216. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  217. #pragma unused(dataType) // For when asserts are off in release.
  218. [stream writeBool:fieldNum value:value];
  219. }
  220. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  221. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  222. #pragma unused(dataType) // For when asserts are off in release.
  223. return GPBComputeEnumSize(fieldNum, value);
  224. }
  225. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  226. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  227. #pragma unused(dataType) // For when asserts are off in release.
  228. [stream writeEnum:fieldNum value:value];
  229. }
  230. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  231. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  232. #pragma unused(dataType) // For when asserts are off in release.
  233. return GPBComputeFloatSize(fieldNum, value);
  234. }
  235. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
  236. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  237. #pragma unused(dataType) // For when asserts are off in release.
  238. [stream writeFloat:fieldNum value:value];
  239. }
  240. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  241. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  242. #pragma unused(dataType) // For when asserts are off in release.
  243. return GPBComputeDoubleSize(fieldNum, value);
  244. }
  245. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
  246. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  247. #pragma unused(dataType) // For when asserts are off in release.
  248. [stream writeDouble:fieldNum value:value];
  249. }
  250. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  251. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  252. #pragma unused(dataType) // For when asserts are off in release.
  253. return GPBComputeStringSize(fieldNum, value);
  254. }
  255. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  256. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  257. #pragma unused(dataType) // For when asserts are off in release.
  258. [stream writeString:fieldNum value:value];
  259. }
  260. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  261. if (dataType == GPBDataTypeMessage) {
  262. return GPBComputeMessageSize(fieldNum, value);
  263. } else if (dataType == GPBDataTypeString) {
  264. return GPBComputeStringSize(fieldNum, value);
  265. } else if (dataType == GPBDataTypeBytes) {
  266. return GPBComputeBytesSize(fieldNum, value);
  267. } else {
  268. NSCAssert(NO, @"Unexpected type %d", dataType);
  269. return 0;
  270. }
  271. }
  272. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  273. if (dataType == GPBDataTypeMessage) {
  274. [stream writeMessage:fieldNum value:value];
  275. } else if (dataType == GPBDataTypeString) {
  276. [stream writeString:fieldNum value:value];
  277. } else if (dataType == GPBDataTypeBytes) {
  278. [stream writeBytes:fieldNum value:value];
  279. } else {
  280. NSCAssert(NO, @"Unexpected type %d", dataType);
  281. }
  282. }
  283. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  284. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  285. GPBDataType mapValueType = GPBGetFieldDataType(field);
  286. __block size_t result = 0;
  287. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  288. #pragma unused(stop)
  289. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  290. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  291. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  292. }];
  293. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  294. result += tagSize * dict.count;
  295. return result;
  296. }
  297. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  298. NSDictionary *dict,
  299. GPBFieldDescriptor *field) {
  300. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  301. GPBDataType mapValueType = GPBGetFieldDataType(field);
  302. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  303. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  304. #pragma unused(stop)
  305. // Write the tag.
  306. [outputStream writeInt32NoTag:tag];
  307. // Write the size of the message.
  308. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  309. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  310. // Write the size and fields.
  311. [outputStream writeInt32NoTag:(int32_t)msgSize];
  312. [outputStream writeString:kMapKeyFieldNumber value:key];
  313. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  314. }];
  315. }
  316. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  317. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  318. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  319. #pragma unused(field) // For when asserts are off in release.
  320. for (GPBMessage *msg in [dict objectEnumerator]) {
  321. if (!msg.initialized) {
  322. return NO;
  323. }
  324. }
  325. return YES;
  326. }
  327. // Note: if the type is an object, it the retain pass back to the caller.
  328. static void ReadValue(GPBCodedInputStream *stream,
  329. GPBGenericValue *valueToFill,
  330. GPBDataType type,
  331. GPBExtensionRegistry *registry,
  332. GPBFieldDescriptor *field) {
  333. switch (type) {
  334. case GPBDataTypeBool:
  335. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  336. break;
  337. case GPBDataTypeFixed32:
  338. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  339. break;
  340. case GPBDataTypeSFixed32:
  341. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  342. break;
  343. case GPBDataTypeFloat:
  344. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  345. break;
  346. case GPBDataTypeFixed64:
  347. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  348. break;
  349. case GPBDataTypeSFixed64:
  350. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  351. break;
  352. case GPBDataTypeDouble:
  353. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  354. break;
  355. case GPBDataTypeInt32:
  356. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  357. break;
  358. case GPBDataTypeInt64:
  359. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  360. break;
  361. case GPBDataTypeSInt32:
  362. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  363. break;
  364. case GPBDataTypeSInt64:
  365. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  366. break;
  367. case GPBDataTypeUInt32:
  368. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  369. break;
  370. case GPBDataTypeUInt64:
  371. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  372. break;
  373. case GPBDataTypeBytes:
  374. [valueToFill->valueData release];
  375. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  376. break;
  377. case GPBDataTypeString:
  378. [valueToFill->valueString release];
  379. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  380. break;
  381. case GPBDataTypeMessage: {
  382. GPBMessage *message = [[field.msgClass alloc] init];
  383. [stream readMessage:message extensionRegistry:registry];
  384. [valueToFill->valueMessage release];
  385. valueToFill->valueMessage = message;
  386. break;
  387. }
  388. case GPBDataTypeGroup:
  389. NSCAssert(NO, @"Can't happen");
  390. break;
  391. case GPBDataTypeEnum:
  392. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  393. break;
  394. }
  395. }
  396. void GPBDictionaryReadEntry(id mapDictionary,
  397. GPBCodedInputStream *stream,
  398. GPBExtensionRegistry *registry,
  399. GPBFieldDescriptor *field,
  400. GPBMessage *parentMessage) {
  401. GPBDataType keyDataType = field.mapKeyDataType;
  402. GPBDataType valueDataType = GPBGetFieldDataType(field);
  403. GPBGenericValue key;
  404. GPBGenericValue value;
  405. // Zero them (but pick up any enum default for proto2).
  406. key.valueString = value.valueString = nil;
  407. if (valueDataType == GPBDataTypeEnum) {
  408. value = field.defaultValue;
  409. }
  410. GPBCodedInputStreamState *state = &stream->state_;
  411. uint32_t keyTag =
  412. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  413. uint32_t valueTag =
  414. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  415. BOOL hitError = NO;
  416. while (YES) {
  417. uint32_t tag = GPBCodedInputStreamReadTag(state);
  418. if (tag == keyTag) {
  419. ReadValue(stream, &key, keyDataType, registry, field);
  420. } else if (tag == valueTag) {
  421. ReadValue(stream, &value, valueDataType, registry, field);
  422. } else if (tag == 0) {
  423. // zero signals EOF / limit reached
  424. break;
  425. } else { // Unknown
  426. if (![stream skipField:tag]){
  427. hitError = YES;
  428. break;
  429. }
  430. }
  431. }
  432. if (!hitError) {
  433. // Handle the special defaults and/or missing key/value.
  434. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  435. key.valueString = [@"" retain];
  436. }
  437. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  438. switch (valueDataType) {
  439. case GPBDataTypeString:
  440. value.valueString = [@"" retain];
  441. break;
  442. case GPBDataTypeBytes:
  443. value.valueData = [GPBEmptyNSData() retain];
  444. break;
  445. #if defined(__clang_analyzer__)
  446. case GPBDataTypeGroup:
  447. // Maps can't really have Groups as the value type, but this case is needed
  448. // so the analyzer won't report the posibility of send nil in for the value
  449. // in the NSMutableDictionary case below.
  450. #endif
  451. case GPBDataTypeMessage: {
  452. value.valueMessage = [[field.msgClass alloc] init];
  453. break;
  454. }
  455. default:
  456. // Nothing
  457. break;
  458. }
  459. }
  460. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  461. // mapDictionary is an NSMutableDictionary
  462. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  463. forKey:key.valueString];
  464. } else {
  465. if (valueDataType == GPBDataTypeEnum) {
  466. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  467. [field isValidEnumValue:value.valueEnum]) {
  468. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  469. } else {
  470. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  471. forKey:&key
  472. keyDataType:keyDataType];
  473. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  474. }
  475. } else {
  476. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  477. }
  478. }
  479. }
  480. if (GPBDataTypeIsObject(keyDataType)) {
  481. [key.valueString release];
  482. }
  483. if (GPBDataTypeIsObject(valueDataType)) {
  484. [value.valueString release];
  485. }
  486. }
  487. //
  488. // Macros for the common basic cases.
  489. //
  490. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  491. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  492. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  493. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  494. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  495. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  496. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  497. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  498. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  499. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  500. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  501. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  502. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  503. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, value)
  504. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  505. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, object)
  506. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME)
  507. //%#pragma mark - KEY_NAME -> VALUE_NAME
  508. //%
  509. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  510. //% @package
  511. //% NSMutableDictionary *_dictionary;
  512. //%}
  513. //%
  514. //%+ (instancetype)dictionary {
  515. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  516. //%}
  517. //%
  518. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)##VNAME
  519. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  520. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  521. //% // on to get the type correct.
  522. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  523. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  524. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  525. //%}
  526. //%
  527. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  528. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  529. //% ##VNAME$S## count:(NSUInteger)count {
  530. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  531. //% // on to get the type correct.
  532. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  533. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  534. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  535. //%}
  536. //%
  537. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  538. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  539. //% // on to get the type correct.
  540. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  541. //%}
  542. //%
  543. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  544. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  545. //%}
  546. //%
  547. //%- (instancetype)init {
  548. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  549. //%}
  550. //%
  551. //%- (instancetype)initWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  552. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  553. //% ##VNAME$S## count:(NSUInteger)count {
  554. //% self = [super init];
  555. //% if (self) {
  556. //% _dictionary = [[NSMutableDictionary alloc] init];
  557. //% if (count && VNAME##s && keys) {
  558. //% for (NSUInteger i = 0; i < count; ++i) {
  559. //% [_dictionary setObject:WRAPPED##VHELPER(VNAME##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
  560. //% }
  561. //% }
  562. //% }
  563. //% return self;
  564. //%}
  565. //%
  566. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  567. //% self = [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  568. //% if (self) {
  569. //% if (dictionary) {
  570. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  571. //% }
  572. //% }
  573. //% return self;
  574. //%}
  575. //%
  576. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  577. //% #pragma unused(numItems)
  578. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  579. //%}
  580. //%
  581. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  582. //%
  583. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  584. //%
  585. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  586. //%
  587. //%@end
  588. //%
  589. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  590. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  591. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  592. //%#pragma mark - KEY_NAME -> VALUE_NAME
  593. //%
  594. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  595. //% @package
  596. //% NSMutableDictionary *_dictionary;
  597. //% GPBEnumValidationFunc _validationFunc;
  598. //%}
  599. //%
  600. //%@synthesize validationFunc = _validationFunc;
  601. //%
  602. //%+ (instancetype)dictionary {
  603. //% return [[[self alloc] initWithValidationFunction:NULL
  604. //% rawValues:NULL
  605. //% forKeys:NULL
  606. //% count:0] autorelease];
  607. //%}
  608. //%
  609. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  610. //% return [[[self alloc] initWithValidationFunction:func
  611. //% rawValues:NULL
  612. //% forKeys:NULL
  613. //% count:0] autorelease];
  614. //%}
  615. //%
  616. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  617. //% rawValue:(VALUE_TYPE)rawValue
  618. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  619. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  620. //% // on to get the type correct.
  621. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  622. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  623. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  624. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  625. //%}
  626. //%
  627. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  628. //% rawValues:(const VALUE_TYPE [])rawValues
  629. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  630. //% count:(NSUInteger)count {
  631. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  632. //% // on to get the type correct.
  633. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  634. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  635. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  636. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  637. //%}
  638. //%
  639. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  640. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  641. //% // on to get the type correct.
  642. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  643. //%}
  644. //%
  645. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  646. //% capacity:(NSUInteger)numItems {
  647. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  648. //%}
  649. //%
  650. //%- (instancetype)init {
  651. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  652. //%}
  653. //%
  654. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  655. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  656. //%}
  657. //%
  658. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  659. //% rawValues:(const VALUE_TYPE [])rawValues
  660. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  661. //% count:(NSUInteger)count {
  662. //% self = [super init];
  663. //% if (self) {
  664. //% _dictionary = [[NSMutableDictionary alloc] init];
  665. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  666. //% if (count && rawValues && keys) {
  667. //% for (NSUInteger i = 0; i < count; ++i) {
  668. //% [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  669. //% }
  670. //% }
  671. //% }
  672. //% return self;
  673. //%}
  674. //%
  675. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  676. //% self = [self initWithValidationFunction:dictionary.validationFunc
  677. //% rawValues:NULL
  678. //% forKeys:NULL
  679. //% count:0];
  680. //% if (self) {
  681. //% if (dictionary) {
  682. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  683. //% }
  684. //% }
  685. //% return self;
  686. //%}
  687. //%
  688. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  689. //% capacity:(NSUInteger)numItems {
  690. //% #pragma unused(numItems)
  691. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  692. //%}
  693. //%
  694. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  695. //%
  696. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  697. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  698. //% if (wrapped && value) {
  699. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  700. //% if (!_validationFunc(result)) {
  701. //% result = kGPBUnrecognizedEnumeratorValue;
  702. //% }
  703. //% *value = result;
  704. //% }
  705. //% return (wrapped != NULL);
  706. //%}
  707. //%
  708. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  709. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  710. //% if (wrapped && rawValue) {
  711. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  712. //% }
  713. //% return (wrapped != NULL);
  714. //%}
  715. //%
  716. //%- (void)enumerateKeysAndValuesUsingBlock:
  717. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  718. //% GPBEnumValidationFunc func = _validationFunc;
  719. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  720. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  721. //% BOOL *stop) {
  722. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  723. //% if (!func(unwrapped)) {
  724. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  725. //% }
  726. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  727. //% }];
  728. //%}
  729. //%
  730. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  731. //%
  732. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  733. //% if (!_validationFunc(value)) {
  734. //% [NSException raise:NSInvalidArgumentException
  735. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  736. //% value];
  737. //% }
  738. //%
  739. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  740. //% if (_autocreator) {
  741. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  742. //% }
  743. //%}
  744. //%
  745. //%@end
  746. //%
  747. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  748. //%- (void)dealloc {
  749. //% NSAssert(!_autocreator,
  750. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  751. //% [self class], _autocreator);
  752. //% [_dictionary release];
  753. //% [super dealloc];
  754. //%}
  755. //%
  756. //%- (instancetype)copyWithZone:(NSZone *)zone {
  757. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  758. //%}
  759. //%
  760. //%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
  761. //% if (self == other) {
  762. //% return YES;
  763. //% }
  764. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  765. //% return NO;
  766. //% }
  767. //% return [_dictionary isEqual:other->_dictionary];
  768. //%}
  769. //%
  770. //%- (NSUInteger)hash {
  771. //% return _dictionary.count;
  772. //%}
  773. //%
  774. //%- (NSString *)description {
  775. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  776. //%}
  777. //%
  778. //%- (NSUInteger)count {
  779. //% return _dictionary.count;
  780. //%}
  781. //%
  782. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:
  783. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop))block {
  784. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  785. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  786. //% BOOL *stop) {
  787. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME$u), stop);
  788. //% }];
  789. //%}
  790. //%
  791. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  792. //% NSUInteger count = _dictionary.count;
  793. //% if (count == 0) {
  794. //% return 0;
  795. //% }
  796. //%
  797. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  798. //% GPBDataType keyDataType = field.mapKeyDataType;
  799. //% __block size_t result = 0;
  800. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  801. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u##,
  802. //% BOOL *stop) {
  803. //% #pragma unused(stop)
  804. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  805. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  806. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  807. //% }];
  808. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  809. //% result += tagSize * count;
  810. //% return result;
  811. //%}
  812. //%
  813. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  814. //% asField:(GPBFieldDescriptor *)field {
  815. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  816. //% GPBDataType keyDataType = field.mapKeyDataType;
  817. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  818. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  819. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  820. //% BOOL *stop) {
  821. //% #pragma unused(stop)
  822. //% // Write the tag.
  823. //% [outputStream writeInt32NoTag:tag];
  824. //% // Write the size of the message.
  825. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  826. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  827. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  828. //% // Write the fields.
  829. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  830. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  831. //% }];
  832. //%}
  833. //%
  834. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  835. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  836. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  837. //%}
  838. //%
  839. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  840. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop) {
  841. //% #pragma unused(stop)
  842. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME));
  843. //% }];
  844. //%}
  845. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  846. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  847. //% if (otherDictionary) {
  848. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  849. //% if (_autocreator) {
  850. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  851. //% }
  852. //% }
  853. //%}
  854. //%
  855. //%- (void)set##ACCESSOR_NAME##VNAME$u##:(VALUE_TYPE)VNAME forKey:(KEY_TYPE##KisP$S##KisP)key {
  856. //% [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)];
  857. //% if (_autocreator) {
  858. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  859. //% }
  860. //%}
  861. //%
  862. //%- (void)remove##VNAME$u##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  863. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  864. //%}
  865. //%
  866. //%- (void)removeAll {
  867. //% [_dictionary removeAllObjects];
  868. //%}
  869. //
  870. // Custom Generation for Bool keys
  871. //
  872. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  873. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, value)
  874. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  875. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, object)
  876. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME)
  877. //%#pragma mark - Bool -> VALUE_NAME
  878. //%
  879. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  880. //% @package
  881. //% VALUE_TYPE _values[2];
  882. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  883. //%
  884. //%+ (instancetype)dictionary {
  885. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  886. //%}
  887. //%
  888. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)VNAME
  889. //% ##VNAME$S## forKey:(BOOL)key {
  890. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  891. //% // on to get the type correct.
  892. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  893. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  894. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  895. //%}
  896. //%
  897. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  898. //% ##VNAME$S## forKeys:(const BOOL [])keys
  899. //% ##VNAME$S## count:(NSUInteger)count {
  900. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  901. //% // on to get the type correct.
  902. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  903. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  904. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  905. //%}
  906. //%
  907. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  908. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  909. //% // on to get the type correct.
  910. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  911. //%}
  912. //%
  913. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  914. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  915. //%}
  916. //%
  917. //%- (instancetype)init {
  918. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  919. //%}
  920. //%
  921. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  922. //%
  923. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  924. //% #pragma unused(numItems)
  925. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  926. //%}
  927. //%
  928. //%BOOL_DICT_DEALLOC##HELPER()
  929. //%
  930. //%- (instancetype)copyWithZone:(NSZone *)zone {
  931. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  932. //%}
  933. //%
  934. //%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
  935. //% if (self == other) {
  936. //% return YES;
  937. //% }
  938. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  939. //% return NO;
  940. //% }
  941. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
  942. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
  943. //% return NO;
  944. //% }
  945. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
  946. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
  947. //% return NO;
  948. //% }
  949. //% return YES;
  950. //%}
  951. //%
  952. //%- (NSUInteger)hash {
  953. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  954. //%}
  955. //%
  956. //%- (NSString *)description {
  957. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  958. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  959. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  960. //% }
  961. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  962. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  963. //% }
  964. //% [result appendString:@" }"];
  965. //% return result;
  966. //%}
  967. //%
  968. //%- (NSUInteger)count {
  969. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  970. //%}
  971. //%
  972. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  973. //%
  974. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  975. //%
  976. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  977. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  978. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  979. //% }
  980. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  981. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  982. //% }
  983. //%}
  984. //%
  985. //%- (void)enumerateKeysAnd##VNAME$u##sUsingBlock:
  986. //% (void (^)(BOOL key, VALUE_TYPE VNAME, BOOL *stop))block {
  987. //% BOOL stop = NO;
  988. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  989. //% block(NO, _values[0], &stop);
  990. //% }
  991. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  992. //% block(YES, _values[1], &stop);
  993. //% }
  994. //%}
  995. //%
  996. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  997. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  998. //% NSUInteger count = 0;
  999. //% size_t result = 0;
  1000. //% for (int i = 0; i < 2; ++i) {
  1001. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1002. //% ++count;
  1003. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1004. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1005. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1006. //% }
  1007. //% }
  1008. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1009. //% result += tagSize * count;
  1010. //% return result;
  1011. //%}
  1012. //%
  1013. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1014. //% asField:(GPBFieldDescriptor *)field {
  1015. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1016. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1017. //% for (int i = 0; i < 2; ++i) {
  1018. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1019. //% // Write the tag.
  1020. //% [outputStream writeInt32NoTag:tag];
  1021. //% // Write the size of the message.
  1022. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1023. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1024. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1025. //% // Write the fields.
  1026. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1027. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1028. //% }
  1029. //% }
  1030. //%}
  1031. //%
  1032. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1033. //%
  1034. //%@end
  1035. //%
  1036. //
  1037. // Helpers for PODs
  1038. //
  1039. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1040. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  1041. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1042. //% if (wrapped && value) {
  1043. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1044. //% }
  1045. //% return (wrapped != NULL);
  1046. //%}
  1047. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1048. //%@(VALUE)
  1049. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1050. //%[VALUE unsignedIntValue]
  1051. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1052. //%[VALUE intValue]
  1053. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1054. //%[VALUE unsignedLongLongValue]
  1055. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1056. //%[VALUE longLongValue]
  1057. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1058. //%[VALUE boolValue]
  1059. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1060. //%[VALUE floatValue]
  1061. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1062. //%[VALUE doubleValue]
  1063. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1064. //%[VALUE intValue]
  1065. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1066. //%[NSString stringWithFormat:@"%u", VALUE]
  1067. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1068. //%[NSString stringWithFormat:@"%d", VALUE]
  1069. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1070. //%[NSString stringWithFormat:@"%llu", VALUE]
  1071. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1072. //%[NSString stringWithFormat:@"%lld", VALUE]
  1073. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1074. //%(VALUE ? @"true" : @"false")
  1075. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1076. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1077. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1078. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1079. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1080. //%@(VALUE)
  1081. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1082. //%NSNumber *
  1083. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1084. //%VAL1 != VAL2
  1085. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1086. // Empty
  1087. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1088. // Empty
  1089. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1090. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1091. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1092. // Empty
  1093. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1094. // Empty
  1095. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1096. // Empty
  1097. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1098. // Empty
  1099. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1100. // Empty
  1101. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1102. // Empty
  1103. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1104. // Empty
  1105. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1106. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1107. //% forKey:(GPBGenericValue *)key
  1108. //% keyDataType:(GPBDataType)keyDataType {
  1109. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1110. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1111. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1112. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1113. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1114. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1115. //% [outputStream release];
  1116. //% return data;
  1117. //%}
  1118. //%
  1119. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1120. //%value##VALUE_NAME
  1121. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1122. //% BOOL _valueSet[2];
  1123. //%
  1124. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1125. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1126. //% forKeys:(const BOOL [])keys
  1127. //% count:(NSUInteger)count {
  1128. //% self = [super init];
  1129. //% if (self) {
  1130. //% for (NSUInteger i = 0; i < count; ++i) {
  1131. //% int idx = keys[i] ? 1 : 0;
  1132. //% _values[idx] = values[i];
  1133. //% _valueSet[idx] = YES;
  1134. //% }
  1135. //% }
  1136. //% return self;
  1137. //%}
  1138. //%
  1139. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1140. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1141. //% if (self) {
  1142. //% if (dictionary) {
  1143. //% for (int i = 0; i < 2; ++i) {
  1144. //% if (dictionary->_valueSet[i]) {
  1145. //% _values[i] = dictionary->_values[i];
  1146. //% _valueSet[i] = YES;
  1147. //% }
  1148. //% }
  1149. //% }
  1150. //% }
  1151. //% return self;
  1152. //%}
  1153. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1154. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1155. //%- (void)dealloc {
  1156. //% NSAssert(!_autocreator,
  1157. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1158. //% [self class], _autocreator);
  1159. //% [super dealloc];
  1160. //%}
  1161. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1162. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1163. //%BOOL_DICT_HASPOD(IDX, REF)
  1164. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1165. //%REF##_valueSet[IDX]
  1166. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1167. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1168. //% int idx = (key ? 1 : 0);
  1169. //% if (_valueSet[idx]) {
  1170. //% if (value) {
  1171. //% *value = _values[idx];
  1172. //% }
  1173. //% return YES;
  1174. //% }
  1175. //% return NO;
  1176. //%}
  1177. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1178. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1179. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1180. //% int idx = (key->valueBool ? 1 : 0);
  1181. //% _values[idx] = value->value##VALUE_NAME;
  1182. //% _valueSet[idx] = YES;
  1183. //%}
  1184. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1185. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1186. //% if (otherDictionary) {
  1187. //% for (int i = 0; i < 2; ++i) {
  1188. //% if (otherDictionary->_valueSet[i]) {
  1189. //% _valueSet[i] = YES;
  1190. //% _values[i] = otherDictionary->_values[i];
  1191. //% }
  1192. //% }
  1193. //% if (_autocreator) {
  1194. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1195. //% }
  1196. //% }
  1197. //%}
  1198. //%
  1199. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1200. //% int idx = (key ? 1 : 0);
  1201. //% _values[idx] = value;
  1202. //% _valueSet[idx] = YES;
  1203. //% if (_autocreator) {
  1204. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1205. //% }
  1206. //%}
  1207. //%
  1208. //%- (void)removeValueForKey:(BOOL)aKey {
  1209. //% _valueSet[aKey ? 1 : 0] = NO;
  1210. //%}
  1211. //%
  1212. //%- (void)removeAll {
  1213. //% _valueSet[0] = NO;
  1214. //% _valueSet[1] = NO;
  1215. //%}
  1216. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1217. //%STR_FORMAT_##VALUE_NAME()
  1218. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1219. //%%u
  1220. //%PDDM-DEFINE STR_FORMAT_Int32()
  1221. //%%d
  1222. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1223. //%%llu
  1224. //%PDDM-DEFINE STR_FORMAT_Int64()
  1225. //%%lld
  1226. //%PDDM-DEFINE STR_FORMAT_Bool()
  1227. //%%d
  1228. //%PDDM-DEFINE STR_FORMAT_Float()
  1229. //%%f
  1230. //%PDDM-DEFINE STR_FORMAT_Double()
  1231. //%%lf
  1232. //
  1233. // Helpers for Objects
  1234. //
  1235. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1236. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1237. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1238. //% return result;
  1239. //%}
  1240. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1241. //%VALUE
  1242. //%PDDM-DEFINE UNWRAPString(VALUE)
  1243. //%VALUE
  1244. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1245. //%VALUE
  1246. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1247. //%VALUE
  1248. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1249. //%VALUE
  1250. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1251. //%ENUM_TYPEOBJECT_##TYPE()
  1252. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1253. //%NSString *
  1254. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1255. //%id ##
  1256. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1257. //%![VAL1 isEqual:VAL2]
  1258. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1259. //%- (BOOL)isInitialized {
  1260. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1261. //% if (!msg.initialized) {
  1262. //% return NO;
  1263. //% }
  1264. //% }
  1265. //% return YES;
  1266. //%}
  1267. //%
  1268. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1269. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1270. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1271. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1272. //% GPBMessage *msg,
  1273. //% BOOL *stop) {
  1274. //% #pragma unused(stop)
  1275. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1276. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1277. //% [copiedMsg release];
  1278. //% }];
  1279. //% return newDict;
  1280. //%}
  1281. //%
  1282. //%
  1283. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1284. //%- (BOOL)isInitialized {
  1285. //% if (_values[0] && ![_values[0] isInitialized]) {
  1286. //% return NO;
  1287. //% }
  1288. //% if (_values[1] && ![_values[1] isInitialized]) {
  1289. //% return NO;
  1290. //% }
  1291. //% return YES;
  1292. //%}
  1293. //%
  1294. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1295. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1296. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1297. //% for (int i = 0; i < 2; ++i) {
  1298. //% if (_values[i] != nil) {
  1299. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1300. //% }
  1301. //% }
  1302. //% return newDict;
  1303. //%}
  1304. //%
  1305. //%
  1306. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1307. // Empty
  1308. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1309. //%valueString
  1310. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1311. // Empty
  1312. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1313. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1314. //% forKeys:(const BOOL [])keys
  1315. //% count:(NSUInteger)count {
  1316. //% self = [super init];
  1317. //% if (self) {
  1318. //% for (NSUInteger i = 0; i < count; ++i) {
  1319. //% int idx = keys[i] ? 1 : 0;
  1320. //% [_values[idx] release];
  1321. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1322. //% }
  1323. //% }
  1324. //% return self;
  1325. //%}
  1326. //%
  1327. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1328. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1329. //% if (self) {
  1330. //% if (dictionary) {
  1331. //% _values[0] = [dictionary->_values[0] retain];
  1332. //% _values[1] = [dictionary->_values[1] retain];
  1333. //% }
  1334. //% }
  1335. //% return self;
  1336. //%}
  1337. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1338. //%- (void)dealloc {
  1339. //% NSAssert(!_autocreator,
  1340. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1341. //% [self class], _autocreator);
  1342. //% [_values[0] release];
  1343. //% [_values[1] release];
  1344. //% [super dealloc];
  1345. //%}
  1346. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1347. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1348. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1349. //%REF##_values[IDX] != nil
  1350. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1351. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1352. //% return _values[key ? 1 : 0];
  1353. //%}
  1354. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1355. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1356. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1357. //% int idx = (key->valueBool ? 1 : 0);
  1358. //% [_values[idx] release];
  1359. //% _values[idx] = [value->valueString retain];
  1360. //%}
  1361. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1362. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1363. //% if (otherDictionary) {
  1364. //% for (int i = 0; i < 2; ++i) {
  1365. //% if (otherDictionary->_values[i] != nil) {
  1366. //% [_values[i] release];
  1367. //% _values[i] = [otherDictionary->_values[i] retain];
  1368. //% }
  1369. //% }
  1370. //% if (_autocreator) {
  1371. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1372. //% }
  1373. //% }
  1374. //%}
  1375. //%
  1376. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1377. //% int idx = (key ? 1 : 0);
  1378. //% [_values[idx] release];
  1379. //% _values[idx] = [object retain];
  1380. //% if (_autocreator) {
  1381. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1382. //% }
  1383. //%}
  1384. //%
  1385. //%- (void)removeObjectForKey:(BOOL)aKey {
  1386. //% int idx = (aKey ? 1 : 0);
  1387. //% [_values[idx] release];
  1388. //% _values[idx] = nil;
  1389. //%}
  1390. //%
  1391. //%- (void)removeAll {
  1392. //% for (int i = 0; i < 2; ++i) {
  1393. //% [_values[i] release];
  1394. //% _values[i] = nil;
  1395. //% }
  1396. //%}
  1397. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1398. //%%@
  1399. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1400. // This block of code is generated, do not edit it directly.
  1401. #pragma mark - UInt32 -> UInt32
  1402. @implementation GPBUInt32UInt32Dictionary {
  1403. @package
  1404. NSMutableDictionary *_dictionary;
  1405. }
  1406. + (instancetype)dictionary {
  1407. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1408. }
  1409. + (instancetype)dictionaryWithValue:(uint32_t)value
  1410. forKey:(uint32_t)key {
  1411. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1412. // on to get the type correct.
  1413. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1414. forKeys:&key
  1415. count:1] autorelease];
  1416. }
  1417. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1418. forKeys:(const uint32_t [])keys
  1419. count:(NSUInteger)count {
  1420. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1421. // on to get the type correct.
  1422. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1423. forKeys:keys
  1424. count:count] autorelease];
  1425. }
  1426. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1427. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1428. // on to get the type correct.
  1429. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1430. }
  1431. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1432. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1433. }
  1434. - (instancetype)init {
  1435. return [self initWithValues:NULL forKeys:NULL count:0];
  1436. }
  1437. - (instancetype)initWithValues:(const uint32_t [])values
  1438. forKeys:(const uint32_t [])keys
  1439. count:(NSUInteger)count {
  1440. self = [super init];
  1441. if (self) {
  1442. _dictionary = [[NSMutableDictionary alloc] init];
  1443. if (count && values && keys) {
  1444. for (NSUInteger i = 0; i < count; ++i) {
  1445. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1446. }
  1447. }
  1448. }
  1449. return self;
  1450. }
  1451. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1452. self = [self initWithValues:NULL forKeys:NULL count:0];
  1453. if (self) {
  1454. if (dictionary) {
  1455. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1456. }
  1457. }
  1458. return self;
  1459. }
  1460. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1461. #pragma unused(numItems)
  1462. return [self initWithValues:NULL forKeys:NULL count:0];
  1463. }
  1464. - (void)dealloc {
  1465. NSAssert(!_autocreator,
  1466. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1467. [self class], _autocreator);
  1468. [_dictionary release];
  1469. [super dealloc];
  1470. }
  1471. - (instancetype)copyWithZone:(NSZone *)zone {
  1472. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1473. }
  1474. - (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
  1475. if (self == other) {
  1476. return YES;
  1477. }
  1478. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1479. return NO;
  1480. }
  1481. return [_dictionary isEqual:other->_dictionary];
  1482. }
  1483. - (NSUInteger)hash {
  1484. return _dictionary.count;
  1485. }
  1486. - (NSString *)description {
  1487. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1488. }
  1489. - (NSUInteger)count {
  1490. return _dictionary.count;
  1491. }
  1492. - (void)enumerateKeysAndValuesUsingBlock:
  1493. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1494. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1495. NSNumber *aValue,
  1496. BOOL *stop) {
  1497. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1498. }];
  1499. }
  1500. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1501. NSUInteger count = _dictionary.count;
  1502. if (count == 0) {
  1503. return 0;
  1504. }
  1505. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1506. GPBDataType keyDataType = field.mapKeyDataType;
  1507. __block size_t result = 0;
  1508. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1509. NSNumber *aValue,
  1510. BOOL *stop) {
  1511. #pragma unused(stop)
  1512. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1513. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1514. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1515. }];
  1516. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1517. result += tagSize * count;
  1518. return result;
  1519. }
  1520. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1521. asField:(GPBFieldDescriptor *)field {
  1522. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1523. GPBDataType keyDataType = field.mapKeyDataType;
  1524. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1525. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1526. NSNumber *aValue,
  1527. BOOL *stop) {
  1528. #pragma unused(stop)
  1529. // Write the tag.
  1530. [outputStream writeInt32NoTag:tag];
  1531. // Write the size of the message.
  1532. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1533. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1534. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1535. // Write the fields.
  1536. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1537. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1538. }];
  1539. }
  1540. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1541. forGPBGenericValueKey:(GPBGenericValue *)key {
  1542. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1543. }
  1544. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1545. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1546. #pragma unused(stop)
  1547. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1548. }];
  1549. }
  1550. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1551. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1552. if (wrapped && value) {
  1553. *value = [wrapped unsignedIntValue];
  1554. }
  1555. return (wrapped != NULL);
  1556. }
  1557. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1558. if (otherDictionary) {
  1559. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1560. if (_autocreator) {
  1561. GPBAutocreatedDictionaryModified(_autocreator, self);
  1562. }
  1563. }
  1564. }
  1565. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1566. [_dictionary setObject:@(value) forKey:@(key)];
  1567. if (_autocreator) {
  1568. GPBAutocreatedDictionaryModified(_autocreator, self);
  1569. }
  1570. }
  1571. - (void)removeValueForKey:(uint32_t)aKey {
  1572. [_dictionary removeObjectForKey:@(aKey)];
  1573. }
  1574. - (void)removeAll {
  1575. [_dictionary removeAllObjects];
  1576. }
  1577. @end
  1578. #pragma mark - UInt32 -> Int32
  1579. @implementation GPBUInt32Int32Dictionary {
  1580. @package
  1581. NSMutableDictionary *_dictionary;
  1582. }
  1583. + (instancetype)dictionary {
  1584. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1585. }
  1586. + (instancetype)dictionaryWithValue:(int32_t)value
  1587. forKey:(uint32_t)key {
  1588. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1589. // on to get the type correct.
  1590. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1591. forKeys:&key
  1592. count:1] autorelease];
  1593. }
  1594. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1595. forKeys:(const uint32_t [])keys
  1596. count:(NSUInteger)count {
  1597. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1598. // on to get the type correct.
  1599. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1600. forKeys:keys
  1601. count:count] autorelease];
  1602. }
  1603. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1604. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1605. // on to get the type correct.
  1606. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1607. }
  1608. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1609. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1610. }
  1611. - (instancetype)init {
  1612. return [self initWithValues:NULL forKeys:NULL count:0];
  1613. }
  1614. - (instancetype)initWithValues:(const int32_t [])values
  1615. forKeys:(const uint32_t [])keys
  1616. count:(NSUInteger)count {
  1617. self = [super init];
  1618. if (self) {
  1619. _dictionary = [[NSMutableDictionary alloc] init];
  1620. if (count && values && keys) {
  1621. for (NSUInteger i = 0; i < count; ++i) {
  1622. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1623. }
  1624. }
  1625. }
  1626. return self;
  1627. }
  1628. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1629. self = [self initWithValues:NULL forKeys:NULL count:0];
  1630. if (self) {
  1631. if (dictionary) {
  1632. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1633. }
  1634. }
  1635. return self;
  1636. }
  1637. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1638. #pragma unused(numItems)
  1639. return [self initWithValues:NULL forKeys:NULL count:0];
  1640. }
  1641. - (void)dealloc {
  1642. NSAssert(!_autocreator,
  1643. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1644. [self class], _autocreator);
  1645. [_dictionary release];
  1646. [super dealloc];
  1647. }
  1648. - (instancetype)copyWithZone:(NSZone *)zone {
  1649. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1650. }
  1651. - (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
  1652. if (self == other) {
  1653. return YES;
  1654. }
  1655. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1656. return NO;
  1657. }
  1658. return [_dictionary isEqual:other->_dictionary];
  1659. }
  1660. - (NSUInteger)hash {
  1661. return _dictionary.count;
  1662. }
  1663. - (NSString *)description {
  1664. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1665. }
  1666. - (NSUInteger)count {
  1667. return _dictionary.count;
  1668. }
  1669. - (void)enumerateKeysAndValuesUsingBlock:
  1670. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1671. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1672. NSNumber *aValue,
  1673. BOOL *stop) {
  1674. block([aKey unsignedIntValue], [aValue intValue], stop);
  1675. }];
  1676. }
  1677. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1678. NSUInteger count = _dictionary.count;
  1679. if (count == 0) {
  1680. return 0;
  1681. }
  1682. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1683. GPBDataType keyDataType = field.mapKeyDataType;
  1684. __block size_t result = 0;
  1685. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1686. NSNumber *aValue,
  1687. BOOL *stop) {
  1688. #pragma unused(stop)
  1689. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1690. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1691. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1692. }];
  1693. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1694. result += tagSize * count;
  1695. return result;
  1696. }
  1697. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1698. asField:(GPBFieldDescriptor *)field {
  1699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1700. GPBDataType keyDataType = field.mapKeyDataType;
  1701. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1703. NSNumber *aValue,
  1704. BOOL *stop) {
  1705. #pragma unused(stop)
  1706. // Write the tag.
  1707. [outputStream writeInt32NoTag:tag];
  1708. // Write the size of the message.
  1709. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1710. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1711. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1712. // Write the fields.
  1713. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1714. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1715. }];
  1716. }
  1717. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1718. forGPBGenericValueKey:(GPBGenericValue *)key {
  1719. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1720. }
  1721. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1722. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1723. #pragma unused(stop)
  1724. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1725. }];
  1726. }
  1727. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1728. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1729. if (wrapped && value) {
  1730. *value = [wrapped intValue];
  1731. }
  1732. return (wrapped != NULL);
  1733. }
  1734. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1735. if (otherDictionary) {
  1736. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1737. if (_autocreator) {
  1738. GPBAutocreatedDictionaryModified(_autocreator, self);
  1739. }
  1740. }
  1741. }
  1742. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1743. [_dictionary setObject:@(value) forKey:@(key)];
  1744. if (_autocreator) {
  1745. GPBAutocreatedDictionaryModified(_autocreator, self);
  1746. }
  1747. }
  1748. - (void)removeValueForKey:(uint32_t)aKey {
  1749. [_dictionary removeObjectForKey:@(aKey)];
  1750. }
  1751. - (void)removeAll {
  1752. [_dictionary removeAllObjects];
  1753. }
  1754. @end
  1755. #pragma mark - UInt32 -> UInt64
  1756. @implementation GPBUInt32UInt64Dictionary {
  1757. @package
  1758. NSMutableDictionary *_dictionary;
  1759. }
  1760. + (instancetype)dictionary {
  1761. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1762. }
  1763. + (instancetype)dictionaryWithValue:(uint64_t)value
  1764. forKey:(uint32_t)key {
  1765. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1766. // on to get the type correct.
  1767. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1768. forKeys:&key
  1769. count:1] autorelease];
  1770. }
  1771. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1772. forKeys:(const uint32_t [])keys
  1773. count:(NSUInteger)count {
  1774. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1775. // on to get the type correct.
  1776. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1777. forKeys:keys
  1778. count:count] autorelease];
  1779. }
  1780. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1781. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1782. // on to get the type correct.
  1783. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1784. }
  1785. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1786. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1787. }
  1788. - (instancetype)init {
  1789. return [self initWithValues:NULL forKeys:NULL count:0];
  1790. }
  1791. - (instancetype)initWithValues:(const uint64_t [])values
  1792. forKeys:(const uint32_t [])keys
  1793. count:(NSUInteger)count {
  1794. self = [super init];
  1795. if (self) {
  1796. _dictionary = [[NSMutableDictionary alloc] init];
  1797. if (count && values && keys) {
  1798. for (NSUInteger i = 0; i < count; ++i) {
  1799. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1800. }
  1801. }
  1802. }
  1803. return self;
  1804. }
  1805. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1806. self = [self initWithValues:NULL forKeys:NULL count:0];
  1807. if (self) {
  1808. if (dictionary) {
  1809. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1810. }
  1811. }
  1812. return self;
  1813. }
  1814. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1815. #pragma unused(numItems)
  1816. return [self initWithValues:NULL forKeys:NULL count:0];
  1817. }
  1818. - (void)dealloc {
  1819. NSAssert(!_autocreator,
  1820. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1821. [self class], _autocreator);
  1822. [_dictionary release];
  1823. [super dealloc];
  1824. }
  1825. - (instancetype)copyWithZone:(NSZone *)zone {
  1826. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1827. }
  1828. - (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
  1829. if (self == other) {
  1830. return YES;
  1831. }
  1832. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1833. return NO;
  1834. }
  1835. return [_dictionary isEqual:other->_dictionary];
  1836. }
  1837. - (NSUInteger)hash {
  1838. return _dictionary.count;
  1839. }
  1840. - (NSString *)description {
  1841. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1842. }
  1843. - (NSUInteger)count {
  1844. return _dictionary.count;
  1845. }
  1846. - (void)enumerateKeysAndValuesUsingBlock:
  1847. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1848. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1849. NSNumber *aValue,
  1850. BOOL *stop) {
  1851. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1852. }];
  1853. }
  1854. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1855. NSUInteger count = _dictionary.count;
  1856. if (count == 0) {
  1857. return 0;
  1858. }
  1859. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1860. GPBDataType keyDataType = field.mapKeyDataType;
  1861. __block size_t result = 0;
  1862. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1863. NSNumber *aValue,
  1864. BOOL *stop) {
  1865. #pragma unused(stop)
  1866. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1867. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1868. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1869. }];
  1870. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1871. result += tagSize * count;
  1872. return result;
  1873. }
  1874. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1875. asField:(GPBFieldDescriptor *)field {
  1876. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1877. GPBDataType keyDataType = field.mapKeyDataType;
  1878. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1879. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1880. NSNumber *aValue,
  1881. BOOL *stop) {
  1882. #pragma unused(stop)
  1883. // Write the tag.
  1884. [outputStream writeInt32NoTag:tag];
  1885. // Write the size of the message.
  1886. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1887. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1888. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1889. // Write the fields.
  1890. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1891. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1892. }];
  1893. }
  1894. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1895. forGPBGenericValueKey:(GPBGenericValue *)key {
  1896. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1897. }
  1898. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1899. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1900. #pragma unused(stop)
  1901. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1902. }];
  1903. }
  1904. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1905. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1906. if (wrapped && value) {
  1907. *value = [wrapped unsignedLongLongValue];
  1908. }
  1909. return (wrapped != NULL);
  1910. }
  1911. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1912. if (otherDictionary) {
  1913. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1914. if (_autocreator) {
  1915. GPBAutocreatedDictionaryModified(_autocreator, self);
  1916. }
  1917. }
  1918. }
  1919. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1920. [_dictionary setObject:@(value) forKey:@(key)];
  1921. if (_autocreator) {
  1922. GPBAutocreatedDictionaryModified(_autocreator, self);
  1923. }
  1924. }
  1925. - (void)removeValueForKey:(uint32_t)aKey {
  1926. [_dictionary removeObjectForKey:@(aKey)];
  1927. }
  1928. - (void)removeAll {
  1929. [_dictionary removeAllObjects];
  1930. }
  1931. @end
  1932. #pragma mark - UInt32 -> Int64
  1933. @implementation GPBUInt32Int64Dictionary {
  1934. @package
  1935. NSMutableDictionary *_dictionary;
  1936. }
  1937. + (instancetype)dictionary {
  1938. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1939. }
  1940. + (instancetype)dictionaryWithValue:(int64_t)value
  1941. forKey:(uint32_t)key {
  1942. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1943. // on to get the type correct.
  1944. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  1945. forKeys:&key
  1946. count:1] autorelease];
  1947. }
  1948. + (instancetype)dictionaryWithValues:(const int64_t [])values
  1949. forKeys:(const uint32_t [])keys
  1950. count:(NSUInteger)count {
  1951. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1952. // on to get the type correct.
  1953. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  1954. forKeys:keys
  1955. count:count] autorelease];
  1956. }
  1957. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1958. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1959. // on to get the type correct.
  1960. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1961. }
  1962. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1963. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1964. }
  1965. - (instancetype)init {
  1966. return [self initWithValues:NULL forKeys:NULL count:0];
  1967. }
  1968. - (instancetype)initWithValues:(const int64_t [])values
  1969. forKeys:(const uint32_t [])keys
  1970. count:(NSUInteger)count {
  1971. self = [super init];
  1972. if (self) {
  1973. _dictionary = [[NSMutableDictionary alloc] init];
  1974. if (count && values && keys) {
  1975. for (NSUInteger i = 0; i < count; ++i) {
  1976. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1977. }
  1978. }
  1979. }
  1980. return self;
  1981. }
  1982. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1983. self = [self initWithValues:NULL forKeys:NULL count:0];
  1984. if (self) {
  1985. if (dictionary) {
  1986. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1987. }
  1988. }
  1989. return self;
  1990. }
  1991. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1992. #pragma unused(numItems)
  1993. return [self initWithValues:NULL forKeys:NULL count:0];
  1994. }
  1995. - (void)dealloc {
  1996. NSAssert(!_autocreator,
  1997. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1998. [self class], _autocreator);
  1999. [_dictionary release];
  2000. [super dealloc];
  2001. }
  2002. - (instancetype)copyWithZone:(NSZone *)zone {
  2003. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2004. }
  2005. - (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
  2006. if (self == other) {
  2007. return YES;
  2008. }
  2009. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2010. return NO;
  2011. }
  2012. return [_dictionary isEqual:other->_dictionary];
  2013. }
  2014. - (NSUInteger)hash {
  2015. return _dictionary.count;
  2016. }
  2017. - (NSString *)description {
  2018. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2019. }
  2020. - (NSUInteger)count {
  2021. return _dictionary.count;
  2022. }
  2023. - (void)enumerateKeysAndValuesUsingBlock:
  2024. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2025. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2026. NSNumber *aValue,
  2027. BOOL *stop) {
  2028. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2029. }];
  2030. }
  2031. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2032. NSUInteger count = _dictionary.count;
  2033. if (count == 0) {
  2034. return 0;
  2035. }
  2036. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2037. GPBDataType keyDataType = field.mapKeyDataType;
  2038. __block size_t result = 0;
  2039. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2040. NSNumber *aValue,
  2041. BOOL *stop) {
  2042. #pragma unused(stop)
  2043. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2044. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2045. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2046. }];
  2047. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2048. result += tagSize * count;
  2049. return result;
  2050. }
  2051. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2052. asField:(GPBFieldDescriptor *)field {
  2053. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2054. GPBDataType keyDataType = field.mapKeyDataType;
  2055. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2056. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2057. NSNumber *aValue,
  2058. BOOL *stop) {
  2059. #pragma unused(stop)
  2060. // Write the tag.
  2061. [outputStream writeInt32NoTag:tag];
  2062. // Write the size of the message.
  2063. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2064. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2065. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2066. // Write the fields.
  2067. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2068. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2069. }];
  2070. }
  2071. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2072. forGPBGenericValueKey:(GPBGenericValue *)key {
  2073. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2074. }
  2075. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2076. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2077. #pragma unused(stop)
  2078. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2079. }];
  2080. }
  2081. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  2082. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2083. if (wrapped && value) {
  2084. *value = [wrapped longLongValue];
  2085. }
  2086. return (wrapped != NULL);
  2087. }
  2088. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2089. if (otherDictionary) {
  2090. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2091. if (_autocreator) {
  2092. GPBAutocreatedDictionaryModified(_autocreator, self);
  2093. }
  2094. }
  2095. }
  2096. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  2097. [_dictionary setObject:@(value) forKey:@(key)];
  2098. if (_autocreator) {
  2099. GPBAutocreatedDictionaryModified(_autocreator, self);
  2100. }
  2101. }
  2102. - (void)removeValueForKey:(uint32_t)aKey {
  2103. [_dictionary removeObjectForKey:@(aKey)];
  2104. }
  2105. - (void)removeAll {
  2106. [_dictionary removeAllObjects];
  2107. }
  2108. @end
  2109. #pragma mark - UInt32 -> Bool
  2110. @implementation GPBUInt32BoolDictionary {
  2111. @package
  2112. NSMutableDictionary *_dictionary;
  2113. }
  2114. + (instancetype)dictionary {
  2115. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2116. }
  2117. + (instancetype)dictionaryWithValue:(BOOL)value
  2118. forKey:(uint32_t)key {
  2119. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2120. // on to get the type correct.
  2121. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2122. forKeys:&key
  2123. count:1] autorelease];
  2124. }
  2125. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2126. forKeys:(const uint32_t [])keys
  2127. count:(NSUInteger)count {
  2128. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2129. // on to get the type correct.
  2130. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2131. forKeys:keys
  2132. count:count] autorelease];
  2133. }
  2134. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2135. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2136. // on to get the type correct.
  2137. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2138. }
  2139. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2140. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2141. }
  2142. - (instancetype)init {
  2143. return [self initWithValues:NULL forKeys:NULL count:0];
  2144. }
  2145. - (instancetype)initWithValues:(const BOOL [])values
  2146. forKeys:(const uint32_t [])keys
  2147. count:(NSUInteger)count {
  2148. self = [super init];
  2149. if (self) {
  2150. _dictionary = [[NSMutableDictionary alloc] init];
  2151. if (count && values && keys) {
  2152. for (NSUInteger i = 0; i < count; ++i) {
  2153. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2154. }
  2155. }
  2156. }
  2157. return self;
  2158. }
  2159. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2160. self = [self initWithValues:NULL forKeys:NULL count:0];
  2161. if (self) {
  2162. if (dictionary) {
  2163. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2164. }
  2165. }
  2166. return self;
  2167. }
  2168. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2169. #pragma unused(numItems)
  2170. return [self initWithValues:NULL forKeys:NULL count:0];
  2171. }
  2172. - (void)dealloc {
  2173. NSAssert(!_autocreator,
  2174. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2175. [self class], _autocreator);
  2176. [_dictionary release];
  2177. [super dealloc];
  2178. }
  2179. - (instancetype)copyWithZone:(NSZone *)zone {
  2180. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2181. }
  2182. - (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
  2183. if (self == other) {
  2184. return YES;
  2185. }
  2186. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2187. return NO;
  2188. }
  2189. return [_dictionary isEqual:other->_dictionary];
  2190. }
  2191. - (NSUInteger)hash {
  2192. return _dictionary.count;
  2193. }
  2194. - (NSString *)description {
  2195. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2196. }
  2197. - (NSUInteger)count {
  2198. return _dictionary.count;
  2199. }
  2200. - (void)enumerateKeysAndValuesUsingBlock:
  2201. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2202. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2203. NSNumber *aValue,
  2204. BOOL *stop) {
  2205. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2206. }];
  2207. }
  2208. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2209. NSUInteger count = _dictionary.count;
  2210. if (count == 0) {
  2211. return 0;
  2212. }
  2213. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2214. GPBDataType keyDataType = field.mapKeyDataType;
  2215. __block size_t result = 0;
  2216. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2217. NSNumber *aValue,
  2218. BOOL *stop) {
  2219. #pragma unused(stop)
  2220. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2221. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2222. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2223. }];
  2224. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2225. result += tagSize * count;
  2226. return result;
  2227. }
  2228. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2229. asField:(GPBFieldDescriptor *)field {
  2230. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2231. GPBDataType keyDataType = field.mapKeyDataType;
  2232. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2233. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2234. NSNumber *aValue,
  2235. BOOL *stop) {
  2236. #pragma unused(stop)
  2237. // Write the tag.
  2238. [outputStream writeInt32NoTag:tag];
  2239. // Write the size of the message.
  2240. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2241. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2242. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2243. // Write the fields.
  2244. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2245. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2246. }];
  2247. }
  2248. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2249. forGPBGenericValueKey:(GPBGenericValue *)key {
  2250. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2251. }
  2252. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2253. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2254. #pragma unused(stop)
  2255. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2256. }];
  2257. }
  2258. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2259. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2260. if (wrapped && value) {
  2261. *value = [wrapped boolValue];
  2262. }
  2263. return (wrapped != NULL);
  2264. }
  2265. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2266. if (otherDictionary) {
  2267. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2268. if (_autocreator) {
  2269. GPBAutocreatedDictionaryModified(_autocreator, self);
  2270. }
  2271. }
  2272. }
  2273. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2274. [_dictionary setObject:@(value) forKey:@(key)];
  2275. if (_autocreator) {
  2276. GPBAutocreatedDictionaryModified(_autocreator, self);
  2277. }
  2278. }
  2279. - (void)removeValueForKey:(uint32_t)aKey {
  2280. [_dictionary removeObjectForKey:@(aKey)];
  2281. }
  2282. - (void)removeAll {
  2283. [_dictionary removeAllObjects];
  2284. }
  2285. @end
  2286. #pragma mark - UInt32 -> Float
  2287. @implementation GPBUInt32FloatDictionary {
  2288. @package
  2289. NSMutableDictionary *_dictionary;
  2290. }
  2291. + (instancetype)dictionary {
  2292. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2293. }
  2294. + (instancetype)dictionaryWithValue:(float)value
  2295. forKey:(uint32_t)key {
  2296. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2297. // on to get the type correct.
  2298. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2299. forKeys:&key
  2300. count:1] autorelease];
  2301. }
  2302. + (instancetype)dictionaryWithValues:(const float [])values
  2303. forKeys:(const uint32_t [])keys
  2304. count:(NSUInteger)count {
  2305. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2306. // on to get the type correct.
  2307. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2308. forKeys:keys
  2309. count:count] autorelease];
  2310. }
  2311. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2312. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2313. // on to get the type correct.
  2314. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2315. }
  2316. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2317. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2318. }
  2319. - (instancetype)init {
  2320. return [self initWithValues:NULL forKeys:NULL count:0];
  2321. }
  2322. - (instancetype)initWithValues:(const float [])values
  2323. forKeys:(const uint32_t [])keys
  2324. count:(NSUInteger)count {
  2325. self = [super init];
  2326. if (self) {
  2327. _dictionary = [[NSMutableDictionary alloc] init];
  2328. if (count && values && keys) {
  2329. for (NSUInteger i = 0; i < count; ++i) {
  2330. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2331. }
  2332. }
  2333. }
  2334. return self;
  2335. }
  2336. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2337. self = [self initWithValues:NULL forKeys:NULL count:0];
  2338. if (self) {
  2339. if (dictionary) {
  2340. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2341. }
  2342. }
  2343. return self;
  2344. }
  2345. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2346. #pragma unused(numItems)
  2347. return [self initWithValues:NULL forKeys:NULL count:0];
  2348. }
  2349. - (void)dealloc {
  2350. NSAssert(!_autocreator,
  2351. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2352. [self class], _autocreator);
  2353. [_dictionary release];
  2354. [super dealloc];
  2355. }
  2356. - (instancetype)copyWithZone:(NSZone *)zone {
  2357. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2358. }
  2359. - (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
  2360. if (self == other) {
  2361. return YES;
  2362. }
  2363. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2364. return NO;
  2365. }
  2366. return [_dictionary isEqual:other->_dictionary];
  2367. }
  2368. - (NSUInteger)hash {
  2369. return _dictionary.count;
  2370. }
  2371. - (NSString *)description {
  2372. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2373. }
  2374. - (NSUInteger)count {
  2375. return _dictionary.count;
  2376. }
  2377. - (void)enumerateKeysAndValuesUsingBlock:
  2378. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2379. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2380. NSNumber *aValue,
  2381. BOOL *stop) {
  2382. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2383. }];
  2384. }
  2385. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2386. NSUInteger count = _dictionary.count;
  2387. if (count == 0) {
  2388. return 0;
  2389. }
  2390. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2391. GPBDataType keyDataType = field.mapKeyDataType;
  2392. __block size_t result = 0;
  2393. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2394. NSNumber *aValue,
  2395. BOOL *stop) {
  2396. #pragma unused(stop)
  2397. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2398. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2399. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2400. }];
  2401. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2402. result += tagSize * count;
  2403. return result;
  2404. }
  2405. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2406. asField:(GPBFieldDescriptor *)field {
  2407. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2408. GPBDataType keyDataType = field.mapKeyDataType;
  2409. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2410. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2411. NSNumber *aValue,
  2412. BOOL *stop) {
  2413. #pragma unused(stop)
  2414. // Write the tag.
  2415. [outputStream writeInt32NoTag:tag];
  2416. // Write the size of the message.
  2417. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2418. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2419. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2420. // Write the fields.
  2421. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2422. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2423. }];
  2424. }
  2425. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2426. forGPBGenericValueKey:(GPBGenericValue *)key {
  2427. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2428. }
  2429. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2430. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2431. #pragma unused(stop)
  2432. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2433. }];
  2434. }
  2435. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2436. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2437. if (wrapped && value) {
  2438. *value = [wrapped floatValue];
  2439. }
  2440. return (wrapped != NULL);
  2441. }
  2442. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2443. if (otherDictionary) {
  2444. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2445. if (_autocreator) {
  2446. GPBAutocreatedDictionaryModified(_autocreator, self);
  2447. }
  2448. }
  2449. }
  2450. - (void)setValue:(float)value forKey:(uint32_t)key {
  2451. [_dictionary setObject:@(value) forKey:@(key)];
  2452. if (_autocreator) {
  2453. GPBAutocreatedDictionaryModified(_autocreator, self);
  2454. }
  2455. }
  2456. - (void)removeValueForKey:(uint32_t)aKey {
  2457. [_dictionary removeObjectForKey:@(aKey)];
  2458. }
  2459. - (void)removeAll {
  2460. [_dictionary removeAllObjects];
  2461. }
  2462. @end
  2463. #pragma mark - UInt32 -> Double
  2464. @implementation GPBUInt32DoubleDictionary {
  2465. @package
  2466. NSMutableDictionary *_dictionary;
  2467. }
  2468. + (instancetype)dictionary {
  2469. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2470. }
  2471. + (instancetype)dictionaryWithValue:(double)value
  2472. forKey:(uint32_t)key {
  2473. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2474. // on to get the type correct.
  2475. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2476. forKeys:&key
  2477. count:1] autorelease];
  2478. }
  2479. + (instancetype)dictionaryWithValues:(const double [])values
  2480. forKeys:(const uint32_t [])keys
  2481. count:(NSUInteger)count {
  2482. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2483. // on to get the type correct.
  2484. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2485. forKeys:keys
  2486. count:count] autorelease];
  2487. }
  2488. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2489. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2490. // on to get the type correct.
  2491. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2492. }
  2493. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2494. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2495. }
  2496. - (instancetype)init {
  2497. return [self initWithValues:NULL forKeys:NULL count:0];
  2498. }
  2499. - (instancetype)initWithValues:(const double [])values
  2500. forKeys:(const uint32_t [])keys
  2501. count:(NSUInteger)count {
  2502. self = [super init];
  2503. if (self) {
  2504. _dictionary = [[NSMutableDictionary alloc] init];
  2505. if (count && values && keys) {
  2506. for (NSUInteger i = 0; i < count; ++i) {
  2507. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2508. }
  2509. }
  2510. }
  2511. return self;
  2512. }
  2513. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2514. self = [self initWithValues:NULL forKeys:NULL count:0];
  2515. if (self) {
  2516. if (dictionary) {
  2517. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2518. }
  2519. }
  2520. return self;
  2521. }
  2522. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2523. #pragma unused(numItems)
  2524. return [self initWithValues:NULL forKeys:NULL count:0];
  2525. }
  2526. - (void)dealloc {
  2527. NSAssert(!_autocreator,
  2528. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2529. [self class], _autocreator);
  2530. [_dictionary release];
  2531. [super dealloc];
  2532. }
  2533. - (instancetype)copyWithZone:(NSZone *)zone {
  2534. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2535. }
  2536. - (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
  2537. if (self == other) {
  2538. return YES;
  2539. }
  2540. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2541. return NO;
  2542. }
  2543. return [_dictionary isEqual:other->_dictionary];
  2544. }
  2545. - (NSUInteger)hash {
  2546. return _dictionary.count;
  2547. }
  2548. - (NSString *)description {
  2549. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2550. }
  2551. - (NSUInteger)count {
  2552. return _dictionary.count;
  2553. }
  2554. - (void)enumerateKeysAndValuesUsingBlock:
  2555. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2556. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2557. NSNumber *aValue,
  2558. BOOL *stop) {
  2559. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2560. }];
  2561. }
  2562. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2563. NSUInteger count = _dictionary.count;
  2564. if (count == 0) {
  2565. return 0;
  2566. }
  2567. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2568. GPBDataType keyDataType = field.mapKeyDataType;
  2569. __block size_t result = 0;
  2570. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2571. NSNumber *aValue,
  2572. BOOL *stop) {
  2573. #pragma unused(stop)
  2574. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2575. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2576. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2577. }];
  2578. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2579. result += tagSize * count;
  2580. return result;
  2581. }
  2582. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2583. asField:(GPBFieldDescriptor *)field {
  2584. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2585. GPBDataType keyDataType = field.mapKeyDataType;
  2586. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2587. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2588. NSNumber *aValue,
  2589. BOOL *stop) {
  2590. #pragma unused(stop)
  2591. // Write the tag.
  2592. [outputStream writeInt32NoTag:tag];
  2593. // Write the size of the message.
  2594. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2595. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2596. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2597. // Write the fields.
  2598. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2599. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2600. }];
  2601. }
  2602. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2603. forGPBGenericValueKey:(GPBGenericValue *)key {
  2604. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2605. }
  2606. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2607. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2608. #pragma unused(stop)
  2609. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2610. }];
  2611. }
  2612. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2613. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2614. if (wrapped && value) {
  2615. *value = [wrapped doubleValue];
  2616. }
  2617. return (wrapped != NULL);
  2618. }
  2619. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2620. if (otherDictionary) {
  2621. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2622. if (_autocreator) {
  2623. GPBAutocreatedDictionaryModified(_autocreator, self);
  2624. }
  2625. }
  2626. }
  2627. - (void)setValue:(double)value forKey:(uint32_t)key {
  2628. [_dictionary setObject:@(value) forKey:@(key)];
  2629. if (_autocreator) {
  2630. GPBAutocreatedDictionaryModified(_autocreator, self);
  2631. }
  2632. }
  2633. - (void)removeValueForKey:(uint32_t)aKey {
  2634. [_dictionary removeObjectForKey:@(aKey)];
  2635. }
  2636. - (void)removeAll {
  2637. [_dictionary removeAllObjects];
  2638. }
  2639. @end
  2640. #pragma mark - UInt32 -> Enum
  2641. @implementation GPBUInt32EnumDictionary {
  2642. @package
  2643. NSMutableDictionary *_dictionary;
  2644. GPBEnumValidationFunc _validationFunc;
  2645. }
  2646. @synthesize validationFunc = _validationFunc;
  2647. + (instancetype)dictionary {
  2648. return [[[self alloc] initWithValidationFunction:NULL
  2649. rawValues:NULL
  2650. forKeys:NULL
  2651. count:0] autorelease];
  2652. }
  2653. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2654. return [[[self alloc] initWithValidationFunction:func
  2655. rawValues:NULL
  2656. forKeys:NULL
  2657. count:0] autorelease];
  2658. }
  2659. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2660. rawValue:(int32_t)rawValue
  2661. forKey:(uint32_t)key {
  2662. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2663. // on to get the type correct.
  2664. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2665. rawValues:&rawValue
  2666. forKeys:&key
  2667. count:1] autorelease];
  2668. }
  2669. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2670. rawValues:(const int32_t [])rawValues
  2671. forKeys:(const uint32_t [])keys
  2672. count:(NSUInteger)count {
  2673. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2674. // on to get the type correct.
  2675. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2676. rawValues:rawValues
  2677. forKeys:keys
  2678. count:count] autorelease];
  2679. }
  2680. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2681. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2682. // on to get the type correct.
  2683. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2684. }
  2685. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2686. capacity:(NSUInteger)numItems {
  2687. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2688. }
  2689. - (instancetype)init {
  2690. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2691. }
  2692. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2693. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2694. }
  2695. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2696. rawValues:(const int32_t [])rawValues
  2697. forKeys:(const uint32_t [])keys
  2698. count:(NSUInteger)count {
  2699. self = [super init];
  2700. if (self) {
  2701. _dictionary = [[NSMutableDictionary alloc] init];
  2702. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2703. if (count && rawValues && keys) {
  2704. for (NSUInteger i = 0; i < count; ++i) {
  2705. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2706. }
  2707. }
  2708. }
  2709. return self;
  2710. }
  2711. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2712. self = [self initWithValidationFunction:dictionary.validationFunc
  2713. rawValues:NULL
  2714. forKeys:NULL
  2715. count:0];
  2716. if (self) {
  2717. if (dictionary) {
  2718. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2719. }
  2720. }
  2721. return self;
  2722. }
  2723. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2724. capacity:(NSUInteger)numItems {
  2725. #pragma unused(numItems)
  2726. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2727. }
  2728. - (void)dealloc {
  2729. NSAssert(!_autocreator,
  2730. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2731. [self class], _autocreator);
  2732. [_dictionary release];
  2733. [super dealloc];
  2734. }
  2735. - (instancetype)copyWithZone:(NSZone *)zone {
  2736. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2737. }
  2738. - (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
  2739. if (self == other) {
  2740. return YES;
  2741. }
  2742. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2743. return NO;
  2744. }
  2745. return [_dictionary isEqual:other->_dictionary];
  2746. }
  2747. - (NSUInteger)hash {
  2748. return _dictionary.count;
  2749. }
  2750. - (NSString *)description {
  2751. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2752. }
  2753. - (NSUInteger)count {
  2754. return _dictionary.count;
  2755. }
  2756. - (void)enumerateKeysAndRawValuesUsingBlock:
  2757. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2758. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2759. NSNumber *aValue,
  2760. BOOL *stop) {
  2761. block([aKey unsignedIntValue], [aValue intValue], stop);
  2762. }];
  2763. }
  2764. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2765. NSUInteger count = _dictionary.count;
  2766. if (count == 0) {
  2767. return 0;
  2768. }
  2769. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2770. GPBDataType keyDataType = field.mapKeyDataType;
  2771. __block size_t result = 0;
  2772. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2773. NSNumber *aValue,
  2774. BOOL *stop) {
  2775. #pragma unused(stop)
  2776. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2777. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2778. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2779. }];
  2780. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2781. result += tagSize * count;
  2782. return result;
  2783. }
  2784. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2785. asField:(GPBFieldDescriptor *)field {
  2786. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2787. GPBDataType keyDataType = field.mapKeyDataType;
  2788. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2789. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2790. NSNumber *aValue,
  2791. BOOL *stop) {
  2792. #pragma unused(stop)
  2793. // Write the tag.
  2794. [outputStream writeInt32NoTag:tag];
  2795. // Write the size of the message.
  2796. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2797. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2798. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2799. // Write the fields.
  2800. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2801. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2802. }];
  2803. }
  2804. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2805. forKey:(GPBGenericValue *)key
  2806. keyDataType:(GPBDataType)keyDataType {
  2807. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2808. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2809. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2810. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2811. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2812. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2813. [outputStream release];
  2814. return data;
  2815. }
  2816. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2817. forGPBGenericValueKey:(GPBGenericValue *)key {
  2818. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2819. }
  2820. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2821. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2822. #pragma unused(stop)
  2823. block([NSString stringWithFormat:@"%u", key], @(value));
  2824. }];
  2825. }
  2826. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2827. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2828. if (wrapped && value) {
  2829. int32_t result = [wrapped intValue];
  2830. if (!_validationFunc(result)) {
  2831. result = kGPBUnrecognizedEnumeratorValue;
  2832. }
  2833. *value = result;
  2834. }
  2835. return (wrapped != NULL);
  2836. }
  2837. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2838. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2839. if (wrapped && rawValue) {
  2840. *rawValue = [wrapped intValue];
  2841. }
  2842. return (wrapped != NULL);
  2843. }
  2844. - (void)enumerateKeysAndValuesUsingBlock:
  2845. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2846. GPBEnumValidationFunc func = _validationFunc;
  2847. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2848. NSNumber *aValue,
  2849. BOOL *stop) {
  2850. int32_t unwrapped = [aValue intValue];
  2851. if (!func(unwrapped)) {
  2852. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2853. }
  2854. block([aKey unsignedIntValue], unwrapped, stop);
  2855. }];
  2856. }
  2857. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2858. if (otherDictionary) {
  2859. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2860. if (_autocreator) {
  2861. GPBAutocreatedDictionaryModified(_autocreator, self);
  2862. }
  2863. }
  2864. }
  2865. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2866. [_dictionary setObject:@(value) forKey:@(key)];
  2867. if (_autocreator) {
  2868. GPBAutocreatedDictionaryModified(_autocreator, self);
  2869. }
  2870. }
  2871. - (void)removeValueForKey:(uint32_t)aKey {
  2872. [_dictionary removeObjectForKey:@(aKey)];
  2873. }
  2874. - (void)removeAll {
  2875. [_dictionary removeAllObjects];
  2876. }
  2877. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2878. if (!_validationFunc(value)) {
  2879. [NSException raise:NSInvalidArgumentException
  2880. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2881. value];
  2882. }
  2883. [_dictionary setObject:@(value) forKey:@(key)];
  2884. if (_autocreator) {
  2885. GPBAutocreatedDictionaryModified(_autocreator, self);
  2886. }
  2887. }
  2888. @end
  2889. #pragma mark - UInt32 -> Object
  2890. @implementation GPBUInt32ObjectDictionary {
  2891. @package
  2892. NSMutableDictionary *_dictionary;
  2893. }
  2894. + (instancetype)dictionary {
  2895. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  2896. }
  2897. + (instancetype)dictionaryWithObject:(id)object
  2898. forKey:(uint32_t)key {
  2899. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2900. // on to get the type correct.
  2901. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  2902. forKeys:&key
  2903. count:1] autorelease];
  2904. }
  2905. + (instancetype)dictionaryWithObjects:(const id [])objects
  2906. forKeys:(const uint32_t [])keys
  2907. count:(NSUInteger)count {
  2908. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2909. // on to get the type correct.
  2910. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  2911. forKeys:keys
  2912. count:count] autorelease];
  2913. }
  2914. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2915. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2916. // on to get the type correct.
  2917. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2918. }
  2919. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2920. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2921. }
  2922. - (instancetype)init {
  2923. return [self initWithObjects:NULL forKeys:NULL count:0];
  2924. }
  2925. - (instancetype)initWithObjects:(const id [])objects
  2926. forKeys:(const uint32_t [])keys
  2927. count:(NSUInteger)count {
  2928. self = [super init];
  2929. if (self) {
  2930. _dictionary = [[NSMutableDictionary alloc] init];
  2931. if (count && objects && keys) {
  2932. for (NSUInteger i = 0; i < count; ++i) {
  2933. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  2934. }
  2935. }
  2936. }
  2937. return self;
  2938. }
  2939. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2940. self = [self initWithObjects:NULL forKeys:NULL count:0];
  2941. if (self) {
  2942. if (dictionary) {
  2943. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2944. }
  2945. }
  2946. return self;
  2947. }
  2948. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2949. #pragma unused(numItems)
  2950. return [self initWithObjects:NULL forKeys:NULL count:0];
  2951. }
  2952. - (void)dealloc {
  2953. NSAssert(!_autocreator,
  2954. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2955. [self class], _autocreator);
  2956. [_dictionary release];
  2957. [super dealloc];
  2958. }
  2959. - (instancetype)copyWithZone:(NSZone *)zone {
  2960. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2961. }
  2962. - (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
  2963. if (self == other) {
  2964. return YES;
  2965. }
  2966. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2967. return NO;
  2968. }
  2969. return [_dictionary isEqual:other->_dictionary];
  2970. }
  2971. - (NSUInteger)hash {
  2972. return _dictionary.count;
  2973. }
  2974. - (NSString *)description {
  2975. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2976. }
  2977. - (NSUInteger)count {
  2978. return _dictionary.count;
  2979. }
  2980. - (void)enumerateKeysAndObjectsUsingBlock:
  2981. (void (^)(uint32_t key, id object, BOOL *stop))block {
  2982. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2983. id aObject,
  2984. BOOL *stop) {
  2985. block([aKey unsignedIntValue], aObject, stop);
  2986. }];
  2987. }
  2988. - (BOOL)isInitialized {
  2989. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2990. if (!msg.initialized) {
  2991. return NO;
  2992. }
  2993. }
  2994. return YES;
  2995. }
  2996. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2997. GPBUInt32ObjectDictionary *newDict =
  2998. [[GPBUInt32ObjectDictionary alloc] init];
  2999. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  3000. GPBMessage *msg,
  3001. BOOL *stop) {
  3002. #pragma unused(stop)
  3003. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3004. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  3005. [copiedMsg release];
  3006. }];
  3007. return newDict;
  3008. }
  3009. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3010. NSUInteger count = _dictionary.count;
  3011. if (count == 0) {
  3012. return 0;
  3013. }
  3014. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3015. GPBDataType keyDataType = field.mapKeyDataType;
  3016. __block size_t result = 0;
  3017. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3018. id aObject,
  3019. BOOL *stop) {
  3020. #pragma unused(stop)
  3021. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3022. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3023. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3024. }];
  3025. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3026. result += tagSize * count;
  3027. return result;
  3028. }
  3029. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3030. asField:(GPBFieldDescriptor *)field {
  3031. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3032. GPBDataType keyDataType = field.mapKeyDataType;
  3033. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3034. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3035. id aObject,
  3036. BOOL *stop) {
  3037. #pragma unused(stop)
  3038. // Write the tag.
  3039. [outputStream writeInt32NoTag:tag];
  3040. // Write the size of the message.
  3041. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3042. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3043. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3044. // Write the fields.
  3045. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3046. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  3047. }];
  3048. }
  3049. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3050. forGPBGenericValueKey:(GPBGenericValue *)key {
  3051. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3052. }
  3053. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3054. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3055. #pragma unused(stop)
  3056. block([NSString stringWithFormat:@"%u", key], object);
  3057. }];
  3058. }
  3059. - (id)objectForKey:(uint32_t)key {
  3060. id result = [_dictionary objectForKey:@(key)];
  3061. return result;
  3062. }
  3063. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3064. if (otherDictionary) {
  3065. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3066. if (_autocreator) {
  3067. GPBAutocreatedDictionaryModified(_autocreator, self);
  3068. }
  3069. }
  3070. }
  3071. - (void)setObject:(id)object forKey:(uint32_t)key {
  3072. [_dictionary setObject:object forKey:@(key)];
  3073. if (_autocreator) {
  3074. GPBAutocreatedDictionaryModified(_autocreator, self);
  3075. }
  3076. }
  3077. - (void)removeObjectForKey:(uint32_t)aKey {
  3078. [_dictionary removeObjectForKey:@(aKey)];
  3079. }
  3080. - (void)removeAll {
  3081. [_dictionary removeAllObjects];
  3082. }
  3083. @end
  3084. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3085. // This block of code is generated, do not edit it directly.
  3086. #pragma mark - Int32 -> UInt32
  3087. @implementation GPBInt32UInt32Dictionary {
  3088. @package
  3089. NSMutableDictionary *_dictionary;
  3090. }
  3091. + (instancetype)dictionary {
  3092. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3093. }
  3094. + (instancetype)dictionaryWithValue:(uint32_t)value
  3095. forKey:(int32_t)key {
  3096. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3097. // on to get the type correct.
  3098. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  3099. forKeys:&key
  3100. count:1] autorelease];
  3101. }
  3102. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  3103. forKeys:(const int32_t [])keys
  3104. count:(NSUInteger)count {
  3105. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3106. // on to get the type correct.
  3107. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  3108. forKeys:keys
  3109. count:count] autorelease];
  3110. }
  3111. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3112. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3113. // on to get the type correct.
  3114. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3115. }
  3116. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3117. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3118. }
  3119. - (instancetype)init {
  3120. return [self initWithValues:NULL forKeys:NULL count:0];
  3121. }
  3122. - (instancetype)initWithValues:(const uint32_t [])values
  3123. forKeys:(const int32_t [])keys
  3124. count:(NSUInteger)count {
  3125. self = [super init];
  3126. if (self) {
  3127. _dictionary = [[NSMutableDictionary alloc] init];
  3128. if (count && values && keys) {
  3129. for (NSUInteger i = 0; i < count; ++i) {
  3130. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3131. }
  3132. }
  3133. }
  3134. return self;
  3135. }
  3136. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3137. self = [self initWithValues:NULL forKeys:NULL count:0];
  3138. if (self) {
  3139. if (dictionary) {
  3140. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3141. }
  3142. }
  3143. return self;
  3144. }
  3145. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3146. #pragma unused(numItems)
  3147. return [self initWithValues:NULL forKeys:NULL count:0];
  3148. }
  3149. - (void)dealloc {
  3150. NSAssert(!_autocreator,
  3151. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3152. [self class], _autocreator);
  3153. [_dictionary release];
  3154. [super dealloc];
  3155. }
  3156. - (instancetype)copyWithZone:(NSZone *)zone {
  3157. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3158. }
  3159. - (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
  3160. if (self == other) {
  3161. return YES;
  3162. }
  3163. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3164. return NO;
  3165. }
  3166. return [_dictionary isEqual:other->_dictionary];
  3167. }
  3168. - (NSUInteger)hash {
  3169. return _dictionary.count;
  3170. }
  3171. - (NSString *)description {
  3172. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3173. }
  3174. - (NSUInteger)count {
  3175. return _dictionary.count;
  3176. }
  3177. - (void)enumerateKeysAndValuesUsingBlock:
  3178. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3179. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3180. NSNumber *aValue,
  3181. BOOL *stop) {
  3182. block([aKey intValue], [aValue unsignedIntValue], stop);
  3183. }];
  3184. }
  3185. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3186. NSUInteger count = _dictionary.count;
  3187. if (count == 0) {
  3188. return 0;
  3189. }
  3190. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3191. GPBDataType keyDataType = field.mapKeyDataType;
  3192. __block size_t result = 0;
  3193. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3194. NSNumber *aValue,
  3195. BOOL *stop) {
  3196. #pragma unused(stop)
  3197. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3198. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3199. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3200. }];
  3201. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3202. result += tagSize * count;
  3203. return result;
  3204. }
  3205. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3206. asField:(GPBFieldDescriptor *)field {
  3207. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3208. GPBDataType keyDataType = field.mapKeyDataType;
  3209. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3210. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3211. NSNumber *aValue,
  3212. BOOL *stop) {
  3213. #pragma unused(stop)
  3214. // Write the tag.
  3215. [outputStream writeInt32NoTag:tag];
  3216. // Write the size of the message.
  3217. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3218. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3219. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3220. // Write the fields.
  3221. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3222. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3223. }];
  3224. }
  3225. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3226. forGPBGenericValueKey:(GPBGenericValue *)key {
  3227. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3228. }
  3229. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3230. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3231. #pragma unused(stop)
  3232. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3233. }];
  3234. }
  3235. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3236. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3237. if (wrapped && value) {
  3238. *value = [wrapped unsignedIntValue];
  3239. }
  3240. return (wrapped != NULL);
  3241. }
  3242. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3243. if (otherDictionary) {
  3244. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3245. if (_autocreator) {
  3246. GPBAutocreatedDictionaryModified(_autocreator, self);
  3247. }
  3248. }
  3249. }
  3250. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3251. [_dictionary setObject:@(value) forKey:@(key)];
  3252. if (_autocreator) {
  3253. GPBAutocreatedDictionaryModified(_autocreator, self);
  3254. }
  3255. }
  3256. - (void)removeValueForKey:(int32_t)aKey {
  3257. [_dictionary removeObjectForKey:@(aKey)];
  3258. }
  3259. - (void)removeAll {
  3260. [_dictionary removeAllObjects];
  3261. }
  3262. @end
  3263. #pragma mark - Int32 -> Int32
  3264. @implementation GPBInt32Int32Dictionary {
  3265. @package
  3266. NSMutableDictionary *_dictionary;
  3267. }
  3268. + (instancetype)dictionary {
  3269. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3270. }
  3271. + (instancetype)dictionaryWithValue:(int32_t)value
  3272. forKey:(int32_t)key {
  3273. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3274. // on to get the type correct.
  3275. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3276. forKeys:&key
  3277. count:1] autorelease];
  3278. }
  3279. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3280. forKeys:(const int32_t [])keys
  3281. count:(NSUInteger)count {
  3282. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3283. // on to get the type correct.
  3284. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3285. forKeys:keys
  3286. count:count] autorelease];
  3287. }
  3288. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3289. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3290. // on to get the type correct.
  3291. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3292. }
  3293. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3294. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3295. }
  3296. - (instancetype)init {
  3297. return [self initWithValues:NULL forKeys:NULL count:0];
  3298. }
  3299. - (instancetype)initWithValues:(const int32_t [])values
  3300. forKeys:(const int32_t [])keys
  3301. count:(NSUInteger)count {
  3302. self = [super init];
  3303. if (self) {
  3304. _dictionary = [[NSMutableDictionary alloc] init];
  3305. if (count && values && keys) {
  3306. for (NSUInteger i = 0; i < count; ++i) {
  3307. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3308. }
  3309. }
  3310. }
  3311. return self;
  3312. }
  3313. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3314. self = [self initWithValues:NULL forKeys:NULL count:0];
  3315. if (self) {
  3316. if (dictionary) {
  3317. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3318. }
  3319. }
  3320. return self;
  3321. }
  3322. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3323. #pragma unused(numItems)
  3324. return [self initWithValues:NULL forKeys:NULL count:0];
  3325. }
  3326. - (void)dealloc {
  3327. NSAssert(!_autocreator,
  3328. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3329. [self class], _autocreator);
  3330. [_dictionary release];
  3331. [super dealloc];
  3332. }
  3333. - (instancetype)copyWithZone:(NSZone *)zone {
  3334. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3335. }
  3336. - (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
  3337. if (self == other) {
  3338. return YES;
  3339. }
  3340. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3341. return NO;
  3342. }
  3343. return [_dictionary isEqual:other->_dictionary];
  3344. }
  3345. - (NSUInteger)hash {
  3346. return _dictionary.count;
  3347. }
  3348. - (NSString *)description {
  3349. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3350. }
  3351. - (NSUInteger)count {
  3352. return _dictionary.count;
  3353. }
  3354. - (void)enumerateKeysAndValuesUsingBlock:
  3355. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3356. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3357. NSNumber *aValue,
  3358. BOOL *stop) {
  3359. block([aKey intValue], [aValue intValue], stop);
  3360. }];
  3361. }
  3362. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3363. NSUInteger count = _dictionary.count;
  3364. if (count == 0) {
  3365. return 0;
  3366. }
  3367. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3368. GPBDataType keyDataType = field.mapKeyDataType;
  3369. __block size_t result = 0;
  3370. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3371. NSNumber *aValue,
  3372. BOOL *stop) {
  3373. #pragma unused(stop)
  3374. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3375. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3376. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3377. }];
  3378. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3379. result += tagSize * count;
  3380. return result;
  3381. }
  3382. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3383. asField:(GPBFieldDescriptor *)field {
  3384. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3385. GPBDataType keyDataType = field.mapKeyDataType;
  3386. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3387. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3388. NSNumber *aValue,
  3389. BOOL *stop) {
  3390. #pragma unused(stop)
  3391. // Write the tag.
  3392. [outputStream writeInt32NoTag:tag];
  3393. // Write the size of the message.
  3394. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3395. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3396. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3397. // Write the fields.
  3398. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3399. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3400. }];
  3401. }
  3402. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3403. forGPBGenericValueKey:(GPBGenericValue *)key {
  3404. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3405. }
  3406. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3407. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3408. #pragma unused(stop)
  3409. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3410. }];
  3411. }
  3412. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3413. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3414. if (wrapped && value) {
  3415. *value = [wrapped intValue];
  3416. }
  3417. return (wrapped != NULL);
  3418. }
  3419. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3420. if (otherDictionary) {
  3421. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3422. if (_autocreator) {
  3423. GPBAutocreatedDictionaryModified(_autocreator, self);
  3424. }
  3425. }
  3426. }
  3427. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3428. [_dictionary setObject:@(value) forKey:@(key)];
  3429. if (_autocreator) {
  3430. GPBAutocreatedDictionaryModified(_autocreator, self);
  3431. }
  3432. }
  3433. - (void)removeValueForKey:(int32_t)aKey {
  3434. [_dictionary removeObjectForKey:@(aKey)];
  3435. }
  3436. - (void)removeAll {
  3437. [_dictionary removeAllObjects];
  3438. }
  3439. @end
  3440. #pragma mark - Int32 -> UInt64
  3441. @implementation GPBInt32UInt64Dictionary {
  3442. @package
  3443. NSMutableDictionary *_dictionary;
  3444. }
  3445. + (instancetype)dictionary {
  3446. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3447. }
  3448. + (instancetype)dictionaryWithValue:(uint64_t)value
  3449. forKey:(int32_t)key {
  3450. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3451. // on to get the type correct.
  3452. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3453. forKeys:&key
  3454. count:1] autorelease];
  3455. }
  3456. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3457. forKeys:(const int32_t [])keys
  3458. count:(NSUInteger)count {
  3459. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3460. // on to get the type correct.
  3461. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3462. forKeys:keys
  3463. count:count] autorelease];
  3464. }
  3465. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3466. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3467. // on to get the type correct.
  3468. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3469. }
  3470. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3471. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3472. }
  3473. - (instancetype)init {
  3474. return [self initWithValues:NULL forKeys:NULL count:0];
  3475. }
  3476. - (instancetype)initWithValues:(const uint64_t [])values
  3477. forKeys:(const int32_t [])keys
  3478. count:(NSUInteger)count {
  3479. self = [super init];
  3480. if (self) {
  3481. _dictionary = [[NSMutableDictionary alloc] init];
  3482. if (count && values && keys) {
  3483. for (NSUInteger i = 0; i < count; ++i) {
  3484. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3485. }
  3486. }
  3487. }
  3488. return self;
  3489. }
  3490. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3491. self = [self initWithValues:NULL forKeys:NULL count:0];
  3492. if (self) {
  3493. if (dictionary) {
  3494. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3495. }
  3496. }
  3497. return self;
  3498. }
  3499. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3500. #pragma unused(numItems)
  3501. return [self initWithValues:NULL forKeys:NULL count:0];
  3502. }
  3503. - (void)dealloc {
  3504. NSAssert(!_autocreator,
  3505. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3506. [self class], _autocreator);
  3507. [_dictionary release];
  3508. [super dealloc];
  3509. }
  3510. - (instancetype)copyWithZone:(NSZone *)zone {
  3511. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3512. }
  3513. - (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
  3514. if (self == other) {
  3515. return YES;
  3516. }
  3517. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3518. return NO;
  3519. }
  3520. return [_dictionary isEqual:other->_dictionary];
  3521. }
  3522. - (NSUInteger)hash {
  3523. return _dictionary.count;
  3524. }
  3525. - (NSString *)description {
  3526. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3527. }
  3528. - (NSUInteger)count {
  3529. return _dictionary.count;
  3530. }
  3531. - (void)enumerateKeysAndValuesUsingBlock:
  3532. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3533. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3534. NSNumber *aValue,
  3535. BOOL *stop) {
  3536. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3537. }];
  3538. }
  3539. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3540. NSUInteger count = _dictionary.count;
  3541. if (count == 0) {
  3542. return 0;
  3543. }
  3544. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3545. GPBDataType keyDataType = field.mapKeyDataType;
  3546. __block size_t result = 0;
  3547. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3548. NSNumber *aValue,
  3549. BOOL *stop) {
  3550. #pragma unused(stop)
  3551. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3552. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3553. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3554. }];
  3555. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3556. result += tagSize * count;
  3557. return result;
  3558. }
  3559. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3560. asField:(GPBFieldDescriptor *)field {
  3561. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3562. GPBDataType keyDataType = field.mapKeyDataType;
  3563. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3564. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3565. NSNumber *aValue,
  3566. BOOL *stop) {
  3567. #pragma unused(stop)
  3568. // Write the tag.
  3569. [outputStream writeInt32NoTag:tag];
  3570. // Write the size of the message.
  3571. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3572. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3573. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3574. // Write the fields.
  3575. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3576. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3577. }];
  3578. }
  3579. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3580. forGPBGenericValueKey:(GPBGenericValue *)key {
  3581. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3582. }
  3583. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3584. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3585. #pragma unused(stop)
  3586. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3587. }];
  3588. }
  3589. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3590. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3591. if (wrapped && value) {
  3592. *value = [wrapped unsignedLongLongValue];
  3593. }
  3594. return (wrapped != NULL);
  3595. }
  3596. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3597. if (otherDictionary) {
  3598. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3599. if (_autocreator) {
  3600. GPBAutocreatedDictionaryModified(_autocreator, self);
  3601. }
  3602. }
  3603. }
  3604. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3605. [_dictionary setObject:@(value) forKey:@(key)];
  3606. if (_autocreator) {
  3607. GPBAutocreatedDictionaryModified(_autocreator, self);
  3608. }
  3609. }
  3610. - (void)removeValueForKey:(int32_t)aKey {
  3611. [_dictionary removeObjectForKey:@(aKey)];
  3612. }
  3613. - (void)removeAll {
  3614. [_dictionary removeAllObjects];
  3615. }
  3616. @end
  3617. #pragma mark - Int32 -> Int64
  3618. @implementation GPBInt32Int64Dictionary {
  3619. @package
  3620. NSMutableDictionary *_dictionary;
  3621. }
  3622. + (instancetype)dictionary {
  3623. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3624. }
  3625. + (instancetype)dictionaryWithValue:(int64_t)value
  3626. forKey:(int32_t)key {
  3627. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3628. // on to get the type correct.
  3629. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3630. forKeys:&key
  3631. count:1] autorelease];
  3632. }
  3633. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3634. forKeys:(const int32_t [])keys
  3635. count:(NSUInteger)count {
  3636. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3637. // on to get the type correct.
  3638. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3639. forKeys:keys
  3640. count:count] autorelease];
  3641. }
  3642. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3643. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3644. // on to get the type correct.
  3645. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3646. }
  3647. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3648. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3649. }
  3650. - (instancetype)init {
  3651. return [self initWithValues:NULL forKeys:NULL count:0];
  3652. }
  3653. - (instancetype)initWithValues:(const int64_t [])values
  3654. forKeys:(const int32_t [])keys
  3655. count:(NSUInteger)count {
  3656. self = [super init];
  3657. if (self) {
  3658. _dictionary = [[NSMutableDictionary alloc] init];
  3659. if (count && values && keys) {
  3660. for (NSUInteger i = 0; i < count; ++i) {
  3661. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3662. }
  3663. }
  3664. }
  3665. return self;
  3666. }
  3667. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3668. self = [self initWithValues:NULL forKeys:NULL count:0];
  3669. if (self) {
  3670. if (dictionary) {
  3671. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3672. }
  3673. }
  3674. return self;
  3675. }
  3676. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3677. #pragma unused(numItems)
  3678. return [self initWithValues:NULL forKeys:NULL count:0];
  3679. }
  3680. - (void)dealloc {
  3681. NSAssert(!_autocreator,
  3682. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3683. [self class], _autocreator);
  3684. [_dictionary release];
  3685. [super dealloc];
  3686. }
  3687. - (instancetype)copyWithZone:(NSZone *)zone {
  3688. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3689. }
  3690. - (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
  3691. if (self == other) {
  3692. return YES;
  3693. }
  3694. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3695. return NO;
  3696. }
  3697. return [_dictionary isEqual:other->_dictionary];
  3698. }
  3699. - (NSUInteger)hash {
  3700. return _dictionary.count;
  3701. }
  3702. - (NSString *)description {
  3703. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3704. }
  3705. - (NSUInteger)count {
  3706. return _dictionary.count;
  3707. }
  3708. - (void)enumerateKeysAndValuesUsingBlock:
  3709. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3710. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3711. NSNumber *aValue,
  3712. BOOL *stop) {
  3713. block([aKey intValue], [aValue longLongValue], stop);
  3714. }];
  3715. }
  3716. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3717. NSUInteger count = _dictionary.count;
  3718. if (count == 0) {
  3719. return 0;
  3720. }
  3721. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3722. GPBDataType keyDataType = field.mapKeyDataType;
  3723. __block size_t result = 0;
  3724. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3725. NSNumber *aValue,
  3726. BOOL *stop) {
  3727. #pragma unused(stop)
  3728. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3729. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3730. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3731. }];
  3732. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3733. result += tagSize * count;
  3734. return result;
  3735. }
  3736. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3737. asField:(GPBFieldDescriptor *)field {
  3738. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3739. GPBDataType keyDataType = field.mapKeyDataType;
  3740. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3741. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3742. NSNumber *aValue,
  3743. BOOL *stop) {
  3744. #pragma unused(stop)
  3745. // Write the tag.
  3746. [outputStream writeInt32NoTag:tag];
  3747. // Write the size of the message.
  3748. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3749. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3750. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3751. // Write the fields.
  3752. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3753. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3754. }];
  3755. }
  3756. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3757. forGPBGenericValueKey:(GPBGenericValue *)key {
  3758. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3759. }
  3760. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3761. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3762. #pragma unused(stop)
  3763. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3764. }];
  3765. }
  3766. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3767. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3768. if (wrapped && value) {
  3769. *value = [wrapped longLongValue];
  3770. }
  3771. return (wrapped != NULL);
  3772. }
  3773. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3774. if (otherDictionary) {
  3775. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3776. if (_autocreator) {
  3777. GPBAutocreatedDictionaryModified(_autocreator, self);
  3778. }
  3779. }
  3780. }
  3781. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3782. [_dictionary setObject:@(value) forKey:@(key)];
  3783. if (_autocreator) {
  3784. GPBAutocreatedDictionaryModified(_autocreator, self);
  3785. }
  3786. }
  3787. - (void)removeValueForKey:(int32_t)aKey {
  3788. [_dictionary removeObjectForKey:@(aKey)];
  3789. }
  3790. - (void)removeAll {
  3791. [_dictionary removeAllObjects];
  3792. }
  3793. @end
  3794. #pragma mark - Int32 -> Bool
  3795. @implementation GPBInt32BoolDictionary {
  3796. @package
  3797. NSMutableDictionary *_dictionary;
  3798. }
  3799. + (instancetype)dictionary {
  3800. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3801. }
  3802. + (instancetype)dictionaryWithValue:(BOOL)value
  3803. forKey:(int32_t)key {
  3804. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3805. // on to get the type correct.
  3806. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3807. forKeys:&key
  3808. count:1] autorelease];
  3809. }
  3810. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3811. forKeys:(const int32_t [])keys
  3812. count:(NSUInteger)count {
  3813. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3814. // on to get the type correct.
  3815. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3816. forKeys:keys
  3817. count:count] autorelease];
  3818. }
  3819. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3820. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3821. // on to get the type correct.
  3822. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3823. }
  3824. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3825. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3826. }
  3827. - (instancetype)init {
  3828. return [self initWithValues:NULL forKeys:NULL count:0];
  3829. }
  3830. - (instancetype)initWithValues:(const BOOL [])values
  3831. forKeys:(const int32_t [])keys
  3832. count:(NSUInteger)count {
  3833. self = [super init];
  3834. if (self) {
  3835. _dictionary = [[NSMutableDictionary alloc] init];
  3836. if (count && values && keys) {
  3837. for (NSUInteger i = 0; i < count; ++i) {
  3838. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3839. }
  3840. }
  3841. }
  3842. return self;
  3843. }
  3844. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3845. self = [self initWithValues:NULL forKeys:NULL count:0];
  3846. if (self) {
  3847. if (dictionary) {
  3848. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3849. }
  3850. }
  3851. return self;
  3852. }
  3853. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3854. #pragma unused(numItems)
  3855. return [self initWithValues:NULL forKeys:NULL count:0];
  3856. }
  3857. - (void)dealloc {
  3858. NSAssert(!_autocreator,
  3859. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3860. [self class], _autocreator);
  3861. [_dictionary release];
  3862. [super dealloc];
  3863. }
  3864. - (instancetype)copyWithZone:(NSZone *)zone {
  3865. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3866. }
  3867. - (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
  3868. if (self == other) {
  3869. return YES;
  3870. }
  3871. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3872. return NO;
  3873. }
  3874. return [_dictionary isEqual:other->_dictionary];
  3875. }
  3876. - (NSUInteger)hash {
  3877. return _dictionary.count;
  3878. }
  3879. - (NSString *)description {
  3880. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3881. }
  3882. - (NSUInteger)count {
  3883. return _dictionary.count;
  3884. }
  3885. - (void)enumerateKeysAndValuesUsingBlock:
  3886. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3887. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3888. NSNumber *aValue,
  3889. BOOL *stop) {
  3890. block([aKey intValue], [aValue boolValue], stop);
  3891. }];
  3892. }
  3893. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3894. NSUInteger count = _dictionary.count;
  3895. if (count == 0) {
  3896. return 0;
  3897. }
  3898. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3899. GPBDataType keyDataType = field.mapKeyDataType;
  3900. __block size_t result = 0;
  3901. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3902. NSNumber *aValue,
  3903. BOOL *stop) {
  3904. #pragma unused(stop)
  3905. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3906. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3907. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3908. }];
  3909. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3910. result += tagSize * count;
  3911. return result;
  3912. }
  3913. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3914. asField:(GPBFieldDescriptor *)field {
  3915. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3916. GPBDataType keyDataType = field.mapKeyDataType;
  3917. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3918. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3919. NSNumber *aValue,
  3920. BOOL *stop) {
  3921. #pragma unused(stop)
  3922. // Write the tag.
  3923. [outputStream writeInt32NoTag:tag];
  3924. // Write the size of the message.
  3925. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3926. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3927. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3928. // Write the fields.
  3929. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3930. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  3931. }];
  3932. }
  3933. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3934. forGPBGenericValueKey:(GPBGenericValue *)key {
  3935. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3936. }
  3937. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3938. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3939. #pragma unused(stop)
  3940. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3941. }];
  3942. }
  3943. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  3944. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3945. if (wrapped && value) {
  3946. *value = [wrapped boolValue];
  3947. }
  3948. return (wrapped != NULL);
  3949. }
  3950. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3951. if (otherDictionary) {
  3952. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3953. if (_autocreator) {
  3954. GPBAutocreatedDictionaryModified(_autocreator, self);
  3955. }
  3956. }
  3957. }
  3958. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  3959. [_dictionary setObject:@(value) forKey:@(key)];
  3960. if (_autocreator) {
  3961. GPBAutocreatedDictionaryModified(_autocreator, self);
  3962. }
  3963. }
  3964. - (void)removeValueForKey:(int32_t)aKey {
  3965. [_dictionary removeObjectForKey:@(aKey)];
  3966. }
  3967. - (void)removeAll {
  3968. [_dictionary removeAllObjects];
  3969. }
  3970. @end
  3971. #pragma mark - Int32 -> Float
  3972. @implementation GPBInt32FloatDictionary {
  3973. @package
  3974. NSMutableDictionary *_dictionary;
  3975. }
  3976. + (instancetype)dictionary {
  3977. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3978. }
  3979. + (instancetype)dictionaryWithValue:(float)value
  3980. forKey:(int32_t)key {
  3981. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3982. // on to get the type correct.
  3983. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  3984. forKeys:&key
  3985. count:1] autorelease];
  3986. }
  3987. + (instancetype)dictionaryWithValues:(const float [])values
  3988. forKeys:(const int32_t [])keys
  3989. count:(NSUInteger)count {
  3990. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3991. // on to get the type correct.
  3992. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  3993. forKeys:keys
  3994. count:count] autorelease];
  3995. }
  3996. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3997. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3998. // on to get the type correct.
  3999. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4000. }
  4001. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4002. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4003. }
  4004. - (instancetype)init {
  4005. return [self initWithValues:NULL forKeys:NULL count:0];
  4006. }
  4007. - (instancetype)initWithValues:(const float [])values
  4008. forKeys:(const int32_t [])keys
  4009. count:(NSUInteger)count {
  4010. self = [super init];
  4011. if (self) {
  4012. _dictionary = [[NSMutableDictionary alloc] init];
  4013. if (count && values && keys) {
  4014. for (NSUInteger i = 0; i < count; ++i) {
  4015. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4016. }
  4017. }
  4018. }
  4019. return self;
  4020. }
  4021. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4022. self = [self initWithValues:NULL forKeys:NULL count:0];
  4023. if (self) {
  4024. if (dictionary) {
  4025. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4026. }
  4027. }
  4028. return self;
  4029. }
  4030. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4031. #pragma unused(numItems)
  4032. return [self initWithValues:NULL forKeys:NULL count:0];
  4033. }
  4034. - (void)dealloc {
  4035. NSAssert(!_autocreator,
  4036. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4037. [self class], _autocreator);
  4038. [_dictionary release];
  4039. [super dealloc];
  4040. }
  4041. - (instancetype)copyWithZone:(NSZone *)zone {
  4042. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4043. }
  4044. - (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
  4045. if (self == other) {
  4046. return YES;
  4047. }
  4048. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4049. return NO;
  4050. }
  4051. return [_dictionary isEqual:other->_dictionary];
  4052. }
  4053. - (NSUInteger)hash {
  4054. return _dictionary.count;
  4055. }
  4056. - (NSString *)description {
  4057. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4058. }
  4059. - (NSUInteger)count {
  4060. return _dictionary.count;
  4061. }
  4062. - (void)enumerateKeysAndValuesUsingBlock:
  4063. (void (^)(int32_t key, float value, BOOL *stop))block {
  4064. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4065. NSNumber *aValue,
  4066. BOOL *stop) {
  4067. block([aKey intValue], [aValue floatValue], stop);
  4068. }];
  4069. }
  4070. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4071. NSUInteger count = _dictionary.count;
  4072. if (count == 0) {
  4073. return 0;
  4074. }
  4075. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4076. GPBDataType keyDataType = field.mapKeyDataType;
  4077. __block size_t result = 0;
  4078. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4079. NSNumber *aValue,
  4080. BOOL *stop) {
  4081. #pragma unused(stop)
  4082. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4083. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4084. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4085. }];
  4086. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4087. result += tagSize * count;
  4088. return result;
  4089. }
  4090. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4091. asField:(GPBFieldDescriptor *)field {
  4092. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4093. GPBDataType keyDataType = field.mapKeyDataType;
  4094. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4095. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4096. NSNumber *aValue,
  4097. BOOL *stop) {
  4098. #pragma unused(stop)
  4099. // Write the tag.
  4100. [outputStream writeInt32NoTag:tag];
  4101. // Write the size of the message.
  4102. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4103. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4104. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4105. // Write the fields.
  4106. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4107. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4108. }];
  4109. }
  4110. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4111. forGPBGenericValueKey:(GPBGenericValue *)key {
  4112. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4113. }
  4114. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4115. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4116. #pragma unused(stop)
  4117. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4118. }];
  4119. }
  4120. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  4121. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4122. if (wrapped && value) {
  4123. *value = [wrapped floatValue];
  4124. }
  4125. return (wrapped != NULL);
  4126. }
  4127. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4128. if (otherDictionary) {
  4129. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4130. if (_autocreator) {
  4131. GPBAutocreatedDictionaryModified(_autocreator, self);
  4132. }
  4133. }
  4134. }
  4135. - (void)setValue:(float)value forKey:(int32_t)key {
  4136. [_dictionary setObject:@(value) forKey:@(key)];
  4137. if (_autocreator) {
  4138. GPBAutocreatedDictionaryModified(_autocreator, self);
  4139. }
  4140. }
  4141. - (void)removeValueForKey:(int32_t)aKey {
  4142. [_dictionary removeObjectForKey:@(aKey)];
  4143. }
  4144. - (void)removeAll {
  4145. [_dictionary removeAllObjects];
  4146. }
  4147. @end
  4148. #pragma mark - Int32 -> Double
  4149. @implementation GPBInt32DoubleDictionary {
  4150. @package
  4151. NSMutableDictionary *_dictionary;
  4152. }
  4153. + (instancetype)dictionary {
  4154. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4155. }
  4156. + (instancetype)dictionaryWithValue:(double)value
  4157. forKey:(int32_t)key {
  4158. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4159. // on to get the type correct.
  4160. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  4161. forKeys:&key
  4162. count:1] autorelease];
  4163. }
  4164. + (instancetype)dictionaryWithValues:(const double [])values
  4165. forKeys:(const int32_t [])keys
  4166. count:(NSUInteger)count {
  4167. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4168. // on to get the type correct.
  4169. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  4170. forKeys:keys
  4171. count:count] autorelease];
  4172. }
  4173. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4174. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4175. // on to get the type correct.
  4176. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4177. }
  4178. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4179. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4180. }
  4181. - (instancetype)init {
  4182. return [self initWithValues:NULL forKeys:NULL count:0];
  4183. }
  4184. - (instancetype)initWithValues:(const double [])values
  4185. forKeys:(const int32_t [])keys
  4186. count:(NSUInteger)count {
  4187. self = [super init];
  4188. if (self) {
  4189. _dictionary = [[NSMutableDictionary alloc] init];
  4190. if (count && values && keys) {
  4191. for (NSUInteger i = 0; i < count; ++i) {
  4192. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4193. }
  4194. }
  4195. }
  4196. return self;
  4197. }
  4198. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4199. self = [self initWithValues:NULL forKeys:NULL count:0];
  4200. if (self) {
  4201. if (dictionary) {
  4202. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4203. }
  4204. }
  4205. return self;
  4206. }
  4207. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4208. #pragma unused(numItems)
  4209. return [self initWithValues:NULL forKeys:NULL count:0];
  4210. }
  4211. - (void)dealloc {
  4212. NSAssert(!_autocreator,
  4213. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4214. [self class], _autocreator);
  4215. [_dictionary release];
  4216. [super dealloc];
  4217. }
  4218. - (instancetype)copyWithZone:(NSZone *)zone {
  4219. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4220. }
  4221. - (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
  4222. if (self == other) {
  4223. return YES;
  4224. }
  4225. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4226. return NO;
  4227. }
  4228. return [_dictionary isEqual:other->_dictionary];
  4229. }
  4230. - (NSUInteger)hash {
  4231. return _dictionary.count;
  4232. }
  4233. - (NSString *)description {
  4234. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4235. }
  4236. - (NSUInteger)count {
  4237. return _dictionary.count;
  4238. }
  4239. - (void)enumerateKeysAndValuesUsingBlock:
  4240. (void (^)(int32_t key, double value, BOOL *stop))block {
  4241. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4242. NSNumber *aValue,
  4243. BOOL *stop) {
  4244. block([aKey intValue], [aValue doubleValue], stop);
  4245. }];
  4246. }
  4247. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4248. NSUInteger count = _dictionary.count;
  4249. if (count == 0) {
  4250. return 0;
  4251. }
  4252. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4253. GPBDataType keyDataType = field.mapKeyDataType;
  4254. __block size_t result = 0;
  4255. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4256. NSNumber *aValue,
  4257. BOOL *stop) {
  4258. #pragma unused(stop)
  4259. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4260. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4261. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4262. }];
  4263. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4264. result += tagSize * count;
  4265. return result;
  4266. }
  4267. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4268. asField:(GPBFieldDescriptor *)field {
  4269. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4270. GPBDataType keyDataType = field.mapKeyDataType;
  4271. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4272. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4273. NSNumber *aValue,
  4274. BOOL *stop) {
  4275. #pragma unused(stop)
  4276. // Write the tag.
  4277. [outputStream writeInt32NoTag:tag];
  4278. // Write the size of the message.
  4279. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4280. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4281. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4282. // Write the fields.
  4283. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4284. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4285. }];
  4286. }
  4287. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4288. forGPBGenericValueKey:(GPBGenericValue *)key {
  4289. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4290. }
  4291. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4292. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4293. #pragma unused(stop)
  4294. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4295. }];
  4296. }
  4297. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4298. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4299. if (wrapped && value) {
  4300. *value = [wrapped doubleValue];
  4301. }
  4302. return (wrapped != NULL);
  4303. }
  4304. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4305. if (otherDictionary) {
  4306. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4307. if (_autocreator) {
  4308. GPBAutocreatedDictionaryModified(_autocreator, self);
  4309. }
  4310. }
  4311. }
  4312. - (void)setValue:(double)value forKey:(int32_t)key {
  4313. [_dictionary setObject:@(value) forKey:@(key)];
  4314. if (_autocreator) {
  4315. GPBAutocreatedDictionaryModified(_autocreator, self);
  4316. }
  4317. }
  4318. - (void)removeValueForKey:(int32_t)aKey {
  4319. [_dictionary removeObjectForKey:@(aKey)];
  4320. }
  4321. - (void)removeAll {
  4322. [_dictionary removeAllObjects];
  4323. }
  4324. @end
  4325. #pragma mark - Int32 -> Enum
  4326. @implementation GPBInt32EnumDictionary {
  4327. @package
  4328. NSMutableDictionary *_dictionary;
  4329. GPBEnumValidationFunc _validationFunc;
  4330. }
  4331. @synthesize validationFunc = _validationFunc;
  4332. + (instancetype)dictionary {
  4333. return [[[self alloc] initWithValidationFunction:NULL
  4334. rawValues:NULL
  4335. forKeys:NULL
  4336. count:0] autorelease];
  4337. }
  4338. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4339. return [[[self alloc] initWithValidationFunction:func
  4340. rawValues:NULL
  4341. forKeys:NULL
  4342. count:0] autorelease];
  4343. }
  4344. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4345. rawValue:(int32_t)rawValue
  4346. forKey:(int32_t)key {
  4347. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4348. // on to get the type correct.
  4349. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4350. rawValues:&rawValue
  4351. forKeys:&key
  4352. count:1] autorelease];
  4353. }
  4354. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4355. rawValues:(const int32_t [])rawValues
  4356. forKeys:(const int32_t [])keys
  4357. count:(NSUInteger)count {
  4358. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4359. // on to get the type correct.
  4360. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4361. rawValues:rawValues
  4362. forKeys:keys
  4363. count:count] autorelease];
  4364. }
  4365. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4366. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4367. // on to get the type correct.
  4368. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4369. }
  4370. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4371. capacity:(NSUInteger)numItems {
  4372. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4373. }
  4374. - (instancetype)init {
  4375. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4376. }
  4377. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4378. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4379. }
  4380. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4381. rawValues:(const int32_t [])rawValues
  4382. forKeys:(const int32_t [])keys
  4383. count:(NSUInteger)count {
  4384. self = [super init];
  4385. if (self) {
  4386. _dictionary = [[NSMutableDictionary alloc] init];
  4387. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4388. if (count && rawValues && keys) {
  4389. for (NSUInteger i = 0; i < count; ++i) {
  4390. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4391. }
  4392. }
  4393. }
  4394. return self;
  4395. }
  4396. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4397. self = [self initWithValidationFunction:dictionary.validationFunc
  4398. rawValues:NULL
  4399. forKeys:NULL
  4400. count:0];
  4401. if (self) {
  4402. if (dictionary) {
  4403. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4404. }
  4405. }
  4406. return self;
  4407. }
  4408. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4409. capacity:(NSUInteger)numItems {
  4410. #pragma unused(numItems)
  4411. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4412. }
  4413. - (void)dealloc {
  4414. NSAssert(!_autocreator,
  4415. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4416. [self class], _autocreator);
  4417. [_dictionary release];
  4418. [super dealloc];
  4419. }
  4420. - (instancetype)copyWithZone:(NSZone *)zone {
  4421. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4422. }
  4423. - (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
  4424. if (self == other) {
  4425. return YES;
  4426. }
  4427. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4428. return NO;
  4429. }
  4430. return [_dictionary isEqual:other->_dictionary];
  4431. }
  4432. - (NSUInteger)hash {
  4433. return _dictionary.count;
  4434. }
  4435. - (NSString *)description {
  4436. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4437. }
  4438. - (NSUInteger)count {
  4439. return _dictionary.count;
  4440. }
  4441. - (void)enumerateKeysAndRawValuesUsingBlock:
  4442. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4443. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4444. NSNumber *aValue,
  4445. BOOL *stop) {
  4446. block([aKey intValue], [aValue intValue], stop);
  4447. }];
  4448. }
  4449. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4450. NSUInteger count = _dictionary.count;
  4451. if (count == 0) {
  4452. return 0;
  4453. }
  4454. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4455. GPBDataType keyDataType = field.mapKeyDataType;
  4456. __block size_t result = 0;
  4457. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4458. NSNumber *aValue,
  4459. BOOL *stop) {
  4460. #pragma unused(stop)
  4461. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4462. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4463. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4464. }];
  4465. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4466. result += tagSize * count;
  4467. return result;
  4468. }
  4469. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4470. asField:(GPBFieldDescriptor *)field {
  4471. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4472. GPBDataType keyDataType = field.mapKeyDataType;
  4473. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4474. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4475. NSNumber *aValue,
  4476. BOOL *stop) {
  4477. #pragma unused(stop)
  4478. // Write the tag.
  4479. [outputStream writeInt32NoTag:tag];
  4480. // Write the size of the message.
  4481. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4482. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4483. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4484. // Write the fields.
  4485. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4486. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4487. }];
  4488. }
  4489. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4490. forKey:(GPBGenericValue *)key
  4491. keyDataType:(GPBDataType)keyDataType {
  4492. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4493. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4494. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4495. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4496. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4497. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4498. [outputStream release];
  4499. return data;
  4500. }
  4501. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4502. forGPBGenericValueKey:(GPBGenericValue *)key {
  4503. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4504. }
  4505. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4506. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4507. #pragma unused(stop)
  4508. block([NSString stringWithFormat:@"%d", key], @(value));
  4509. }];
  4510. }
  4511. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4512. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4513. if (wrapped && value) {
  4514. int32_t result = [wrapped intValue];
  4515. if (!_validationFunc(result)) {
  4516. result = kGPBUnrecognizedEnumeratorValue;
  4517. }
  4518. *value = result;
  4519. }
  4520. return (wrapped != NULL);
  4521. }
  4522. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4523. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4524. if (wrapped && rawValue) {
  4525. *rawValue = [wrapped intValue];
  4526. }
  4527. return (wrapped != NULL);
  4528. }
  4529. - (void)enumerateKeysAndValuesUsingBlock:
  4530. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4531. GPBEnumValidationFunc func = _validationFunc;
  4532. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4533. NSNumber *aValue,
  4534. BOOL *stop) {
  4535. int32_t unwrapped = [aValue intValue];
  4536. if (!func(unwrapped)) {
  4537. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4538. }
  4539. block([aKey intValue], unwrapped, stop);
  4540. }];
  4541. }
  4542. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4543. if (otherDictionary) {
  4544. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4545. if (_autocreator) {
  4546. GPBAutocreatedDictionaryModified(_autocreator, self);
  4547. }
  4548. }
  4549. }
  4550. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4551. [_dictionary setObject:@(value) forKey:@(key)];
  4552. if (_autocreator) {
  4553. GPBAutocreatedDictionaryModified(_autocreator, self);
  4554. }
  4555. }
  4556. - (void)removeValueForKey:(int32_t)aKey {
  4557. [_dictionary removeObjectForKey:@(aKey)];
  4558. }
  4559. - (void)removeAll {
  4560. [_dictionary removeAllObjects];
  4561. }
  4562. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4563. if (!_validationFunc(value)) {
  4564. [NSException raise:NSInvalidArgumentException
  4565. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4566. value];
  4567. }
  4568. [_dictionary setObject:@(value) forKey:@(key)];
  4569. if (_autocreator) {
  4570. GPBAutocreatedDictionaryModified(_autocreator, self);
  4571. }
  4572. }
  4573. @end
  4574. #pragma mark - Int32 -> Object
  4575. @implementation GPBInt32ObjectDictionary {
  4576. @package
  4577. NSMutableDictionary *_dictionary;
  4578. }
  4579. + (instancetype)dictionary {
  4580. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4581. }
  4582. + (instancetype)dictionaryWithObject:(id)object
  4583. forKey:(int32_t)key {
  4584. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4585. // on to get the type correct.
  4586. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4587. forKeys:&key
  4588. count:1] autorelease];
  4589. }
  4590. + (instancetype)dictionaryWithObjects:(const id [])objects
  4591. forKeys:(const int32_t [])keys
  4592. count:(NSUInteger)count {
  4593. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4594. // on to get the type correct.
  4595. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4596. forKeys:keys
  4597. count:count] autorelease];
  4598. }
  4599. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4600. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4601. // on to get the type correct.
  4602. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4603. }
  4604. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4605. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4606. }
  4607. - (instancetype)init {
  4608. return [self initWithObjects:NULL forKeys:NULL count:0];
  4609. }
  4610. - (instancetype)initWithObjects:(const id [])objects
  4611. forKeys:(const int32_t [])keys
  4612. count:(NSUInteger)count {
  4613. self = [super init];
  4614. if (self) {
  4615. _dictionary = [[NSMutableDictionary alloc] init];
  4616. if (count && objects && keys) {
  4617. for (NSUInteger i = 0; i < count; ++i) {
  4618. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4619. }
  4620. }
  4621. }
  4622. return self;
  4623. }
  4624. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4625. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4626. if (self) {
  4627. if (dictionary) {
  4628. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4629. }
  4630. }
  4631. return self;
  4632. }
  4633. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4634. #pragma unused(numItems)
  4635. return [self initWithObjects:NULL forKeys:NULL count:0];
  4636. }
  4637. - (void)dealloc {
  4638. NSAssert(!_autocreator,
  4639. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4640. [self class], _autocreator);
  4641. [_dictionary release];
  4642. [super dealloc];
  4643. }
  4644. - (instancetype)copyWithZone:(NSZone *)zone {
  4645. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4646. }
  4647. - (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
  4648. if (self == other) {
  4649. return YES;
  4650. }
  4651. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4652. return NO;
  4653. }
  4654. return [_dictionary isEqual:other->_dictionary];
  4655. }
  4656. - (NSUInteger)hash {
  4657. return _dictionary.count;
  4658. }
  4659. - (NSString *)description {
  4660. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4661. }
  4662. - (NSUInteger)count {
  4663. return _dictionary.count;
  4664. }
  4665. - (void)enumerateKeysAndObjectsUsingBlock:
  4666. (void (^)(int32_t key, id object, BOOL *stop))block {
  4667. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4668. id aObject,
  4669. BOOL *stop) {
  4670. block([aKey intValue], aObject, stop);
  4671. }];
  4672. }
  4673. - (BOOL)isInitialized {
  4674. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4675. if (!msg.initialized) {
  4676. return NO;
  4677. }
  4678. }
  4679. return YES;
  4680. }
  4681. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4682. GPBInt32ObjectDictionary *newDict =
  4683. [[GPBInt32ObjectDictionary alloc] init];
  4684. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4685. GPBMessage *msg,
  4686. BOOL *stop) {
  4687. #pragma unused(stop)
  4688. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4689. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4690. [copiedMsg release];
  4691. }];
  4692. return newDict;
  4693. }
  4694. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4695. NSUInteger count = _dictionary.count;
  4696. if (count == 0) {
  4697. return 0;
  4698. }
  4699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4700. GPBDataType keyDataType = field.mapKeyDataType;
  4701. __block size_t result = 0;
  4702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4703. id aObject,
  4704. BOOL *stop) {
  4705. #pragma unused(stop)
  4706. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4707. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4708. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4709. }];
  4710. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4711. result += tagSize * count;
  4712. return result;
  4713. }
  4714. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4715. asField:(GPBFieldDescriptor *)field {
  4716. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4717. GPBDataType keyDataType = field.mapKeyDataType;
  4718. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4719. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4720. id aObject,
  4721. BOOL *stop) {
  4722. #pragma unused(stop)
  4723. // Write the tag.
  4724. [outputStream writeInt32NoTag:tag];
  4725. // Write the size of the message.
  4726. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4727. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4728. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4729. // Write the fields.
  4730. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4731. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  4732. }];
  4733. }
  4734. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4735. forGPBGenericValueKey:(GPBGenericValue *)key {
  4736. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4737. }
  4738. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4739. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4740. #pragma unused(stop)
  4741. block([NSString stringWithFormat:@"%d", key], object);
  4742. }];
  4743. }
  4744. - (id)objectForKey:(int32_t)key {
  4745. id result = [_dictionary objectForKey:@(key)];
  4746. return result;
  4747. }
  4748. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4749. if (otherDictionary) {
  4750. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4751. if (_autocreator) {
  4752. GPBAutocreatedDictionaryModified(_autocreator, self);
  4753. }
  4754. }
  4755. }
  4756. - (void)setObject:(id)object forKey:(int32_t)key {
  4757. [_dictionary setObject:object forKey:@(key)];
  4758. if (_autocreator) {
  4759. GPBAutocreatedDictionaryModified(_autocreator, self);
  4760. }
  4761. }
  4762. - (void)removeObjectForKey:(int32_t)aKey {
  4763. [_dictionary removeObjectForKey:@(aKey)];
  4764. }
  4765. - (void)removeAll {
  4766. [_dictionary removeAllObjects];
  4767. }
  4768. @end
  4769. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4770. // This block of code is generated, do not edit it directly.
  4771. #pragma mark - UInt64 -> UInt32
  4772. @implementation GPBUInt64UInt32Dictionary {
  4773. @package
  4774. NSMutableDictionary *_dictionary;
  4775. }
  4776. + (instancetype)dictionary {
  4777. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4778. }
  4779. + (instancetype)dictionaryWithValue:(uint32_t)value
  4780. forKey:(uint64_t)key {
  4781. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4782. // on to get the type correct.
  4783. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4784. forKeys:&key
  4785. count:1] autorelease];
  4786. }
  4787. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4788. forKeys:(const uint64_t [])keys
  4789. count:(NSUInteger)count {
  4790. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4791. // on to get the type correct.
  4792. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4793. forKeys:keys
  4794. count:count] autorelease];
  4795. }
  4796. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4797. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4798. // on to get the type correct.
  4799. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4800. }
  4801. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4802. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4803. }
  4804. - (instancetype)init {
  4805. return [self initWithValues:NULL forKeys:NULL count:0];
  4806. }
  4807. - (instancetype)initWithValues:(const uint32_t [])values
  4808. forKeys:(const uint64_t [])keys
  4809. count:(NSUInteger)count {
  4810. self = [super init];
  4811. if (self) {
  4812. _dictionary = [[NSMutableDictionary alloc] init];
  4813. if (count && values && keys) {
  4814. for (NSUInteger i = 0; i < count; ++i) {
  4815. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4816. }
  4817. }
  4818. }
  4819. return self;
  4820. }
  4821. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4822. self = [self initWithValues:NULL forKeys:NULL count:0];
  4823. if (self) {
  4824. if (dictionary) {
  4825. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4826. }
  4827. }
  4828. return self;
  4829. }
  4830. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4831. #pragma unused(numItems)
  4832. return [self initWithValues:NULL forKeys:NULL count:0];
  4833. }
  4834. - (void)dealloc {
  4835. NSAssert(!_autocreator,
  4836. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4837. [self class], _autocreator);
  4838. [_dictionary release];
  4839. [super dealloc];
  4840. }
  4841. - (instancetype)copyWithZone:(NSZone *)zone {
  4842. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4843. }
  4844. - (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
  4845. if (self == other) {
  4846. return YES;
  4847. }
  4848. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4849. return NO;
  4850. }
  4851. return [_dictionary isEqual:other->_dictionary];
  4852. }
  4853. - (NSUInteger)hash {
  4854. return _dictionary.count;
  4855. }
  4856. - (NSString *)description {
  4857. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4858. }
  4859. - (NSUInteger)count {
  4860. return _dictionary.count;
  4861. }
  4862. - (void)enumerateKeysAndValuesUsingBlock:
  4863. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4864. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4865. NSNumber *aValue,
  4866. BOOL *stop) {
  4867. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4868. }];
  4869. }
  4870. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4871. NSUInteger count = _dictionary.count;
  4872. if (count == 0) {
  4873. return 0;
  4874. }
  4875. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4876. GPBDataType keyDataType = field.mapKeyDataType;
  4877. __block size_t result = 0;
  4878. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4879. NSNumber *aValue,
  4880. BOOL *stop) {
  4881. #pragma unused(stop)
  4882. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4883. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4884. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4885. }];
  4886. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4887. result += tagSize * count;
  4888. return result;
  4889. }
  4890. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4891. asField:(GPBFieldDescriptor *)field {
  4892. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4893. GPBDataType keyDataType = field.mapKeyDataType;
  4894. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4895. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4896. NSNumber *aValue,
  4897. BOOL *stop) {
  4898. #pragma unused(stop)
  4899. // Write the tag.
  4900. [outputStream writeInt32NoTag:tag];
  4901. // Write the size of the message.
  4902. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4903. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4904. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4905. // Write the fields.
  4906. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4907. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4908. }];
  4909. }
  4910. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4911. forGPBGenericValueKey:(GPBGenericValue *)key {
  4912. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4913. }
  4914. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4915. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4916. #pragma unused(stop)
  4917. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4918. }];
  4919. }
  4920. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  4921. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4922. if (wrapped && value) {
  4923. *value = [wrapped unsignedIntValue];
  4924. }
  4925. return (wrapped != NULL);
  4926. }
  4927. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4928. if (otherDictionary) {
  4929. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4930. if (_autocreator) {
  4931. GPBAutocreatedDictionaryModified(_autocreator, self);
  4932. }
  4933. }
  4934. }
  4935. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  4936. [_dictionary setObject:@(value) forKey:@(key)];
  4937. if (_autocreator) {
  4938. GPBAutocreatedDictionaryModified(_autocreator, self);
  4939. }
  4940. }
  4941. - (void)removeValueForKey:(uint64_t)aKey {
  4942. [_dictionary removeObjectForKey:@(aKey)];
  4943. }
  4944. - (void)removeAll {
  4945. [_dictionary removeAllObjects];
  4946. }
  4947. @end
  4948. #pragma mark - UInt64 -> Int32
  4949. @implementation GPBUInt64Int32Dictionary {
  4950. @package
  4951. NSMutableDictionary *_dictionary;
  4952. }
  4953. + (instancetype)dictionary {
  4954. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4955. }
  4956. + (instancetype)dictionaryWithValue:(int32_t)value
  4957. forKey:(uint64_t)key {
  4958. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4959. // on to get the type correct.
  4960. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  4961. forKeys:&key
  4962. count:1] autorelease];
  4963. }
  4964. + (instancetype)dictionaryWithValues:(const int32_t [])values
  4965. forKeys:(const uint64_t [])keys
  4966. count:(NSUInteger)count {
  4967. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4968. // on to get the type correct.
  4969. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  4970. forKeys:keys
  4971. count:count] autorelease];
  4972. }
  4973. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4974. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4975. // on to get the type correct.
  4976. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4977. }
  4978. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4979. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4980. }
  4981. - (instancetype)init {
  4982. return [self initWithValues:NULL forKeys:NULL count:0];
  4983. }
  4984. - (instancetype)initWithValues:(const int32_t [])values
  4985. forKeys:(const uint64_t [])keys
  4986. count:(NSUInteger)count {
  4987. self = [super init];
  4988. if (self) {
  4989. _dictionary = [[NSMutableDictionary alloc] init];
  4990. if (count && values && keys) {
  4991. for (NSUInteger i = 0; i < count; ++i) {
  4992. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4993. }
  4994. }
  4995. }
  4996. return self;
  4997. }
  4998. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4999. self = [self initWithValues:NULL forKeys:NULL count:0];
  5000. if (self) {
  5001. if (dictionary) {
  5002. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5003. }
  5004. }
  5005. return self;
  5006. }
  5007. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5008. #pragma unused(numItems)
  5009. return [self initWithValues:NULL forKeys:NULL count:0];
  5010. }
  5011. - (void)dealloc {
  5012. NSAssert(!_autocreator,
  5013. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5014. [self class], _autocreator);
  5015. [_dictionary release];
  5016. [super dealloc];
  5017. }
  5018. - (instancetype)copyWithZone:(NSZone *)zone {
  5019. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5020. }
  5021. - (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
  5022. if (self == other) {
  5023. return YES;
  5024. }
  5025. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5026. return NO;
  5027. }
  5028. return [_dictionary isEqual:other->_dictionary];
  5029. }
  5030. - (NSUInteger)hash {
  5031. return _dictionary.count;
  5032. }
  5033. - (NSString *)description {
  5034. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5035. }
  5036. - (NSUInteger)count {
  5037. return _dictionary.count;
  5038. }
  5039. - (void)enumerateKeysAndValuesUsingBlock:
  5040. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5041. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5042. NSNumber *aValue,
  5043. BOOL *stop) {
  5044. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5045. }];
  5046. }
  5047. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5048. NSUInteger count = _dictionary.count;
  5049. if (count == 0) {
  5050. return 0;
  5051. }
  5052. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5053. GPBDataType keyDataType = field.mapKeyDataType;
  5054. __block size_t result = 0;
  5055. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5056. NSNumber *aValue,
  5057. BOOL *stop) {
  5058. #pragma unused(stop)
  5059. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5060. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5061. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5062. }];
  5063. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5064. result += tagSize * count;
  5065. return result;
  5066. }
  5067. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5068. asField:(GPBFieldDescriptor *)field {
  5069. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5070. GPBDataType keyDataType = field.mapKeyDataType;
  5071. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5072. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5073. NSNumber *aValue,
  5074. BOOL *stop) {
  5075. #pragma unused(stop)
  5076. // Write the tag.
  5077. [outputStream writeInt32NoTag:tag];
  5078. // Write the size of the message.
  5079. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5080. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5081. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5082. // Write the fields.
  5083. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5084. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5085. }];
  5086. }
  5087. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5088. forGPBGenericValueKey:(GPBGenericValue *)key {
  5089. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5090. }
  5091. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5092. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5093. #pragma unused(stop)
  5094. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5095. }];
  5096. }
  5097. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5098. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5099. if (wrapped && value) {
  5100. *value = [wrapped intValue];
  5101. }
  5102. return (wrapped != NULL);
  5103. }
  5104. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5105. if (otherDictionary) {
  5106. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5107. if (_autocreator) {
  5108. GPBAutocreatedDictionaryModified(_autocreator, self);
  5109. }
  5110. }
  5111. }
  5112. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5113. [_dictionary setObject:@(value) forKey:@(key)];
  5114. if (_autocreator) {
  5115. GPBAutocreatedDictionaryModified(_autocreator, self);
  5116. }
  5117. }
  5118. - (void)removeValueForKey:(uint64_t)aKey {
  5119. [_dictionary removeObjectForKey:@(aKey)];
  5120. }
  5121. - (void)removeAll {
  5122. [_dictionary removeAllObjects];
  5123. }
  5124. @end
  5125. #pragma mark - UInt64 -> UInt64
  5126. @implementation GPBUInt64UInt64Dictionary {
  5127. @package
  5128. NSMutableDictionary *_dictionary;
  5129. }
  5130. + (instancetype)dictionary {
  5131. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5132. }
  5133. + (instancetype)dictionaryWithValue:(uint64_t)value
  5134. forKey:(uint64_t)key {
  5135. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5136. // on to get the type correct.
  5137. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  5138. forKeys:&key
  5139. count:1] autorelease];
  5140. }
  5141. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  5142. forKeys:(const uint64_t [])keys
  5143. count:(NSUInteger)count {
  5144. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5145. // on to get the type correct.
  5146. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  5147. forKeys:keys
  5148. count:count] autorelease];
  5149. }
  5150. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5151. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5152. // on to get the type correct.
  5153. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5154. }
  5155. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5156. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5157. }
  5158. - (instancetype)init {
  5159. return [self initWithValues:NULL forKeys:NULL count:0];
  5160. }
  5161. - (instancetype)initWithValues:(const uint64_t [])values
  5162. forKeys:(const uint64_t [])keys
  5163. count:(NSUInteger)count {
  5164. self = [super init];
  5165. if (self) {
  5166. _dictionary = [[NSMutableDictionary alloc] init];
  5167. if (count && values && keys) {
  5168. for (NSUInteger i = 0; i < count; ++i) {
  5169. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5170. }
  5171. }
  5172. }
  5173. return self;
  5174. }
  5175. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5176. self = [self initWithValues:NULL forKeys:NULL count:0];
  5177. if (self) {
  5178. if (dictionary) {
  5179. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5180. }
  5181. }
  5182. return self;
  5183. }
  5184. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5185. #pragma unused(numItems)
  5186. return [self initWithValues:NULL forKeys:NULL count:0];
  5187. }
  5188. - (void)dealloc {
  5189. NSAssert(!_autocreator,
  5190. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5191. [self class], _autocreator);
  5192. [_dictionary release];
  5193. [super dealloc];
  5194. }
  5195. - (instancetype)copyWithZone:(NSZone *)zone {
  5196. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5197. }
  5198. - (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
  5199. if (self == other) {
  5200. return YES;
  5201. }
  5202. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5203. return NO;
  5204. }
  5205. return [_dictionary isEqual:other->_dictionary];
  5206. }
  5207. - (NSUInteger)hash {
  5208. return _dictionary.count;
  5209. }
  5210. - (NSString *)description {
  5211. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5212. }
  5213. - (NSUInteger)count {
  5214. return _dictionary.count;
  5215. }
  5216. - (void)enumerateKeysAndValuesUsingBlock:
  5217. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5218. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5219. NSNumber *aValue,
  5220. BOOL *stop) {
  5221. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5222. }];
  5223. }
  5224. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5225. NSUInteger count = _dictionary.count;
  5226. if (count == 0) {
  5227. return 0;
  5228. }
  5229. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5230. GPBDataType keyDataType = field.mapKeyDataType;
  5231. __block size_t result = 0;
  5232. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5233. NSNumber *aValue,
  5234. BOOL *stop) {
  5235. #pragma unused(stop)
  5236. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5237. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5238. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5239. }];
  5240. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5241. result += tagSize * count;
  5242. return result;
  5243. }
  5244. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5245. asField:(GPBFieldDescriptor *)field {
  5246. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5247. GPBDataType keyDataType = field.mapKeyDataType;
  5248. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5249. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5250. NSNumber *aValue,
  5251. BOOL *stop) {
  5252. #pragma unused(stop)
  5253. // Write the tag.
  5254. [outputStream writeInt32NoTag:tag];
  5255. // Write the size of the message.
  5256. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5257. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5258. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5259. // Write the fields.
  5260. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5261. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5262. }];
  5263. }
  5264. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5265. forGPBGenericValueKey:(GPBGenericValue *)key {
  5266. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5267. }
  5268. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5269. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5270. #pragma unused(stop)
  5271. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5272. }];
  5273. }
  5274. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  5275. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5276. if (wrapped && value) {
  5277. *value = [wrapped unsignedLongLongValue];
  5278. }
  5279. return (wrapped != NULL);
  5280. }
  5281. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5282. if (otherDictionary) {
  5283. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5284. if (_autocreator) {
  5285. GPBAutocreatedDictionaryModified(_autocreator, self);
  5286. }
  5287. }
  5288. }
  5289. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5290. [_dictionary setObject:@(value) forKey:@(key)];
  5291. if (_autocreator) {
  5292. GPBAutocreatedDictionaryModified(_autocreator, self);
  5293. }
  5294. }
  5295. - (void)removeValueForKey:(uint64_t)aKey {
  5296. [_dictionary removeObjectForKey:@(aKey)];
  5297. }
  5298. - (void)removeAll {
  5299. [_dictionary removeAllObjects];
  5300. }
  5301. @end
  5302. #pragma mark - UInt64 -> Int64
  5303. @implementation GPBUInt64Int64Dictionary {
  5304. @package
  5305. NSMutableDictionary *_dictionary;
  5306. }
  5307. + (instancetype)dictionary {
  5308. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5309. }
  5310. + (instancetype)dictionaryWithValue:(int64_t)value
  5311. forKey:(uint64_t)key {
  5312. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5313. // on to get the type correct.
  5314. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5315. forKeys:&key
  5316. count:1] autorelease];
  5317. }
  5318. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5319. forKeys:(const uint64_t [])keys
  5320. count:(NSUInteger)count {
  5321. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5322. // on to get the type correct.
  5323. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5324. forKeys:keys
  5325. count:count] autorelease];
  5326. }
  5327. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5328. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5329. // on to get the type correct.
  5330. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5331. }
  5332. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5333. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5334. }
  5335. - (instancetype)init {
  5336. return [self initWithValues:NULL forKeys:NULL count:0];
  5337. }
  5338. - (instancetype)initWithValues:(const int64_t [])values
  5339. forKeys:(const uint64_t [])keys
  5340. count:(NSUInteger)count {
  5341. self = [super init];
  5342. if (self) {
  5343. _dictionary = [[NSMutableDictionary alloc] init];
  5344. if (count && values && keys) {
  5345. for (NSUInteger i = 0; i < count; ++i) {
  5346. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5347. }
  5348. }
  5349. }
  5350. return self;
  5351. }
  5352. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5353. self = [self initWithValues:NULL forKeys:NULL count:0];
  5354. if (self) {
  5355. if (dictionary) {
  5356. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5357. }
  5358. }
  5359. return self;
  5360. }
  5361. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5362. #pragma unused(numItems)
  5363. return [self initWithValues:NULL forKeys:NULL count:0];
  5364. }
  5365. - (void)dealloc {
  5366. NSAssert(!_autocreator,
  5367. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5368. [self class], _autocreator);
  5369. [_dictionary release];
  5370. [super dealloc];
  5371. }
  5372. - (instancetype)copyWithZone:(NSZone *)zone {
  5373. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5374. }
  5375. - (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
  5376. if (self == other) {
  5377. return YES;
  5378. }
  5379. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5380. return NO;
  5381. }
  5382. return [_dictionary isEqual:other->_dictionary];
  5383. }
  5384. - (NSUInteger)hash {
  5385. return _dictionary.count;
  5386. }
  5387. - (NSString *)description {
  5388. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5389. }
  5390. - (NSUInteger)count {
  5391. return _dictionary.count;
  5392. }
  5393. - (void)enumerateKeysAndValuesUsingBlock:
  5394. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5395. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5396. NSNumber *aValue,
  5397. BOOL *stop) {
  5398. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5399. }];
  5400. }
  5401. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5402. NSUInteger count = _dictionary.count;
  5403. if (count == 0) {
  5404. return 0;
  5405. }
  5406. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5407. GPBDataType keyDataType = field.mapKeyDataType;
  5408. __block size_t result = 0;
  5409. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5410. NSNumber *aValue,
  5411. BOOL *stop) {
  5412. #pragma unused(stop)
  5413. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5414. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5415. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5416. }];
  5417. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5418. result += tagSize * count;
  5419. return result;
  5420. }
  5421. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5422. asField:(GPBFieldDescriptor *)field {
  5423. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5424. GPBDataType keyDataType = field.mapKeyDataType;
  5425. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5426. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5427. NSNumber *aValue,
  5428. BOOL *stop) {
  5429. #pragma unused(stop)
  5430. // Write the tag.
  5431. [outputStream writeInt32NoTag:tag];
  5432. // Write the size of the message.
  5433. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5434. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5435. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5436. // Write the fields.
  5437. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5438. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5439. }];
  5440. }
  5441. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5442. forGPBGenericValueKey:(GPBGenericValue *)key {
  5443. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5444. }
  5445. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5446. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5447. #pragma unused(stop)
  5448. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5449. }];
  5450. }
  5451. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5452. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5453. if (wrapped && value) {
  5454. *value = [wrapped longLongValue];
  5455. }
  5456. return (wrapped != NULL);
  5457. }
  5458. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5459. if (otherDictionary) {
  5460. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5461. if (_autocreator) {
  5462. GPBAutocreatedDictionaryModified(_autocreator, self);
  5463. }
  5464. }
  5465. }
  5466. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5467. [_dictionary setObject:@(value) forKey:@(key)];
  5468. if (_autocreator) {
  5469. GPBAutocreatedDictionaryModified(_autocreator, self);
  5470. }
  5471. }
  5472. - (void)removeValueForKey:(uint64_t)aKey {
  5473. [_dictionary removeObjectForKey:@(aKey)];
  5474. }
  5475. - (void)removeAll {
  5476. [_dictionary removeAllObjects];
  5477. }
  5478. @end
  5479. #pragma mark - UInt64 -> Bool
  5480. @implementation GPBUInt64BoolDictionary {
  5481. @package
  5482. NSMutableDictionary *_dictionary;
  5483. }
  5484. + (instancetype)dictionary {
  5485. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5486. }
  5487. + (instancetype)dictionaryWithValue:(BOOL)value
  5488. forKey:(uint64_t)key {
  5489. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5490. // on to get the type correct.
  5491. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5492. forKeys:&key
  5493. count:1] autorelease];
  5494. }
  5495. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5496. forKeys:(const uint64_t [])keys
  5497. count:(NSUInteger)count {
  5498. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5499. // on to get the type correct.
  5500. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5501. forKeys:keys
  5502. count:count] autorelease];
  5503. }
  5504. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5505. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5506. // on to get the type correct.
  5507. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5508. }
  5509. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5510. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5511. }
  5512. - (instancetype)init {
  5513. return [self initWithValues:NULL forKeys:NULL count:0];
  5514. }
  5515. - (instancetype)initWithValues:(const BOOL [])values
  5516. forKeys:(const uint64_t [])keys
  5517. count:(NSUInteger)count {
  5518. self = [super init];
  5519. if (self) {
  5520. _dictionary = [[NSMutableDictionary alloc] init];
  5521. if (count && values && keys) {
  5522. for (NSUInteger i = 0; i < count; ++i) {
  5523. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5524. }
  5525. }
  5526. }
  5527. return self;
  5528. }
  5529. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5530. self = [self initWithValues:NULL forKeys:NULL count:0];
  5531. if (self) {
  5532. if (dictionary) {
  5533. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5534. }
  5535. }
  5536. return self;
  5537. }
  5538. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5539. #pragma unused(numItems)
  5540. return [self initWithValues:NULL forKeys:NULL count:0];
  5541. }
  5542. - (void)dealloc {
  5543. NSAssert(!_autocreator,
  5544. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5545. [self class], _autocreator);
  5546. [_dictionary release];
  5547. [super dealloc];
  5548. }
  5549. - (instancetype)copyWithZone:(NSZone *)zone {
  5550. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5551. }
  5552. - (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
  5553. if (self == other) {
  5554. return YES;
  5555. }
  5556. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5557. return NO;
  5558. }
  5559. return [_dictionary isEqual:other->_dictionary];
  5560. }
  5561. - (NSUInteger)hash {
  5562. return _dictionary.count;
  5563. }
  5564. - (NSString *)description {
  5565. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5566. }
  5567. - (NSUInteger)count {
  5568. return _dictionary.count;
  5569. }
  5570. - (void)enumerateKeysAndValuesUsingBlock:
  5571. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5572. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5573. NSNumber *aValue,
  5574. BOOL *stop) {
  5575. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5576. }];
  5577. }
  5578. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5579. NSUInteger count = _dictionary.count;
  5580. if (count == 0) {
  5581. return 0;
  5582. }
  5583. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5584. GPBDataType keyDataType = field.mapKeyDataType;
  5585. __block size_t result = 0;
  5586. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5587. NSNumber *aValue,
  5588. BOOL *stop) {
  5589. #pragma unused(stop)
  5590. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5591. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5592. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5593. }];
  5594. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5595. result += tagSize * count;
  5596. return result;
  5597. }
  5598. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5599. asField:(GPBFieldDescriptor *)field {
  5600. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5601. GPBDataType keyDataType = field.mapKeyDataType;
  5602. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5603. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5604. NSNumber *aValue,
  5605. BOOL *stop) {
  5606. #pragma unused(stop)
  5607. // Write the tag.
  5608. [outputStream writeInt32NoTag:tag];
  5609. // Write the size of the message.
  5610. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5611. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5612. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5613. // Write the fields.
  5614. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5615. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5616. }];
  5617. }
  5618. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5619. forGPBGenericValueKey:(GPBGenericValue *)key {
  5620. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5621. }
  5622. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5623. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5624. #pragma unused(stop)
  5625. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5626. }];
  5627. }
  5628. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5629. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5630. if (wrapped && value) {
  5631. *value = [wrapped boolValue];
  5632. }
  5633. return (wrapped != NULL);
  5634. }
  5635. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5636. if (otherDictionary) {
  5637. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5638. if (_autocreator) {
  5639. GPBAutocreatedDictionaryModified(_autocreator, self);
  5640. }
  5641. }
  5642. }
  5643. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5644. [_dictionary setObject:@(value) forKey:@(key)];
  5645. if (_autocreator) {
  5646. GPBAutocreatedDictionaryModified(_autocreator, self);
  5647. }
  5648. }
  5649. - (void)removeValueForKey:(uint64_t)aKey {
  5650. [_dictionary removeObjectForKey:@(aKey)];
  5651. }
  5652. - (void)removeAll {
  5653. [_dictionary removeAllObjects];
  5654. }
  5655. @end
  5656. #pragma mark - UInt64 -> Float
  5657. @implementation GPBUInt64FloatDictionary {
  5658. @package
  5659. NSMutableDictionary *_dictionary;
  5660. }
  5661. + (instancetype)dictionary {
  5662. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5663. }
  5664. + (instancetype)dictionaryWithValue:(float)value
  5665. forKey:(uint64_t)key {
  5666. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5667. // on to get the type correct.
  5668. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5669. forKeys:&key
  5670. count:1] autorelease];
  5671. }
  5672. + (instancetype)dictionaryWithValues:(const float [])values
  5673. forKeys:(const uint64_t [])keys
  5674. count:(NSUInteger)count {
  5675. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5676. // on to get the type correct.
  5677. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5678. forKeys:keys
  5679. count:count] autorelease];
  5680. }
  5681. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5682. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5683. // on to get the type correct.
  5684. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5685. }
  5686. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5687. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5688. }
  5689. - (instancetype)init {
  5690. return [self initWithValues:NULL forKeys:NULL count:0];
  5691. }
  5692. - (instancetype)initWithValues:(const float [])values
  5693. forKeys:(const uint64_t [])keys
  5694. count:(NSUInteger)count {
  5695. self = [super init];
  5696. if (self) {
  5697. _dictionary = [[NSMutableDictionary alloc] init];
  5698. if (count && values && keys) {
  5699. for (NSUInteger i = 0; i < count; ++i) {
  5700. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5701. }
  5702. }
  5703. }
  5704. return self;
  5705. }
  5706. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5707. self = [self initWithValues:NULL forKeys:NULL count:0];
  5708. if (self) {
  5709. if (dictionary) {
  5710. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5711. }
  5712. }
  5713. return self;
  5714. }
  5715. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5716. #pragma unused(numItems)
  5717. return [self initWithValues:NULL forKeys:NULL count:0];
  5718. }
  5719. - (void)dealloc {
  5720. NSAssert(!_autocreator,
  5721. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5722. [self class], _autocreator);
  5723. [_dictionary release];
  5724. [super dealloc];
  5725. }
  5726. - (instancetype)copyWithZone:(NSZone *)zone {
  5727. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5728. }
  5729. - (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
  5730. if (self == other) {
  5731. return YES;
  5732. }
  5733. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5734. return NO;
  5735. }
  5736. return [_dictionary isEqual:other->_dictionary];
  5737. }
  5738. - (NSUInteger)hash {
  5739. return _dictionary.count;
  5740. }
  5741. - (NSString *)description {
  5742. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5743. }
  5744. - (NSUInteger)count {
  5745. return _dictionary.count;
  5746. }
  5747. - (void)enumerateKeysAndValuesUsingBlock:
  5748. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5749. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5750. NSNumber *aValue,
  5751. BOOL *stop) {
  5752. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5753. }];
  5754. }
  5755. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5756. NSUInteger count = _dictionary.count;
  5757. if (count == 0) {
  5758. return 0;
  5759. }
  5760. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5761. GPBDataType keyDataType = field.mapKeyDataType;
  5762. __block size_t result = 0;
  5763. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5764. NSNumber *aValue,
  5765. BOOL *stop) {
  5766. #pragma unused(stop)
  5767. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5768. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5769. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5770. }];
  5771. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5772. result += tagSize * count;
  5773. return result;
  5774. }
  5775. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5776. asField:(GPBFieldDescriptor *)field {
  5777. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5778. GPBDataType keyDataType = field.mapKeyDataType;
  5779. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5780. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5781. NSNumber *aValue,
  5782. BOOL *stop) {
  5783. #pragma unused(stop)
  5784. // Write the tag.
  5785. [outputStream writeInt32NoTag:tag];
  5786. // Write the size of the message.
  5787. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5788. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5789. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5790. // Write the fields.
  5791. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5792. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5793. }];
  5794. }
  5795. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5796. forGPBGenericValueKey:(GPBGenericValue *)key {
  5797. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5798. }
  5799. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5800. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5801. #pragma unused(stop)
  5802. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5803. }];
  5804. }
  5805. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5806. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5807. if (wrapped && value) {
  5808. *value = [wrapped floatValue];
  5809. }
  5810. return (wrapped != NULL);
  5811. }
  5812. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5813. if (otherDictionary) {
  5814. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5815. if (_autocreator) {
  5816. GPBAutocreatedDictionaryModified(_autocreator, self);
  5817. }
  5818. }
  5819. }
  5820. - (void)setValue:(float)value forKey:(uint64_t)key {
  5821. [_dictionary setObject:@(value) forKey:@(key)];
  5822. if (_autocreator) {
  5823. GPBAutocreatedDictionaryModified(_autocreator, self);
  5824. }
  5825. }
  5826. - (void)removeValueForKey:(uint64_t)aKey {
  5827. [_dictionary removeObjectForKey:@(aKey)];
  5828. }
  5829. - (void)removeAll {
  5830. [_dictionary removeAllObjects];
  5831. }
  5832. @end
  5833. #pragma mark - UInt64 -> Double
  5834. @implementation GPBUInt64DoubleDictionary {
  5835. @package
  5836. NSMutableDictionary *_dictionary;
  5837. }
  5838. + (instancetype)dictionary {
  5839. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5840. }
  5841. + (instancetype)dictionaryWithValue:(double)value
  5842. forKey:(uint64_t)key {
  5843. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5844. // on to get the type correct.
  5845. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5846. forKeys:&key
  5847. count:1] autorelease];
  5848. }
  5849. + (instancetype)dictionaryWithValues:(const double [])values
  5850. forKeys:(const uint64_t [])keys
  5851. count:(NSUInteger)count {
  5852. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5853. // on to get the type correct.
  5854. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5855. forKeys:keys
  5856. count:count] autorelease];
  5857. }
  5858. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5859. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5860. // on to get the type correct.
  5861. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5862. }
  5863. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5864. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5865. }
  5866. - (instancetype)init {
  5867. return [self initWithValues:NULL forKeys:NULL count:0];
  5868. }
  5869. - (instancetype)initWithValues:(const double [])values
  5870. forKeys:(const uint64_t [])keys
  5871. count:(NSUInteger)count {
  5872. self = [super init];
  5873. if (self) {
  5874. _dictionary = [[NSMutableDictionary alloc] init];
  5875. if (count && values && keys) {
  5876. for (NSUInteger i = 0; i < count; ++i) {
  5877. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5878. }
  5879. }
  5880. }
  5881. return self;
  5882. }
  5883. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5884. self = [self initWithValues:NULL forKeys:NULL count:0];
  5885. if (self) {
  5886. if (dictionary) {
  5887. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5888. }
  5889. }
  5890. return self;
  5891. }
  5892. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5893. #pragma unused(numItems)
  5894. return [self initWithValues:NULL forKeys:NULL count:0];
  5895. }
  5896. - (void)dealloc {
  5897. NSAssert(!_autocreator,
  5898. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5899. [self class], _autocreator);
  5900. [_dictionary release];
  5901. [super dealloc];
  5902. }
  5903. - (instancetype)copyWithZone:(NSZone *)zone {
  5904. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5905. }
  5906. - (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
  5907. if (self == other) {
  5908. return YES;
  5909. }
  5910. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5911. return NO;
  5912. }
  5913. return [_dictionary isEqual:other->_dictionary];
  5914. }
  5915. - (NSUInteger)hash {
  5916. return _dictionary.count;
  5917. }
  5918. - (NSString *)description {
  5919. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5920. }
  5921. - (NSUInteger)count {
  5922. return _dictionary.count;
  5923. }
  5924. - (void)enumerateKeysAndValuesUsingBlock:
  5925. (void (^)(uint64_t key, double value, BOOL *stop))block {
  5926. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5927. NSNumber *aValue,
  5928. BOOL *stop) {
  5929. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  5930. }];
  5931. }
  5932. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5933. NSUInteger count = _dictionary.count;
  5934. if (count == 0) {
  5935. return 0;
  5936. }
  5937. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5938. GPBDataType keyDataType = field.mapKeyDataType;
  5939. __block size_t result = 0;
  5940. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5941. NSNumber *aValue,
  5942. BOOL *stop) {
  5943. #pragma unused(stop)
  5944. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5945. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5946. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5947. }];
  5948. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5949. result += tagSize * count;
  5950. return result;
  5951. }
  5952. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5953. asField:(GPBFieldDescriptor *)field {
  5954. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5955. GPBDataType keyDataType = field.mapKeyDataType;
  5956. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5957. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5958. NSNumber *aValue,
  5959. BOOL *stop) {
  5960. #pragma unused(stop)
  5961. // Write the tag.
  5962. [outputStream writeInt32NoTag:tag];
  5963. // Write the size of the message.
  5964. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5965. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5966. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5967. // Write the fields.
  5968. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5969. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5970. }];
  5971. }
  5972. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5973. forGPBGenericValueKey:(GPBGenericValue *)key {
  5974. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5975. }
  5976. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5977. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  5978. #pragma unused(stop)
  5979. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5980. }];
  5981. }
  5982. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  5983. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5984. if (wrapped && value) {
  5985. *value = [wrapped doubleValue];
  5986. }
  5987. return (wrapped != NULL);
  5988. }
  5989. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5990. if (otherDictionary) {
  5991. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5992. if (_autocreator) {
  5993. GPBAutocreatedDictionaryModified(_autocreator, self);
  5994. }
  5995. }
  5996. }
  5997. - (void)setValue:(double)value forKey:(uint64_t)key {
  5998. [_dictionary setObject:@(value) forKey:@(key)];
  5999. if (_autocreator) {
  6000. GPBAutocreatedDictionaryModified(_autocreator, self);
  6001. }
  6002. }
  6003. - (void)removeValueForKey:(uint64_t)aKey {
  6004. [_dictionary removeObjectForKey:@(aKey)];
  6005. }
  6006. - (void)removeAll {
  6007. [_dictionary removeAllObjects];
  6008. }
  6009. @end
  6010. #pragma mark - UInt64 -> Enum
  6011. @implementation GPBUInt64EnumDictionary {
  6012. @package
  6013. NSMutableDictionary *_dictionary;
  6014. GPBEnumValidationFunc _validationFunc;
  6015. }
  6016. @synthesize validationFunc = _validationFunc;
  6017. + (instancetype)dictionary {
  6018. return [[[self alloc] initWithValidationFunction:NULL
  6019. rawValues:NULL
  6020. forKeys:NULL
  6021. count:0] autorelease];
  6022. }
  6023. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6024. return [[[self alloc] initWithValidationFunction:func
  6025. rawValues:NULL
  6026. forKeys:NULL
  6027. count:0] autorelease];
  6028. }
  6029. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6030. rawValue:(int32_t)rawValue
  6031. forKey:(uint64_t)key {
  6032. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6033. // on to get the type correct.
  6034. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6035. rawValues:&rawValue
  6036. forKeys:&key
  6037. count:1] autorelease];
  6038. }
  6039. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6040. rawValues:(const int32_t [])rawValues
  6041. forKeys:(const uint64_t [])keys
  6042. count:(NSUInteger)count {
  6043. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6044. // on to get the type correct.
  6045. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6046. rawValues:rawValues
  6047. forKeys:keys
  6048. count:count] autorelease];
  6049. }
  6050. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6051. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6052. // on to get the type correct.
  6053. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6054. }
  6055. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6056. capacity:(NSUInteger)numItems {
  6057. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6058. }
  6059. - (instancetype)init {
  6060. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6061. }
  6062. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6063. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6064. }
  6065. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6066. rawValues:(const int32_t [])rawValues
  6067. forKeys:(const uint64_t [])keys
  6068. count:(NSUInteger)count {
  6069. self = [super init];
  6070. if (self) {
  6071. _dictionary = [[NSMutableDictionary alloc] init];
  6072. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6073. if (count && rawValues && keys) {
  6074. for (NSUInteger i = 0; i < count; ++i) {
  6075. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6076. }
  6077. }
  6078. }
  6079. return self;
  6080. }
  6081. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6082. self = [self initWithValidationFunction:dictionary.validationFunc
  6083. rawValues:NULL
  6084. forKeys:NULL
  6085. count:0];
  6086. if (self) {
  6087. if (dictionary) {
  6088. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6089. }
  6090. }
  6091. return self;
  6092. }
  6093. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6094. capacity:(NSUInteger)numItems {
  6095. #pragma unused(numItems)
  6096. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6097. }
  6098. - (void)dealloc {
  6099. NSAssert(!_autocreator,
  6100. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6101. [self class], _autocreator);
  6102. [_dictionary release];
  6103. [super dealloc];
  6104. }
  6105. - (instancetype)copyWithZone:(NSZone *)zone {
  6106. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6107. }
  6108. - (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
  6109. if (self == other) {
  6110. return YES;
  6111. }
  6112. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6113. return NO;
  6114. }
  6115. return [_dictionary isEqual:other->_dictionary];
  6116. }
  6117. - (NSUInteger)hash {
  6118. return _dictionary.count;
  6119. }
  6120. - (NSString *)description {
  6121. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6122. }
  6123. - (NSUInteger)count {
  6124. return _dictionary.count;
  6125. }
  6126. - (void)enumerateKeysAndRawValuesUsingBlock:
  6127. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6128. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6129. NSNumber *aValue,
  6130. BOOL *stop) {
  6131. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6132. }];
  6133. }
  6134. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6135. NSUInteger count = _dictionary.count;
  6136. if (count == 0) {
  6137. return 0;
  6138. }
  6139. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6140. GPBDataType keyDataType = field.mapKeyDataType;
  6141. __block size_t result = 0;
  6142. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6143. NSNumber *aValue,
  6144. BOOL *stop) {
  6145. #pragma unused(stop)
  6146. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6147. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6148. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6149. }];
  6150. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6151. result += tagSize * count;
  6152. return result;
  6153. }
  6154. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6155. asField:(GPBFieldDescriptor *)field {
  6156. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6157. GPBDataType keyDataType = field.mapKeyDataType;
  6158. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6159. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6160. NSNumber *aValue,
  6161. BOOL *stop) {
  6162. #pragma unused(stop)
  6163. // Write the tag.
  6164. [outputStream writeInt32NoTag:tag];
  6165. // Write the size of the message.
  6166. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6167. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6168. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6169. // Write the fields.
  6170. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6171. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6172. }];
  6173. }
  6174. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6175. forKey:(GPBGenericValue *)key
  6176. keyDataType:(GPBDataType)keyDataType {
  6177. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6178. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6179. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6180. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6181. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6182. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6183. [outputStream release];
  6184. return data;
  6185. }
  6186. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6187. forGPBGenericValueKey:(GPBGenericValue *)key {
  6188. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6189. }
  6190. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6191. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6192. #pragma unused(stop)
  6193. block([NSString stringWithFormat:@"%llu", key], @(value));
  6194. }];
  6195. }
  6196. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  6197. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6198. if (wrapped && value) {
  6199. int32_t result = [wrapped intValue];
  6200. if (!_validationFunc(result)) {
  6201. result = kGPBUnrecognizedEnumeratorValue;
  6202. }
  6203. *value = result;
  6204. }
  6205. return (wrapped != NULL);
  6206. }
  6207. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  6208. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6209. if (wrapped && rawValue) {
  6210. *rawValue = [wrapped intValue];
  6211. }
  6212. return (wrapped != NULL);
  6213. }
  6214. - (void)enumerateKeysAndValuesUsingBlock:
  6215. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6216. GPBEnumValidationFunc func = _validationFunc;
  6217. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6218. NSNumber *aValue,
  6219. BOOL *stop) {
  6220. int32_t unwrapped = [aValue intValue];
  6221. if (!func(unwrapped)) {
  6222. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6223. }
  6224. block([aKey unsignedLongLongValue], unwrapped, stop);
  6225. }];
  6226. }
  6227. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6228. if (otherDictionary) {
  6229. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6230. if (_autocreator) {
  6231. GPBAutocreatedDictionaryModified(_autocreator, self);
  6232. }
  6233. }
  6234. }
  6235. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6236. [_dictionary setObject:@(value) forKey:@(key)];
  6237. if (_autocreator) {
  6238. GPBAutocreatedDictionaryModified(_autocreator, self);
  6239. }
  6240. }
  6241. - (void)removeValueForKey:(uint64_t)aKey {
  6242. [_dictionary removeObjectForKey:@(aKey)];
  6243. }
  6244. - (void)removeAll {
  6245. [_dictionary removeAllObjects];
  6246. }
  6247. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  6248. if (!_validationFunc(value)) {
  6249. [NSException raise:NSInvalidArgumentException
  6250. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6251. value];
  6252. }
  6253. [_dictionary setObject:@(value) forKey:@(key)];
  6254. if (_autocreator) {
  6255. GPBAutocreatedDictionaryModified(_autocreator, self);
  6256. }
  6257. }
  6258. @end
  6259. #pragma mark - UInt64 -> Object
  6260. @implementation GPBUInt64ObjectDictionary {
  6261. @package
  6262. NSMutableDictionary *_dictionary;
  6263. }
  6264. + (instancetype)dictionary {
  6265. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6266. }
  6267. + (instancetype)dictionaryWithObject:(id)object
  6268. forKey:(uint64_t)key {
  6269. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6270. // on to get the type correct.
  6271. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6272. forKeys:&key
  6273. count:1] autorelease];
  6274. }
  6275. + (instancetype)dictionaryWithObjects:(const id [])objects
  6276. forKeys:(const uint64_t [])keys
  6277. count:(NSUInteger)count {
  6278. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6279. // on to get the type correct.
  6280. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6281. forKeys:keys
  6282. count:count] autorelease];
  6283. }
  6284. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6285. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6286. // on to get the type correct.
  6287. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6288. }
  6289. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6290. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6291. }
  6292. - (instancetype)init {
  6293. return [self initWithObjects:NULL forKeys:NULL count:0];
  6294. }
  6295. - (instancetype)initWithObjects:(const id [])objects
  6296. forKeys:(const uint64_t [])keys
  6297. count:(NSUInteger)count {
  6298. self = [super init];
  6299. if (self) {
  6300. _dictionary = [[NSMutableDictionary alloc] init];
  6301. if (count && objects && keys) {
  6302. for (NSUInteger i = 0; i < count; ++i) {
  6303. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6304. }
  6305. }
  6306. }
  6307. return self;
  6308. }
  6309. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6310. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6311. if (self) {
  6312. if (dictionary) {
  6313. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6314. }
  6315. }
  6316. return self;
  6317. }
  6318. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6319. #pragma unused(numItems)
  6320. return [self initWithObjects:NULL forKeys:NULL count:0];
  6321. }
  6322. - (void)dealloc {
  6323. NSAssert(!_autocreator,
  6324. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6325. [self class], _autocreator);
  6326. [_dictionary release];
  6327. [super dealloc];
  6328. }
  6329. - (instancetype)copyWithZone:(NSZone *)zone {
  6330. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6331. }
  6332. - (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
  6333. if (self == other) {
  6334. return YES;
  6335. }
  6336. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6337. return NO;
  6338. }
  6339. return [_dictionary isEqual:other->_dictionary];
  6340. }
  6341. - (NSUInteger)hash {
  6342. return _dictionary.count;
  6343. }
  6344. - (NSString *)description {
  6345. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6346. }
  6347. - (NSUInteger)count {
  6348. return _dictionary.count;
  6349. }
  6350. - (void)enumerateKeysAndObjectsUsingBlock:
  6351. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6352. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6353. id aObject,
  6354. BOOL *stop) {
  6355. block([aKey unsignedLongLongValue], aObject, stop);
  6356. }];
  6357. }
  6358. - (BOOL)isInitialized {
  6359. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6360. if (!msg.initialized) {
  6361. return NO;
  6362. }
  6363. }
  6364. return YES;
  6365. }
  6366. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6367. GPBUInt64ObjectDictionary *newDict =
  6368. [[GPBUInt64ObjectDictionary alloc] init];
  6369. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6370. GPBMessage *msg,
  6371. BOOL *stop) {
  6372. #pragma unused(stop)
  6373. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6374. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6375. [copiedMsg release];
  6376. }];
  6377. return newDict;
  6378. }
  6379. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6380. NSUInteger count = _dictionary.count;
  6381. if (count == 0) {
  6382. return 0;
  6383. }
  6384. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6385. GPBDataType keyDataType = field.mapKeyDataType;
  6386. __block size_t result = 0;
  6387. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6388. id aObject,
  6389. BOOL *stop) {
  6390. #pragma unused(stop)
  6391. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6392. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6393. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6394. }];
  6395. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6396. result += tagSize * count;
  6397. return result;
  6398. }
  6399. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6400. asField:(GPBFieldDescriptor *)field {
  6401. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6402. GPBDataType keyDataType = field.mapKeyDataType;
  6403. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6404. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6405. id aObject,
  6406. BOOL *stop) {
  6407. #pragma unused(stop)
  6408. // Write the tag.
  6409. [outputStream writeInt32NoTag:tag];
  6410. // Write the size of the message.
  6411. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6412. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6413. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6414. // Write the fields.
  6415. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6416. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  6417. }];
  6418. }
  6419. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6420. forGPBGenericValueKey:(GPBGenericValue *)key {
  6421. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6422. }
  6423. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6424. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6425. #pragma unused(stop)
  6426. block([NSString stringWithFormat:@"%llu", key], object);
  6427. }];
  6428. }
  6429. - (id)objectForKey:(uint64_t)key {
  6430. id result = [_dictionary objectForKey:@(key)];
  6431. return result;
  6432. }
  6433. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6434. if (otherDictionary) {
  6435. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6436. if (_autocreator) {
  6437. GPBAutocreatedDictionaryModified(_autocreator, self);
  6438. }
  6439. }
  6440. }
  6441. - (void)setObject:(id)object forKey:(uint64_t)key {
  6442. [_dictionary setObject:object forKey:@(key)];
  6443. if (_autocreator) {
  6444. GPBAutocreatedDictionaryModified(_autocreator, self);
  6445. }
  6446. }
  6447. - (void)removeObjectForKey:(uint64_t)aKey {
  6448. [_dictionary removeObjectForKey:@(aKey)];
  6449. }
  6450. - (void)removeAll {
  6451. [_dictionary removeAllObjects];
  6452. }
  6453. @end
  6454. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6455. // This block of code is generated, do not edit it directly.
  6456. #pragma mark - Int64 -> UInt32
  6457. @implementation GPBInt64UInt32Dictionary {
  6458. @package
  6459. NSMutableDictionary *_dictionary;
  6460. }
  6461. + (instancetype)dictionary {
  6462. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6463. }
  6464. + (instancetype)dictionaryWithValue:(uint32_t)value
  6465. forKey:(int64_t)key {
  6466. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6467. // on to get the type correct.
  6468. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6469. forKeys:&key
  6470. count:1] autorelease];
  6471. }
  6472. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6473. forKeys:(const int64_t [])keys
  6474. count:(NSUInteger)count {
  6475. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6476. // on to get the type correct.
  6477. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6478. forKeys:keys
  6479. count:count] autorelease];
  6480. }
  6481. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6482. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6483. // on to get the type correct.
  6484. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6485. }
  6486. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6487. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6488. }
  6489. - (instancetype)init {
  6490. return [self initWithValues:NULL forKeys:NULL count:0];
  6491. }
  6492. - (instancetype)initWithValues:(const uint32_t [])values
  6493. forKeys:(const int64_t [])keys
  6494. count:(NSUInteger)count {
  6495. self = [super init];
  6496. if (self) {
  6497. _dictionary = [[NSMutableDictionary alloc] init];
  6498. if (count && values && keys) {
  6499. for (NSUInteger i = 0; i < count; ++i) {
  6500. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6501. }
  6502. }
  6503. }
  6504. return self;
  6505. }
  6506. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6507. self = [self initWithValues:NULL forKeys:NULL count:0];
  6508. if (self) {
  6509. if (dictionary) {
  6510. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6511. }
  6512. }
  6513. return self;
  6514. }
  6515. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6516. #pragma unused(numItems)
  6517. return [self initWithValues:NULL forKeys:NULL count:0];
  6518. }
  6519. - (void)dealloc {
  6520. NSAssert(!_autocreator,
  6521. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6522. [self class], _autocreator);
  6523. [_dictionary release];
  6524. [super dealloc];
  6525. }
  6526. - (instancetype)copyWithZone:(NSZone *)zone {
  6527. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6528. }
  6529. - (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
  6530. if (self == other) {
  6531. return YES;
  6532. }
  6533. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6534. return NO;
  6535. }
  6536. return [_dictionary isEqual:other->_dictionary];
  6537. }
  6538. - (NSUInteger)hash {
  6539. return _dictionary.count;
  6540. }
  6541. - (NSString *)description {
  6542. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6543. }
  6544. - (NSUInteger)count {
  6545. return _dictionary.count;
  6546. }
  6547. - (void)enumerateKeysAndValuesUsingBlock:
  6548. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6549. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6550. NSNumber *aValue,
  6551. BOOL *stop) {
  6552. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6553. }];
  6554. }
  6555. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6556. NSUInteger count = _dictionary.count;
  6557. if (count == 0) {
  6558. return 0;
  6559. }
  6560. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6561. GPBDataType keyDataType = field.mapKeyDataType;
  6562. __block size_t result = 0;
  6563. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6564. NSNumber *aValue,
  6565. BOOL *stop) {
  6566. #pragma unused(stop)
  6567. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6568. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6570. }];
  6571. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6572. result += tagSize * count;
  6573. return result;
  6574. }
  6575. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6576. asField:(GPBFieldDescriptor *)field {
  6577. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6578. GPBDataType keyDataType = field.mapKeyDataType;
  6579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6580. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6581. NSNumber *aValue,
  6582. BOOL *stop) {
  6583. #pragma unused(stop)
  6584. // Write the tag.
  6585. [outputStream writeInt32NoTag:tag];
  6586. // Write the size of the message.
  6587. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6588. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6589. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6590. // Write the fields.
  6591. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6592. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6593. }];
  6594. }
  6595. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6596. forGPBGenericValueKey:(GPBGenericValue *)key {
  6597. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6598. }
  6599. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6600. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6601. #pragma unused(stop)
  6602. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6603. }];
  6604. }
  6605. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6606. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6607. if (wrapped && value) {
  6608. *value = [wrapped unsignedIntValue];
  6609. }
  6610. return (wrapped != NULL);
  6611. }
  6612. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6613. if (otherDictionary) {
  6614. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6615. if (_autocreator) {
  6616. GPBAutocreatedDictionaryModified(_autocreator, self);
  6617. }
  6618. }
  6619. }
  6620. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6621. [_dictionary setObject:@(value) forKey:@(key)];
  6622. if (_autocreator) {
  6623. GPBAutocreatedDictionaryModified(_autocreator, self);
  6624. }
  6625. }
  6626. - (void)removeValueForKey:(int64_t)aKey {
  6627. [_dictionary removeObjectForKey:@(aKey)];
  6628. }
  6629. - (void)removeAll {
  6630. [_dictionary removeAllObjects];
  6631. }
  6632. @end
  6633. #pragma mark - Int64 -> Int32
  6634. @implementation GPBInt64Int32Dictionary {
  6635. @package
  6636. NSMutableDictionary *_dictionary;
  6637. }
  6638. + (instancetype)dictionary {
  6639. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6640. }
  6641. + (instancetype)dictionaryWithValue:(int32_t)value
  6642. forKey:(int64_t)key {
  6643. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6644. // on to get the type correct.
  6645. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6646. forKeys:&key
  6647. count:1] autorelease];
  6648. }
  6649. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6650. forKeys:(const int64_t [])keys
  6651. count:(NSUInteger)count {
  6652. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6653. // on to get the type correct.
  6654. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6655. forKeys:keys
  6656. count:count] autorelease];
  6657. }
  6658. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6659. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6660. // on to get the type correct.
  6661. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6662. }
  6663. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6664. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6665. }
  6666. - (instancetype)init {
  6667. return [self initWithValues:NULL forKeys:NULL count:0];
  6668. }
  6669. - (instancetype)initWithValues:(const int32_t [])values
  6670. forKeys:(const int64_t [])keys
  6671. count:(NSUInteger)count {
  6672. self = [super init];
  6673. if (self) {
  6674. _dictionary = [[NSMutableDictionary alloc] init];
  6675. if (count && values && keys) {
  6676. for (NSUInteger i = 0; i < count; ++i) {
  6677. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6678. }
  6679. }
  6680. }
  6681. return self;
  6682. }
  6683. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6684. self = [self initWithValues:NULL forKeys:NULL count:0];
  6685. if (self) {
  6686. if (dictionary) {
  6687. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6688. }
  6689. }
  6690. return self;
  6691. }
  6692. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6693. #pragma unused(numItems)
  6694. return [self initWithValues:NULL forKeys:NULL count:0];
  6695. }
  6696. - (void)dealloc {
  6697. NSAssert(!_autocreator,
  6698. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6699. [self class], _autocreator);
  6700. [_dictionary release];
  6701. [super dealloc];
  6702. }
  6703. - (instancetype)copyWithZone:(NSZone *)zone {
  6704. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6705. }
  6706. - (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
  6707. if (self == other) {
  6708. return YES;
  6709. }
  6710. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6711. return NO;
  6712. }
  6713. return [_dictionary isEqual:other->_dictionary];
  6714. }
  6715. - (NSUInteger)hash {
  6716. return _dictionary.count;
  6717. }
  6718. - (NSString *)description {
  6719. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6720. }
  6721. - (NSUInteger)count {
  6722. return _dictionary.count;
  6723. }
  6724. - (void)enumerateKeysAndValuesUsingBlock:
  6725. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6726. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6727. NSNumber *aValue,
  6728. BOOL *stop) {
  6729. block([aKey longLongValue], [aValue intValue], stop);
  6730. }];
  6731. }
  6732. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6733. NSUInteger count = _dictionary.count;
  6734. if (count == 0) {
  6735. return 0;
  6736. }
  6737. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6738. GPBDataType keyDataType = field.mapKeyDataType;
  6739. __block size_t result = 0;
  6740. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6741. NSNumber *aValue,
  6742. BOOL *stop) {
  6743. #pragma unused(stop)
  6744. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6745. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6746. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6747. }];
  6748. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6749. result += tagSize * count;
  6750. return result;
  6751. }
  6752. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6753. asField:(GPBFieldDescriptor *)field {
  6754. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6755. GPBDataType keyDataType = field.mapKeyDataType;
  6756. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6757. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6758. NSNumber *aValue,
  6759. BOOL *stop) {
  6760. #pragma unused(stop)
  6761. // Write the tag.
  6762. [outputStream writeInt32NoTag:tag];
  6763. // Write the size of the message.
  6764. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6765. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6766. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6767. // Write the fields.
  6768. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6769. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6770. }];
  6771. }
  6772. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6773. forGPBGenericValueKey:(GPBGenericValue *)key {
  6774. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6775. }
  6776. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6777. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6778. #pragma unused(stop)
  6779. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6780. }];
  6781. }
  6782. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6783. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6784. if (wrapped && value) {
  6785. *value = [wrapped intValue];
  6786. }
  6787. return (wrapped != NULL);
  6788. }
  6789. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6790. if (otherDictionary) {
  6791. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6792. if (_autocreator) {
  6793. GPBAutocreatedDictionaryModified(_autocreator, self);
  6794. }
  6795. }
  6796. }
  6797. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6798. [_dictionary setObject:@(value) forKey:@(key)];
  6799. if (_autocreator) {
  6800. GPBAutocreatedDictionaryModified(_autocreator, self);
  6801. }
  6802. }
  6803. - (void)removeValueForKey:(int64_t)aKey {
  6804. [_dictionary removeObjectForKey:@(aKey)];
  6805. }
  6806. - (void)removeAll {
  6807. [_dictionary removeAllObjects];
  6808. }
  6809. @end
  6810. #pragma mark - Int64 -> UInt64
  6811. @implementation GPBInt64UInt64Dictionary {
  6812. @package
  6813. NSMutableDictionary *_dictionary;
  6814. }
  6815. + (instancetype)dictionary {
  6816. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6817. }
  6818. + (instancetype)dictionaryWithValue:(uint64_t)value
  6819. forKey:(int64_t)key {
  6820. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6821. // on to get the type correct.
  6822. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6823. forKeys:&key
  6824. count:1] autorelease];
  6825. }
  6826. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6827. forKeys:(const int64_t [])keys
  6828. count:(NSUInteger)count {
  6829. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6830. // on to get the type correct.
  6831. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6832. forKeys:keys
  6833. count:count] autorelease];
  6834. }
  6835. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6836. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6837. // on to get the type correct.
  6838. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6839. }
  6840. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6841. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6842. }
  6843. - (instancetype)init {
  6844. return [self initWithValues:NULL forKeys:NULL count:0];
  6845. }
  6846. - (instancetype)initWithValues:(const uint64_t [])values
  6847. forKeys:(const int64_t [])keys
  6848. count:(NSUInteger)count {
  6849. self = [super init];
  6850. if (self) {
  6851. _dictionary = [[NSMutableDictionary alloc] init];
  6852. if (count && values && keys) {
  6853. for (NSUInteger i = 0; i < count; ++i) {
  6854. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6855. }
  6856. }
  6857. }
  6858. return self;
  6859. }
  6860. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6861. self = [self initWithValues:NULL forKeys:NULL count:0];
  6862. if (self) {
  6863. if (dictionary) {
  6864. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6865. }
  6866. }
  6867. return self;
  6868. }
  6869. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6870. #pragma unused(numItems)
  6871. return [self initWithValues:NULL forKeys:NULL count:0];
  6872. }
  6873. - (void)dealloc {
  6874. NSAssert(!_autocreator,
  6875. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6876. [self class], _autocreator);
  6877. [_dictionary release];
  6878. [super dealloc];
  6879. }
  6880. - (instancetype)copyWithZone:(NSZone *)zone {
  6881. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6882. }
  6883. - (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
  6884. if (self == other) {
  6885. return YES;
  6886. }
  6887. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6888. return NO;
  6889. }
  6890. return [_dictionary isEqual:other->_dictionary];
  6891. }
  6892. - (NSUInteger)hash {
  6893. return _dictionary.count;
  6894. }
  6895. - (NSString *)description {
  6896. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6897. }
  6898. - (NSUInteger)count {
  6899. return _dictionary.count;
  6900. }
  6901. - (void)enumerateKeysAndValuesUsingBlock:
  6902. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  6903. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6904. NSNumber *aValue,
  6905. BOOL *stop) {
  6906. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  6907. }];
  6908. }
  6909. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6910. NSUInteger count = _dictionary.count;
  6911. if (count == 0) {
  6912. return 0;
  6913. }
  6914. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6915. GPBDataType keyDataType = field.mapKeyDataType;
  6916. __block size_t result = 0;
  6917. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6918. NSNumber *aValue,
  6919. BOOL *stop) {
  6920. #pragma unused(stop)
  6921. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6922. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6923. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6924. }];
  6925. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6926. result += tagSize * count;
  6927. return result;
  6928. }
  6929. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6930. asField:(GPBFieldDescriptor *)field {
  6931. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6932. GPBDataType keyDataType = field.mapKeyDataType;
  6933. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6934. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6935. NSNumber *aValue,
  6936. BOOL *stop) {
  6937. #pragma unused(stop)
  6938. // Write the tag.
  6939. [outputStream writeInt32NoTag:tag];
  6940. // Write the size of the message.
  6941. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6942. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6943. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6944. // Write the fields.
  6945. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6946. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6947. }];
  6948. }
  6949. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6950. forGPBGenericValueKey:(GPBGenericValue *)key {
  6951. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6952. }
  6953. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6954. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  6955. #pragma unused(stop)
  6956. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6957. }];
  6958. }
  6959. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  6960. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6961. if (wrapped && value) {
  6962. *value = [wrapped unsignedLongLongValue];
  6963. }
  6964. return (wrapped != NULL);
  6965. }
  6966. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6967. if (otherDictionary) {
  6968. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6969. if (_autocreator) {
  6970. GPBAutocreatedDictionaryModified(_autocreator, self);
  6971. }
  6972. }
  6973. }
  6974. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  6975. [_dictionary setObject:@(value) forKey:@(key)];
  6976. if (_autocreator) {
  6977. GPBAutocreatedDictionaryModified(_autocreator, self);
  6978. }
  6979. }
  6980. - (void)removeValueForKey:(int64_t)aKey {
  6981. [_dictionary removeObjectForKey:@(aKey)];
  6982. }
  6983. - (void)removeAll {
  6984. [_dictionary removeAllObjects];
  6985. }
  6986. @end
  6987. #pragma mark - Int64 -> Int64
  6988. @implementation GPBInt64Int64Dictionary {
  6989. @package
  6990. NSMutableDictionary *_dictionary;
  6991. }
  6992. + (instancetype)dictionary {
  6993. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6994. }
  6995. + (instancetype)dictionaryWithValue:(int64_t)value
  6996. forKey:(int64_t)key {
  6997. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6998. // on to get the type correct.
  6999. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  7000. forKeys:&key
  7001. count:1] autorelease];
  7002. }
  7003. + (instancetype)dictionaryWithValues:(const int64_t [])values
  7004. forKeys:(const int64_t [])keys
  7005. count:(NSUInteger)count {
  7006. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7007. // on to get the type correct.
  7008. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  7009. forKeys:keys
  7010. count:count] autorelease];
  7011. }
  7012. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7013. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7014. // on to get the type correct.
  7015. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7016. }
  7017. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7018. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7019. }
  7020. - (instancetype)init {
  7021. return [self initWithValues:NULL forKeys:NULL count:0];
  7022. }
  7023. - (instancetype)initWithValues:(const int64_t [])values
  7024. forKeys:(const int64_t [])keys
  7025. count:(NSUInteger)count {
  7026. self = [super init];
  7027. if (self) {
  7028. _dictionary = [[NSMutableDictionary alloc] init];
  7029. if (count && values && keys) {
  7030. for (NSUInteger i = 0; i < count; ++i) {
  7031. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7032. }
  7033. }
  7034. }
  7035. return self;
  7036. }
  7037. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7038. self = [self initWithValues:NULL forKeys:NULL count:0];
  7039. if (self) {
  7040. if (dictionary) {
  7041. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7042. }
  7043. }
  7044. return self;
  7045. }
  7046. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7047. #pragma unused(numItems)
  7048. return [self initWithValues:NULL forKeys:NULL count:0];
  7049. }
  7050. - (void)dealloc {
  7051. NSAssert(!_autocreator,
  7052. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7053. [self class], _autocreator);
  7054. [_dictionary release];
  7055. [super dealloc];
  7056. }
  7057. - (instancetype)copyWithZone:(NSZone *)zone {
  7058. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7059. }
  7060. - (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
  7061. if (self == other) {
  7062. return YES;
  7063. }
  7064. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7065. return NO;
  7066. }
  7067. return [_dictionary isEqual:other->_dictionary];
  7068. }
  7069. - (NSUInteger)hash {
  7070. return _dictionary.count;
  7071. }
  7072. - (NSString *)description {
  7073. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7074. }
  7075. - (NSUInteger)count {
  7076. return _dictionary.count;
  7077. }
  7078. - (void)enumerateKeysAndValuesUsingBlock:
  7079. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7080. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7081. NSNumber *aValue,
  7082. BOOL *stop) {
  7083. block([aKey longLongValue], [aValue longLongValue], stop);
  7084. }];
  7085. }
  7086. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7087. NSUInteger count = _dictionary.count;
  7088. if (count == 0) {
  7089. return 0;
  7090. }
  7091. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7092. GPBDataType keyDataType = field.mapKeyDataType;
  7093. __block size_t result = 0;
  7094. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7095. NSNumber *aValue,
  7096. BOOL *stop) {
  7097. #pragma unused(stop)
  7098. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7099. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7100. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7101. }];
  7102. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7103. result += tagSize * count;
  7104. return result;
  7105. }
  7106. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7107. asField:(GPBFieldDescriptor *)field {
  7108. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7109. GPBDataType keyDataType = field.mapKeyDataType;
  7110. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7111. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7112. NSNumber *aValue,
  7113. BOOL *stop) {
  7114. #pragma unused(stop)
  7115. // Write the tag.
  7116. [outputStream writeInt32NoTag:tag];
  7117. // Write the size of the message.
  7118. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7119. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7120. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7121. // Write the fields.
  7122. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7123. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7124. }];
  7125. }
  7126. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7127. forGPBGenericValueKey:(GPBGenericValue *)key {
  7128. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7129. }
  7130. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7131. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7132. #pragma unused(stop)
  7133. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7134. }];
  7135. }
  7136. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  7137. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7138. if (wrapped && value) {
  7139. *value = [wrapped longLongValue];
  7140. }
  7141. return (wrapped != NULL);
  7142. }
  7143. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7144. if (otherDictionary) {
  7145. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7146. if (_autocreator) {
  7147. GPBAutocreatedDictionaryModified(_autocreator, self);
  7148. }
  7149. }
  7150. }
  7151. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  7152. [_dictionary setObject:@(value) forKey:@(key)];
  7153. if (_autocreator) {
  7154. GPBAutocreatedDictionaryModified(_autocreator, self);
  7155. }
  7156. }
  7157. - (void)removeValueForKey:(int64_t)aKey {
  7158. [_dictionary removeObjectForKey:@(aKey)];
  7159. }
  7160. - (void)removeAll {
  7161. [_dictionary removeAllObjects];
  7162. }
  7163. @end
  7164. #pragma mark - Int64 -> Bool
  7165. @implementation GPBInt64BoolDictionary {
  7166. @package
  7167. NSMutableDictionary *_dictionary;
  7168. }
  7169. + (instancetype)dictionary {
  7170. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7171. }
  7172. + (instancetype)dictionaryWithValue:(BOOL)value
  7173. forKey:(int64_t)key {
  7174. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7175. // on to get the type correct.
  7176. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  7177. forKeys:&key
  7178. count:1] autorelease];
  7179. }
  7180. + (instancetype)dictionaryWithValues:(const BOOL [])values
  7181. forKeys:(const int64_t [])keys
  7182. count:(NSUInteger)count {
  7183. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7184. // on to get the type correct.
  7185. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  7186. forKeys:keys
  7187. count:count] autorelease];
  7188. }
  7189. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7190. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7191. // on to get the type correct.
  7192. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7193. }
  7194. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7195. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7196. }
  7197. - (instancetype)init {
  7198. return [self initWithValues:NULL forKeys:NULL count:0];
  7199. }
  7200. - (instancetype)initWithValues:(const BOOL [])values
  7201. forKeys:(const int64_t [])keys
  7202. count:(NSUInteger)count {
  7203. self = [super init];
  7204. if (self) {
  7205. _dictionary = [[NSMutableDictionary alloc] init];
  7206. if (count && values && keys) {
  7207. for (NSUInteger i = 0; i < count; ++i) {
  7208. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7209. }
  7210. }
  7211. }
  7212. return self;
  7213. }
  7214. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7215. self = [self initWithValues:NULL forKeys:NULL count:0];
  7216. if (self) {
  7217. if (dictionary) {
  7218. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7219. }
  7220. }
  7221. return self;
  7222. }
  7223. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7224. #pragma unused(numItems)
  7225. return [self initWithValues:NULL forKeys:NULL count:0];
  7226. }
  7227. - (void)dealloc {
  7228. NSAssert(!_autocreator,
  7229. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7230. [self class], _autocreator);
  7231. [_dictionary release];
  7232. [super dealloc];
  7233. }
  7234. - (instancetype)copyWithZone:(NSZone *)zone {
  7235. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7236. }
  7237. - (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
  7238. if (self == other) {
  7239. return YES;
  7240. }
  7241. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7242. return NO;
  7243. }
  7244. return [_dictionary isEqual:other->_dictionary];
  7245. }
  7246. - (NSUInteger)hash {
  7247. return _dictionary.count;
  7248. }
  7249. - (NSString *)description {
  7250. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7251. }
  7252. - (NSUInteger)count {
  7253. return _dictionary.count;
  7254. }
  7255. - (void)enumerateKeysAndValuesUsingBlock:
  7256. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7258. NSNumber *aValue,
  7259. BOOL *stop) {
  7260. block([aKey longLongValue], [aValue boolValue], stop);
  7261. }];
  7262. }
  7263. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7264. NSUInteger count = _dictionary.count;
  7265. if (count == 0) {
  7266. return 0;
  7267. }
  7268. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7269. GPBDataType keyDataType = field.mapKeyDataType;
  7270. __block size_t result = 0;
  7271. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7272. NSNumber *aValue,
  7273. BOOL *stop) {
  7274. #pragma unused(stop)
  7275. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7276. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7277. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7278. }];
  7279. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7280. result += tagSize * count;
  7281. return result;
  7282. }
  7283. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7284. asField:(GPBFieldDescriptor *)field {
  7285. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7286. GPBDataType keyDataType = field.mapKeyDataType;
  7287. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7288. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7289. NSNumber *aValue,
  7290. BOOL *stop) {
  7291. #pragma unused(stop)
  7292. // Write the tag.
  7293. [outputStream writeInt32NoTag:tag];
  7294. // Write the size of the message.
  7295. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7296. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7297. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7298. // Write the fields.
  7299. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7300. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7301. }];
  7302. }
  7303. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7304. forGPBGenericValueKey:(GPBGenericValue *)key {
  7305. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7306. }
  7307. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7308. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7309. #pragma unused(stop)
  7310. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7311. }];
  7312. }
  7313. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  7314. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7315. if (wrapped && value) {
  7316. *value = [wrapped boolValue];
  7317. }
  7318. return (wrapped != NULL);
  7319. }
  7320. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7321. if (otherDictionary) {
  7322. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7323. if (_autocreator) {
  7324. GPBAutocreatedDictionaryModified(_autocreator, self);
  7325. }
  7326. }
  7327. }
  7328. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  7329. [_dictionary setObject:@(value) forKey:@(key)];
  7330. if (_autocreator) {
  7331. GPBAutocreatedDictionaryModified(_autocreator, self);
  7332. }
  7333. }
  7334. - (void)removeValueForKey:(int64_t)aKey {
  7335. [_dictionary removeObjectForKey:@(aKey)];
  7336. }
  7337. - (void)removeAll {
  7338. [_dictionary removeAllObjects];
  7339. }
  7340. @end
  7341. #pragma mark - Int64 -> Float
  7342. @implementation GPBInt64FloatDictionary {
  7343. @package
  7344. NSMutableDictionary *_dictionary;
  7345. }
  7346. + (instancetype)dictionary {
  7347. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7348. }
  7349. + (instancetype)dictionaryWithValue:(float)value
  7350. forKey:(int64_t)key {
  7351. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7352. // on to get the type correct.
  7353. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  7354. forKeys:&key
  7355. count:1] autorelease];
  7356. }
  7357. + (instancetype)dictionaryWithValues:(const float [])values
  7358. forKeys:(const int64_t [])keys
  7359. count:(NSUInteger)count {
  7360. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7361. // on to get the type correct.
  7362. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  7363. forKeys:keys
  7364. count:count] autorelease];
  7365. }
  7366. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7367. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7368. // on to get the type correct.
  7369. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7370. }
  7371. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7372. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7373. }
  7374. - (instancetype)init {
  7375. return [self initWithValues:NULL forKeys:NULL count:0];
  7376. }
  7377. - (instancetype)initWithValues:(const float [])values
  7378. forKeys:(const int64_t [])keys
  7379. count:(NSUInteger)count {
  7380. self = [super init];
  7381. if (self) {
  7382. _dictionary = [[NSMutableDictionary alloc] init];
  7383. if (count && values && keys) {
  7384. for (NSUInteger i = 0; i < count; ++i) {
  7385. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7386. }
  7387. }
  7388. }
  7389. return self;
  7390. }
  7391. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7392. self = [self initWithValues:NULL forKeys:NULL count:0];
  7393. if (self) {
  7394. if (dictionary) {
  7395. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7396. }
  7397. }
  7398. return self;
  7399. }
  7400. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7401. #pragma unused(numItems)
  7402. return [self initWithValues:NULL forKeys:NULL count:0];
  7403. }
  7404. - (void)dealloc {
  7405. NSAssert(!_autocreator,
  7406. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7407. [self class], _autocreator);
  7408. [_dictionary release];
  7409. [super dealloc];
  7410. }
  7411. - (instancetype)copyWithZone:(NSZone *)zone {
  7412. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7413. }
  7414. - (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
  7415. if (self == other) {
  7416. return YES;
  7417. }
  7418. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7419. return NO;
  7420. }
  7421. return [_dictionary isEqual:other->_dictionary];
  7422. }
  7423. - (NSUInteger)hash {
  7424. return _dictionary.count;
  7425. }
  7426. - (NSString *)description {
  7427. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7428. }
  7429. - (NSUInteger)count {
  7430. return _dictionary.count;
  7431. }
  7432. - (void)enumerateKeysAndValuesUsingBlock:
  7433. (void (^)(int64_t key, float value, BOOL *stop))block {
  7434. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7435. NSNumber *aValue,
  7436. BOOL *stop) {
  7437. block([aKey longLongValue], [aValue floatValue], stop);
  7438. }];
  7439. }
  7440. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7441. NSUInteger count = _dictionary.count;
  7442. if (count == 0) {
  7443. return 0;
  7444. }
  7445. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7446. GPBDataType keyDataType = field.mapKeyDataType;
  7447. __block size_t result = 0;
  7448. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7449. NSNumber *aValue,
  7450. BOOL *stop) {
  7451. #pragma unused(stop)
  7452. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7453. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7454. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7455. }];
  7456. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7457. result += tagSize * count;
  7458. return result;
  7459. }
  7460. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7461. asField:(GPBFieldDescriptor *)field {
  7462. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7463. GPBDataType keyDataType = field.mapKeyDataType;
  7464. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7465. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7466. NSNumber *aValue,
  7467. BOOL *stop) {
  7468. #pragma unused(stop)
  7469. // Write the tag.
  7470. [outputStream writeInt32NoTag:tag];
  7471. // Write the size of the message.
  7472. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7473. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7474. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7475. // Write the fields.
  7476. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7477. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7478. }];
  7479. }
  7480. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7481. forGPBGenericValueKey:(GPBGenericValue *)key {
  7482. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7483. }
  7484. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7485. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7486. #pragma unused(stop)
  7487. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7488. }];
  7489. }
  7490. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7491. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7492. if (wrapped && value) {
  7493. *value = [wrapped floatValue];
  7494. }
  7495. return (wrapped != NULL);
  7496. }
  7497. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7498. if (otherDictionary) {
  7499. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7500. if (_autocreator) {
  7501. GPBAutocreatedDictionaryModified(_autocreator, self);
  7502. }
  7503. }
  7504. }
  7505. - (void)setValue:(float)value forKey:(int64_t)key {
  7506. [_dictionary setObject:@(value) forKey:@(key)];
  7507. if (_autocreator) {
  7508. GPBAutocreatedDictionaryModified(_autocreator, self);
  7509. }
  7510. }
  7511. - (void)removeValueForKey:(int64_t)aKey {
  7512. [_dictionary removeObjectForKey:@(aKey)];
  7513. }
  7514. - (void)removeAll {
  7515. [_dictionary removeAllObjects];
  7516. }
  7517. @end
  7518. #pragma mark - Int64 -> Double
  7519. @implementation GPBInt64DoubleDictionary {
  7520. @package
  7521. NSMutableDictionary *_dictionary;
  7522. }
  7523. + (instancetype)dictionary {
  7524. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7525. }
  7526. + (instancetype)dictionaryWithValue:(double)value
  7527. forKey:(int64_t)key {
  7528. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7529. // on to get the type correct.
  7530. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7531. forKeys:&key
  7532. count:1] autorelease];
  7533. }
  7534. + (instancetype)dictionaryWithValues:(const double [])values
  7535. forKeys:(const int64_t [])keys
  7536. count:(NSUInteger)count {
  7537. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7538. // on to get the type correct.
  7539. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7540. forKeys:keys
  7541. count:count] autorelease];
  7542. }
  7543. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7544. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7545. // on to get the type correct.
  7546. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7547. }
  7548. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7549. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7550. }
  7551. - (instancetype)init {
  7552. return [self initWithValues:NULL forKeys:NULL count:0];
  7553. }
  7554. - (instancetype)initWithValues:(const double [])values
  7555. forKeys:(const int64_t [])keys
  7556. count:(NSUInteger)count {
  7557. self = [super init];
  7558. if (self) {
  7559. _dictionary = [[NSMutableDictionary alloc] init];
  7560. if (count && values && keys) {
  7561. for (NSUInteger i = 0; i < count; ++i) {
  7562. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7563. }
  7564. }
  7565. }
  7566. return self;
  7567. }
  7568. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7569. self = [self initWithValues:NULL forKeys:NULL count:0];
  7570. if (self) {
  7571. if (dictionary) {
  7572. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7573. }
  7574. }
  7575. return self;
  7576. }
  7577. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7578. #pragma unused(numItems)
  7579. return [self initWithValues:NULL forKeys:NULL count:0];
  7580. }
  7581. - (void)dealloc {
  7582. NSAssert(!_autocreator,
  7583. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7584. [self class], _autocreator);
  7585. [_dictionary release];
  7586. [super dealloc];
  7587. }
  7588. - (instancetype)copyWithZone:(NSZone *)zone {
  7589. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7590. }
  7591. - (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
  7592. if (self == other) {
  7593. return YES;
  7594. }
  7595. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7596. return NO;
  7597. }
  7598. return [_dictionary isEqual:other->_dictionary];
  7599. }
  7600. - (NSUInteger)hash {
  7601. return _dictionary.count;
  7602. }
  7603. - (NSString *)description {
  7604. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7605. }
  7606. - (NSUInteger)count {
  7607. return _dictionary.count;
  7608. }
  7609. - (void)enumerateKeysAndValuesUsingBlock:
  7610. (void (^)(int64_t key, double value, BOOL *stop))block {
  7611. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7612. NSNumber *aValue,
  7613. BOOL *stop) {
  7614. block([aKey longLongValue], [aValue doubleValue], stop);
  7615. }];
  7616. }
  7617. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7618. NSUInteger count = _dictionary.count;
  7619. if (count == 0) {
  7620. return 0;
  7621. }
  7622. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7623. GPBDataType keyDataType = field.mapKeyDataType;
  7624. __block size_t result = 0;
  7625. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7626. NSNumber *aValue,
  7627. BOOL *stop) {
  7628. #pragma unused(stop)
  7629. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7630. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7631. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7632. }];
  7633. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7634. result += tagSize * count;
  7635. return result;
  7636. }
  7637. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7638. asField:(GPBFieldDescriptor *)field {
  7639. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7640. GPBDataType keyDataType = field.mapKeyDataType;
  7641. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7642. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7643. NSNumber *aValue,
  7644. BOOL *stop) {
  7645. #pragma unused(stop)
  7646. // Write the tag.
  7647. [outputStream writeInt32NoTag:tag];
  7648. // Write the size of the message.
  7649. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7650. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7651. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7652. // Write the fields.
  7653. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7654. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7655. }];
  7656. }
  7657. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7658. forGPBGenericValueKey:(GPBGenericValue *)key {
  7659. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7660. }
  7661. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7662. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7663. #pragma unused(stop)
  7664. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7665. }];
  7666. }
  7667. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7668. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7669. if (wrapped && value) {
  7670. *value = [wrapped doubleValue];
  7671. }
  7672. return (wrapped != NULL);
  7673. }
  7674. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7675. if (otherDictionary) {
  7676. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7677. if (_autocreator) {
  7678. GPBAutocreatedDictionaryModified(_autocreator, self);
  7679. }
  7680. }
  7681. }
  7682. - (void)setValue:(double)value forKey:(int64_t)key {
  7683. [_dictionary setObject:@(value) forKey:@(key)];
  7684. if (_autocreator) {
  7685. GPBAutocreatedDictionaryModified(_autocreator, self);
  7686. }
  7687. }
  7688. - (void)removeValueForKey:(int64_t)aKey {
  7689. [_dictionary removeObjectForKey:@(aKey)];
  7690. }
  7691. - (void)removeAll {
  7692. [_dictionary removeAllObjects];
  7693. }
  7694. @end
  7695. #pragma mark - Int64 -> Enum
  7696. @implementation GPBInt64EnumDictionary {
  7697. @package
  7698. NSMutableDictionary *_dictionary;
  7699. GPBEnumValidationFunc _validationFunc;
  7700. }
  7701. @synthesize validationFunc = _validationFunc;
  7702. + (instancetype)dictionary {
  7703. return [[[self alloc] initWithValidationFunction:NULL
  7704. rawValues:NULL
  7705. forKeys:NULL
  7706. count:0] autorelease];
  7707. }
  7708. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7709. return [[[self alloc] initWithValidationFunction:func
  7710. rawValues:NULL
  7711. forKeys:NULL
  7712. count:0] autorelease];
  7713. }
  7714. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7715. rawValue:(int32_t)rawValue
  7716. forKey:(int64_t)key {
  7717. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7718. // on to get the type correct.
  7719. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7720. rawValues:&rawValue
  7721. forKeys:&key
  7722. count:1] autorelease];
  7723. }
  7724. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7725. rawValues:(const int32_t [])rawValues
  7726. forKeys:(const int64_t [])keys
  7727. count:(NSUInteger)count {
  7728. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7729. // on to get the type correct.
  7730. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7731. rawValues:rawValues
  7732. forKeys:keys
  7733. count:count] autorelease];
  7734. }
  7735. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7736. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7737. // on to get the type correct.
  7738. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7739. }
  7740. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7741. capacity:(NSUInteger)numItems {
  7742. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7743. }
  7744. - (instancetype)init {
  7745. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7746. }
  7747. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7748. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7749. }
  7750. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7751. rawValues:(const int32_t [])rawValues
  7752. forKeys:(const int64_t [])keys
  7753. count:(NSUInteger)count {
  7754. self = [super init];
  7755. if (self) {
  7756. _dictionary = [[NSMutableDictionary alloc] init];
  7757. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7758. if (count && rawValues && keys) {
  7759. for (NSUInteger i = 0; i < count; ++i) {
  7760. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7761. }
  7762. }
  7763. }
  7764. return self;
  7765. }
  7766. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7767. self = [self initWithValidationFunction:dictionary.validationFunc
  7768. rawValues:NULL
  7769. forKeys:NULL
  7770. count:0];
  7771. if (self) {
  7772. if (dictionary) {
  7773. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7774. }
  7775. }
  7776. return self;
  7777. }
  7778. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7779. capacity:(NSUInteger)numItems {
  7780. #pragma unused(numItems)
  7781. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7782. }
  7783. - (void)dealloc {
  7784. NSAssert(!_autocreator,
  7785. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7786. [self class], _autocreator);
  7787. [_dictionary release];
  7788. [super dealloc];
  7789. }
  7790. - (instancetype)copyWithZone:(NSZone *)zone {
  7791. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7792. }
  7793. - (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
  7794. if (self == other) {
  7795. return YES;
  7796. }
  7797. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7798. return NO;
  7799. }
  7800. return [_dictionary isEqual:other->_dictionary];
  7801. }
  7802. - (NSUInteger)hash {
  7803. return _dictionary.count;
  7804. }
  7805. - (NSString *)description {
  7806. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7807. }
  7808. - (NSUInteger)count {
  7809. return _dictionary.count;
  7810. }
  7811. - (void)enumerateKeysAndRawValuesUsingBlock:
  7812. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7813. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7814. NSNumber *aValue,
  7815. BOOL *stop) {
  7816. block([aKey longLongValue], [aValue intValue], stop);
  7817. }];
  7818. }
  7819. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7820. NSUInteger count = _dictionary.count;
  7821. if (count == 0) {
  7822. return 0;
  7823. }
  7824. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7825. GPBDataType keyDataType = field.mapKeyDataType;
  7826. __block size_t result = 0;
  7827. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7828. NSNumber *aValue,
  7829. BOOL *stop) {
  7830. #pragma unused(stop)
  7831. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7832. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7833. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7834. }];
  7835. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7836. result += tagSize * count;
  7837. return result;
  7838. }
  7839. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7840. asField:(GPBFieldDescriptor *)field {
  7841. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7842. GPBDataType keyDataType = field.mapKeyDataType;
  7843. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7844. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7845. NSNumber *aValue,
  7846. BOOL *stop) {
  7847. #pragma unused(stop)
  7848. // Write the tag.
  7849. [outputStream writeInt32NoTag:tag];
  7850. // Write the size of the message.
  7851. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7852. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7853. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7854. // Write the fields.
  7855. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7856. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7857. }];
  7858. }
  7859. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7860. forKey:(GPBGenericValue *)key
  7861. keyDataType:(GPBDataType)keyDataType {
  7862. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7863. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7864. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7865. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7866. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7867. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7868. [outputStream release];
  7869. return data;
  7870. }
  7871. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7872. forGPBGenericValueKey:(GPBGenericValue *)key {
  7873. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7874. }
  7875. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7876. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7877. #pragma unused(stop)
  7878. block([NSString stringWithFormat:@"%lld", key], @(value));
  7879. }];
  7880. }
  7881. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7882. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7883. if (wrapped && value) {
  7884. int32_t result = [wrapped intValue];
  7885. if (!_validationFunc(result)) {
  7886. result = kGPBUnrecognizedEnumeratorValue;
  7887. }
  7888. *value = result;
  7889. }
  7890. return (wrapped != NULL);
  7891. }
  7892. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  7893. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7894. if (wrapped && rawValue) {
  7895. *rawValue = [wrapped intValue];
  7896. }
  7897. return (wrapped != NULL);
  7898. }
  7899. - (void)enumerateKeysAndValuesUsingBlock:
  7900. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7901. GPBEnumValidationFunc func = _validationFunc;
  7902. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7903. NSNumber *aValue,
  7904. BOOL *stop) {
  7905. int32_t unwrapped = [aValue intValue];
  7906. if (!func(unwrapped)) {
  7907. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7908. }
  7909. block([aKey longLongValue], unwrapped, stop);
  7910. }];
  7911. }
  7912. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7913. if (otherDictionary) {
  7914. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7915. if (_autocreator) {
  7916. GPBAutocreatedDictionaryModified(_autocreator, self);
  7917. }
  7918. }
  7919. }
  7920. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7921. [_dictionary setObject:@(value) forKey:@(key)];
  7922. if (_autocreator) {
  7923. GPBAutocreatedDictionaryModified(_autocreator, self);
  7924. }
  7925. }
  7926. - (void)removeValueForKey:(int64_t)aKey {
  7927. [_dictionary removeObjectForKey:@(aKey)];
  7928. }
  7929. - (void)removeAll {
  7930. [_dictionary removeAllObjects];
  7931. }
  7932. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  7933. if (!_validationFunc(value)) {
  7934. [NSException raise:NSInvalidArgumentException
  7935. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7936. value];
  7937. }
  7938. [_dictionary setObject:@(value) forKey:@(key)];
  7939. if (_autocreator) {
  7940. GPBAutocreatedDictionaryModified(_autocreator, self);
  7941. }
  7942. }
  7943. @end
  7944. #pragma mark - Int64 -> Object
  7945. @implementation GPBInt64ObjectDictionary {
  7946. @package
  7947. NSMutableDictionary *_dictionary;
  7948. }
  7949. + (instancetype)dictionary {
  7950. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  7951. }
  7952. + (instancetype)dictionaryWithObject:(id)object
  7953. forKey:(int64_t)key {
  7954. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  7955. // on to get the type correct.
  7956. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  7957. forKeys:&key
  7958. count:1] autorelease];
  7959. }
  7960. + (instancetype)dictionaryWithObjects:(const id [])objects
  7961. forKeys:(const int64_t [])keys
  7962. count:(NSUInteger)count {
  7963. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  7964. // on to get the type correct.
  7965. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  7966. forKeys:keys
  7967. count:count] autorelease];
  7968. }
  7969. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7970. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7971. // on to get the type correct.
  7972. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7973. }
  7974. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7975. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7976. }
  7977. - (instancetype)init {
  7978. return [self initWithObjects:NULL forKeys:NULL count:0];
  7979. }
  7980. - (instancetype)initWithObjects:(const id [])objects
  7981. forKeys:(const int64_t [])keys
  7982. count:(NSUInteger)count {
  7983. self = [super init];
  7984. if (self) {
  7985. _dictionary = [[NSMutableDictionary alloc] init];
  7986. if (count && objects && keys) {
  7987. for (NSUInteger i = 0; i < count; ++i) {
  7988. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  7989. }
  7990. }
  7991. }
  7992. return self;
  7993. }
  7994. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7995. self = [self initWithObjects:NULL forKeys:NULL count:0];
  7996. if (self) {
  7997. if (dictionary) {
  7998. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7999. }
  8000. }
  8001. return self;
  8002. }
  8003. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8004. #pragma unused(numItems)
  8005. return [self initWithObjects:NULL forKeys:NULL count:0];
  8006. }
  8007. - (void)dealloc {
  8008. NSAssert(!_autocreator,
  8009. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8010. [self class], _autocreator);
  8011. [_dictionary release];
  8012. [super dealloc];
  8013. }
  8014. - (instancetype)copyWithZone:(NSZone *)zone {
  8015. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8016. }
  8017. - (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
  8018. if (self == other) {
  8019. return YES;
  8020. }
  8021. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8022. return NO;
  8023. }
  8024. return [_dictionary isEqual:other->_dictionary];
  8025. }
  8026. - (NSUInteger)hash {
  8027. return _dictionary.count;
  8028. }
  8029. - (NSString *)description {
  8030. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8031. }
  8032. - (NSUInteger)count {
  8033. return _dictionary.count;
  8034. }
  8035. - (void)enumerateKeysAndObjectsUsingBlock:
  8036. (void (^)(int64_t key, id object, BOOL *stop))block {
  8037. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8038. id aObject,
  8039. BOOL *stop) {
  8040. block([aKey longLongValue], aObject, stop);
  8041. }];
  8042. }
  8043. - (BOOL)isInitialized {
  8044. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8045. if (!msg.initialized) {
  8046. return NO;
  8047. }
  8048. }
  8049. return YES;
  8050. }
  8051. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8052. GPBInt64ObjectDictionary *newDict =
  8053. [[GPBInt64ObjectDictionary alloc] init];
  8054. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8055. GPBMessage *msg,
  8056. BOOL *stop) {
  8057. #pragma unused(stop)
  8058. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8059. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8060. [copiedMsg release];
  8061. }];
  8062. return newDict;
  8063. }
  8064. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8065. NSUInteger count = _dictionary.count;
  8066. if (count == 0) {
  8067. return 0;
  8068. }
  8069. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8070. GPBDataType keyDataType = field.mapKeyDataType;
  8071. __block size_t result = 0;
  8072. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8073. id aObject,
  8074. BOOL *stop) {
  8075. #pragma unused(stop)
  8076. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8077. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8078. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8079. }];
  8080. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8081. result += tagSize * count;
  8082. return result;
  8083. }
  8084. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8085. asField:(GPBFieldDescriptor *)field {
  8086. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8087. GPBDataType keyDataType = field.mapKeyDataType;
  8088. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8089. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8090. id aObject,
  8091. BOOL *stop) {
  8092. #pragma unused(stop)
  8093. // Write the tag.
  8094. [outputStream writeInt32NoTag:tag];
  8095. // Write the size of the message.
  8096. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8097. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8098. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8099. // Write the fields.
  8100. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8101. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  8102. }];
  8103. }
  8104. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8105. forGPBGenericValueKey:(GPBGenericValue *)key {
  8106. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8107. }
  8108. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8109. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8110. #pragma unused(stop)
  8111. block([NSString stringWithFormat:@"%lld", key], object);
  8112. }];
  8113. }
  8114. - (id)objectForKey:(int64_t)key {
  8115. id result = [_dictionary objectForKey:@(key)];
  8116. return result;
  8117. }
  8118. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8119. if (otherDictionary) {
  8120. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8121. if (_autocreator) {
  8122. GPBAutocreatedDictionaryModified(_autocreator, self);
  8123. }
  8124. }
  8125. }
  8126. - (void)setObject:(id)object forKey:(int64_t)key {
  8127. [_dictionary setObject:object forKey:@(key)];
  8128. if (_autocreator) {
  8129. GPBAutocreatedDictionaryModified(_autocreator, self);
  8130. }
  8131. }
  8132. - (void)removeObjectForKey:(int64_t)aKey {
  8133. [_dictionary removeObjectForKey:@(aKey)];
  8134. }
  8135. - (void)removeAll {
  8136. [_dictionary removeAllObjects];
  8137. }
  8138. @end
  8139. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8140. // This block of code is generated, do not edit it directly.
  8141. #pragma mark - String -> UInt32
  8142. @implementation GPBStringUInt32Dictionary {
  8143. @package
  8144. NSMutableDictionary *_dictionary;
  8145. }
  8146. + (instancetype)dictionary {
  8147. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8148. }
  8149. + (instancetype)dictionaryWithValue:(uint32_t)value
  8150. forKey:(NSString *)key {
  8151. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8152. // on to get the type correct.
  8153. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  8154. forKeys:&key
  8155. count:1] autorelease];
  8156. }
  8157. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  8158. forKeys:(const NSString * [])keys
  8159. count:(NSUInteger)count {
  8160. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8161. // on to get the type correct.
  8162. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  8163. forKeys:keys
  8164. count:count] autorelease];
  8165. }
  8166. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8167. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8168. // on to get the type correct.
  8169. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8170. }
  8171. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8172. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8173. }
  8174. - (instancetype)init {
  8175. return [self initWithValues:NULL forKeys:NULL count:0];
  8176. }
  8177. - (instancetype)initWithValues:(const uint32_t [])values
  8178. forKeys:(const NSString * [])keys
  8179. count:(NSUInteger)count {
  8180. self = [super init];
  8181. if (self) {
  8182. _dictionary = [[NSMutableDictionary alloc] init];
  8183. if (count && values && keys) {
  8184. for (NSUInteger i = 0; i < count; ++i) {
  8185. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8186. }
  8187. }
  8188. }
  8189. return self;
  8190. }
  8191. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8192. self = [self initWithValues:NULL forKeys:NULL count:0];
  8193. if (self) {
  8194. if (dictionary) {
  8195. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8196. }
  8197. }
  8198. return self;
  8199. }
  8200. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8201. #pragma unused(numItems)
  8202. return [self initWithValues:NULL forKeys:NULL count:0];
  8203. }
  8204. - (void)dealloc {
  8205. NSAssert(!_autocreator,
  8206. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8207. [self class], _autocreator);
  8208. [_dictionary release];
  8209. [super dealloc];
  8210. }
  8211. - (instancetype)copyWithZone:(NSZone *)zone {
  8212. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8213. }
  8214. - (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
  8215. if (self == other) {
  8216. return YES;
  8217. }
  8218. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8219. return NO;
  8220. }
  8221. return [_dictionary isEqual:other->_dictionary];
  8222. }
  8223. - (NSUInteger)hash {
  8224. return _dictionary.count;
  8225. }
  8226. - (NSString *)description {
  8227. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8228. }
  8229. - (NSUInteger)count {
  8230. return _dictionary.count;
  8231. }
  8232. - (void)enumerateKeysAndValuesUsingBlock:
  8233. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8234. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8235. NSNumber *aValue,
  8236. BOOL *stop) {
  8237. block(aKey, [aValue unsignedIntValue], stop);
  8238. }];
  8239. }
  8240. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8241. NSUInteger count = _dictionary.count;
  8242. if (count == 0) {
  8243. return 0;
  8244. }
  8245. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8246. GPBDataType keyDataType = field.mapKeyDataType;
  8247. __block size_t result = 0;
  8248. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8249. NSNumber *aValue,
  8250. BOOL *stop) {
  8251. #pragma unused(stop)
  8252. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8253. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8254. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8255. }];
  8256. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8257. result += tagSize * count;
  8258. return result;
  8259. }
  8260. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8261. asField:(GPBFieldDescriptor *)field {
  8262. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8263. GPBDataType keyDataType = field.mapKeyDataType;
  8264. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8265. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8266. NSNumber *aValue,
  8267. BOOL *stop) {
  8268. #pragma unused(stop)
  8269. // Write the tag.
  8270. [outputStream writeInt32NoTag:tag];
  8271. // Write the size of the message.
  8272. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8273. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8274. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8275. // Write the fields.
  8276. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8277. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8278. }];
  8279. }
  8280. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8281. forGPBGenericValueKey:(GPBGenericValue *)key {
  8282. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8283. }
  8284. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8285. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8286. #pragma unused(stop)
  8287. block(key, [NSString stringWithFormat:@"%u", value]);
  8288. }];
  8289. }
  8290. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  8291. NSNumber *wrapped = [_dictionary objectForKey:key];
  8292. if (wrapped && value) {
  8293. *value = [wrapped unsignedIntValue];
  8294. }
  8295. return (wrapped != NULL);
  8296. }
  8297. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8298. if (otherDictionary) {
  8299. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8300. if (_autocreator) {
  8301. GPBAutocreatedDictionaryModified(_autocreator, self);
  8302. }
  8303. }
  8304. }
  8305. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  8306. [_dictionary setObject:@(value) forKey:key];
  8307. if (_autocreator) {
  8308. GPBAutocreatedDictionaryModified(_autocreator, self);
  8309. }
  8310. }
  8311. - (void)removeValueForKey:(NSString *)aKey {
  8312. [_dictionary removeObjectForKey:aKey];
  8313. }
  8314. - (void)removeAll {
  8315. [_dictionary removeAllObjects];
  8316. }
  8317. @end
  8318. #pragma mark - String -> Int32
  8319. @implementation GPBStringInt32Dictionary {
  8320. @package
  8321. NSMutableDictionary *_dictionary;
  8322. }
  8323. + (instancetype)dictionary {
  8324. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8325. }
  8326. + (instancetype)dictionaryWithValue:(int32_t)value
  8327. forKey:(NSString *)key {
  8328. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8329. // on to get the type correct.
  8330. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  8331. forKeys:&key
  8332. count:1] autorelease];
  8333. }
  8334. + (instancetype)dictionaryWithValues:(const int32_t [])values
  8335. forKeys:(const NSString * [])keys
  8336. count:(NSUInteger)count {
  8337. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8338. // on to get the type correct.
  8339. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  8340. forKeys:keys
  8341. count:count] autorelease];
  8342. }
  8343. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8344. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8345. // on to get the type correct.
  8346. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8347. }
  8348. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8349. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8350. }
  8351. - (instancetype)init {
  8352. return [self initWithValues:NULL forKeys:NULL count:0];
  8353. }
  8354. - (instancetype)initWithValues:(const int32_t [])values
  8355. forKeys:(const NSString * [])keys
  8356. count:(NSUInteger)count {
  8357. self = [super init];
  8358. if (self) {
  8359. _dictionary = [[NSMutableDictionary alloc] init];
  8360. if (count && values && keys) {
  8361. for (NSUInteger i = 0; i < count; ++i) {
  8362. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8363. }
  8364. }
  8365. }
  8366. return self;
  8367. }
  8368. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8369. self = [self initWithValues:NULL forKeys:NULL count:0];
  8370. if (self) {
  8371. if (dictionary) {
  8372. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8373. }
  8374. }
  8375. return self;
  8376. }
  8377. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8378. #pragma unused(numItems)
  8379. return [self initWithValues:NULL forKeys:NULL count:0];
  8380. }
  8381. - (void)dealloc {
  8382. NSAssert(!_autocreator,
  8383. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8384. [self class], _autocreator);
  8385. [_dictionary release];
  8386. [super dealloc];
  8387. }
  8388. - (instancetype)copyWithZone:(NSZone *)zone {
  8389. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8390. }
  8391. - (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
  8392. if (self == other) {
  8393. return YES;
  8394. }
  8395. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8396. return NO;
  8397. }
  8398. return [_dictionary isEqual:other->_dictionary];
  8399. }
  8400. - (NSUInteger)hash {
  8401. return _dictionary.count;
  8402. }
  8403. - (NSString *)description {
  8404. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8405. }
  8406. - (NSUInteger)count {
  8407. return _dictionary.count;
  8408. }
  8409. - (void)enumerateKeysAndValuesUsingBlock:
  8410. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8411. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8412. NSNumber *aValue,
  8413. BOOL *stop) {
  8414. block(aKey, [aValue intValue], stop);
  8415. }];
  8416. }
  8417. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8418. NSUInteger count = _dictionary.count;
  8419. if (count == 0) {
  8420. return 0;
  8421. }
  8422. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8423. GPBDataType keyDataType = field.mapKeyDataType;
  8424. __block size_t result = 0;
  8425. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8426. NSNumber *aValue,
  8427. BOOL *stop) {
  8428. #pragma unused(stop)
  8429. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8430. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8431. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8432. }];
  8433. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8434. result += tagSize * count;
  8435. return result;
  8436. }
  8437. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8438. asField:(GPBFieldDescriptor *)field {
  8439. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8440. GPBDataType keyDataType = field.mapKeyDataType;
  8441. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8442. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8443. NSNumber *aValue,
  8444. BOOL *stop) {
  8445. #pragma unused(stop)
  8446. // Write the tag.
  8447. [outputStream writeInt32NoTag:tag];
  8448. // Write the size of the message.
  8449. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8450. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8451. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8452. // Write the fields.
  8453. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8454. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8455. }];
  8456. }
  8457. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8458. forGPBGenericValueKey:(GPBGenericValue *)key {
  8459. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8460. }
  8461. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8462. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8463. #pragma unused(stop)
  8464. block(key, [NSString stringWithFormat:@"%d", value]);
  8465. }];
  8466. }
  8467. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8468. NSNumber *wrapped = [_dictionary objectForKey:key];
  8469. if (wrapped && value) {
  8470. *value = [wrapped intValue];
  8471. }
  8472. return (wrapped != NULL);
  8473. }
  8474. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8475. if (otherDictionary) {
  8476. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8477. if (_autocreator) {
  8478. GPBAutocreatedDictionaryModified(_autocreator, self);
  8479. }
  8480. }
  8481. }
  8482. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8483. [_dictionary setObject:@(value) forKey:key];
  8484. if (_autocreator) {
  8485. GPBAutocreatedDictionaryModified(_autocreator, self);
  8486. }
  8487. }
  8488. - (void)removeValueForKey:(NSString *)aKey {
  8489. [_dictionary removeObjectForKey:aKey];
  8490. }
  8491. - (void)removeAll {
  8492. [_dictionary removeAllObjects];
  8493. }
  8494. @end
  8495. #pragma mark - String -> UInt64
  8496. @implementation GPBStringUInt64Dictionary {
  8497. @package
  8498. NSMutableDictionary *_dictionary;
  8499. }
  8500. + (instancetype)dictionary {
  8501. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8502. }
  8503. + (instancetype)dictionaryWithValue:(uint64_t)value
  8504. forKey:(NSString *)key {
  8505. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8506. // on to get the type correct.
  8507. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8508. forKeys:&key
  8509. count:1] autorelease];
  8510. }
  8511. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8512. forKeys:(const NSString * [])keys
  8513. count:(NSUInteger)count {
  8514. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8515. // on to get the type correct.
  8516. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8517. forKeys:keys
  8518. count:count] autorelease];
  8519. }
  8520. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8521. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8522. // on to get the type correct.
  8523. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8524. }
  8525. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8526. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8527. }
  8528. - (instancetype)init {
  8529. return [self initWithValues:NULL forKeys:NULL count:0];
  8530. }
  8531. - (instancetype)initWithValues:(const uint64_t [])values
  8532. forKeys:(const NSString * [])keys
  8533. count:(NSUInteger)count {
  8534. self = [super init];
  8535. if (self) {
  8536. _dictionary = [[NSMutableDictionary alloc] init];
  8537. if (count && values && keys) {
  8538. for (NSUInteger i = 0; i < count; ++i) {
  8539. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8540. }
  8541. }
  8542. }
  8543. return self;
  8544. }
  8545. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8546. self = [self initWithValues:NULL forKeys:NULL count:0];
  8547. if (self) {
  8548. if (dictionary) {
  8549. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8550. }
  8551. }
  8552. return self;
  8553. }
  8554. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8555. #pragma unused(numItems)
  8556. return [self initWithValues:NULL forKeys:NULL count:0];
  8557. }
  8558. - (void)dealloc {
  8559. NSAssert(!_autocreator,
  8560. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8561. [self class], _autocreator);
  8562. [_dictionary release];
  8563. [super dealloc];
  8564. }
  8565. - (instancetype)copyWithZone:(NSZone *)zone {
  8566. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8567. }
  8568. - (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
  8569. if (self == other) {
  8570. return YES;
  8571. }
  8572. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8573. return NO;
  8574. }
  8575. return [_dictionary isEqual:other->_dictionary];
  8576. }
  8577. - (NSUInteger)hash {
  8578. return _dictionary.count;
  8579. }
  8580. - (NSString *)description {
  8581. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8582. }
  8583. - (NSUInteger)count {
  8584. return _dictionary.count;
  8585. }
  8586. - (void)enumerateKeysAndValuesUsingBlock:
  8587. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8588. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8589. NSNumber *aValue,
  8590. BOOL *stop) {
  8591. block(aKey, [aValue unsignedLongLongValue], stop);
  8592. }];
  8593. }
  8594. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8595. NSUInteger count = _dictionary.count;
  8596. if (count == 0) {
  8597. return 0;
  8598. }
  8599. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8600. GPBDataType keyDataType = field.mapKeyDataType;
  8601. __block size_t result = 0;
  8602. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8603. NSNumber *aValue,
  8604. BOOL *stop) {
  8605. #pragma unused(stop)
  8606. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8607. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8608. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8609. }];
  8610. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8611. result += tagSize * count;
  8612. return result;
  8613. }
  8614. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8615. asField:(GPBFieldDescriptor *)field {
  8616. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8617. GPBDataType keyDataType = field.mapKeyDataType;
  8618. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8619. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8620. NSNumber *aValue,
  8621. BOOL *stop) {
  8622. #pragma unused(stop)
  8623. // Write the tag.
  8624. [outputStream writeInt32NoTag:tag];
  8625. // Write the size of the message.
  8626. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8627. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8628. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8629. // Write the fields.
  8630. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8631. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8632. }];
  8633. }
  8634. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8635. forGPBGenericValueKey:(GPBGenericValue *)key {
  8636. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8637. }
  8638. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8639. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8640. #pragma unused(stop)
  8641. block(key, [NSString stringWithFormat:@"%llu", value]);
  8642. }];
  8643. }
  8644. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8645. NSNumber *wrapped = [_dictionary objectForKey:key];
  8646. if (wrapped && value) {
  8647. *value = [wrapped unsignedLongLongValue];
  8648. }
  8649. return (wrapped != NULL);
  8650. }
  8651. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8652. if (otherDictionary) {
  8653. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8654. if (_autocreator) {
  8655. GPBAutocreatedDictionaryModified(_autocreator, self);
  8656. }
  8657. }
  8658. }
  8659. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8660. [_dictionary setObject:@(value) forKey:key];
  8661. if (_autocreator) {
  8662. GPBAutocreatedDictionaryModified(_autocreator, self);
  8663. }
  8664. }
  8665. - (void)removeValueForKey:(NSString *)aKey {
  8666. [_dictionary removeObjectForKey:aKey];
  8667. }
  8668. - (void)removeAll {
  8669. [_dictionary removeAllObjects];
  8670. }
  8671. @end
  8672. #pragma mark - String -> Int64
  8673. @implementation GPBStringInt64Dictionary {
  8674. @package
  8675. NSMutableDictionary *_dictionary;
  8676. }
  8677. + (instancetype)dictionary {
  8678. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8679. }
  8680. + (instancetype)dictionaryWithValue:(int64_t)value
  8681. forKey:(NSString *)key {
  8682. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8683. // on to get the type correct.
  8684. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8685. forKeys:&key
  8686. count:1] autorelease];
  8687. }
  8688. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8689. forKeys:(const NSString * [])keys
  8690. count:(NSUInteger)count {
  8691. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8692. // on to get the type correct.
  8693. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8694. forKeys:keys
  8695. count:count] autorelease];
  8696. }
  8697. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8698. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8699. // on to get the type correct.
  8700. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8701. }
  8702. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8703. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8704. }
  8705. - (instancetype)init {
  8706. return [self initWithValues:NULL forKeys:NULL count:0];
  8707. }
  8708. - (instancetype)initWithValues:(const int64_t [])values
  8709. forKeys:(const NSString * [])keys
  8710. count:(NSUInteger)count {
  8711. self = [super init];
  8712. if (self) {
  8713. _dictionary = [[NSMutableDictionary alloc] init];
  8714. if (count && values && keys) {
  8715. for (NSUInteger i = 0; i < count; ++i) {
  8716. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8717. }
  8718. }
  8719. }
  8720. return self;
  8721. }
  8722. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8723. self = [self initWithValues:NULL forKeys:NULL count:0];
  8724. if (self) {
  8725. if (dictionary) {
  8726. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8727. }
  8728. }
  8729. return self;
  8730. }
  8731. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8732. #pragma unused(numItems)
  8733. return [self initWithValues:NULL forKeys:NULL count:0];
  8734. }
  8735. - (void)dealloc {
  8736. NSAssert(!_autocreator,
  8737. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8738. [self class], _autocreator);
  8739. [_dictionary release];
  8740. [super dealloc];
  8741. }
  8742. - (instancetype)copyWithZone:(NSZone *)zone {
  8743. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8744. }
  8745. - (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
  8746. if (self == other) {
  8747. return YES;
  8748. }
  8749. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8750. return NO;
  8751. }
  8752. return [_dictionary isEqual:other->_dictionary];
  8753. }
  8754. - (NSUInteger)hash {
  8755. return _dictionary.count;
  8756. }
  8757. - (NSString *)description {
  8758. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8759. }
  8760. - (NSUInteger)count {
  8761. return _dictionary.count;
  8762. }
  8763. - (void)enumerateKeysAndValuesUsingBlock:
  8764. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8765. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8766. NSNumber *aValue,
  8767. BOOL *stop) {
  8768. block(aKey, [aValue longLongValue], stop);
  8769. }];
  8770. }
  8771. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8772. NSUInteger count = _dictionary.count;
  8773. if (count == 0) {
  8774. return 0;
  8775. }
  8776. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8777. GPBDataType keyDataType = field.mapKeyDataType;
  8778. __block size_t result = 0;
  8779. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8780. NSNumber *aValue,
  8781. BOOL *stop) {
  8782. #pragma unused(stop)
  8783. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8784. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8785. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8786. }];
  8787. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8788. result += tagSize * count;
  8789. return result;
  8790. }
  8791. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8792. asField:(GPBFieldDescriptor *)field {
  8793. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8794. GPBDataType keyDataType = field.mapKeyDataType;
  8795. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8796. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8797. NSNumber *aValue,
  8798. BOOL *stop) {
  8799. #pragma unused(stop)
  8800. // Write the tag.
  8801. [outputStream writeInt32NoTag:tag];
  8802. // Write the size of the message.
  8803. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8804. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8805. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8806. // Write the fields.
  8807. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8808. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8809. }];
  8810. }
  8811. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8812. forGPBGenericValueKey:(GPBGenericValue *)key {
  8813. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8814. }
  8815. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8816. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8817. #pragma unused(stop)
  8818. block(key, [NSString stringWithFormat:@"%lld", value]);
  8819. }];
  8820. }
  8821. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8822. NSNumber *wrapped = [_dictionary objectForKey:key];
  8823. if (wrapped && value) {
  8824. *value = [wrapped longLongValue];
  8825. }
  8826. return (wrapped != NULL);
  8827. }
  8828. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8829. if (otherDictionary) {
  8830. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8831. if (_autocreator) {
  8832. GPBAutocreatedDictionaryModified(_autocreator, self);
  8833. }
  8834. }
  8835. }
  8836. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8837. [_dictionary setObject:@(value) forKey:key];
  8838. if (_autocreator) {
  8839. GPBAutocreatedDictionaryModified(_autocreator, self);
  8840. }
  8841. }
  8842. - (void)removeValueForKey:(NSString *)aKey {
  8843. [_dictionary removeObjectForKey:aKey];
  8844. }
  8845. - (void)removeAll {
  8846. [_dictionary removeAllObjects];
  8847. }
  8848. @end
  8849. #pragma mark - String -> Bool
  8850. @implementation GPBStringBoolDictionary {
  8851. @package
  8852. NSMutableDictionary *_dictionary;
  8853. }
  8854. + (instancetype)dictionary {
  8855. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8856. }
  8857. + (instancetype)dictionaryWithValue:(BOOL)value
  8858. forKey:(NSString *)key {
  8859. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8860. // on to get the type correct.
  8861. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  8862. forKeys:&key
  8863. count:1] autorelease];
  8864. }
  8865. + (instancetype)dictionaryWithValues:(const BOOL [])values
  8866. forKeys:(const NSString * [])keys
  8867. count:(NSUInteger)count {
  8868. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8869. // on to get the type correct.
  8870. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  8871. forKeys:keys
  8872. count:count] autorelease];
  8873. }
  8874. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8875. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8876. // on to get the type correct.
  8877. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8878. }
  8879. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8880. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8881. }
  8882. - (instancetype)init {
  8883. return [self initWithValues:NULL forKeys:NULL count:0];
  8884. }
  8885. - (instancetype)initWithValues:(const BOOL [])values
  8886. forKeys:(const NSString * [])keys
  8887. count:(NSUInteger)count {
  8888. self = [super init];
  8889. if (self) {
  8890. _dictionary = [[NSMutableDictionary alloc] init];
  8891. if (count && values && keys) {
  8892. for (NSUInteger i = 0; i < count; ++i) {
  8893. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8894. }
  8895. }
  8896. }
  8897. return self;
  8898. }
  8899. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8900. self = [self initWithValues:NULL forKeys:NULL count:0];
  8901. if (self) {
  8902. if (dictionary) {
  8903. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8904. }
  8905. }
  8906. return self;
  8907. }
  8908. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8909. #pragma unused(numItems)
  8910. return [self initWithValues:NULL forKeys:NULL count:0];
  8911. }
  8912. - (void)dealloc {
  8913. NSAssert(!_autocreator,
  8914. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8915. [self class], _autocreator);
  8916. [_dictionary release];
  8917. [super dealloc];
  8918. }
  8919. - (instancetype)copyWithZone:(NSZone *)zone {
  8920. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8921. }
  8922. - (BOOL)isEqual:(GPBStringBoolDictionary *)other {
  8923. if (self == other) {
  8924. return YES;
  8925. }
  8926. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8927. return NO;
  8928. }
  8929. return [_dictionary isEqual:other->_dictionary];
  8930. }
  8931. - (NSUInteger)hash {
  8932. return _dictionary.count;
  8933. }
  8934. - (NSString *)description {
  8935. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8936. }
  8937. - (NSUInteger)count {
  8938. return _dictionary.count;
  8939. }
  8940. - (void)enumerateKeysAndValuesUsingBlock:
  8941. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  8942. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8943. NSNumber *aValue,
  8944. BOOL *stop) {
  8945. block(aKey, [aValue boolValue], stop);
  8946. }];
  8947. }
  8948. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8949. NSUInteger count = _dictionary.count;
  8950. if (count == 0) {
  8951. return 0;
  8952. }
  8953. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8954. GPBDataType keyDataType = field.mapKeyDataType;
  8955. __block size_t result = 0;
  8956. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8957. NSNumber *aValue,
  8958. BOOL *stop) {
  8959. #pragma unused(stop)
  8960. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8961. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8962. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8963. }];
  8964. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8965. result += tagSize * count;
  8966. return result;
  8967. }
  8968. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8969. asField:(GPBFieldDescriptor *)field {
  8970. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8971. GPBDataType keyDataType = field.mapKeyDataType;
  8972. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8973. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8974. NSNumber *aValue,
  8975. BOOL *stop) {
  8976. #pragma unused(stop)
  8977. // Write the tag.
  8978. [outputStream writeInt32NoTag:tag];
  8979. // Write the size of the message.
  8980. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8981. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8982. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8983. // Write the fields.
  8984. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8985. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  8986. }];
  8987. }
  8988. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8989. forGPBGenericValueKey:(GPBGenericValue *)key {
  8990. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8991. }
  8992. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8993. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  8994. #pragma unused(stop)
  8995. block(key, (value ? @"true" : @"false"));
  8996. }];
  8997. }
  8998. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  8999. NSNumber *wrapped = [_dictionary objectForKey:key];
  9000. if (wrapped && value) {
  9001. *value = [wrapped boolValue];
  9002. }
  9003. return (wrapped != NULL);
  9004. }
  9005. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9006. if (otherDictionary) {
  9007. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9008. if (_autocreator) {
  9009. GPBAutocreatedDictionaryModified(_autocreator, self);
  9010. }
  9011. }
  9012. }
  9013. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  9014. [_dictionary setObject:@(value) forKey:key];
  9015. if (_autocreator) {
  9016. GPBAutocreatedDictionaryModified(_autocreator, self);
  9017. }
  9018. }
  9019. - (void)removeValueForKey:(NSString *)aKey {
  9020. [_dictionary removeObjectForKey:aKey];
  9021. }
  9022. - (void)removeAll {
  9023. [_dictionary removeAllObjects];
  9024. }
  9025. @end
  9026. #pragma mark - String -> Float
  9027. @implementation GPBStringFloatDictionary {
  9028. @package
  9029. NSMutableDictionary *_dictionary;
  9030. }
  9031. + (instancetype)dictionary {
  9032. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9033. }
  9034. + (instancetype)dictionaryWithValue:(float)value
  9035. forKey:(NSString *)key {
  9036. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9037. // on to get the type correct.
  9038. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  9039. forKeys:&key
  9040. count:1] autorelease];
  9041. }
  9042. + (instancetype)dictionaryWithValues:(const float [])values
  9043. forKeys:(const NSString * [])keys
  9044. count:(NSUInteger)count {
  9045. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9046. // on to get the type correct.
  9047. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  9048. forKeys:keys
  9049. count:count] autorelease];
  9050. }
  9051. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9052. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9053. // on to get the type correct.
  9054. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9055. }
  9056. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9057. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9058. }
  9059. - (instancetype)init {
  9060. return [self initWithValues:NULL forKeys:NULL count:0];
  9061. }
  9062. - (instancetype)initWithValues:(const float [])values
  9063. forKeys:(const NSString * [])keys
  9064. count:(NSUInteger)count {
  9065. self = [super init];
  9066. if (self) {
  9067. _dictionary = [[NSMutableDictionary alloc] init];
  9068. if (count && values && keys) {
  9069. for (NSUInteger i = 0; i < count; ++i) {
  9070. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9071. }
  9072. }
  9073. }
  9074. return self;
  9075. }
  9076. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9077. self = [self initWithValues:NULL forKeys:NULL count:0];
  9078. if (self) {
  9079. if (dictionary) {
  9080. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9081. }
  9082. }
  9083. return self;
  9084. }
  9085. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9086. #pragma unused(numItems)
  9087. return [self initWithValues:NULL forKeys:NULL count:0];
  9088. }
  9089. - (void)dealloc {
  9090. NSAssert(!_autocreator,
  9091. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9092. [self class], _autocreator);
  9093. [_dictionary release];
  9094. [super dealloc];
  9095. }
  9096. - (instancetype)copyWithZone:(NSZone *)zone {
  9097. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9098. }
  9099. - (BOOL)isEqual:(GPBStringFloatDictionary *)other {
  9100. if (self == other) {
  9101. return YES;
  9102. }
  9103. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9104. return NO;
  9105. }
  9106. return [_dictionary isEqual:other->_dictionary];
  9107. }
  9108. - (NSUInteger)hash {
  9109. return _dictionary.count;
  9110. }
  9111. - (NSString *)description {
  9112. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9113. }
  9114. - (NSUInteger)count {
  9115. return _dictionary.count;
  9116. }
  9117. - (void)enumerateKeysAndValuesUsingBlock:
  9118. (void (^)(NSString *key, float value, BOOL *stop))block {
  9119. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9120. NSNumber *aValue,
  9121. BOOL *stop) {
  9122. block(aKey, [aValue floatValue], stop);
  9123. }];
  9124. }
  9125. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9126. NSUInteger count = _dictionary.count;
  9127. if (count == 0) {
  9128. return 0;
  9129. }
  9130. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9131. GPBDataType keyDataType = field.mapKeyDataType;
  9132. __block size_t result = 0;
  9133. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9134. NSNumber *aValue,
  9135. BOOL *stop) {
  9136. #pragma unused(stop)
  9137. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9138. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9139. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9140. }];
  9141. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9142. result += tagSize * count;
  9143. return result;
  9144. }
  9145. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9146. asField:(GPBFieldDescriptor *)field {
  9147. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9148. GPBDataType keyDataType = field.mapKeyDataType;
  9149. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9150. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9151. NSNumber *aValue,
  9152. BOOL *stop) {
  9153. #pragma unused(stop)
  9154. // Write the tag.
  9155. [outputStream writeInt32NoTag:tag];
  9156. // Write the size of the message.
  9157. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9158. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9159. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9160. // Write the fields.
  9161. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9162. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9163. }];
  9164. }
  9165. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9166. forGPBGenericValueKey:(GPBGenericValue *)key {
  9167. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9168. }
  9169. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9170. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9171. #pragma unused(stop)
  9172. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9173. }];
  9174. }
  9175. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  9176. NSNumber *wrapped = [_dictionary objectForKey:key];
  9177. if (wrapped && value) {
  9178. *value = [wrapped floatValue];
  9179. }
  9180. return (wrapped != NULL);
  9181. }
  9182. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9183. if (otherDictionary) {
  9184. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9185. if (_autocreator) {
  9186. GPBAutocreatedDictionaryModified(_autocreator, self);
  9187. }
  9188. }
  9189. }
  9190. - (void)setValue:(float)value forKey:(NSString *)key {
  9191. [_dictionary setObject:@(value) forKey:key];
  9192. if (_autocreator) {
  9193. GPBAutocreatedDictionaryModified(_autocreator, self);
  9194. }
  9195. }
  9196. - (void)removeValueForKey:(NSString *)aKey {
  9197. [_dictionary removeObjectForKey:aKey];
  9198. }
  9199. - (void)removeAll {
  9200. [_dictionary removeAllObjects];
  9201. }
  9202. @end
  9203. #pragma mark - String -> Double
  9204. @implementation GPBStringDoubleDictionary {
  9205. @package
  9206. NSMutableDictionary *_dictionary;
  9207. }
  9208. + (instancetype)dictionary {
  9209. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9210. }
  9211. + (instancetype)dictionaryWithValue:(double)value
  9212. forKey:(NSString *)key {
  9213. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9214. // on to get the type correct.
  9215. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  9216. forKeys:&key
  9217. count:1] autorelease];
  9218. }
  9219. + (instancetype)dictionaryWithValues:(const double [])values
  9220. forKeys:(const NSString * [])keys
  9221. count:(NSUInteger)count {
  9222. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9223. // on to get the type correct.
  9224. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  9225. forKeys:keys
  9226. count:count] autorelease];
  9227. }
  9228. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9229. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9230. // on to get the type correct.
  9231. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9232. }
  9233. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9234. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9235. }
  9236. - (instancetype)init {
  9237. return [self initWithValues:NULL forKeys:NULL count:0];
  9238. }
  9239. - (instancetype)initWithValues:(const double [])values
  9240. forKeys:(const NSString * [])keys
  9241. count:(NSUInteger)count {
  9242. self = [super init];
  9243. if (self) {
  9244. _dictionary = [[NSMutableDictionary alloc] init];
  9245. if (count && values && keys) {
  9246. for (NSUInteger i = 0; i < count; ++i) {
  9247. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9248. }
  9249. }
  9250. }
  9251. return self;
  9252. }
  9253. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9254. self = [self initWithValues: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 initWithValues: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 [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9275. }
  9276. - (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
  9277. if (self == other) {
  9278. return YES;
  9279. }
  9280. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9281. return NO;
  9282. }
  9283. return [_dictionary isEqual:other->_dictionary];
  9284. }
  9285. - (NSUInteger)hash {
  9286. return _dictionary.count;
  9287. }
  9288. - (NSString *)description {
  9289. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9290. }
  9291. - (NSUInteger)count {
  9292. return _dictionary.count;
  9293. }
  9294. - (void)enumerateKeysAndValuesUsingBlock:
  9295. (void (^)(NSString *key, double value, BOOL *stop))block {
  9296. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9297. NSNumber *aValue,
  9298. BOOL *stop) {
  9299. block(aKey, [aValue doubleValue], stop);
  9300. }];
  9301. }
  9302. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9303. NSUInteger count = _dictionary.count;
  9304. if (count == 0) {
  9305. return 0;
  9306. }
  9307. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9308. GPBDataType keyDataType = field.mapKeyDataType;
  9309. __block size_t result = 0;
  9310. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9311. NSNumber *aValue,
  9312. BOOL *stop) {
  9313. #pragma unused(stop)
  9314. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9315. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9316. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9317. }];
  9318. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9319. result += tagSize * count;
  9320. return result;
  9321. }
  9322. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9323. asField:(GPBFieldDescriptor *)field {
  9324. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9325. GPBDataType keyDataType = field.mapKeyDataType;
  9326. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9327. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9328. NSNumber *aValue,
  9329. BOOL *stop) {
  9330. #pragma unused(stop)
  9331. // Write the tag.
  9332. [outputStream writeInt32NoTag:tag];
  9333. // Write the size of the message.
  9334. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9335. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9336. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9337. // Write the fields.
  9338. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9339. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9340. }];
  9341. }
  9342. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9343. forGPBGenericValueKey:(GPBGenericValue *)key {
  9344. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9345. }
  9346. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9347. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9348. #pragma unused(stop)
  9349. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9350. }];
  9351. }
  9352. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  9353. NSNumber *wrapped = [_dictionary objectForKey:key];
  9354. if (wrapped && value) {
  9355. *value = [wrapped doubleValue];
  9356. }
  9357. return (wrapped != NULL);
  9358. }
  9359. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9360. if (otherDictionary) {
  9361. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9362. if (_autocreator) {
  9363. GPBAutocreatedDictionaryModified(_autocreator, self);
  9364. }
  9365. }
  9366. }
  9367. - (void)setValue:(double)value forKey:(NSString *)key {
  9368. [_dictionary setObject:@(value) forKey:key];
  9369. if (_autocreator) {
  9370. GPBAutocreatedDictionaryModified(_autocreator, self);
  9371. }
  9372. }
  9373. - (void)removeValueForKey:(NSString *)aKey {
  9374. [_dictionary removeObjectForKey:aKey];
  9375. }
  9376. - (void)removeAll {
  9377. [_dictionary removeAllObjects];
  9378. }
  9379. @end
  9380. #pragma mark - String -> Enum
  9381. @implementation GPBStringEnumDictionary {
  9382. @package
  9383. NSMutableDictionary *_dictionary;
  9384. GPBEnumValidationFunc _validationFunc;
  9385. }
  9386. @synthesize validationFunc = _validationFunc;
  9387. + (instancetype)dictionary {
  9388. return [[[self alloc] initWithValidationFunction:NULL
  9389. rawValues:NULL
  9390. forKeys:NULL
  9391. count:0] autorelease];
  9392. }
  9393. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9394. return [[[self alloc] initWithValidationFunction:func
  9395. rawValues:NULL
  9396. forKeys:NULL
  9397. count:0] autorelease];
  9398. }
  9399. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9400. rawValue:(int32_t)rawValue
  9401. forKey:(NSString *)key {
  9402. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9403. // on to get the type correct.
  9404. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9405. rawValues:&rawValue
  9406. forKeys:&key
  9407. count:1] autorelease];
  9408. }
  9409. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9410. rawValues:(const int32_t [])rawValues
  9411. forKeys:(const NSString * [])keys
  9412. count:(NSUInteger)count {
  9413. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9414. // on to get the type correct.
  9415. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9416. rawValues:rawValues
  9417. forKeys:keys
  9418. count:count] autorelease];
  9419. }
  9420. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9421. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9422. // on to get the type correct.
  9423. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9424. }
  9425. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9426. capacity:(NSUInteger)numItems {
  9427. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9428. }
  9429. - (instancetype)init {
  9430. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9431. }
  9432. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9433. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9434. }
  9435. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9436. rawValues:(const int32_t [])rawValues
  9437. forKeys:(const NSString * [])keys
  9438. count:(NSUInteger)count {
  9439. self = [super init];
  9440. if (self) {
  9441. _dictionary = [[NSMutableDictionary alloc] init];
  9442. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9443. if (count && rawValues && keys) {
  9444. for (NSUInteger i = 0; i < count; ++i) {
  9445. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9446. }
  9447. }
  9448. }
  9449. return self;
  9450. }
  9451. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9452. self = [self initWithValidationFunction:dictionary.validationFunc
  9453. rawValues:NULL
  9454. forKeys:NULL
  9455. count:0];
  9456. if (self) {
  9457. if (dictionary) {
  9458. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9459. }
  9460. }
  9461. return self;
  9462. }
  9463. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9464. capacity:(NSUInteger)numItems {
  9465. #pragma unused(numItems)
  9466. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9467. }
  9468. - (void)dealloc {
  9469. NSAssert(!_autocreator,
  9470. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9471. [self class], _autocreator);
  9472. [_dictionary release];
  9473. [super dealloc];
  9474. }
  9475. - (instancetype)copyWithZone:(NSZone *)zone {
  9476. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9477. }
  9478. - (BOOL)isEqual:(GPBStringEnumDictionary *)other {
  9479. if (self == other) {
  9480. return YES;
  9481. }
  9482. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9483. return NO;
  9484. }
  9485. return [_dictionary isEqual:other->_dictionary];
  9486. }
  9487. - (NSUInteger)hash {
  9488. return _dictionary.count;
  9489. }
  9490. - (NSString *)description {
  9491. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9492. }
  9493. - (NSUInteger)count {
  9494. return _dictionary.count;
  9495. }
  9496. - (void)enumerateKeysAndRawValuesUsingBlock:
  9497. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9498. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9499. NSNumber *aValue,
  9500. BOOL *stop) {
  9501. block(aKey, [aValue intValue], stop);
  9502. }];
  9503. }
  9504. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9505. NSUInteger count = _dictionary.count;
  9506. if (count == 0) {
  9507. return 0;
  9508. }
  9509. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9510. GPBDataType keyDataType = field.mapKeyDataType;
  9511. __block size_t result = 0;
  9512. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9513. NSNumber *aValue,
  9514. BOOL *stop) {
  9515. #pragma unused(stop)
  9516. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9517. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9518. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9519. }];
  9520. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9521. result += tagSize * count;
  9522. return result;
  9523. }
  9524. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9525. asField:(GPBFieldDescriptor *)field {
  9526. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9527. GPBDataType keyDataType = field.mapKeyDataType;
  9528. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9529. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9530. NSNumber *aValue,
  9531. BOOL *stop) {
  9532. #pragma unused(stop)
  9533. // Write the tag.
  9534. [outputStream writeInt32NoTag:tag];
  9535. // Write the size of the message.
  9536. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9537. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9538. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9539. // Write the fields.
  9540. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9541. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9542. }];
  9543. }
  9544. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9545. forKey:(GPBGenericValue *)key
  9546. keyDataType:(GPBDataType)keyDataType {
  9547. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9548. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9549. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9550. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9551. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9552. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9553. [outputStream release];
  9554. return data;
  9555. }
  9556. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9557. forGPBGenericValueKey:(GPBGenericValue *)key {
  9558. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9559. }
  9560. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9561. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9562. #pragma unused(stop)
  9563. block(key, @(value));
  9564. }];
  9565. }
  9566. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9567. NSNumber *wrapped = [_dictionary objectForKey:key];
  9568. if (wrapped && value) {
  9569. int32_t result = [wrapped intValue];
  9570. if (!_validationFunc(result)) {
  9571. result = kGPBUnrecognizedEnumeratorValue;
  9572. }
  9573. *value = result;
  9574. }
  9575. return (wrapped != NULL);
  9576. }
  9577. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9578. NSNumber *wrapped = [_dictionary objectForKey:key];
  9579. if (wrapped && rawValue) {
  9580. *rawValue = [wrapped intValue];
  9581. }
  9582. return (wrapped != NULL);
  9583. }
  9584. - (void)enumerateKeysAndValuesUsingBlock:
  9585. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9586. GPBEnumValidationFunc func = _validationFunc;
  9587. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9588. NSNumber *aValue,
  9589. BOOL *stop) {
  9590. int32_t unwrapped = [aValue intValue];
  9591. if (!func(unwrapped)) {
  9592. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9593. }
  9594. block(aKey, unwrapped, stop);
  9595. }];
  9596. }
  9597. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9598. if (otherDictionary) {
  9599. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9600. if (_autocreator) {
  9601. GPBAutocreatedDictionaryModified(_autocreator, self);
  9602. }
  9603. }
  9604. }
  9605. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9606. [_dictionary setObject:@(value) forKey:key];
  9607. if (_autocreator) {
  9608. GPBAutocreatedDictionaryModified(_autocreator, self);
  9609. }
  9610. }
  9611. - (void)removeValueForKey:(NSString *)aKey {
  9612. [_dictionary removeObjectForKey:aKey];
  9613. }
  9614. - (void)removeAll {
  9615. [_dictionary removeAllObjects];
  9616. }
  9617. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9618. if (!_validationFunc(value)) {
  9619. [NSException raise:NSInvalidArgumentException
  9620. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9621. value];
  9622. }
  9623. [_dictionary setObject:@(value) forKey:key];
  9624. if (_autocreator) {
  9625. GPBAutocreatedDictionaryModified(_autocreator, self);
  9626. }
  9627. }
  9628. @end
  9629. //%PDDM-EXPAND-END (5 expansions)
  9630. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9631. // This block of code is generated, do not edit it directly.
  9632. #pragma mark - Bool -> UInt32
  9633. @implementation GPBBoolUInt32Dictionary {
  9634. @package
  9635. uint32_t _values[2];
  9636. BOOL _valueSet[2];
  9637. }
  9638. + (instancetype)dictionary {
  9639. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9640. }
  9641. + (instancetype)dictionaryWithValue:(uint32_t)value
  9642. forKey:(BOOL)key {
  9643. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9644. // on to get the type correct.
  9645. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9646. forKeys:&key
  9647. count:1] autorelease];
  9648. }
  9649. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9650. forKeys:(const BOOL [])keys
  9651. count:(NSUInteger)count {
  9652. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9653. // on to get the type correct.
  9654. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9655. forKeys:keys
  9656. count:count] autorelease];
  9657. }
  9658. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9659. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9660. // on to get the type correct.
  9661. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9662. }
  9663. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9664. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9665. }
  9666. - (instancetype)init {
  9667. return [self initWithValues:NULL forKeys:NULL count:0];
  9668. }
  9669. - (instancetype)initWithValues:(const uint32_t [])values
  9670. forKeys:(const BOOL [])keys
  9671. count:(NSUInteger)count {
  9672. self = [super init];
  9673. if (self) {
  9674. for (NSUInteger i = 0; i < count; ++i) {
  9675. int idx = keys[i] ? 1 : 0;
  9676. _values[idx] = values[i];
  9677. _valueSet[idx] = YES;
  9678. }
  9679. }
  9680. return self;
  9681. }
  9682. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9683. self = [self initWithValues:NULL forKeys:NULL count:0];
  9684. if (self) {
  9685. if (dictionary) {
  9686. for (int i = 0; i < 2; ++i) {
  9687. if (dictionary->_valueSet[i]) {
  9688. _values[i] = dictionary->_values[i];
  9689. _valueSet[i] = YES;
  9690. }
  9691. }
  9692. }
  9693. }
  9694. return self;
  9695. }
  9696. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9697. #pragma unused(numItems)
  9698. return [self initWithValues:NULL forKeys:NULL count:0];
  9699. }
  9700. #if !defined(NS_BLOCK_ASSERTIONS)
  9701. - (void)dealloc {
  9702. NSAssert(!_autocreator,
  9703. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9704. [self class], _autocreator);
  9705. [super dealloc];
  9706. }
  9707. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9708. - (instancetype)copyWithZone:(NSZone *)zone {
  9709. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9710. }
  9711. - (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
  9712. if (self == other) {
  9713. return YES;
  9714. }
  9715. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9716. return NO;
  9717. }
  9718. if ((_valueSet[0] != other->_valueSet[0]) ||
  9719. (_valueSet[1] != other->_valueSet[1])) {
  9720. return NO;
  9721. }
  9722. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9723. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9724. return NO;
  9725. }
  9726. return YES;
  9727. }
  9728. - (NSUInteger)hash {
  9729. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9730. }
  9731. - (NSString *)description {
  9732. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9733. if (_valueSet[0]) {
  9734. [result appendFormat:@"NO: %u", _values[0]];
  9735. }
  9736. if (_valueSet[1]) {
  9737. [result appendFormat:@"YES: %u", _values[1]];
  9738. }
  9739. [result appendString:@" }"];
  9740. return result;
  9741. }
  9742. - (NSUInteger)count {
  9743. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9744. }
  9745. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9746. int idx = (key ? 1 : 0);
  9747. if (_valueSet[idx]) {
  9748. if (value) {
  9749. *value = _values[idx];
  9750. }
  9751. return YES;
  9752. }
  9753. return NO;
  9754. }
  9755. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9756. forGPBGenericValueKey:(GPBGenericValue *)key {
  9757. int idx = (key->valueBool ? 1 : 0);
  9758. _values[idx] = value->valueUInt32;
  9759. _valueSet[idx] = YES;
  9760. }
  9761. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9762. if (_valueSet[0]) {
  9763. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9764. }
  9765. if (_valueSet[1]) {
  9766. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9767. }
  9768. }
  9769. - (void)enumerateKeysAndValuesUsingBlock:
  9770. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9771. BOOL stop = NO;
  9772. if (_valueSet[0]) {
  9773. block(NO, _values[0], &stop);
  9774. }
  9775. if (!stop && _valueSet[1]) {
  9776. block(YES, _values[1], &stop);
  9777. }
  9778. }
  9779. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9780. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9781. NSUInteger count = 0;
  9782. size_t result = 0;
  9783. for (int i = 0; i < 2; ++i) {
  9784. if (_valueSet[i]) {
  9785. ++count;
  9786. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9787. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9788. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9789. }
  9790. }
  9791. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9792. result += tagSize * count;
  9793. return result;
  9794. }
  9795. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9796. asField:(GPBFieldDescriptor *)field {
  9797. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9798. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9799. for (int i = 0; i < 2; ++i) {
  9800. if (_valueSet[i]) {
  9801. // Write the tag.
  9802. [outputStream writeInt32NoTag:tag];
  9803. // Write the size of the message.
  9804. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9805. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9806. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9807. // Write the fields.
  9808. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9809. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9810. }
  9811. }
  9812. }
  9813. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9814. if (otherDictionary) {
  9815. for (int i = 0; i < 2; ++i) {
  9816. if (otherDictionary->_valueSet[i]) {
  9817. _valueSet[i] = YES;
  9818. _values[i] = otherDictionary->_values[i];
  9819. }
  9820. }
  9821. if (_autocreator) {
  9822. GPBAutocreatedDictionaryModified(_autocreator, self);
  9823. }
  9824. }
  9825. }
  9826. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  9827. int idx = (key ? 1 : 0);
  9828. _values[idx] = value;
  9829. _valueSet[idx] = YES;
  9830. if (_autocreator) {
  9831. GPBAutocreatedDictionaryModified(_autocreator, self);
  9832. }
  9833. }
  9834. - (void)removeValueForKey:(BOOL)aKey {
  9835. _valueSet[aKey ? 1 : 0] = NO;
  9836. }
  9837. - (void)removeAll {
  9838. _valueSet[0] = NO;
  9839. _valueSet[1] = NO;
  9840. }
  9841. @end
  9842. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9843. // This block of code is generated, do not edit it directly.
  9844. #pragma mark - Bool -> Int32
  9845. @implementation GPBBoolInt32Dictionary {
  9846. @package
  9847. int32_t _values[2];
  9848. BOOL _valueSet[2];
  9849. }
  9850. + (instancetype)dictionary {
  9851. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9852. }
  9853. + (instancetype)dictionaryWithValue:(int32_t)value
  9854. forKey:(BOOL)key {
  9855. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9856. // on to get the type correct.
  9857. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  9858. forKeys:&key
  9859. count:1] autorelease];
  9860. }
  9861. + (instancetype)dictionaryWithValues:(const int32_t [])values
  9862. forKeys:(const BOOL [])keys
  9863. count:(NSUInteger)count {
  9864. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9865. // on to get the type correct.
  9866. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  9867. forKeys:keys
  9868. count:count] autorelease];
  9869. }
  9870. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9871. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9872. // on to get the type correct.
  9873. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9874. }
  9875. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9876. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9877. }
  9878. - (instancetype)init {
  9879. return [self initWithValues:NULL forKeys:NULL count:0];
  9880. }
  9881. - (instancetype)initWithValues:(const int32_t [])values
  9882. forKeys:(const BOOL [])keys
  9883. count:(NSUInteger)count {
  9884. self = [super init];
  9885. if (self) {
  9886. for (NSUInteger i = 0; i < count; ++i) {
  9887. int idx = keys[i] ? 1 : 0;
  9888. _values[idx] = values[i];
  9889. _valueSet[idx] = YES;
  9890. }
  9891. }
  9892. return self;
  9893. }
  9894. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9895. self = [self initWithValues:NULL forKeys:NULL count:0];
  9896. if (self) {
  9897. if (dictionary) {
  9898. for (int i = 0; i < 2; ++i) {
  9899. if (dictionary->_valueSet[i]) {
  9900. _values[i] = dictionary->_values[i];
  9901. _valueSet[i] = YES;
  9902. }
  9903. }
  9904. }
  9905. }
  9906. return self;
  9907. }
  9908. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9909. #pragma unused(numItems)
  9910. return [self initWithValues:NULL forKeys:NULL count:0];
  9911. }
  9912. #if !defined(NS_BLOCK_ASSERTIONS)
  9913. - (void)dealloc {
  9914. NSAssert(!_autocreator,
  9915. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9916. [self class], _autocreator);
  9917. [super dealloc];
  9918. }
  9919. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9920. - (instancetype)copyWithZone:(NSZone *)zone {
  9921. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9922. }
  9923. - (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
  9924. if (self == other) {
  9925. return YES;
  9926. }
  9927. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  9928. return NO;
  9929. }
  9930. if ((_valueSet[0] != other->_valueSet[0]) ||
  9931. (_valueSet[1] != other->_valueSet[1])) {
  9932. return NO;
  9933. }
  9934. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9935. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9936. return NO;
  9937. }
  9938. return YES;
  9939. }
  9940. - (NSUInteger)hash {
  9941. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9942. }
  9943. - (NSString *)description {
  9944. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9945. if (_valueSet[0]) {
  9946. [result appendFormat:@"NO: %d", _values[0]];
  9947. }
  9948. if (_valueSet[1]) {
  9949. [result appendFormat:@"YES: %d", _values[1]];
  9950. }
  9951. [result appendString:@" }"];
  9952. return result;
  9953. }
  9954. - (NSUInteger)count {
  9955. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9956. }
  9957. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  9958. int idx = (key ? 1 : 0);
  9959. if (_valueSet[idx]) {
  9960. if (value) {
  9961. *value = _values[idx];
  9962. }
  9963. return YES;
  9964. }
  9965. return NO;
  9966. }
  9967. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9968. forGPBGenericValueKey:(GPBGenericValue *)key {
  9969. int idx = (key->valueBool ? 1 : 0);
  9970. _values[idx] = value->valueInt32;
  9971. _valueSet[idx] = YES;
  9972. }
  9973. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9974. if (_valueSet[0]) {
  9975. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9976. }
  9977. if (_valueSet[1]) {
  9978. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9979. }
  9980. }
  9981. - (void)enumerateKeysAndValuesUsingBlock:
  9982. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  9983. BOOL stop = NO;
  9984. if (_valueSet[0]) {
  9985. block(NO, _values[0], &stop);
  9986. }
  9987. if (!stop && _valueSet[1]) {
  9988. block(YES, _values[1], &stop);
  9989. }
  9990. }
  9991. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9992. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9993. NSUInteger count = 0;
  9994. size_t result = 0;
  9995. for (int i = 0; i < 2; ++i) {
  9996. if (_valueSet[i]) {
  9997. ++count;
  9998. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9999. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10000. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10001. }
  10002. }
  10003. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10004. result += tagSize * count;
  10005. return result;
  10006. }
  10007. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10008. asField:(GPBFieldDescriptor *)field {
  10009. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10010. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10011. for (int i = 0; i < 2; ++i) {
  10012. if (_valueSet[i]) {
  10013. // Write the tag.
  10014. [outputStream writeInt32NoTag:tag];
  10015. // Write the size of the message.
  10016. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10017. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10018. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10019. // Write the fields.
  10020. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10021. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10022. }
  10023. }
  10024. }
  10025. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10026. if (otherDictionary) {
  10027. for (int i = 0; i < 2; ++i) {
  10028. if (otherDictionary->_valueSet[i]) {
  10029. _valueSet[i] = YES;
  10030. _values[i] = otherDictionary->_values[i];
  10031. }
  10032. }
  10033. if (_autocreator) {
  10034. GPBAutocreatedDictionaryModified(_autocreator, self);
  10035. }
  10036. }
  10037. }
  10038. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  10039. int idx = (key ? 1 : 0);
  10040. _values[idx] = value;
  10041. _valueSet[idx] = YES;
  10042. if (_autocreator) {
  10043. GPBAutocreatedDictionaryModified(_autocreator, self);
  10044. }
  10045. }
  10046. - (void)removeValueForKey:(BOOL)aKey {
  10047. _valueSet[aKey ? 1 : 0] = NO;
  10048. }
  10049. - (void)removeAll {
  10050. _valueSet[0] = NO;
  10051. _valueSet[1] = NO;
  10052. }
  10053. @end
  10054. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10055. // This block of code is generated, do not edit it directly.
  10056. #pragma mark - Bool -> UInt64
  10057. @implementation GPBBoolUInt64Dictionary {
  10058. @package
  10059. uint64_t _values[2];
  10060. BOOL _valueSet[2];
  10061. }
  10062. + (instancetype)dictionary {
  10063. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10064. }
  10065. + (instancetype)dictionaryWithValue:(uint64_t)value
  10066. forKey:(BOOL)key {
  10067. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10068. // on to get the type correct.
  10069. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  10070. forKeys:&key
  10071. count:1] autorelease];
  10072. }
  10073. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  10074. forKeys:(const BOOL [])keys
  10075. count:(NSUInteger)count {
  10076. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10077. // on to get the type correct.
  10078. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  10079. forKeys:keys
  10080. count:count] autorelease];
  10081. }
  10082. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10083. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10084. // on to get the type correct.
  10085. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10086. }
  10087. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10088. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10089. }
  10090. - (instancetype)init {
  10091. return [self initWithValues:NULL forKeys:NULL count:0];
  10092. }
  10093. - (instancetype)initWithValues:(const uint64_t [])values
  10094. forKeys:(const BOOL [])keys
  10095. count:(NSUInteger)count {
  10096. self = [super init];
  10097. if (self) {
  10098. for (NSUInteger i = 0; i < count; ++i) {
  10099. int idx = keys[i] ? 1 : 0;
  10100. _values[idx] = values[i];
  10101. _valueSet[idx] = YES;
  10102. }
  10103. }
  10104. return self;
  10105. }
  10106. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10107. self = [self initWithValues:NULL forKeys:NULL count:0];
  10108. if (self) {
  10109. if (dictionary) {
  10110. for (int i = 0; i < 2; ++i) {
  10111. if (dictionary->_valueSet[i]) {
  10112. _values[i] = dictionary->_values[i];
  10113. _valueSet[i] = YES;
  10114. }
  10115. }
  10116. }
  10117. }
  10118. return self;
  10119. }
  10120. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10121. #pragma unused(numItems)
  10122. return [self initWithValues:NULL forKeys:NULL count:0];
  10123. }
  10124. #if !defined(NS_BLOCK_ASSERTIONS)
  10125. - (void)dealloc {
  10126. NSAssert(!_autocreator,
  10127. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10128. [self class], _autocreator);
  10129. [super dealloc];
  10130. }
  10131. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10132. - (instancetype)copyWithZone:(NSZone *)zone {
  10133. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10134. }
  10135. - (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
  10136. if (self == other) {
  10137. return YES;
  10138. }
  10139. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10140. return NO;
  10141. }
  10142. if ((_valueSet[0] != other->_valueSet[0]) ||
  10143. (_valueSet[1] != other->_valueSet[1])) {
  10144. return NO;
  10145. }
  10146. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10147. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10148. return NO;
  10149. }
  10150. return YES;
  10151. }
  10152. - (NSUInteger)hash {
  10153. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10154. }
  10155. - (NSString *)description {
  10156. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10157. if (_valueSet[0]) {
  10158. [result appendFormat:@"NO: %llu", _values[0]];
  10159. }
  10160. if (_valueSet[1]) {
  10161. [result appendFormat:@"YES: %llu", _values[1]];
  10162. }
  10163. [result appendString:@" }"];
  10164. return result;
  10165. }
  10166. - (NSUInteger)count {
  10167. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10168. }
  10169. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  10170. int idx = (key ? 1 : 0);
  10171. if (_valueSet[idx]) {
  10172. if (value) {
  10173. *value = _values[idx];
  10174. }
  10175. return YES;
  10176. }
  10177. return NO;
  10178. }
  10179. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10180. forGPBGenericValueKey:(GPBGenericValue *)key {
  10181. int idx = (key->valueBool ? 1 : 0);
  10182. _values[idx] = value->valueUInt64;
  10183. _valueSet[idx] = YES;
  10184. }
  10185. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10186. if (_valueSet[0]) {
  10187. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10188. }
  10189. if (_valueSet[1]) {
  10190. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10191. }
  10192. }
  10193. - (void)enumerateKeysAndValuesUsingBlock:
  10194. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10195. BOOL stop = NO;
  10196. if (_valueSet[0]) {
  10197. block(NO, _values[0], &stop);
  10198. }
  10199. if (!stop && _valueSet[1]) {
  10200. block(YES, _values[1], &stop);
  10201. }
  10202. }
  10203. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10204. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10205. NSUInteger count = 0;
  10206. size_t result = 0;
  10207. for (int i = 0; i < 2; ++i) {
  10208. if (_valueSet[i]) {
  10209. ++count;
  10210. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10211. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10212. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10213. }
  10214. }
  10215. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10216. result += tagSize * count;
  10217. return result;
  10218. }
  10219. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10220. asField:(GPBFieldDescriptor *)field {
  10221. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10222. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10223. for (int i = 0; i < 2; ++i) {
  10224. if (_valueSet[i]) {
  10225. // Write the tag.
  10226. [outputStream writeInt32NoTag:tag];
  10227. // Write the size of the message.
  10228. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10229. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10230. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10231. // Write the fields.
  10232. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10233. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10234. }
  10235. }
  10236. }
  10237. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10238. if (otherDictionary) {
  10239. for (int i = 0; i < 2; ++i) {
  10240. if (otherDictionary->_valueSet[i]) {
  10241. _valueSet[i] = YES;
  10242. _values[i] = otherDictionary->_values[i];
  10243. }
  10244. }
  10245. if (_autocreator) {
  10246. GPBAutocreatedDictionaryModified(_autocreator, self);
  10247. }
  10248. }
  10249. }
  10250. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  10251. int idx = (key ? 1 : 0);
  10252. _values[idx] = value;
  10253. _valueSet[idx] = YES;
  10254. if (_autocreator) {
  10255. GPBAutocreatedDictionaryModified(_autocreator, self);
  10256. }
  10257. }
  10258. - (void)removeValueForKey:(BOOL)aKey {
  10259. _valueSet[aKey ? 1 : 0] = NO;
  10260. }
  10261. - (void)removeAll {
  10262. _valueSet[0] = NO;
  10263. _valueSet[1] = NO;
  10264. }
  10265. @end
  10266. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10267. // This block of code is generated, do not edit it directly.
  10268. #pragma mark - Bool -> Int64
  10269. @implementation GPBBoolInt64Dictionary {
  10270. @package
  10271. int64_t _values[2];
  10272. BOOL _valueSet[2];
  10273. }
  10274. + (instancetype)dictionary {
  10275. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10276. }
  10277. + (instancetype)dictionaryWithValue:(int64_t)value
  10278. forKey:(BOOL)key {
  10279. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10280. // on to get the type correct.
  10281. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  10282. forKeys:&key
  10283. count:1] autorelease];
  10284. }
  10285. + (instancetype)dictionaryWithValues:(const int64_t [])values
  10286. forKeys:(const BOOL [])keys
  10287. count:(NSUInteger)count {
  10288. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10289. // on to get the type correct.
  10290. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  10291. forKeys:keys
  10292. count:count] autorelease];
  10293. }
  10294. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10295. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10296. // on to get the type correct.
  10297. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10298. }
  10299. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10300. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10301. }
  10302. - (instancetype)init {
  10303. return [self initWithValues:NULL forKeys:NULL count:0];
  10304. }
  10305. - (instancetype)initWithValues:(const int64_t [])values
  10306. forKeys:(const BOOL [])keys
  10307. count:(NSUInteger)count {
  10308. self = [super init];
  10309. if (self) {
  10310. for (NSUInteger i = 0; i < count; ++i) {
  10311. int idx = keys[i] ? 1 : 0;
  10312. _values[idx] = values[i];
  10313. _valueSet[idx] = YES;
  10314. }
  10315. }
  10316. return self;
  10317. }
  10318. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10319. self = [self initWithValues:NULL forKeys:NULL count:0];
  10320. if (self) {
  10321. if (dictionary) {
  10322. for (int i = 0; i < 2; ++i) {
  10323. if (dictionary->_valueSet[i]) {
  10324. _values[i] = dictionary->_values[i];
  10325. _valueSet[i] = YES;
  10326. }
  10327. }
  10328. }
  10329. }
  10330. return self;
  10331. }
  10332. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10333. #pragma unused(numItems)
  10334. return [self initWithValues:NULL forKeys:NULL count:0];
  10335. }
  10336. #if !defined(NS_BLOCK_ASSERTIONS)
  10337. - (void)dealloc {
  10338. NSAssert(!_autocreator,
  10339. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10340. [self class], _autocreator);
  10341. [super dealloc];
  10342. }
  10343. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10344. - (instancetype)copyWithZone:(NSZone *)zone {
  10345. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10346. }
  10347. - (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
  10348. if (self == other) {
  10349. return YES;
  10350. }
  10351. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10352. return NO;
  10353. }
  10354. if ((_valueSet[0] != other->_valueSet[0]) ||
  10355. (_valueSet[1] != other->_valueSet[1])) {
  10356. return NO;
  10357. }
  10358. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10359. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10360. return NO;
  10361. }
  10362. return YES;
  10363. }
  10364. - (NSUInteger)hash {
  10365. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10366. }
  10367. - (NSString *)description {
  10368. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10369. if (_valueSet[0]) {
  10370. [result appendFormat:@"NO: %lld", _values[0]];
  10371. }
  10372. if (_valueSet[1]) {
  10373. [result appendFormat:@"YES: %lld", _values[1]];
  10374. }
  10375. [result appendString:@" }"];
  10376. return result;
  10377. }
  10378. - (NSUInteger)count {
  10379. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10380. }
  10381. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  10382. int idx = (key ? 1 : 0);
  10383. if (_valueSet[idx]) {
  10384. if (value) {
  10385. *value = _values[idx];
  10386. }
  10387. return YES;
  10388. }
  10389. return NO;
  10390. }
  10391. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10392. forGPBGenericValueKey:(GPBGenericValue *)key {
  10393. int idx = (key->valueBool ? 1 : 0);
  10394. _values[idx] = value->valueInt64;
  10395. _valueSet[idx] = YES;
  10396. }
  10397. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10398. if (_valueSet[0]) {
  10399. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10400. }
  10401. if (_valueSet[1]) {
  10402. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10403. }
  10404. }
  10405. - (void)enumerateKeysAndValuesUsingBlock:
  10406. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10407. BOOL stop = NO;
  10408. if (_valueSet[0]) {
  10409. block(NO, _values[0], &stop);
  10410. }
  10411. if (!stop && _valueSet[1]) {
  10412. block(YES, _values[1], &stop);
  10413. }
  10414. }
  10415. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10416. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10417. NSUInteger count = 0;
  10418. size_t result = 0;
  10419. for (int i = 0; i < 2; ++i) {
  10420. if (_valueSet[i]) {
  10421. ++count;
  10422. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10423. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10424. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10425. }
  10426. }
  10427. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10428. result += tagSize * count;
  10429. return result;
  10430. }
  10431. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10432. asField:(GPBFieldDescriptor *)field {
  10433. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10434. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10435. for (int i = 0; i < 2; ++i) {
  10436. if (_valueSet[i]) {
  10437. // Write the tag.
  10438. [outputStream writeInt32NoTag:tag];
  10439. // Write the size of the message.
  10440. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10441. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10442. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10443. // Write the fields.
  10444. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10445. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10446. }
  10447. }
  10448. }
  10449. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10450. if (otherDictionary) {
  10451. for (int i = 0; i < 2; ++i) {
  10452. if (otherDictionary->_valueSet[i]) {
  10453. _valueSet[i] = YES;
  10454. _values[i] = otherDictionary->_values[i];
  10455. }
  10456. }
  10457. if (_autocreator) {
  10458. GPBAutocreatedDictionaryModified(_autocreator, self);
  10459. }
  10460. }
  10461. }
  10462. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  10463. int idx = (key ? 1 : 0);
  10464. _values[idx] = value;
  10465. _valueSet[idx] = YES;
  10466. if (_autocreator) {
  10467. GPBAutocreatedDictionaryModified(_autocreator, self);
  10468. }
  10469. }
  10470. - (void)removeValueForKey:(BOOL)aKey {
  10471. _valueSet[aKey ? 1 : 0] = NO;
  10472. }
  10473. - (void)removeAll {
  10474. _valueSet[0] = NO;
  10475. _valueSet[1] = NO;
  10476. }
  10477. @end
  10478. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10479. // This block of code is generated, do not edit it directly.
  10480. #pragma mark - Bool -> Bool
  10481. @implementation GPBBoolBoolDictionary {
  10482. @package
  10483. BOOL _values[2];
  10484. BOOL _valueSet[2];
  10485. }
  10486. + (instancetype)dictionary {
  10487. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10488. }
  10489. + (instancetype)dictionaryWithValue:(BOOL)value
  10490. forKey:(BOOL)key {
  10491. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10492. // on to get the type correct.
  10493. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  10494. forKeys:&key
  10495. count:1] autorelease];
  10496. }
  10497. + (instancetype)dictionaryWithValues:(const BOOL [])values
  10498. forKeys:(const BOOL [])keys
  10499. count:(NSUInteger)count {
  10500. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10501. // on to get the type correct.
  10502. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  10503. forKeys:keys
  10504. count:count] autorelease];
  10505. }
  10506. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10507. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10508. // on to get the type correct.
  10509. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10510. }
  10511. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10512. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10513. }
  10514. - (instancetype)init {
  10515. return [self initWithValues:NULL forKeys:NULL count:0];
  10516. }
  10517. - (instancetype)initWithValues:(const BOOL [])values
  10518. forKeys:(const BOOL [])keys
  10519. count:(NSUInteger)count {
  10520. self = [super init];
  10521. if (self) {
  10522. for (NSUInteger i = 0; i < count; ++i) {
  10523. int idx = keys[i] ? 1 : 0;
  10524. _values[idx] = values[i];
  10525. _valueSet[idx] = YES;
  10526. }
  10527. }
  10528. return self;
  10529. }
  10530. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10531. self = [self initWithValues:NULL forKeys:NULL count:0];
  10532. if (self) {
  10533. if (dictionary) {
  10534. for (int i = 0; i < 2; ++i) {
  10535. if (dictionary->_valueSet[i]) {
  10536. _values[i] = dictionary->_values[i];
  10537. _valueSet[i] = YES;
  10538. }
  10539. }
  10540. }
  10541. }
  10542. return self;
  10543. }
  10544. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10545. #pragma unused(numItems)
  10546. return [self initWithValues:NULL forKeys:NULL count:0];
  10547. }
  10548. #if !defined(NS_BLOCK_ASSERTIONS)
  10549. - (void)dealloc {
  10550. NSAssert(!_autocreator,
  10551. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10552. [self class], _autocreator);
  10553. [super dealloc];
  10554. }
  10555. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10556. - (instancetype)copyWithZone:(NSZone *)zone {
  10557. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10558. }
  10559. - (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
  10560. if (self == other) {
  10561. return YES;
  10562. }
  10563. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10564. return NO;
  10565. }
  10566. if ((_valueSet[0] != other->_valueSet[0]) ||
  10567. (_valueSet[1] != other->_valueSet[1])) {
  10568. return NO;
  10569. }
  10570. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10571. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10572. return NO;
  10573. }
  10574. return YES;
  10575. }
  10576. - (NSUInteger)hash {
  10577. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10578. }
  10579. - (NSString *)description {
  10580. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10581. if (_valueSet[0]) {
  10582. [result appendFormat:@"NO: %d", _values[0]];
  10583. }
  10584. if (_valueSet[1]) {
  10585. [result appendFormat:@"YES: %d", _values[1]];
  10586. }
  10587. [result appendString:@" }"];
  10588. return result;
  10589. }
  10590. - (NSUInteger)count {
  10591. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10592. }
  10593. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10594. int idx = (key ? 1 : 0);
  10595. if (_valueSet[idx]) {
  10596. if (value) {
  10597. *value = _values[idx];
  10598. }
  10599. return YES;
  10600. }
  10601. return NO;
  10602. }
  10603. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10604. forGPBGenericValueKey:(GPBGenericValue *)key {
  10605. int idx = (key->valueBool ? 1 : 0);
  10606. _values[idx] = value->valueBool;
  10607. _valueSet[idx] = YES;
  10608. }
  10609. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10610. if (_valueSet[0]) {
  10611. block(@"false", (_values[0] ? @"true" : @"false"));
  10612. }
  10613. if (_valueSet[1]) {
  10614. block(@"true", (_values[1] ? @"true" : @"false"));
  10615. }
  10616. }
  10617. - (void)enumerateKeysAndValuesUsingBlock:
  10618. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10619. BOOL stop = NO;
  10620. if (_valueSet[0]) {
  10621. block(NO, _values[0], &stop);
  10622. }
  10623. if (!stop && _valueSet[1]) {
  10624. block(YES, _values[1], &stop);
  10625. }
  10626. }
  10627. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10628. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10629. NSUInteger count = 0;
  10630. size_t result = 0;
  10631. for (int i = 0; i < 2; ++i) {
  10632. if (_valueSet[i]) {
  10633. ++count;
  10634. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10635. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10636. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10637. }
  10638. }
  10639. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10640. result += tagSize * count;
  10641. return result;
  10642. }
  10643. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10644. asField:(GPBFieldDescriptor *)field {
  10645. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10646. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10647. for (int i = 0; i < 2; ++i) {
  10648. if (_valueSet[i]) {
  10649. // Write the tag.
  10650. [outputStream writeInt32NoTag:tag];
  10651. // Write the size of the message.
  10652. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10653. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10654. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10655. // Write the fields.
  10656. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10657. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10658. }
  10659. }
  10660. }
  10661. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10662. if (otherDictionary) {
  10663. for (int i = 0; i < 2; ++i) {
  10664. if (otherDictionary->_valueSet[i]) {
  10665. _valueSet[i] = YES;
  10666. _values[i] = otherDictionary->_values[i];
  10667. }
  10668. }
  10669. if (_autocreator) {
  10670. GPBAutocreatedDictionaryModified(_autocreator, self);
  10671. }
  10672. }
  10673. }
  10674. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10675. int idx = (key ? 1 : 0);
  10676. _values[idx] = value;
  10677. _valueSet[idx] = YES;
  10678. if (_autocreator) {
  10679. GPBAutocreatedDictionaryModified(_autocreator, self);
  10680. }
  10681. }
  10682. - (void)removeValueForKey:(BOOL)aKey {
  10683. _valueSet[aKey ? 1 : 0] = NO;
  10684. }
  10685. - (void)removeAll {
  10686. _valueSet[0] = NO;
  10687. _valueSet[1] = NO;
  10688. }
  10689. @end
  10690. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10691. // This block of code is generated, do not edit it directly.
  10692. #pragma mark - Bool -> Float
  10693. @implementation GPBBoolFloatDictionary {
  10694. @package
  10695. float _values[2];
  10696. BOOL _valueSet[2];
  10697. }
  10698. + (instancetype)dictionary {
  10699. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10700. }
  10701. + (instancetype)dictionaryWithValue:(float)value
  10702. forKey:(BOOL)key {
  10703. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10704. // on to get the type correct.
  10705. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10706. forKeys:&key
  10707. count:1] autorelease];
  10708. }
  10709. + (instancetype)dictionaryWithValues:(const float [])values
  10710. forKeys:(const BOOL [])keys
  10711. count:(NSUInteger)count {
  10712. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10713. // on to get the type correct.
  10714. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10715. forKeys:keys
  10716. count:count] autorelease];
  10717. }
  10718. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10719. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10720. // on to get the type correct.
  10721. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10722. }
  10723. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10724. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10725. }
  10726. - (instancetype)init {
  10727. return [self initWithValues:NULL forKeys:NULL count:0];
  10728. }
  10729. - (instancetype)initWithValues:(const float [])values
  10730. forKeys:(const BOOL [])keys
  10731. count:(NSUInteger)count {
  10732. self = [super init];
  10733. if (self) {
  10734. for (NSUInteger i = 0; i < count; ++i) {
  10735. int idx = keys[i] ? 1 : 0;
  10736. _values[idx] = values[i];
  10737. _valueSet[idx] = YES;
  10738. }
  10739. }
  10740. return self;
  10741. }
  10742. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10743. self = [self initWithValues:NULL forKeys:NULL count:0];
  10744. if (self) {
  10745. if (dictionary) {
  10746. for (int i = 0; i < 2; ++i) {
  10747. if (dictionary->_valueSet[i]) {
  10748. _values[i] = dictionary->_values[i];
  10749. _valueSet[i] = YES;
  10750. }
  10751. }
  10752. }
  10753. }
  10754. return self;
  10755. }
  10756. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10757. #pragma unused(numItems)
  10758. return [self initWithValues:NULL forKeys:NULL count:0];
  10759. }
  10760. #if !defined(NS_BLOCK_ASSERTIONS)
  10761. - (void)dealloc {
  10762. NSAssert(!_autocreator,
  10763. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10764. [self class], _autocreator);
  10765. [super dealloc];
  10766. }
  10767. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10768. - (instancetype)copyWithZone:(NSZone *)zone {
  10769. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10770. }
  10771. - (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
  10772. if (self == other) {
  10773. return YES;
  10774. }
  10775. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10776. return NO;
  10777. }
  10778. if ((_valueSet[0] != other->_valueSet[0]) ||
  10779. (_valueSet[1] != other->_valueSet[1])) {
  10780. return NO;
  10781. }
  10782. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10783. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10784. return NO;
  10785. }
  10786. return YES;
  10787. }
  10788. - (NSUInteger)hash {
  10789. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10790. }
  10791. - (NSString *)description {
  10792. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10793. if (_valueSet[0]) {
  10794. [result appendFormat:@"NO: %f", _values[0]];
  10795. }
  10796. if (_valueSet[1]) {
  10797. [result appendFormat:@"YES: %f", _values[1]];
  10798. }
  10799. [result appendString:@" }"];
  10800. return result;
  10801. }
  10802. - (NSUInteger)count {
  10803. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10804. }
  10805. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  10806. int idx = (key ? 1 : 0);
  10807. if (_valueSet[idx]) {
  10808. if (value) {
  10809. *value = _values[idx];
  10810. }
  10811. return YES;
  10812. }
  10813. return NO;
  10814. }
  10815. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10816. forGPBGenericValueKey:(GPBGenericValue *)key {
  10817. int idx = (key->valueBool ? 1 : 0);
  10818. _values[idx] = value->valueFloat;
  10819. _valueSet[idx] = YES;
  10820. }
  10821. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10822. if (_valueSet[0]) {
  10823. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  10824. }
  10825. if (_valueSet[1]) {
  10826. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  10827. }
  10828. }
  10829. - (void)enumerateKeysAndValuesUsingBlock:
  10830. (void (^)(BOOL key, float value, BOOL *stop))block {
  10831. BOOL stop = NO;
  10832. if (_valueSet[0]) {
  10833. block(NO, _values[0], &stop);
  10834. }
  10835. if (!stop && _valueSet[1]) {
  10836. block(YES, _values[1], &stop);
  10837. }
  10838. }
  10839. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10840. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10841. NSUInteger count = 0;
  10842. size_t result = 0;
  10843. for (int i = 0; i < 2; ++i) {
  10844. if (_valueSet[i]) {
  10845. ++count;
  10846. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10847. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10848. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10849. }
  10850. }
  10851. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10852. result += tagSize * count;
  10853. return result;
  10854. }
  10855. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10856. asField:(GPBFieldDescriptor *)field {
  10857. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10858. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10859. for (int i = 0; i < 2; ++i) {
  10860. if (_valueSet[i]) {
  10861. // Write the tag.
  10862. [outputStream writeInt32NoTag:tag];
  10863. // Write the size of the message.
  10864. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10865. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10866. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10867. // Write the fields.
  10868. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10869. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10870. }
  10871. }
  10872. }
  10873. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  10874. if (otherDictionary) {
  10875. for (int i = 0; i < 2; ++i) {
  10876. if (otherDictionary->_valueSet[i]) {
  10877. _valueSet[i] = YES;
  10878. _values[i] = otherDictionary->_values[i];
  10879. }
  10880. }
  10881. if (_autocreator) {
  10882. GPBAutocreatedDictionaryModified(_autocreator, self);
  10883. }
  10884. }
  10885. }
  10886. - (void)setValue:(float)value forKey:(BOOL)key {
  10887. int idx = (key ? 1 : 0);
  10888. _values[idx] = value;
  10889. _valueSet[idx] = YES;
  10890. if (_autocreator) {
  10891. GPBAutocreatedDictionaryModified(_autocreator, self);
  10892. }
  10893. }
  10894. - (void)removeValueForKey:(BOOL)aKey {
  10895. _valueSet[aKey ? 1 : 0] = NO;
  10896. }
  10897. - (void)removeAll {
  10898. _valueSet[0] = NO;
  10899. _valueSet[1] = NO;
  10900. }
  10901. @end
  10902. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  10903. // This block of code is generated, do not edit it directly.
  10904. #pragma mark - Bool -> Double
  10905. @implementation GPBBoolDoubleDictionary {
  10906. @package
  10907. double _values[2];
  10908. BOOL _valueSet[2];
  10909. }
  10910. + (instancetype)dictionary {
  10911. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10912. }
  10913. + (instancetype)dictionaryWithValue:(double)value
  10914. forKey:(BOOL)key {
  10915. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10916. // on to get the type correct.
  10917. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  10918. forKeys:&key
  10919. count:1] autorelease];
  10920. }
  10921. + (instancetype)dictionaryWithValues:(const double [])values
  10922. forKeys:(const BOOL [])keys
  10923. count:(NSUInteger)count {
  10924. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10925. // on to get the type correct.
  10926. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  10927. forKeys:keys
  10928. count:count] autorelease];
  10929. }
  10930. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10931. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10932. // on to get the type correct.
  10933. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10934. }
  10935. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10936. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10937. }
  10938. - (instancetype)init {
  10939. return [self initWithValues:NULL forKeys:NULL count:0];
  10940. }
  10941. - (instancetype)initWithValues:(const double [])values
  10942. forKeys:(const BOOL [])keys
  10943. count:(NSUInteger)count {
  10944. self = [super init];
  10945. if (self) {
  10946. for (NSUInteger i = 0; i < count; ++i) {
  10947. int idx = keys[i] ? 1 : 0;
  10948. _values[idx] = values[i];
  10949. _valueSet[idx] = YES;
  10950. }
  10951. }
  10952. return self;
  10953. }
  10954. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10955. self = [self initWithValues:NULL forKeys:NULL count:0];
  10956. if (self) {
  10957. if (dictionary) {
  10958. for (int i = 0; i < 2; ++i) {
  10959. if (dictionary->_valueSet[i]) {
  10960. _values[i] = dictionary->_values[i];
  10961. _valueSet[i] = YES;
  10962. }
  10963. }
  10964. }
  10965. }
  10966. return self;
  10967. }
  10968. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10969. #pragma unused(numItems)
  10970. return [self initWithValues:NULL forKeys:NULL count:0];
  10971. }
  10972. #if !defined(NS_BLOCK_ASSERTIONS)
  10973. - (void)dealloc {
  10974. NSAssert(!_autocreator,
  10975. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10976. [self class], _autocreator);
  10977. [super dealloc];
  10978. }
  10979. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10980. - (instancetype)copyWithZone:(NSZone *)zone {
  10981. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  10982. }
  10983. - (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
  10984. if (self == other) {
  10985. return YES;
  10986. }
  10987. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  10988. return NO;
  10989. }
  10990. if ((_valueSet[0] != other->_valueSet[0]) ||
  10991. (_valueSet[1] != other->_valueSet[1])) {
  10992. return NO;
  10993. }
  10994. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10995. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10996. return NO;
  10997. }
  10998. return YES;
  10999. }
  11000. - (NSUInteger)hash {
  11001. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11002. }
  11003. - (NSString *)description {
  11004. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11005. if (_valueSet[0]) {
  11006. [result appendFormat:@"NO: %lf", _values[0]];
  11007. }
  11008. if (_valueSet[1]) {
  11009. [result appendFormat:@"YES: %lf", _values[1]];
  11010. }
  11011. [result appendString:@" }"];
  11012. return result;
  11013. }
  11014. - (NSUInteger)count {
  11015. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11016. }
  11017. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  11018. int idx = (key ? 1 : 0);
  11019. if (_valueSet[idx]) {
  11020. if (value) {
  11021. *value = _values[idx];
  11022. }
  11023. return YES;
  11024. }
  11025. return NO;
  11026. }
  11027. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11028. forGPBGenericValueKey:(GPBGenericValue *)key {
  11029. int idx = (key->valueBool ? 1 : 0);
  11030. _values[idx] = value->valueDouble;
  11031. _valueSet[idx] = YES;
  11032. }
  11033. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11034. if (_valueSet[0]) {
  11035. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11036. }
  11037. if (_valueSet[1]) {
  11038. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11039. }
  11040. }
  11041. - (void)enumerateKeysAndValuesUsingBlock:
  11042. (void (^)(BOOL key, double value, BOOL *stop))block {
  11043. BOOL stop = NO;
  11044. if (_valueSet[0]) {
  11045. block(NO, _values[0], &stop);
  11046. }
  11047. if (!stop && _valueSet[1]) {
  11048. block(YES, _values[1], &stop);
  11049. }
  11050. }
  11051. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11052. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11053. NSUInteger count = 0;
  11054. size_t result = 0;
  11055. for (int i = 0; i < 2; ++i) {
  11056. if (_valueSet[i]) {
  11057. ++count;
  11058. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11059. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11060. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11061. }
  11062. }
  11063. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11064. result += tagSize * count;
  11065. return result;
  11066. }
  11067. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11068. asField:(GPBFieldDescriptor *)field {
  11069. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11070. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11071. for (int i = 0; i < 2; ++i) {
  11072. if (_valueSet[i]) {
  11073. // Write the tag.
  11074. [outputStream writeInt32NoTag:tag];
  11075. // Write the size of the message.
  11076. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11077. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11078. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11079. // Write the fields.
  11080. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11081. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11082. }
  11083. }
  11084. }
  11085. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11086. if (otherDictionary) {
  11087. for (int i = 0; i < 2; ++i) {
  11088. if (otherDictionary->_valueSet[i]) {
  11089. _valueSet[i] = YES;
  11090. _values[i] = otherDictionary->_values[i];
  11091. }
  11092. }
  11093. if (_autocreator) {
  11094. GPBAutocreatedDictionaryModified(_autocreator, self);
  11095. }
  11096. }
  11097. }
  11098. - (void)setValue:(double)value forKey:(BOOL)key {
  11099. int idx = (key ? 1 : 0);
  11100. _values[idx] = value;
  11101. _valueSet[idx] = YES;
  11102. if (_autocreator) {
  11103. GPBAutocreatedDictionaryModified(_autocreator, self);
  11104. }
  11105. }
  11106. - (void)removeValueForKey:(BOOL)aKey {
  11107. _valueSet[aKey ? 1 : 0] = NO;
  11108. }
  11109. - (void)removeAll {
  11110. _valueSet[0] = NO;
  11111. _valueSet[1] = NO;
  11112. }
  11113. @end
  11114. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11115. // This block of code is generated, do not edit it directly.
  11116. #pragma mark - Bool -> Object
  11117. @implementation GPBBoolObjectDictionary {
  11118. @package
  11119. id _values[2];
  11120. }
  11121. + (instancetype)dictionary {
  11122. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11123. }
  11124. + (instancetype)dictionaryWithObject:(id)object
  11125. forKey:(BOOL)key {
  11126. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11127. // on to get the type correct.
  11128. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11129. forKeys:&key
  11130. count:1] autorelease];
  11131. }
  11132. + (instancetype)dictionaryWithObjects:(const id [])objects
  11133. forKeys:(const BOOL [])keys
  11134. count:(NSUInteger)count {
  11135. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11136. // on to get the type correct.
  11137. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11138. forKeys:keys
  11139. count:count] autorelease];
  11140. }
  11141. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11142. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11143. // on to get the type correct.
  11144. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11145. }
  11146. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11147. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11148. }
  11149. - (instancetype)init {
  11150. return [self initWithObjects:NULL forKeys:NULL count:0];
  11151. }
  11152. - (instancetype)initWithObjects:(const id [])objects
  11153. forKeys:(const BOOL [])keys
  11154. count:(NSUInteger)count {
  11155. self = [super init];
  11156. if (self) {
  11157. for (NSUInteger i = 0; i < count; ++i) {
  11158. int idx = keys[i] ? 1 : 0;
  11159. [_values[idx] release];
  11160. _values[idx] = (id)[objects[i] retain];
  11161. }
  11162. }
  11163. return self;
  11164. }
  11165. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11166. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11167. if (self) {
  11168. if (dictionary) {
  11169. _values[0] = [dictionary->_values[0] retain];
  11170. _values[1] = [dictionary->_values[1] retain];
  11171. }
  11172. }
  11173. return self;
  11174. }
  11175. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11176. #pragma unused(numItems)
  11177. return [self initWithObjects:NULL forKeys:NULL count:0];
  11178. }
  11179. - (void)dealloc {
  11180. NSAssert(!_autocreator,
  11181. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11182. [self class], _autocreator);
  11183. [_values[0] release];
  11184. [_values[1] release];
  11185. [super dealloc];
  11186. }
  11187. - (instancetype)copyWithZone:(NSZone *)zone {
  11188. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11189. }
  11190. - (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
  11191. if (self == other) {
  11192. return YES;
  11193. }
  11194. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11195. return NO;
  11196. }
  11197. if (((_values[0] != nil) != (other->_values[0] != nil)) ||
  11198. ((_values[1] != nil) != (other->_values[1] != nil))) {
  11199. return NO;
  11200. }
  11201. if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
  11202. ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
  11203. return NO;
  11204. }
  11205. return YES;
  11206. }
  11207. - (NSUInteger)hash {
  11208. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11209. }
  11210. - (NSString *)description {
  11211. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11212. if ((_values[0] != nil)) {
  11213. [result appendFormat:@"NO: %@", _values[0]];
  11214. }
  11215. if ((_values[1] != nil)) {
  11216. [result appendFormat:@"YES: %@", _values[1]];
  11217. }
  11218. [result appendString:@" }"];
  11219. return result;
  11220. }
  11221. - (NSUInteger)count {
  11222. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11223. }
  11224. - (id)objectForKey:(BOOL)key {
  11225. return _values[key ? 1 : 0];
  11226. }
  11227. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11228. forGPBGenericValueKey:(GPBGenericValue *)key {
  11229. int idx = (key->valueBool ? 1 : 0);
  11230. [_values[idx] release];
  11231. _values[idx] = [value->valueString retain];
  11232. }
  11233. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11234. if (_values[0] != nil) {
  11235. block(@"false", _values[0]);
  11236. }
  11237. if ((_values[1] != nil)) {
  11238. block(@"true", _values[1]);
  11239. }
  11240. }
  11241. - (void)enumerateKeysAndObjectsUsingBlock:
  11242. (void (^)(BOOL key, id object, BOOL *stop))block {
  11243. BOOL stop = NO;
  11244. if (_values[0] != nil) {
  11245. block(NO, _values[0], &stop);
  11246. }
  11247. if (!stop && (_values[1] != nil)) {
  11248. block(YES, _values[1], &stop);
  11249. }
  11250. }
  11251. - (BOOL)isInitialized {
  11252. if (_values[0] && ![_values[0] isInitialized]) {
  11253. return NO;
  11254. }
  11255. if (_values[1] && ![_values[1] isInitialized]) {
  11256. return NO;
  11257. }
  11258. return YES;
  11259. }
  11260. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11261. GPBBoolObjectDictionary *newDict =
  11262. [[GPBBoolObjectDictionary alloc] init];
  11263. for (int i = 0; i < 2; ++i) {
  11264. if (_values[i] != nil) {
  11265. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11266. }
  11267. }
  11268. return newDict;
  11269. }
  11270. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11271. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11272. NSUInteger count = 0;
  11273. size_t result = 0;
  11274. for (int i = 0; i < 2; ++i) {
  11275. if (_values[i] != nil) {
  11276. ++count;
  11277. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11278. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11279. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11280. }
  11281. }
  11282. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11283. result += tagSize * count;
  11284. return result;
  11285. }
  11286. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11287. asField:(GPBFieldDescriptor *)field {
  11288. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11289. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11290. for (int i = 0; i < 2; ++i) {
  11291. if (_values[i] != nil) {
  11292. // Write the tag.
  11293. [outputStream writeInt32NoTag:tag];
  11294. // Write the size of the message.
  11295. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11296. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11297. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11298. // Write the fields.
  11299. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11300. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11301. }
  11302. }
  11303. }
  11304. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11305. if (otherDictionary) {
  11306. for (int i = 0; i < 2; ++i) {
  11307. if (otherDictionary->_values[i] != nil) {
  11308. [_values[i] release];
  11309. _values[i] = [otherDictionary->_values[i] retain];
  11310. }
  11311. }
  11312. if (_autocreator) {
  11313. GPBAutocreatedDictionaryModified(_autocreator, self);
  11314. }
  11315. }
  11316. }
  11317. - (void)setObject:(id)object forKey:(BOOL)key {
  11318. int idx = (key ? 1 : 0);
  11319. [_values[idx] release];
  11320. _values[idx] = [object retain];
  11321. if (_autocreator) {
  11322. GPBAutocreatedDictionaryModified(_autocreator, self);
  11323. }
  11324. }
  11325. - (void)removeObjectForKey:(BOOL)aKey {
  11326. int idx = (aKey ? 1 : 0);
  11327. [_values[idx] release];
  11328. _values[idx] = nil;
  11329. }
  11330. - (void)removeAll {
  11331. for (int i = 0; i < 2; ++i) {
  11332. [_values[i] release];
  11333. _values[i] = nil;
  11334. }
  11335. }
  11336. @end
  11337. //%PDDM-EXPAND-END (8 expansions)
  11338. #pragma mark - Bool -> Enum
  11339. @implementation GPBBoolEnumDictionary {
  11340. @package
  11341. GPBEnumValidationFunc _validationFunc;
  11342. int32_t _values[2];
  11343. BOOL _valueSet[2];
  11344. }
  11345. @synthesize validationFunc = _validationFunc;
  11346. + (instancetype)dictionary {
  11347. return [[[self alloc] initWithValidationFunction:NULL
  11348. rawValues:NULL
  11349. forKeys:NULL
  11350. count:0] autorelease];
  11351. }
  11352. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11353. return [[[self alloc] initWithValidationFunction:func
  11354. rawValues:NULL
  11355. forKeys:NULL
  11356. count:0] autorelease];
  11357. }
  11358. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11359. rawValue:(int32_t)rawValue
  11360. forKey:(BOOL)key {
  11361. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11362. // on to get the type correct.
  11363. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11364. rawValues:&rawValue
  11365. forKeys:&key
  11366. count:1] autorelease];
  11367. }
  11368. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11369. rawValues:(const int32_t [])values
  11370. forKeys:(const BOOL [])keys
  11371. count:(NSUInteger)count {
  11372. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11373. // on to get the type correct.
  11374. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11375. rawValues:values
  11376. forKeys:keys
  11377. count:count] autorelease];
  11378. }
  11379. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11380. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11381. // on to get the type correct.
  11382. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11383. }
  11384. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11385. capacity:(NSUInteger)numItems {
  11386. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11387. }
  11388. - (instancetype)init {
  11389. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11390. }
  11391. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11392. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11393. }
  11394. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11395. rawValues:(const int32_t [])rawValues
  11396. forKeys:(const BOOL [])keys
  11397. count:(NSUInteger)count {
  11398. self = [super init];
  11399. if (self) {
  11400. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11401. for (NSUInteger i = 0; i < count; ++i) {
  11402. int idx = keys[i] ? 1 : 0;
  11403. _values[idx] = rawValues[i];
  11404. _valueSet[idx] = YES;
  11405. }
  11406. }
  11407. return self;
  11408. }
  11409. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11410. self = [self initWithValidationFunction:dictionary.validationFunc
  11411. rawValues:NULL
  11412. forKeys:NULL
  11413. count:0];
  11414. if (self) {
  11415. if (dictionary) {
  11416. for (int i = 0; i < 2; ++i) {
  11417. if (dictionary->_valueSet[i]) {
  11418. _values[i] = dictionary->_values[i];
  11419. _valueSet[i] = YES;
  11420. }
  11421. }
  11422. }
  11423. }
  11424. return self;
  11425. }
  11426. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11427. capacity:(NSUInteger)numItems {
  11428. #pragma unused(numItems)
  11429. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11430. }
  11431. #if !defined(NS_BLOCK_ASSERTIONS)
  11432. - (void)dealloc {
  11433. NSAssert(!_autocreator,
  11434. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11435. [self class], _autocreator);
  11436. [super dealloc];
  11437. }
  11438. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11439. - (instancetype)copyWithZone:(NSZone *)zone {
  11440. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11441. }
  11442. - (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
  11443. if (self == other) {
  11444. return YES;
  11445. }
  11446. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11447. return NO;
  11448. }
  11449. if ((_valueSet[0] != other->_valueSet[0]) ||
  11450. (_valueSet[1] != other->_valueSet[1])) {
  11451. return NO;
  11452. }
  11453. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11454. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11455. return NO;
  11456. }
  11457. return YES;
  11458. }
  11459. - (NSUInteger)hash {
  11460. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11461. }
  11462. - (NSString *)description {
  11463. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11464. if (_valueSet[0]) {
  11465. [result appendFormat:@"NO: %d", _values[0]];
  11466. }
  11467. if (_valueSet[1]) {
  11468. [result appendFormat:@"YES: %d", _values[1]];
  11469. }
  11470. [result appendString:@" }"];
  11471. return result;
  11472. }
  11473. - (NSUInteger)count {
  11474. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11475. }
  11476. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  11477. int idx = (key ? 1 : 0);
  11478. if (_valueSet[idx]) {
  11479. if (value) {
  11480. int32_t result = _values[idx];
  11481. if (!_validationFunc(result)) {
  11482. result = kGPBUnrecognizedEnumeratorValue;
  11483. }
  11484. *value = result;
  11485. }
  11486. return YES;
  11487. }
  11488. return NO;
  11489. }
  11490. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  11491. int idx = (key ? 1 : 0);
  11492. if (_valueSet[idx]) {
  11493. if (rawValue) {
  11494. *rawValue = _values[idx];
  11495. }
  11496. return YES;
  11497. }
  11498. return NO;
  11499. }
  11500. - (void)enumerateKeysAndValuesUsingBlock:
  11501. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11502. BOOL stop = NO;
  11503. if (_valueSet[0]) {
  11504. block(NO, _values[0], &stop);
  11505. }
  11506. if (!stop && _valueSet[1]) {
  11507. block(YES, _values[1], &stop);
  11508. }
  11509. }
  11510. - (void)enumerateKeysAndRawValuesUsingBlock:
  11511. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11512. BOOL stop = NO;
  11513. GPBEnumValidationFunc func = _validationFunc;
  11514. int32_t validatedValue;
  11515. if (_valueSet[0]) {
  11516. validatedValue = _values[0];
  11517. if (!func(validatedValue)) {
  11518. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11519. }
  11520. block(NO, validatedValue, &stop);
  11521. }
  11522. if (!stop && _valueSet[1]) {
  11523. validatedValue = _values[1];
  11524. if (!func(validatedValue)) {
  11525. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11526. }
  11527. block(YES, validatedValue, &stop);
  11528. }
  11529. }
  11530. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11531. // This block of code is generated, do not edit it directly.
  11532. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11533. forKey:(GPBGenericValue *)key
  11534. keyDataType:(GPBDataType)keyDataType {
  11535. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11536. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11537. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11538. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11539. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11540. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11541. [outputStream release];
  11542. return data;
  11543. }
  11544. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11545. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11546. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11547. NSUInteger count = 0;
  11548. size_t result = 0;
  11549. for (int i = 0; i < 2; ++i) {
  11550. if (_valueSet[i]) {
  11551. ++count;
  11552. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11553. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11554. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11555. }
  11556. }
  11557. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11558. result += tagSize * count;
  11559. return result;
  11560. }
  11561. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11562. asField:(GPBFieldDescriptor *)field {
  11563. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11564. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11565. for (int i = 0; i < 2; ++i) {
  11566. if (_valueSet[i]) {
  11567. // Write the tag.
  11568. [outputStream writeInt32NoTag:tag];
  11569. // Write the size of the message.
  11570. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11571. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11572. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11573. // Write the fields.
  11574. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11575. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11576. }
  11577. }
  11578. }
  11579. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11580. if (_valueSet[0]) {
  11581. block(@"false", @(_values[0]));
  11582. }
  11583. if (_valueSet[1]) {
  11584. block(@"true", @(_values[1]));
  11585. }
  11586. }
  11587. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11588. forGPBGenericValueKey:(GPBGenericValue *)key {
  11589. int idx = (key->valueBool ? 1 : 0);
  11590. _values[idx] = value->valueInt32;
  11591. _valueSet[idx] = YES;
  11592. }
  11593. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11594. if (otherDictionary) {
  11595. for (int i = 0; i < 2; ++i) {
  11596. if (otherDictionary->_valueSet[i]) {
  11597. _valueSet[i] = YES;
  11598. _values[i] = otherDictionary->_values[i];
  11599. }
  11600. }
  11601. if (_autocreator) {
  11602. GPBAutocreatedDictionaryModified(_autocreator, self);
  11603. }
  11604. }
  11605. }
  11606. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  11607. if (!_validationFunc(value)) {
  11608. [NSException raise:NSInvalidArgumentException
  11609. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11610. value];
  11611. }
  11612. int idx = (key ? 1 : 0);
  11613. _values[idx] = value;
  11614. _valueSet[idx] = YES;
  11615. if (_autocreator) {
  11616. GPBAutocreatedDictionaryModified(_autocreator, self);
  11617. }
  11618. }
  11619. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11620. int idx = (key ? 1 : 0);
  11621. _values[idx] = rawValue;
  11622. _valueSet[idx] = YES;
  11623. if (_autocreator) {
  11624. GPBAutocreatedDictionaryModified(_autocreator, self);
  11625. }
  11626. }
  11627. - (void)removeValueForKey:(BOOL)aKey {
  11628. _valueSet[aKey ? 1 : 0] = NO;
  11629. }
  11630. - (void)removeAll {
  11631. _valueSet[0] = NO;
  11632. _valueSet[1] = NO;
  11633. }
  11634. @end
  11635. #pragma mark - NSDictionary Subclass
  11636. @implementation GPBAutocreatedDictionary {
  11637. NSMutableDictionary *_dictionary;
  11638. }
  11639. - (void)dealloc {
  11640. NSAssert(!_autocreator,
  11641. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11642. [self class], _autocreator);
  11643. [_dictionary release];
  11644. [super dealloc];
  11645. }
  11646. #pragma mark Required NSDictionary overrides
  11647. - (instancetype)initWithObjects:(const id [])objects
  11648. forKeys:(const id<NSCopying> [])keys
  11649. count:(NSUInteger)count {
  11650. self = [super init];
  11651. if (self) {
  11652. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11653. forKeys:keys
  11654. count:count];
  11655. }
  11656. return self;
  11657. }
  11658. - (NSUInteger)count {
  11659. return [_dictionary count];
  11660. }
  11661. - (id)objectForKey:(id)aKey {
  11662. return [_dictionary objectForKey:aKey];
  11663. }
  11664. - (NSEnumerator *)keyEnumerator {
  11665. if (_dictionary == nil) {
  11666. _dictionary = [[NSMutableDictionary alloc] init];
  11667. }
  11668. return [_dictionary keyEnumerator];
  11669. }
  11670. #pragma mark Required NSMutableDictionary overrides
  11671. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11672. // since we only autocreate empty dictionaries.
  11673. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11674. if (_dictionary == nil) {
  11675. _dictionary = [[NSMutableDictionary alloc] init];
  11676. }
  11677. [_dictionary setObject:anObject forKey:aKey];
  11678. if (_autocreator) {
  11679. GPBAutocreatedDictionaryModified(_autocreator, self);
  11680. }
  11681. }
  11682. - (void)removeObjectForKey:(id)aKey {
  11683. [_dictionary removeObjectForKey:aKey];
  11684. }
  11685. #pragma mark Extra things hooked
  11686. - (id)copyWithZone:(NSZone *)zone {
  11687. if (_dictionary == nil) {
  11688. _dictionary = [[NSMutableDictionary alloc] init];
  11689. }
  11690. return [_dictionary copyWithZone:zone];
  11691. }
  11692. - (id)mutableCopyWithZone:(NSZone *)zone {
  11693. if (_dictionary == nil) {
  11694. _dictionary = [[NSMutableDictionary alloc] init];
  11695. }
  11696. return [_dictionary mutableCopyWithZone:zone];
  11697. }
  11698. - (id)objectForKeyedSubscript:(id)key {
  11699. return [_dictionary objectForKeyedSubscript:key];
  11700. }
  11701. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11702. if (_dictionary == nil) {
  11703. _dictionary = [[NSMutableDictionary alloc] init];
  11704. }
  11705. [_dictionary setObject:obj forKeyedSubscript:key];
  11706. if (_autocreator) {
  11707. GPBAutocreatedDictionaryModified(_autocreator, self);
  11708. }
  11709. }
  11710. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11711. id obj,
  11712. BOOL *stop))block {
  11713. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11714. }
  11715. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11716. usingBlock:(void (^)(id key,
  11717. id obj,
  11718. BOOL *stop))block {
  11719. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11720. }
  11721. @end