GPBDictionary.m 450 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336
  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, GPBTYPE_NAME1, GPBTYPE_NAME2)
  53. //%GPB_INLINE size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  54. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  55. //% return GPBCompute##GPBTYPE_NAME1##Size(fieldNum, value);
  56. //% } else if (wireType == GPBType##GPBTYPE_NAME2) {
  57. //% return GPBCompute##GPBTYPE_NAME2##Size(fieldNum, value);
  58. //% } else {
  59. //% NSCAssert(NO, @"Unexpected type %d", wireType);
  60. //% return 0;
  61. //% }
  62. //%}
  63. //%
  64. //%GPB_INLINE void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  65. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  66. //% [stream write##GPBTYPE_NAME1##:fieldNum value:value];
  67. //% } else if (wireType == GPBType##GPBTYPE_NAME2) {
  68. //% [stream write##GPBTYPE_NAME2##:fieldNum value:value];
  69. //% } else {
  70. //% NSCAssert(NO, @"Unexpected type %d", wireType);
  71. //% }
  72. //%}
  73. //%
  74. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBTYPE_NAME1, GPBTYPE_NAME2, GPBTYPE_NAME3)
  75. //%GPB_INLINE size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  76. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  77. //% return GPBCompute##GPBTYPE_NAME1##Size(fieldNum, value);
  78. //% } else if (wireType == GPBType##GPBTYPE_NAME2) {
  79. //% return GPBCompute##GPBTYPE_NAME2##Size(fieldNum, value);
  80. //% } else if (wireType == GPBType##GPBTYPE_NAME3) {
  81. //% return GPBCompute##GPBTYPE_NAME3##Size(fieldNum, value);
  82. //% } else {
  83. //% NSCAssert(NO, @"Unexpected type %d", wireType);
  84. //% return 0;
  85. //% }
  86. //%}
  87. //%
  88. //%GPB_INLINE void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBType wireType) {
  89. //% if (wireType == GPBType##GPBTYPE_NAME1) {
  90. //% [stream write##GPBTYPE_NAME1##:fieldNum value:value];
  91. //% } else if (wireType == GPBType##GPBTYPE_NAME2) {
  92. //% [stream write##GPBTYPE_NAME2##:fieldNum value:value];
  93. //% } else if (wireType == GPBType##GPBTYPE_NAME3) {
  94. //% [stream write##GPBTYPE_NAME3##:fieldNum value:value];
  95. //% } else {
  96. //% NSCAssert(NO, @"Unexpected type %d", wireType);
  97. //% }
  98. //%}
  99. //%
  100. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  101. //%GPB_INLINE size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBType wireType) {
  102. //% NSCAssert(wireType == GPBType##VALUE_NAME, @"bad type: %d", wireType);
  103. //% #pragma unused(wireType) // For when asserts are off in release.
  104. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  105. //%}
  106. //%
  107. //%GPB_INLINE void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBType wireType) {
  108. //% NSCAssert(wireType == GPBType##VALUE_NAME, @"bad type: %d", wireType);
  109. //% #pragma unused(wireType) // 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, Data)
  124. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  125. // This block of code is generated, do not edit it directly.
  126. GPB_INLINE size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBType wireType) {
  127. if (wireType == GPBTypeInt32) {
  128. return GPBComputeInt32Size(fieldNum, value);
  129. } else if (wireType == GPBTypeSInt32) {
  130. return GPBComputeSInt32Size(fieldNum, value);
  131. } else if (wireType == GPBTypeSFixed32) {
  132. return GPBComputeSFixed32Size(fieldNum, value);
  133. } else {
  134. NSCAssert(NO, @"Unexpected type %d", wireType);
  135. return 0;
  136. }
  137. }
  138. GPB_INLINE void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBType wireType) {
  139. if (wireType == GPBTypeInt32) {
  140. [stream writeInt32:fieldNum value:value];
  141. } else if (wireType == GPBTypeSInt32) {
  142. [stream writeSInt32:fieldNum value:value];
  143. } else if (wireType == GPBTypeSFixed32) {
  144. [stream writeSFixed32:fieldNum value:value];
  145. } else {
  146. NSCAssert(NO, @"Unexpected type %d", wireType);
  147. }
  148. }
  149. GPB_INLINE size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBType wireType) {
  150. if (wireType == GPBTypeUInt32) {
  151. return GPBComputeUInt32Size(fieldNum, value);
  152. } else if (wireType == GPBTypeFixed32) {
  153. return GPBComputeFixed32Size(fieldNum, value);
  154. } else {
  155. NSCAssert(NO, @"Unexpected type %d", wireType);
  156. return 0;
  157. }
  158. }
  159. GPB_INLINE void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBType wireType) {
  160. if (wireType == GPBTypeUInt32) {
  161. [stream writeUInt32:fieldNum value:value];
  162. } else if (wireType == GPBTypeFixed32) {
  163. [stream writeFixed32:fieldNum value:value];
  164. } else {
  165. NSCAssert(NO, @"Unexpected type %d", wireType);
  166. }
  167. }
  168. GPB_INLINE size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBType wireType) {
  169. if (wireType == GPBTypeInt64) {
  170. return GPBComputeInt64Size(fieldNum, value);
  171. } else if (wireType == GPBTypeSInt64) {
  172. return GPBComputeSInt64Size(fieldNum, value);
  173. } else if (wireType == GPBTypeSFixed64) {
  174. return GPBComputeSFixed64Size(fieldNum, value);
  175. } else {
  176. NSCAssert(NO, @"Unexpected type %d", wireType);
  177. return 0;
  178. }
  179. }
  180. GPB_INLINE void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBType wireType) {
  181. if (wireType == GPBTypeInt64) {
  182. [stream writeInt64:fieldNum value:value];
  183. } else if (wireType == GPBTypeSInt64) {
  184. [stream writeSInt64:fieldNum value:value];
  185. } else if (wireType == GPBTypeSFixed64) {
  186. [stream writeSFixed64:fieldNum value:value];
  187. } else {
  188. NSCAssert(NO, @"Unexpected type %d", wireType);
  189. }
  190. }
  191. GPB_INLINE size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBType wireType) {
  192. if (wireType == GPBTypeUInt64) {
  193. return GPBComputeUInt64Size(fieldNum, value);
  194. } else if (wireType == GPBTypeFixed64) {
  195. return GPBComputeFixed64Size(fieldNum, value);
  196. } else {
  197. NSCAssert(NO, @"Unexpected type %d", wireType);
  198. return 0;
  199. }
  200. }
  201. GPB_INLINE void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBType wireType) {
  202. if (wireType == GPBTypeUInt64) {
  203. [stream writeUInt64:fieldNum value:value];
  204. } else if (wireType == GPBTypeFixed64) {
  205. [stream writeFixed64:fieldNum value:value];
  206. } else {
  207. NSCAssert(NO, @"Unexpected type %d", wireType);
  208. }
  209. }
  210. GPB_INLINE size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBType wireType) {
  211. NSCAssert(wireType == GPBTypeBool, @"bad type: %d", wireType);
  212. #pragma unused(wireType) // For when asserts are off in release.
  213. return GPBComputeBoolSize(fieldNum, value);
  214. }
  215. GPB_INLINE void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBType wireType) {
  216. NSCAssert(wireType == GPBTypeBool, @"bad type: %d", wireType);
  217. #pragma unused(wireType) // For when asserts are off in release.
  218. [stream writeBool:fieldNum value:value];
  219. }
  220. GPB_INLINE size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBType wireType) {
  221. NSCAssert(wireType == GPBTypeEnum, @"bad type: %d", wireType);
  222. #pragma unused(wireType) // For when asserts are off in release.
  223. return GPBComputeEnumSize(fieldNum, value);
  224. }
  225. GPB_INLINE void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBType wireType) {
  226. NSCAssert(wireType == GPBTypeEnum, @"bad type: %d", wireType);
  227. #pragma unused(wireType) // For when asserts are off in release.
  228. [stream writeEnum:fieldNum value:value];
  229. }
  230. GPB_INLINE size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBType wireType) {
  231. NSCAssert(wireType == GPBTypeFloat, @"bad type: %d", wireType);
  232. #pragma unused(wireType) // For when asserts are off in release.
  233. return GPBComputeFloatSize(fieldNum, value);
  234. }
  235. GPB_INLINE void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBType wireType) {
  236. NSCAssert(wireType == GPBTypeFloat, @"bad type: %d", wireType);
  237. #pragma unused(wireType) // For when asserts are off in release.
  238. [stream writeFloat:fieldNum value:value];
  239. }
  240. GPB_INLINE size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBType wireType) {
  241. NSCAssert(wireType == GPBTypeDouble, @"bad type: %d", wireType);
  242. #pragma unused(wireType) // For when asserts are off in release.
  243. return GPBComputeDoubleSize(fieldNum, value);
  244. }
  245. GPB_INLINE void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBType wireType) {
  246. NSCAssert(wireType == GPBTypeDouble, @"bad type: %d", wireType);
  247. #pragma unused(wireType) // For when asserts are off in release.
  248. [stream writeDouble:fieldNum value:value];
  249. }
  250. GPB_INLINE size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBType wireType) {
  251. NSCAssert(wireType == GPBTypeString, @"bad type: %d", wireType);
  252. #pragma unused(wireType) // For when asserts are off in release.
  253. return GPBComputeStringSize(fieldNum, value);
  254. }
  255. GPB_INLINE void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBType wireType) {
  256. NSCAssert(wireType == GPBTypeString, @"bad type: %d", wireType);
  257. #pragma unused(wireType) // For when asserts are off in release.
  258. [stream writeString:fieldNum value:value];
  259. }
  260. GPB_INLINE size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBType wireType) {
  261. if (wireType == GPBTypeMessage) {
  262. return GPBComputeMessageSize(fieldNum, value);
  263. } else if (wireType == GPBTypeString) {
  264. return GPBComputeStringSize(fieldNum, value);
  265. } else if (wireType == GPBTypeData) {
  266. return GPBComputeDataSize(fieldNum, value);
  267. } else {
  268. NSCAssert(NO, @"Unexpected type %d", wireType);
  269. return 0;
  270. }
  271. }
  272. GPB_INLINE void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBType wireType) {
  273. if (wireType == GPBTypeMessage) {
  274. [stream writeMessage:fieldNum value:value];
  275. } else if (wireType == GPBTypeString) {
  276. [stream writeString:fieldNum value:value];
  277. } else if (wireType == GPBTypeData) {
  278. [stream writeData:fieldNum value:value];
  279. } else {
  280. NSCAssert(NO, @"Unexpected type %d", wireType);
  281. }
  282. }
  283. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  284. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  285. GPBType mapValueType = GPBGetFieldType(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), GPBTypeMessage);
  294. result += tagSize * dict.count;
  295. return result;
  296. }
  297. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  298. NSDictionary *dict,
  299. GPBFieldDescriptor *field) {
  300. NSCAssert(field.mapKeyType == GPBTypeString, @"Unexpected key type");
  301. GPBType mapValueType = GPBGetFieldType(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.mapKeyType == GPBTypeString, @"Unexpected key type");
  318. NSCAssert(GPBGetFieldType(field) == GPBTypeMessage, @"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. GPBValue *valueToFill,
  330. GPBType type,
  331. GPBExtensionRegistry *registry,
  332. GPBFieldDescriptor *field) {
  333. switch (type) {
  334. case GPBTypeBool:
  335. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  336. break;
  337. case GPBTypeFixed32:
  338. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  339. break;
  340. case GPBTypeSFixed32:
  341. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  342. break;
  343. case GPBTypeFloat:
  344. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  345. break;
  346. case GPBTypeFixed64:
  347. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  348. break;
  349. case GPBTypeSFixed64:
  350. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  351. break;
  352. case GPBTypeDouble:
  353. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  354. break;
  355. case GPBTypeInt32:
  356. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  357. break;
  358. case GPBTypeInt64:
  359. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  360. break;
  361. case GPBTypeSInt32:
  362. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  363. break;
  364. case GPBTypeSInt64:
  365. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  366. break;
  367. case GPBTypeUInt32:
  368. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  369. break;
  370. case GPBTypeUInt64:
  371. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  372. break;
  373. case GPBTypeData:
  374. [valueToFill->valueData release];
  375. valueToFill->valueData = GPBCodedInputStreamReadRetainedData(&stream->state_);
  376. break;
  377. case GPBTypeString:
  378. [valueToFill->valueString release];
  379. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  380. break;
  381. case GPBTypeMessage: {
  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 GPBTypeGroup:
  389. NSCAssert(NO, @"Can't happen");
  390. break;
  391. case GPBTypeEnum:
  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. GPBType keyType = field.mapKeyType;
  402. GPBType valueType = GPBGetFieldType(field);
  403. GPBValue key;
  404. GPBValue value;
  405. // Zero them (but pick up any enum default for proto2).
  406. key.valueString = value.valueString = nil;
  407. if (valueType == GPBTypeEnum) {
  408. value = field.defaultValue;
  409. }
  410. GPBCodedInputStreamState *state = &stream->state_;
  411. uint32_t keyTag =
  412. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyType, NO));
  413. uint32_t valueTag =
  414. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueType, NO));
  415. BOOL hitError = NO;
  416. while (YES) {
  417. uint32_t tag = GPBCodedInputStreamReadTag(state);
  418. if (tag == keyTag) {
  419. ReadValue(stream, &key, keyType, registry, field);
  420. } else if (tag == valueTag) {
  421. ReadValue(stream, &value, valueType, 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 ((keyType == GPBTypeString) && (key.valueString == nil)) {
  435. key.valueString = [@"" retain];
  436. }
  437. if (GPBTypeIsObject(valueType) && value.valueString == nil) {
  438. switch (valueType) {
  439. case GPBTypeString:
  440. value.valueString = [@"" retain];
  441. break;
  442. case GPBTypeData:
  443. value.valueData = [GPBEmptyNSData() retain];
  444. break;
  445. case GPBTypeMessage: {
  446. value.valueMessage = [[field.msgClass alloc] init];
  447. break;
  448. }
  449. default:
  450. // Nothing
  451. break;
  452. }
  453. }
  454. if ((keyType == GPBTypeString) && GPBTypeIsObject(valueType)) {
  455. // mapDictionary is an NSMutableDictionary
  456. [mapDictionary setObject:value.valueString forKey:key.valueString];
  457. } else {
  458. if (valueType == GPBTypeEnum) {
  459. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  460. [field isValidEnumValue:value.valueEnum]) {
  461. [mapDictionary setGPBValue:&value forGPBValueKey:&key];
  462. } else {
  463. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  464. forKey:&key
  465. keyType:keyType];
  466. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  467. }
  468. } else {
  469. [mapDictionary setGPBValue:&value forGPBValueKey:&key];
  470. }
  471. }
  472. }
  473. if (GPBTypeIsObject(keyType)) {
  474. [key.valueString release];
  475. }
  476. if (GPBTypeIsObject(valueType)) {
  477. [value.valueString release];
  478. }
  479. }
  480. //
  481. // Macros for the common basic cases.
  482. //
  483. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  484. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  485. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  486. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  487. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  488. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  489. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  490. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  491. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  492. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  493. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  494. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  495. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  496. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  497. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  498. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT)
  499. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  500. //%#pragma mark - KEY_NAME -> VALUE_NAME
  501. //%
  502. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  503. //% @package
  504. //% NSMutableDictionary *_dictionary;
  505. //%}
  506. //%
  507. //%+ (instancetype)dictionary {
  508. //% return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  509. //%}
  510. //%
  511. //%+ (instancetype)dictionaryWithValue:(VALUE_TYPE)value
  512. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  513. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  514. //% // on to get the type correct.
  515. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValues:&value
  516. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  517. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  518. //%}
  519. //%
  520. //%+ (instancetype)dictionaryWithValues:(const VALUE_TYPE [])values
  521. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  522. //% count:(NSUInteger)count {
  523. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  524. //% // on to get the type correct.
  525. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValues:values
  526. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  527. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  528. //%}
  529. //%
  530. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  531. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  532. //% // on to get the type correct.
  533. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  534. //%}
  535. //%
  536. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  537. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  538. //%}
  539. //%
  540. //%- (instancetype)init {
  541. //% return [self initWithValues:NULL forKeys:NULL count:0];
  542. //%}
  543. //%
  544. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  545. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  546. //% count:(NSUInteger)count {
  547. //% self = [super init];
  548. //% if (self) {
  549. //% _dictionary = [[NSMutableDictionary alloc] init];
  550. //% if (count && values && keys) {
  551. //% for (NSUInteger i = 0; i < count; ++i) {
  552. //% [_dictionary setObject:WRAPPED##VHELPER(values[i]) forKey:WRAPPED##KHELPER(keys[i])];
  553. //% }
  554. //% }
  555. //% }
  556. //% return self;
  557. //%}
  558. //%
  559. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  560. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  561. //% if (self) {
  562. //% if (dictionary) {
  563. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  564. //% }
  565. //% }
  566. //% return self;
  567. //%}
  568. //%
  569. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  570. //% #pragma unused(numItems)
  571. //% return [self initWithValues:NULL forKeys:NULL count:0];
  572. //%}
  573. //%
  574. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, )
  575. //%
  576. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  577. //%
  578. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, )
  579. //%
  580. //%@end
  581. //%
  582. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  583. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  584. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  585. //%#pragma mark - KEY_NAME -> VALUE_NAME
  586. //%
  587. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  588. //% @package
  589. //% NSMutableDictionary *_dictionary;
  590. //% GPBEnumValidationFunc _validationFunc;
  591. //%}
  592. //%
  593. //%@synthesize validationFunc = _validationFunc;
  594. //%
  595. //%+ (instancetype)dictionary {
  596. //% return [[[self alloc] initWithValidationFunction:NULL
  597. //% rawValues:NULL
  598. //% forKeys:NULL
  599. //% count:0] autorelease];
  600. //%}
  601. //%
  602. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  603. //% return [[[self alloc] initWithValidationFunction:func
  604. //% rawValues:NULL
  605. //% forKeys:NULL
  606. //% count:0] autorelease];
  607. //%}
  608. //%
  609. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  610. //% rawValue:(VALUE_TYPE)rawValue
  611. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  612. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  613. //% // on to get the type correct.
  614. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  615. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  616. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  617. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  618. //%}
  619. //%
  620. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  621. //% rawValues:(const VALUE_TYPE [])rawValues
  622. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  623. //% count:(NSUInteger)count {
  624. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  625. //% // on to get the type correct.
  626. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  627. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  628. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  629. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  630. //%}
  631. //%
  632. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  633. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  634. //% // on to get the type correct.
  635. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  636. //%}
  637. //%
  638. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  639. //% capacity:(NSUInteger)numItems {
  640. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  641. //%}
  642. //%
  643. //%- (instancetype)init {
  644. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  645. //%}
  646. //%
  647. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  648. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  649. //%}
  650. //%
  651. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  652. //% rawValues:(const VALUE_TYPE [])rawValues
  653. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  654. //% count:(NSUInteger)count {
  655. //% self = [super init];
  656. //% if (self) {
  657. //% _dictionary = [[NSMutableDictionary alloc] init];
  658. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  659. //% if (count && rawValues && keys) {
  660. //% for (NSUInteger i = 0; i < count; ++i) {
  661. //% [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  662. //% }
  663. //% }
  664. //% }
  665. //% return self;
  666. //%}
  667. //%
  668. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  669. //% self = [self initWithValidationFunction:dictionary.validationFunc
  670. //% rawValues:NULL
  671. //% forKeys:NULL
  672. //% count:0];
  673. //% if (self) {
  674. //% if (dictionary) {
  675. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  676. //% }
  677. //% }
  678. //% return self;
  679. //%}
  680. //%
  681. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  682. //% capacity:(NSUInteger)numItems {
  683. //% #pragma unused(numItems)
  684. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  685. //%}
  686. //%
  687. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Raw)
  688. //%
  689. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  690. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  691. //% if (wrapped && value) {
  692. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  693. //% if (!_validationFunc(result)) {
  694. //% result = kGPBUnrecognizedEnumeratorValue;
  695. //% }
  696. //% *value = result;
  697. //% }
  698. //% return (wrapped != NULL);
  699. //%}
  700. //%
  701. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  702. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  703. //% if (wrapped && rawValue) {
  704. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  705. //% }
  706. //% return (wrapped != NULL);
  707. //%}
  708. //%
  709. //%- (void)enumerateKeysAndValuesUsingBlock:
  710. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  711. //% GPBEnumValidationFunc func = _validationFunc;
  712. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  713. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  714. //% BOOL *stop) {
  715. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  716. //% if (!func(unwrapped)) {
  717. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  718. //% }
  719. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  720. //% }];
  721. //%}
  722. //%
  723. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Raw)
  724. //%
  725. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  726. //% if (!_validationFunc(value)) {
  727. //% [NSException raise:NSInvalidArgumentException
  728. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  729. //% value];
  730. //% }
  731. //%
  732. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  733. //% if (_autocreator) {
  734. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  735. //% }
  736. //%}
  737. //%
  738. //%@end
  739. //%
  740. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, ACCESSOR_NAME)
  741. //%- (void)dealloc {
  742. //% NSAssert(!_autocreator,
  743. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  744. //% [self class], _autocreator);
  745. //% [_dictionary release];
  746. //% [super dealloc];
  747. //%}
  748. //%
  749. //%- (instancetype)copyWithZone:(NSZone *)zone {
  750. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  751. //%}
  752. //%
  753. //%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
  754. //% if (self == other) {
  755. //% return YES;
  756. //% }
  757. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  758. //% return NO;
  759. //% }
  760. //% return [_dictionary isEqual:other->_dictionary];
  761. //%}
  762. //%
  763. //%- (NSUInteger)hash {
  764. //% return _dictionary.count;
  765. //%}
  766. //%
  767. //%- (NSString *)description {
  768. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  769. //%}
  770. //%
  771. //%- (NSUInteger)count {
  772. //% return _dictionary.count;
  773. //%}
  774. //%
  775. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##ValuesUsingBlock:
  776. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  777. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  778. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  779. //% BOOL *stop) {
  780. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(aValue), stop);
  781. //% }];
  782. //%}
  783. //%
  784. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  785. //% NSUInteger count = _dictionary.count;
  786. //% if (count == 0) {
  787. //% return 0;
  788. //% }
  789. //%
  790. //% GPBType valueType = GPBGetFieldType(field);
  791. //% GPBType keyType = field.mapKeyType;
  792. //% __block size_t result = 0;
  793. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  794. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  795. //% BOOL *stop) {
  796. //% #pragma unused(stop)
  797. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyType);
  798. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueType);
  799. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  800. //% }];
  801. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  802. //% result += tagSize * count;
  803. //% return result;
  804. //%}
  805. //%
  806. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  807. //% asField:(GPBFieldDescriptor *)field {
  808. //% GPBType valueType = GPBGetFieldType(field);
  809. //% GPBType keyType = field.mapKeyType;
  810. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  811. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  812. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  813. //% BOOL *stop) {
  814. //% #pragma unused(stop)
  815. //% // Write the tag.
  816. //% [outputStream writeInt32NoTag:tag];
  817. //% // Write the size of the message.
  818. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyType);
  819. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueType);
  820. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  821. //% // Write the fields.
  822. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyType);
  823. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(aValue), kMapValueFieldNumber, valueType);
  824. //% }];
  825. //%}
  826. //%
  827. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  828. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  829. //%}
  830. //%
  831. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  832. //% [self enumerateKeysAnd##ACCESSOR_NAME##ValuesUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop) {
  833. //% #pragma unused(stop)
  834. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(value));
  835. //% }];
  836. //%}
  837. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, ACCESSOR_NAME)
  838. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  839. //% if (otherDictionary) {
  840. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  841. //% if (_autocreator) {
  842. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  843. //% }
  844. //% }
  845. //%}
  846. //%
  847. //%- (void)set##ACCESSOR_NAME##Value:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  848. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  849. //% if (_autocreator) {
  850. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  851. //% }
  852. //%}
  853. //%
  854. //%- (void)removeValueForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  855. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  856. //%}
  857. //%
  858. //%- (void)removeAll {
  859. //% [_dictionary removeAllObjects];
  860. //%}
  861. //
  862. // Custom Generation for Bool keys
  863. //
  864. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  865. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD)
  866. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  867. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT)
  868. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER)
  869. //%#pragma mark - Bool -> VALUE_NAME
  870. //%
  871. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  872. //% @package
  873. //% VALUE_TYPE _values[2];
  874. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  875. //%
  876. //%+ (instancetype)dictionary {
  877. //% return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  878. //%}
  879. //%
  880. //%+ (instancetype)dictionaryWithValue:(VALUE_TYPE)value
  881. //% forKey:(BOOL)key {
  882. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  883. //% // on to get the type correct.
  884. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithValues:&value
  885. //% VALUE_NAME$S forKeys:&key
  886. //% VALUE_NAME$S count:1] autorelease];
  887. //%}
  888. //%
  889. //%+ (instancetype)dictionaryWithValues:(const VALUE_TYPE [])values
  890. //% forKeys:(const BOOL [])keys
  891. //% count:(NSUInteger)count {
  892. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  893. //% // on to get the type correct.
  894. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithValues:values
  895. //% VALUE_NAME$S forKeys:keys
  896. //% VALUE_NAME$S count:count] autorelease];
  897. //%}
  898. //%
  899. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  900. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  901. //% // on to get the type correct.
  902. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  903. //%}
  904. //%
  905. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  906. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  907. //%}
  908. //%
  909. //%- (instancetype)init {
  910. //% return [self initWithValues:NULL forKeys:NULL count:0];
  911. //%}
  912. //%
  913. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  914. //%
  915. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  916. //% #pragma unused(numItems)
  917. //% return [self initWithValues:NULL forKeys:NULL count:0];
  918. //%}
  919. //%
  920. //%BOOL_DICT_DEALLOC##HELPER()
  921. //%
  922. //%- (instancetype)copyWithZone:(NSZone *)zone {
  923. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  924. //%}
  925. //%
  926. //%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
  927. //% if (self == other) {
  928. //% return YES;
  929. //% }
  930. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  931. //% return NO;
  932. //% }
  933. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
  934. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
  935. //% return NO;
  936. //% }
  937. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
  938. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
  939. //% return NO;
  940. //% }
  941. //% return YES;
  942. //%}
  943. //%
  944. //%- (NSUInteger)hash {
  945. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  946. //%}
  947. //%
  948. //%- (NSString *)description {
  949. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  950. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  951. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  952. //% }
  953. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  954. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  955. //% }
  956. //% [result appendString:@" }"];
  957. //% return result;
  958. //%}
  959. //%
  960. //%- (NSUInteger)count {
  961. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  962. //%}
  963. //%
  964. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  965. //%
  966. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  967. //%
  968. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  969. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  970. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  971. //% }
  972. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  973. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  974. //% }
  975. //%}
  976. //%
  977. //%- (void)enumerateKeysAndValuesUsingBlock:
  978. //% (void (^)(BOOL key, VALUE_TYPE value, BOOL *stop))block {
  979. //% BOOL stop = NO;
  980. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  981. //% block(NO, _values[0], &stop);
  982. //% }
  983. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  984. //% block(YES, _values[1], &stop);
  985. //% }
  986. //%}
  987. //%
  988. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  989. //% GPBType valueType = GPBGetFieldType(field);
  990. //% NSUInteger count = 0;
  991. //% size_t result = 0;
  992. //% for (int i = 0; i < 2; ++i) {
  993. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  994. //% ++count;
  995. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  996. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueType);
  997. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  998. //% }
  999. //% }
  1000. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1001. //% result += tagSize * count;
  1002. //% return result;
  1003. //%}
  1004. //%
  1005. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1006. //% asField:(GPBFieldDescriptor *)field {
  1007. //% GPBType valueType = GPBGetFieldType(field);
  1008. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1009. //% for (int i = 0; i < 2; ++i) {
  1010. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1011. //% // Write the tag.
  1012. //% [outputStream writeInt32NoTag:tag];
  1013. //% // Write the size of the message.
  1014. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  1015. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueType);
  1016. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1017. //% // Write the fields.
  1018. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  1019. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  1020. //% }
  1021. //% }
  1022. //%}
  1023. //%
  1024. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1025. //%
  1026. //%@end
  1027. //%
  1028. //
  1029. // Helpers for PODs
  1030. //
  1031. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1032. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  1033. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1034. //% if (wrapped && value) {
  1035. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1036. //% }
  1037. //% return (wrapped != NULL);
  1038. //%}
  1039. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1040. //%@(VALUE)
  1041. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1042. //%[VALUE unsignedIntValue]
  1043. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1044. //%[VALUE intValue]
  1045. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1046. //%[VALUE unsignedLongLongValue]
  1047. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1048. //%[VALUE longLongValue]
  1049. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1050. //%[VALUE boolValue]
  1051. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1052. //%[VALUE floatValue]
  1053. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1054. //%[VALUE doubleValue]
  1055. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1056. //%[VALUE intValue]
  1057. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1058. //%[NSString stringWithFormat:@"%u", VALUE]
  1059. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1060. //%[NSString stringWithFormat:@"%d", VALUE]
  1061. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1062. //%[NSString stringWithFormat:@"%llu", VALUE]
  1063. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1064. //%[NSString stringWithFormat:@"%lld", VALUE]
  1065. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1066. //%(VALUE ? @"true" : @"false")
  1067. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1068. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1069. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1070. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1071. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1072. //%@(VALUE)
  1073. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1074. //%NSNumber *
  1075. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1076. //%VAL1 != VAL2
  1077. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1078. // Empty
  1079. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1080. // Empty
  1081. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1082. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1083. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1084. // Empty
  1085. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1086. // Empty
  1087. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1088. // Empty
  1089. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1090. // Empty
  1091. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1092. // Empty
  1093. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1094. // Empty
  1095. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1096. // Empty
  1097. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1098. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1099. //% forKey:(GPBValue *)key
  1100. //% keyType:(GPBType)keyType {
  1101. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyType);
  1102. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  1103. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1104. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1105. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyType);
  1106. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  1107. //% [outputStream release];
  1108. //% return data;
  1109. //%}
  1110. //%
  1111. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1112. //%value##VALUE_NAME
  1113. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1114. //% BOOL _valueSet[2];
  1115. //%
  1116. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1117. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1118. //% forKeys:(const BOOL [])keys
  1119. //% count:(NSUInteger)count {
  1120. //% self = [super init];
  1121. //% if (self) {
  1122. //% for (NSUInteger i = 0; i < count; ++i) {
  1123. //% int idx = keys[i] ? 1 : 0;
  1124. //% _values[idx] = values[i];
  1125. //% _valueSet[idx] = YES;
  1126. //% }
  1127. //% }
  1128. //% return self;
  1129. //%}
  1130. //%
  1131. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1132. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1133. //% if (self) {
  1134. //% if (dictionary) {
  1135. //% for (int i = 0; i < 2; ++i) {
  1136. //% if (dictionary->_valueSet[i]) {
  1137. //% _values[i] = dictionary->_values[i];
  1138. //% _valueSet[i] = YES;
  1139. //% }
  1140. //% }
  1141. //% }
  1142. //% }
  1143. //% return self;
  1144. //%}
  1145. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1146. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1147. //%- (void)dealloc {
  1148. //% NSAssert(!_autocreator,
  1149. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1150. //% [self class], _autocreator);
  1151. //% [super dealloc];
  1152. //%}
  1153. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1154. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1155. //%BOOL_DICT_HASPOD(IDX, REF)
  1156. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1157. //%REF##_valueSet[IDX]
  1158. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1159. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1160. //% int idx = (key ? 1 : 0);
  1161. //% if (_valueSet[idx]) {
  1162. //% if (value) {
  1163. //% *value = _values[idx];
  1164. //% }
  1165. //% return YES;
  1166. //% }
  1167. //% return NO;
  1168. //%}
  1169. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1170. //%- (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1171. //% int idx = (key->valueBool ? 1 : 0);
  1172. //% _values[idx] = value->value##VALUE_NAME;
  1173. //% _valueSet[idx] = YES;
  1174. //%}
  1175. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1176. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1177. //% if (otherDictionary) {
  1178. //% for (int i = 0; i < 2; ++i) {
  1179. //% if (otherDictionary->_valueSet[i]) {
  1180. //% _valueSet[i] = YES;
  1181. //% _values[i] = otherDictionary->_values[i];
  1182. //% }
  1183. //% }
  1184. //% if (_autocreator) {
  1185. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1186. //% }
  1187. //% }
  1188. //%}
  1189. //%
  1190. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1191. //% int idx = (key ? 1 : 0);
  1192. //% _values[idx] = value;
  1193. //% _valueSet[idx] = YES;
  1194. //% if (_autocreator) {
  1195. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1196. //% }
  1197. //%}
  1198. //%
  1199. //%- (void)removeValueForKey:(BOOL)aKey {
  1200. //% _valueSet[aKey ? 1 : 0] = NO;
  1201. //%}
  1202. //%
  1203. //%- (void)removeAll {
  1204. //% _valueSet[0] = NO;
  1205. //% _valueSet[1] = NO;
  1206. //%}
  1207. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1208. //%STR_FORMAT_##VALUE_NAME()
  1209. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1210. //%%u
  1211. //%PDDM-DEFINE STR_FORMAT_Int32()
  1212. //%%d
  1213. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1214. //%%llu
  1215. //%PDDM-DEFINE STR_FORMAT_Int64()
  1216. //%%lld
  1217. //%PDDM-DEFINE STR_FORMAT_Bool()
  1218. //%%d
  1219. //%PDDM-DEFINE STR_FORMAT_Float()
  1220. //%%f
  1221. //%PDDM-DEFINE STR_FORMAT_Double()
  1222. //%%lf
  1223. //
  1224. // Helpers for Objects
  1225. //
  1226. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1227. //%- (VALUE_TYPE)valueForKey:(KEY_TYPE)key {
  1228. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1229. //% return result;
  1230. //%}
  1231. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1232. //%VALUE
  1233. //%PDDM-DEFINE UNWRAPString(VALUE)
  1234. //%VALUE
  1235. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1236. //%VALUE
  1237. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1238. //%VALUE
  1239. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1240. //%VALUE
  1241. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1242. //%ENUM_TYPEOBJECT_##TYPE()
  1243. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1244. //%NSString *
  1245. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1246. //%id ##
  1247. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1248. //%![VAL1 isEqual:VAL2]
  1249. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1250. //%- (BOOL)isInitialized {
  1251. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1252. //% if (!msg.initialized) {
  1253. //% return NO;
  1254. //% }
  1255. //% }
  1256. //% return YES;
  1257. //%}
  1258. //%
  1259. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1260. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1261. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1262. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1263. //% GPBMessage *msg,
  1264. //% BOOL *stop) {
  1265. //% #pragma unused(stop)
  1266. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1267. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1268. //% [copiedMsg release];
  1269. //% }];
  1270. //% return newDict;
  1271. //%}
  1272. //%
  1273. //%
  1274. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1275. //%- (BOOL)isInitialized {
  1276. //% if (_values[0] && ![_values[0] isInitialized]) {
  1277. //% return NO;
  1278. //% }
  1279. //% if (_values[1] && ![_values[1] isInitialized]) {
  1280. //% return NO;
  1281. //% }
  1282. //% return YES;
  1283. //%}
  1284. //%
  1285. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1286. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1287. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1288. //% for (int i = 0; i < 2; ++i) {
  1289. //% if (_values[i] != nil) {
  1290. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1291. //% }
  1292. //% }
  1293. //% return newDict;
  1294. //%}
  1295. //%
  1296. //%
  1297. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1298. // Empty
  1299. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1300. //%valueString
  1301. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1302. // Empty
  1303. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1304. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1305. //% forKeys:(const BOOL [])keys
  1306. //% count:(NSUInteger)count {
  1307. //% self = [super init];
  1308. //% if (self) {
  1309. //% for (NSUInteger i = 0; i < count; ++i) {
  1310. //% int idx = keys[i] ? 1 : 0;
  1311. //% [_values[idx] release];
  1312. //% _values[idx] = (VALUE_TYPE)[values[i] retain];
  1313. //% }
  1314. //% }
  1315. //% return self;
  1316. //%}
  1317. //%
  1318. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1319. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1320. //% if (self) {
  1321. //% if (dictionary) {
  1322. //% _values[0] = [dictionary->_values[0] retain];
  1323. //% _values[1] = [dictionary->_values[1] retain];
  1324. //% }
  1325. //% }
  1326. //% return self;
  1327. //%}
  1328. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1329. //%- (void)dealloc {
  1330. //% NSAssert(!_autocreator,
  1331. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1332. //% [self class], _autocreator);
  1333. //% [_values[0] release];
  1334. //% [_values[1] release];
  1335. //% [super dealloc];
  1336. //%}
  1337. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1338. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1339. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1340. //%REF##_values[IDX] != nil
  1341. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1342. //%- (VALUE_TYPE)valueForKey:(BOOL)key {
  1343. //% return _values[key ? 1 : 0];
  1344. //%}
  1345. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1346. //%- (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1347. //% int idx = (key->valueBool ? 1 : 0);
  1348. //% [_values[idx] release];
  1349. //% _values[idx] = [value->valueString retain];
  1350. //%}
  1351. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1352. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1353. //% if (otherDictionary) {
  1354. //% for (int i = 0; i < 2; ++i) {
  1355. //% if (otherDictionary->_values[i] != nil) {
  1356. //% [_values[i] release];
  1357. //% _values[i] = [otherDictionary->_values[i] retain];
  1358. //% }
  1359. //% }
  1360. //% if (_autocreator) {
  1361. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1362. //% }
  1363. //% }
  1364. //%}
  1365. //%
  1366. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1367. //% int idx = (key ? 1 : 0);
  1368. //% [_values[idx] release];
  1369. //% _values[idx] = [value retain];
  1370. //% if (_autocreator) {
  1371. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1372. //% }
  1373. //%}
  1374. //%
  1375. //%- (void)removeValueForKey:(BOOL)aKey {
  1376. //% int idx = (aKey ? 1 : 0);
  1377. //% [_values[idx] release];
  1378. //% _values[idx] = nil;
  1379. //%}
  1380. //%
  1381. //%- (void)removeAll {
  1382. //% for (int i = 0; i < 2; ++i) {
  1383. //% [_values[i] release];
  1384. //% _values[i] = nil;
  1385. //% }
  1386. //%}
  1387. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1388. //%%@
  1389. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1390. // This block of code is generated, do not edit it directly.
  1391. #pragma mark - UInt32 -> UInt32
  1392. @implementation GPBUInt32UInt32Dictionary {
  1393. @package
  1394. NSMutableDictionary *_dictionary;
  1395. }
  1396. + (instancetype)dictionary {
  1397. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1398. }
  1399. + (instancetype)dictionaryWithValue:(uint32_t)value
  1400. forKey:(uint32_t)key {
  1401. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1402. // on to get the type correct.
  1403. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1404. forKeys:&key
  1405. count:1] autorelease];
  1406. }
  1407. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1408. forKeys:(const uint32_t [])keys
  1409. count:(NSUInteger)count {
  1410. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1411. // on to get the type correct.
  1412. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1413. forKeys:keys
  1414. count:count] autorelease];
  1415. }
  1416. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1417. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1418. // on to get the type correct.
  1419. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1420. }
  1421. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1422. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1423. }
  1424. - (instancetype)init {
  1425. return [self initWithValues:NULL forKeys:NULL count:0];
  1426. }
  1427. - (instancetype)initWithValues:(const uint32_t [])values
  1428. forKeys:(const uint32_t [])keys
  1429. count:(NSUInteger)count {
  1430. self = [super init];
  1431. if (self) {
  1432. _dictionary = [[NSMutableDictionary alloc] init];
  1433. if (count && values && keys) {
  1434. for (NSUInteger i = 0; i < count; ++i) {
  1435. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1436. }
  1437. }
  1438. }
  1439. return self;
  1440. }
  1441. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1442. self = [self initWithValues:NULL forKeys:NULL count:0];
  1443. if (self) {
  1444. if (dictionary) {
  1445. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1446. }
  1447. }
  1448. return self;
  1449. }
  1450. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1451. #pragma unused(numItems)
  1452. return [self initWithValues:NULL forKeys:NULL count:0];
  1453. }
  1454. - (void)dealloc {
  1455. NSAssert(!_autocreator,
  1456. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1457. [self class], _autocreator);
  1458. [_dictionary release];
  1459. [super dealloc];
  1460. }
  1461. - (instancetype)copyWithZone:(NSZone *)zone {
  1462. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1463. }
  1464. - (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
  1465. if (self == other) {
  1466. return YES;
  1467. }
  1468. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1469. return NO;
  1470. }
  1471. return [_dictionary isEqual:other->_dictionary];
  1472. }
  1473. - (NSUInteger)hash {
  1474. return _dictionary.count;
  1475. }
  1476. - (NSString *)description {
  1477. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1478. }
  1479. - (NSUInteger)count {
  1480. return _dictionary.count;
  1481. }
  1482. - (void)enumerateKeysAndValuesUsingBlock:
  1483. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1484. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1485. NSNumber *aValue,
  1486. BOOL *stop) {
  1487. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1488. }];
  1489. }
  1490. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1491. NSUInteger count = _dictionary.count;
  1492. if (count == 0) {
  1493. return 0;
  1494. }
  1495. GPBType valueType = GPBGetFieldType(field);
  1496. GPBType keyType = field.mapKeyType;
  1497. __block size_t result = 0;
  1498. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1499. NSNumber *aValue,
  1500. BOOL *stop) {
  1501. #pragma unused(stop)
  1502. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1503. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  1504. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1505. }];
  1506. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1507. result += tagSize * count;
  1508. return result;
  1509. }
  1510. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1511. asField:(GPBFieldDescriptor *)field {
  1512. GPBType valueType = GPBGetFieldType(field);
  1513. GPBType keyType = field.mapKeyType;
  1514. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1515. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1516. NSNumber *aValue,
  1517. BOOL *stop) {
  1518. #pragma unused(stop)
  1519. // Write the tag.
  1520. [outputStream writeInt32NoTag:tag];
  1521. // Write the size of the message.
  1522. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1523. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  1524. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1525. // Write the fields.
  1526. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1527. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  1528. }];
  1529. }
  1530. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1531. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1532. }
  1533. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1534. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1535. #pragma unused(stop)
  1536. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1537. }];
  1538. }
  1539. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1540. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1541. if (wrapped && value) {
  1542. *value = [wrapped unsignedIntValue];
  1543. }
  1544. return (wrapped != NULL);
  1545. }
  1546. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1547. if (otherDictionary) {
  1548. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1549. if (_autocreator) {
  1550. GPBAutocreatedDictionaryModified(_autocreator, self);
  1551. }
  1552. }
  1553. }
  1554. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1555. [_dictionary setObject:@(value) forKey:@(key)];
  1556. if (_autocreator) {
  1557. GPBAutocreatedDictionaryModified(_autocreator, self);
  1558. }
  1559. }
  1560. - (void)removeValueForKey:(uint32_t)aKey {
  1561. [_dictionary removeObjectForKey:@(aKey)];
  1562. }
  1563. - (void)removeAll {
  1564. [_dictionary removeAllObjects];
  1565. }
  1566. @end
  1567. #pragma mark - UInt32 -> Int32
  1568. @implementation GPBUInt32Int32Dictionary {
  1569. @package
  1570. NSMutableDictionary *_dictionary;
  1571. }
  1572. + (instancetype)dictionary {
  1573. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1574. }
  1575. + (instancetype)dictionaryWithValue:(int32_t)value
  1576. forKey:(uint32_t)key {
  1577. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1578. // on to get the type correct.
  1579. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1580. forKeys:&key
  1581. count:1] autorelease];
  1582. }
  1583. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1584. forKeys:(const uint32_t [])keys
  1585. count:(NSUInteger)count {
  1586. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1587. // on to get the type correct.
  1588. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1589. forKeys:keys
  1590. count:count] autorelease];
  1591. }
  1592. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1593. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1594. // on to get the type correct.
  1595. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1596. }
  1597. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1598. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1599. }
  1600. - (instancetype)init {
  1601. return [self initWithValues:NULL forKeys:NULL count:0];
  1602. }
  1603. - (instancetype)initWithValues:(const int32_t [])values
  1604. forKeys:(const uint32_t [])keys
  1605. count:(NSUInteger)count {
  1606. self = [super init];
  1607. if (self) {
  1608. _dictionary = [[NSMutableDictionary alloc] init];
  1609. if (count && values && keys) {
  1610. for (NSUInteger i = 0; i < count; ++i) {
  1611. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1612. }
  1613. }
  1614. }
  1615. return self;
  1616. }
  1617. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1618. self = [self initWithValues:NULL forKeys:NULL count:0];
  1619. if (self) {
  1620. if (dictionary) {
  1621. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1622. }
  1623. }
  1624. return self;
  1625. }
  1626. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1627. #pragma unused(numItems)
  1628. return [self initWithValues:NULL forKeys:NULL count:0];
  1629. }
  1630. - (void)dealloc {
  1631. NSAssert(!_autocreator,
  1632. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1633. [self class], _autocreator);
  1634. [_dictionary release];
  1635. [super dealloc];
  1636. }
  1637. - (instancetype)copyWithZone:(NSZone *)zone {
  1638. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1639. }
  1640. - (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
  1641. if (self == other) {
  1642. return YES;
  1643. }
  1644. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1645. return NO;
  1646. }
  1647. return [_dictionary isEqual:other->_dictionary];
  1648. }
  1649. - (NSUInteger)hash {
  1650. return _dictionary.count;
  1651. }
  1652. - (NSString *)description {
  1653. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1654. }
  1655. - (NSUInteger)count {
  1656. return _dictionary.count;
  1657. }
  1658. - (void)enumerateKeysAndValuesUsingBlock:
  1659. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1660. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1661. NSNumber *aValue,
  1662. BOOL *stop) {
  1663. block([aKey unsignedIntValue], [aValue intValue], stop);
  1664. }];
  1665. }
  1666. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1667. NSUInteger count = _dictionary.count;
  1668. if (count == 0) {
  1669. return 0;
  1670. }
  1671. GPBType valueType = GPBGetFieldType(field);
  1672. GPBType keyType = field.mapKeyType;
  1673. __block size_t result = 0;
  1674. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1675. NSNumber *aValue,
  1676. BOOL *stop) {
  1677. #pragma unused(stop)
  1678. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1679. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  1680. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1681. }];
  1682. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1683. result += tagSize * count;
  1684. return result;
  1685. }
  1686. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1687. asField:(GPBFieldDescriptor *)field {
  1688. GPBType valueType = GPBGetFieldType(field);
  1689. GPBType keyType = field.mapKeyType;
  1690. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1691. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1692. NSNumber *aValue,
  1693. BOOL *stop) {
  1694. #pragma unused(stop)
  1695. // Write the tag.
  1696. [outputStream writeInt32NoTag:tag];
  1697. // Write the size of the message.
  1698. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1699. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  1700. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1701. // Write the fields.
  1702. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1703. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  1704. }];
  1705. }
  1706. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1707. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1708. }
  1709. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1710. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1711. #pragma unused(stop)
  1712. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1713. }];
  1714. }
  1715. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1716. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1717. if (wrapped && value) {
  1718. *value = [wrapped intValue];
  1719. }
  1720. return (wrapped != NULL);
  1721. }
  1722. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1723. if (otherDictionary) {
  1724. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1725. if (_autocreator) {
  1726. GPBAutocreatedDictionaryModified(_autocreator, self);
  1727. }
  1728. }
  1729. }
  1730. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1731. [_dictionary setObject:@(value) forKey:@(key)];
  1732. if (_autocreator) {
  1733. GPBAutocreatedDictionaryModified(_autocreator, self);
  1734. }
  1735. }
  1736. - (void)removeValueForKey:(uint32_t)aKey {
  1737. [_dictionary removeObjectForKey:@(aKey)];
  1738. }
  1739. - (void)removeAll {
  1740. [_dictionary removeAllObjects];
  1741. }
  1742. @end
  1743. #pragma mark - UInt32 -> UInt64
  1744. @implementation GPBUInt32UInt64Dictionary {
  1745. @package
  1746. NSMutableDictionary *_dictionary;
  1747. }
  1748. + (instancetype)dictionary {
  1749. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1750. }
  1751. + (instancetype)dictionaryWithValue:(uint64_t)value
  1752. forKey:(uint32_t)key {
  1753. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1754. // on to get the type correct.
  1755. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1756. forKeys:&key
  1757. count:1] autorelease];
  1758. }
  1759. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1760. forKeys:(const uint32_t [])keys
  1761. count:(NSUInteger)count {
  1762. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1763. // on to get the type correct.
  1764. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1765. forKeys:keys
  1766. count:count] autorelease];
  1767. }
  1768. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1769. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1770. // on to get the type correct.
  1771. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1772. }
  1773. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1774. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1775. }
  1776. - (instancetype)init {
  1777. return [self initWithValues:NULL forKeys:NULL count:0];
  1778. }
  1779. - (instancetype)initWithValues:(const uint64_t [])values
  1780. forKeys:(const uint32_t [])keys
  1781. count:(NSUInteger)count {
  1782. self = [super init];
  1783. if (self) {
  1784. _dictionary = [[NSMutableDictionary alloc] init];
  1785. if (count && values && keys) {
  1786. for (NSUInteger i = 0; i < count; ++i) {
  1787. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1788. }
  1789. }
  1790. }
  1791. return self;
  1792. }
  1793. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1794. self = [self initWithValues:NULL forKeys:NULL count:0];
  1795. if (self) {
  1796. if (dictionary) {
  1797. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1798. }
  1799. }
  1800. return self;
  1801. }
  1802. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1803. #pragma unused(numItems)
  1804. return [self initWithValues:NULL forKeys:NULL count:0];
  1805. }
  1806. - (void)dealloc {
  1807. NSAssert(!_autocreator,
  1808. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1809. [self class], _autocreator);
  1810. [_dictionary release];
  1811. [super dealloc];
  1812. }
  1813. - (instancetype)copyWithZone:(NSZone *)zone {
  1814. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1815. }
  1816. - (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
  1817. if (self == other) {
  1818. return YES;
  1819. }
  1820. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1821. return NO;
  1822. }
  1823. return [_dictionary isEqual:other->_dictionary];
  1824. }
  1825. - (NSUInteger)hash {
  1826. return _dictionary.count;
  1827. }
  1828. - (NSString *)description {
  1829. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1830. }
  1831. - (NSUInteger)count {
  1832. return _dictionary.count;
  1833. }
  1834. - (void)enumerateKeysAndValuesUsingBlock:
  1835. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1836. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1837. NSNumber *aValue,
  1838. BOOL *stop) {
  1839. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1840. }];
  1841. }
  1842. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1843. NSUInteger count = _dictionary.count;
  1844. if (count == 0) {
  1845. return 0;
  1846. }
  1847. GPBType valueType = GPBGetFieldType(field);
  1848. GPBType keyType = field.mapKeyType;
  1849. __block size_t result = 0;
  1850. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1851. NSNumber *aValue,
  1852. BOOL *stop) {
  1853. #pragma unused(stop)
  1854. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1855. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  1856. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1857. }];
  1858. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  1859. result += tagSize * count;
  1860. return result;
  1861. }
  1862. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1863. asField:(GPBFieldDescriptor *)field {
  1864. GPBType valueType = GPBGetFieldType(field);
  1865. GPBType keyType = field.mapKeyType;
  1866. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1867. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1868. NSNumber *aValue,
  1869. BOOL *stop) {
  1870. #pragma unused(stop)
  1871. // Write the tag.
  1872. [outputStream writeInt32NoTag:tag];
  1873. // Write the size of the message.
  1874. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1875. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  1876. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1877. // Write the fields.
  1878. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  1879. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  1880. }];
  1881. }
  1882. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  1883. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1884. }
  1885. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1886. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1887. #pragma unused(stop)
  1888. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1889. }];
  1890. }
  1891. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1892. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1893. if (wrapped && value) {
  1894. *value = [wrapped unsignedLongLongValue];
  1895. }
  1896. return (wrapped != NULL);
  1897. }
  1898. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1899. if (otherDictionary) {
  1900. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1901. if (_autocreator) {
  1902. GPBAutocreatedDictionaryModified(_autocreator, self);
  1903. }
  1904. }
  1905. }
  1906. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1907. [_dictionary setObject:@(value) forKey:@(key)];
  1908. if (_autocreator) {
  1909. GPBAutocreatedDictionaryModified(_autocreator, self);
  1910. }
  1911. }
  1912. - (void)removeValueForKey:(uint32_t)aKey {
  1913. [_dictionary removeObjectForKey:@(aKey)];
  1914. }
  1915. - (void)removeAll {
  1916. [_dictionary removeAllObjects];
  1917. }
  1918. @end
  1919. #pragma mark - UInt32 -> Int64
  1920. @implementation GPBUInt32Int64Dictionary {
  1921. @package
  1922. NSMutableDictionary *_dictionary;
  1923. }
  1924. + (instancetype)dictionary {
  1925. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1926. }
  1927. + (instancetype)dictionaryWithValue:(int64_t)value
  1928. forKey:(uint32_t)key {
  1929. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1930. // on to get the type correct.
  1931. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  1932. forKeys:&key
  1933. count:1] autorelease];
  1934. }
  1935. + (instancetype)dictionaryWithValues:(const int64_t [])values
  1936. forKeys:(const uint32_t [])keys
  1937. count:(NSUInteger)count {
  1938. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1939. // on to get the type correct.
  1940. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  1941. forKeys:keys
  1942. count:count] autorelease];
  1943. }
  1944. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1945. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1946. // on to get the type correct.
  1947. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1948. }
  1949. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1950. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1951. }
  1952. - (instancetype)init {
  1953. return [self initWithValues:NULL forKeys:NULL count:0];
  1954. }
  1955. - (instancetype)initWithValues:(const int64_t [])values
  1956. forKeys:(const uint32_t [])keys
  1957. count:(NSUInteger)count {
  1958. self = [super init];
  1959. if (self) {
  1960. _dictionary = [[NSMutableDictionary alloc] init];
  1961. if (count && values && keys) {
  1962. for (NSUInteger i = 0; i < count; ++i) {
  1963. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1964. }
  1965. }
  1966. }
  1967. return self;
  1968. }
  1969. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1970. self = [self initWithValues:NULL forKeys:NULL count:0];
  1971. if (self) {
  1972. if (dictionary) {
  1973. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1974. }
  1975. }
  1976. return self;
  1977. }
  1978. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1979. #pragma unused(numItems)
  1980. return [self initWithValues:NULL forKeys:NULL count:0];
  1981. }
  1982. - (void)dealloc {
  1983. NSAssert(!_autocreator,
  1984. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1985. [self class], _autocreator);
  1986. [_dictionary release];
  1987. [super dealloc];
  1988. }
  1989. - (instancetype)copyWithZone:(NSZone *)zone {
  1990. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  1991. }
  1992. - (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
  1993. if (self == other) {
  1994. return YES;
  1995. }
  1996. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  1997. return NO;
  1998. }
  1999. return [_dictionary isEqual:other->_dictionary];
  2000. }
  2001. - (NSUInteger)hash {
  2002. return _dictionary.count;
  2003. }
  2004. - (NSString *)description {
  2005. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2006. }
  2007. - (NSUInteger)count {
  2008. return _dictionary.count;
  2009. }
  2010. - (void)enumerateKeysAndValuesUsingBlock:
  2011. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2012. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2013. NSNumber *aValue,
  2014. BOOL *stop) {
  2015. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2016. }];
  2017. }
  2018. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2019. NSUInteger count = _dictionary.count;
  2020. if (count == 0) {
  2021. return 0;
  2022. }
  2023. GPBType valueType = GPBGetFieldType(field);
  2024. GPBType keyType = field.mapKeyType;
  2025. __block size_t result = 0;
  2026. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2027. NSNumber *aValue,
  2028. BOOL *stop) {
  2029. #pragma unused(stop)
  2030. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2031. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  2032. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2033. }];
  2034. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2035. result += tagSize * count;
  2036. return result;
  2037. }
  2038. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2039. asField:(GPBFieldDescriptor *)field {
  2040. GPBType valueType = GPBGetFieldType(field);
  2041. GPBType keyType = field.mapKeyType;
  2042. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2043. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2044. NSNumber *aValue,
  2045. BOOL *stop) {
  2046. #pragma unused(stop)
  2047. // Write the tag.
  2048. [outputStream writeInt32NoTag:tag];
  2049. // Write the size of the message.
  2050. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2051. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  2052. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2053. // Write the fields.
  2054. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2055. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  2056. }];
  2057. }
  2058. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2059. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2060. }
  2061. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2062. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2063. #pragma unused(stop)
  2064. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2065. }];
  2066. }
  2067. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  2068. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2069. if (wrapped && value) {
  2070. *value = [wrapped longLongValue];
  2071. }
  2072. return (wrapped != NULL);
  2073. }
  2074. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2075. if (otherDictionary) {
  2076. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2077. if (_autocreator) {
  2078. GPBAutocreatedDictionaryModified(_autocreator, self);
  2079. }
  2080. }
  2081. }
  2082. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  2083. [_dictionary setObject:@(value) forKey:@(key)];
  2084. if (_autocreator) {
  2085. GPBAutocreatedDictionaryModified(_autocreator, self);
  2086. }
  2087. }
  2088. - (void)removeValueForKey:(uint32_t)aKey {
  2089. [_dictionary removeObjectForKey:@(aKey)];
  2090. }
  2091. - (void)removeAll {
  2092. [_dictionary removeAllObjects];
  2093. }
  2094. @end
  2095. #pragma mark - UInt32 -> Bool
  2096. @implementation GPBUInt32BoolDictionary {
  2097. @package
  2098. NSMutableDictionary *_dictionary;
  2099. }
  2100. + (instancetype)dictionary {
  2101. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2102. }
  2103. + (instancetype)dictionaryWithValue:(BOOL)value
  2104. forKey:(uint32_t)key {
  2105. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2106. // on to get the type correct.
  2107. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2108. forKeys:&key
  2109. count:1] autorelease];
  2110. }
  2111. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2112. forKeys:(const uint32_t [])keys
  2113. count:(NSUInteger)count {
  2114. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2115. // on to get the type correct.
  2116. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2117. forKeys:keys
  2118. count:count] autorelease];
  2119. }
  2120. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2121. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2122. // on to get the type correct.
  2123. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2124. }
  2125. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2126. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2127. }
  2128. - (instancetype)init {
  2129. return [self initWithValues:NULL forKeys:NULL count:0];
  2130. }
  2131. - (instancetype)initWithValues:(const BOOL [])values
  2132. forKeys:(const uint32_t [])keys
  2133. count:(NSUInteger)count {
  2134. self = [super init];
  2135. if (self) {
  2136. _dictionary = [[NSMutableDictionary alloc] init];
  2137. if (count && values && keys) {
  2138. for (NSUInteger i = 0; i < count; ++i) {
  2139. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2140. }
  2141. }
  2142. }
  2143. return self;
  2144. }
  2145. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2146. self = [self initWithValues:NULL forKeys:NULL count:0];
  2147. if (self) {
  2148. if (dictionary) {
  2149. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2150. }
  2151. }
  2152. return self;
  2153. }
  2154. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2155. #pragma unused(numItems)
  2156. return [self initWithValues:NULL forKeys:NULL count:0];
  2157. }
  2158. - (void)dealloc {
  2159. NSAssert(!_autocreator,
  2160. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2161. [self class], _autocreator);
  2162. [_dictionary release];
  2163. [super dealloc];
  2164. }
  2165. - (instancetype)copyWithZone:(NSZone *)zone {
  2166. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2167. }
  2168. - (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
  2169. if (self == other) {
  2170. return YES;
  2171. }
  2172. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2173. return NO;
  2174. }
  2175. return [_dictionary isEqual:other->_dictionary];
  2176. }
  2177. - (NSUInteger)hash {
  2178. return _dictionary.count;
  2179. }
  2180. - (NSString *)description {
  2181. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2182. }
  2183. - (NSUInteger)count {
  2184. return _dictionary.count;
  2185. }
  2186. - (void)enumerateKeysAndValuesUsingBlock:
  2187. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2188. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2189. NSNumber *aValue,
  2190. BOOL *stop) {
  2191. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2192. }];
  2193. }
  2194. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2195. NSUInteger count = _dictionary.count;
  2196. if (count == 0) {
  2197. return 0;
  2198. }
  2199. GPBType valueType = GPBGetFieldType(field);
  2200. GPBType keyType = field.mapKeyType;
  2201. __block size_t result = 0;
  2202. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2203. NSNumber *aValue,
  2204. BOOL *stop) {
  2205. #pragma unused(stop)
  2206. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2207. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  2208. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2209. }];
  2210. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2211. result += tagSize * count;
  2212. return result;
  2213. }
  2214. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2215. asField:(GPBFieldDescriptor *)field {
  2216. GPBType valueType = GPBGetFieldType(field);
  2217. GPBType keyType = field.mapKeyType;
  2218. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2219. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2220. NSNumber *aValue,
  2221. BOOL *stop) {
  2222. #pragma unused(stop)
  2223. // Write the tag.
  2224. [outputStream writeInt32NoTag:tag];
  2225. // Write the size of the message.
  2226. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2227. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  2228. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2229. // Write the fields.
  2230. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2231. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  2232. }];
  2233. }
  2234. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2235. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2236. }
  2237. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2238. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2239. #pragma unused(stop)
  2240. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2241. }];
  2242. }
  2243. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2244. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2245. if (wrapped && value) {
  2246. *value = [wrapped boolValue];
  2247. }
  2248. return (wrapped != NULL);
  2249. }
  2250. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2251. if (otherDictionary) {
  2252. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2253. if (_autocreator) {
  2254. GPBAutocreatedDictionaryModified(_autocreator, self);
  2255. }
  2256. }
  2257. }
  2258. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2259. [_dictionary setObject:@(value) forKey:@(key)];
  2260. if (_autocreator) {
  2261. GPBAutocreatedDictionaryModified(_autocreator, self);
  2262. }
  2263. }
  2264. - (void)removeValueForKey:(uint32_t)aKey {
  2265. [_dictionary removeObjectForKey:@(aKey)];
  2266. }
  2267. - (void)removeAll {
  2268. [_dictionary removeAllObjects];
  2269. }
  2270. @end
  2271. #pragma mark - UInt32 -> Float
  2272. @implementation GPBUInt32FloatDictionary {
  2273. @package
  2274. NSMutableDictionary *_dictionary;
  2275. }
  2276. + (instancetype)dictionary {
  2277. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2278. }
  2279. + (instancetype)dictionaryWithValue:(float)value
  2280. forKey:(uint32_t)key {
  2281. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2282. // on to get the type correct.
  2283. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2284. forKeys:&key
  2285. count:1] autorelease];
  2286. }
  2287. + (instancetype)dictionaryWithValues:(const float [])values
  2288. forKeys:(const uint32_t [])keys
  2289. count:(NSUInteger)count {
  2290. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2291. // on to get the type correct.
  2292. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2293. forKeys:keys
  2294. count:count] autorelease];
  2295. }
  2296. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2297. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2298. // on to get the type correct.
  2299. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2300. }
  2301. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2302. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2303. }
  2304. - (instancetype)init {
  2305. return [self initWithValues:NULL forKeys:NULL count:0];
  2306. }
  2307. - (instancetype)initWithValues:(const float [])values
  2308. forKeys:(const uint32_t [])keys
  2309. count:(NSUInteger)count {
  2310. self = [super init];
  2311. if (self) {
  2312. _dictionary = [[NSMutableDictionary alloc] init];
  2313. if (count && values && keys) {
  2314. for (NSUInteger i = 0; i < count; ++i) {
  2315. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2316. }
  2317. }
  2318. }
  2319. return self;
  2320. }
  2321. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2322. self = [self initWithValues:NULL forKeys:NULL count:0];
  2323. if (self) {
  2324. if (dictionary) {
  2325. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2326. }
  2327. }
  2328. return self;
  2329. }
  2330. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2331. #pragma unused(numItems)
  2332. return [self initWithValues:NULL forKeys:NULL count:0];
  2333. }
  2334. - (void)dealloc {
  2335. NSAssert(!_autocreator,
  2336. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2337. [self class], _autocreator);
  2338. [_dictionary release];
  2339. [super dealloc];
  2340. }
  2341. - (instancetype)copyWithZone:(NSZone *)zone {
  2342. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2343. }
  2344. - (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
  2345. if (self == other) {
  2346. return YES;
  2347. }
  2348. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2349. return NO;
  2350. }
  2351. return [_dictionary isEqual:other->_dictionary];
  2352. }
  2353. - (NSUInteger)hash {
  2354. return _dictionary.count;
  2355. }
  2356. - (NSString *)description {
  2357. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2358. }
  2359. - (NSUInteger)count {
  2360. return _dictionary.count;
  2361. }
  2362. - (void)enumerateKeysAndValuesUsingBlock:
  2363. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2364. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2365. NSNumber *aValue,
  2366. BOOL *stop) {
  2367. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2368. }];
  2369. }
  2370. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2371. NSUInteger count = _dictionary.count;
  2372. if (count == 0) {
  2373. return 0;
  2374. }
  2375. GPBType valueType = GPBGetFieldType(field);
  2376. GPBType keyType = field.mapKeyType;
  2377. __block size_t result = 0;
  2378. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2379. NSNumber *aValue,
  2380. BOOL *stop) {
  2381. #pragma unused(stop)
  2382. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2383. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  2384. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2385. }];
  2386. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2387. result += tagSize * count;
  2388. return result;
  2389. }
  2390. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2391. asField:(GPBFieldDescriptor *)field {
  2392. GPBType valueType = GPBGetFieldType(field);
  2393. GPBType keyType = field.mapKeyType;
  2394. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2395. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2396. NSNumber *aValue,
  2397. BOOL *stop) {
  2398. #pragma unused(stop)
  2399. // Write the tag.
  2400. [outputStream writeInt32NoTag:tag];
  2401. // Write the size of the message.
  2402. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2403. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  2404. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2405. // Write the fields.
  2406. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2407. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  2408. }];
  2409. }
  2410. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2411. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2412. }
  2413. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2414. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2415. #pragma unused(stop)
  2416. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2417. }];
  2418. }
  2419. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2420. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2421. if (wrapped && value) {
  2422. *value = [wrapped floatValue];
  2423. }
  2424. return (wrapped != NULL);
  2425. }
  2426. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2427. if (otherDictionary) {
  2428. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2429. if (_autocreator) {
  2430. GPBAutocreatedDictionaryModified(_autocreator, self);
  2431. }
  2432. }
  2433. }
  2434. - (void)setValue:(float)value forKey:(uint32_t)key {
  2435. [_dictionary setObject:@(value) forKey:@(key)];
  2436. if (_autocreator) {
  2437. GPBAutocreatedDictionaryModified(_autocreator, self);
  2438. }
  2439. }
  2440. - (void)removeValueForKey:(uint32_t)aKey {
  2441. [_dictionary removeObjectForKey:@(aKey)];
  2442. }
  2443. - (void)removeAll {
  2444. [_dictionary removeAllObjects];
  2445. }
  2446. @end
  2447. #pragma mark - UInt32 -> Double
  2448. @implementation GPBUInt32DoubleDictionary {
  2449. @package
  2450. NSMutableDictionary *_dictionary;
  2451. }
  2452. + (instancetype)dictionary {
  2453. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2454. }
  2455. + (instancetype)dictionaryWithValue:(double)value
  2456. forKey:(uint32_t)key {
  2457. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2458. // on to get the type correct.
  2459. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2460. forKeys:&key
  2461. count:1] autorelease];
  2462. }
  2463. + (instancetype)dictionaryWithValues:(const double [])values
  2464. forKeys:(const uint32_t [])keys
  2465. count:(NSUInteger)count {
  2466. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2467. // on to get the type correct.
  2468. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2469. forKeys:keys
  2470. count:count] autorelease];
  2471. }
  2472. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  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] initWithDictionary:dictionary] autorelease];
  2476. }
  2477. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2478. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2479. }
  2480. - (instancetype)init {
  2481. return [self initWithValues:NULL forKeys:NULL count:0];
  2482. }
  2483. - (instancetype)initWithValues:(const double [])values
  2484. forKeys:(const uint32_t [])keys
  2485. count:(NSUInteger)count {
  2486. self = [super init];
  2487. if (self) {
  2488. _dictionary = [[NSMutableDictionary alloc] init];
  2489. if (count && values && keys) {
  2490. for (NSUInteger i = 0; i < count; ++i) {
  2491. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2492. }
  2493. }
  2494. }
  2495. return self;
  2496. }
  2497. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2498. self = [self initWithValues:NULL forKeys:NULL count:0];
  2499. if (self) {
  2500. if (dictionary) {
  2501. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2502. }
  2503. }
  2504. return self;
  2505. }
  2506. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2507. #pragma unused(numItems)
  2508. return [self initWithValues:NULL forKeys:NULL count:0];
  2509. }
  2510. - (void)dealloc {
  2511. NSAssert(!_autocreator,
  2512. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2513. [self class], _autocreator);
  2514. [_dictionary release];
  2515. [super dealloc];
  2516. }
  2517. - (instancetype)copyWithZone:(NSZone *)zone {
  2518. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2519. }
  2520. - (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
  2521. if (self == other) {
  2522. return YES;
  2523. }
  2524. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2525. return NO;
  2526. }
  2527. return [_dictionary isEqual:other->_dictionary];
  2528. }
  2529. - (NSUInteger)hash {
  2530. return _dictionary.count;
  2531. }
  2532. - (NSString *)description {
  2533. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2534. }
  2535. - (NSUInteger)count {
  2536. return _dictionary.count;
  2537. }
  2538. - (void)enumerateKeysAndValuesUsingBlock:
  2539. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2540. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2541. NSNumber *aValue,
  2542. BOOL *stop) {
  2543. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2544. }];
  2545. }
  2546. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2547. NSUInteger count = _dictionary.count;
  2548. if (count == 0) {
  2549. return 0;
  2550. }
  2551. GPBType valueType = GPBGetFieldType(field);
  2552. GPBType keyType = field.mapKeyType;
  2553. __block size_t result = 0;
  2554. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2555. NSNumber *aValue,
  2556. BOOL *stop) {
  2557. #pragma unused(stop)
  2558. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2559. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  2560. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2561. }];
  2562. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2563. result += tagSize * count;
  2564. return result;
  2565. }
  2566. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2567. asField:(GPBFieldDescriptor *)field {
  2568. GPBType valueType = GPBGetFieldType(field);
  2569. GPBType keyType = field.mapKeyType;
  2570. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2571. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2572. NSNumber *aValue,
  2573. BOOL *stop) {
  2574. #pragma unused(stop)
  2575. // Write the tag.
  2576. [outputStream writeInt32NoTag:tag];
  2577. // Write the size of the message.
  2578. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2579. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  2580. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2581. // Write the fields.
  2582. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2583. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  2584. }];
  2585. }
  2586. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2587. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2588. }
  2589. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2590. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2591. #pragma unused(stop)
  2592. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2593. }];
  2594. }
  2595. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2596. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2597. if (wrapped && value) {
  2598. *value = [wrapped doubleValue];
  2599. }
  2600. return (wrapped != NULL);
  2601. }
  2602. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2603. if (otherDictionary) {
  2604. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2605. if (_autocreator) {
  2606. GPBAutocreatedDictionaryModified(_autocreator, self);
  2607. }
  2608. }
  2609. }
  2610. - (void)setValue:(double)value forKey:(uint32_t)key {
  2611. [_dictionary setObject:@(value) forKey:@(key)];
  2612. if (_autocreator) {
  2613. GPBAutocreatedDictionaryModified(_autocreator, self);
  2614. }
  2615. }
  2616. - (void)removeValueForKey:(uint32_t)aKey {
  2617. [_dictionary removeObjectForKey:@(aKey)];
  2618. }
  2619. - (void)removeAll {
  2620. [_dictionary removeAllObjects];
  2621. }
  2622. @end
  2623. #pragma mark - UInt32 -> Enum
  2624. @implementation GPBUInt32EnumDictionary {
  2625. @package
  2626. NSMutableDictionary *_dictionary;
  2627. GPBEnumValidationFunc _validationFunc;
  2628. }
  2629. @synthesize validationFunc = _validationFunc;
  2630. + (instancetype)dictionary {
  2631. return [[[self alloc] initWithValidationFunction:NULL
  2632. rawValues:NULL
  2633. forKeys:NULL
  2634. count:0] autorelease];
  2635. }
  2636. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2637. return [[[self alloc] initWithValidationFunction:func
  2638. rawValues:NULL
  2639. forKeys:NULL
  2640. count:0] autorelease];
  2641. }
  2642. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2643. rawValue:(int32_t)rawValue
  2644. forKey:(uint32_t)key {
  2645. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2646. // on to get the type correct.
  2647. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2648. rawValues:&rawValue
  2649. forKeys:&key
  2650. count:1] autorelease];
  2651. }
  2652. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2653. rawValues:(const int32_t [])rawValues
  2654. forKeys:(const uint32_t [])keys
  2655. count:(NSUInteger)count {
  2656. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2657. // on to get the type correct.
  2658. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2659. rawValues:rawValues
  2660. forKeys:keys
  2661. count:count] autorelease];
  2662. }
  2663. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2664. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2665. // on to get the type correct.
  2666. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2667. }
  2668. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2669. capacity:(NSUInteger)numItems {
  2670. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2671. }
  2672. - (instancetype)init {
  2673. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2674. }
  2675. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2676. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2677. }
  2678. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2679. rawValues:(const int32_t [])rawValues
  2680. forKeys:(const uint32_t [])keys
  2681. count:(NSUInteger)count {
  2682. self = [super init];
  2683. if (self) {
  2684. _dictionary = [[NSMutableDictionary alloc] init];
  2685. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2686. if (count && rawValues && keys) {
  2687. for (NSUInteger i = 0; i < count; ++i) {
  2688. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2689. }
  2690. }
  2691. }
  2692. return self;
  2693. }
  2694. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2695. self = [self initWithValidationFunction:dictionary.validationFunc
  2696. rawValues:NULL
  2697. forKeys:NULL
  2698. count:0];
  2699. if (self) {
  2700. if (dictionary) {
  2701. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2702. }
  2703. }
  2704. return self;
  2705. }
  2706. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2707. capacity:(NSUInteger)numItems {
  2708. #pragma unused(numItems)
  2709. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2710. }
  2711. - (void)dealloc {
  2712. NSAssert(!_autocreator,
  2713. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2714. [self class], _autocreator);
  2715. [_dictionary release];
  2716. [super dealloc];
  2717. }
  2718. - (instancetype)copyWithZone:(NSZone *)zone {
  2719. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2720. }
  2721. - (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
  2722. if (self == other) {
  2723. return YES;
  2724. }
  2725. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2726. return NO;
  2727. }
  2728. return [_dictionary isEqual:other->_dictionary];
  2729. }
  2730. - (NSUInteger)hash {
  2731. return _dictionary.count;
  2732. }
  2733. - (NSString *)description {
  2734. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2735. }
  2736. - (NSUInteger)count {
  2737. return _dictionary.count;
  2738. }
  2739. - (void)enumerateKeysAndRawValuesUsingBlock:
  2740. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2741. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2742. NSNumber *aValue,
  2743. BOOL *stop) {
  2744. block([aKey unsignedIntValue], [aValue intValue], stop);
  2745. }];
  2746. }
  2747. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2748. NSUInteger count = _dictionary.count;
  2749. if (count == 0) {
  2750. return 0;
  2751. }
  2752. GPBType valueType = GPBGetFieldType(field);
  2753. GPBType keyType = field.mapKeyType;
  2754. __block size_t result = 0;
  2755. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2756. NSNumber *aValue,
  2757. BOOL *stop) {
  2758. #pragma unused(stop)
  2759. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2760. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  2761. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2762. }];
  2763. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  2764. result += tagSize * count;
  2765. return result;
  2766. }
  2767. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2768. asField:(GPBFieldDescriptor *)field {
  2769. GPBType valueType = GPBGetFieldType(field);
  2770. GPBType keyType = field.mapKeyType;
  2771. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2772. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2773. NSNumber *aValue,
  2774. BOOL *stop) {
  2775. #pragma unused(stop)
  2776. // Write the tag.
  2777. [outputStream writeInt32NoTag:tag];
  2778. // Write the size of the message.
  2779. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2780. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  2781. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2782. // Write the fields.
  2783. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  2784. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  2785. }];
  2786. }
  2787. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2788. forKey:(GPBValue *)key
  2789. keyType:(GPBType)keyType {
  2790. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyType);
  2791. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  2792. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2793. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2794. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyType);
  2795. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  2796. [outputStream release];
  2797. return data;
  2798. }
  2799. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  2800. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2801. }
  2802. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2803. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2804. #pragma unused(stop)
  2805. block([NSString stringWithFormat:@"%u", key], @(value));
  2806. }];
  2807. }
  2808. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2809. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2810. if (wrapped && value) {
  2811. int32_t result = [wrapped intValue];
  2812. if (!_validationFunc(result)) {
  2813. result = kGPBUnrecognizedEnumeratorValue;
  2814. }
  2815. *value = result;
  2816. }
  2817. return (wrapped != NULL);
  2818. }
  2819. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2820. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2821. if (wrapped && rawValue) {
  2822. *rawValue = [wrapped intValue];
  2823. }
  2824. return (wrapped != NULL);
  2825. }
  2826. - (void)enumerateKeysAndValuesUsingBlock:
  2827. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2828. GPBEnumValidationFunc func = _validationFunc;
  2829. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2830. NSNumber *aValue,
  2831. BOOL *stop) {
  2832. int32_t unwrapped = [aValue intValue];
  2833. if (!func(unwrapped)) {
  2834. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2835. }
  2836. block([aKey unsignedIntValue], unwrapped, stop);
  2837. }];
  2838. }
  2839. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2840. if (otherDictionary) {
  2841. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2842. if (_autocreator) {
  2843. GPBAutocreatedDictionaryModified(_autocreator, self);
  2844. }
  2845. }
  2846. }
  2847. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2848. [_dictionary setObject:@(value) forKey:@(key)];
  2849. if (_autocreator) {
  2850. GPBAutocreatedDictionaryModified(_autocreator, self);
  2851. }
  2852. }
  2853. - (void)removeValueForKey:(uint32_t)aKey {
  2854. [_dictionary removeObjectForKey:@(aKey)];
  2855. }
  2856. - (void)removeAll {
  2857. [_dictionary removeAllObjects];
  2858. }
  2859. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2860. if (!_validationFunc(value)) {
  2861. [NSException raise:NSInvalidArgumentException
  2862. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2863. value];
  2864. }
  2865. [_dictionary setObject:@(value) forKey:@(key)];
  2866. if (_autocreator) {
  2867. GPBAutocreatedDictionaryModified(_autocreator, self);
  2868. }
  2869. }
  2870. @end
  2871. #pragma mark - UInt32 -> Object
  2872. @implementation GPBUInt32ObjectDictionary {
  2873. @package
  2874. NSMutableDictionary *_dictionary;
  2875. }
  2876. + (instancetype)dictionary {
  2877. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2878. }
  2879. + (instancetype)dictionaryWithValue:(id)value
  2880. forKey:(uint32_t)key {
  2881. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2882. // on to get the type correct.
  2883. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithValues:&value
  2884. forKeys:&key
  2885. count:1] autorelease];
  2886. }
  2887. + (instancetype)dictionaryWithValues:(const id [])values
  2888. forKeys:(const uint32_t [])keys
  2889. count:(NSUInteger)count {
  2890. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2891. // on to get the type correct.
  2892. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithValues:values
  2893. forKeys:keys
  2894. count:count] autorelease];
  2895. }
  2896. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2897. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2898. // on to get the type correct.
  2899. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2900. }
  2901. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2902. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2903. }
  2904. - (instancetype)init {
  2905. return [self initWithValues:NULL forKeys:NULL count:0];
  2906. }
  2907. - (instancetype)initWithValues:(const id [])values
  2908. forKeys:(const uint32_t [])keys
  2909. count:(NSUInteger)count {
  2910. self = [super init];
  2911. if (self) {
  2912. _dictionary = [[NSMutableDictionary alloc] init];
  2913. if (count && values && keys) {
  2914. for (NSUInteger i = 0; i < count; ++i) {
  2915. [_dictionary setObject:values[i] forKey:@(keys[i])];
  2916. }
  2917. }
  2918. }
  2919. return self;
  2920. }
  2921. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2922. self = [self initWithValues:NULL forKeys:NULL count:0];
  2923. if (self) {
  2924. if (dictionary) {
  2925. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2926. }
  2927. }
  2928. return self;
  2929. }
  2930. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2931. #pragma unused(numItems)
  2932. return [self initWithValues:NULL forKeys:NULL count:0];
  2933. }
  2934. - (void)dealloc {
  2935. NSAssert(!_autocreator,
  2936. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2937. [self class], _autocreator);
  2938. [_dictionary release];
  2939. [super dealloc];
  2940. }
  2941. - (instancetype)copyWithZone:(NSZone *)zone {
  2942. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2943. }
  2944. - (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
  2945. if (self == other) {
  2946. return YES;
  2947. }
  2948. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2949. return NO;
  2950. }
  2951. return [_dictionary isEqual:other->_dictionary];
  2952. }
  2953. - (NSUInteger)hash {
  2954. return _dictionary.count;
  2955. }
  2956. - (NSString *)description {
  2957. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2958. }
  2959. - (NSUInteger)count {
  2960. return _dictionary.count;
  2961. }
  2962. - (void)enumerateKeysAndValuesUsingBlock:
  2963. (void (^)(uint32_t key, id value, BOOL *stop))block {
  2964. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2965. id aValue,
  2966. BOOL *stop) {
  2967. block([aKey unsignedIntValue], aValue, stop);
  2968. }];
  2969. }
  2970. - (BOOL)isInitialized {
  2971. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2972. if (!msg.initialized) {
  2973. return NO;
  2974. }
  2975. }
  2976. return YES;
  2977. }
  2978. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2979. GPBUInt32ObjectDictionary *newDict =
  2980. [[GPBUInt32ObjectDictionary alloc] init];
  2981. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  2982. GPBMessage *msg,
  2983. BOOL *stop) {
  2984. #pragma unused(stop)
  2985. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  2986. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  2987. [copiedMsg release];
  2988. }];
  2989. return newDict;
  2990. }
  2991. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2992. NSUInteger count = _dictionary.count;
  2993. if (count == 0) {
  2994. return 0;
  2995. }
  2996. GPBType valueType = GPBGetFieldType(field);
  2997. GPBType keyType = field.mapKeyType;
  2998. __block size_t result = 0;
  2999. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3000. id aValue,
  3001. BOOL *stop) {
  3002. #pragma unused(stop)
  3003. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  3004. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  3005. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3006. }];
  3007. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3008. result += tagSize * count;
  3009. return result;
  3010. }
  3011. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3012. asField:(GPBFieldDescriptor *)field {
  3013. GPBType valueType = GPBGetFieldType(field);
  3014. GPBType keyType = field.mapKeyType;
  3015. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3016. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3017. id aValue,
  3018. BOOL *stop) {
  3019. #pragma unused(stop)
  3020. // Write the tag.
  3021. [outputStream writeInt32NoTag:tag];
  3022. // Write the size of the message.
  3023. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  3024. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  3025. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3026. // Write the fields.
  3027. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyType);
  3028. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  3029. }];
  3030. }
  3031. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3032. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3033. }
  3034. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3035. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, id value, BOOL *stop) {
  3036. #pragma unused(stop)
  3037. block([NSString stringWithFormat:@"%u", key], value);
  3038. }];
  3039. }
  3040. - (id)valueForKey:(uint32_t)key {
  3041. id result = [_dictionary objectForKey:@(key)];
  3042. return result;
  3043. }
  3044. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3045. if (otherDictionary) {
  3046. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3047. if (_autocreator) {
  3048. GPBAutocreatedDictionaryModified(_autocreator, self);
  3049. }
  3050. }
  3051. }
  3052. - (void)setValue:(id)value forKey:(uint32_t)key {
  3053. [_dictionary setObject:value forKey:@(key)];
  3054. if (_autocreator) {
  3055. GPBAutocreatedDictionaryModified(_autocreator, self);
  3056. }
  3057. }
  3058. - (void)removeValueForKey:(uint32_t)aKey {
  3059. [_dictionary removeObjectForKey:@(aKey)];
  3060. }
  3061. - (void)removeAll {
  3062. [_dictionary removeAllObjects];
  3063. }
  3064. @end
  3065. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3066. // This block of code is generated, do not edit it directly.
  3067. #pragma mark - Int32 -> UInt32
  3068. @implementation GPBInt32UInt32Dictionary {
  3069. @package
  3070. NSMutableDictionary *_dictionary;
  3071. }
  3072. + (instancetype)dictionary {
  3073. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3074. }
  3075. + (instancetype)dictionaryWithValue:(uint32_t)value
  3076. forKey:(int32_t)key {
  3077. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3078. // on to get the type correct.
  3079. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  3080. forKeys:&key
  3081. count:1] autorelease];
  3082. }
  3083. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  3084. forKeys:(const int32_t [])keys
  3085. count:(NSUInteger)count {
  3086. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3087. // on to get the type correct.
  3088. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  3089. forKeys:keys
  3090. count:count] autorelease];
  3091. }
  3092. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3093. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3094. // on to get the type correct.
  3095. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3096. }
  3097. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3098. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3099. }
  3100. - (instancetype)init {
  3101. return [self initWithValues:NULL forKeys:NULL count:0];
  3102. }
  3103. - (instancetype)initWithValues:(const uint32_t [])values
  3104. forKeys:(const int32_t [])keys
  3105. count:(NSUInteger)count {
  3106. self = [super init];
  3107. if (self) {
  3108. _dictionary = [[NSMutableDictionary alloc] init];
  3109. if (count && values && keys) {
  3110. for (NSUInteger i = 0; i < count; ++i) {
  3111. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3112. }
  3113. }
  3114. }
  3115. return self;
  3116. }
  3117. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3118. self = [self initWithValues:NULL forKeys:NULL count:0];
  3119. if (self) {
  3120. if (dictionary) {
  3121. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3122. }
  3123. }
  3124. return self;
  3125. }
  3126. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3127. #pragma unused(numItems)
  3128. return [self initWithValues:NULL forKeys:NULL count:0];
  3129. }
  3130. - (void)dealloc {
  3131. NSAssert(!_autocreator,
  3132. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3133. [self class], _autocreator);
  3134. [_dictionary release];
  3135. [super dealloc];
  3136. }
  3137. - (instancetype)copyWithZone:(NSZone *)zone {
  3138. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3139. }
  3140. - (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
  3141. if (self == other) {
  3142. return YES;
  3143. }
  3144. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3145. return NO;
  3146. }
  3147. return [_dictionary isEqual:other->_dictionary];
  3148. }
  3149. - (NSUInteger)hash {
  3150. return _dictionary.count;
  3151. }
  3152. - (NSString *)description {
  3153. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3154. }
  3155. - (NSUInteger)count {
  3156. return _dictionary.count;
  3157. }
  3158. - (void)enumerateKeysAndValuesUsingBlock:
  3159. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3160. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3161. NSNumber *aValue,
  3162. BOOL *stop) {
  3163. block([aKey intValue], [aValue unsignedIntValue], stop);
  3164. }];
  3165. }
  3166. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3167. NSUInteger count = _dictionary.count;
  3168. if (count == 0) {
  3169. return 0;
  3170. }
  3171. GPBType valueType = GPBGetFieldType(field);
  3172. GPBType keyType = field.mapKeyType;
  3173. __block size_t result = 0;
  3174. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3175. NSNumber *aValue,
  3176. BOOL *stop) {
  3177. #pragma unused(stop)
  3178. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3179. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  3180. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3181. }];
  3182. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3183. result += tagSize * count;
  3184. return result;
  3185. }
  3186. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3187. asField:(GPBFieldDescriptor *)field {
  3188. GPBType valueType = GPBGetFieldType(field);
  3189. GPBType keyType = field.mapKeyType;
  3190. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3191. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3192. NSNumber *aValue,
  3193. BOOL *stop) {
  3194. #pragma unused(stop)
  3195. // Write the tag.
  3196. [outputStream writeInt32NoTag:tag];
  3197. // Write the size of the message.
  3198. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3199. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  3200. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3201. // Write the fields.
  3202. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3203. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  3204. }];
  3205. }
  3206. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3207. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3208. }
  3209. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3210. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3211. #pragma unused(stop)
  3212. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3213. }];
  3214. }
  3215. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3216. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3217. if (wrapped && value) {
  3218. *value = [wrapped unsignedIntValue];
  3219. }
  3220. return (wrapped != NULL);
  3221. }
  3222. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3223. if (otherDictionary) {
  3224. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3225. if (_autocreator) {
  3226. GPBAutocreatedDictionaryModified(_autocreator, self);
  3227. }
  3228. }
  3229. }
  3230. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3231. [_dictionary setObject:@(value) forKey:@(key)];
  3232. if (_autocreator) {
  3233. GPBAutocreatedDictionaryModified(_autocreator, self);
  3234. }
  3235. }
  3236. - (void)removeValueForKey:(int32_t)aKey {
  3237. [_dictionary removeObjectForKey:@(aKey)];
  3238. }
  3239. - (void)removeAll {
  3240. [_dictionary removeAllObjects];
  3241. }
  3242. @end
  3243. #pragma mark - Int32 -> Int32
  3244. @implementation GPBInt32Int32Dictionary {
  3245. @package
  3246. NSMutableDictionary *_dictionary;
  3247. }
  3248. + (instancetype)dictionary {
  3249. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3250. }
  3251. + (instancetype)dictionaryWithValue:(int32_t)value
  3252. forKey:(int32_t)key {
  3253. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3254. // on to get the type correct.
  3255. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3256. forKeys:&key
  3257. count:1] autorelease];
  3258. }
  3259. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3260. forKeys:(const int32_t [])keys
  3261. count:(NSUInteger)count {
  3262. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3263. // on to get the type correct.
  3264. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3265. forKeys:keys
  3266. count:count] autorelease];
  3267. }
  3268. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3269. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3270. // on to get the type correct.
  3271. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3272. }
  3273. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3274. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3275. }
  3276. - (instancetype)init {
  3277. return [self initWithValues:NULL forKeys:NULL count:0];
  3278. }
  3279. - (instancetype)initWithValues:(const int32_t [])values
  3280. forKeys:(const int32_t [])keys
  3281. count:(NSUInteger)count {
  3282. self = [super init];
  3283. if (self) {
  3284. _dictionary = [[NSMutableDictionary alloc] init];
  3285. if (count && values && keys) {
  3286. for (NSUInteger i = 0; i < count; ++i) {
  3287. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3288. }
  3289. }
  3290. }
  3291. return self;
  3292. }
  3293. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3294. self = [self initWithValues:NULL forKeys:NULL count:0];
  3295. if (self) {
  3296. if (dictionary) {
  3297. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3298. }
  3299. }
  3300. return self;
  3301. }
  3302. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3303. #pragma unused(numItems)
  3304. return [self initWithValues:NULL forKeys:NULL count:0];
  3305. }
  3306. - (void)dealloc {
  3307. NSAssert(!_autocreator,
  3308. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3309. [self class], _autocreator);
  3310. [_dictionary release];
  3311. [super dealloc];
  3312. }
  3313. - (instancetype)copyWithZone:(NSZone *)zone {
  3314. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3315. }
  3316. - (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
  3317. if (self == other) {
  3318. return YES;
  3319. }
  3320. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3321. return NO;
  3322. }
  3323. return [_dictionary isEqual:other->_dictionary];
  3324. }
  3325. - (NSUInteger)hash {
  3326. return _dictionary.count;
  3327. }
  3328. - (NSString *)description {
  3329. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3330. }
  3331. - (NSUInteger)count {
  3332. return _dictionary.count;
  3333. }
  3334. - (void)enumerateKeysAndValuesUsingBlock:
  3335. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3336. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3337. NSNumber *aValue,
  3338. BOOL *stop) {
  3339. block([aKey intValue], [aValue intValue], stop);
  3340. }];
  3341. }
  3342. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3343. NSUInteger count = _dictionary.count;
  3344. if (count == 0) {
  3345. return 0;
  3346. }
  3347. GPBType valueType = GPBGetFieldType(field);
  3348. GPBType keyType = field.mapKeyType;
  3349. __block size_t result = 0;
  3350. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3351. NSNumber *aValue,
  3352. BOOL *stop) {
  3353. #pragma unused(stop)
  3354. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3355. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  3356. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3357. }];
  3358. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3359. result += tagSize * count;
  3360. return result;
  3361. }
  3362. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3363. asField:(GPBFieldDescriptor *)field {
  3364. GPBType valueType = GPBGetFieldType(field);
  3365. GPBType keyType = field.mapKeyType;
  3366. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3367. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3368. NSNumber *aValue,
  3369. BOOL *stop) {
  3370. #pragma unused(stop)
  3371. // Write the tag.
  3372. [outputStream writeInt32NoTag:tag];
  3373. // Write the size of the message.
  3374. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3375. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  3376. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3377. // Write the fields.
  3378. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3379. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  3380. }];
  3381. }
  3382. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3383. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3384. }
  3385. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3386. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3387. #pragma unused(stop)
  3388. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3389. }];
  3390. }
  3391. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3392. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3393. if (wrapped && value) {
  3394. *value = [wrapped intValue];
  3395. }
  3396. return (wrapped != NULL);
  3397. }
  3398. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3399. if (otherDictionary) {
  3400. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3401. if (_autocreator) {
  3402. GPBAutocreatedDictionaryModified(_autocreator, self);
  3403. }
  3404. }
  3405. }
  3406. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3407. [_dictionary setObject:@(value) forKey:@(key)];
  3408. if (_autocreator) {
  3409. GPBAutocreatedDictionaryModified(_autocreator, self);
  3410. }
  3411. }
  3412. - (void)removeValueForKey:(int32_t)aKey {
  3413. [_dictionary removeObjectForKey:@(aKey)];
  3414. }
  3415. - (void)removeAll {
  3416. [_dictionary removeAllObjects];
  3417. }
  3418. @end
  3419. #pragma mark - Int32 -> UInt64
  3420. @implementation GPBInt32UInt64Dictionary {
  3421. @package
  3422. NSMutableDictionary *_dictionary;
  3423. }
  3424. + (instancetype)dictionary {
  3425. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3426. }
  3427. + (instancetype)dictionaryWithValue:(uint64_t)value
  3428. forKey:(int32_t)key {
  3429. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3430. // on to get the type correct.
  3431. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3432. forKeys:&key
  3433. count:1] autorelease];
  3434. }
  3435. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3436. forKeys:(const int32_t [])keys
  3437. count:(NSUInteger)count {
  3438. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3439. // on to get the type correct.
  3440. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3441. forKeys:keys
  3442. count:count] autorelease];
  3443. }
  3444. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3445. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3446. // on to get the type correct.
  3447. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3448. }
  3449. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3450. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3451. }
  3452. - (instancetype)init {
  3453. return [self initWithValues:NULL forKeys:NULL count:0];
  3454. }
  3455. - (instancetype)initWithValues:(const uint64_t [])values
  3456. forKeys:(const int32_t [])keys
  3457. count:(NSUInteger)count {
  3458. self = [super init];
  3459. if (self) {
  3460. _dictionary = [[NSMutableDictionary alloc] init];
  3461. if (count && values && keys) {
  3462. for (NSUInteger i = 0; i < count; ++i) {
  3463. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3464. }
  3465. }
  3466. }
  3467. return self;
  3468. }
  3469. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3470. self = [self initWithValues:NULL forKeys:NULL count:0];
  3471. if (self) {
  3472. if (dictionary) {
  3473. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3474. }
  3475. }
  3476. return self;
  3477. }
  3478. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3479. #pragma unused(numItems)
  3480. return [self initWithValues:NULL forKeys:NULL count:0];
  3481. }
  3482. - (void)dealloc {
  3483. NSAssert(!_autocreator,
  3484. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3485. [self class], _autocreator);
  3486. [_dictionary release];
  3487. [super dealloc];
  3488. }
  3489. - (instancetype)copyWithZone:(NSZone *)zone {
  3490. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3491. }
  3492. - (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
  3493. if (self == other) {
  3494. return YES;
  3495. }
  3496. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3497. return NO;
  3498. }
  3499. return [_dictionary isEqual:other->_dictionary];
  3500. }
  3501. - (NSUInteger)hash {
  3502. return _dictionary.count;
  3503. }
  3504. - (NSString *)description {
  3505. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3506. }
  3507. - (NSUInteger)count {
  3508. return _dictionary.count;
  3509. }
  3510. - (void)enumerateKeysAndValuesUsingBlock:
  3511. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3512. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3513. NSNumber *aValue,
  3514. BOOL *stop) {
  3515. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3516. }];
  3517. }
  3518. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3519. NSUInteger count = _dictionary.count;
  3520. if (count == 0) {
  3521. return 0;
  3522. }
  3523. GPBType valueType = GPBGetFieldType(field);
  3524. GPBType keyType = field.mapKeyType;
  3525. __block size_t result = 0;
  3526. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3527. NSNumber *aValue,
  3528. BOOL *stop) {
  3529. #pragma unused(stop)
  3530. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3531. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  3532. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3533. }];
  3534. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3535. result += tagSize * count;
  3536. return result;
  3537. }
  3538. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3539. asField:(GPBFieldDescriptor *)field {
  3540. GPBType valueType = GPBGetFieldType(field);
  3541. GPBType keyType = field.mapKeyType;
  3542. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3543. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3544. NSNumber *aValue,
  3545. BOOL *stop) {
  3546. #pragma unused(stop)
  3547. // Write the tag.
  3548. [outputStream writeInt32NoTag:tag];
  3549. // Write the size of the message.
  3550. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3551. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  3552. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3553. // Write the fields.
  3554. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3555. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  3556. }];
  3557. }
  3558. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3559. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3560. }
  3561. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3562. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3563. #pragma unused(stop)
  3564. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3565. }];
  3566. }
  3567. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3568. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3569. if (wrapped && value) {
  3570. *value = [wrapped unsignedLongLongValue];
  3571. }
  3572. return (wrapped != NULL);
  3573. }
  3574. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3575. if (otherDictionary) {
  3576. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3577. if (_autocreator) {
  3578. GPBAutocreatedDictionaryModified(_autocreator, self);
  3579. }
  3580. }
  3581. }
  3582. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3583. [_dictionary setObject:@(value) forKey:@(key)];
  3584. if (_autocreator) {
  3585. GPBAutocreatedDictionaryModified(_autocreator, self);
  3586. }
  3587. }
  3588. - (void)removeValueForKey:(int32_t)aKey {
  3589. [_dictionary removeObjectForKey:@(aKey)];
  3590. }
  3591. - (void)removeAll {
  3592. [_dictionary removeAllObjects];
  3593. }
  3594. @end
  3595. #pragma mark - Int32 -> Int64
  3596. @implementation GPBInt32Int64Dictionary {
  3597. @package
  3598. NSMutableDictionary *_dictionary;
  3599. }
  3600. + (instancetype)dictionary {
  3601. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3602. }
  3603. + (instancetype)dictionaryWithValue:(int64_t)value
  3604. forKey:(int32_t)key {
  3605. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3606. // on to get the type correct.
  3607. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3608. forKeys:&key
  3609. count:1] autorelease];
  3610. }
  3611. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3612. forKeys:(const int32_t [])keys
  3613. count:(NSUInteger)count {
  3614. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3615. // on to get the type correct.
  3616. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3617. forKeys:keys
  3618. count:count] autorelease];
  3619. }
  3620. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3621. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3622. // on to get the type correct.
  3623. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3624. }
  3625. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3626. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3627. }
  3628. - (instancetype)init {
  3629. return [self initWithValues:NULL forKeys:NULL count:0];
  3630. }
  3631. - (instancetype)initWithValues:(const int64_t [])values
  3632. forKeys:(const int32_t [])keys
  3633. count:(NSUInteger)count {
  3634. self = [super init];
  3635. if (self) {
  3636. _dictionary = [[NSMutableDictionary alloc] init];
  3637. if (count && values && keys) {
  3638. for (NSUInteger i = 0; i < count; ++i) {
  3639. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3640. }
  3641. }
  3642. }
  3643. return self;
  3644. }
  3645. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3646. self = [self initWithValues:NULL forKeys:NULL count:0];
  3647. if (self) {
  3648. if (dictionary) {
  3649. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3650. }
  3651. }
  3652. return self;
  3653. }
  3654. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3655. #pragma unused(numItems)
  3656. return [self initWithValues:NULL forKeys:NULL count:0];
  3657. }
  3658. - (void)dealloc {
  3659. NSAssert(!_autocreator,
  3660. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3661. [self class], _autocreator);
  3662. [_dictionary release];
  3663. [super dealloc];
  3664. }
  3665. - (instancetype)copyWithZone:(NSZone *)zone {
  3666. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3667. }
  3668. - (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
  3669. if (self == other) {
  3670. return YES;
  3671. }
  3672. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3673. return NO;
  3674. }
  3675. return [_dictionary isEqual:other->_dictionary];
  3676. }
  3677. - (NSUInteger)hash {
  3678. return _dictionary.count;
  3679. }
  3680. - (NSString *)description {
  3681. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3682. }
  3683. - (NSUInteger)count {
  3684. return _dictionary.count;
  3685. }
  3686. - (void)enumerateKeysAndValuesUsingBlock:
  3687. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3688. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3689. NSNumber *aValue,
  3690. BOOL *stop) {
  3691. block([aKey intValue], [aValue longLongValue], stop);
  3692. }];
  3693. }
  3694. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3695. NSUInteger count = _dictionary.count;
  3696. if (count == 0) {
  3697. return 0;
  3698. }
  3699. GPBType valueType = GPBGetFieldType(field);
  3700. GPBType keyType = field.mapKeyType;
  3701. __block size_t result = 0;
  3702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3703. NSNumber *aValue,
  3704. BOOL *stop) {
  3705. #pragma unused(stop)
  3706. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3707. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  3708. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3709. }];
  3710. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3711. result += tagSize * count;
  3712. return result;
  3713. }
  3714. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3715. asField:(GPBFieldDescriptor *)field {
  3716. GPBType valueType = GPBGetFieldType(field);
  3717. GPBType keyType = field.mapKeyType;
  3718. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3719. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3720. NSNumber *aValue,
  3721. BOOL *stop) {
  3722. #pragma unused(stop)
  3723. // Write the tag.
  3724. [outputStream writeInt32NoTag:tag];
  3725. // Write the size of the message.
  3726. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3727. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  3728. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3729. // Write the fields.
  3730. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3731. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  3732. }];
  3733. }
  3734. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3735. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3736. }
  3737. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3738. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3739. #pragma unused(stop)
  3740. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3741. }];
  3742. }
  3743. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3744. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3745. if (wrapped && value) {
  3746. *value = [wrapped longLongValue];
  3747. }
  3748. return (wrapped != NULL);
  3749. }
  3750. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3751. if (otherDictionary) {
  3752. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3753. if (_autocreator) {
  3754. GPBAutocreatedDictionaryModified(_autocreator, self);
  3755. }
  3756. }
  3757. }
  3758. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3759. [_dictionary setObject:@(value) forKey:@(key)];
  3760. if (_autocreator) {
  3761. GPBAutocreatedDictionaryModified(_autocreator, self);
  3762. }
  3763. }
  3764. - (void)removeValueForKey:(int32_t)aKey {
  3765. [_dictionary removeObjectForKey:@(aKey)];
  3766. }
  3767. - (void)removeAll {
  3768. [_dictionary removeAllObjects];
  3769. }
  3770. @end
  3771. #pragma mark - Int32 -> Bool
  3772. @implementation GPBInt32BoolDictionary {
  3773. @package
  3774. NSMutableDictionary *_dictionary;
  3775. }
  3776. + (instancetype)dictionary {
  3777. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3778. }
  3779. + (instancetype)dictionaryWithValue:(BOOL)value
  3780. forKey:(int32_t)key {
  3781. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3782. // on to get the type correct.
  3783. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3784. forKeys:&key
  3785. count:1] autorelease];
  3786. }
  3787. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3788. forKeys:(const int32_t [])keys
  3789. count:(NSUInteger)count {
  3790. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3791. // on to get the type correct.
  3792. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3793. forKeys:keys
  3794. count:count] autorelease];
  3795. }
  3796. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3797. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3798. // on to get the type correct.
  3799. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3800. }
  3801. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3802. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3803. }
  3804. - (instancetype)init {
  3805. return [self initWithValues:NULL forKeys:NULL count:0];
  3806. }
  3807. - (instancetype)initWithValues:(const BOOL [])values
  3808. forKeys:(const int32_t [])keys
  3809. count:(NSUInteger)count {
  3810. self = [super init];
  3811. if (self) {
  3812. _dictionary = [[NSMutableDictionary alloc] init];
  3813. if (count && values && keys) {
  3814. for (NSUInteger i = 0; i < count; ++i) {
  3815. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3816. }
  3817. }
  3818. }
  3819. return self;
  3820. }
  3821. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3822. self = [self initWithValues:NULL forKeys:NULL count:0];
  3823. if (self) {
  3824. if (dictionary) {
  3825. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3826. }
  3827. }
  3828. return self;
  3829. }
  3830. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3831. #pragma unused(numItems)
  3832. return [self initWithValues:NULL forKeys:NULL count:0];
  3833. }
  3834. - (void)dealloc {
  3835. NSAssert(!_autocreator,
  3836. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3837. [self class], _autocreator);
  3838. [_dictionary release];
  3839. [super dealloc];
  3840. }
  3841. - (instancetype)copyWithZone:(NSZone *)zone {
  3842. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3843. }
  3844. - (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
  3845. if (self == other) {
  3846. return YES;
  3847. }
  3848. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3849. return NO;
  3850. }
  3851. return [_dictionary isEqual:other->_dictionary];
  3852. }
  3853. - (NSUInteger)hash {
  3854. return _dictionary.count;
  3855. }
  3856. - (NSString *)description {
  3857. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3858. }
  3859. - (NSUInteger)count {
  3860. return _dictionary.count;
  3861. }
  3862. - (void)enumerateKeysAndValuesUsingBlock:
  3863. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3864. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3865. NSNumber *aValue,
  3866. BOOL *stop) {
  3867. block([aKey intValue], [aValue boolValue], stop);
  3868. }];
  3869. }
  3870. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3871. NSUInteger count = _dictionary.count;
  3872. if (count == 0) {
  3873. return 0;
  3874. }
  3875. GPBType valueType = GPBGetFieldType(field);
  3876. GPBType keyType = field.mapKeyType;
  3877. __block size_t result = 0;
  3878. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3879. NSNumber *aValue,
  3880. BOOL *stop) {
  3881. #pragma unused(stop)
  3882. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3883. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  3884. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3885. }];
  3886. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  3887. result += tagSize * count;
  3888. return result;
  3889. }
  3890. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3891. asField:(GPBFieldDescriptor *)field {
  3892. GPBType valueType = GPBGetFieldType(field);
  3893. GPBType keyType = field.mapKeyType;
  3894. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3895. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3896. NSNumber *aValue,
  3897. BOOL *stop) {
  3898. #pragma unused(stop)
  3899. // Write the tag.
  3900. [outputStream writeInt32NoTag:tag];
  3901. // Write the size of the message.
  3902. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  3903. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  3904. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3905. // Write the fields.
  3906. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  3907. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  3908. }];
  3909. }
  3910. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  3911. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3912. }
  3913. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3914. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3915. #pragma unused(stop)
  3916. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3917. }];
  3918. }
  3919. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  3920. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3921. if (wrapped && value) {
  3922. *value = [wrapped boolValue];
  3923. }
  3924. return (wrapped != NULL);
  3925. }
  3926. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3927. if (otherDictionary) {
  3928. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3929. if (_autocreator) {
  3930. GPBAutocreatedDictionaryModified(_autocreator, self);
  3931. }
  3932. }
  3933. }
  3934. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  3935. [_dictionary setObject:@(value) forKey:@(key)];
  3936. if (_autocreator) {
  3937. GPBAutocreatedDictionaryModified(_autocreator, self);
  3938. }
  3939. }
  3940. - (void)removeValueForKey:(int32_t)aKey {
  3941. [_dictionary removeObjectForKey:@(aKey)];
  3942. }
  3943. - (void)removeAll {
  3944. [_dictionary removeAllObjects];
  3945. }
  3946. @end
  3947. #pragma mark - Int32 -> Float
  3948. @implementation GPBInt32FloatDictionary {
  3949. @package
  3950. NSMutableDictionary *_dictionary;
  3951. }
  3952. + (instancetype)dictionary {
  3953. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3954. }
  3955. + (instancetype)dictionaryWithValue:(float)value
  3956. forKey:(int32_t)key {
  3957. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3958. // on to get the type correct.
  3959. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  3960. forKeys:&key
  3961. count:1] autorelease];
  3962. }
  3963. + (instancetype)dictionaryWithValues:(const float [])values
  3964. forKeys:(const int32_t [])keys
  3965. count:(NSUInteger)count {
  3966. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3967. // on to get the type correct.
  3968. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  3969. forKeys:keys
  3970. count:count] autorelease];
  3971. }
  3972. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3973. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3974. // on to get the type correct.
  3975. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3976. }
  3977. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3978. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3979. }
  3980. - (instancetype)init {
  3981. return [self initWithValues:NULL forKeys:NULL count:0];
  3982. }
  3983. - (instancetype)initWithValues:(const float [])values
  3984. forKeys:(const int32_t [])keys
  3985. count:(NSUInteger)count {
  3986. self = [super init];
  3987. if (self) {
  3988. _dictionary = [[NSMutableDictionary alloc] init];
  3989. if (count && values && keys) {
  3990. for (NSUInteger i = 0; i < count; ++i) {
  3991. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3992. }
  3993. }
  3994. }
  3995. return self;
  3996. }
  3997. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3998. self = [self initWithValues:NULL forKeys:NULL count:0];
  3999. if (self) {
  4000. if (dictionary) {
  4001. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4002. }
  4003. }
  4004. return self;
  4005. }
  4006. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4007. #pragma unused(numItems)
  4008. return [self initWithValues:NULL forKeys:NULL count:0];
  4009. }
  4010. - (void)dealloc {
  4011. NSAssert(!_autocreator,
  4012. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4013. [self class], _autocreator);
  4014. [_dictionary release];
  4015. [super dealloc];
  4016. }
  4017. - (instancetype)copyWithZone:(NSZone *)zone {
  4018. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4019. }
  4020. - (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
  4021. if (self == other) {
  4022. return YES;
  4023. }
  4024. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4025. return NO;
  4026. }
  4027. return [_dictionary isEqual:other->_dictionary];
  4028. }
  4029. - (NSUInteger)hash {
  4030. return _dictionary.count;
  4031. }
  4032. - (NSString *)description {
  4033. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4034. }
  4035. - (NSUInteger)count {
  4036. return _dictionary.count;
  4037. }
  4038. - (void)enumerateKeysAndValuesUsingBlock:
  4039. (void (^)(int32_t key, float value, BOOL *stop))block {
  4040. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4041. NSNumber *aValue,
  4042. BOOL *stop) {
  4043. block([aKey intValue], [aValue floatValue], stop);
  4044. }];
  4045. }
  4046. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4047. NSUInteger count = _dictionary.count;
  4048. if (count == 0) {
  4049. return 0;
  4050. }
  4051. GPBType valueType = GPBGetFieldType(field);
  4052. GPBType keyType = field.mapKeyType;
  4053. __block size_t result = 0;
  4054. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4055. NSNumber *aValue,
  4056. BOOL *stop) {
  4057. #pragma unused(stop)
  4058. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4059. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  4060. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4061. }];
  4062. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4063. result += tagSize * count;
  4064. return result;
  4065. }
  4066. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4067. asField:(GPBFieldDescriptor *)field {
  4068. GPBType valueType = GPBGetFieldType(field);
  4069. GPBType keyType = field.mapKeyType;
  4070. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4071. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4072. NSNumber *aValue,
  4073. BOOL *stop) {
  4074. #pragma unused(stop)
  4075. // Write the tag.
  4076. [outputStream writeInt32NoTag:tag];
  4077. // Write the size of the message.
  4078. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4079. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  4080. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4081. // Write the fields.
  4082. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4083. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  4084. }];
  4085. }
  4086. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4087. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4088. }
  4089. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4090. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4091. #pragma unused(stop)
  4092. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4093. }];
  4094. }
  4095. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  4096. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4097. if (wrapped && value) {
  4098. *value = [wrapped floatValue];
  4099. }
  4100. return (wrapped != NULL);
  4101. }
  4102. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4103. if (otherDictionary) {
  4104. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4105. if (_autocreator) {
  4106. GPBAutocreatedDictionaryModified(_autocreator, self);
  4107. }
  4108. }
  4109. }
  4110. - (void)setValue:(float)value forKey:(int32_t)key {
  4111. [_dictionary setObject:@(value) forKey:@(key)];
  4112. if (_autocreator) {
  4113. GPBAutocreatedDictionaryModified(_autocreator, self);
  4114. }
  4115. }
  4116. - (void)removeValueForKey:(int32_t)aKey {
  4117. [_dictionary removeObjectForKey:@(aKey)];
  4118. }
  4119. - (void)removeAll {
  4120. [_dictionary removeAllObjects];
  4121. }
  4122. @end
  4123. #pragma mark - Int32 -> Double
  4124. @implementation GPBInt32DoubleDictionary {
  4125. @package
  4126. NSMutableDictionary *_dictionary;
  4127. }
  4128. + (instancetype)dictionary {
  4129. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4130. }
  4131. + (instancetype)dictionaryWithValue:(double)value
  4132. forKey:(int32_t)key {
  4133. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4134. // on to get the type correct.
  4135. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  4136. forKeys:&key
  4137. count:1] autorelease];
  4138. }
  4139. + (instancetype)dictionaryWithValues:(const double [])values
  4140. forKeys:(const int32_t [])keys
  4141. count:(NSUInteger)count {
  4142. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4143. // on to get the type correct.
  4144. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  4145. forKeys:keys
  4146. count:count] autorelease];
  4147. }
  4148. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4149. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4150. // on to get the type correct.
  4151. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4152. }
  4153. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4154. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4155. }
  4156. - (instancetype)init {
  4157. return [self initWithValues:NULL forKeys:NULL count:0];
  4158. }
  4159. - (instancetype)initWithValues:(const double [])values
  4160. forKeys:(const int32_t [])keys
  4161. count:(NSUInteger)count {
  4162. self = [super init];
  4163. if (self) {
  4164. _dictionary = [[NSMutableDictionary alloc] init];
  4165. if (count && values && keys) {
  4166. for (NSUInteger i = 0; i < count; ++i) {
  4167. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4168. }
  4169. }
  4170. }
  4171. return self;
  4172. }
  4173. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4174. self = [self initWithValues:NULL forKeys:NULL count:0];
  4175. if (self) {
  4176. if (dictionary) {
  4177. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4178. }
  4179. }
  4180. return self;
  4181. }
  4182. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4183. #pragma unused(numItems)
  4184. return [self initWithValues:NULL forKeys:NULL count:0];
  4185. }
  4186. - (void)dealloc {
  4187. NSAssert(!_autocreator,
  4188. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4189. [self class], _autocreator);
  4190. [_dictionary release];
  4191. [super dealloc];
  4192. }
  4193. - (instancetype)copyWithZone:(NSZone *)zone {
  4194. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4195. }
  4196. - (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
  4197. if (self == other) {
  4198. return YES;
  4199. }
  4200. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4201. return NO;
  4202. }
  4203. return [_dictionary isEqual:other->_dictionary];
  4204. }
  4205. - (NSUInteger)hash {
  4206. return _dictionary.count;
  4207. }
  4208. - (NSString *)description {
  4209. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4210. }
  4211. - (NSUInteger)count {
  4212. return _dictionary.count;
  4213. }
  4214. - (void)enumerateKeysAndValuesUsingBlock:
  4215. (void (^)(int32_t key, double value, BOOL *stop))block {
  4216. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4217. NSNumber *aValue,
  4218. BOOL *stop) {
  4219. block([aKey intValue], [aValue doubleValue], stop);
  4220. }];
  4221. }
  4222. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4223. NSUInteger count = _dictionary.count;
  4224. if (count == 0) {
  4225. return 0;
  4226. }
  4227. GPBType valueType = GPBGetFieldType(field);
  4228. GPBType keyType = field.mapKeyType;
  4229. __block size_t result = 0;
  4230. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4231. NSNumber *aValue,
  4232. BOOL *stop) {
  4233. #pragma unused(stop)
  4234. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4235. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  4236. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4237. }];
  4238. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4239. result += tagSize * count;
  4240. return result;
  4241. }
  4242. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4243. asField:(GPBFieldDescriptor *)field {
  4244. GPBType valueType = GPBGetFieldType(field);
  4245. GPBType keyType = field.mapKeyType;
  4246. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4247. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4248. NSNumber *aValue,
  4249. BOOL *stop) {
  4250. #pragma unused(stop)
  4251. // Write the tag.
  4252. [outputStream writeInt32NoTag:tag];
  4253. // Write the size of the message.
  4254. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4255. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  4256. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4257. // Write the fields.
  4258. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4259. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  4260. }];
  4261. }
  4262. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4263. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4264. }
  4265. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4266. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4267. #pragma unused(stop)
  4268. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4269. }];
  4270. }
  4271. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4272. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4273. if (wrapped && value) {
  4274. *value = [wrapped doubleValue];
  4275. }
  4276. return (wrapped != NULL);
  4277. }
  4278. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4279. if (otherDictionary) {
  4280. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4281. if (_autocreator) {
  4282. GPBAutocreatedDictionaryModified(_autocreator, self);
  4283. }
  4284. }
  4285. }
  4286. - (void)setValue:(double)value forKey:(int32_t)key {
  4287. [_dictionary setObject:@(value) forKey:@(key)];
  4288. if (_autocreator) {
  4289. GPBAutocreatedDictionaryModified(_autocreator, self);
  4290. }
  4291. }
  4292. - (void)removeValueForKey:(int32_t)aKey {
  4293. [_dictionary removeObjectForKey:@(aKey)];
  4294. }
  4295. - (void)removeAll {
  4296. [_dictionary removeAllObjects];
  4297. }
  4298. @end
  4299. #pragma mark - Int32 -> Enum
  4300. @implementation GPBInt32EnumDictionary {
  4301. @package
  4302. NSMutableDictionary *_dictionary;
  4303. GPBEnumValidationFunc _validationFunc;
  4304. }
  4305. @synthesize validationFunc = _validationFunc;
  4306. + (instancetype)dictionary {
  4307. return [[[self alloc] initWithValidationFunction:NULL
  4308. rawValues:NULL
  4309. forKeys:NULL
  4310. count:0] autorelease];
  4311. }
  4312. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4313. return [[[self alloc] initWithValidationFunction:func
  4314. rawValues:NULL
  4315. forKeys:NULL
  4316. count:0] autorelease];
  4317. }
  4318. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4319. rawValue:(int32_t)rawValue
  4320. forKey:(int32_t)key {
  4321. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4322. // on to get the type correct.
  4323. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4324. rawValues:&rawValue
  4325. forKeys:&key
  4326. count:1] autorelease];
  4327. }
  4328. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4329. rawValues:(const int32_t [])rawValues
  4330. forKeys:(const int32_t [])keys
  4331. count:(NSUInteger)count {
  4332. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4333. // on to get the type correct.
  4334. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4335. rawValues:rawValues
  4336. forKeys:keys
  4337. count:count] autorelease];
  4338. }
  4339. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4340. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4341. // on to get the type correct.
  4342. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4343. }
  4344. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4345. capacity:(NSUInteger)numItems {
  4346. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4347. }
  4348. - (instancetype)init {
  4349. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4350. }
  4351. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4352. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4353. }
  4354. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4355. rawValues:(const int32_t [])rawValues
  4356. forKeys:(const int32_t [])keys
  4357. count:(NSUInteger)count {
  4358. self = [super init];
  4359. if (self) {
  4360. _dictionary = [[NSMutableDictionary alloc] init];
  4361. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4362. if (count && rawValues && keys) {
  4363. for (NSUInteger i = 0; i < count; ++i) {
  4364. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4365. }
  4366. }
  4367. }
  4368. return self;
  4369. }
  4370. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4371. self = [self initWithValidationFunction:dictionary.validationFunc
  4372. rawValues:NULL
  4373. forKeys:NULL
  4374. count:0];
  4375. if (self) {
  4376. if (dictionary) {
  4377. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4378. }
  4379. }
  4380. return self;
  4381. }
  4382. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4383. capacity:(NSUInteger)numItems {
  4384. #pragma unused(numItems)
  4385. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4386. }
  4387. - (void)dealloc {
  4388. NSAssert(!_autocreator,
  4389. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4390. [self class], _autocreator);
  4391. [_dictionary release];
  4392. [super dealloc];
  4393. }
  4394. - (instancetype)copyWithZone:(NSZone *)zone {
  4395. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4396. }
  4397. - (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
  4398. if (self == other) {
  4399. return YES;
  4400. }
  4401. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4402. return NO;
  4403. }
  4404. return [_dictionary isEqual:other->_dictionary];
  4405. }
  4406. - (NSUInteger)hash {
  4407. return _dictionary.count;
  4408. }
  4409. - (NSString *)description {
  4410. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4411. }
  4412. - (NSUInteger)count {
  4413. return _dictionary.count;
  4414. }
  4415. - (void)enumerateKeysAndRawValuesUsingBlock:
  4416. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4417. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4418. NSNumber *aValue,
  4419. BOOL *stop) {
  4420. block([aKey intValue], [aValue intValue], stop);
  4421. }];
  4422. }
  4423. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4424. NSUInteger count = _dictionary.count;
  4425. if (count == 0) {
  4426. return 0;
  4427. }
  4428. GPBType valueType = GPBGetFieldType(field);
  4429. GPBType keyType = field.mapKeyType;
  4430. __block size_t result = 0;
  4431. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4432. NSNumber *aValue,
  4433. BOOL *stop) {
  4434. #pragma unused(stop)
  4435. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4436. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  4437. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4438. }];
  4439. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4440. result += tagSize * count;
  4441. return result;
  4442. }
  4443. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4444. asField:(GPBFieldDescriptor *)field {
  4445. GPBType valueType = GPBGetFieldType(field);
  4446. GPBType keyType = field.mapKeyType;
  4447. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4448. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4449. NSNumber *aValue,
  4450. BOOL *stop) {
  4451. #pragma unused(stop)
  4452. // Write the tag.
  4453. [outputStream writeInt32NoTag:tag];
  4454. // Write the size of the message.
  4455. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4456. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  4457. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4458. // Write the fields.
  4459. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4460. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  4461. }];
  4462. }
  4463. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4464. forKey:(GPBValue *)key
  4465. keyType:(GPBType)keyType {
  4466. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyType);
  4467. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  4468. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4469. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4470. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyType);
  4471. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  4472. [outputStream release];
  4473. return data;
  4474. }
  4475. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4476. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4477. }
  4478. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4479. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4480. #pragma unused(stop)
  4481. block([NSString stringWithFormat:@"%d", key], @(value));
  4482. }];
  4483. }
  4484. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4485. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4486. if (wrapped && value) {
  4487. int32_t result = [wrapped intValue];
  4488. if (!_validationFunc(result)) {
  4489. result = kGPBUnrecognizedEnumeratorValue;
  4490. }
  4491. *value = result;
  4492. }
  4493. return (wrapped != NULL);
  4494. }
  4495. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4496. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4497. if (wrapped && rawValue) {
  4498. *rawValue = [wrapped intValue];
  4499. }
  4500. return (wrapped != NULL);
  4501. }
  4502. - (void)enumerateKeysAndValuesUsingBlock:
  4503. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4504. GPBEnumValidationFunc func = _validationFunc;
  4505. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4506. NSNumber *aValue,
  4507. BOOL *stop) {
  4508. int32_t unwrapped = [aValue intValue];
  4509. if (!func(unwrapped)) {
  4510. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4511. }
  4512. block([aKey intValue], unwrapped, stop);
  4513. }];
  4514. }
  4515. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4516. if (otherDictionary) {
  4517. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4518. if (_autocreator) {
  4519. GPBAutocreatedDictionaryModified(_autocreator, self);
  4520. }
  4521. }
  4522. }
  4523. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4524. [_dictionary setObject:@(value) forKey:@(key)];
  4525. if (_autocreator) {
  4526. GPBAutocreatedDictionaryModified(_autocreator, self);
  4527. }
  4528. }
  4529. - (void)removeValueForKey:(int32_t)aKey {
  4530. [_dictionary removeObjectForKey:@(aKey)];
  4531. }
  4532. - (void)removeAll {
  4533. [_dictionary removeAllObjects];
  4534. }
  4535. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4536. if (!_validationFunc(value)) {
  4537. [NSException raise:NSInvalidArgumentException
  4538. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4539. value];
  4540. }
  4541. [_dictionary setObject:@(value) forKey:@(key)];
  4542. if (_autocreator) {
  4543. GPBAutocreatedDictionaryModified(_autocreator, self);
  4544. }
  4545. }
  4546. @end
  4547. #pragma mark - Int32 -> Object
  4548. @implementation GPBInt32ObjectDictionary {
  4549. @package
  4550. NSMutableDictionary *_dictionary;
  4551. }
  4552. + (instancetype)dictionary {
  4553. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4554. }
  4555. + (instancetype)dictionaryWithValue:(id)value
  4556. forKey:(int32_t)key {
  4557. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4558. // on to get the type correct.
  4559. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithValues:&value
  4560. forKeys:&key
  4561. count:1] autorelease];
  4562. }
  4563. + (instancetype)dictionaryWithValues:(const id [])values
  4564. forKeys:(const int32_t [])keys
  4565. count:(NSUInteger)count {
  4566. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4567. // on to get the type correct.
  4568. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithValues:values
  4569. forKeys:keys
  4570. count:count] autorelease];
  4571. }
  4572. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4573. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4574. // on to get the type correct.
  4575. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4576. }
  4577. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4578. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4579. }
  4580. - (instancetype)init {
  4581. return [self initWithValues:NULL forKeys:NULL count:0];
  4582. }
  4583. - (instancetype)initWithValues:(const id [])values
  4584. forKeys:(const int32_t [])keys
  4585. count:(NSUInteger)count {
  4586. self = [super init];
  4587. if (self) {
  4588. _dictionary = [[NSMutableDictionary alloc] init];
  4589. if (count && values && keys) {
  4590. for (NSUInteger i = 0; i < count; ++i) {
  4591. [_dictionary setObject:values[i] forKey:@(keys[i])];
  4592. }
  4593. }
  4594. }
  4595. return self;
  4596. }
  4597. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4598. self = [self initWithValues:NULL forKeys:NULL count:0];
  4599. if (self) {
  4600. if (dictionary) {
  4601. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4602. }
  4603. }
  4604. return self;
  4605. }
  4606. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4607. #pragma unused(numItems)
  4608. return [self initWithValues:NULL forKeys:NULL count:0];
  4609. }
  4610. - (void)dealloc {
  4611. NSAssert(!_autocreator,
  4612. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4613. [self class], _autocreator);
  4614. [_dictionary release];
  4615. [super dealloc];
  4616. }
  4617. - (instancetype)copyWithZone:(NSZone *)zone {
  4618. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4619. }
  4620. - (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
  4621. if (self == other) {
  4622. return YES;
  4623. }
  4624. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4625. return NO;
  4626. }
  4627. return [_dictionary isEqual:other->_dictionary];
  4628. }
  4629. - (NSUInteger)hash {
  4630. return _dictionary.count;
  4631. }
  4632. - (NSString *)description {
  4633. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4634. }
  4635. - (NSUInteger)count {
  4636. return _dictionary.count;
  4637. }
  4638. - (void)enumerateKeysAndValuesUsingBlock:
  4639. (void (^)(int32_t key, id value, BOOL *stop))block {
  4640. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4641. id aValue,
  4642. BOOL *stop) {
  4643. block([aKey intValue], aValue, stop);
  4644. }];
  4645. }
  4646. - (BOOL)isInitialized {
  4647. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4648. if (!msg.initialized) {
  4649. return NO;
  4650. }
  4651. }
  4652. return YES;
  4653. }
  4654. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4655. GPBInt32ObjectDictionary *newDict =
  4656. [[GPBInt32ObjectDictionary alloc] init];
  4657. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4658. GPBMessage *msg,
  4659. BOOL *stop) {
  4660. #pragma unused(stop)
  4661. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4662. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4663. [copiedMsg release];
  4664. }];
  4665. return newDict;
  4666. }
  4667. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4668. NSUInteger count = _dictionary.count;
  4669. if (count == 0) {
  4670. return 0;
  4671. }
  4672. GPBType valueType = GPBGetFieldType(field);
  4673. GPBType keyType = field.mapKeyType;
  4674. __block size_t result = 0;
  4675. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4676. id aValue,
  4677. BOOL *stop) {
  4678. #pragma unused(stop)
  4679. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4680. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  4681. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4682. }];
  4683. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4684. result += tagSize * count;
  4685. return result;
  4686. }
  4687. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4688. asField:(GPBFieldDescriptor *)field {
  4689. GPBType valueType = GPBGetFieldType(field);
  4690. GPBType keyType = field.mapKeyType;
  4691. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4692. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4693. id aValue,
  4694. BOOL *stop) {
  4695. #pragma unused(stop)
  4696. // Write the tag.
  4697. [outputStream writeInt32NoTag:tag];
  4698. // Write the size of the message.
  4699. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyType);
  4700. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  4701. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4702. // Write the fields.
  4703. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyType);
  4704. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  4705. }];
  4706. }
  4707. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4708. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4709. }
  4710. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4711. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, id value, BOOL *stop) {
  4712. #pragma unused(stop)
  4713. block([NSString stringWithFormat:@"%d", key], value);
  4714. }];
  4715. }
  4716. - (id)valueForKey:(int32_t)key {
  4717. id result = [_dictionary objectForKey:@(key)];
  4718. return result;
  4719. }
  4720. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4721. if (otherDictionary) {
  4722. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4723. if (_autocreator) {
  4724. GPBAutocreatedDictionaryModified(_autocreator, self);
  4725. }
  4726. }
  4727. }
  4728. - (void)setValue:(id)value forKey:(int32_t)key {
  4729. [_dictionary setObject:value forKey:@(key)];
  4730. if (_autocreator) {
  4731. GPBAutocreatedDictionaryModified(_autocreator, self);
  4732. }
  4733. }
  4734. - (void)removeValueForKey:(int32_t)aKey {
  4735. [_dictionary removeObjectForKey:@(aKey)];
  4736. }
  4737. - (void)removeAll {
  4738. [_dictionary removeAllObjects];
  4739. }
  4740. @end
  4741. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4742. // This block of code is generated, do not edit it directly.
  4743. #pragma mark - UInt64 -> UInt32
  4744. @implementation GPBUInt64UInt32Dictionary {
  4745. @package
  4746. NSMutableDictionary *_dictionary;
  4747. }
  4748. + (instancetype)dictionary {
  4749. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4750. }
  4751. + (instancetype)dictionaryWithValue:(uint32_t)value
  4752. forKey:(uint64_t)key {
  4753. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4754. // on to get the type correct.
  4755. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4756. forKeys:&key
  4757. count:1] autorelease];
  4758. }
  4759. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4760. forKeys:(const uint64_t [])keys
  4761. count:(NSUInteger)count {
  4762. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4763. // on to get the type correct.
  4764. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4765. forKeys:keys
  4766. count:count] autorelease];
  4767. }
  4768. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4769. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4770. // on to get the type correct.
  4771. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4772. }
  4773. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4774. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4775. }
  4776. - (instancetype)init {
  4777. return [self initWithValues:NULL forKeys:NULL count:0];
  4778. }
  4779. - (instancetype)initWithValues:(const uint32_t [])values
  4780. forKeys:(const uint64_t [])keys
  4781. count:(NSUInteger)count {
  4782. self = [super init];
  4783. if (self) {
  4784. _dictionary = [[NSMutableDictionary alloc] init];
  4785. if (count && values && keys) {
  4786. for (NSUInteger i = 0; i < count; ++i) {
  4787. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4788. }
  4789. }
  4790. }
  4791. return self;
  4792. }
  4793. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4794. self = [self initWithValues:NULL forKeys:NULL count:0];
  4795. if (self) {
  4796. if (dictionary) {
  4797. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4798. }
  4799. }
  4800. return self;
  4801. }
  4802. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4803. #pragma unused(numItems)
  4804. return [self initWithValues:NULL forKeys:NULL count:0];
  4805. }
  4806. - (void)dealloc {
  4807. NSAssert(!_autocreator,
  4808. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4809. [self class], _autocreator);
  4810. [_dictionary release];
  4811. [super dealloc];
  4812. }
  4813. - (instancetype)copyWithZone:(NSZone *)zone {
  4814. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4815. }
  4816. - (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
  4817. if (self == other) {
  4818. return YES;
  4819. }
  4820. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4821. return NO;
  4822. }
  4823. return [_dictionary isEqual:other->_dictionary];
  4824. }
  4825. - (NSUInteger)hash {
  4826. return _dictionary.count;
  4827. }
  4828. - (NSString *)description {
  4829. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4830. }
  4831. - (NSUInteger)count {
  4832. return _dictionary.count;
  4833. }
  4834. - (void)enumerateKeysAndValuesUsingBlock:
  4835. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4836. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4837. NSNumber *aValue,
  4838. BOOL *stop) {
  4839. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4840. }];
  4841. }
  4842. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4843. NSUInteger count = _dictionary.count;
  4844. if (count == 0) {
  4845. return 0;
  4846. }
  4847. GPBType valueType = GPBGetFieldType(field);
  4848. GPBType keyType = field.mapKeyType;
  4849. __block size_t result = 0;
  4850. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4851. NSNumber *aValue,
  4852. BOOL *stop) {
  4853. #pragma unused(stop)
  4854. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4855. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  4856. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4857. }];
  4858. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  4859. result += tagSize * count;
  4860. return result;
  4861. }
  4862. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4863. asField:(GPBFieldDescriptor *)field {
  4864. GPBType valueType = GPBGetFieldType(field);
  4865. GPBType keyType = field.mapKeyType;
  4866. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4867. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4868. NSNumber *aValue,
  4869. BOOL *stop) {
  4870. #pragma unused(stop)
  4871. // Write the tag.
  4872. [outputStream writeInt32NoTag:tag];
  4873. // Write the size of the message.
  4874. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4875. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  4876. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4877. // Write the fields.
  4878. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  4879. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  4880. }];
  4881. }
  4882. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  4883. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4884. }
  4885. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4886. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4887. #pragma unused(stop)
  4888. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4889. }];
  4890. }
  4891. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  4892. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4893. if (wrapped && value) {
  4894. *value = [wrapped unsignedIntValue];
  4895. }
  4896. return (wrapped != NULL);
  4897. }
  4898. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4899. if (otherDictionary) {
  4900. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4901. if (_autocreator) {
  4902. GPBAutocreatedDictionaryModified(_autocreator, self);
  4903. }
  4904. }
  4905. }
  4906. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  4907. [_dictionary setObject:@(value) forKey:@(key)];
  4908. if (_autocreator) {
  4909. GPBAutocreatedDictionaryModified(_autocreator, self);
  4910. }
  4911. }
  4912. - (void)removeValueForKey:(uint64_t)aKey {
  4913. [_dictionary removeObjectForKey:@(aKey)];
  4914. }
  4915. - (void)removeAll {
  4916. [_dictionary removeAllObjects];
  4917. }
  4918. @end
  4919. #pragma mark - UInt64 -> Int32
  4920. @implementation GPBUInt64Int32Dictionary {
  4921. @package
  4922. NSMutableDictionary *_dictionary;
  4923. }
  4924. + (instancetype)dictionary {
  4925. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4926. }
  4927. + (instancetype)dictionaryWithValue:(int32_t)value
  4928. forKey:(uint64_t)key {
  4929. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4930. // on to get the type correct.
  4931. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  4932. forKeys:&key
  4933. count:1] autorelease];
  4934. }
  4935. + (instancetype)dictionaryWithValues:(const int32_t [])values
  4936. forKeys:(const uint64_t [])keys
  4937. count:(NSUInteger)count {
  4938. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4939. // on to get the type correct.
  4940. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  4941. forKeys:keys
  4942. count:count] autorelease];
  4943. }
  4944. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4945. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4946. // on to get the type correct.
  4947. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4948. }
  4949. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4950. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4951. }
  4952. - (instancetype)init {
  4953. return [self initWithValues:NULL forKeys:NULL count:0];
  4954. }
  4955. - (instancetype)initWithValues:(const int32_t [])values
  4956. forKeys:(const uint64_t [])keys
  4957. count:(NSUInteger)count {
  4958. self = [super init];
  4959. if (self) {
  4960. _dictionary = [[NSMutableDictionary alloc] init];
  4961. if (count && values && keys) {
  4962. for (NSUInteger i = 0; i < count; ++i) {
  4963. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4964. }
  4965. }
  4966. }
  4967. return self;
  4968. }
  4969. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4970. self = [self initWithValues:NULL forKeys:NULL count:0];
  4971. if (self) {
  4972. if (dictionary) {
  4973. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4974. }
  4975. }
  4976. return self;
  4977. }
  4978. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4979. #pragma unused(numItems)
  4980. return [self initWithValues:NULL forKeys:NULL count:0];
  4981. }
  4982. - (void)dealloc {
  4983. NSAssert(!_autocreator,
  4984. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4985. [self class], _autocreator);
  4986. [_dictionary release];
  4987. [super dealloc];
  4988. }
  4989. - (instancetype)copyWithZone:(NSZone *)zone {
  4990. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  4991. }
  4992. - (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
  4993. if (self == other) {
  4994. return YES;
  4995. }
  4996. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  4997. return NO;
  4998. }
  4999. return [_dictionary isEqual:other->_dictionary];
  5000. }
  5001. - (NSUInteger)hash {
  5002. return _dictionary.count;
  5003. }
  5004. - (NSString *)description {
  5005. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5006. }
  5007. - (NSUInteger)count {
  5008. return _dictionary.count;
  5009. }
  5010. - (void)enumerateKeysAndValuesUsingBlock:
  5011. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5012. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5013. NSNumber *aValue,
  5014. BOOL *stop) {
  5015. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5016. }];
  5017. }
  5018. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5019. NSUInteger count = _dictionary.count;
  5020. if (count == 0) {
  5021. return 0;
  5022. }
  5023. GPBType valueType = GPBGetFieldType(field);
  5024. GPBType keyType = field.mapKeyType;
  5025. __block size_t result = 0;
  5026. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5027. NSNumber *aValue,
  5028. BOOL *stop) {
  5029. #pragma unused(stop)
  5030. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5031. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  5032. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5033. }];
  5034. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5035. result += tagSize * count;
  5036. return result;
  5037. }
  5038. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5039. asField:(GPBFieldDescriptor *)field {
  5040. GPBType valueType = GPBGetFieldType(field);
  5041. GPBType keyType = field.mapKeyType;
  5042. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5043. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5044. NSNumber *aValue,
  5045. BOOL *stop) {
  5046. #pragma unused(stop)
  5047. // Write the tag.
  5048. [outputStream writeInt32NoTag:tag];
  5049. // Write the size of the message.
  5050. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5051. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  5052. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5053. // Write the fields.
  5054. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5055. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  5056. }];
  5057. }
  5058. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5059. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5060. }
  5061. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5062. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5063. #pragma unused(stop)
  5064. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5065. }];
  5066. }
  5067. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5068. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5069. if (wrapped && value) {
  5070. *value = [wrapped intValue];
  5071. }
  5072. return (wrapped != NULL);
  5073. }
  5074. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5075. if (otherDictionary) {
  5076. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5077. if (_autocreator) {
  5078. GPBAutocreatedDictionaryModified(_autocreator, self);
  5079. }
  5080. }
  5081. }
  5082. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5083. [_dictionary setObject:@(value) forKey:@(key)];
  5084. if (_autocreator) {
  5085. GPBAutocreatedDictionaryModified(_autocreator, self);
  5086. }
  5087. }
  5088. - (void)removeValueForKey:(uint64_t)aKey {
  5089. [_dictionary removeObjectForKey:@(aKey)];
  5090. }
  5091. - (void)removeAll {
  5092. [_dictionary removeAllObjects];
  5093. }
  5094. @end
  5095. #pragma mark - UInt64 -> UInt64
  5096. @implementation GPBUInt64UInt64Dictionary {
  5097. @package
  5098. NSMutableDictionary *_dictionary;
  5099. }
  5100. + (instancetype)dictionary {
  5101. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5102. }
  5103. + (instancetype)dictionaryWithValue:(uint64_t)value
  5104. forKey:(uint64_t)key {
  5105. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5106. // on to get the type correct.
  5107. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  5108. forKeys:&key
  5109. count:1] autorelease];
  5110. }
  5111. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  5112. forKeys:(const uint64_t [])keys
  5113. count:(NSUInteger)count {
  5114. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5115. // on to get the type correct.
  5116. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  5117. forKeys:keys
  5118. count:count] autorelease];
  5119. }
  5120. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5121. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5122. // on to get the type correct.
  5123. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5124. }
  5125. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5126. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5127. }
  5128. - (instancetype)init {
  5129. return [self initWithValues:NULL forKeys:NULL count:0];
  5130. }
  5131. - (instancetype)initWithValues:(const uint64_t [])values
  5132. forKeys:(const uint64_t [])keys
  5133. count:(NSUInteger)count {
  5134. self = [super init];
  5135. if (self) {
  5136. _dictionary = [[NSMutableDictionary alloc] init];
  5137. if (count && values && keys) {
  5138. for (NSUInteger i = 0; i < count; ++i) {
  5139. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5140. }
  5141. }
  5142. }
  5143. return self;
  5144. }
  5145. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5146. self = [self initWithValues:NULL forKeys:NULL count:0];
  5147. if (self) {
  5148. if (dictionary) {
  5149. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5150. }
  5151. }
  5152. return self;
  5153. }
  5154. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5155. #pragma unused(numItems)
  5156. return [self initWithValues:NULL forKeys:NULL count:0];
  5157. }
  5158. - (void)dealloc {
  5159. NSAssert(!_autocreator,
  5160. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5161. [self class], _autocreator);
  5162. [_dictionary release];
  5163. [super dealloc];
  5164. }
  5165. - (instancetype)copyWithZone:(NSZone *)zone {
  5166. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5167. }
  5168. - (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
  5169. if (self == other) {
  5170. return YES;
  5171. }
  5172. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5173. return NO;
  5174. }
  5175. return [_dictionary isEqual:other->_dictionary];
  5176. }
  5177. - (NSUInteger)hash {
  5178. return _dictionary.count;
  5179. }
  5180. - (NSString *)description {
  5181. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5182. }
  5183. - (NSUInteger)count {
  5184. return _dictionary.count;
  5185. }
  5186. - (void)enumerateKeysAndValuesUsingBlock:
  5187. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5188. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5189. NSNumber *aValue,
  5190. BOOL *stop) {
  5191. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5192. }];
  5193. }
  5194. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5195. NSUInteger count = _dictionary.count;
  5196. if (count == 0) {
  5197. return 0;
  5198. }
  5199. GPBType valueType = GPBGetFieldType(field);
  5200. GPBType keyType = field.mapKeyType;
  5201. __block size_t result = 0;
  5202. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5203. NSNumber *aValue,
  5204. BOOL *stop) {
  5205. #pragma unused(stop)
  5206. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5207. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  5208. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5209. }];
  5210. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5211. result += tagSize * count;
  5212. return result;
  5213. }
  5214. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5215. asField:(GPBFieldDescriptor *)field {
  5216. GPBType valueType = GPBGetFieldType(field);
  5217. GPBType keyType = field.mapKeyType;
  5218. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5219. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5220. NSNumber *aValue,
  5221. BOOL *stop) {
  5222. #pragma unused(stop)
  5223. // Write the tag.
  5224. [outputStream writeInt32NoTag:tag];
  5225. // Write the size of the message.
  5226. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5227. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  5228. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5229. // Write the fields.
  5230. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5231. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  5232. }];
  5233. }
  5234. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5235. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5236. }
  5237. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5238. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5239. #pragma unused(stop)
  5240. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5241. }];
  5242. }
  5243. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  5244. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5245. if (wrapped && value) {
  5246. *value = [wrapped unsignedLongLongValue];
  5247. }
  5248. return (wrapped != NULL);
  5249. }
  5250. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5251. if (otherDictionary) {
  5252. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5253. if (_autocreator) {
  5254. GPBAutocreatedDictionaryModified(_autocreator, self);
  5255. }
  5256. }
  5257. }
  5258. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5259. [_dictionary setObject:@(value) forKey:@(key)];
  5260. if (_autocreator) {
  5261. GPBAutocreatedDictionaryModified(_autocreator, self);
  5262. }
  5263. }
  5264. - (void)removeValueForKey:(uint64_t)aKey {
  5265. [_dictionary removeObjectForKey:@(aKey)];
  5266. }
  5267. - (void)removeAll {
  5268. [_dictionary removeAllObjects];
  5269. }
  5270. @end
  5271. #pragma mark - UInt64 -> Int64
  5272. @implementation GPBUInt64Int64Dictionary {
  5273. @package
  5274. NSMutableDictionary *_dictionary;
  5275. }
  5276. + (instancetype)dictionary {
  5277. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5278. }
  5279. + (instancetype)dictionaryWithValue:(int64_t)value
  5280. forKey:(uint64_t)key {
  5281. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5282. // on to get the type correct.
  5283. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5284. forKeys:&key
  5285. count:1] autorelease];
  5286. }
  5287. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5288. forKeys:(const uint64_t [])keys
  5289. count:(NSUInteger)count {
  5290. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5291. // on to get the type correct.
  5292. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5293. forKeys:keys
  5294. count:count] autorelease];
  5295. }
  5296. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5297. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5298. // on to get the type correct.
  5299. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5300. }
  5301. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5302. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5303. }
  5304. - (instancetype)init {
  5305. return [self initWithValues:NULL forKeys:NULL count:0];
  5306. }
  5307. - (instancetype)initWithValues:(const int64_t [])values
  5308. forKeys:(const uint64_t [])keys
  5309. count:(NSUInteger)count {
  5310. self = [super init];
  5311. if (self) {
  5312. _dictionary = [[NSMutableDictionary alloc] init];
  5313. if (count && values && keys) {
  5314. for (NSUInteger i = 0; i < count; ++i) {
  5315. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5316. }
  5317. }
  5318. }
  5319. return self;
  5320. }
  5321. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5322. self = [self initWithValues:NULL forKeys:NULL count:0];
  5323. if (self) {
  5324. if (dictionary) {
  5325. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5326. }
  5327. }
  5328. return self;
  5329. }
  5330. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5331. #pragma unused(numItems)
  5332. return [self initWithValues:NULL forKeys:NULL count:0];
  5333. }
  5334. - (void)dealloc {
  5335. NSAssert(!_autocreator,
  5336. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5337. [self class], _autocreator);
  5338. [_dictionary release];
  5339. [super dealloc];
  5340. }
  5341. - (instancetype)copyWithZone:(NSZone *)zone {
  5342. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5343. }
  5344. - (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
  5345. if (self == other) {
  5346. return YES;
  5347. }
  5348. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5349. return NO;
  5350. }
  5351. return [_dictionary isEqual:other->_dictionary];
  5352. }
  5353. - (NSUInteger)hash {
  5354. return _dictionary.count;
  5355. }
  5356. - (NSString *)description {
  5357. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5358. }
  5359. - (NSUInteger)count {
  5360. return _dictionary.count;
  5361. }
  5362. - (void)enumerateKeysAndValuesUsingBlock:
  5363. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5364. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5365. NSNumber *aValue,
  5366. BOOL *stop) {
  5367. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5368. }];
  5369. }
  5370. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5371. NSUInteger count = _dictionary.count;
  5372. if (count == 0) {
  5373. return 0;
  5374. }
  5375. GPBType valueType = GPBGetFieldType(field);
  5376. GPBType keyType = field.mapKeyType;
  5377. __block size_t result = 0;
  5378. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5379. NSNumber *aValue,
  5380. BOOL *stop) {
  5381. #pragma unused(stop)
  5382. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5383. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  5384. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5385. }];
  5386. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5387. result += tagSize * count;
  5388. return result;
  5389. }
  5390. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5391. asField:(GPBFieldDescriptor *)field {
  5392. GPBType valueType = GPBGetFieldType(field);
  5393. GPBType keyType = field.mapKeyType;
  5394. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5395. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5396. NSNumber *aValue,
  5397. BOOL *stop) {
  5398. #pragma unused(stop)
  5399. // Write the tag.
  5400. [outputStream writeInt32NoTag:tag];
  5401. // Write the size of the message.
  5402. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5403. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  5404. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5405. // Write the fields.
  5406. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5407. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  5408. }];
  5409. }
  5410. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5411. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5412. }
  5413. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5414. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5415. #pragma unused(stop)
  5416. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5417. }];
  5418. }
  5419. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5420. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5421. if (wrapped && value) {
  5422. *value = [wrapped longLongValue];
  5423. }
  5424. return (wrapped != NULL);
  5425. }
  5426. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5427. if (otherDictionary) {
  5428. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5429. if (_autocreator) {
  5430. GPBAutocreatedDictionaryModified(_autocreator, self);
  5431. }
  5432. }
  5433. }
  5434. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5435. [_dictionary setObject:@(value) forKey:@(key)];
  5436. if (_autocreator) {
  5437. GPBAutocreatedDictionaryModified(_autocreator, self);
  5438. }
  5439. }
  5440. - (void)removeValueForKey:(uint64_t)aKey {
  5441. [_dictionary removeObjectForKey:@(aKey)];
  5442. }
  5443. - (void)removeAll {
  5444. [_dictionary removeAllObjects];
  5445. }
  5446. @end
  5447. #pragma mark - UInt64 -> Bool
  5448. @implementation GPBUInt64BoolDictionary {
  5449. @package
  5450. NSMutableDictionary *_dictionary;
  5451. }
  5452. + (instancetype)dictionary {
  5453. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5454. }
  5455. + (instancetype)dictionaryWithValue:(BOOL)value
  5456. forKey:(uint64_t)key {
  5457. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5458. // on to get the type correct.
  5459. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5460. forKeys:&key
  5461. count:1] autorelease];
  5462. }
  5463. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5464. forKeys:(const uint64_t [])keys
  5465. count:(NSUInteger)count {
  5466. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5467. // on to get the type correct.
  5468. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5469. forKeys:keys
  5470. count:count] autorelease];
  5471. }
  5472. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5473. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5474. // on to get the type correct.
  5475. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5476. }
  5477. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5478. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5479. }
  5480. - (instancetype)init {
  5481. return [self initWithValues:NULL forKeys:NULL count:0];
  5482. }
  5483. - (instancetype)initWithValues:(const BOOL [])values
  5484. forKeys:(const uint64_t [])keys
  5485. count:(NSUInteger)count {
  5486. self = [super init];
  5487. if (self) {
  5488. _dictionary = [[NSMutableDictionary alloc] init];
  5489. if (count && values && keys) {
  5490. for (NSUInteger i = 0; i < count; ++i) {
  5491. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5492. }
  5493. }
  5494. }
  5495. return self;
  5496. }
  5497. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5498. self = [self initWithValues:NULL forKeys:NULL count:0];
  5499. if (self) {
  5500. if (dictionary) {
  5501. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5502. }
  5503. }
  5504. return self;
  5505. }
  5506. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5507. #pragma unused(numItems)
  5508. return [self initWithValues:NULL forKeys:NULL count:0];
  5509. }
  5510. - (void)dealloc {
  5511. NSAssert(!_autocreator,
  5512. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5513. [self class], _autocreator);
  5514. [_dictionary release];
  5515. [super dealloc];
  5516. }
  5517. - (instancetype)copyWithZone:(NSZone *)zone {
  5518. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5519. }
  5520. - (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
  5521. if (self == other) {
  5522. return YES;
  5523. }
  5524. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5525. return NO;
  5526. }
  5527. return [_dictionary isEqual:other->_dictionary];
  5528. }
  5529. - (NSUInteger)hash {
  5530. return _dictionary.count;
  5531. }
  5532. - (NSString *)description {
  5533. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5534. }
  5535. - (NSUInteger)count {
  5536. return _dictionary.count;
  5537. }
  5538. - (void)enumerateKeysAndValuesUsingBlock:
  5539. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5540. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5541. NSNumber *aValue,
  5542. BOOL *stop) {
  5543. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5544. }];
  5545. }
  5546. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5547. NSUInteger count = _dictionary.count;
  5548. if (count == 0) {
  5549. return 0;
  5550. }
  5551. GPBType valueType = GPBGetFieldType(field);
  5552. GPBType keyType = field.mapKeyType;
  5553. __block size_t result = 0;
  5554. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5555. NSNumber *aValue,
  5556. BOOL *stop) {
  5557. #pragma unused(stop)
  5558. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5559. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  5560. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5561. }];
  5562. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5563. result += tagSize * count;
  5564. return result;
  5565. }
  5566. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5567. asField:(GPBFieldDescriptor *)field {
  5568. GPBType valueType = GPBGetFieldType(field);
  5569. GPBType keyType = field.mapKeyType;
  5570. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5571. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5572. NSNumber *aValue,
  5573. BOOL *stop) {
  5574. #pragma unused(stop)
  5575. // Write the tag.
  5576. [outputStream writeInt32NoTag:tag];
  5577. // Write the size of the message.
  5578. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5579. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  5580. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5581. // Write the fields.
  5582. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5583. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  5584. }];
  5585. }
  5586. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5587. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5588. }
  5589. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5590. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5591. #pragma unused(stop)
  5592. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5593. }];
  5594. }
  5595. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5596. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5597. if (wrapped && value) {
  5598. *value = [wrapped boolValue];
  5599. }
  5600. return (wrapped != NULL);
  5601. }
  5602. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5603. if (otherDictionary) {
  5604. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5605. if (_autocreator) {
  5606. GPBAutocreatedDictionaryModified(_autocreator, self);
  5607. }
  5608. }
  5609. }
  5610. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5611. [_dictionary setObject:@(value) forKey:@(key)];
  5612. if (_autocreator) {
  5613. GPBAutocreatedDictionaryModified(_autocreator, self);
  5614. }
  5615. }
  5616. - (void)removeValueForKey:(uint64_t)aKey {
  5617. [_dictionary removeObjectForKey:@(aKey)];
  5618. }
  5619. - (void)removeAll {
  5620. [_dictionary removeAllObjects];
  5621. }
  5622. @end
  5623. #pragma mark - UInt64 -> Float
  5624. @implementation GPBUInt64FloatDictionary {
  5625. @package
  5626. NSMutableDictionary *_dictionary;
  5627. }
  5628. + (instancetype)dictionary {
  5629. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5630. }
  5631. + (instancetype)dictionaryWithValue:(float)value
  5632. forKey:(uint64_t)key {
  5633. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5634. // on to get the type correct.
  5635. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5636. forKeys:&key
  5637. count:1] autorelease];
  5638. }
  5639. + (instancetype)dictionaryWithValues:(const float [])values
  5640. forKeys:(const uint64_t [])keys
  5641. count:(NSUInteger)count {
  5642. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5643. // on to get the type correct.
  5644. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5645. forKeys:keys
  5646. count:count] autorelease];
  5647. }
  5648. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5649. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5650. // on to get the type correct.
  5651. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5652. }
  5653. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5654. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5655. }
  5656. - (instancetype)init {
  5657. return [self initWithValues:NULL forKeys:NULL count:0];
  5658. }
  5659. - (instancetype)initWithValues:(const float [])values
  5660. forKeys:(const uint64_t [])keys
  5661. count:(NSUInteger)count {
  5662. self = [super init];
  5663. if (self) {
  5664. _dictionary = [[NSMutableDictionary alloc] init];
  5665. if (count && values && keys) {
  5666. for (NSUInteger i = 0; i < count; ++i) {
  5667. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5668. }
  5669. }
  5670. }
  5671. return self;
  5672. }
  5673. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5674. self = [self initWithValues:NULL forKeys:NULL count:0];
  5675. if (self) {
  5676. if (dictionary) {
  5677. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5678. }
  5679. }
  5680. return self;
  5681. }
  5682. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5683. #pragma unused(numItems)
  5684. return [self initWithValues:NULL forKeys:NULL count:0];
  5685. }
  5686. - (void)dealloc {
  5687. NSAssert(!_autocreator,
  5688. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5689. [self class], _autocreator);
  5690. [_dictionary release];
  5691. [super dealloc];
  5692. }
  5693. - (instancetype)copyWithZone:(NSZone *)zone {
  5694. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5695. }
  5696. - (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
  5697. if (self == other) {
  5698. return YES;
  5699. }
  5700. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5701. return NO;
  5702. }
  5703. return [_dictionary isEqual:other->_dictionary];
  5704. }
  5705. - (NSUInteger)hash {
  5706. return _dictionary.count;
  5707. }
  5708. - (NSString *)description {
  5709. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5710. }
  5711. - (NSUInteger)count {
  5712. return _dictionary.count;
  5713. }
  5714. - (void)enumerateKeysAndValuesUsingBlock:
  5715. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5716. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5717. NSNumber *aValue,
  5718. BOOL *stop) {
  5719. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5720. }];
  5721. }
  5722. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5723. NSUInteger count = _dictionary.count;
  5724. if (count == 0) {
  5725. return 0;
  5726. }
  5727. GPBType valueType = GPBGetFieldType(field);
  5728. GPBType keyType = field.mapKeyType;
  5729. __block size_t result = 0;
  5730. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5731. NSNumber *aValue,
  5732. BOOL *stop) {
  5733. #pragma unused(stop)
  5734. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5735. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  5736. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5737. }];
  5738. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5739. result += tagSize * count;
  5740. return result;
  5741. }
  5742. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5743. asField:(GPBFieldDescriptor *)field {
  5744. GPBType valueType = GPBGetFieldType(field);
  5745. GPBType keyType = field.mapKeyType;
  5746. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5747. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5748. NSNumber *aValue,
  5749. BOOL *stop) {
  5750. #pragma unused(stop)
  5751. // Write the tag.
  5752. [outputStream writeInt32NoTag:tag];
  5753. // Write the size of the message.
  5754. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5755. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  5756. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5757. // Write the fields.
  5758. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5759. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  5760. }];
  5761. }
  5762. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5763. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5764. }
  5765. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5766. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5767. #pragma unused(stop)
  5768. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5769. }];
  5770. }
  5771. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5772. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5773. if (wrapped && value) {
  5774. *value = [wrapped floatValue];
  5775. }
  5776. return (wrapped != NULL);
  5777. }
  5778. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5779. if (otherDictionary) {
  5780. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5781. if (_autocreator) {
  5782. GPBAutocreatedDictionaryModified(_autocreator, self);
  5783. }
  5784. }
  5785. }
  5786. - (void)setValue:(float)value forKey:(uint64_t)key {
  5787. [_dictionary setObject:@(value) forKey:@(key)];
  5788. if (_autocreator) {
  5789. GPBAutocreatedDictionaryModified(_autocreator, self);
  5790. }
  5791. }
  5792. - (void)removeValueForKey:(uint64_t)aKey {
  5793. [_dictionary removeObjectForKey:@(aKey)];
  5794. }
  5795. - (void)removeAll {
  5796. [_dictionary removeAllObjects];
  5797. }
  5798. @end
  5799. #pragma mark - UInt64 -> Double
  5800. @implementation GPBUInt64DoubleDictionary {
  5801. @package
  5802. NSMutableDictionary *_dictionary;
  5803. }
  5804. + (instancetype)dictionary {
  5805. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5806. }
  5807. + (instancetype)dictionaryWithValue:(double)value
  5808. forKey:(uint64_t)key {
  5809. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5810. // on to get the type correct.
  5811. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5812. forKeys:&key
  5813. count:1] autorelease];
  5814. }
  5815. + (instancetype)dictionaryWithValues:(const double [])values
  5816. forKeys:(const uint64_t [])keys
  5817. count:(NSUInteger)count {
  5818. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5819. // on to get the type correct.
  5820. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5821. forKeys:keys
  5822. count:count] autorelease];
  5823. }
  5824. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5825. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5826. // on to get the type correct.
  5827. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5828. }
  5829. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5830. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5831. }
  5832. - (instancetype)init {
  5833. return [self initWithValues:NULL forKeys:NULL count:0];
  5834. }
  5835. - (instancetype)initWithValues:(const double [])values
  5836. forKeys:(const uint64_t [])keys
  5837. count:(NSUInteger)count {
  5838. self = [super init];
  5839. if (self) {
  5840. _dictionary = [[NSMutableDictionary alloc] init];
  5841. if (count && values && keys) {
  5842. for (NSUInteger i = 0; i < count; ++i) {
  5843. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5844. }
  5845. }
  5846. }
  5847. return self;
  5848. }
  5849. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5850. self = [self initWithValues:NULL forKeys:NULL count:0];
  5851. if (self) {
  5852. if (dictionary) {
  5853. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5854. }
  5855. }
  5856. return self;
  5857. }
  5858. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5859. #pragma unused(numItems)
  5860. return [self initWithValues:NULL forKeys:NULL count:0];
  5861. }
  5862. - (void)dealloc {
  5863. NSAssert(!_autocreator,
  5864. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5865. [self class], _autocreator);
  5866. [_dictionary release];
  5867. [super dealloc];
  5868. }
  5869. - (instancetype)copyWithZone:(NSZone *)zone {
  5870. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5871. }
  5872. - (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
  5873. if (self == other) {
  5874. return YES;
  5875. }
  5876. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5877. return NO;
  5878. }
  5879. return [_dictionary isEqual:other->_dictionary];
  5880. }
  5881. - (NSUInteger)hash {
  5882. return _dictionary.count;
  5883. }
  5884. - (NSString *)description {
  5885. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5886. }
  5887. - (NSUInteger)count {
  5888. return _dictionary.count;
  5889. }
  5890. - (void)enumerateKeysAndValuesUsingBlock:
  5891. (void (^)(uint64_t key, double value, BOOL *stop))block {
  5892. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5893. NSNumber *aValue,
  5894. BOOL *stop) {
  5895. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  5896. }];
  5897. }
  5898. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5899. NSUInteger count = _dictionary.count;
  5900. if (count == 0) {
  5901. return 0;
  5902. }
  5903. GPBType valueType = GPBGetFieldType(field);
  5904. GPBType keyType = field.mapKeyType;
  5905. __block size_t result = 0;
  5906. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5907. NSNumber *aValue,
  5908. BOOL *stop) {
  5909. #pragma unused(stop)
  5910. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5911. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  5912. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5913. }];
  5914. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  5915. result += tagSize * count;
  5916. return result;
  5917. }
  5918. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5919. asField:(GPBFieldDescriptor *)field {
  5920. GPBType valueType = GPBGetFieldType(field);
  5921. GPBType keyType = field.mapKeyType;
  5922. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5923. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5924. NSNumber *aValue,
  5925. BOOL *stop) {
  5926. #pragma unused(stop)
  5927. // Write the tag.
  5928. [outputStream writeInt32NoTag:tag];
  5929. // Write the size of the message.
  5930. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5931. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  5932. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5933. // Write the fields.
  5934. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  5935. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  5936. }];
  5937. }
  5938. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  5939. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5940. }
  5941. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5942. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  5943. #pragma unused(stop)
  5944. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5945. }];
  5946. }
  5947. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  5948. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5949. if (wrapped && value) {
  5950. *value = [wrapped doubleValue];
  5951. }
  5952. return (wrapped != NULL);
  5953. }
  5954. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5955. if (otherDictionary) {
  5956. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5957. if (_autocreator) {
  5958. GPBAutocreatedDictionaryModified(_autocreator, self);
  5959. }
  5960. }
  5961. }
  5962. - (void)setValue:(double)value forKey:(uint64_t)key {
  5963. [_dictionary setObject:@(value) forKey:@(key)];
  5964. if (_autocreator) {
  5965. GPBAutocreatedDictionaryModified(_autocreator, self);
  5966. }
  5967. }
  5968. - (void)removeValueForKey:(uint64_t)aKey {
  5969. [_dictionary removeObjectForKey:@(aKey)];
  5970. }
  5971. - (void)removeAll {
  5972. [_dictionary removeAllObjects];
  5973. }
  5974. @end
  5975. #pragma mark - UInt64 -> Enum
  5976. @implementation GPBUInt64EnumDictionary {
  5977. @package
  5978. NSMutableDictionary *_dictionary;
  5979. GPBEnumValidationFunc _validationFunc;
  5980. }
  5981. @synthesize validationFunc = _validationFunc;
  5982. + (instancetype)dictionary {
  5983. return [[[self alloc] initWithValidationFunction:NULL
  5984. rawValues:NULL
  5985. forKeys:NULL
  5986. count:0] autorelease];
  5987. }
  5988. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  5989. return [[[self alloc] initWithValidationFunction:func
  5990. rawValues:NULL
  5991. forKeys:NULL
  5992. count:0] autorelease];
  5993. }
  5994. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  5995. rawValue:(int32_t)rawValue
  5996. forKey:(uint64_t)key {
  5997. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5998. // on to get the type correct.
  5999. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6000. rawValues:&rawValue
  6001. forKeys:&key
  6002. count:1] autorelease];
  6003. }
  6004. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6005. rawValues:(const int32_t [])rawValues
  6006. forKeys:(const uint64_t [])keys
  6007. count:(NSUInteger)count {
  6008. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6009. // on to get the type correct.
  6010. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6011. rawValues:rawValues
  6012. forKeys:keys
  6013. count:count] autorelease];
  6014. }
  6015. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6016. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6017. // on to get the type correct.
  6018. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6019. }
  6020. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6021. capacity:(NSUInteger)numItems {
  6022. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6023. }
  6024. - (instancetype)init {
  6025. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6026. }
  6027. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6028. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6029. }
  6030. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6031. rawValues:(const int32_t [])rawValues
  6032. forKeys:(const uint64_t [])keys
  6033. count:(NSUInteger)count {
  6034. self = [super init];
  6035. if (self) {
  6036. _dictionary = [[NSMutableDictionary alloc] init];
  6037. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6038. if (count && rawValues && keys) {
  6039. for (NSUInteger i = 0; i < count; ++i) {
  6040. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6041. }
  6042. }
  6043. }
  6044. return self;
  6045. }
  6046. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6047. self = [self initWithValidationFunction:dictionary.validationFunc
  6048. rawValues:NULL
  6049. forKeys:NULL
  6050. count:0];
  6051. if (self) {
  6052. if (dictionary) {
  6053. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6054. }
  6055. }
  6056. return self;
  6057. }
  6058. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6059. capacity:(NSUInteger)numItems {
  6060. #pragma unused(numItems)
  6061. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6062. }
  6063. - (void)dealloc {
  6064. NSAssert(!_autocreator,
  6065. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6066. [self class], _autocreator);
  6067. [_dictionary release];
  6068. [super dealloc];
  6069. }
  6070. - (instancetype)copyWithZone:(NSZone *)zone {
  6071. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6072. }
  6073. - (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
  6074. if (self == other) {
  6075. return YES;
  6076. }
  6077. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6078. return NO;
  6079. }
  6080. return [_dictionary isEqual:other->_dictionary];
  6081. }
  6082. - (NSUInteger)hash {
  6083. return _dictionary.count;
  6084. }
  6085. - (NSString *)description {
  6086. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6087. }
  6088. - (NSUInteger)count {
  6089. return _dictionary.count;
  6090. }
  6091. - (void)enumerateKeysAndRawValuesUsingBlock:
  6092. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6093. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6094. NSNumber *aValue,
  6095. BOOL *stop) {
  6096. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6097. }];
  6098. }
  6099. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6100. NSUInteger count = _dictionary.count;
  6101. if (count == 0) {
  6102. return 0;
  6103. }
  6104. GPBType valueType = GPBGetFieldType(field);
  6105. GPBType keyType = field.mapKeyType;
  6106. __block size_t result = 0;
  6107. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6108. NSNumber *aValue,
  6109. BOOL *stop) {
  6110. #pragma unused(stop)
  6111. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6112. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  6113. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6114. }];
  6115. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6116. result += tagSize * count;
  6117. return result;
  6118. }
  6119. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6120. asField:(GPBFieldDescriptor *)field {
  6121. GPBType valueType = GPBGetFieldType(field);
  6122. GPBType keyType = field.mapKeyType;
  6123. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6124. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6125. NSNumber *aValue,
  6126. BOOL *stop) {
  6127. #pragma unused(stop)
  6128. // Write the tag.
  6129. [outputStream writeInt32NoTag:tag];
  6130. // Write the size of the message.
  6131. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6132. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  6133. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6134. // Write the fields.
  6135. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6136. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  6137. }];
  6138. }
  6139. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6140. forKey:(GPBValue *)key
  6141. keyType:(GPBType)keyType {
  6142. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyType);
  6143. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  6144. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6145. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6146. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyType);
  6147. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  6148. [outputStream release];
  6149. return data;
  6150. }
  6151. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6152. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6153. }
  6154. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6155. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6156. #pragma unused(stop)
  6157. block([NSString stringWithFormat:@"%llu", key], @(value));
  6158. }];
  6159. }
  6160. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  6161. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6162. if (wrapped && value) {
  6163. int32_t result = [wrapped intValue];
  6164. if (!_validationFunc(result)) {
  6165. result = kGPBUnrecognizedEnumeratorValue;
  6166. }
  6167. *value = result;
  6168. }
  6169. return (wrapped != NULL);
  6170. }
  6171. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  6172. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6173. if (wrapped && rawValue) {
  6174. *rawValue = [wrapped intValue];
  6175. }
  6176. return (wrapped != NULL);
  6177. }
  6178. - (void)enumerateKeysAndValuesUsingBlock:
  6179. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6180. GPBEnumValidationFunc func = _validationFunc;
  6181. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6182. NSNumber *aValue,
  6183. BOOL *stop) {
  6184. int32_t unwrapped = [aValue intValue];
  6185. if (!func(unwrapped)) {
  6186. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6187. }
  6188. block([aKey unsignedLongLongValue], unwrapped, stop);
  6189. }];
  6190. }
  6191. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6192. if (otherDictionary) {
  6193. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6194. if (_autocreator) {
  6195. GPBAutocreatedDictionaryModified(_autocreator, self);
  6196. }
  6197. }
  6198. }
  6199. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6200. [_dictionary setObject:@(value) forKey:@(key)];
  6201. if (_autocreator) {
  6202. GPBAutocreatedDictionaryModified(_autocreator, self);
  6203. }
  6204. }
  6205. - (void)removeValueForKey:(uint64_t)aKey {
  6206. [_dictionary removeObjectForKey:@(aKey)];
  6207. }
  6208. - (void)removeAll {
  6209. [_dictionary removeAllObjects];
  6210. }
  6211. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  6212. if (!_validationFunc(value)) {
  6213. [NSException raise:NSInvalidArgumentException
  6214. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6215. value];
  6216. }
  6217. [_dictionary setObject:@(value) forKey:@(key)];
  6218. if (_autocreator) {
  6219. GPBAutocreatedDictionaryModified(_autocreator, self);
  6220. }
  6221. }
  6222. @end
  6223. #pragma mark - UInt64 -> Object
  6224. @implementation GPBUInt64ObjectDictionary {
  6225. @package
  6226. NSMutableDictionary *_dictionary;
  6227. }
  6228. + (instancetype)dictionary {
  6229. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6230. }
  6231. + (instancetype)dictionaryWithValue:(id)value
  6232. forKey:(uint64_t)key {
  6233. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6234. // on to get the type correct.
  6235. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithValues:&value
  6236. forKeys:&key
  6237. count:1] autorelease];
  6238. }
  6239. + (instancetype)dictionaryWithValues:(const id [])values
  6240. forKeys:(const uint64_t [])keys
  6241. count:(NSUInteger)count {
  6242. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6243. // on to get the type correct.
  6244. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithValues:values
  6245. forKeys:keys
  6246. count:count] autorelease];
  6247. }
  6248. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6249. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6250. // on to get the type correct.
  6251. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6252. }
  6253. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6254. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6255. }
  6256. - (instancetype)init {
  6257. return [self initWithValues:NULL forKeys:NULL count:0];
  6258. }
  6259. - (instancetype)initWithValues:(const id [])values
  6260. forKeys:(const uint64_t [])keys
  6261. count:(NSUInteger)count {
  6262. self = [super init];
  6263. if (self) {
  6264. _dictionary = [[NSMutableDictionary alloc] init];
  6265. if (count && values && keys) {
  6266. for (NSUInteger i = 0; i < count; ++i) {
  6267. [_dictionary setObject:values[i] forKey:@(keys[i])];
  6268. }
  6269. }
  6270. }
  6271. return self;
  6272. }
  6273. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6274. self = [self initWithValues:NULL forKeys:NULL count:0];
  6275. if (self) {
  6276. if (dictionary) {
  6277. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6278. }
  6279. }
  6280. return self;
  6281. }
  6282. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6283. #pragma unused(numItems)
  6284. return [self initWithValues:NULL forKeys:NULL count:0];
  6285. }
  6286. - (void)dealloc {
  6287. NSAssert(!_autocreator,
  6288. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6289. [self class], _autocreator);
  6290. [_dictionary release];
  6291. [super dealloc];
  6292. }
  6293. - (instancetype)copyWithZone:(NSZone *)zone {
  6294. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6295. }
  6296. - (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
  6297. if (self == other) {
  6298. return YES;
  6299. }
  6300. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6301. return NO;
  6302. }
  6303. return [_dictionary isEqual:other->_dictionary];
  6304. }
  6305. - (NSUInteger)hash {
  6306. return _dictionary.count;
  6307. }
  6308. - (NSString *)description {
  6309. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6310. }
  6311. - (NSUInteger)count {
  6312. return _dictionary.count;
  6313. }
  6314. - (void)enumerateKeysAndValuesUsingBlock:
  6315. (void (^)(uint64_t key, id value, BOOL *stop))block {
  6316. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6317. id aValue,
  6318. BOOL *stop) {
  6319. block([aKey unsignedLongLongValue], aValue, stop);
  6320. }];
  6321. }
  6322. - (BOOL)isInitialized {
  6323. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6324. if (!msg.initialized) {
  6325. return NO;
  6326. }
  6327. }
  6328. return YES;
  6329. }
  6330. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6331. GPBUInt64ObjectDictionary *newDict =
  6332. [[GPBUInt64ObjectDictionary alloc] init];
  6333. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6334. GPBMessage *msg,
  6335. BOOL *stop) {
  6336. #pragma unused(stop)
  6337. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6338. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6339. [copiedMsg release];
  6340. }];
  6341. return newDict;
  6342. }
  6343. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6344. NSUInteger count = _dictionary.count;
  6345. if (count == 0) {
  6346. return 0;
  6347. }
  6348. GPBType valueType = GPBGetFieldType(field);
  6349. GPBType keyType = field.mapKeyType;
  6350. __block size_t result = 0;
  6351. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6352. id aValue,
  6353. BOOL *stop) {
  6354. #pragma unused(stop)
  6355. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6356. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  6357. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6358. }];
  6359. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6360. result += tagSize * count;
  6361. return result;
  6362. }
  6363. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6364. asField:(GPBFieldDescriptor *)field {
  6365. GPBType valueType = GPBGetFieldType(field);
  6366. GPBType keyType = field.mapKeyType;
  6367. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6368. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6369. id aValue,
  6370. BOOL *stop) {
  6371. #pragma unused(stop)
  6372. // Write the tag.
  6373. [outputStream writeInt32NoTag:tag];
  6374. // Write the size of the message.
  6375. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6376. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  6377. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6378. // Write the fields.
  6379. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyType);
  6380. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  6381. }];
  6382. }
  6383. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6384. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6385. }
  6386. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6387. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, id value, BOOL *stop) {
  6388. #pragma unused(stop)
  6389. block([NSString stringWithFormat:@"%llu", key], value);
  6390. }];
  6391. }
  6392. - (id)valueForKey:(uint64_t)key {
  6393. id result = [_dictionary objectForKey:@(key)];
  6394. return result;
  6395. }
  6396. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6397. if (otherDictionary) {
  6398. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6399. if (_autocreator) {
  6400. GPBAutocreatedDictionaryModified(_autocreator, self);
  6401. }
  6402. }
  6403. }
  6404. - (void)setValue:(id)value forKey:(uint64_t)key {
  6405. [_dictionary setObject:value forKey:@(key)];
  6406. if (_autocreator) {
  6407. GPBAutocreatedDictionaryModified(_autocreator, self);
  6408. }
  6409. }
  6410. - (void)removeValueForKey:(uint64_t)aKey {
  6411. [_dictionary removeObjectForKey:@(aKey)];
  6412. }
  6413. - (void)removeAll {
  6414. [_dictionary removeAllObjects];
  6415. }
  6416. @end
  6417. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6418. // This block of code is generated, do not edit it directly.
  6419. #pragma mark - Int64 -> UInt32
  6420. @implementation GPBInt64UInt32Dictionary {
  6421. @package
  6422. NSMutableDictionary *_dictionary;
  6423. }
  6424. + (instancetype)dictionary {
  6425. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6426. }
  6427. + (instancetype)dictionaryWithValue:(uint32_t)value
  6428. forKey:(int64_t)key {
  6429. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6430. // on to get the type correct.
  6431. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6432. forKeys:&key
  6433. count:1] autorelease];
  6434. }
  6435. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6436. forKeys:(const int64_t [])keys
  6437. count:(NSUInteger)count {
  6438. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6439. // on to get the type correct.
  6440. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6441. forKeys:keys
  6442. count:count] autorelease];
  6443. }
  6444. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6445. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6446. // on to get the type correct.
  6447. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6448. }
  6449. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6450. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6451. }
  6452. - (instancetype)init {
  6453. return [self initWithValues:NULL forKeys:NULL count:0];
  6454. }
  6455. - (instancetype)initWithValues:(const uint32_t [])values
  6456. forKeys:(const int64_t [])keys
  6457. count:(NSUInteger)count {
  6458. self = [super init];
  6459. if (self) {
  6460. _dictionary = [[NSMutableDictionary alloc] init];
  6461. if (count && values && keys) {
  6462. for (NSUInteger i = 0; i < count; ++i) {
  6463. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6464. }
  6465. }
  6466. }
  6467. return self;
  6468. }
  6469. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6470. self = [self initWithValues:NULL forKeys:NULL count:0];
  6471. if (self) {
  6472. if (dictionary) {
  6473. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6474. }
  6475. }
  6476. return self;
  6477. }
  6478. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6479. #pragma unused(numItems)
  6480. return [self initWithValues:NULL forKeys:NULL count:0];
  6481. }
  6482. - (void)dealloc {
  6483. NSAssert(!_autocreator,
  6484. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6485. [self class], _autocreator);
  6486. [_dictionary release];
  6487. [super dealloc];
  6488. }
  6489. - (instancetype)copyWithZone:(NSZone *)zone {
  6490. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6491. }
  6492. - (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
  6493. if (self == other) {
  6494. return YES;
  6495. }
  6496. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6497. return NO;
  6498. }
  6499. return [_dictionary isEqual:other->_dictionary];
  6500. }
  6501. - (NSUInteger)hash {
  6502. return _dictionary.count;
  6503. }
  6504. - (NSString *)description {
  6505. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6506. }
  6507. - (NSUInteger)count {
  6508. return _dictionary.count;
  6509. }
  6510. - (void)enumerateKeysAndValuesUsingBlock:
  6511. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6512. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6513. NSNumber *aValue,
  6514. BOOL *stop) {
  6515. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6516. }];
  6517. }
  6518. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6519. NSUInteger count = _dictionary.count;
  6520. if (count == 0) {
  6521. return 0;
  6522. }
  6523. GPBType valueType = GPBGetFieldType(field);
  6524. GPBType keyType = field.mapKeyType;
  6525. __block size_t result = 0;
  6526. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6527. NSNumber *aValue,
  6528. BOOL *stop) {
  6529. #pragma unused(stop)
  6530. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6531. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  6532. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6533. }];
  6534. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6535. result += tagSize * count;
  6536. return result;
  6537. }
  6538. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6539. asField:(GPBFieldDescriptor *)field {
  6540. GPBType valueType = GPBGetFieldType(field);
  6541. GPBType keyType = field.mapKeyType;
  6542. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6543. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6544. NSNumber *aValue,
  6545. BOOL *stop) {
  6546. #pragma unused(stop)
  6547. // Write the tag.
  6548. [outputStream writeInt32NoTag:tag];
  6549. // Write the size of the message.
  6550. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6551. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  6552. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6553. // Write the fields.
  6554. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6555. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  6556. }];
  6557. }
  6558. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6559. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6560. }
  6561. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6562. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6563. #pragma unused(stop)
  6564. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6565. }];
  6566. }
  6567. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6568. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6569. if (wrapped && value) {
  6570. *value = [wrapped unsignedIntValue];
  6571. }
  6572. return (wrapped != NULL);
  6573. }
  6574. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6575. if (otherDictionary) {
  6576. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6577. if (_autocreator) {
  6578. GPBAutocreatedDictionaryModified(_autocreator, self);
  6579. }
  6580. }
  6581. }
  6582. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6583. [_dictionary setObject:@(value) forKey:@(key)];
  6584. if (_autocreator) {
  6585. GPBAutocreatedDictionaryModified(_autocreator, self);
  6586. }
  6587. }
  6588. - (void)removeValueForKey:(int64_t)aKey {
  6589. [_dictionary removeObjectForKey:@(aKey)];
  6590. }
  6591. - (void)removeAll {
  6592. [_dictionary removeAllObjects];
  6593. }
  6594. @end
  6595. #pragma mark - Int64 -> Int32
  6596. @implementation GPBInt64Int32Dictionary {
  6597. @package
  6598. NSMutableDictionary *_dictionary;
  6599. }
  6600. + (instancetype)dictionary {
  6601. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6602. }
  6603. + (instancetype)dictionaryWithValue:(int32_t)value
  6604. forKey:(int64_t)key {
  6605. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6606. // on to get the type correct.
  6607. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6608. forKeys:&key
  6609. count:1] autorelease];
  6610. }
  6611. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6612. forKeys:(const int64_t [])keys
  6613. count:(NSUInteger)count {
  6614. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6615. // on to get the type correct.
  6616. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6617. forKeys:keys
  6618. count:count] autorelease];
  6619. }
  6620. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6621. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6622. // on to get the type correct.
  6623. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6624. }
  6625. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6626. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6627. }
  6628. - (instancetype)init {
  6629. return [self initWithValues:NULL forKeys:NULL count:0];
  6630. }
  6631. - (instancetype)initWithValues:(const int32_t [])values
  6632. forKeys:(const int64_t [])keys
  6633. count:(NSUInteger)count {
  6634. self = [super init];
  6635. if (self) {
  6636. _dictionary = [[NSMutableDictionary alloc] init];
  6637. if (count && values && keys) {
  6638. for (NSUInteger i = 0; i < count; ++i) {
  6639. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6640. }
  6641. }
  6642. }
  6643. return self;
  6644. }
  6645. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6646. self = [self initWithValues:NULL forKeys:NULL count:0];
  6647. if (self) {
  6648. if (dictionary) {
  6649. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6650. }
  6651. }
  6652. return self;
  6653. }
  6654. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6655. #pragma unused(numItems)
  6656. return [self initWithValues:NULL forKeys:NULL count:0];
  6657. }
  6658. - (void)dealloc {
  6659. NSAssert(!_autocreator,
  6660. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6661. [self class], _autocreator);
  6662. [_dictionary release];
  6663. [super dealloc];
  6664. }
  6665. - (instancetype)copyWithZone:(NSZone *)zone {
  6666. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6667. }
  6668. - (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
  6669. if (self == other) {
  6670. return YES;
  6671. }
  6672. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6673. return NO;
  6674. }
  6675. return [_dictionary isEqual:other->_dictionary];
  6676. }
  6677. - (NSUInteger)hash {
  6678. return _dictionary.count;
  6679. }
  6680. - (NSString *)description {
  6681. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6682. }
  6683. - (NSUInteger)count {
  6684. return _dictionary.count;
  6685. }
  6686. - (void)enumerateKeysAndValuesUsingBlock:
  6687. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6688. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6689. NSNumber *aValue,
  6690. BOOL *stop) {
  6691. block([aKey longLongValue], [aValue intValue], stop);
  6692. }];
  6693. }
  6694. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6695. NSUInteger count = _dictionary.count;
  6696. if (count == 0) {
  6697. return 0;
  6698. }
  6699. GPBType valueType = GPBGetFieldType(field);
  6700. GPBType keyType = field.mapKeyType;
  6701. __block size_t result = 0;
  6702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6703. NSNumber *aValue,
  6704. BOOL *stop) {
  6705. #pragma unused(stop)
  6706. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6707. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  6708. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6709. }];
  6710. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6711. result += tagSize * count;
  6712. return result;
  6713. }
  6714. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6715. asField:(GPBFieldDescriptor *)field {
  6716. GPBType valueType = GPBGetFieldType(field);
  6717. GPBType keyType = field.mapKeyType;
  6718. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6719. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6720. NSNumber *aValue,
  6721. BOOL *stop) {
  6722. #pragma unused(stop)
  6723. // Write the tag.
  6724. [outputStream writeInt32NoTag:tag];
  6725. // Write the size of the message.
  6726. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6727. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  6728. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6729. // Write the fields.
  6730. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6731. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  6732. }];
  6733. }
  6734. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6735. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6736. }
  6737. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6738. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6739. #pragma unused(stop)
  6740. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6741. }];
  6742. }
  6743. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6744. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6745. if (wrapped && value) {
  6746. *value = [wrapped intValue];
  6747. }
  6748. return (wrapped != NULL);
  6749. }
  6750. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6751. if (otherDictionary) {
  6752. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6753. if (_autocreator) {
  6754. GPBAutocreatedDictionaryModified(_autocreator, self);
  6755. }
  6756. }
  6757. }
  6758. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6759. [_dictionary setObject:@(value) forKey:@(key)];
  6760. if (_autocreator) {
  6761. GPBAutocreatedDictionaryModified(_autocreator, self);
  6762. }
  6763. }
  6764. - (void)removeValueForKey:(int64_t)aKey {
  6765. [_dictionary removeObjectForKey:@(aKey)];
  6766. }
  6767. - (void)removeAll {
  6768. [_dictionary removeAllObjects];
  6769. }
  6770. @end
  6771. #pragma mark - Int64 -> UInt64
  6772. @implementation GPBInt64UInt64Dictionary {
  6773. @package
  6774. NSMutableDictionary *_dictionary;
  6775. }
  6776. + (instancetype)dictionary {
  6777. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6778. }
  6779. + (instancetype)dictionaryWithValue:(uint64_t)value
  6780. forKey:(int64_t)key {
  6781. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6782. // on to get the type correct.
  6783. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6784. forKeys:&key
  6785. count:1] autorelease];
  6786. }
  6787. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6788. forKeys:(const int64_t [])keys
  6789. count:(NSUInteger)count {
  6790. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6791. // on to get the type correct.
  6792. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6793. forKeys:keys
  6794. count:count] autorelease];
  6795. }
  6796. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6797. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6798. // on to get the type correct.
  6799. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6800. }
  6801. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6802. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6803. }
  6804. - (instancetype)init {
  6805. return [self initWithValues:NULL forKeys:NULL count:0];
  6806. }
  6807. - (instancetype)initWithValues:(const uint64_t [])values
  6808. forKeys:(const int64_t [])keys
  6809. count:(NSUInteger)count {
  6810. self = [super init];
  6811. if (self) {
  6812. _dictionary = [[NSMutableDictionary alloc] init];
  6813. if (count && values && keys) {
  6814. for (NSUInteger i = 0; i < count; ++i) {
  6815. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6816. }
  6817. }
  6818. }
  6819. return self;
  6820. }
  6821. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6822. self = [self initWithValues:NULL forKeys:NULL count:0];
  6823. if (self) {
  6824. if (dictionary) {
  6825. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6826. }
  6827. }
  6828. return self;
  6829. }
  6830. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6831. #pragma unused(numItems)
  6832. return [self initWithValues:NULL forKeys:NULL count:0];
  6833. }
  6834. - (void)dealloc {
  6835. NSAssert(!_autocreator,
  6836. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6837. [self class], _autocreator);
  6838. [_dictionary release];
  6839. [super dealloc];
  6840. }
  6841. - (instancetype)copyWithZone:(NSZone *)zone {
  6842. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6843. }
  6844. - (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
  6845. if (self == other) {
  6846. return YES;
  6847. }
  6848. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6849. return NO;
  6850. }
  6851. return [_dictionary isEqual:other->_dictionary];
  6852. }
  6853. - (NSUInteger)hash {
  6854. return _dictionary.count;
  6855. }
  6856. - (NSString *)description {
  6857. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6858. }
  6859. - (NSUInteger)count {
  6860. return _dictionary.count;
  6861. }
  6862. - (void)enumerateKeysAndValuesUsingBlock:
  6863. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  6864. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6865. NSNumber *aValue,
  6866. BOOL *stop) {
  6867. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  6868. }];
  6869. }
  6870. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6871. NSUInteger count = _dictionary.count;
  6872. if (count == 0) {
  6873. return 0;
  6874. }
  6875. GPBType valueType = GPBGetFieldType(field);
  6876. GPBType keyType = field.mapKeyType;
  6877. __block size_t result = 0;
  6878. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6879. NSNumber *aValue,
  6880. BOOL *stop) {
  6881. #pragma unused(stop)
  6882. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6883. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  6884. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6885. }];
  6886. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  6887. result += tagSize * count;
  6888. return result;
  6889. }
  6890. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6891. asField:(GPBFieldDescriptor *)field {
  6892. GPBType valueType = GPBGetFieldType(field);
  6893. GPBType keyType = field.mapKeyType;
  6894. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6895. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6896. NSNumber *aValue,
  6897. BOOL *stop) {
  6898. #pragma unused(stop)
  6899. // Write the tag.
  6900. [outputStream writeInt32NoTag:tag];
  6901. // Write the size of the message.
  6902. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  6903. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  6904. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6905. // Write the fields.
  6906. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  6907. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  6908. }];
  6909. }
  6910. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  6911. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6912. }
  6913. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6914. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  6915. #pragma unused(stop)
  6916. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6917. }];
  6918. }
  6919. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  6920. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6921. if (wrapped && value) {
  6922. *value = [wrapped unsignedLongLongValue];
  6923. }
  6924. return (wrapped != NULL);
  6925. }
  6926. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6927. if (otherDictionary) {
  6928. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6929. if (_autocreator) {
  6930. GPBAutocreatedDictionaryModified(_autocreator, self);
  6931. }
  6932. }
  6933. }
  6934. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  6935. [_dictionary setObject:@(value) forKey:@(key)];
  6936. if (_autocreator) {
  6937. GPBAutocreatedDictionaryModified(_autocreator, self);
  6938. }
  6939. }
  6940. - (void)removeValueForKey:(int64_t)aKey {
  6941. [_dictionary removeObjectForKey:@(aKey)];
  6942. }
  6943. - (void)removeAll {
  6944. [_dictionary removeAllObjects];
  6945. }
  6946. @end
  6947. #pragma mark - Int64 -> Int64
  6948. @implementation GPBInt64Int64Dictionary {
  6949. @package
  6950. NSMutableDictionary *_dictionary;
  6951. }
  6952. + (instancetype)dictionary {
  6953. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6954. }
  6955. + (instancetype)dictionaryWithValue:(int64_t)value
  6956. forKey:(int64_t)key {
  6957. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6958. // on to get the type correct.
  6959. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  6960. forKeys:&key
  6961. count:1] autorelease];
  6962. }
  6963. + (instancetype)dictionaryWithValues:(const int64_t [])values
  6964. forKeys:(const int64_t [])keys
  6965. count:(NSUInteger)count {
  6966. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6967. // on to get the type correct.
  6968. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  6969. forKeys:keys
  6970. count:count] autorelease];
  6971. }
  6972. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6973. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6974. // on to get the type correct.
  6975. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6976. }
  6977. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6978. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6979. }
  6980. - (instancetype)init {
  6981. return [self initWithValues:NULL forKeys:NULL count:0];
  6982. }
  6983. - (instancetype)initWithValues:(const int64_t [])values
  6984. forKeys:(const int64_t [])keys
  6985. count:(NSUInteger)count {
  6986. self = [super init];
  6987. if (self) {
  6988. _dictionary = [[NSMutableDictionary alloc] init];
  6989. if (count && values && keys) {
  6990. for (NSUInteger i = 0; i < count; ++i) {
  6991. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6992. }
  6993. }
  6994. }
  6995. return self;
  6996. }
  6997. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6998. self = [self initWithValues:NULL forKeys:NULL count:0];
  6999. if (self) {
  7000. if (dictionary) {
  7001. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7002. }
  7003. }
  7004. return self;
  7005. }
  7006. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7007. #pragma unused(numItems)
  7008. return [self initWithValues:NULL forKeys:NULL count:0];
  7009. }
  7010. - (void)dealloc {
  7011. NSAssert(!_autocreator,
  7012. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7013. [self class], _autocreator);
  7014. [_dictionary release];
  7015. [super dealloc];
  7016. }
  7017. - (instancetype)copyWithZone:(NSZone *)zone {
  7018. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7019. }
  7020. - (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
  7021. if (self == other) {
  7022. return YES;
  7023. }
  7024. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7025. return NO;
  7026. }
  7027. return [_dictionary isEqual:other->_dictionary];
  7028. }
  7029. - (NSUInteger)hash {
  7030. return _dictionary.count;
  7031. }
  7032. - (NSString *)description {
  7033. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7034. }
  7035. - (NSUInteger)count {
  7036. return _dictionary.count;
  7037. }
  7038. - (void)enumerateKeysAndValuesUsingBlock:
  7039. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7040. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7041. NSNumber *aValue,
  7042. BOOL *stop) {
  7043. block([aKey longLongValue], [aValue longLongValue], stop);
  7044. }];
  7045. }
  7046. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7047. NSUInteger count = _dictionary.count;
  7048. if (count == 0) {
  7049. return 0;
  7050. }
  7051. GPBType valueType = GPBGetFieldType(field);
  7052. GPBType keyType = field.mapKeyType;
  7053. __block size_t result = 0;
  7054. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7055. NSNumber *aValue,
  7056. BOOL *stop) {
  7057. #pragma unused(stop)
  7058. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7059. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  7060. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7061. }];
  7062. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7063. result += tagSize * count;
  7064. return result;
  7065. }
  7066. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7067. asField:(GPBFieldDescriptor *)field {
  7068. GPBType valueType = GPBGetFieldType(field);
  7069. GPBType keyType = field.mapKeyType;
  7070. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7071. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7072. NSNumber *aValue,
  7073. BOOL *stop) {
  7074. #pragma unused(stop)
  7075. // Write the tag.
  7076. [outputStream writeInt32NoTag:tag];
  7077. // Write the size of the message.
  7078. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7079. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  7080. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7081. // Write the fields.
  7082. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7083. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  7084. }];
  7085. }
  7086. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7087. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7088. }
  7089. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7090. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7091. #pragma unused(stop)
  7092. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7093. }];
  7094. }
  7095. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  7096. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7097. if (wrapped && value) {
  7098. *value = [wrapped longLongValue];
  7099. }
  7100. return (wrapped != NULL);
  7101. }
  7102. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7103. if (otherDictionary) {
  7104. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7105. if (_autocreator) {
  7106. GPBAutocreatedDictionaryModified(_autocreator, self);
  7107. }
  7108. }
  7109. }
  7110. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  7111. [_dictionary setObject:@(value) forKey:@(key)];
  7112. if (_autocreator) {
  7113. GPBAutocreatedDictionaryModified(_autocreator, self);
  7114. }
  7115. }
  7116. - (void)removeValueForKey:(int64_t)aKey {
  7117. [_dictionary removeObjectForKey:@(aKey)];
  7118. }
  7119. - (void)removeAll {
  7120. [_dictionary removeAllObjects];
  7121. }
  7122. @end
  7123. #pragma mark - Int64 -> Bool
  7124. @implementation GPBInt64BoolDictionary {
  7125. @package
  7126. NSMutableDictionary *_dictionary;
  7127. }
  7128. + (instancetype)dictionary {
  7129. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7130. }
  7131. + (instancetype)dictionaryWithValue:(BOOL)value
  7132. forKey:(int64_t)key {
  7133. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7134. // on to get the type correct.
  7135. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  7136. forKeys:&key
  7137. count:1] autorelease];
  7138. }
  7139. + (instancetype)dictionaryWithValues:(const BOOL [])values
  7140. forKeys:(const int64_t [])keys
  7141. count:(NSUInteger)count {
  7142. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7143. // on to get the type correct.
  7144. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  7145. forKeys:keys
  7146. count:count] autorelease];
  7147. }
  7148. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7149. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7150. // on to get the type correct.
  7151. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7152. }
  7153. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7154. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7155. }
  7156. - (instancetype)init {
  7157. return [self initWithValues:NULL forKeys:NULL count:0];
  7158. }
  7159. - (instancetype)initWithValues:(const BOOL [])values
  7160. forKeys:(const int64_t [])keys
  7161. count:(NSUInteger)count {
  7162. self = [super init];
  7163. if (self) {
  7164. _dictionary = [[NSMutableDictionary alloc] init];
  7165. if (count && values && keys) {
  7166. for (NSUInteger i = 0; i < count; ++i) {
  7167. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7168. }
  7169. }
  7170. }
  7171. return self;
  7172. }
  7173. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7174. self = [self initWithValues:NULL forKeys:NULL count:0];
  7175. if (self) {
  7176. if (dictionary) {
  7177. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7178. }
  7179. }
  7180. return self;
  7181. }
  7182. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7183. #pragma unused(numItems)
  7184. return [self initWithValues:NULL forKeys:NULL count:0];
  7185. }
  7186. - (void)dealloc {
  7187. NSAssert(!_autocreator,
  7188. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7189. [self class], _autocreator);
  7190. [_dictionary release];
  7191. [super dealloc];
  7192. }
  7193. - (instancetype)copyWithZone:(NSZone *)zone {
  7194. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7195. }
  7196. - (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
  7197. if (self == other) {
  7198. return YES;
  7199. }
  7200. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7201. return NO;
  7202. }
  7203. return [_dictionary isEqual:other->_dictionary];
  7204. }
  7205. - (NSUInteger)hash {
  7206. return _dictionary.count;
  7207. }
  7208. - (NSString *)description {
  7209. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7210. }
  7211. - (NSUInteger)count {
  7212. return _dictionary.count;
  7213. }
  7214. - (void)enumerateKeysAndValuesUsingBlock:
  7215. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7216. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7217. NSNumber *aValue,
  7218. BOOL *stop) {
  7219. block([aKey longLongValue], [aValue boolValue], stop);
  7220. }];
  7221. }
  7222. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7223. NSUInteger count = _dictionary.count;
  7224. if (count == 0) {
  7225. return 0;
  7226. }
  7227. GPBType valueType = GPBGetFieldType(field);
  7228. GPBType keyType = field.mapKeyType;
  7229. __block size_t result = 0;
  7230. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7231. NSNumber *aValue,
  7232. BOOL *stop) {
  7233. #pragma unused(stop)
  7234. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7235. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  7236. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7237. }];
  7238. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7239. result += tagSize * count;
  7240. return result;
  7241. }
  7242. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7243. asField:(GPBFieldDescriptor *)field {
  7244. GPBType valueType = GPBGetFieldType(field);
  7245. GPBType keyType = field.mapKeyType;
  7246. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7247. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7248. NSNumber *aValue,
  7249. BOOL *stop) {
  7250. #pragma unused(stop)
  7251. // Write the tag.
  7252. [outputStream writeInt32NoTag:tag];
  7253. // Write the size of the message.
  7254. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7255. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  7256. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7257. // Write the fields.
  7258. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7259. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  7260. }];
  7261. }
  7262. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7263. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7264. }
  7265. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7266. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7267. #pragma unused(stop)
  7268. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7269. }];
  7270. }
  7271. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  7272. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7273. if (wrapped && value) {
  7274. *value = [wrapped boolValue];
  7275. }
  7276. return (wrapped != NULL);
  7277. }
  7278. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7279. if (otherDictionary) {
  7280. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7281. if (_autocreator) {
  7282. GPBAutocreatedDictionaryModified(_autocreator, self);
  7283. }
  7284. }
  7285. }
  7286. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  7287. [_dictionary setObject:@(value) forKey:@(key)];
  7288. if (_autocreator) {
  7289. GPBAutocreatedDictionaryModified(_autocreator, self);
  7290. }
  7291. }
  7292. - (void)removeValueForKey:(int64_t)aKey {
  7293. [_dictionary removeObjectForKey:@(aKey)];
  7294. }
  7295. - (void)removeAll {
  7296. [_dictionary removeAllObjects];
  7297. }
  7298. @end
  7299. #pragma mark - Int64 -> Float
  7300. @implementation GPBInt64FloatDictionary {
  7301. @package
  7302. NSMutableDictionary *_dictionary;
  7303. }
  7304. + (instancetype)dictionary {
  7305. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7306. }
  7307. + (instancetype)dictionaryWithValue:(float)value
  7308. forKey:(int64_t)key {
  7309. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7310. // on to get the type correct.
  7311. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  7312. forKeys:&key
  7313. count:1] autorelease];
  7314. }
  7315. + (instancetype)dictionaryWithValues:(const float [])values
  7316. forKeys:(const int64_t [])keys
  7317. count:(NSUInteger)count {
  7318. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7319. // on to get the type correct.
  7320. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  7321. forKeys:keys
  7322. count:count] autorelease];
  7323. }
  7324. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7325. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7326. // on to get the type correct.
  7327. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7328. }
  7329. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7330. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7331. }
  7332. - (instancetype)init {
  7333. return [self initWithValues:NULL forKeys:NULL count:0];
  7334. }
  7335. - (instancetype)initWithValues:(const float [])values
  7336. forKeys:(const int64_t [])keys
  7337. count:(NSUInteger)count {
  7338. self = [super init];
  7339. if (self) {
  7340. _dictionary = [[NSMutableDictionary alloc] init];
  7341. if (count && values && keys) {
  7342. for (NSUInteger i = 0; i < count; ++i) {
  7343. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7344. }
  7345. }
  7346. }
  7347. return self;
  7348. }
  7349. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7350. self = [self initWithValues:NULL forKeys:NULL count:0];
  7351. if (self) {
  7352. if (dictionary) {
  7353. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7354. }
  7355. }
  7356. return self;
  7357. }
  7358. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7359. #pragma unused(numItems)
  7360. return [self initWithValues:NULL forKeys:NULL count:0];
  7361. }
  7362. - (void)dealloc {
  7363. NSAssert(!_autocreator,
  7364. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7365. [self class], _autocreator);
  7366. [_dictionary release];
  7367. [super dealloc];
  7368. }
  7369. - (instancetype)copyWithZone:(NSZone *)zone {
  7370. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7371. }
  7372. - (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
  7373. if (self == other) {
  7374. return YES;
  7375. }
  7376. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7377. return NO;
  7378. }
  7379. return [_dictionary isEqual:other->_dictionary];
  7380. }
  7381. - (NSUInteger)hash {
  7382. return _dictionary.count;
  7383. }
  7384. - (NSString *)description {
  7385. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7386. }
  7387. - (NSUInteger)count {
  7388. return _dictionary.count;
  7389. }
  7390. - (void)enumerateKeysAndValuesUsingBlock:
  7391. (void (^)(int64_t key, float value, BOOL *stop))block {
  7392. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7393. NSNumber *aValue,
  7394. BOOL *stop) {
  7395. block([aKey longLongValue], [aValue floatValue], stop);
  7396. }];
  7397. }
  7398. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7399. NSUInteger count = _dictionary.count;
  7400. if (count == 0) {
  7401. return 0;
  7402. }
  7403. GPBType valueType = GPBGetFieldType(field);
  7404. GPBType keyType = field.mapKeyType;
  7405. __block size_t result = 0;
  7406. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7407. NSNumber *aValue,
  7408. BOOL *stop) {
  7409. #pragma unused(stop)
  7410. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7411. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  7412. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7413. }];
  7414. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7415. result += tagSize * count;
  7416. return result;
  7417. }
  7418. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7419. asField:(GPBFieldDescriptor *)field {
  7420. GPBType valueType = GPBGetFieldType(field);
  7421. GPBType keyType = field.mapKeyType;
  7422. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7423. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7424. NSNumber *aValue,
  7425. BOOL *stop) {
  7426. #pragma unused(stop)
  7427. // Write the tag.
  7428. [outputStream writeInt32NoTag:tag];
  7429. // Write the size of the message.
  7430. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7431. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  7432. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7433. // Write the fields.
  7434. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7435. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  7436. }];
  7437. }
  7438. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7439. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7440. }
  7441. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7442. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7443. #pragma unused(stop)
  7444. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7445. }];
  7446. }
  7447. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7448. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7449. if (wrapped && value) {
  7450. *value = [wrapped floatValue];
  7451. }
  7452. return (wrapped != NULL);
  7453. }
  7454. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7455. if (otherDictionary) {
  7456. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7457. if (_autocreator) {
  7458. GPBAutocreatedDictionaryModified(_autocreator, self);
  7459. }
  7460. }
  7461. }
  7462. - (void)setValue:(float)value forKey:(int64_t)key {
  7463. [_dictionary setObject:@(value) forKey:@(key)];
  7464. if (_autocreator) {
  7465. GPBAutocreatedDictionaryModified(_autocreator, self);
  7466. }
  7467. }
  7468. - (void)removeValueForKey:(int64_t)aKey {
  7469. [_dictionary removeObjectForKey:@(aKey)];
  7470. }
  7471. - (void)removeAll {
  7472. [_dictionary removeAllObjects];
  7473. }
  7474. @end
  7475. #pragma mark - Int64 -> Double
  7476. @implementation GPBInt64DoubleDictionary {
  7477. @package
  7478. NSMutableDictionary *_dictionary;
  7479. }
  7480. + (instancetype)dictionary {
  7481. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7482. }
  7483. + (instancetype)dictionaryWithValue:(double)value
  7484. forKey:(int64_t)key {
  7485. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7486. // on to get the type correct.
  7487. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7488. forKeys:&key
  7489. count:1] autorelease];
  7490. }
  7491. + (instancetype)dictionaryWithValues:(const double [])values
  7492. forKeys:(const int64_t [])keys
  7493. count:(NSUInteger)count {
  7494. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7495. // on to get the type correct.
  7496. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7497. forKeys:keys
  7498. count:count] autorelease];
  7499. }
  7500. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7501. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7502. // on to get the type correct.
  7503. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7504. }
  7505. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7506. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7507. }
  7508. - (instancetype)init {
  7509. return [self initWithValues:NULL forKeys:NULL count:0];
  7510. }
  7511. - (instancetype)initWithValues:(const double [])values
  7512. forKeys:(const int64_t [])keys
  7513. count:(NSUInteger)count {
  7514. self = [super init];
  7515. if (self) {
  7516. _dictionary = [[NSMutableDictionary alloc] init];
  7517. if (count && values && keys) {
  7518. for (NSUInteger i = 0; i < count; ++i) {
  7519. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7520. }
  7521. }
  7522. }
  7523. return self;
  7524. }
  7525. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7526. self = [self initWithValues:NULL forKeys:NULL count:0];
  7527. if (self) {
  7528. if (dictionary) {
  7529. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7530. }
  7531. }
  7532. return self;
  7533. }
  7534. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7535. #pragma unused(numItems)
  7536. return [self initWithValues:NULL forKeys:NULL count:0];
  7537. }
  7538. - (void)dealloc {
  7539. NSAssert(!_autocreator,
  7540. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7541. [self class], _autocreator);
  7542. [_dictionary release];
  7543. [super dealloc];
  7544. }
  7545. - (instancetype)copyWithZone:(NSZone *)zone {
  7546. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7547. }
  7548. - (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
  7549. if (self == other) {
  7550. return YES;
  7551. }
  7552. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7553. return NO;
  7554. }
  7555. return [_dictionary isEqual:other->_dictionary];
  7556. }
  7557. - (NSUInteger)hash {
  7558. return _dictionary.count;
  7559. }
  7560. - (NSString *)description {
  7561. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7562. }
  7563. - (NSUInteger)count {
  7564. return _dictionary.count;
  7565. }
  7566. - (void)enumerateKeysAndValuesUsingBlock:
  7567. (void (^)(int64_t key, double value, BOOL *stop))block {
  7568. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7569. NSNumber *aValue,
  7570. BOOL *stop) {
  7571. block([aKey longLongValue], [aValue doubleValue], stop);
  7572. }];
  7573. }
  7574. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7575. NSUInteger count = _dictionary.count;
  7576. if (count == 0) {
  7577. return 0;
  7578. }
  7579. GPBType valueType = GPBGetFieldType(field);
  7580. GPBType keyType = field.mapKeyType;
  7581. __block size_t result = 0;
  7582. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7583. NSNumber *aValue,
  7584. BOOL *stop) {
  7585. #pragma unused(stop)
  7586. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7587. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  7588. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7589. }];
  7590. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7591. result += tagSize * count;
  7592. return result;
  7593. }
  7594. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7595. asField:(GPBFieldDescriptor *)field {
  7596. GPBType valueType = GPBGetFieldType(field);
  7597. GPBType keyType = field.mapKeyType;
  7598. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7599. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7600. NSNumber *aValue,
  7601. BOOL *stop) {
  7602. #pragma unused(stop)
  7603. // Write the tag.
  7604. [outputStream writeInt32NoTag:tag];
  7605. // Write the size of the message.
  7606. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7607. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  7608. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7609. // Write the fields.
  7610. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7611. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  7612. }];
  7613. }
  7614. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7615. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7616. }
  7617. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7618. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7619. #pragma unused(stop)
  7620. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7621. }];
  7622. }
  7623. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7624. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7625. if (wrapped && value) {
  7626. *value = [wrapped doubleValue];
  7627. }
  7628. return (wrapped != NULL);
  7629. }
  7630. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7631. if (otherDictionary) {
  7632. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7633. if (_autocreator) {
  7634. GPBAutocreatedDictionaryModified(_autocreator, self);
  7635. }
  7636. }
  7637. }
  7638. - (void)setValue:(double)value forKey:(int64_t)key {
  7639. [_dictionary setObject:@(value) forKey:@(key)];
  7640. if (_autocreator) {
  7641. GPBAutocreatedDictionaryModified(_autocreator, self);
  7642. }
  7643. }
  7644. - (void)removeValueForKey:(int64_t)aKey {
  7645. [_dictionary removeObjectForKey:@(aKey)];
  7646. }
  7647. - (void)removeAll {
  7648. [_dictionary removeAllObjects];
  7649. }
  7650. @end
  7651. #pragma mark - Int64 -> Enum
  7652. @implementation GPBInt64EnumDictionary {
  7653. @package
  7654. NSMutableDictionary *_dictionary;
  7655. GPBEnumValidationFunc _validationFunc;
  7656. }
  7657. @synthesize validationFunc = _validationFunc;
  7658. + (instancetype)dictionary {
  7659. return [[[self alloc] initWithValidationFunction:NULL
  7660. rawValues:NULL
  7661. forKeys:NULL
  7662. count:0] autorelease];
  7663. }
  7664. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7665. return [[[self alloc] initWithValidationFunction:func
  7666. rawValues:NULL
  7667. forKeys:NULL
  7668. count:0] autorelease];
  7669. }
  7670. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7671. rawValue:(int32_t)rawValue
  7672. forKey:(int64_t)key {
  7673. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7674. // on to get the type correct.
  7675. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7676. rawValues:&rawValue
  7677. forKeys:&key
  7678. count:1] autorelease];
  7679. }
  7680. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7681. rawValues:(const int32_t [])rawValues
  7682. forKeys:(const int64_t [])keys
  7683. count:(NSUInteger)count {
  7684. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7685. // on to get the type correct.
  7686. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7687. rawValues:rawValues
  7688. forKeys:keys
  7689. count:count] autorelease];
  7690. }
  7691. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7692. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7693. // on to get the type correct.
  7694. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7695. }
  7696. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7697. capacity:(NSUInteger)numItems {
  7698. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7699. }
  7700. - (instancetype)init {
  7701. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7702. }
  7703. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7704. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7705. }
  7706. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7707. rawValues:(const int32_t [])rawValues
  7708. forKeys:(const int64_t [])keys
  7709. count:(NSUInteger)count {
  7710. self = [super init];
  7711. if (self) {
  7712. _dictionary = [[NSMutableDictionary alloc] init];
  7713. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7714. if (count && rawValues && keys) {
  7715. for (NSUInteger i = 0; i < count; ++i) {
  7716. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7717. }
  7718. }
  7719. }
  7720. return self;
  7721. }
  7722. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7723. self = [self initWithValidationFunction:dictionary.validationFunc
  7724. rawValues:NULL
  7725. forKeys:NULL
  7726. count:0];
  7727. if (self) {
  7728. if (dictionary) {
  7729. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7730. }
  7731. }
  7732. return self;
  7733. }
  7734. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7735. capacity:(NSUInteger)numItems {
  7736. #pragma unused(numItems)
  7737. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7738. }
  7739. - (void)dealloc {
  7740. NSAssert(!_autocreator,
  7741. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7742. [self class], _autocreator);
  7743. [_dictionary release];
  7744. [super dealloc];
  7745. }
  7746. - (instancetype)copyWithZone:(NSZone *)zone {
  7747. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7748. }
  7749. - (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
  7750. if (self == other) {
  7751. return YES;
  7752. }
  7753. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7754. return NO;
  7755. }
  7756. return [_dictionary isEqual:other->_dictionary];
  7757. }
  7758. - (NSUInteger)hash {
  7759. return _dictionary.count;
  7760. }
  7761. - (NSString *)description {
  7762. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7763. }
  7764. - (NSUInteger)count {
  7765. return _dictionary.count;
  7766. }
  7767. - (void)enumerateKeysAndRawValuesUsingBlock:
  7768. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7769. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7770. NSNumber *aValue,
  7771. BOOL *stop) {
  7772. block([aKey longLongValue], [aValue intValue], stop);
  7773. }];
  7774. }
  7775. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7776. NSUInteger count = _dictionary.count;
  7777. if (count == 0) {
  7778. return 0;
  7779. }
  7780. GPBType valueType = GPBGetFieldType(field);
  7781. GPBType keyType = field.mapKeyType;
  7782. __block size_t result = 0;
  7783. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7784. NSNumber *aValue,
  7785. BOOL *stop) {
  7786. #pragma unused(stop)
  7787. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7788. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  7789. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7790. }];
  7791. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  7792. result += tagSize * count;
  7793. return result;
  7794. }
  7795. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7796. asField:(GPBFieldDescriptor *)field {
  7797. GPBType valueType = GPBGetFieldType(field);
  7798. GPBType keyType = field.mapKeyType;
  7799. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7800. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7801. NSNumber *aValue,
  7802. BOOL *stop) {
  7803. #pragma unused(stop)
  7804. // Write the tag.
  7805. [outputStream writeInt32NoTag:tag];
  7806. // Write the size of the message.
  7807. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  7808. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  7809. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7810. // Write the fields.
  7811. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  7812. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  7813. }];
  7814. }
  7815. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7816. forKey:(GPBValue *)key
  7817. keyType:(GPBType)keyType {
  7818. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyType);
  7819. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  7820. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7821. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7822. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyType);
  7823. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  7824. [outputStream release];
  7825. return data;
  7826. }
  7827. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  7828. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7829. }
  7830. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7831. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7832. #pragma unused(stop)
  7833. block([NSString stringWithFormat:@"%lld", key], @(value));
  7834. }];
  7835. }
  7836. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7837. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7838. if (wrapped && value) {
  7839. int32_t result = [wrapped intValue];
  7840. if (!_validationFunc(result)) {
  7841. result = kGPBUnrecognizedEnumeratorValue;
  7842. }
  7843. *value = result;
  7844. }
  7845. return (wrapped != NULL);
  7846. }
  7847. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  7848. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7849. if (wrapped && rawValue) {
  7850. *rawValue = [wrapped intValue];
  7851. }
  7852. return (wrapped != NULL);
  7853. }
  7854. - (void)enumerateKeysAndValuesUsingBlock:
  7855. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7856. GPBEnumValidationFunc func = _validationFunc;
  7857. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7858. NSNumber *aValue,
  7859. BOOL *stop) {
  7860. int32_t unwrapped = [aValue intValue];
  7861. if (!func(unwrapped)) {
  7862. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7863. }
  7864. block([aKey longLongValue], unwrapped, stop);
  7865. }];
  7866. }
  7867. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7868. if (otherDictionary) {
  7869. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7870. if (_autocreator) {
  7871. GPBAutocreatedDictionaryModified(_autocreator, self);
  7872. }
  7873. }
  7874. }
  7875. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7876. [_dictionary setObject:@(value) forKey:@(key)];
  7877. if (_autocreator) {
  7878. GPBAutocreatedDictionaryModified(_autocreator, self);
  7879. }
  7880. }
  7881. - (void)removeValueForKey:(int64_t)aKey {
  7882. [_dictionary removeObjectForKey:@(aKey)];
  7883. }
  7884. - (void)removeAll {
  7885. [_dictionary removeAllObjects];
  7886. }
  7887. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  7888. if (!_validationFunc(value)) {
  7889. [NSException raise:NSInvalidArgumentException
  7890. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7891. value];
  7892. }
  7893. [_dictionary setObject:@(value) forKey:@(key)];
  7894. if (_autocreator) {
  7895. GPBAutocreatedDictionaryModified(_autocreator, self);
  7896. }
  7897. }
  7898. @end
  7899. #pragma mark - Int64 -> Object
  7900. @implementation GPBInt64ObjectDictionary {
  7901. @package
  7902. NSMutableDictionary *_dictionary;
  7903. }
  7904. + (instancetype)dictionary {
  7905. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7906. }
  7907. + (instancetype)dictionaryWithValue:(id)value
  7908. forKey:(int64_t)key {
  7909. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7910. // on to get the type correct.
  7911. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithValues:&value
  7912. forKeys:&key
  7913. count:1] autorelease];
  7914. }
  7915. + (instancetype)dictionaryWithValues:(const id [])values
  7916. forKeys:(const int64_t [])keys
  7917. count:(NSUInteger)count {
  7918. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7919. // on to get the type correct.
  7920. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithValues:values
  7921. forKeys:keys
  7922. count:count] autorelease];
  7923. }
  7924. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7925. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7926. // on to get the type correct.
  7927. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7928. }
  7929. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7930. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7931. }
  7932. - (instancetype)init {
  7933. return [self initWithValues:NULL forKeys:NULL count:0];
  7934. }
  7935. - (instancetype)initWithValues:(const id [])values
  7936. forKeys:(const int64_t [])keys
  7937. count:(NSUInteger)count {
  7938. self = [super init];
  7939. if (self) {
  7940. _dictionary = [[NSMutableDictionary alloc] init];
  7941. if (count && values && keys) {
  7942. for (NSUInteger i = 0; i < count; ++i) {
  7943. [_dictionary setObject:values[i] forKey:@(keys[i])];
  7944. }
  7945. }
  7946. }
  7947. return self;
  7948. }
  7949. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7950. self = [self initWithValues:NULL forKeys:NULL count:0];
  7951. if (self) {
  7952. if (dictionary) {
  7953. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7954. }
  7955. }
  7956. return self;
  7957. }
  7958. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7959. #pragma unused(numItems)
  7960. return [self initWithValues:NULL forKeys:NULL count:0];
  7961. }
  7962. - (void)dealloc {
  7963. NSAssert(!_autocreator,
  7964. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7965. [self class], _autocreator);
  7966. [_dictionary release];
  7967. [super dealloc];
  7968. }
  7969. - (instancetype)copyWithZone:(NSZone *)zone {
  7970. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  7971. }
  7972. - (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
  7973. if (self == other) {
  7974. return YES;
  7975. }
  7976. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  7977. return NO;
  7978. }
  7979. return [_dictionary isEqual:other->_dictionary];
  7980. }
  7981. - (NSUInteger)hash {
  7982. return _dictionary.count;
  7983. }
  7984. - (NSString *)description {
  7985. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7986. }
  7987. - (NSUInteger)count {
  7988. return _dictionary.count;
  7989. }
  7990. - (void)enumerateKeysAndValuesUsingBlock:
  7991. (void (^)(int64_t key, id value, BOOL *stop))block {
  7992. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7993. id aValue,
  7994. BOOL *stop) {
  7995. block([aKey longLongValue], aValue, stop);
  7996. }];
  7997. }
  7998. - (BOOL)isInitialized {
  7999. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8000. if (!msg.initialized) {
  8001. return NO;
  8002. }
  8003. }
  8004. return YES;
  8005. }
  8006. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8007. GPBInt64ObjectDictionary *newDict =
  8008. [[GPBInt64ObjectDictionary alloc] init];
  8009. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8010. GPBMessage *msg,
  8011. BOOL *stop) {
  8012. #pragma unused(stop)
  8013. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8014. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8015. [copiedMsg release];
  8016. }];
  8017. return newDict;
  8018. }
  8019. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8020. NSUInteger count = _dictionary.count;
  8021. if (count == 0) {
  8022. return 0;
  8023. }
  8024. GPBType valueType = GPBGetFieldType(field);
  8025. GPBType keyType = field.mapKeyType;
  8026. __block size_t result = 0;
  8027. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8028. id aValue,
  8029. BOOL *stop) {
  8030. #pragma unused(stop)
  8031. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  8032. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  8033. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8034. }];
  8035. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8036. result += tagSize * count;
  8037. return result;
  8038. }
  8039. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8040. asField:(GPBFieldDescriptor *)field {
  8041. GPBType valueType = GPBGetFieldType(field);
  8042. GPBType keyType = field.mapKeyType;
  8043. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8044. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8045. id aValue,
  8046. BOOL *stop) {
  8047. #pragma unused(stop)
  8048. // Write the tag.
  8049. [outputStream writeInt32NoTag:tag];
  8050. // Write the size of the message.
  8051. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyType);
  8052. msgSize += ComputeDictObjectFieldSize(aValue, kMapValueFieldNumber, valueType);
  8053. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8054. // Write the fields.
  8055. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyType);
  8056. WriteDictObjectField(outputStream, aValue, kMapValueFieldNumber, valueType);
  8057. }];
  8058. }
  8059. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8060. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8061. }
  8062. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8063. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, id value, BOOL *stop) {
  8064. #pragma unused(stop)
  8065. block([NSString stringWithFormat:@"%lld", key], value);
  8066. }];
  8067. }
  8068. - (id)valueForKey:(int64_t)key {
  8069. id result = [_dictionary objectForKey:@(key)];
  8070. return result;
  8071. }
  8072. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8073. if (otherDictionary) {
  8074. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8075. if (_autocreator) {
  8076. GPBAutocreatedDictionaryModified(_autocreator, self);
  8077. }
  8078. }
  8079. }
  8080. - (void)setValue:(id)value forKey:(int64_t)key {
  8081. [_dictionary setObject:value forKey:@(key)];
  8082. if (_autocreator) {
  8083. GPBAutocreatedDictionaryModified(_autocreator, self);
  8084. }
  8085. }
  8086. - (void)removeValueForKey:(int64_t)aKey {
  8087. [_dictionary removeObjectForKey:@(aKey)];
  8088. }
  8089. - (void)removeAll {
  8090. [_dictionary removeAllObjects];
  8091. }
  8092. @end
  8093. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8094. // This block of code is generated, do not edit it directly.
  8095. #pragma mark - String -> UInt32
  8096. @implementation GPBStringUInt32Dictionary {
  8097. @package
  8098. NSMutableDictionary *_dictionary;
  8099. }
  8100. + (instancetype)dictionary {
  8101. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8102. }
  8103. + (instancetype)dictionaryWithValue:(uint32_t)value
  8104. forKey:(NSString *)key {
  8105. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8106. // on to get the type correct.
  8107. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  8108. forKeys:&key
  8109. count:1] autorelease];
  8110. }
  8111. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  8112. forKeys:(const NSString * [])keys
  8113. count:(NSUInteger)count {
  8114. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8115. // on to get the type correct.
  8116. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  8117. forKeys:keys
  8118. count:count] autorelease];
  8119. }
  8120. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8121. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8122. // on to get the type correct.
  8123. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8124. }
  8125. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8126. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8127. }
  8128. - (instancetype)init {
  8129. return [self initWithValues:NULL forKeys:NULL count:0];
  8130. }
  8131. - (instancetype)initWithValues:(const uint32_t [])values
  8132. forKeys:(const NSString * [])keys
  8133. count:(NSUInteger)count {
  8134. self = [super init];
  8135. if (self) {
  8136. _dictionary = [[NSMutableDictionary alloc] init];
  8137. if (count && values && keys) {
  8138. for (NSUInteger i = 0; i < count; ++i) {
  8139. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8140. }
  8141. }
  8142. }
  8143. return self;
  8144. }
  8145. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8146. self = [self initWithValues:NULL forKeys:NULL count:0];
  8147. if (self) {
  8148. if (dictionary) {
  8149. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8150. }
  8151. }
  8152. return self;
  8153. }
  8154. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8155. #pragma unused(numItems)
  8156. return [self initWithValues:NULL forKeys:NULL count:0];
  8157. }
  8158. - (void)dealloc {
  8159. NSAssert(!_autocreator,
  8160. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8161. [self class], _autocreator);
  8162. [_dictionary release];
  8163. [super dealloc];
  8164. }
  8165. - (instancetype)copyWithZone:(NSZone *)zone {
  8166. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8167. }
  8168. - (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
  8169. if (self == other) {
  8170. return YES;
  8171. }
  8172. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8173. return NO;
  8174. }
  8175. return [_dictionary isEqual:other->_dictionary];
  8176. }
  8177. - (NSUInteger)hash {
  8178. return _dictionary.count;
  8179. }
  8180. - (NSString *)description {
  8181. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8182. }
  8183. - (NSUInteger)count {
  8184. return _dictionary.count;
  8185. }
  8186. - (void)enumerateKeysAndValuesUsingBlock:
  8187. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8188. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8189. NSNumber *aValue,
  8190. BOOL *stop) {
  8191. block(aKey, [aValue unsignedIntValue], stop);
  8192. }];
  8193. }
  8194. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8195. NSUInteger count = _dictionary.count;
  8196. if (count == 0) {
  8197. return 0;
  8198. }
  8199. GPBType valueType = GPBGetFieldType(field);
  8200. GPBType keyType = field.mapKeyType;
  8201. __block size_t result = 0;
  8202. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8203. NSNumber *aValue,
  8204. BOOL *stop) {
  8205. #pragma unused(stop)
  8206. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8207. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  8208. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8209. }];
  8210. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8211. result += tagSize * count;
  8212. return result;
  8213. }
  8214. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8215. asField:(GPBFieldDescriptor *)field {
  8216. GPBType valueType = GPBGetFieldType(field);
  8217. GPBType keyType = field.mapKeyType;
  8218. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8219. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8220. NSNumber *aValue,
  8221. BOOL *stop) {
  8222. #pragma unused(stop)
  8223. // Write the tag.
  8224. [outputStream writeInt32NoTag:tag];
  8225. // Write the size of the message.
  8226. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8227. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  8228. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8229. // Write the fields.
  8230. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8231. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueType);
  8232. }];
  8233. }
  8234. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8235. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8236. }
  8237. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8238. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8239. #pragma unused(stop)
  8240. block(key, [NSString stringWithFormat:@"%u", value]);
  8241. }];
  8242. }
  8243. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  8244. NSNumber *wrapped = [_dictionary objectForKey:key];
  8245. if (wrapped && value) {
  8246. *value = [wrapped unsignedIntValue];
  8247. }
  8248. return (wrapped != NULL);
  8249. }
  8250. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8251. if (otherDictionary) {
  8252. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8253. if (_autocreator) {
  8254. GPBAutocreatedDictionaryModified(_autocreator, self);
  8255. }
  8256. }
  8257. }
  8258. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  8259. [_dictionary setObject:@(value) forKey:key];
  8260. if (_autocreator) {
  8261. GPBAutocreatedDictionaryModified(_autocreator, self);
  8262. }
  8263. }
  8264. - (void)removeValueForKey:(NSString *)aKey {
  8265. [_dictionary removeObjectForKey:aKey];
  8266. }
  8267. - (void)removeAll {
  8268. [_dictionary removeAllObjects];
  8269. }
  8270. @end
  8271. #pragma mark - String -> Int32
  8272. @implementation GPBStringInt32Dictionary {
  8273. @package
  8274. NSMutableDictionary *_dictionary;
  8275. }
  8276. + (instancetype)dictionary {
  8277. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8278. }
  8279. + (instancetype)dictionaryWithValue:(int32_t)value
  8280. forKey:(NSString *)key {
  8281. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8282. // on to get the type correct.
  8283. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  8284. forKeys:&key
  8285. count:1] autorelease];
  8286. }
  8287. + (instancetype)dictionaryWithValues:(const int32_t [])values
  8288. forKeys:(const NSString * [])keys
  8289. count:(NSUInteger)count {
  8290. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8291. // on to get the type correct.
  8292. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  8293. forKeys:keys
  8294. count:count] autorelease];
  8295. }
  8296. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8297. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8298. // on to get the type correct.
  8299. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8300. }
  8301. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8302. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8303. }
  8304. - (instancetype)init {
  8305. return [self initWithValues:NULL forKeys:NULL count:0];
  8306. }
  8307. - (instancetype)initWithValues:(const int32_t [])values
  8308. forKeys:(const NSString * [])keys
  8309. count:(NSUInteger)count {
  8310. self = [super init];
  8311. if (self) {
  8312. _dictionary = [[NSMutableDictionary alloc] init];
  8313. if (count && values && keys) {
  8314. for (NSUInteger i = 0; i < count; ++i) {
  8315. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8316. }
  8317. }
  8318. }
  8319. return self;
  8320. }
  8321. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8322. self = [self initWithValues:NULL forKeys:NULL count:0];
  8323. if (self) {
  8324. if (dictionary) {
  8325. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8326. }
  8327. }
  8328. return self;
  8329. }
  8330. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8331. #pragma unused(numItems)
  8332. return [self initWithValues:NULL forKeys:NULL count:0];
  8333. }
  8334. - (void)dealloc {
  8335. NSAssert(!_autocreator,
  8336. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8337. [self class], _autocreator);
  8338. [_dictionary release];
  8339. [super dealloc];
  8340. }
  8341. - (instancetype)copyWithZone:(NSZone *)zone {
  8342. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8343. }
  8344. - (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
  8345. if (self == other) {
  8346. return YES;
  8347. }
  8348. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8349. return NO;
  8350. }
  8351. return [_dictionary isEqual:other->_dictionary];
  8352. }
  8353. - (NSUInteger)hash {
  8354. return _dictionary.count;
  8355. }
  8356. - (NSString *)description {
  8357. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8358. }
  8359. - (NSUInteger)count {
  8360. return _dictionary.count;
  8361. }
  8362. - (void)enumerateKeysAndValuesUsingBlock:
  8363. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8364. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8365. NSNumber *aValue,
  8366. BOOL *stop) {
  8367. block(aKey, [aValue intValue], stop);
  8368. }];
  8369. }
  8370. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8371. NSUInteger count = _dictionary.count;
  8372. if (count == 0) {
  8373. return 0;
  8374. }
  8375. GPBType valueType = GPBGetFieldType(field);
  8376. GPBType keyType = field.mapKeyType;
  8377. __block size_t result = 0;
  8378. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8379. NSNumber *aValue,
  8380. BOOL *stop) {
  8381. #pragma unused(stop)
  8382. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8383. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  8384. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8385. }];
  8386. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8387. result += tagSize * count;
  8388. return result;
  8389. }
  8390. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8391. asField:(GPBFieldDescriptor *)field {
  8392. GPBType valueType = GPBGetFieldType(field);
  8393. GPBType keyType = field.mapKeyType;
  8394. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8395. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8396. NSNumber *aValue,
  8397. BOOL *stop) {
  8398. #pragma unused(stop)
  8399. // Write the tag.
  8400. [outputStream writeInt32NoTag:tag];
  8401. // Write the size of the message.
  8402. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8403. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  8404. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8405. // Write the fields.
  8406. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8407. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  8408. }];
  8409. }
  8410. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8411. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8412. }
  8413. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8414. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8415. #pragma unused(stop)
  8416. block(key, [NSString stringWithFormat:@"%d", value]);
  8417. }];
  8418. }
  8419. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8420. NSNumber *wrapped = [_dictionary objectForKey:key];
  8421. if (wrapped && value) {
  8422. *value = [wrapped intValue];
  8423. }
  8424. return (wrapped != NULL);
  8425. }
  8426. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8427. if (otherDictionary) {
  8428. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8429. if (_autocreator) {
  8430. GPBAutocreatedDictionaryModified(_autocreator, self);
  8431. }
  8432. }
  8433. }
  8434. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8435. [_dictionary setObject:@(value) forKey:key];
  8436. if (_autocreator) {
  8437. GPBAutocreatedDictionaryModified(_autocreator, self);
  8438. }
  8439. }
  8440. - (void)removeValueForKey:(NSString *)aKey {
  8441. [_dictionary removeObjectForKey:aKey];
  8442. }
  8443. - (void)removeAll {
  8444. [_dictionary removeAllObjects];
  8445. }
  8446. @end
  8447. #pragma mark - String -> UInt64
  8448. @implementation GPBStringUInt64Dictionary {
  8449. @package
  8450. NSMutableDictionary *_dictionary;
  8451. }
  8452. + (instancetype)dictionary {
  8453. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8454. }
  8455. + (instancetype)dictionaryWithValue:(uint64_t)value
  8456. forKey:(NSString *)key {
  8457. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8458. // on to get the type correct.
  8459. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8460. forKeys:&key
  8461. count:1] autorelease];
  8462. }
  8463. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8464. forKeys:(const NSString * [])keys
  8465. count:(NSUInteger)count {
  8466. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8467. // on to get the type correct.
  8468. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8469. forKeys:keys
  8470. count:count] autorelease];
  8471. }
  8472. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8473. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8474. // on to get the type correct.
  8475. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8476. }
  8477. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8478. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8479. }
  8480. - (instancetype)init {
  8481. return [self initWithValues:NULL forKeys:NULL count:0];
  8482. }
  8483. - (instancetype)initWithValues:(const uint64_t [])values
  8484. forKeys:(const NSString * [])keys
  8485. count:(NSUInteger)count {
  8486. self = [super init];
  8487. if (self) {
  8488. _dictionary = [[NSMutableDictionary alloc] init];
  8489. if (count && values && keys) {
  8490. for (NSUInteger i = 0; i < count; ++i) {
  8491. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8492. }
  8493. }
  8494. }
  8495. return self;
  8496. }
  8497. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8498. self = [self initWithValues:NULL forKeys:NULL count:0];
  8499. if (self) {
  8500. if (dictionary) {
  8501. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8502. }
  8503. }
  8504. return self;
  8505. }
  8506. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8507. #pragma unused(numItems)
  8508. return [self initWithValues:NULL forKeys:NULL count:0];
  8509. }
  8510. - (void)dealloc {
  8511. NSAssert(!_autocreator,
  8512. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8513. [self class], _autocreator);
  8514. [_dictionary release];
  8515. [super dealloc];
  8516. }
  8517. - (instancetype)copyWithZone:(NSZone *)zone {
  8518. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8519. }
  8520. - (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
  8521. if (self == other) {
  8522. return YES;
  8523. }
  8524. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8525. return NO;
  8526. }
  8527. return [_dictionary isEqual:other->_dictionary];
  8528. }
  8529. - (NSUInteger)hash {
  8530. return _dictionary.count;
  8531. }
  8532. - (NSString *)description {
  8533. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8534. }
  8535. - (NSUInteger)count {
  8536. return _dictionary.count;
  8537. }
  8538. - (void)enumerateKeysAndValuesUsingBlock:
  8539. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8540. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8541. NSNumber *aValue,
  8542. BOOL *stop) {
  8543. block(aKey, [aValue unsignedLongLongValue], stop);
  8544. }];
  8545. }
  8546. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8547. NSUInteger count = _dictionary.count;
  8548. if (count == 0) {
  8549. return 0;
  8550. }
  8551. GPBType valueType = GPBGetFieldType(field);
  8552. GPBType keyType = field.mapKeyType;
  8553. __block size_t result = 0;
  8554. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8555. NSNumber *aValue,
  8556. BOOL *stop) {
  8557. #pragma unused(stop)
  8558. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8559. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  8560. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8561. }];
  8562. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8563. result += tagSize * count;
  8564. return result;
  8565. }
  8566. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8567. asField:(GPBFieldDescriptor *)field {
  8568. GPBType valueType = GPBGetFieldType(field);
  8569. GPBType keyType = field.mapKeyType;
  8570. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8571. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8572. NSNumber *aValue,
  8573. BOOL *stop) {
  8574. #pragma unused(stop)
  8575. // Write the tag.
  8576. [outputStream writeInt32NoTag:tag];
  8577. // Write the size of the message.
  8578. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8579. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  8580. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8581. // Write the fields.
  8582. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8583. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueType);
  8584. }];
  8585. }
  8586. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8587. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8588. }
  8589. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8590. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8591. #pragma unused(stop)
  8592. block(key, [NSString stringWithFormat:@"%llu", value]);
  8593. }];
  8594. }
  8595. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8596. NSNumber *wrapped = [_dictionary objectForKey:key];
  8597. if (wrapped && value) {
  8598. *value = [wrapped unsignedLongLongValue];
  8599. }
  8600. return (wrapped != NULL);
  8601. }
  8602. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8603. if (otherDictionary) {
  8604. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8605. if (_autocreator) {
  8606. GPBAutocreatedDictionaryModified(_autocreator, self);
  8607. }
  8608. }
  8609. }
  8610. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8611. [_dictionary setObject:@(value) forKey:key];
  8612. if (_autocreator) {
  8613. GPBAutocreatedDictionaryModified(_autocreator, self);
  8614. }
  8615. }
  8616. - (void)removeValueForKey:(NSString *)aKey {
  8617. [_dictionary removeObjectForKey:aKey];
  8618. }
  8619. - (void)removeAll {
  8620. [_dictionary removeAllObjects];
  8621. }
  8622. @end
  8623. #pragma mark - String -> Int64
  8624. @implementation GPBStringInt64Dictionary {
  8625. @package
  8626. NSMutableDictionary *_dictionary;
  8627. }
  8628. + (instancetype)dictionary {
  8629. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8630. }
  8631. + (instancetype)dictionaryWithValue:(int64_t)value
  8632. forKey:(NSString *)key {
  8633. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8634. // on to get the type correct.
  8635. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8636. forKeys:&key
  8637. count:1] autorelease];
  8638. }
  8639. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8640. forKeys:(const NSString * [])keys
  8641. count:(NSUInteger)count {
  8642. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8643. // on to get the type correct.
  8644. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8645. forKeys:keys
  8646. count:count] autorelease];
  8647. }
  8648. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8649. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8650. // on to get the type correct.
  8651. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8652. }
  8653. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8654. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8655. }
  8656. - (instancetype)init {
  8657. return [self initWithValues:NULL forKeys:NULL count:0];
  8658. }
  8659. - (instancetype)initWithValues:(const int64_t [])values
  8660. forKeys:(const NSString * [])keys
  8661. count:(NSUInteger)count {
  8662. self = [super init];
  8663. if (self) {
  8664. _dictionary = [[NSMutableDictionary alloc] init];
  8665. if (count && values && keys) {
  8666. for (NSUInteger i = 0; i < count; ++i) {
  8667. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8668. }
  8669. }
  8670. }
  8671. return self;
  8672. }
  8673. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8674. self = [self initWithValues:NULL forKeys:NULL count:0];
  8675. if (self) {
  8676. if (dictionary) {
  8677. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8678. }
  8679. }
  8680. return self;
  8681. }
  8682. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8683. #pragma unused(numItems)
  8684. return [self initWithValues:NULL forKeys:NULL count:0];
  8685. }
  8686. - (void)dealloc {
  8687. NSAssert(!_autocreator,
  8688. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8689. [self class], _autocreator);
  8690. [_dictionary release];
  8691. [super dealloc];
  8692. }
  8693. - (instancetype)copyWithZone:(NSZone *)zone {
  8694. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8695. }
  8696. - (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
  8697. if (self == other) {
  8698. return YES;
  8699. }
  8700. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8701. return NO;
  8702. }
  8703. return [_dictionary isEqual:other->_dictionary];
  8704. }
  8705. - (NSUInteger)hash {
  8706. return _dictionary.count;
  8707. }
  8708. - (NSString *)description {
  8709. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8710. }
  8711. - (NSUInteger)count {
  8712. return _dictionary.count;
  8713. }
  8714. - (void)enumerateKeysAndValuesUsingBlock:
  8715. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8716. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8717. NSNumber *aValue,
  8718. BOOL *stop) {
  8719. block(aKey, [aValue longLongValue], stop);
  8720. }];
  8721. }
  8722. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8723. NSUInteger count = _dictionary.count;
  8724. if (count == 0) {
  8725. return 0;
  8726. }
  8727. GPBType valueType = GPBGetFieldType(field);
  8728. GPBType keyType = field.mapKeyType;
  8729. __block size_t result = 0;
  8730. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8731. NSNumber *aValue,
  8732. BOOL *stop) {
  8733. #pragma unused(stop)
  8734. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8735. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  8736. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8737. }];
  8738. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8739. result += tagSize * count;
  8740. return result;
  8741. }
  8742. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8743. asField:(GPBFieldDescriptor *)field {
  8744. GPBType valueType = GPBGetFieldType(field);
  8745. GPBType keyType = field.mapKeyType;
  8746. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8747. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8748. NSNumber *aValue,
  8749. BOOL *stop) {
  8750. #pragma unused(stop)
  8751. // Write the tag.
  8752. [outputStream writeInt32NoTag:tag];
  8753. // Write the size of the message.
  8754. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8755. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueType);
  8756. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8757. // Write the fields.
  8758. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8759. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueType);
  8760. }];
  8761. }
  8762. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8763. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8764. }
  8765. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8766. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8767. #pragma unused(stop)
  8768. block(key, [NSString stringWithFormat:@"%lld", value]);
  8769. }];
  8770. }
  8771. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8772. NSNumber *wrapped = [_dictionary objectForKey:key];
  8773. if (wrapped && value) {
  8774. *value = [wrapped longLongValue];
  8775. }
  8776. return (wrapped != NULL);
  8777. }
  8778. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8779. if (otherDictionary) {
  8780. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8781. if (_autocreator) {
  8782. GPBAutocreatedDictionaryModified(_autocreator, self);
  8783. }
  8784. }
  8785. }
  8786. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8787. [_dictionary setObject:@(value) forKey:key];
  8788. if (_autocreator) {
  8789. GPBAutocreatedDictionaryModified(_autocreator, self);
  8790. }
  8791. }
  8792. - (void)removeValueForKey:(NSString *)aKey {
  8793. [_dictionary removeObjectForKey:aKey];
  8794. }
  8795. - (void)removeAll {
  8796. [_dictionary removeAllObjects];
  8797. }
  8798. @end
  8799. #pragma mark - String -> Bool
  8800. @implementation GPBStringBoolDictionary {
  8801. @package
  8802. NSMutableDictionary *_dictionary;
  8803. }
  8804. + (instancetype)dictionary {
  8805. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8806. }
  8807. + (instancetype)dictionaryWithValue:(BOOL)value
  8808. forKey:(NSString *)key {
  8809. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8810. // on to get the type correct.
  8811. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  8812. forKeys:&key
  8813. count:1] autorelease];
  8814. }
  8815. + (instancetype)dictionaryWithValues:(const BOOL [])values
  8816. forKeys:(const NSString * [])keys
  8817. count:(NSUInteger)count {
  8818. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8819. // on to get the type correct.
  8820. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  8821. forKeys:keys
  8822. count:count] autorelease];
  8823. }
  8824. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8825. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8826. // on to get the type correct.
  8827. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8828. }
  8829. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8830. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8831. }
  8832. - (instancetype)init {
  8833. return [self initWithValues:NULL forKeys:NULL count:0];
  8834. }
  8835. - (instancetype)initWithValues:(const BOOL [])values
  8836. forKeys:(const NSString * [])keys
  8837. count:(NSUInteger)count {
  8838. self = [super init];
  8839. if (self) {
  8840. _dictionary = [[NSMutableDictionary alloc] init];
  8841. if (count && values && keys) {
  8842. for (NSUInteger i = 0; i < count; ++i) {
  8843. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8844. }
  8845. }
  8846. }
  8847. return self;
  8848. }
  8849. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8850. self = [self initWithValues:NULL forKeys:NULL count:0];
  8851. if (self) {
  8852. if (dictionary) {
  8853. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8854. }
  8855. }
  8856. return self;
  8857. }
  8858. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8859. #pragma unused(numItems)
  8860. return [self initWithValues:NULL forKeys:NULL count:0];
  8861. }
  8862. - (void)dealloc {
  8863. NSAssert(!_autocreator,
  8864. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8865. [self class], _autocreator);
  8866. [_dictionary release];
  8867. [super dealloc];
  8868. }
  8869. - (instancetype)copyWithZone:(NSZone *)zone {
  8870. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8871. }
  8872. - (BOOL)isEqual:(GPBStringBoolDictionary *)other {
  8873. if (self == other) {
  8874. return YES;
  8875. }
  8876. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8877. return NO;
  8878. }
  8879. return [_dictionary isEqual:other->_dictionary];
  8880. }
  8881. - (NSUInteger)hash {
  8882. return _dictionary.count;
  8883. }
  8884. - (NSString *)description {
  8885. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8886. }
  8887. - (NSUInteger)count {
  8888. return _dictionary.count;
  8889. }
  8890. - (void)enumerateKeysAndValuesUsingBlock:
  8891. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  8892. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8893. NSNumber *aValue,
  8894. BOOL *stop) {
  8895. block(aKey, [aValue boolValue], stop);
  8896. }];
  8897. }
  8898. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8899. NSUInteger count = _dictionary.count;
  8900. if (count == 0) {
  8901. return 0;
  8902. }
  8903. GPBType valueType = GPBGetFieldType(field);
  8904. GPBType keyType = field.mapKeyType;
  8905. __block size_t result = 0;
  8906. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8907. NSNumber *aValue,
  8908. BOOL *stop) {
  8909. #pragma unused(stop)
  8910. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8911. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  8912. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8913. }];
  8914. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  8915. result += tagSize * count;
  8916. return result;
  8917. }
  8918. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8919. asField:(GPBFieldDescriptor *)field {
  8920. GPBType valueType = GPBGetFieldType(field);
  8921. GPBType keyType = field.mapKeyType;
  8922. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8923. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8924. NSNumber *aValue,
  8925. BOOL *stop) {
  8926. #pragma unused(stop)
  8927. // Write the tag.
  8928. [outputStream writeInt32NoTag:tag];
  8929. // Write the size of the message.
  8930. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  8931. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueType);
  8932. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8933. // Write the fields.
  8934. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  8935. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueType);
  8936. }];
  8937. }
  8938. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  8939. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8940. }
  8941. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8942. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  8943. #pragma unused(stop)
  8944. block(key, (value ? @"true" : @"false"));
  8945. }];
  8946. }
  8947. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  8948. NSNumber *wrapped = [_dictionary objectForKey:key];
  8949. if (wrapped && value) {
  8950. *value = [wrapped boolValue];
  8951. }
  8952. return (wrapped != NULL);
  8953. }
  8954. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  8955. if (otherDictionary) {
  8956. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8957. if (_autocreator) {
  8958. GPBAutocreatedDictionaryModified(_autocreator, self);
  8959. }
  8960. }
  8961. }
  8962. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  8963. [_dictionary setObject:@(value) forKey:key];
  8964. if (_autocreator) {
  8965. GPBAutocreatedDictionaryModified(_autocreator, self);
  8966. }
  8967. }
  8968. - (void)removeValueForKey:(NSString *)aKey {
  8969. [_dictionary removeObjectForKey:aKey];
  8970. }
  8971. - (void)removeAll {
  8972. [_dictionary removeAllObjects];
  8973. }
  8974. @end
  8975. #pragma mark - String -> Float
  8976. @implementation GPBStringFloatDictionary {
  8977. @package
  8978. NSMutableDictionary *_dictionary;
  8979. }
  8980. + (instancetype)dictionary {
  8981. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8982. }
  8983. + (instancetype)dictionaryWithValue:(float)value
  8984. forKey:(NSString *)key {
  8985. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8986. // on to get the type correct.
  8987. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  8988. forKeys:&key
  8989. count:1] autorelease];
  8990. }
  8991. + (instancetype)dictionaryWithValues:(const float [])values
  8992. forKeys:(const NSString * [])keys
  8993. count:(NSUInteger)count {
  8994. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8995. // on to get the type correct.
  8996. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  8997. forKeys:keys
  8998. count:count] autorelease];
  8999. }
  9000. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9001. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9002. // on to get the type correct.
  9003. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9004. }
  9005. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9006. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9007. }
  9008. - (instancetype)init {
  9009. return [self initWithValues:NULL forKeys:NULL count:0];
  9010. }
  9011. - (instancetype)initWithValues:(const float [])values
  9012. forKeys:(const NSString * [])keys
  9013. count:(NSUInteger)count {
  9014. self = [super init];
  9015. if (self) {
  9016. _dictionary = [[NSMutableDictionary alloc] init];
  9017. if (count && values && keys) {
  9018. for (NSUInteger i = 0; i < count; ++i) {
  9019. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9020. }
  9021. }
  9022. }
  9023. return self;
  9024. }
  9025. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9026. self = [self initWithValues:NULL forKeys:NULL count:0];
  9027. if (self) {
  9028. if (dictionary) {
  9029. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9030. }
  9031. }
  9032. return self;
  9033. }
  9034. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9035. #pragma unused(numItems)
  9036. return [self initWithValues:NULL forKeys:NULL count:0];
  9037. }
  9038. - (void)dealloc {
  9039. NSAssert(!_autocreator,
  9040. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9041. [self class], _autocreator);
  9042. [_dictionary release];
  9043. [super dealloc];
  9044. }
  9045. - (instancetype)copyWithZone:(NSZone *)zone {
  9046. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9047. }
  9048. - (BOOL)isEqual:(GPBStringFloatDictionary *)other {
  9049. if (self == other) {
  9050. return YES;
  9051. }
  9052. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9053. return NO;
  9054. }
  9055. return [_dictionary isEqual:other->_dictionary];
  9056. }
  9057. - (NSUInteger)hash {
  9058. return _dictionary.count;
  9059. }
  9060. - (NSString *)description {
  9061. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9062. }
  9063. - (NSUInteger)count {
  9064. return _dictionary.count;
  9065. }
  9066. - (void)enumerateKeysAndValuesUsingBlock:
  9067. (void (^)(NSString *key, float value, BOOL *stop))block {
  9068. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9069. NSNumber *aValue,
  9070. BOOL *stop) {
  9071. block(aKey, [aValue floatValue], stop);
  9072. }];
  9073. }
  9074. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9075. NSUInteger count = _dictionary.count;
  9076. if (count == 0) {
  9077. return 0;
  9078. }
  9079. GPBType valueType = GPBGetFieldType(field);
  9080. GPBType keyType = field.mapKeyType;
  9081. __block size_t result = 0;
  9082. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9083. NSNumber *aValue,
  9084. BOOL *stop) {
  9085. #pragma unused(stop)
  9086. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9087. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  9088. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9089. }];
  9090. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9091. result += tagSize * count;
  9092. return result;
  9093. }
  9094. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9095. asField:(GPBFieldDescriptor *)field {
  9096. GPBType valueType = GPBGetFieldType(field);
  9097. GPBType keyType = field.mapKeyType;
  9098. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9099. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9100. NSNumber *aValue,
  9101. BOOL *stop) {
  9102. #pragma unused(stop)
  9103. // Write the tag.
  9104. [outputStream writeInt32NoTag:tag];
  9105. // Write the size of the message.
  9106. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9107. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueType);
  9108. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9109. // Write the fields.
  9110. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  9111. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueType);
  9112. }];
  9113. }
  9114. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9115. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9116. }
  9117. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9118. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9119. #pragma unused(stop)
  9120. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9121. }];
  9122. }
  9123. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  9124. NSNumber *wrapped = [_dictionary objectForKey:key];
  9125. if (wrapped && value) {
  9126. *value = [wrapped floatValue];
  9127. }
  9128. return (wrapped != NULL);
  9129. }
  9130. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9131. if (otherDictionary) {
  9132. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9133. if (_autocreator) {
  9134. GPBAutocreatedDictionaryModified(_autocreator, self);
  9135. }
  9136. }
  9137. }
  9138. - (void)setValue:(float)value forKey:(NSString *)key {
  9139. [_dictionary setObject:@(value) forKey:key];
  9140. if (_autocreator) {
  9141. GPBAutocreatedDictionaryModified(_autocreator, self);
  9142. }
  9143. }
  9144. - (void)removeValueForKey:(NSString *)aKey {
  9145. [_dictionary removeObjectForKey:aKey];
  9146. }
  9147. - (void)removeAll {
  9148. [_dictionary removeAllObjects];
  9149. }
  9150. @end
  9151. #pragma mark - String -> Double
  9152. @implementation GPBStringDoubleDictionary {
  9153. @package
  9154. NSMutableDictionary *_dictionary;
  9155. }
  9156. + (instancetype)dictionary {
  9157. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9158. }
  9159. + (instancetype)dictionaryWithValue:(double)value
  9160. forKey:(NSString *)key {
  9161. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9162. // on to get the type correct.
  9163. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  9164. forKeys:&key
  9165. count:1] autorelease];
  9166. }
  9167. + (instancetype)dictionaryWithValues:(const double [])values
  9168. forKeys:(const NSString * [])keys
  9169. count:(NSUInteger)count {
  9170. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9171. // on to get the type correct.
  9172. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  9173. forKeys:keys
  9174. count:count] autorelease];
  9175. }
  9176. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9177. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9178. // on to get the type correct.
  9179. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9180. }
  9181. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9182. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9183. }
  9184. - (instancetype)init {
  9185. return [self initWithValues:NULL forKeys:NULL count:0];
  9186. }
  9187. - (instancetype)initWithValues:(const double [])values
  9188. forKeys:(const NSString * [])keys
  9189. count:(NSUInteger)count {
  9190. self = [super init];
  9191. if (self) {
  9192. _dictionary = [[NSMutableDictionary alloc] init];
  9193. if (count && values && keys) {
  9194. for (NSUInteger i = 0; i < count; ++i) {
  9195. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9196. }
  9197. }
  9198. }
  9199. return self;
  9200. }
  9201. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9202. self = [self initWithValues:NULL forKeys:NULL count:0];
  9203. if (self) {
  9204. if (dictionary) {
  9205. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9206. }
  9207. }
  9208. return self;
  9209. }
  9210. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9211. #pragma unused(numItems)
  9212. return [self initWithValues:NULL forKeys:NULL count:0];
  9213. }
  9214. - (void)dealloc {
  9215. NSAssert(!_autocreator,
  9216. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9217. [self class], _autocreator);
  9218. [_dictionary release];
  9219. [super dealloc];
  9220. }
  9221. - (instancetype)copyWithZone:(NSZone *)zone {
  9222. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9223. }
  9224. - (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
  9225. if (self == other) {
  9226. return YES;
  9227. }
  9228. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9229. return NO;
  9230. }
  9231. return [_dictionary isEqual:other->_dictionary];
  9232. }
  9233. - (NSUInteger)hash {
  9234. return _dictionary.count;
  9235. }
  9236. - (NSString *)description {
  9237. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9238. }
  9239. - (NSUInteger)count {
  9240. return _dictionary.count;
  9241. }
  9242. - (void)enumerateKeysAndValuesUsingBlock:
  9243. (void (^)(NSString *key, double value, BOOL *stop))block {
  9244. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9245. NSNumber *aValue,
  9246. BOOL *stop) {
  9247. block(aKey, [aValue doubleValue], stop);
  9248. }];
  9249. }
  9250. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9251. NSUInteger count = _dictionary.count;
  9252. if (count == 0) {
  9253. return 0;
  9254. }
  9255. GPBType valueType = GPBGetFieldType(field);
  9256. GPBType keyType = field.mapKeyType;
  9257. __block size_t result = 0;
  9258. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9259. NSNumber *aValue,
  9260. BOOL *stop) {
  9261. #pragma unused(stop)
  9262. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9263. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  9264. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9265. }];
  9266. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9267. result += tagSize * count;
  9268. return result;
  9269. }
  9270. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9271. asField:(GPBFieldDescriptor *)field {
  9272. GPBType valueType = GPBGetFieldType(field);
  9273. GPBType keyType = field.mapKeyType;
  9274. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9275. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9276. NSNumber *aValue,
  9277. BOOL *stop) {
  9278. #pragma unused(stop)
  9279. // Write the tag.
  9280. [outputStream writeInt32NoTag:tag];
  9281. // Write the size of the message.
  9282. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9283. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueType);
  9284. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9285. // Write the fields.
  9286. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  9287. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueType);
  9288. }];
  9289. }
  9290. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9291. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9292. }
  9293. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9294. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9295. #pragma unused(stop)
  9296. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9297. }];
  9298. }
  9299. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  9300. NSNumber *wrapped = [_dictionary objectForKey:key];
  9301. if (wrapped && value) {
  9302. *value = [wrapped doubleValue];
  9303. }
  9304. return (wrapped != NULL);
  9305. }
  9306. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9307. if (otherDictionary) {
  9308. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9309. if (_autocreator) {
  9310. GPBAutocreatedDictionaryModified(_autocreator, self);
  9311. }
  9312. }
  9313. }
  9314. - (void)setValue:(double)value forKey:(NSString *)key {
  9315. [_dictionary setObject:@(value) forKey:key];
  9316. if (_autocreator) {
  9317. GPBAutocreatedDictionaryModified(_autocreator, self);
  9318. }
  9319. }
  9320. - (void)removeValueForKey:(NSString *)aKey {
  9321. [_dictionary removeObjectForKey:aKey];
  9322. }
  9323. - (void)removeAll {
  9324. [_dictionary removeAllObjects];
  9325. }
  9326. @end
  9327. #pragma mark - String -> Enum
  9328. @implementation GPBStringEnumDictionary {
  9329. @package
  9330. NSMutableDictionary *_dictionary;
  9331. GPBEnumValidationFunc _validationFunc;
  9332. }
  9333. @synthesize validationFunc = _validationFunc;
  9334. + (instancetype)dictionary {
  9335. return [[[self alloc] initWithValidationFunction:NULL
  9336. rawValues:NULL
  9337. forKeys:NULL
  9338. count:0] autorelease];
  9339. }
  9340. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9341. return [[[self alloc] initWithValidationFunction:func
  9342. rawValues:NULL
  9343. forKeys:NULL
  9344. count:0] autorelease];
  9345. }
  9346. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9347. rawValue:(int32_t)rawValue
  9348. forKey:(NSString *)key {
  9349. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9350. // on to get the type correct.
  9351. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9352. rawValues:&rawValue
  9353. forKeys:&key
  9354. count:1] autorelease];
  9355. }
  9356. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9357. rawValues:(const int32_t [])rawValues
  9358. forKeys:(const NSString * [])keys
  9359. count:(NSUInteger)count {
  9360. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9361. // on to get the type correct.
  9362. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9363. rawValues:rawValues
  9364. forKeys:keys
  9365. count:count] autorelease];
  9366. }
  9367. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9368. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9369. // on to get the type correct.
  9370. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9371. }
  9372. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9373. capacity:(NSUInteger)numItems {
  9374. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9375. }
  9376. - (instancetype)init {
  9377. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9378. }
  9379. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9380. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9381. }
  9382. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9383. rawValues:(const int32_t [])rawValues
  9384. forKeys:(const NSString * [])keys
  9385. count:(NSUInteger)count {
  9386. self = [super init];
  9387. if (self) {
  9388. _dictionary = [[NSMutableDictionary alloc] init];
  9389. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9390. if (count && rawValues && keys) {
  9391. for (NSUInteger i = 0; i < count; ++i) {
  9392. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9393. }
  9394. }
  9395. }
  9396. return self;
  9397. }
  9398. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9399. self = [self initWithValidationFunction:dictionary.validationFunc
  9400. rawValues:NULL
  9401. forKeys:NULL
  9402. count:0];
  9403. if (self) {
  9404. if (dictionary) {
  9405. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9406. }
  9407. }
  9408. return self;
  9409. }
  9410. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9411. capacity:(NSUInteger)numItems {
  9412. #pragma unused(numItems)
  9413. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9414. }
  9415. - (void)dealloc {
  9416. NSAssert(!_autocreator,
  9417. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9418. [self class], _autocreator);
  9419. [_dictionary release];
  9420. [super dealloc];
  9421. }
  9422. - (instancetype)copyWithZone:(NSZone *)zone {
  9423. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9424. }
  9425. - (BOOL)isEqual:(GPBStringEnumDictionary *)other {
  9426. if (self == other) {
  9427. return YES;
  9428. }
  9429. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9430. return NO;
  9431. }
  9432. return [_dictionary isEqual:other->_dictionary];
  9433. }
  9434. - (NSUInteger)hash {
  9435. return _dictionary.count;
  9436. }
  9437. - (NSString *)description {
  9438. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9439. }
  9440. - (NSUInteger)count {
  9441. return _dictionary.count;
  9442. }
  9443. - (void)enumerateKeysAndRawValuesUsingBlock:
  9444. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9445. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9446. NSNumber *aValue,
  9447. BOOL *stop) {
  9448. block(aKey, [aValue intValue], stop);
  9449. }];
  9450. }
  9451. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9452. NSUInteger count = _dictionary.count;
  9453. if (count == 0) {
  9454. return 0;
  9455. }
  9456. GPBType valueType = GPBGetFieldType(field);
  9457. GPBType keyType = field.mapKeyType;
  9458. __block size_t result = 0;
  9459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9460. NSNumber *aValue,
  9461. BOOL *stop) {
  9462. #pragma unused(stop)
  9463. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9464. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  9465. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9466. }];
  9467. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9468. result += tagSize * count;
  9469. return result;
  9470. }
  9471. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9472. asField:(GPBFieldDescriptor *)field {
  9473. GPBType valueType = GPBGetFieldType(field);
  9474. GPBType keyType = field.mapKeyType;
  9475. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9476. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9477. NSNumber *aValue,
  9478. BOOL *stop) {
  9479. #pragma unused(stop)
  9480. // Write the tag.
  9481. [outputStream writeInt32NoTag:tag];
  9482. // Write the size of the message.
  9483. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyType);
  9484. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueType);
  9485. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9486. // Write the fields.
  9487. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyType);
  9488. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueType);
  9489. }];
  9490. }
  9491. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9492. forKey:(GPBValue *)key
  9493. keyType:(GPBType)keyType {
  9494. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyType);
  9495. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  9496. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9497. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9498. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyType);
  9499. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  9500. [outputStream release];
  9501. return data;
  9502. }
  9503. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9504. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9505. }
  9506. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9507. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9508. #pragma unused(stop)
  9509. block(key, @(value));
  9510. }];
  9511. }
  9512. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9513. NSNumber *wrapped = [_dictionary objectForKey:key];
  9514. if (wrapped && value) {
  9515. int32_t result = [wrapped intValue];
  9516. if (!_validationFunc(result)) {
  9517. result = kGPBUnrecognizedEnumeratorValue;
  9518. }
  9519. *value = result;
  9520. }
  9521. return (wrapped != NULL);
  9522. }
  9523. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9524. NSNumber *wrapped = [_dictionary objectForKey:key];
  9525. if (wrapped && rawValue) {
  9526. *rawValue = [wrapped intValue];
  9527. }
  9528. return (wrapped != NULL);
  9529. }
  9530. - (void)enumerateKeysAndValuesUsingBlock:
  9531. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9532. GPBEnumValidationFunc func = _validationFunc;
  9533. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9534. NSNumber *aValue,
  9535. BOOL *stop) {
  9536. int32_t unwrapped = [aValue intValue];
  9537. if (!func(unwrapped)) {
  9538. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9539. }
  9540. block(aKey, unwrapped, stop);
  9541. }];
  9542. }
  9543. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9544. if (otherDictionary) {
  9545. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9546. if (_autocreator) {
  9547. GPBAutocreatedDictionaryModified(_autocreator, self);
  9548. }
  9549. }
  9550. }
  9551. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9552. [_dictionary setObject:@(value) forKey:key];
  9553. if (_autocreator) {
  9554. GPBAutocreatedDictionaryModified(_autocreator, self);
  9555. }
  9556. }
  9557. - (void)removeValueForKey:(NSString *)aKey {
  9558. [_dictionary removeObjectForKey:aKey];
  9559. }
  9560. - (void)removeAll {
  9561. [_dictionary removeAllObjects];
  9562. }
  9563. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9564. if (!_validationFunc(value)) {
  9565. [NSException raise:NSInvalidArgumentException
  9566. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9567. value];
  9568. }
  9569. [_dictionary setObject:@(value) forKey:key];
  9570. if (_autocreator) {
  9571. GPBAutocreatedDictionaryModified(_autocreator, self);
  9572. }
  9573. }
  9574. @end
  9575. //%PDDM-EXPAND-END (5 expansions)
  9576. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9577. // This block of code is generated, do not edit it directly.
  9578. #pragma mark - Bool -> UInt32
  9579. @implementation GPBBoolUInt32Dictionary {
  9580. @package
  9581. uint32_t _values[2];
  9582. BOOL _valueSet[2];
  9583. }
  9584. + (instancetype)dictionary {
  9585. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9586. }
  9587. + (instancetype)dictionaryWithValue:(uint32_t)value
  9588. forKey:(BOOL)key {
  9589. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9590. // on to get the type correct.
  9591. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9592. forKeys:&key
  9593. count:1] autorelease];
  9594. }
  9595. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9596. forKeys:(const BOOL [])keys
  9597. count:(NSUInteger)count {
  9598. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9599. // on to get the type correct.
  9600. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9601. forKeys:keys
  9602. count:count] autorelease];
  9603. }
  9604. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9605. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9606. // on to get the type correct.
  9607. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9608. }
  9609. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9610. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9611. }
  9612. - (instancetype)init {
  9613. return [self initWithValues:NULL forKeys:NULL count:0];
  9614. }
  9615. - (instancetype)initWithValues:(const uint32_t [])values
  9616. forKeys:(const BOOL [])keys
  9617. count:(NSUInteger)count {
  9618. self = [super init];
  9619. if (self) {
  9620. for (NSUInteger i = 0; i < count; ++i) {
  9621. int idx = keys[i] ? 1 : 0;
  9622. _values[idx] = values[i];
  9623. _valueSet[idx] = YES;
  9624. }
  9625. }
  9626. return self;
  9627. }
  9628. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9629. self = [self initWithValues:NULL forKeys:NULL count:0];
  9630. if (self) {
  9631. if (dictionary) {
  9632. for (int i = 0; i < 2; ++i) {
  9633. if (dictionary->_valueSet[i]) {
  9634. _values[i] = dictionary->_values[i];
  9635. _valueSet[i] = YES;
  9636. }
  9637. }
  9638. }
  9639. }
  9640. return self;
  9641. }
  9642. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9643. #pragma unused(numItems)
  9644. return [self initWithValues:NULL forKeys:NULL count:0];
  9645. }
  9646. #if !defined(NS_BLOCK_ASSERTIONS)
  9647. - (void)dealloc {
  9648. NSAssert(!_autocreator,
  9649. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9650. [self class], _autocreator);
  9651. [super dealloc];
  9652. }
  9653. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9654. - (instancetype)copyWithZone:(NSZone *)zone {
  9655. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9656. }
  9657. - (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
  9658. if (self == other) {
  9659. return YES;
  9660. }
  9661. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9662. return NO;
  9663. }
  9664. if ((_valueSet[0] != other->_valueSet[0]) ||
  9665. (_valueSet[1] != other->_valueSet[1])) {
  9666. return NO;
  9667. }
  9668. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9669. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9670. return NO;
  9671. }
  9672. return YES;
  9673. }
  9674. - (NSUInteger)hash {
  9675. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9676. }
  9677. - (NSString *)description {
  9678. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9679. if (_valueSet[0]) {
  9680. [result appendFormat:@"NO: %u", _values[0]];
  9681. }
  9682. if (_valueSet[1]) {
  9683. [result appendFormat:@"YES: %u", _values[1]];
  9684. }
  9685. [result appendString:@" }"];
  9686. return result;
  9687. }
  9688. - (NSUInteger)count {
  9689. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9690. }
  9691. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9692. int idx = (key ? 1 : 0);
  9693. if (_valueSet[idx]) {
  9694. if (value) {
  9695. *value = _values[idx];
  9696. }
  9697. return YES;
  9698. }
  9699. return NO;
  9700. }
  9701. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9702. int idx = (key->valueBool ? 1 : 0);
  9703. _values[idx] = value->valueUInt32;
  9704. _valueSet[idx] = YES;
  9705. }
  9706. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9707. if (_valueSet[0]) {
  9708. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9709. }
  9710. if (_valueSet[1]) {
  9711. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9712. }
  9713. }
  9714. - (void)enumerateKeysAndValuesUsingBlock:
  9715. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9716. BOOL stop = NO;
  9717. if (_valueSet[0]) {
  9718. block(NO, _values[0], &stop);
  9719. }
  9720. if (!stop && _valueSet[1]) {
  9721. block(YES, _values[1], &stop);
  9722. }
  9723. }
  9724. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9725. GPBType valueType = GPBGetFieldType(field);
  9726. NSUInteger count = 0;
  9727. size_t result = 0;
  9728. for (int i = 0; i < 2; ++i) {
  9729. if (_valueSet[i]) {
  9730. ++count;
  9731. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9732. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9733. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9734. }
  9735. }
  9736. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9737. result += tagSize * count;
  9738. return result;
  9739. }
  9740. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9741. asField:(GPBFieldDescriptor *)field {
  9742. GPBType valueType = GPBGetFieldType(field);
  9743. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9744. for (int i = 0; i < 2; ++i) {
  9745. if (_valueSet[i]) {
  9746. // Write the tag.
  9747. [outputStream writeInt32NoTag:tag];
  9748. // Write the size of the message.
  9749. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9750. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9751. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9752. // Write the fields.
  9753. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9754. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  9755. }
  9756. }
  9757. }
  9758. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9759. if (otherDictionary) {
  9760. for (int i = 0; i < 2; ++i) {
  9761. if (otherDictionary->_valueSet[i]) {
  9762. _valueSet[i] = YES;
  9763. _values[i] = otherDictionary->_values[i];
  9764. }
  9765. }
  9766. if (_autocreator) {
  9767. GPBAutocreatedDictionaryModified(_autocreator, self);
  9768. }
  9769. }
  9770. }
  9771. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  9772. int idx = (key ? 1 : 0);
  9773. _values[idx] = value;
  9774. _valueSet[idx] = YES;
  9775. if (_autocreator) {
  9776. GPBAutocreatedDictionaryModified(_autocreator, self);
  9777. }
  9778. }
  9779. - (void)removeValueForKey:(BOOL)aKey {
  9780. _valueSet[aKey ? 1 : 0] = NO;
  9781. }
  9782. - (void)removeAll {
  9783. _valueSet[0] = NO;
  9784. _valueSet[1] = NO;
  9785. }
  9786. @end
  9787. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9788. // This block of code is generated, do not edit it directly.
  9789. #pragma mark - Bool -> Int32
  9790. @implementation GPBBoolInt32Dictionary {
  9791. @package
  9792. int32_t _values[2];
  9793. BOOL _valueSet[2];
  9794. }
  9795. + (instancetype)dictionary {
  9796. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9797. }
  9798. + (instancetype)dictionaryWithValue:(int32_t)value
  9799. forKey:(BOOL)key {
  9800. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9801. // on to get the type correct.
  9802. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  9803. forKeys:&key
  9804. count:1] autorelease];
  9805. }
  9806. + (instancetype)dictionaryWithValues:(const int32_t [])values
  9807. forKeys:(const BOOL [])keys
  9808. count:(NSUInteger)count {
  9809. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9810. // on to get the type correct.
  9811. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  9812. forKeys:keys
  9813. count:count] autorelease];
  9814. }
  9815. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9816. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9817. // on to get the type correct.
  9818. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9819. }
  9820. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9821. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9822. }
  9823. - (instancetype)init {
  9824. return [self initWithValues:NULL forKeys:NULL count:0];
  9825. }
  9826. - (instancetype)initWithValues:(const int32_t [])values
  9827. forKeys:(const BOOL [])keys
  9828. count:(NSUInteger)count {
  9829. self = [super init];
  9830. if (self) {
  9831. for (NSUInteger i = 0; i < count; ++i) {
  9832. int idx = keys[i] ? 1 : 0;
  9833. _values[idx] = values[i];
  9834. _valueSet[idx] = YES;
  9835. }
  9836. }
  9837. return self;
  9838. }
  9839. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9840. self = [self initWithValues:NULL forKeys:NULL count:0];
  9841. if (self) {
  9842. if (dictionary) {
  9843. for (int i = 0; i < 2; ++i) {
  9844. if (dictionary->_valueSet[i]) {
  9845. _values[i] = dictionary->_values[i];
  9846. _valueSet[i] = YES;
  9847. }
  9848. }
  9849. }
  9850. }
  9851. return self;
  9852. }
  9853. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9854. #pragma unused(numItems)
  9855. return [self initWithValues:NULL forKeys:NULL count:0];
  9856. }
  9857. #if !defined(NS_BLOCK_ASSERTIONS)
  9858. - (void)dealloc {
  9859. NSAssert(!_autocreator,
  9860. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9861. [self class], _autocreator);
  9862. [super dealloc];
  9863. }
  9864. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9865. - (instancetype)copyWithZone:(NSZone *)zone {
  9866. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9867. }
  9868. - (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
  9869. if (self == other) {
  9870. return YES;
  9871. }
  9872. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  9873. return NO;
  9874. }
  9875. if ((_valueSet[0] != other->_valueSet[0]) ||
  9876. (_valueSet[1] != other->_valueSet[1])) {
  9877. return NO;
  9878. }
  9879. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9880. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9881. return NO;
  9882. }
  9883. return YES;
  9884. }
  9885. - (NSUInteger)hash {
  9886. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9887. }
  9888. - (NSString *)description {
  9889. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9890. if (_valueSet[0]) {
  9891. [result appendFormat:@"NO: %d", _values[0]];
  9892. }
  9893. if (_valueSet[1]) {
  9894. [result appendFormat:@"YES: %d", _values[1]];
  9895. }
  9896. [result appendString:@" }"];
  9897. return result;
  9898. }
  9899. - (NSUInteger)count {
  9900. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9901. }
  9902. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  9903. int idx = (key ? 1 : 0);
  9904. if (_valueSet[idx]) {
  9905. if (value) {
  9906. *value = _values[idx];
  9907. }
  9908. return YES;
  9909. }
  9910. return NO;
  9911. }
  9912. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  9913. int idx = (key->valueBool ? 1 : 0);
  9914. _values[idx] = value->valueInt32;
  9915. _valueSet[idx] = YES;
  9916. }
  9917. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9918. if (_valueSet[0]) {
  9919. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9920. }
  9921. if (_valueSet[1]) {
  9922. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9923. }
  9924. }
  9925. - (void)enumerateKeysAndValuesUsingBlock:
  9926. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  9927. BOOL stop = NO;
  9928. if (_valueSet[0]) {
  9929. block(NO, _values[0], &stop);
  9930. }
  9931. if (!stop && _valueSet[1]) {
  9932. block(YES, _values[1], &stop);
  9933. }
  9934. }
  9935. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9936. GPBType valueType = GPBGetFieldType(field);
  9937. NSUInteger count = 0;
  9938. size_t result = 0;
  9939. for (int i = 0; i < 2; ++i) {
  9940. if (_valueSet[i]) {
  9941. ++count;
  9942. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9943. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9944. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9945. }
  9946. }
  9947. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  9948. result += tagSize * count;
  9949. return result;
  9950. }
  9951. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9952. asField:(GPBFieldDescriptor *)field {
  9953. GPBType valueType = GPBGetFieldType(field);
  9954. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9955. for (int i = 0; i < 2; ++i) {
  9956. if (_valueSet[i]) {
  9957. // Write the tag.
  9958. [outputStream writeInt32NoTag:tag];
  9959. // Write the size of the message.
  9960. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9961. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  9962. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9963. // Write the fields.
  9964. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  9965. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  9966. }
  9967. }
  9968. }
  9969. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  9970. if (otherDictionary) {
  9971. for (int i = 0; i < 2; ++i) {
  9972. if (otherDictionary->_valueSet[i]) {
  9973. _valueSet[i] = YES;
  9974. _values[i] = otherDictionary->_values[i];
  9975. }
  9976. }
  9977. if (_autocreator) {
  9978. GPBAutocreatedDictionaryModified(_autocreator, self);
  9979. }
  9980. }
  9981. }
  9982. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  9983. int idx = (key ? 1 : 0);
  9984. _values[idx] = value;
  9985. _valueSet[idx] = YES;
  9986. if (_autocreator) {
  9987. GPBAutocreatedDictionaryModified(_autocreator, self);
  9988. }
  9989. }
  9990. - (void)removeValueForKey:(BOOL)aKey {
  9991. _valueSet[aKey ? 1 : 0] = NO;
  9992. }
  9993. - (void)removeAll {
  9994. _valueSet[0] = NO;
  9995. _valueSet[1] = NO;
  9996. }
  9997. @end
  9998. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  9999. // This block of code is generated, do not edit it directly.
  10000. #pragma mark - Bool -> UInt64
  10001. @implementation GPBBoolUInt64Dictionary {
  10002. @package
  10003. uint64_t _values[2];
  10004. BOOL _valueSet[2];
  10005. }
  10006. + (instancetype)dictionary {
  10007. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10008. }
  10009. + (instancetype)dictionaryWithValue:(uint64_t)value
  10010. forKey:(BOOL)key {
  10011. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10012. // on to get the type correct.
  10013. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  10014. forKeys:&key
  10015. count:1] autorelease];
  10016. }
  10017. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  10018. forKeys:(const BOOL [])keys
  10019. count:(NSUInteger)count {
  10020. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10021. // on to get the type correct.
  10022. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  10023. forKeys:keys
  10024. count:count] autorelease];
  10025. }
  10026. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10027. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10028. // on to get the type correct.
  10029. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10030. }
  10031. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10032. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10033. }
  10034. - (instancetype)init {
  10035. return [self initWithValues:NULL forKeys:NULL count:0];
  10036. }
  10037. - (instancetype)initWithValues:(const uint64_t [])values
  10038. forKeys:(const BOOL [])keys
  10039. count:(NSUInteger)count {
  10040. self = [super init];
  10041. if (self) {
  10042. for (NSUInteger i = 0; i < count; ++i) {
  10043. int idx = keys[i] ? 1 : 0;
  10044. _values[idx] = values[i];
  10045. _valueSet[idx] = YES;
  10046. }
  10047. }
  10048. return self;
  10049. }
  10050. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10051. self = [self initWithValues:NULL forKeys:NULL count:0];
  10052. if (self) {
  10053. if (dictionary) {
  10054. for (int i = 0; i < 2; ++i) {
  10055. if (dictionary->_valueSet[i]) {
  10056. _values[i] = dictionary->_values[i];
  10057. _valueSet[i] = YES;
  10058. }
  10059. }
  10060. }
  10061. }
  10062. return self;
  10063. }
  10064. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10065. #pragma unused(numItems)
  10066. return [self initWithValues:NULL forKeys:NULL count:0];
  10067. }
  10068. #if !defined(NS_BLOCK_ASSERTIONS)
  10069. - (void)dealloc {
  10070. NSAssert(!_autocreator,
  10071. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10072. [self class], _autocreator);
  10073. [super dealloc];
  10074. }
  10075. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10076. - (instancetype)copyWithZone:(NSZone *)zone {
  10077. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10078. }
  10079. - (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
  10080. if (self == other) {
  10081. return YES;
  10082. }
  10083. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10084. return NO;
  10085. }
  10086. if ((_valueSet[0] != other->_valueSet[0]) ||
  10087. (_valueSet[1] != other->_valueSet[1])) {
  10088. return NO;
  10089. }
  10090. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10091. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10092. return NO;
  10093. }
  10094. return YES;
  10095. }
  10096. - (NSUInteger)hash {
  10097. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10098. }
  10099. - (NSString *)description {
  10100. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10101. if (_valueSet[0]) {
  10102. [result appendFormat:@"NO: %llu", _values[0]];
  10103. }
  10104. if (_valueSet[1]) {
  10105. [result appendFormat:@"YES: %llu", _values[1]];
  10106. }
  10107. [result appendString:@" }"];
  10108. return result;
  10109. }
  10110. - (NSUInteger)count {
  10111. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10112. }
  10113. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  10114. int idx = (key ? 1 : 0);
  10115. if (_valueSet[idx]) {
  10116. if (value) {
  10117. *value = _values[idx];
  10118. }
  10119. return YES;
  10120. }
  10121. return NO;
  10122. }
  10123. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10124. int idx = (key->valueBool ? 1 : 0);
  10125. _values[idx] = value->valueUInt64;
  10126. _valueSet[idx] = YES;
  10127. }
  10128. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10129. if (_valueSet[0]) {
  10130. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10131. }
  10132. if (_valueSet[1]) {
  10133. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10134. }
  10135. }
  10136. - (void)enumerateKeysAndValuesUsingBlock:
  10137. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10138. BOOL stop = NO;
  10139. if (_valueSet[0]) {
  10140. block(NO, _values[0], &stop);
  10141. }
  10142. if (!stop && _valueSet[1]) {
  10143. block(YES, _values[1], &stop);
  10144. }
  10145. }
  10146. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10147. GPBType valueType = GPBGetFieldType(field);
  10148. NSUInteger count = 0;
  10149. size_t result = 0;
  10150. for (int i = 0; i < 2; ++i) {
  10151. if (_valueSet[i]) {
  10152. ++count;
  10153. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10154. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  10155. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10156. }
  10157. }
  10158. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10159. result += tagSize * count;
  10160. return result;
  10161. }
  10162. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10163. asField:(GPBFieldDescriptor *)field {
  10164. GPBType valueType = GPBGetFieldType(field);
  10165. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10166. for (int i = 0; i < 2; ++i) {
  10167. if (_valueSet[i]) {
  10168. // Write the tag.
  10169. [outputStream writeInt32NoTag:tag];
  10170. // Write the size of the message.
  10171. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10172. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  10173. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10174. // Write the fields.
  10175. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10176. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10177. }
  10178. }
  10179. }
  10180. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10181. if (otherDictionary) {
  10182. for (int i = 0; i < 2; ++i) {
  10183. if (otherDictionary->_valueSet[i]) {
  10184. _valueSet[i] = YES;
  10185. _values[i] = otherDictionary->_values[i];
  10186. }
  10187. }
  10188. if (_autocreator) {
  10189. GPBAutocreatedDictionaryModified(_autocreator, self);
  10190. }
  10191. }
  10192. }
  10193. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  10194. int idx = (key ? 1 : 0);
  10195. _values[idx] = value;
  10196. _valueSet[idx] = YES;
  10197. if (_autocreator) {
  10198. GPBAutocreatedDictionaryModified(_autocreator, self);
  10199. }
  10200. }
  10201. - (void)removeValueForKey:(BOOL)aKey {
  10202. _valueSet[aKey ? 1 : 0] = NO;
  10203. }
  10204. - (void)removeAll {
  10205. _valueSet[0] = NO;
  10206. _valueSet[1] = NO;
  10207. }
  10208. @end
  10209. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10210. // This block of code is generated, do not edit it directly.
  10211. #pragma mark - Bool -> Int64
  10212. @implementation GPBBoolInt64Dictionary {
  10213. @package
  10214. int64_t _values[2];
  10215. BOOL _valueSet[2];
  10216. }
  10217. + (instancetype)dictionary {
  10218. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10219. }
  10220. + (instancetype)dictionaryWithValue:(int64_t)value
  10221. forKey:(BOOL)key {
  10222. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10223. // on to get the type correct.
  10224. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  10225. forKeys:&key
  10226. count:1] autorelease];
  10227. }
  10228. + (instancetype)dictionaryWithValues:(const int64_t [])values
  10229. forKeys:(const BOOL [])keys
  10230. count:(NSUInteger)count {
  10231. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10232. // on to get the type correct.
  10233. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  10234. forKeys:keys
  10235. count:count] autorelease];
  10236. }
  10237. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10238. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10239. // on to get the type correct.
  10240. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10241. }
  10242. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10243. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10244. }
  10245. - (instancetype)init {
  10246. return [self initWithValues:NULL forKeys:NULL count:0];
  10247. }
  10248. - (instancetype)initWithValues:(const int64_t [])values
  10249. forKeys:(const BOOL [])keys
  10250. count:(NSUInteger)count {
  10251. self = [super init];
  10252. if (self) {
  10253. for (NSUInteger i = 0; i < count; ++i) {
  10254. int idx = keys[i] ? 1 : 0;
  10255. _values[idx] = values[i];
  10256. _valueSet[idx] = YES;
  10257. }
  10258. }
  10259. return self;
  10260. }
  10261. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10262. self = [self initWithValues:NULL forKeys:NULL count:0];
  10263. if (self) {
  10264. if (dictionary) {
  10265. for (int i = 0; i < 2; ++i) {
  10266. if (dictionary->_valueSet[i]) {
  10267. _values[i] = dictionary->_values[i];
  10268. _valueSet[i] = YES;
  10269. }
  10270. }
  10271. }
  10272. }
  10273. return self;
  10274. }
  10275. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10276. #pragma unused(numItems)
  10277. return [self initWithValues:NULL forKeys:NULL count:0];
  10278. }
  10279. #if !defined(NS_BLOCK_ASSERTIONS)
  10280. - (void)dealloc {
  10281. NSAssert(!_autocreator,
  10282. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10283. [self class], _autocreator);
  10284. [super dealloc];
  10285. }
  10286. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10287. - (instancetype)copyWithZone:(NSZone *)zone {
  10288. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10289. }
  10290. - (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
  10291. if (self == other) {
  10292. return YES;
  10293. }
  10294. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10295. return NO;
  10296. }
  10297. if ((_valueSet[0] != other->_valueSet[0]) ||
  10298. (_valueSet[1] != other->_valueSet[1])) {
  10299. return NO;
  10300. }
  10301. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10302. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10303. return NO;
  10304. }
  10305. return YES;
  10306. }
  10307. - (NSUInteger)hash {
  10308. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10309. }
  10310. - (NSString *)description {
  10311. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10312. if (_valueSet[0]) {
  10313. [result appendFormat:@"NO: %lld", _values[0]];
  10314. }
  10315. if (_valueSet[1]) {
  10316. [result appendFormat:@"YES: %lld", _values[1]];
  10317. }
  10318. [result appendString:@" }"];
  10319. return result;
  10320. }
  10321. - (NSUInteger)count {
  10322. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10323. }
  10324. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  10325. int idx = (key ? 1 : 0);
  10326. if (_valueSet[idx]) {
  10327. if (value) {
  10328. *value = _values[idx];
  10329. }
  10330. return YES;
  10331. }
  10332. return NO;
  10333. }
  10334. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10335. int idx = (key->valueBool ? 1 : 0);
  10336. _values[idx] = value->valueInt64;
  10337. _valueSet[idx] = YES;
  10338. }
  10339. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10340. if (_valueSet[0]) {
  10341. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10342. }
  10343. if (_valueSet[1]) {
  10344. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10345. }
  10346. }
  10347. - (void)enumerateKeysAndValuesUsingBlock:
  10348. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10349. BOOL stop = NO;
  10350. if (_valueSet[0]) {
  10351. block(NO, _values[0], &stop);
  10352. }
  10353. if (!stop && _valueSet[1]) {
  10354. block(YES, _values[1], &stop);
  10355. }
  10356. }
  10357. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10358. GPBType valueType = GPBGetFieldType(field);
  10359. NSUInteger count = 0;
  10360. size_t result = 0;
  10361. for (int i = 0; i < 2; ++i) {
  10362. if (_valueSet[i]) {
  10363. ++count;
  10364. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10365. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  10366. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10367. }
  10368. }
  10369. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10370. result += tagSize * count;
  10371. return result;
  10372. }
  10373. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10374. asField:(GPBFieldDescriptor *)field {
  10375. GPBType valueType = GPBGetFieldType(field);
  10376. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10377. for (int i = 0; i < 2; ++i) {
  10378. if (_valueSet[i]) {
  10379. // Write the tag.
  10380. [outputStream writeInt32NoTag:tag];
  10381. // Write the size of the message.
  10382. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10383. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueType);
  10384. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10385. // Write the fields.
  10386. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10387. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10388. }
  10389. }
  10390. }
  10391. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10392. if (otherDictionary) {
  10393. for (int i = 0; i < 2; ++i) {
  10394. if (otherDictionary->_valueSet[i]) {
  10395. _valueSet[i] = YES;
  10396. _values[i] = otherDictionary->_values[i];
  10397. }
  10398. }
  10399. if (_autocreator) {
  10400. GPBAutocreatedDictionaryModified(_autocreator, self);
  10401. }
  10402. }
  10403. }
  10404. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  10405. int idx = (key ? 1 : 0);
  10406. _values[idx] = value;
  10407. _valueSet[idx] = YES;
  10408. if (_autocreator) {
  10409. GPBAutocreatedDictionaryModified(_autocreator, self);
  10410. }
  10411. }
  10412. - (void)removeValueForKey:(BOOL)aKey {
  10413. _valueSet[aKey ? 1 : 0] = NO;
  10414. }
  10415. - (void)removeAll {
  10416. _valueSet[0] = NO;
  10417. _valueSet[1] = NO;
  10418. }
  10419. @end
  10420. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10421. // This block of code is generated, do not edit it directly.
  10422. #pragma mark - Bool -> Bool
  10423. @implementation GPBBoolBoolDictionary {
  10424. @package
  10425. BOOL _values[2];
  10426. BOOL _valueSet[2];
  10427. }
  10428. + (instancetype)dictionary {
  10429. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10430. }
  10431. + (instancetype)dictionaryWithValue:(BOOL)value
  10432. forKey:(BOOL)key {
  10433. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10434. // on to get the type correct.
  10435. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  10436. forKeys:&key
  10437. count:1] autorelease];
  10438. }
  10439. + (instancetype)dictionaryWithValues:(const BOOL [])values
  10440. forKeys:(const BOOL [])keys
  10441. count:(NSUInteger)count {
  10442. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10443. // on to get the type correct.
  10444. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  10445. forKeys:keys
  10446. count:count] autorelease];
  10447. }
  10448. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10449. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10450. // on to get the type correct.
  10451. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10452. }
  10453. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10454. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10455. }
  10456. - (instancetype)init {
  10457. return [self initWithValues:NULL forKeys:NULL count:0];
  10458. }
  10459. - (instancetype)initWithValues:(const BOOL [])values
  10460. forKeys:(const BOOL [])keys
  10461. count:(NSUInteger)count {
  10462. self = [super init];
  10463. if (self) {
  10464. for (NSUInteger i = 0; i < count; ++i) {
  10465. int idx = keys[i] ? 1 : 0;
  10466. _values[idx] = values[i];
  10467. _valueSet[idx] = YES;
  10468. }
  10469. }
  10470. return self;
  10471. }
  10472. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10473. self = [self initWithValues:NULL forKeys:NULL count:0];
  10474. if (self) {
  10475. if (dictionary) {
  10476. for (int i = 0; i < 2; ++i) {
  10477. if (dictionary->_valueSet[i]) {
  10478. _values[i] = dictionary->_values[i];
  10479. _valueSet[i] = YES;
  10480. }
  10481. }
  10482. }
  10483. }
  10484. return self;
  10485. }
  10486. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10487. #pragma unused(numItems)
  10488. return [self initWithValues:NULL forKeys:NULL count:0];
  10489. }
  10490. #if !defined(NS_BLOCK_ASSERTIONS)
  10491. - (void)dealloc {
  10492. NSAssert(!_autocreator,
  10493. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10494. [self class], _autocreator);
  10495. [super dealloc];
  10496. }
  10497. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10498. - (instancetype)copyWithZone:(NSZone *)zone {
  10499. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10500. }
  10501. - (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
  10502. if (self == other) {
  10503. return YES;
  10504. }
  10505. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10506. return NO;
  10507. }
  10508. if ((_valueSet[0] != other->_valueSet[0]) ||
  10509. (_valueSet[1] != other->_valueSet[1])) {
  10510. return NO;
  10511. }
  10512. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10513. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10514. return NO;
  10515. }
  10516. return YES;
  10517. }
  10518. - (NSUInteger)hash {
  10519. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10520. }
  10521. - (NSString *)description {
  10522. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10523. if (_valueSet[0]) {
  10524. [result appendFormat:@"NO: %d", _values[0]];
  10525. }
  10526. if (_valueSet[1]) {
  10527. [result appendFormat:@"YES: %d", _values[1]];
  10528. }
  10529. [result appendString:@" }"];
  10530. return result;
  10531. }
  10532. - (NSUInteger)count {
  10533. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10534. }
  10535. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10536. int idx = (key ? 1 : 0);
  10537. if (_valueSet[idx]) {
  10538. if (value) {
  10539. *value = _values[idx];
  10540. }
  10541. return YES;
  10542. }
  10543. return NO;
  10544. }
  10545. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10546. int idx = (key->valueBool ? 1 : 0);
  10547. _values[idx] = value->valueBool;
  10548. _valueSet[idx] = YES;
  10549. }
  10550. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10551. if (_valueSet[0]) {
  10552. block(@"false", (_values[0] ? @"true" : @"false"));
  10553. }
  10554. if (_valueSet[1]) {
  10555. block(@"true", (_values[1] ? @"true" : @"false"));
  10556. }
  10557. }
  10558. - (void)enumerateKeysAndValuesUsingBlock:
  10559. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10560. BOOL stop = NO;
  10561. if (_valueSet[0]) {
  10562. block(NO, _values[0], &stop);
  10563. }
  10564. if (!stop && _valueSet[1]) {
  10565. block(YES, _values[1], &stop);
  10566. }
  10567. }
  10568. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10569. GPBType valueType = GPBGetFieldType(field);
  10570. NSUInteger count = 0;
  10571. size_t result = 0;
  10572. for (int i = 0; i < 2; ++i) {
  10573. if (_valueSet[i]) {
  10574. ++count;
  10575. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10576. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10577. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10578. }
  10579. }
  10580. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10581. result += tagSize * count;
  10582. return result;
  10583. }
  10584. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10585. asField:(GPBFieldDescriptor *)field {
  10586. GPBType valueType = GPBGetFieldType(field);
  10587. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10588. for (int i = 0; i < 2; ++i) {
  10589. if (_valueSet[i]) {
  10590. // Write the tag.
  10591. [outputStream writeInt32NoTag:tag];
  10592. // Write the size of the message.
  10593. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10594. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10595. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10596. // Write the fields.
  10597. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10598. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10599. }
  10600. }
  10601. }
  10602. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10603. if (otherDictionary) {
  10604. for (int i = 0; i < 2; ++i) {
  10605. if (otherDictionary->_valueSet[i]) {
  10606. _valueSet[i] = YES;
  10607. _values[i] = otherDictionary->_values[i];
  10608. }
  10609. }
  10610. if (_autocreator) {
  10611. GPBAutocreatedDictionaryModified(_autocreator, self);
  10612. }
  10613. }
  10614. }
  10615. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10616. int idx = (key ? 1 : 0);
  10617. _values[idx] = value;
  10618. _valueSet[idx] = YES;
  10619. if (_autocreator) {
  10620. GPBAutocreatedDictionaryModified(_autocreator, self);
  10621. }
  10622. }
  10623. - (void)removeValueForKey:(BOOL)aKey {
  10624. _valueSet[aKey ? 1 : 0] = NO;
  10625. }
  10626. - (void)removeAll {
  10627. _valueSet[0] = NO;
  10628. _valueSet[1] = NO;
  10629. }
  10630. @end
  10631. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10632. // This block of code is generated, do not edit it directly.
  10633. #pragma mark - Bool -> Float
  10634. @implementation GPBBoolFloatDictionary {
  10635. @package
  10636. float _values[2];
  10637. BOOL _valueSet[2];
  10638. }
  10639. + (instancetype)dictionary {
  10640. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10641. }
  10642. + (instancetype)dictionaryWithValue:(float)value
  10643. forKey:(BOOL)key {
  10644. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10645. // on to get the type correct.
  10646. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10647. forKeys:&key
  10648. count:1] autorelease];
  10649. }
  10650. + (instancetype)dictionaryWithValues:(const float [])values
  10651. forKeys:(const BOOL [])keys
  10652. count:(NSUInteger)count {
  10653. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10654. // on to get the type correct.
  10655. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10656. forKeys:keys
  10657. count:count] autorelease];
  10658. }
  10659. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10660. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10661. // on to get the type correct.
  10662. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10663. }
  10664. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10665. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10666. }
  10667. - (instancetype)init {
  10668. return [self initWithValues:NULL forKeys:NULL count:0];
  10669. }
  10670. - (instancetype)initWithValues:(const float [])values
  10671. forKeys:(const BOOL [])keys
  10672. count:(NSUInteger)count {
  10673. self = [super init];
  10674. if (self) {
  10675. for (NSUInteger i = 0; i < count; ++i) {
  10676. int idx = keys[i] ? 1 : 0;
  10677. _values[idx] = values[i];
  10678. _valueSet[idx] = YES;
  10679. }
  10680. }
  10681. return self;
  10682. }
  10683. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10684. self = [self initWithValues:NULL forKeys:NULL count:0];
  10685. if (self) {
  10686. if (dictionary) {
  10687. for (int i = 0; i < 2; ++i) {
  10688. if (dictionary->_valueSet[i]) {
  10689. _values[i] = dictionary->_values[i];
  10690. _valueSet[i] = YES;
  10691. }
  10692. }
  10693. }
  10694. }
  10695. return self;
  10696. }
  10697. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10698. #pragma unused(numItems)
  10699. return [self initWithValues:NULL forKeys:NULL count:0];
  10700. }
  10701. #if !defined(NS_BLOCK_ASSERTIONS)
  10702. - (void)dealloc {
  10703. NSAssert(!_autocreator,
  10704. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10705. [self class], _autocreator);
  10706. [super dealloc];
  10707. }
  10708. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10709. - (instancetype)copyWithZone:(NSZone *)zone {
  10710. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10711. }
  10712. - (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
  10713. if (self == other) {
  10714. return YES;
  10715. }
  10716. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10717. return NO;
  10718. }
  10719. if ((_valueSet[0] != other->_valueSet[0]) ||
  10720. (_valueSet[1] != other->_valueSet[1])) {
  10721. return NO;
  10722. }
  10723. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10724. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10725. return NO;
  10726. }
  10727. return YES;
  10728. }
  10729. - (NSUInteger)hash {
  10730. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10731. }
  10732. - (NSString *)description {
  10733. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10734. if (_valueSet[0]) {
  10735. [result appendFormat:@"NO: %f", _values[0]];
  10736. }
  10737. if (_valueSet[1]) {
  10738. [result appendFormat:@"YES: %f", _values[1]];
  10739. }
  10740. [result appendString:@" }"];
  10741. return result;
  10742. }
  10743. - (NSUInteger)count {
  10744. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10745. }
  10746. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  10747. int idx = (key ? 1 : 0);
  10748. if (_valueSet[idx]) {
  10749. if (value) {
  10750. *value = _values[idx];
  10751. }
  10752. return YES;
  10753. }
  10754. return NO;
  10755. }
  10756. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10757. int idx = (key->valueBool ? 1 : 0);
  10758. _values[idx] = value->valueFloat;
  10759. _valueSet[idx] = YES;
  10760. }
  10761. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10762. if (_valueSet[0]) {
  10763. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  10764. }
  10765. if (_valueSet[1]) {
  10766. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  10767. }
  10768. }
  10769. - (void)enumerateKeysAndValuesUsingBlock:
  10770. (void (^)(BOOL key, float value, BOOL *stop))block {
  10771. BOOL stop = NO;
  10772. if (_valueSet[0]) {
  10773. block(NO, _values[0], &stop);
  10774. }
  10775. if (!stop && _valueSet[1]) {
  10776. block(YES, _values[1], &stop);
  10777. }
  10778. }
  10779. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10780. GPBType valueType = GPBGetFieldType(field);
  10781. NSUInteger count = 0;
  10782. size_t result = 0;
  10783. for (int i = 0; i < 2; ++i) {
  10784. if (_valueSet[i]) {
  10785. ++count;
  10786. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10787. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10788. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10789. }
  10790. }
  10791. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  10792. result += tagSize * count;
  10793. return result;
  10794. }
  10795. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10796. asField:(GPBFieldDescriptor *)field {
  10797. GPBType valueType = GPBGetFieldType(field);
  10798. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10799. for (int i = 0; i < 2; ++i) {
  10800. if (_valueSet[i]) {
  10801. // Write the tag.
  10802. [outputStream writeInt32NoTag:tag];
  10803. // Write the size of the message.
  10804. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10805. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10806. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10807. // Write the fields.
  10808. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10809. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  10810. }
  10811. }
  10812. }
  10813. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  10814. if (otherDictionary) {
  10815. for (int i = 0; i < 2; ++i) {
  10816. if (otherDictionary->_valueSet[i]) {
  10817. _valueSet[i] = YES;
  10818. _values[i] = otherDictionary->_values[i];
  10819. }
  10820. }
  10821. if (_autocreator) {
  10822. GPBAutocreatedDictionaryModified(_autocreator, self);
  10823. }
  10824. }
  10825. }
  10826. - (void)setValue:(float)value forKey:(BOOL)key {
  10827. int idx = (key ? 1 : 0);
  10828. _values[idx] = value;
  10829. _valueSet[idx] = YES;
  10830. if (_autocreator) {
  10831. GPBAutocreatedDictionaryModified(_autocreator, self);
  10832. }
  10833. }
  10834. - (void)removeValueForKey:(BOOL)aKey {
  10835. _valueSet[aKey ? 1 : 0] = NO;
  10836. }
  10837. - (void)removeAll {
  10838. _valueSet[0] = NO;
  10839. _valueSet[1] = NO;
  10840. }
  10841. @end
  10842. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  10843. // This block of code is generated, do not edit it directly.
  10844. #pragma mark - Bool -> Double
  10845. @implementation GPBBoolDoubleDictionary {
  10846. @package
  10847. double _values[2];
  10848. BOOL _valueSet[2];
  10849. }
  10850. + (instancetype)dictionary {
  10851. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10852. }
  10853. + (instancetype)dictionaryWithValue:(double)value
  10854. forKey:(BOOL)key {
  10855. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10856. // on to get the type correct.
  10857. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  10858. forKeys:&key
  10859. count:1] autorelease];
  10860. }
  10861. + (instancetype)dictionaryWithValues:(const double [])values
  10862. forKeys:(const BOOL [])keys
  10863. count:(NSUInteger)count {
  10864. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10865. // on to get the type correct.
  10866. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  10867. forKeys:keys
  10868. count:count] autorelease];
  10869. }
  10870. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10871. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10872. // on to get the type correct.
  10873. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10874. }
  10875. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10876. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10877. }
  10878. - (instancetype)init {
  10879. return [self initWithValues:NULL forKeys:NULL count:0];
  10880. }
  10881. - (instancetype)initWithValues:(const double [])values
  10882. forKeys:(const BOOL [])keys
  10883. count:(NSUInteger)count {
  10884. self = [super init];
  10885. if (self) {
  10886. for (NSUInteger i = 0; i < count; ++i) {
  10887. int idx = keys[i] ? 1 : 0;
  10888. _values[idx] = values[i];
  10889. _valueSet[idx] = YES;
  10890. }
  10891. }
  10892. return self;
  10893. }
  10894. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10895. self = [self initWithValues:NULL forKeys:NULL count:0];
  10896. if (self) {
  10897. if (dictionary) {
  10898. for (int i = 0; i < 2; ++i) {
  10899. if (dictionary->_valueSet[i]) {
  10900. _values[i] = dictionary->_values[i];
  10901. _valueSet[i] = YES;
  10902. }
  10903. }
  10904. }
  10905. }
  10906. return self;
  10907. }
  10908. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10909. #pragma unused(numItems)
  10910. return [self initWithValues:NULL forKeys:NULL count:0];
  10911. }
  10912. #if !defined(NS_BLOCK_ASSERTIONS)
  10913. - (void)dealloc {
  10914. NSAssert(!_autocreator,
  10915. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10916. [self class], _autocreator);
  10917. [super dealloc];
  10918. }
  10919. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10920. - (instancetype)copyWithZone:(NSZone *)zone {
  10921. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  10922. }
  10923. - (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
  10924. if (self == other) {
  10925. return YES;
  10926. }
  10927. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  10928. return NO;
  10929. }
  10930. if ((_valueSet[0] != other->_valueSet[0]) ||
  10931. (_valueSet[1] != other->_valueSet[1])) {
  10932. return NO;
  10933. }
  10934. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10935. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10936. return NO;
  10937. }
  10938. return YES;
  10939. }
  10940. - (NSUInteger)hash {
  10941. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10942. }
  10943. - (NSString *)description {
  10944. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10945. if (_valueSet[0]) {
  10946. [result appendFormat:@"NO: %lf", _values[0]];
  10947. }
  10948. if (_valueSet[1]) {
  10949. [result appendFormat:@"YES: %lf", _values[1]];
  10950. }
  10951. [result appendString:@" }"];
  10952. return result;
  10953. }
  10954. - (NSUInteger)count {
  10955. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10956. }
  10957. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  10958. int idx = (key ? 1 : 0);
  10959. if (_valueSet[idx]) {
  10960. if (value) {
  10961. *value = _values[idx];
  10962. }
  10963. return YES;
  10964. }
  10965. return NO;
  10966. }
  10967. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  10968. int idx = (key->valueBool ? 1 : 0);
  10969. _values[idx] = value->valueDouble;
  10970. _valueSet[idx] = YES;
  10971. }
  10972. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10973. if (_valueSet[0]) {
  10974. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  10975. }
  10976. if (_valueSet[1]) {
  10977. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  10978. }
  10979. }
  10980. - (void)enumerateKeysAndValuesUsingBlock:
  10981. (void (^)(BOOL key, double value, BOOL *stop))block {
  10982. BOOL stop = NO;
  10983. if (_valueSet[0]) {
  10984. block(NO, _values[0], &stop);
  10985. }
  10986. if (!stop && _valueSet[1]) {
  10987. block(YES, _values[1], &stop);
  10988. }
  10989. }
  10990. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10991. GPBType valueType = GPBGetFieldType(field);
  10992. NSUInteger count = 0;
  10993. size_t result = 0;
  10994. for (int i = 0; i < 2; ++i) {
  10995. if (_valueSet[i]) {
  10996. ++count;
  10997. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  10998. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueType);
  10999. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11000. }
  11001. }
  11002. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  11003. result += tagSize * count;
  11004. return result;
  11005. }
  11006. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11007. asField:(GPBFieldDescriptor *)field {
  11008. GPBType valueType = GPBGetFieldType(field);
  11009. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11010. for (int i = 0; i < 2; ++i) {
  11011. if (_valueSet[i]) {
  11012. // Write the tag.
  11013. [outputStream writeInt32NoTag:tag];
  11014. // Write the size of the message.
  11015. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11016. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueType);
  11017. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11018. // Write the fields.
  11019. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11020. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  11021. }
  11022. }
  11023. }
  11024. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11025. if (otherDictionary) {
  11026. for (int i = 0; i < 2; ++i) {
  11027. if (otherDictionary->_valueSet[i]) {
  11028. _valueSet[i] = YES;
  11029. _values[i] = otherDictionary->_values[i];
  11030. }
  11031. }
  11032. if (_autocreator) {
  11033. GPBAutocreatedDictionaryModified(_autocreator, self);
  11034. }
  11035. }
  11036. }
  11037. - (void)setValue:(double)value forKey:(BOOL)key {
  11038. int idx = (key ? 1 : 0);
  11039. _values[idx] = value;
  11040. _valueSet[idx] = YES;
  11041. if (_autocreator) {
  11042. GPBAutocreatedDictionaryModified(_autocreator, self);
  11043. }
  11044. }
  11045. - (void)removeValueForKey:(BOOL)aKey {
  11046. _valueSet[aKey ? 1 : 0] = NO;
  11047. }
  11048. - (void)removeAll {
  11049. _valueSet[0] = NO;
  11050. _valueSet[1] = NO;
  11051. }
  11052. @end
  11053. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11054. // This block of code is generated, do not edit it directly.
  11055. #pragma mark - Bool -> Object
  11056. @implementation GPBBoolObjectDictionary {
  11057. @package
  11058. id _values[2];
  11059. }
  11060. + (instancetype)dictionary {
  11061. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  11062. }
  11063. + (instancetype)dictionaryWithValue:(id)value
  11064. forKey:(BOOL)key {
  11065. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11066. // on to get the type correct.
  11067. return [[(GPBBoolObjectDictionary*)[self alloc] initWithValues:&value
  11068. forKeys:&key
  11069. count:1] autorelease];
  11070. }
  11071. + (instancetype)dictionaryWithValues:(const id [])values
  11072. forKeys:(const BOOL [])keys
  11073. count:(NSUInteger)count {
  11074. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11075. // on to get the type correct.
  11076. return [[(GPBBoolObjectDictionary*)[self alloc] initWithValues:values
  11077. forKeys:keys
  11078. count:count] autorelease];
  11079. }
  11080. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11081. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11082. // on to get the type correct.
  11083. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11084. }
  11085. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11086. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11087. }
  11088. - (instancetype)init {
  11089. return [self initWithValues:NULL forKeys:NULL count:0];
  11090. }
  11091. - (instancetype)initWithValues:(const id [])values
  11092. forKeys:(const BOOL [])keys
  11093. count:(NSUInteger)count {
  11094. self = [super init];
  11095. if (self) {
  11096. for (NSUInteger i = 0; i < count; ++i) {
  11097. int idx = keys[i] ? 1 : 0;
  11098. [_values[idx] release];
  11099. _values[idx] = (id)[values[i] retain];
  11100. }
  11101. }
  11102. return self;
  11103. }
  11104. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11105. self = [self initWithValues:NULL forKeys:NULL count:0];
  11106. if (self) {
  11107. if (dictionary) {
  11108. _values[0] = [dictionary->_values[0] retain];
  11109. _values[1] = [dictionary->_values[1] retain];
  11110. }
  11111. }
  11112. return self;
  11113. }
  11114. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11115. #pragma unused(numItems)
  11116. return [self initWithValues:NULL forKeys:NULL count:0];
  11117. }
  11118. - (void)dealloc {
  11119. NSAssert(!_autocreator,
  11120. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11121. [self class], _autocreator);
  11122. [_values[0] release];
  11123. [_values[1] release];
  11124. [super dealloc];
  11125. }
  11126. - (instancetype)copyWithZone:(NSZone *)zone {
  11127. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11128. }
  11129. - (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
  11130. if (self == other) {
  11131. return YES;
  11132. }
  11133. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11134. return NO;
  11135. }
  11136. if (((_values[0] != nil) != (other->_values[0] != nil)) ||
  11137. ((_values[1] != nil) != (other->_values[1] != nil))) {
  11138. return NO;
  11139. }
  11140. if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
  11141. ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
  11142. return NO;
  11143. }
  11144. return YES;
  11145. }
  11146. - (NSUInteger)hash {
  11147. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11148. }
  11149. - (NSString *)description {
  11150. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11151. if ((_values[0] != nil)) {
  11152. [result appendFormat:@"NO: %@", _values[0]];
  11153. }
  11154. if ((_values[1] != nil)) {
  11155. [result appendFormat:@"YES: %@", _values[1]];
  11156. }
  11157. [result appendString:@" }"];
  11158. return result;
  11159. }
  11160. - (NSUInteger)count {
  11161. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11162. }
  11163. - (id)valueForKey:(BOOL)key {
  11164. return _values[key ? 1 : 0];
  11165. }
  11166. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  11167. int idx = (key->valueBool ? 1 : 0);
  11168. [_values[idx] release];
  11169. _values[idx] = [value->valueString retain];
  11170. }
  11171. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11172. if (_values[0] != nil) {
  11173. block(@"false", _values[0]);
  11174. }
  11175. if ((_values[1] != nil)) {
  11176. block(@"true", _values[1]);
  11177. }
  11178. }
  11179. - (void)enumerateKeysAndValuesUsingBlock:
  11180. (void (^)(BOOL key, id value, BOOL *stop))block {
  11181. BOOL stop = NO;
  11182. if (_values[0] != nil) {
  11183. block(NO, _values[0], &stop);
  11184. }
  11185. if (!stop && (_values[1] != nil)) {
  11186. block(YES, _values[1], &stop);
  11187. }
  11188. }
  11189. - (BOOL)isInitialized {
  11190. if (_values[0] && ![_values[0] isInitialized]) {
  11191. return NO;
  11192. }
  11193. if (_values[1] && ![_values[1] isInitialized]) {
  11194. return NO;
  11195. }
  11196. return YES;
  11197. }
  11198. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11199. GPBBoolObjectDictionary *newDict =
  11200. [[GPBBoolObjectDictionary alloc] init];
  11201. for (int i = 0; i < 2; ++i) {
  11202. if (_values[i] != nil) {
  11203. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11204. }
  11205. }
  11206. return newDict;
  11207. }
  11208. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11209. GPBType valueType = GPBGetFieldType(field);
  11210. NSUInteger count = 0;
  11211. size_t result = 0;
  11212. for (int i = 0; i < 2; ++i) {
  11213. if (_values[i] != nil) {
  11214. ++count;
  11215. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11216. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueType);
  11217. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11218. }
  11219. }
  11220. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  11221. result += tagSize * count;
  11222. return result;
  11223. }
  11224. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11225. asField:(GPBFieldDescriptor *)field {
  11226. GPBType valueType = GPBGetFieldType(field);
  11227. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11228. for (int i = 0; i < 2; ++i) {
  11229. if (_values[i] != nil) {
  11230. // Write the tag.
  11231. [outputStream writeInt32NoTag:tag];
  11232. // Write the size of the message.
  11233. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11234. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueType);
  11235. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11236. // Write the fields.
  11237. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11238. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueType);
  11239. }
  11240. }
  11241. }
  11242. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11243. if (otherDictionary) {
  11244. for (int i = 0; i < 2; ++i) {
  11245. if (otherDictionary->_values[i] != nil) {
  11246. [_values[i] release];
  11247. _values[i] = [otherDictionary->_values[i] retain];
  11248. }
  11249. }
  11250. if (_autocreator) {
  11251. GPBAutocreatedDictionaryModified(_autocreator, self);
  11252. }
  11253. }
  11254. }
  11255. - (void)setValue:(id)value forKey:(BOOL)key {
  11256. int idx = (key ? 1 : 0);
  11257. [_values[idx] release];
  11258. _values[idx] = [value retain];
  11259. if (_autocreator) {
  11260. GPBAutocreatedDictionaryModified(_autocreator, self);
  11261. }
  11262. }
  11263. - (void)removeValueForKey:(BOOL)aKey {
  11264. int idx = (aKey ? 1 : 0);
  11265. [_values[idx] release];
  11266. _values[idx] = nil;
  11267. }
  11268. - (void)removeAll {
  11269. for (int i = 0; i < 2; ++i) {
  11270. [_values[i] release];
  11271. _values[i] = nil;
  11272. }
  11273. }
  11274. @end
  11275. //%PDDM-EXPAND-END (8 expansions)
  11276. #pragma mark - Bool -> Enum
  11277. @implementation GPBBoolEnumDictionary {
  11278. @package
  11279. GPBEnumValidationFunc _validationFunc;
  11280. int32_t _values[2];
  11281. BOOL _valueSet[2];
  11282. }
  11283. @synthesize validationFunc = _validationFunc;
  11284. + (instancetype)dictionary {
  11285. return [[[self alloc] initWithValidationFunction:NULL
  11286. rawValues:NULL
  11287. forKeys:NULL
  11288. count:0] autorelease];
  11289. }
  11290. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11291. return [[[self alloc] initWithValidationFunction:func
  11292. rawValues:NULL
  11293. forKeys:NULL
  11294. count:0] autorelease];
  11295. }
  11296. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11297. rawValue:(int32_t)rawValue
  11298. forKey:(BOOL)key {
  11299. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11300. // on to get the type correct.
  11301. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11302. rawValues:&rawValue
  11303. forKeys:&key
  11304. count:1] autorelease];
  11305. }
  11306. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11307. rawValues:(const int32_t [])values
  11308. forKeys:(const BOOL [])keys
  11309. count:(NSUInteger)count {
  11310. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11311. // on to get the type correct.
  11312. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11313. rawValues:values
  11314. forKeys:keys
  11315. count:count] autorelease];
  11316. }
  11317. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11318. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11319. // on to get the type correct.
  11320. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11321. }
  11322. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11323. capacity:(NSUInteger)numItems {
  11324. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11325. }
  11326. - (instancetype)init {
  11327. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11328. }
  11329. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11330. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11331. }
  11332. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11333. rawValues:(const int32_t [])rawValues
  11334. forKeys:(const BOOL [])keys
  11335. count:(NSUInteger)count {
  11336. self = [super init];
  11337. if (self) {
  11338. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11339. for (NSUInteger i = 0; i < count; ++i) {
  11340. int idx = keys[i] ? 1 : 0;
  11341. _values[idx] = rawValues[i];
  11342. _valueSet[idx] = YES;
  11343. }
  11344. }
  11345. return self;
  11346. }
  11347. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11348. self = [self initWithValidationFunction:dictionary.validationFunc
  11349. rawValues:NULL
  11350. forKeys:NULL
  11351. count:0];
  11352. if (self) {
  11353. if (dictionary) {
  11354. for (int i = 0; i < 2; ++i) {
  11355. if (dictionary->_valueSet[i]) {
  11356. _values[i] = dictionary->_values[i];
  11357. _valueSet[i] = YES;
  11358. }
  11359. }
  11360. }
  11361. }
  11362. return self;
  11363. }
  11364. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11365. capacity:(NSUInteger)numItems {
  11366. #pragma unused(numItems)
  11367. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11368. }
  11369. #if !defined(NS_BLOCK_ASSERTIONS)
  11370. - (void)dealloc {
  11371. NSAssert(!_autocreator,
  11372. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11373. [self class], _autocreator);
  11374. [super dealloc];
  11375. }
  11376. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11377. - (instancetype)copyWithZone:(NSZone *)zone {
  11378. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11379. }
  11380. - (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
  11381. if (self == other) {
  11382. return YES;
  11383. }
  11384. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11385. return NO;
  11386. }
  11387. if ((_valueSet[0] != other->_valueSet[0]) ||
  11388. (_valueSet[1] != other->_valueSet[1])) {
  11389. return NO;
  11390. }
  11391. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11392. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11393. return NO;
  11394. }
  11395. return YES;
  11396. }
  11397. - (NSUInteger)hash {
  11398. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11399. }
  11400. - (NSString *)description {
  11401. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11402. if (_valueSet[0]) {
  11403. [result appendFormat:@"NO: %d", _values[0]];
  11404. }
  11405. if (_valueSet[1]) {
  11406. [result appendFormat:@"YES: %d", _values[1]];
  11407. }
  11408. [result appendString:@" }"];
  11409. return result;
  11410. }
  11411. - (NSUInteger)count {
  11412. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11413. }
  11414. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  11415. int idx = (key ? 1 : 0);
  11416. if (_valueSet[idx]) {
  11417. if (value) {
  11418. int32_t result = _values[idx];
  11419. if (!_validationFunc(result)) {
  11420. result = kGPBUnrecognizedEnumeratorValue;
  11421. }
  11422. *value = result;
  11423. }
  11424. return YES;
  11425. }
  11426. return NO;
  11427. }
  11428. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  11429. int idx = (key ? 1 : 0);
  11430. if (_valueSet[idx]) {
  11431. if (rawValue) {
  11432. *rawValue = _values[idx];
  11433. }
  11434. return YES;
  11435. }
  11436. return NO;
  11437. }
  11438. - (void)enumerateKeysAndValuesUsingBlock:
  11439. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11440. BOOL stop = NO;
  11441. if (_valueSet[0]) {
  11442. block(NO, _values[0], &stop);
  11443. }
  11444. if (!stop && _valueSet[1]) {
  11445. block(YES, _values[1], &stop);
  11446. }
  11447. }
  11448. - (void)enumerateKeysAndRawValuesUsingBlock:
  11449. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11450. BOOL stop = NO;
  11451. GPBEnumValidationFunc func = _validationFunc;
  11452. int32_t validatedValue;
  11453. if (_valueSet[0]) {
  11454. validatedValue = _values[0];
  11455. if (!func(validatedValue)) {
  11456. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11457. }
  11458. block(NO, validatedValue, &stop);
  11459. }
  11460. if (!stop && _valueSet[1]) {
  11461. validatedValue = _values[1];
  11462. if (!func(validatedValue)) {
  11463. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11464. }
  11465. block(YES, validatedValue, &stop);
  11466. }
  11467. }
  11468. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11469. // This block of code is generated, do not edit it directly.
  11470. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11471. forKey:(GPBValue *)key
  11472. keyType:(GPBType)keyType {
  11473. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyType);
  11474. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBTypeEnum);
  11475. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11476. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11477. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyType);
  11478. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBTypeEnum);
  11479. [outputStream release];
  11480. return data;
  11481. }
  11482. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11483. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11484. GPBType valueType = GPBGetFieldType(field);
  11485. NSUInteger count = 0;
  11486. size_t result = 0;
  11487. for (int i = 0; i < 2; ++i) {
  11488. if (_valueSet[i]) {
  11489. ++count;
  11490. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11491. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  11492. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11493. }
  11494. }
  11495. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBTypeMessage);
  11496. result += tagSize * count;
  11497. return result;
  11498. }
  11499. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11500. asField:(GPBFieldDescriptor *)field {
  11501. GPBType valueType = GPBGetFieldType(field);
  11502. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11503. for (int i = 0; i < 2; ++i) {
  11504. if (_valueSet[i]) {
  11505. // Write the tag.
  11506. [outputStream writeInt32NoTag:tag];
  11507. // Write the size of the message.
  11508. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11509. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueType);
  11510. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11511. // Write the fields.
  11512. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBTypeBool);
  11513. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueType);
  11514. }
  11515. }
  11516. }
  11517. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11518. if (_valueSet[0]) {
  11519. block(@"false", @(_values[0]));
  11520. }
  11521. if (_valueSet[1]) {
  11522. block(@"true", @(_values[1]));
  11523. }
  11524. }
  11525. - (void)setGPBValue:(GPBValue *)value forGPBValueKey:(GPBValue *)key {
  11526. int idx = (key->valueBool ? 1 : 0);
  11527. _values[idx] = value->valueInt32;
  11528. _valueSet[idx] = YES;
  11529. }
  11530. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11531. if (otherDictionary) {
  11532. for (int i = 0; i < 2; ++i) {
  11533. if (otherDictionary->_valueSet[i]) {
  11534. _valueSet[i] = YES;
  11535. _values[i] = otherDictionary->_values[i];
  11536. }
  11537. }
  11538. if (_autocreator) {
  11539. GPBAutocreatedDictionaryModified(_autocreator, self);
  11540. }
  11541. }
  11542. }
  11543. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  11544. if (!_validationFunc(value)) {
  11545. [NSException raise:NSInvalidArgumentException
  11546. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11547. value];
  11548. }
  11549. int idx = (key ? 1 : 0);
  11550. _values[idx] = value;
  11551. _valueSet[idx] = YES;
  11552. if (_autocreator) {
  11553. GPBAutocreatedDictionaryModified(_autocreator, self);
  11554. }
  11555. }
  11556. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11557. int idx = (key ? 1 : 0);
  11558. _values[idx] = rawValue;
  11559. _valueSet[idx] = YES;
  11560. if (_autocreator) {
  11561. GPBAutocreatedDictionaryModified(_autocreator, self);
  11562. }
  11563. }
  11564. - (void)removeValueForKey:(BOOL)aKey {
  11565. _valueSet[aKey ? 1 : 0] = NO;
  11566. }
  11567. - (void)removeAll {
  11568. _valueSet[0] = NO;
  11569. _valueSet[1] = NO;
  11570. }
  11571. @end
  11572. #pragma mark - NSDictionary Subclass
  11573. @implementation GPBAutocreatedDictionary {
  11574. NSMutableDictionary *_dictionary;
  11575. }
  11576. - (void)dealloc {
  11577. NSAssert(!_autocreator,
  11578. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11579. [self class], _autocreator);
  11580. [_dictionary release];
  11581. [super dealloc];
  11582. }
  11583. #pragma mark Required NSDictionary overrides
  11584. - (instancetype)initWithObjects:(const id [])objects
  11585. forKeys:(const id<NSCopying> [])keys
  11586. count:(NSUInteger)count {
  11587. self = [super init];
  11588. if (self) {
  11589. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11590. forKeys:keys
  11591. count:count];
  11592. }
  11593. return self;
  11594. }
  11595. - (NSUInteger)count {
  11596. return [_dictionary count];
  11597. }
  11598. - (id)objectForKey:(id)aKey {
  11599. return [_dictionary objectForKey:aKey];
  11600. }
  11601. - (NSEnumerator *)keyEnumerator {
  11602. if (_dictionary == nil) {
  11603. _dictionary = [[NSMutableDictionary alloc] init];
  11604. }
  11605. return [_dictionary keyEnumerator];
  11606. }
  11607. #pragma mark Required NSMutableDictionary overrides
  11608. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11609. // since we only autocreate empty dictionaries.
  11610. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11611. if (_dictionary == nil) {
  11612. _dictionary = [[NSMutableDictionary alloc] init];
  11613. }
  11614. [_dictionary setObject:anObject forKey:aKey];
  11615. if (_autocreator) {
  11616. GPBAutocreatedDictionaryModified(_autocreator, self);
  11617. }
  11618. }
  11619. - (void)removeObjectForKey:(id)aKey {
  11620. [_dictionary removeObjectForKey:aKey];
  11621. }
  11622. #pragma mark Extra things hooked
  11623. - (id)copyWithZone:(NSZone *)zone {
  11624. if (_dictionary == nil) {
  11625. _dictionary = [[NSMutableDictionary alloc] init];
  11626. }
  11627. return [_dictionary copyWithZone:zone];
  11628. }
  11629. - (id)mutableCopyWithZone:(NSZone *)zone {
  11630. if (_dictionary == nil) {
  11631. _dictionary = [[NSMutableDictionary alloc] init];
  11632. }
  11633. return [_dictionary mutableCopyWithZone:zone];
  11634. }
  11635. - (id)objectForKeyedSubscript:(id)key {
  11636. return [_dictionary objectForKeyedSubscript:key];
  11637. }
  11638. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11639. if (_dictionary == nil) {
  11640. _dictionary = [[NSMutableDictionary alloc] init];
  11641. }
  11642. [_dictionary setObject:obj forKeyedSubscript:key];
  11643. if (_autocreator) {
  11644. GPBAutocreatedDictionaryModified(_autocreator, self);
  11645. }
  11646. }
  11647. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11648. id obj,
  11649. BOOL *stop))block {
  11650. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11651. }
  11652. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11653. usingBlock:(void (^)(id key,
  11654. id obj,
  11655. BOOL *stop))block {
  11656. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11657. }
  11658. @end