GPBDictionary.m 466 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBDictionary_PackagePrivate.h"
  31. #import "GPBCodedInputStream_PackagePrivate.h"
  32. #import "GPBCodedOutputStream_PackagePrivate.h"
  33. #import "GPBDescriptor_PackagePrivate.h"
  34. #import "GPBMessage_PackagePrivate.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. // ------------------------------ NOTE ------------------------------
  37. // At the moment, this is all using NSNumbers in NSDictionaries under
  38. // the hood, but it is all hidden so we can come back and optimize
  39. // with direct CFDictionary usage later. The reason that wasn't
  40. // done yet is needing to support 32bit iOS builds. Otherwise
  41. // it would be pretty simple to store all this data in CFDictionaries
  42. // directly.
  43. // ------------------------------------------------------------------
  44. // Direct access is use for speed, to avoid even internally declaring things
  45. // read/write, etc. The warning is enabled in the project to ensure code calling
  46. // protos can turn on -Wdirect-ivar-access without issues.
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  49. // Used to include code only visible to specific versions of the static
  50. // analyzer. Useful for wrapping code that only exists to silence the analyzer.
  51. // Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
  52. // END_APPLE_BUILD_VERSION using:
  53. // xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
  54. // Example usage:
  55. // #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
  56. #define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
  57. (defined(__clang_analyzer__) && \
  58. (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
  59. __apple_build_version__ <= END_APPLE_BUILD_VERSION))
  60. enum {
  61. kMapKeyFieldNumber = 1,
  62. kMapValueFieldNumber = 2,
  63. };
  64. static BOOL DictDefault_IsValidValue(int32_t value) {
  65. // Anything but the bad value marker is allowed.
  66. return (value != kGPBUnrecognizedEnumeratorValue);
  67. }
  68. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  69. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  70. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  71. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  72. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  73. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  74. //% } else {
  75. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  76. //% return 0;
  77. //% }
  78. //%}
  79. //%
  80. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  81. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  82. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  83. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  84. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  85. //% } else {
  86. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  87. //% }
  88. //%}
  89. //%
  90. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  91. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  92. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  93. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  94. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  95. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  96. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  97. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  98. //% } else {
  99. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  100. //% return 0;
  101. //% }
  102. //%}
  103. //%
  104. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  105. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  106. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  107. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  108. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  109. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  110. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  111. //% } else {
  112. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  113. //% }
  114. //%}
  115. //%
  116. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  117. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  118. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  119. //% #pragma unused(dataType) // For when asserts are off in release.
  120. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  121. //%}
  122. //%
  123. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  124. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  125. //% #pragma unused(dataType) // For when asserts are off in release.
  126. //% [stream write##VALUE_NAME##:fieldNum value:value];
  127. //%}
  128. //%
  129. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  130. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  131. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  132. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  133. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  134. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  135. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  136. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  137. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  138. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  139. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  140. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  141. // This block of code is generated, do not edit it directly.
  142. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  143. if (dataType == GPBDataTypeInt32) {
  144. return GPBComputeInt32Size(fieldNum, value);
  145. } else if (dataType == GPBDataTypeSInt32) {
  146. return GPBComputeSInt32Size(fieldNum, value);
  147. } else if (dataType == GPBDataTypeSFixed32) {
  148. return GPBComputeSFixed32Size(fieldNum, value);
  149. } else {
  150. NSCAssert(NO, @"Unexpected type %d", dataType);
  151. return 0;
  152. }
  153. }
  154. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  155. if (dataType == GPBDataTypeInt32) {
  156. [stream writeInt32:fieldNum value:value];
  157. } else if (dataType == GPBDataTypeSInt32) {
  158. [stream writeSInt32:fieldNum value:value];
  159. } else if (dataType == GPBDataTypeSFixed32) {
  160. [stream writeSFixed32:fieldNum value:value];
  161. } else {
  162. NSCAssert(NO, @"Unexpected type %d", dataType);
  163. }
  164. }
  165. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  166. if (dataType == GPBDataTypeUInt32) {
  167. return GPBComputeUInt32Size(fieldNum, value);
  168. } else if (dataType == GPBDataTypeFixed32) {
  169. return GPBComputeFixed32Size(fieldNum, value);
  170. } else {
  171. NSCAssert(NO, @"Unexpected type %d", dataType);
  172. return 0;
  173. }
  174. }
  175. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  176. if (dataType == GPBDataTypeUInt32) {
  177. [stream writeUInt32:fieldNum value:value];
  178. } else if (dataType == GPBDataTypeFixed32) {
  179. [stream writeFixed32:fieldNum value:value];
  180. } else {
  181. NSCAssert(NO, @"Unexpected type %d", dataType);
  182. }
  183. }
  184. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  185. if (dataType == GPBDataTypeInt64) {
  186. return GPBComputeInt64Size(fieldNum, value);
  187. } else if (dataType == GPBDataTypeSInt64) {
  188. return GPBComputeSInt64Size(fieldNum, value);
  189. } else if (dataType == GPBDataTypeSFixed64) {
  190. return GPBComputeSFixed64Size(fieldNum, value);
  191. } else {
  192. NSCAssert(NO, @"Unexpected type %d", dataType);
  193. return 0;
  194. }
  195. }
  196. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  197. if (dataType == GPBDataTypeInt64) {
  198. [stream writeInt64:fieldNum value:value];
  199. } else if (dataType == GPBDataTypeSInt64) {
  200. [stream writeSInt64:fieldNum value:value];
  201. } else if (dataType == GPBDataTypeSFixed64) {
  202. [stream writeSFixed64:fieldNum value:value];
  203. } else {
  204. NSCAssert(NO, @"Unexpected type %d", dataType);
  205. }
  206. }
  207. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  208. if (dataType == GPBDataTypeUInt64) {
  209. return GPBComputeUInt64Size(fieldNum, value);
  210. } else if (dataType == GPBDataTypeFixed64) {
  211. return GPBComputeFixed64Size(fieldNum, value);
  212. } else {
  213. NSCAssert(NO, @"Unexpected type %d", dataType);
  214. return 0;
  215. }
  216. }
  217. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  218. if (dataType == GPBDataTypeUInt64) {
  219. [stream writeUInt64:fieldNum value:value];
  220. } else if (dataType == GPBDataTypeFixed64) {
  221. [stream writeFixed64:fieldNum value:value];
  222. } else {
  223. NSCAssert(NO, @"Unexpected type %d", dataType);
  224. }
  225. }
  226. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  227. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  228. #pragma unused(dataType) // For when asserts are off in release.
  229. return GPBComputeBoolSize(fieldNum, value);
  230. }
  231. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  232. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  233. #pragma unused(dataType) // For when asserts are off in release.
  234. [stream writeBool:fieldNum value:value];
  235. }
  236. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  237. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  238. #pragma unused(dataType) // For when asserts are off in release.
  239. return GPBComputeEnumSize(fieldNum, value);
  240. }
  241. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  242. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  243. #pragma unused(dataType) // For when asserts are off in release.
  244. [stream writeEnum:fieldNum value:value];
  245. }
  246. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  247. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  248. #pragma unused(dataType) // For when asserts are off in release.
  249. return GPBComputeFloatSize(fieldNum, value);
  250. }
  251. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
  252. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  253. #pragma unused(dataType) // For when asserts are off in release.
  254. [stream writeFloat:fieldNum value:value];
  255. }
  256. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  257. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  258. #pragma unused(dataType) // For when asserts are off in release.
  259. return GPBComputeDoubleSize(fieldNum, value);
  260. }
  261. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
  262. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  263. #pragma unused(dataType) // For when asserts are off in release.
  264. [stream writeDouble:fieldNum value:value];
  265. }
  266. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  267. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  268. #pragma unused(dataType) // For when asserts are off in release.
  269. return GPBComputeStringSize(fieldNum, value);
  270. }
  271. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  272. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  273. #pragma unused(dataType) // For when asserts are off in release.
  274. [stream writeString:fieldNum value:value];
  275. }
  276. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  277. if (dataType == GPBDataTypeMessage) {
  278. return GPBComputeMessageSize(fieldNum, value);
  279. } else if (dataType == GPBDataTypeString) {
  280. return GPBComputeStringSize(fieldNum, value);
  281. } else if (dataType == GPBDataTypeBytes) {
  282. return GPBComputeBytesSize(fieldNum, value);
  283. } else {
  284. NSCAssert(NO, @"Unexpected type %d", dataType);
  285. return 0;
  286. }
  287. }
  288. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  289. if (dataType == GPBDataTypeMessage) {
  290. [stream writeMessage:fieldNum value:value];
  291. } else if (dataType == GPBDataTypeString) {
  292. [stream writeString:fieldNum value:value];
  293. } else if (dataType == GPBDataTypeBytes) {
  294. [stream writeBytes:fieldNum value:value];
  295. } else {
  296. NSCAssert(NO, @"Unexpected type %d", dataType);
  297. }
  298. }
  299. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  300. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  301. GPBDataType mapValueType = GPBGetFieldDataType(field);
  302. __block size_t result = 0;
  303. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  304. #pragma unused(stop)
  305. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  306. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  307. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  308. }];
  309. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  310. result += tagSize * dict.count;
  311. return result;
  312. }
  313. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  314. NSDictionary *dict,
  315. GPBFieldDescriptor *field) {
  316. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  317. GPBDataType mapValueType = GPBGetFieldDataType(field);
  318. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  319. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  320. #pragma unused(stop)
  321. // Write the tag.
  322. [outputStream writeInt32NoTag:tag];
  323. // Write the size of the message.
  324. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  325. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  326. // Write the size and fields.
  327. [outputStream writeInt32NoTag:(int32_t)msgSize];
  328. [outputStream writeString:kMapKeyFieldNumber value:key];
  329. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  330. }];
  331. }
  332. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  333. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  334. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  335. #pragma unused(field) // For when asserts are off in release.
  336. for (GPBMessage *msg in [dict objectEnumerator]) {
  337. if (!msg.initialized) {
  338. return NO;
  339. }
  340. }
  341. return YES;
  342. }
  343. // Note: if the type is an object, it the retain pass back to the caller.
  344. static void ReadValue(GPBCodedInputStream *stream,
  345. GPBGenericValue *valueToFill,
  346. GPBDataType type,
  347. GPBExtensionRegistry *registry,
  348. GPBFieldDescriptor *field) {
  349. switch (type) {
  350. case GPBDataTypeBool:
  351. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  352. break;
  353. case GPBDataTypeFixed32:
  354. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  355. break;
  356. case GPBDataTypeSFixed32:
  357. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  358. break;
  359. case GPBDataTypeFloat:
  360. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  361. break;
  362. case GPBDataTypeFixed64:
  363. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  364. break;
  365. case GPBDataTypeSFixed64:
  366. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  367. break;
  368. case GPBDataTypeDouble:
  369. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  370. break;
  371. case GPBDataTypeInt32:
  372. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  373. break;
  374. case GPBDataTypeInt64:
  375. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  376. break;
  377. case GPBDataTypeSInt32:
  378. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  379. break;
  380. case GPBDataTypeSInt64:
  381. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  382. break;
  383. case GPBDataTypeUInt32:
  384. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  385. break;
  386. case GPBDataTypeUInt64:
  387. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  388. break;
  389. case GPBDataTypeBytes:
  390. [valueToFill->valueData release];
  391. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  392. break;
  393. case GPBDataTypeString:
  394. [valueToFill->valueString release];
  395. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  396. break;
  397. case GPBDataTypeMessage: {
  398. GPBMessage *message = [[field.msgClass alloc] init];
  399. [stream readMessage:message extensionRegistry:registry];
  400. [valueToFill->valueMessage release];
  401. valueToFill->valueMessage = message;
  402. break;
  403. }
  404. case GPBDataTypeGroup:
  405. NSCAssert(NO, @"Can't happen");
  406. break;
  407. case GPBDataTypeEnum:
  408. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  409. break;
  410. }
  411. }
  412. void GPBDictionaryReadEntry(id mapDictionary,
  413. GPBCodedInputStream *stream,
  414. GPBExtensionRegistry *registry,
  415. GPBFieldDescriptor *field,
  416. GPBMessage *parentMessage) {
  417. GPBDataType keyDataType = field.mapKeyDataType;
  418. GPBDataType valueDataType = GPBGetFieldDataType(field);
  419. GPBGenericValue key;
  420. GPBGenericValue value;
  421. // Zero them (but pick up any enum default for proto2).
  422. key.valueString = value.valueString = nil;
  423. if (valueDataType == GPBDataTypeEnum) {
  424. value = field.defaultValue;
  425. }
  426. GPBCodedInputStreamState *state = &stream->state_;
  427. uint32_t keyTag =
  428. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  429. uint32_t valueTag =
  430. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  431. BOOL hitError = NO;
  432. while (YES) {
  433. uint32_t tag = GPBCodedInputStreamReadTag(state);
  434. if (tag == keyTag) {
  435. ReadValue(stream, &key, keyDataType, registry, field);
  436. } else if (tag == valueTag) {
  437. ReadValue(stream, &value, valueDataType, registry, field);
  438. } else if (tag == 0) {
  439. // zero signals EOF / limit reached
  440. break;
  441. } else { // Unknown
  442. if (![stream skipField:tag]){
  443. hitError = YES;
  444. break;
  445. }
  446. }
  447. }
  448. if (!hitError) {
  449. // Handle the special defaults and/or missing key/value.
  450. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  451. key.valueString = [@"" retain];
  452. }
  453. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  454. #pragma clang diagnostic push
  455. #pragma clang diagnostic ignored "-Wswitch-enum"
  456. switch (valueDataType) {
  457. case GPBDataTypeString:
  458. value.valueString = [@"" retain];
  459. break;
  460. case GPBDataTypeBytes:
  461. value.valueData = [GPBEmptyNSData() retain];
  462. break;
  463. #if defined(__clang_analyzer__)
  464. case GPBDataTypeGroup:
  465. // Maps can't really have Groups as the value type, but this case is needed
  466. // so the analyzer won't report the posibility of send nil in for the value
  467. // in the NSMutableDictionary case below.
  468. #endif
  469. case GPBDataTypeMessage: {
  470. value.valueMessage = [[field.msgClass alloc] init];
  471. break;
  472. }
  473. default:
  474. // Nothing
  475. break;
  476. }
  477. #pragma clang diagnostic pop
  478. }
  479. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  480. #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
  481. // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
  482. // be raised as needed for new Xcodes.
  483. //
  484. // This is only needed on a "shallow" analyze; on a "deep" analyze, the
  485. // existing code path gets this correct. In shallow, the analyzer decides
  486. // GPBDataTypeIsObject(valueDataType) is both false and true on a single
  487. // path through this function, allowing nil to be used for the
  488. // setObject:forKey:.
  489. if (value.valueString == nil) {
  490. value.valueString = [@"" retain];
  491. }
  492. #endif
  493. // mapDictionary is an NSMutableDictionary
  494. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  495. forKey:key.valueString];
  496. } else {
  497. if (valueDataType == GPBDataTypeEnum) {
  498. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  499. [field isValidEnumValue:value.valueEnum]) {
  500. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  501. } else {
  502. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  503. forKey:&key
  504. keyDataType:keyDataType];
  505. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  506. }
  507. } else {
  508. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  509. }
  510. }
  511. }
  512. if (GPBDataTypeIsObject(keyDataType)) {
  513. [key.valueString release];
  514. }
  515. if (GPBDataTypeIsObject(valueDataType)) {
  516. [value.valueString release];
  517. }
  518. }
  519. //
  520. // Macros for the common basic cases.
  521. //
  522. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  523. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  524. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  525. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  526. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  527. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  528. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  529. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  530. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  531. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  532. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  533. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  534. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  535. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, value)
  536. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  537. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, object)
  538. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME)
  539. //%#pragma mark - KEY_NAME -> VALUE_NAME
  540. //%
  541. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  542. //% @package
  543. //% NSMutableDictionary *_dictionary;
  544. //%}
  545. //%
  546. //%+ (instancetype)dictionary {
  547. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  548. //%}
  549. //%
  550. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)##VNAME
  551. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  552. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  553. //% // on to get the type correct.
  554. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  555. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  556. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  557. //%}
  558. //%
  559. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  560. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  561. //% ##VNAME$S## count:(NSUInteger)count {
  562. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  563. //% // on to get the type correct.
  564. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  565. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  566. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  567. //%}
  568. //%
  569. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  570. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  571. //% // on to get the type correct.
  572. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  573. //%}
  574. //%
  575. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  576. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  577. //%}
  578. //%
  579. //%- (instancetype)init {
  580. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  581. //%}
  582. //%
  583. //%- (instancetype)initWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  584. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  585. //% ##VNAME$S## count:(NSUInteger)count {
  586. //% self = [super init];
  587. //% if (self) {
  588. //% _dictionary = [[NSMutableDictionary alloc] init];
  589. //% if (count && VNAME##s && keys) {
  590. //% for (NSUInteger i = 0; i < count; ++i) {
  591. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
  592. //% }
  593. //% }
  594. //% }
  595. //% return self;
  596. //%}
  597. //%
  598. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  599. //% self = [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  600. //% if (self) {
  601. //% if (dictionary) {
  602. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  603. //% }
  604. //% }
  605. //% return self;
  606. //%}
  607. //%
  608. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  609. //% #pragma unused(numItems)
  610. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  611. //%}
  612. //%
  613. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  614. //%
  615. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  616. //%
  617. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  618. //%
  619. //%@end
  620. //%
  621. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  622. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  623. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  624. //%#pragma mark - KEY_NAME -> VALUE_NAME
  625. //%
  626. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  627. //% @package
  628. //% NSMutableDictionary *_dictionary;
  629. //% GPBEnumValidationFunc _validationFunc;
  630. //%}
  631. //%
  632. //%@synthesize validationFunc = _validationFunc;
  633. //%
  634. //%+ (instancetype)dictionary {
  635. //% return [[[self alloc] initWithValidationFunction:NULL
  636. //% rawValues:NULL
  637. //% forKeys:NULL
  638. //% count:0] autorelease];
  639. //%}
  640. //%
  641. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  642. //% return [[[self alloc] initWithValidationFunction:func
  643. //% rawValues:NULL
  644. //% forKeys:NULL
  645. //% count:0] autorelease];
  646. //%}
  647. //%
  648. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  649. //% rawValue:(VALUE_TYPE)rawValue
  650. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  651. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  652. //% // on to get the type correct.
  653. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  654. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  655. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  656. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  657. //%}
  658. //%
  659. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  660. //% rawValues:(const VALUE_TYPE [])rawValues
  661. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  662. //% count:(NSUInteger)count {
  663. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  664. //% // on to get the type correct.
  665. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  666. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  667. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  668. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  669. //%}
  670. //%
  671. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  672. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  673. //% // on to get the type correct.
  674. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  675. //%}
  676. //%
  677. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  678. //% capacity:(NSUInteger)numItems {
  679. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  680. //%}
  681. //%
  682. //%- (instancetype)init {
  683. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  684. //%}
  685. //%
  686. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  687. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  688. //%}
  689. //%
  690. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  691. //% rawValues:(const VALUE_TYPE [])rawValues
  692. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  693. //% count:(NSUInteger)count {
  694. //% self = [super init];
  695. //% if (self) {
  696. //% _dictionary = [[NSMutableDictionary alloc] init];
  697. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  698. //% if (count && rawValues && keys) {
  699. //% for (NSUInteger i = 0; i < count; ++i) {
  700. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  701. //% }
  702. //% }
  703. //% }
  704. //% return self;
  705. //%}
  706. //%
  707. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  708. //% self = [self initWithValidationFunction:dictionary.validationFunc
  709. //% rawValues:NULL
  710. //% forKeys:NULL
  711. //% count:0];
  712. //% if (self) {
  713. //% if (dictionary) {
  714. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  715. //% }
  716. //% }
  717. //% return self;
  718. //%}
  719. //%
  720. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  721. //% capacity:(NSUInteger)numItems {
  722. //% #pragma unused(numItems)
  723. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  724. //%}
  725. //%
  726. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  727. //%
  728. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  729. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  730. //% if (wrapped && value) {
  731. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  732. //% if (!_validationFunc(result)) {
  733. //% result = kGPBUnrecognizedEnumeratorValue;
  734. //% }
  735. //% *value = result;
  736. //% }
  737. //% return (wrapped != NULL);
  738. //%}
  739. //%
  740. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  741. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  742. //% if (wrapped && rawValue) {
  743. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  744. //% }
  745. //% return (wrapped != NULL);
  746. //%}
  747. //%
  748. //%- (void)enumerateKeysAndValuesUsingBlock:
  749. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  750. //% GPBEnumValidationFunc func = _validationFunc;
  751. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  752. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  753. //% BOOL *stop) {
  754. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  755. //% if (!func(unwrapped)) {
  756. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  757. //% }
  758. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  759. //% }];
  760. //%}
  761. //%
  762. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  763. //%
  764. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  765. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  766. //% [NSException raise:NSInvalidArgumentException
  767. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  768. //% value];
  769. //% }
  770. //%
  771. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  772. //% if (_autocreator) {
  773. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  774. //% }
  775. //%}
  776. //%
  777. //%@end
  778. //%
  779. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  780. //%- (void)dealloc {
  781. //% NSAssert(!_autocreator,
  782. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  783. //% [self class], _autocreator);
  784. //% [_dictionary release];
  785. //% [super dealloc];
  786. //%}
  787. //%
  788. //%- (instancetype)copyWithZone:(NSZone *)zone {
  789. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  790. //%}
  791. //%
  792. //%- (BOOL)isEqual:(id)other {
  793. //% if (self == other) {
  794. //% return YES;
  795. //% }
  796. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  797. //% return NO;
  798. //% }
  799. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  800. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  801. //%}
  802. //%
  803. //%- (NSUInteger)hash {
  804. //% return _dictionary.count;
  805. //%}
  806. //%
  807. //%- (NSString *)description {
  808. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  809. //%}
  810. //%
  811. //%- (NSUInteger)count {
  812. //% return _dictionary.count;
  813. //%}
  814. //%
  815. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:
  816. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop))block {
  817. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  818. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  819. //% BOOL *stop) {
  820. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME$u), stop);
  821. //% }];
  822. //%}
  823. //%
  824. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  825. //% NSUInteger count = _dictionary.count;
  826. //% if (count == 0) {
  827. //% return 0;
  828. //% }
  829. //%
  830. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  831. //% GPBDataType keyDataType = field.mapKeyDataType;
  832. //% __block size_t result = 0;
  833. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  834. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u##,
  835. //% BOOL *stop) {
  836. //% #pragma unused(stop)
  837. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  838. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  839. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  840. //% }];
  841. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  842. //% result += tagSize * count;
  843. //% return result;
  844. //%}
  845. //%
  846. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  847. //% asField:(GPBFieldDescriptor *)field {
  848. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  849. //% GPBDataType keyDataType = field.mapKeyDataType;
  850. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  851. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  852. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  853. //% BOOL *stop) {
  854. //% #pragma unused(stop)
  855. //% // Write the tag.
  856. //% [outputStream writeInt32NoTag:tag];
  857. //% // Write the size of the message.
  858. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  859. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  860. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  861. //% // Write the fields.
  862. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  863. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  864. //% }];
  865. //%}
  866. //%
  867. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  868. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  869. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  870. //%}
  871. //%
  872. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  873. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop) {
  874. //% #pragma unused(stop)
  875. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME));
  876. //% }];
  877. //%}
  878. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  879. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  880. //% if (otherDictionary) {
  881. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  882. //% if (_autocreator) {
  883. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  884. //% }
  885. //% }
  886. //%}
  887. //%
  888. //%- (void)set##ACCESSOR_NAME##VNAME$u##:(VALUE_TYPE)VNAME forKey:(KEY_TYPE##KisP$S##KisP)key {
  889. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)];
  890. //% if (_autocreator) {
  891. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  892. //% }
  893. //%}
  894. //%
  895. //%- (void)remove##VNAME$u##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  896. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  897. //%}
  898. //%
  899. //%- (void)removeAll {
  900. //% [_dictionary removeAllObjects];
  901. //%}
  902. //
  903. // Custom Generation for Bool keys
  904. //
  905. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  906. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, value)
  907. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  908. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, object)
  909. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME)
  910. //%#pragma mark - Bool -> VALUE_NAME
  911. //%
  912. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  913. //% @package
  914. //% VALUE_TYPE _values[2];
  915. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  916. //%
  917. //%+ (instancetype)dictionary {
  918. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  919. //%}
  920. //%
  921. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)VNAME
  922. //% ##VNAME$S## forKey:(BOOL)key {
  923. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  924. //% // on to get the type correct.
  925. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  926. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  927. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  928. //%}
  929. //%
  930. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  931. //% ##VNAME$S## forKeys:(const BOOL [])keys
  932. //% ##VNAME$S## count:(NSUInteger)count {
  933. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  934. //% // on to get the type correct.
  935. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  936. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  937. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  938. //%}
  939. //%
  940. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  941. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  942. //% // on to get the type correct.
  943. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  944. //%}
  945. //%
  946. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  947. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  948. //%}
  949. //%
  950. //%- (instancetype)init {
  951. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  952. //%}
  953. //%
  954. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  955. //%
  956. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  957. //% #pragma unused(numItems)
  958. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  959. //%}
  960. //%
  961. //%BOOL_DICT_DEALLOC##HELPER()
  962. //%
  963. //%- (instancetype)copyWithZone:(NSZone *)zone {
  964. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  965. //%}
  966. //%
  967. //%- (BOOL)isEqual:(id)other {
  968. //% if (self == other) {
  969. //% return YES;
  970. //% }
  971. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  972. //% return NO;
  973. //% }
  974. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  975. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  976. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  977. //% return NO;
  978. //% }
  979. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  980. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  981. //% return NO;
  982. //% }
  983. //% return YES;
  984. //%}
  985. //%
  986. //%- (NSUInteger)hash {
  987. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  988. //%}
  989. //%
  990. //%- (NSString *)description {
  991. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  992. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  993. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  994. //% }
  995. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  996. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  997. //% }
  998. //% [result appendString:@" }"];
  999. //% return result;
  1000. //%}
  1001. //%
  1002. //%- (NSUInteger)count {
  1003. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  1004. //%}
  1005. //%
  1006. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  1007. //%
  1008. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  1009. //%
  1010. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1011. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1012. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  1013. //% }
  1014. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  1015. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  1016. //% }
  1017. //%}
  1018. //%
  1019. //%- (void)enumerateKeysAnd##VNAME$u##sUsingBlock:
  1020. //% (void (^)(BOOL key, VALUE_TYPE VNAME, BOOL *stop))block {
  1021. //% BOOL stop = NO;
  1022. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1023. //% block(NO, _values[0], &stop);
  1024. //% }
  1025. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  1026. //% block(YES, _values[1], &stop);
  1027. //% }
  1028. //%}
  1029. //%
  1030. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1031. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1032. //% NSUInteger count = 0;
  1033. //% size_t result = 0;
  1034. //% for (int i = 0; i < 2; ++i) {
  1035. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1036. //% ++count;
  1037. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1038. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1039. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1040. //% }
  1041. //% }
  1042. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1043. //% result += tagSize * count;
  1044. //% return result;
  1045. //%}
  1046. //%
  1047. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1048. //% asField:(GPBFieldDescriptor *)field {
  1049. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1050. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1051. //% for (int i = 0; i < 2; ++i) {
  1052. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1053. //% // Write the tag.
  1054. //% [outputStream writeInt32NoTag:tag];
  1055. //% // Write the size of the message.
  1056. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1057. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1058. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1059. //% // Write the fields.
  1060. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1061. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1062. //% }
  1063. //% }
  1064. //%}
  1065. //%
  1066. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1067. //%
  1068. //%@end
  1069. //%
  1070. //
  1071. // Helpers for PODs
  1072. //
  1073. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1074. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  1075. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1076. //% if (wrapped && value) {
  1077. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1078. //% }
  1079. //% return (wrapped != NULL);
  1080. //%}
  1081. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1082. //%@(VALUE)
  1083. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1084. //%[VALUE unsignedIntValue]
  1085. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1086. //%[VALUE intValue]
  1087. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1088. //%[VALUE unsignedLongLongValue]
  1089. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1090. //%[VALUE longLongValue]
  1091. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1092. //%[VALUE boolValue]
  1093. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1094. //%[VALUE floatValue]
  1095. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1096. //%[VALUE doubleValue]
  1097. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1098. //%[VALUE intValue]
  1099. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1100. //%[NSString stringWithFormat:@"%u", VALUE]
  1101. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1102. //%[NSString stringWithFormat:@"%d", VALUE]
  1103. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1104. //%[NSString stringWithFormat:@"%llu", VALUE]
  1105. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1106. //%[NSString stringWithFormat:@"%lld", VALUE]
  1107. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1108. //%(VALUE ? @"true" : @"false")
  1109. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1110. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1111. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1112. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1113. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1114. //%@(VALUE)
  1115. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1116. //%NSNumber *
  1117. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1118. //%VAL1 != VAL2
  1119. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1120. // Empty
  1121. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1122. // Empty
  1123. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1124. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1125. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1126. // Empty
  1127. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1128. // Empty
  1129. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1130. // Empty
  1131. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1132. // Empty
  1133. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1134. // Empty
  1135. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1136. // Empty
  1137. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1138. // Empty
  1139. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1140. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1141. //% forKey:(GPBGenericValue *)key
  1142. //% keyDataType:(GPBDataType)keyDataType {
  1143. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1144. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1145. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1146. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1147. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1148. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1149. //% [outputStream release];
  1150. //% return data;
  1151. //%}
  1152. //%
  1153. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1154. //%value##VALUE_NAME
  1155. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1156. // Empty
  1157. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1158. // Empty
  1159. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1160. //% BOOL _valueSet[2];
  1161. //%
  1162. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1163. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1164. //% forKeys:(const BOOL [])keys
  1165. //% count:(NSUInteger)count {
  1166. //% self = [super init];
  1167. //% if (self) {
  1168. //% for (NSUInteger i = 0; i < count; ++i) {
  1169. //% int idx = keys[i] ? 1 : 0;
  1170. //% _values[idx] = values[i];
  1171. //% _valueSet[idx] = YES;
  1172. //% }
  1173. //% }
  1174. //% return self;
  1175. //%}
  1176. //%
  1177. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1178. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1179. //% if (self) {
  1180. //% if (dictionary) {
  1181. //% for (int i = 0; i < 2; ++i) {
  1182. //% if (dictionary->_valueSet[i]) {
  1183. //% _values[i] = dictionary->_values[i];
  1184. //% _valueSet[i] = YES;
  1185. //% }
  1186. //% }
  1187. //% }
  1188. //% }
  1189. //% return self;
  1190. //%}
  1191. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1192. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1193. //%- (void)dealloc {
  1194. //% NSAssert(!_autocreator,
  1195. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1196. //% [self class], _autocreator);
  1197. //% [super dealloc];
  1198. //%}
  1199. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1200. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1201. //%BOOL_DICT_HASPOD(IDX, REF)
  1202. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1203. //%REF##_valueSet[IDX]
  1204. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1205. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1206. //% int idx = (key ? 1 : 0);
  1207. //% if (_valueSet[idx]) {
  1208. //% if (value) {
  1209. //% *value = _values[idx];
  1210. //% }
  1211. //% return YES;
  1212. //% }
  1213. //% return NO;
  1214. //%}
  1215. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1216. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1217. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1218. //% int idx = (key->valueBool ? 1 : 0);
  1219. //% _values[idx] = value->value##VALUE_NAME;
  1220. //% _valueSet[idx] = YES;
  1221. //%}
  1222. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1223. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1224. //% if (otherDictionary) {
  1225. //% for (int i = 0; i < 2; ++i) {
  1226. //% if (otherDictionary->_valueSet[i]) {
  1227. //% _valueSet[i] = YES;
  1228. //% _values[i] = otherDictionary->_values[i];
  1229. //% }
  1230. //% }
  1231. //% if (_autocreator) {
  1232. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1233. //% }
  1234. //% }
  1235. //%}
  1236. //%
  1237. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1238. //% int idx = (key ? 1 : 0);
  1239. //% _values[idx] = value;
  1240. //% _valueSet[idx] = YES;
  1241. //% if (_autocreator) {
  1242. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1243. //% }
  1244. //%}
  1245. //%
  1246. //%- (void)removeValueForKey:(BOOL)aKey {
  1247. //% _valueSet[aKey ? 1 : 0] = NO;
  1248. //%}
  1249. //%
  1250. //%- (void)removeAll {
  1251. //% _valueSet[0] = NO;
  1252. //% _valueSet[1] = NO;
  1253. //%}
  1254. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1255. //%STR_FORMAT_##VALUE_NAME()
  1256. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1257. //%%u
  1258. //%PDDM-DEFINE STR_FORMAT_Int32()
  1259. //%%d
  1260. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1261. //%%llu
  1262. //%PDDM-DEFINE STR_FORMAT_Int64()
  1263. //%%lld
  1264. //%PDDM-DEFINE STR_FORMAT_Bool()
  1265. //%%d
  1266. //%PDDM-DEFINE STR_FORMAT_Float()
  1267. //%%f
  1268. //%PDDM-DEFINE STR_FORMAT_Double()
  1269. //%%lf
  1270. //
  1271. // Helpers for Objects
  1272. //
  1273. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1274. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1275. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1276. //% return result;
  1277. //%}
  1278. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1279. //%VALUE
  1280. //%PDDM-DEFINE UNWRAPString(VALUE)
  1281. //%VALUE
  1282. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1283. //%VALUE
  1284. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1285. //%VALUE
  1286. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1287. //%VALUE
  1288. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1289. //%ENUM_TYPEOBJECT_##TYPE()
  1290. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1291. //%NSString *
  1292. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1293. //%id ##
  1294. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1295. //%![VAL1 isEqual:VAL2]
  1296. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1297. //%- (BOOL)isInitialized {
  1298. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1299. //% if (!msg.initialized) {
  1300. //% return NO;
  1301. //% }
  1302. //% }
  1303. //% return YES;
  1304. //%}
  1305. //%
  1306. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1307. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1308. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1309. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1310. //% GPBMessage *msg,
  1311. //% BOOL *stop) {
  1312. //% #pragma unused(stop)
  1313. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1314. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1315. //% [copiedMsg release];
  1316. //% }];
  1317. //% return newDict;
  1318. //%}
  1319. //%
  1320. //%
  1321. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1322. //%- (BOOL)isInitialized {
  1323. //% if (_values[0] && ![_values[0] isInitialized]) {
  1324. //% return NO;
  1325. //% }
  1326. //% if (_values[1] && ![_values[1] isInitialized]) {
  1327. //% return NO;
  1328. //% }
  1329. //% return YES;
  1330. //%}
  1331. //%
  1332. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1333. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1334. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1335. //% for (int i = 0; i < 2; ++i) {
  1336. //% if (_values[i] != nil) {
  1337. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1338. //% }
  1339. //% }
  1340. //% return newDict;
  1341. //%}
  1342. //%
  1343. //%
  1344. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1345. // Empty
  1346. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1347. //%valueString
  1348. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1349. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1350. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1351. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1352. //%##EXTRA_INDENT$S## }
  1353. //%
  1354. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1355. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1356. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1357. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1358. //%##EXTRA_INDENT$S## }
  1359. //%
  1360. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1361. // Empty
  1362. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1363. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1364. //% forKeys:(const BOOL [])keys
  1365. //% count:(NSUInteger)count {
  1366. //% self = [super init];
  1367. //% if (self) {
  1368. //% for (NSUInteger i = 0; i < count; ++i) {
  1369. //% if (!objects[i]) {
  1370. //% [NSException raise:NSInvalidArgumentException
  1371. //% format:@"Attempting to add nil object to a Dictionary"];
  1372. //% }
  1373. //% int idx = keys[i] ? 1 : 0;
  1374. //% [_values[idx] release];
  1375. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1376. //% }
  1377. //% }
  1378. //% return self;
  1379. //%}
  1380. //%
  1381. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1382. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1383. //% if (self) {
  1384. //% if (dictionary) {
  1385. //% _values[0] = [dictionary->_values[0] retain];
  1386. //% _values[1] = [dictionary->_values[1] retain];
  1387. //% }
  1388. //% }
  1389. //% return self;
  1390. //%}
  1391. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1392. //%- (void)dealloc {
  1393. //% NSAssert(!_autocreator,
  1394. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1395. //% [self class], _autocreator);
  1396. //% [_values[0] release];
  1397. //% [_values[1] release];
  1398. //% [super dealloc];
  1399. //%}
  1400. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1401. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1402. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1403. //%REF##_values[IDX] != nil
  1404. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1405. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1406. //% return _values[key ? 1 : 0];
  1407. //%}
  1408. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1409. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1410. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1411. //% int idx = (key->valueBool ? 1 : 0);
  1412. //% [_values[idx] release];
  1413. //% _values[idx] = [value->valueString retain];
  1414. //%}
  1415. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1416. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1417. //% if (otherDictionary) {
  1418. //% for (int i = 0; i < 2; ++i) {
  1419. //% if (otherDictionary->_values[i] != nil) {
  1420. //% [_values[i] release];
  1421. //% _values[i] = [otherDictionary->_values[i] retain];
  1422. //% }
  1423. //% }
  1424. //% if (_autocreator) {
  1425. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1426. //% }
  1427. //% }
  1428. //%}
  1429. //%
  1430. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1431. //% if (!object) {
  1432. //% [NSException raise:NSInvalidArgumentException
  1433. //% format:@"Attempting to add nil object to a Dictionary"];
  1434. //% }
  1435. //% int idx = (key ? 1 : 0);
  1436. //% [_values[idx] release];
  1437. //% _values[idx] = [object retain];
  1438. //% if (_autocreator) {
  1439. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1440. //% }
  1441. //%}
  1442. //%
  1443. //%- (void)removeObjectForKey:(BOOL)aKey {
  1444. //% int idx = (aKey ? 1 : 0);
  1445. //% [_values[idx] release];
  1446. //% _values[idx] = nil;
  1447. //%}
  1448. //%
  1449. //%- (void)removeAll {
  1450. //% for (int i = 0; i < 2; ++i) {
  1451. //% [_values[i] release];
  1452. //% _values[i] = nil;
  1453. //% }
  1454. //%}
  1455. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1456. //%%@
  1457. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1458. // This block of code is generated, do not edit it directly.
  1459. #pragma mark - UInt32 -> UInt32
  1460. @implementation GPBUInt32UInt32Dictionary {
  1461. @package
  1462. NSMutableDictionary *_dictionary;
  1463. }
  1464. + (instancetype)dictionary {
  1465. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1466. }
  1467. + (instancetype)dictionaryWithValue:(uint32_t)value
  1468. forKey:(uint32_t)key {
  1469. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1470. // on to get the type correct.
  1471. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1472. forKeys:&key
  1473. count:1] autorelease];
  1474. }
  1475. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1476. forKeys:(const uint32_t [])keys
  1477. count:(NSUInteger)count {
  1478. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1479. // on to get the type correct.
  1480. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1481. forKeys:keys
  1482. count:count] autorelease];
  1483. }
  1484. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1485. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1486. // on to get the type correct.
  1487. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1488. }
  1489. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1490. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1491. }
  1492. - (instancetype)init {
  1493. return [self initWithValues:NULL forKeys:NULL count:0];
  1494. }
  1495. - (instancetype)initWithValues:(const uint32_t [])values
  1496. forKeys:(const uint32_t [])keys
  1497. count:(NSUInteger)count {
  1498. self = [super init];
  1499. if (self) {
  1500. _dictionary = [[NSMutableDictionary alloc] init];
  1501. if (count && values && keys) {
  1502. for (NSUInteger i = 0; i < count; ++i) {
  1503. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1504. }
  1505. }
  1506. }
  1507. return self;
  1508. }
  1509. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1510. self = [self initWithValues:NULL forKeys:NULL count:0];
  1511. if (self) {
  1512. if (dictionary) {
  1513. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1514. }
  1515. }
  1516. return self;
  1517. }
  1518. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1519. #pragma unused(numItems)
  1520. return [self initWithValues:NULL forKeys:NULL count:0];
  1521. }
  1522. - (void)dealloc {
  1523. NSAssert(!_autocreator,
  1524. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1525. [self class], _autocreator);
  1526. [_dictionary release];
  1527. [super dealloc];
  1528. }
  1529. - (instancetype)copyWithZone:(NSZone *)zone {
  1530. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1531. }
  1532. - (BOOL)isEqual:(id)other {
  1533. if (self == other) {
  1534. return YES;
  1535. }
  1536. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1537. return NO;
  1538. }
  1539. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1540. return [_dictionary isEqual:otherDictionary->_dictionary];
  1541. }
  1542. - (NSUInteger)hash {
  1543. return _dictionary.count;
  1544. }
  1545. - (NSString *)description {
  1546. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1547. }
  1548. - (NSUInteger)count {
  1549. return _dictionary.count;
  1550. }
  1551. - (void)enumerateKeysAndValuesUsingBlock:
  1552. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1553. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1554. NSNumber *aValue,
  1555. BOOL *stop) {
  1556. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1557. }];
  1558. }
  1559. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1560. NSUInteger count = _dictionary.count;
  1561. if (count == 0) {
  1562. return 0;
  1563. }
  1564. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1565. GPBDataType keyDataType = field.mapKeyDataType;
  1566. __block size_t result = 0;
  1567. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1568. NSNumber *aValue,
  1569. BOOL *stop) {
  1570. #pragma unused(stop)
  1571. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1572. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1573. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1574. }];
  1575. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1576. result += tagSize * count;
  1577. return result;
  1578. }
  1579. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1580. asField:(GPBFieldDescriptor *)field {
  1581. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1582. GPBDataType keyDataType = field.mapKeyDataType;
  1583. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1584. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1585. NSNumber *aValue,
  1586. BOOL *stop) {
  1587. #pragma unused(stop)
  1588. // Write the tag.
  1589. [outputStream writeInt32NoTag:tag];
  1590. // Write the size of the message.
  1591. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1592. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1593. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1594. // Write the fields.
  1595. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1596. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1597. }];
  1598. }
  1599. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1600. forGPBGenericValueKey:(GPBGenericValue *)key {
  1601. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1602. }
  1603. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1604. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1605. #pragma unused(stop)
  1606. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1607. }];
  1608. }
  1609. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1610. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1611. if (wrapped && value) {
  1612. *value = [wrapped unsignedIntValue];
  1613. }
  1614. return (wrapped != NULL);
  1615. }
  1616. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1617. if (otherDictionary) {
  1618. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1619. if (_autocreator) {
  1620. GPBAutocreatedDictionaryModified(_autocreator, self);
  1621. }
  1622. }
  1623. }
  1624. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1625. [_dictionary setObject:@(value) forKey:@(key)];
  1626. if (_autocreator) {
  1627. GPBAutocreatedDictionaryModified(_autocreator, self);
  1628. }
  1629. }
  1630. - (void)removeValueForKey:(uint32_t)aKey {
  1631. [_dictionary removeObjectForKey:@(aKey)];
  1632. }
  1633. - (void)removeAll {
  1634. [_dictionary removeAllObjects];
  1635. }
  1636. @end
  1637. #pragma mark - UInt32 -> Int32
  1638. @implementation GPBUInt32Int32Dictionary {
  1639. @package
  1640. NSMutableDictionary *_dictionary;
  1641. }
  1642. + (instancetype)dictionary {
  1643. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1644. }
  1645. + (instancetype)dictionaryWithValue:(int32_t)value
  1646. forKey:(uint32_t)key {
  1647. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1648. // on to get the type correct.
  1649. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1650. forKeys:&key
  1651. count:1] autorelease];
  1652. }
  1653. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1654. forKeys:(const uint32_t [])keys
  1655. count:(NSUInteger)count {
  1656. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1657. // on to get the type correct.
  1658. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1659. forKeys:keys
  1660. count:count] autorelease];
  1661. }
  1662. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1663. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1664. // on to get the type correct.
  1665. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1666. }
  1667. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1668. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1669. }
  1670. - (instancetype)init {
  1671. return [self initWithValues:NULL forKeys:NULL count:0];
  1672. }
  1673. - (instancetype)initWithValues:(const int32_t [])values
  1674. forKeys:(const uint32_t [])keys
  1675. count:(NSUInteger)count {
  1676. self = [super init];
  1677. if (self) {
  1678. _dictionary = [[NSMutableDictionary alloc] init];
  1679. if (count && values && keys) {
  1680. for (NSUInteger i = 0; i < count; ++i) {
  1681. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1682. }
  1683. }
  1684. }
  1685. return self;
  1686. }
  1687. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1688. self = [self initWithValues:NULL forKeys:NULL count:0];
  1689. if (self) {
  1690. if (dictionary) {
  1691. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1692. }
  1693. }
  1694. return self;
  1695. }
  1696. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1697. #pragma unused(numItems)
  1698. return [self initWithValues:NULL forKeys:NULL count:0];
  1699. }
  1700. - (void)dealloc {
  1701. NSAssert(!_autocreator,
  1702. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1703. [self class], _autocreator);
  1704. [_dictionary release];
  1705. [super dealloc];
  1706. }
  1707. - (instancetype)copyWithZone:(NSZone *)zone {
  1708. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1709. }
  1710. - (BOOL)isEqual:(id)other {
  1711. if (self == other) {
  1712. return YES;
  1713. }
  1714. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1715. return NO;
  1716. }
  1717. GPBUInt32Int32Dictionary *otherDictionary = other;
  1718. return [_dictionary isEqual:otherDictionary->_dictionary];
  1719. }
  1720. - (NSUInteger)hash {
  1721. return _dictionary.count;
  1722. }
  1723. - (NSString *)description {
  1724. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1725. }
  1726. - (NSUInteger)count {
  1727. return _dictionary.count;
  1728. }
  1729. - (void)enumerateKeysAndValuesUsingBlock:
  1730. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1731. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1732. NSNumber *aValue,
  1733. BOOL *stop) {
  1734. block([aKey unsignedIntValue], [aValue intValue], stop);
  1735. }];
  1736. }
  1737. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1738. NSUInteger count = _dictionary.count;
  1739. if (count == 0) {
  1740. return 0;
  1741. }
  1742. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1743. GPBDataType keyDataType = field.mapKeyDataType;
  1744. __block size_t result = 0;
  1745. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1746. NSNumber *aValue,
  1747. BOOL *stop) {
  1748. #pragma unused(stop)
  1749. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1750. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1751. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1752. }];
  1753. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1754. result += tagSize * count;
  1755. return result;
  1756. }
  1757. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1758. asField:(GPBFieldDescriptor *)field {
  1759. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1760. GPBDataType keyDataType = field.mapKeyDataType;
  1761. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1762. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1763. NSNumber *aValue,
  1764. BOOL *stop) {
  1765. #pragma unused(stop)
  1766. // Write the tag.
  1767. [outputStream writeInt32NoTag:tag];
  1768. // Write the size of the message.
  1769. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1770. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1771. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1772. // Write the fields.
  1773. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1774. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1775. }];
  1776. }
  1777. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1778. forGPBGenericValueKey:(GPBGenericValue *)key {
  1779. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1780. }
  1781. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1782. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1783. #pragma unused(stop)
  1784. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1785. }];
  1786. }
  1787. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1788. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1789. if (wrapped && value) {
  1790. *value = [wrapped intValue];
  1791. }
  1792. return (wrapped != NULL);
  1793. }
  1794. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1795. if (otherDictionary) {
  1796. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1797. if (_autocreator) {
  1798. GPBAutocreatedDictionaryModified(_autocreator, self);
  1799. }
  1800. }
  1801. }
  1802. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1803. [_dictionary setObject:@(value) forKey:@(key)];
  1804. if (_autocreator) {
  1805. GPBAutocreatedDictionaryModified(_autocreator, self);
  1806. }
  1807. }
  1808. - (void)removeValueForKey:(uint32_t)aKey {
  1809. [_dictionary removeObjectForKey:@(aKey)];
  1810. }
  1811. - (void)removeAll {
  1812. [_dictionary removeAllObjects];
  1813. }
  1814. @end
  1815. #pragma mark - UInt32 -> UInt64
  1816. @implementation GPBUInt32UInt64Dictionary {
  1817. @package
  1818. NSMutableDictionary *_dictionary;
  1819. }
  1820. + (instancetype)dictionary {
  1821. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1822. }
  1823. + (instancetype)dictionaryWithValue:(uint64_t)value
  1824. forKey:(uint32_t)key {
  1825. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1826. // on to get the type correct.
  1827. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1828. forKeys:&key
  1829. count:1] autorelease];
  1830. }
  1831. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1832. forKeys:(const uint32_t [])keys
  1833. count:(NSUInteger)count {
  1834. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1835. // on to get the type correct.
  1836. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1837. forKeys:keys
  1838. count:count] autorelease];
  1839. }
  1840. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1841. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1842. // on to get the type correct.
  1843. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1844. }
  1845. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1846. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1847. }
  1848. - (instancetype)init {
  1849. return [self initWithValues:NULL forKeys:NULL count:0];
  1850. }
  1851. - (instancetype)initWithValues:(const uint64_t [])values
  1852. forKeys:(const uint32_t [])keys
  1853. count:(NSUInteger)count {
  1854. self = [super init];
  1855. if (self) {
  1856. _dictionary = [[NSMutableDictionary alloc] init];
  1857. if (count && values && keys) {
  1858. for (NSUInteger i = 0; i < count; ++i) {
  1859. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1860. }
  1861. }
  1862. }
  1863. return self;
  1864. }
  1865. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1866. self = [self initWithValues:NULL forKeys:NULL count:0];
  1867. if (self) {
  1868. if (dictionary) {
  1869. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1870. }
  1871. }
  1872. return self;
  1873. }
  1874. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1875. #pragma unused(numItems)
  1876. return [self initWithValues:NULL forKeys:NULL count:0];
  1877. }
  1878. - (void)dealloc {
  1879. NSAssert(!_autocreator,
  1880. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1881. [self class], _autocreator);
  1882. [_dictionary release];
  1883. [super dealloc];
  1884. }
  1885. - (instancetype)copyWithZone:(NSZone *)zone {
  1886. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1887. }
  1888. - (BOOL)isEqual:(id)other {
  1889. if (self == other) {
  1890. return YES;
  1891. }
  1892. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1893. return NO;
  1894. }
  1895. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1896. return [_dictionary isEqual:otherDictionary->_dictionary];
  1897. }
  1898. - (NSUInteger)hash {
  1899. return _dictionary.count;
  1900. }
  1901. - (NSString *)description {
  1902. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1903. }
  1904. - (NSUInteger)count {
  1905. return _dictionary.count;
  1906. }
  1907. - (void)enumerateKeysAndValuesUsingBlock:
  1908. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1909. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1910. NSNumber *aValue,
  1911. BOOL *stop) {
  1912. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1913. }];
  1914. }
  1915. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1916. NSUInteger count = _dictionary.count;
  1917. if (count == 0) {
  1918. return 0;
  1919. }
  1920. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1921. GPBDataType keyDataType = field.mapKeyDataType;
  1922. __block size_t result = 0;
  1923. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1924. NSNumber *aValue,
  1925. BOOL *stop) {
  1926. #pragma unused(stop)
  1927. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1928. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1929. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1930. }];
  1931. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1932. result += tagSize * count;
  1933. return result;
  1934. }
  1935. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1936. asField:(GPBFieldDescriptor *)field {
  1937. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1938. GPBDataType keyDataType = field.mapKeyDataType;
  1939. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1940. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1941. NSNumber *aValue,
  1942. BOOL *stop) {
  1943. #pragma unused(stop)
  1944. // Write the tag.
  1945. [outputStream writeInt32NoTag:tag];
  1946. // Write the size of the message.
  1947. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1948. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1949. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1950. // Write the fields.
  1951. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1952. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1953. }];
  1954. }
  1955. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1956. forGPBGenericValueKey:(GPBGenericValue *)key {
  1957. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1958. }
  1959. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1960. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1961. #pragma unused(stop)
  1962. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1963. }];
  1964. }
  1965. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1966. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1967. if (wrapped && value) {
  1968. *value = [wrapped unsignedLongLongValue];
  1969. }
  1970. return (wrapped != NULL);
  1971. }
  1972. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1973. if (otherDictionary) {
  1974. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1975. if (_autocreator) {
  1976. GPBAutocreatedDictionaryModified(_autocreator, self);
  1977. }
  1978. }
  1979. }
  1980. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1981. [_dictionary setObject:@(value) forKey:@(key)];
  1982. if (_autocreator) {
  1983. GPBAutocreatedDictionaryModified(_autocreator, self);
  1984. }
  1985. }
  1986. - (void)removeValueForKey:(uint32_t)aKey {
  1987. [_dictionary removeObjectForKey:@(aKey)];
  1988. }
  1989. - (void)removeAll {
  1990. [_dictionary removeAllObjects];
  1991. }
  1992. @end
  1993. #pragma mark - UInt32 -> Int64
  1994. @implementation GPBUInt32Int64Dictionary {
  1995. @package
  1996. NSMutableDictionary *_dictionary;
  1997. }
  1998. + (instancetype)dictionary {
  1999. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2000. }
  2001. + (instancetype)dictionaryWithValue:(int64_t)value
  2002. forKey:(uint32_t)key {
  2003. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2004. // on to get the type correct.
  2005. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  2006. forKeys:&key
  2007. count:1] autorelease];
  2008. }
  2009. + (instancetype)dictionaryWithValues:(const int64_t [])values
  2010. forKeys:(const uint32_t [])keys
  2011. count:(NSUInteger)count {
  2012. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2013. // on to get the type correct.
  2014. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  2015. forKeys:keys
  2016. count:count] autorelease];
  2017. }
  2018. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2019. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2020. // on to get the type correct.
  2021. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2022. }
  2023. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2024. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2025. }
  2026. - (instancetype)init {
  2027. return [self initWithValues:NULL forKeys:NULL count:0];
  2028. }
  2029. - (instancetype)initWithValues:(const int64_t [])values
  2030. forKeys:(const uint32_t [])keys
  2031. count:(NSUInteger)count {
  2032. self = [super init];
  2033. if (self) {
  2034. _dictionary = [[NSMutableDictionary alloc] init];
  2035. if (count && values && keys) {
  2036. for (NSUInteger i = 0; i < count; ++i) {
  2037. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2038. }
  2039. }
  2040. }
  2041. return self;
  2042. }
  2043. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2044. self = [self initWithValues:NULL forKeys:NULL count:0];
  2045. if (self) {
  2046. if (dictionary) {
  2047. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2048. }
  2049. }
  2050. return self;
  2051. }
  2052. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2053. #pragma unused(numItems)
  2054. return [self initWithValues:NULL forKeys:NULL count:0];
  2055. }
  2056. - (void)dealloc {
  2057. NSAssert(!_autocreator,
  2058. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2059. [self class], _autocreator);
  2060. [_dictionary release];
  2061. [super dealloc];
  2062. }
  2063. - (instancetype)copyWithZone:(NSZone *)zone {
  2064. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2065. }
  2066. - (BOOL)isEqual:(id)other {
  2067. if (self == other) {
  2068. return YES;
  2069. }
  2070. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2071. return NO;
  2072. }
  2073. GPBUInt32Int64Dictionary *otherDictionary = other;
  2074. return [_dictionary isEqual:otherDictionary->_dictionary];
  2075. }
  2076. - (NSUInteger)hash {
  2077. return _dictionary.count;
  2078. }
  2079. - (NSString *)description {
  2080. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2081. }
  2082. - (NSUInteger)count {
  2083. return _dictionary.count;
  2084. }
  2085. - (void)enumerateKeysAndValuesUsingBlock:
  2086. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2087. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2088. NSNumber *aValue,
  2089. BOOL *stop) {
  2090. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2091. }];
  2092. }
  2093. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2094. NSUInteger count = _dictionary.count;
  2095. if (count == 0) {
  2096. return 0;
  2097. }
  2098. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2099. GPBDataType keyDataType = field.mapKeyDataType;
  2100. __block size_t result = 0;
  2101. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2102. NSNumber *aValue,
  2103. BOOL *stop) {
  2104. #pragma unused(stop)
  2105. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2106. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2107. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2108. }];
  2109. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2110. result += tagSize * count;
  2111. return result;
  2112. }
  2113. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2114. asField:(GPBFieldDescriptor *)field {
  2115. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2116. GPBDataType keyDataType = field.mapKeyDataType;
  2117. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2118. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2119. NSNumber *aValue,
  2120. BOOL *stop) {
  2121. #pragma unused(stop)
  2122. // Write the tag.
  2123. [outputStream writeInt32NoTag:tag];
  2124. // Write the size of the message.
  2125. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2126. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2127. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2128. // Write the fields.
  2129. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2130. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2131. }];
  2132. }
  2133. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2134. forGPBGenericValueKey:(GPBGenericValue *)key {
  2135. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2136. }
  2137. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2138. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2139. #pragma unused(stop)
  2140. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2141. }];
  2142. }
  2143. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  2144. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2145. if (wrapped && value) {
  2146. *value = [wrapped longLongValue];
  2147. }
  2148. return (wrapped != NULL);
  2149. }
  2150. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2151. if (otherDictionary) {
  2152. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2153. if (_autocreator) {
  2154. GPBAutocreatedDictionaryModified(_autocreator, self);
  2155. }
  2156. }
  2157. }
  2158. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  2159. [_dictionary setObject:@(value) forKey:@(key)];
  2160. if (_autocreator) {
  2161. GPBAutocreatedDictionaryModified(_autocreator, self);
  2162. }
  2163. }
  2164. - (void)removeValueForKey:(uint32_t)aKey {
  2165. [_dictionary removeObjectForKey:@(aKey)];
  2166. }
  2167. - (void)removeAll {
  2168. [_dictionary removeAllObjects];
  2169. }
  2170. @end
  2171. #pragma mark - UInt32 -> Bool
  2172. @implementation GPBUInt32BoolDictionary {
  2173. @package
  2174. NSMutableDictionary *_dictionary;
  2175. }
  2176. + (instancetype)dictionary {
  2177. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2178. }
  2179. + (instancetype)dictionaryWithValue:(BOOL)value
  2180. forKey:(uint32_t)key {
  2181. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2182. // on to get the type correct.
  2183. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2184. forKeys:&key
  2185. count:1] autorelease];
  2186. }
  2187. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2188. forKeys:(const uint32_t [])keys
  2189. count:(NSUInteger)count {
  2190. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2191. // on to get the type correct.
  2192. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2193. forKeys:keys
  2194. count:count] autorelease];
  2195. }
  2196. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2197. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2198. // on to get the type correct.
  2199. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2200. }
  2201. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2202. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2203. }
  2204. - (instancetype)init {
  2205. return [self initWithValues:NULL forKeys:NULL count:0];
  2206. }
  2207. - (instancetype)initWithValues:(const BOOL [])values
  2208. forKeys:(const uint32_t [])keys
  2209. count:(NSUInteger)count {
  2210. self = [super init];
  2211. if (self) {
  2212. _dictionary = [[NSMutableDictionary alloc] init];
  2213. if (count && values && keys) {
  2214. for (NSUInteger i = 0; i < count; ++i) {
  2215. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2216. }
  2217. }
  2218. }
  2219. return self;
  2220. }
  2221. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2222. self = [self initWithValues:NULL forKeys:NULL count:0];
  2223. if (self) {
  2224. if (dictionary) {
  2225. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2226. }
  2227. }
  2228. return self;
  2229. }
  2230. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2231. #pragma unused(numItems)
  2232. return [self initWithValues:NULL forKeys:NULL count:0];
  2233. }
  2234. - (void)dealloc {
  2235. NSAssert(!_autocreator,
  2236. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2237. [self class], _autocreator);
  2238. [_dictionary release];
  2239. [super dealloc];
  2240. }
  2241. - (instancetype)copyWithZone:(NSZone *)zone {
  2242. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2243. }
  2244. - (BOOL)isEqual:(id)other {
  2245. if (self == other) {
  2246. return YES;
  2247. }
  2248. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2249. return NO;
  2250. }
  2251. GPBUInt32BoolDictionary *otherDictionary = other;
  2252. return [_dictionary isEqual:otherDictionary->_dictionary];
  2253. }
  2254. - (NSUInteger)hash {
  2255. return _dictionary.count;
  2256. }
  2257. - (NSString *)description {
  2258. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2259. }
  2260. - (NSUInteger)count {
  2261. return _dictionary.count;
  2262. }
  2263. - (void)enumerateKeysAndValuesUsingBlock:
  2264. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2265. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2266. NSNumber *aValue,
  2267. BOOL *stop) {
  2268. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2269. }];
  2270. }
  2271. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2272. NSUInteger count = _dictionary.count;
  2273. if (count == 0) {
  2274. return 0;
  2275. }
  2276. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2277. GPBDataType keyDataType = field.mapKeyDataType;
  2278. __block size_t result = 0;
  2279. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2280. NSNumber *aValue,
  2281. BOOL *stop) {
  2282. #pragma unused(stop)
  2283. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2284. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2285. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2286. }];
  2287. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2288. result += tagSize * count;
  2289. return result;
  2290. }
  2291. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2292. asField:(GPBFieldDescriptor *)field {
  2293. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2294. GPBDataType keyDataType = field.mapKeyDataType;
  2295. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2296. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2297. NSNumber *aValue,
  2298. BOOL *stop) {
  2299. #pragma unused(stop)
  2300. // Write the tag.
  2301. [outputStream writeInt32NoTag:tag];
  2302. // Write the size of the message.
  2303. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2304. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2305. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2306. // Write the fields.
  2307. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2308. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2309. }];
  2310. }
  2311. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2312. forGPBGenericValueKey:(GPBGenericValue *)key {
  2313. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2314. }
  2315. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2316. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2317. #pragma unused(stop)
  2318. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2319. }];
  2320. }
  2321. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2322. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2323. if (wrapped && value) {
  2324. *value = [wrapped boolValue];
  2325. }
  2326. return (wrapped != NULL);
  2327. }
  2328. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2329. if (otherDictionary) {
  2330. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2331. if (_autocreator) {
  2332. GPBAutocreatedDictionaryModified(_autocreator, self);
  2333. }
  2334. }
  2335. }
  2336. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2337. [_dictionary setObject:@(value) forKey:@(key)];
  2338. if (_autocreator) {
  2339. GPBAutocreatedDictionaryModified(_autocreator, self);
  2340. }
  2341. }
  2342. - (void)removeValueForKey:(uint32_t)aKey {
  2343. [_dictionary removeObjectForKey:@(aKey)];
  2344. }
  2345. - (void)removeAll {
  2346. [_dictionary removeAllObjects];
  2347. }
  2348. @end
  2349. #pragma mark - UInt32 -> Float
  2350. @implementation GPBUInt32FloatDictionary {
  2351. @package
  2352. NSMutableDictionary *_dictionary;
  2353. }
  2354. + (instancetype)dictionary {
  2355. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2356. }
  2357. + (instancetype)dictionaryWithValue:(float)value
  2358. forKey:(uint32_t)key {
  2359. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2360. // on to get the type correct.
  2361. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2362. forKeys:&key
  2363. count:1] autorelease];
  2364. }
  2365. + (instancetype)dictionaryWithValues:(const float [])values
  2366. forKeys:(const uint32_t [])keys
  2367. count:(NSUInteger)count {
  2368. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2369. // on to get the type correct.
  2370. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2371. forKeys:keys
  2372. count:count] autorelease];
  2373. }
  2374. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2375. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2376. // on to get the type correct.
  2377. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2378. }
  2379. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2380. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2381. }
  2382. - (instancetype)init {
  2383. return [self initWithValues:NULL forKeys:NULL count:0];
  2384. }
  2385. - (instancetype)initWithValues:(const float [])values
  2386. forKeys:(const uint32_t [])keys
  2387. count:(NSUInteger)count {
  2388. self = [super init];
  2389. if (self) {
  2390. _dictionary = [[NSMutableDictionary alloc] init];
  2391. if (count && values && keys) {
  2392. for (NSUInteger i = 0; i < count; ++i) {
  2393. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2394. }
  2395. }
  2396. }
  2397. return self;
  2398. }
  2399. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2400. self = [self initWithValues:NULL forKeys:NULL count:0];
  2401. if (self) {
  2402. if (dictionary) {
  2403. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2404. }
  2405. }
  2406. return self;
  2407. }
  2408. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2409. #pragma unused(numItems)
  2410. return [self initWithValues:NULL forKeys:NULL count:0];
  2411. }
  2412. - (void)dealloc {
  2413. NSAssert(!_autocreator,
  2414. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2415. [self class], _autocreator);
  2416. [_dictionary release];
  2417. [super dealloc];
  2418. }
  2419. - (instancetype)copyWithZone:(NSZone *)zone {
  2420. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2421. }
  2422. - (BOOL)isEqual:(id)other {
  2423. if (self == other) {
  2424. return YES;
  2425. }
  2426. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2427. return NO;
  2428. }
  2429. GPBUInt32FloatDictionary *otherDictionary = other;
  2430. return [_dictionary isEqual:otherDictionary->_dictionary];
  2431. }
  2432. - (NSUInteger)hash {
  2433. return _dictionary.count;
  2434. }
  2435. - (NSString *)description {
  2436. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2437. }
  2438. - (NSUInteger)count {
  2439. return _dictionary.count;
  2440. }
  2441. - (void)enumerateKeysAndValuesUsingBlock:
  2442. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2443. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2444. NSNumber *aValue,
  2445. BOOL *stop) {
  2446. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2447. }];
  2448. }
  2449. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2450. NSUInteger count = _dictionary.count;
  2451. if (count == 0) {
  2452. return 0;
  2453. }
  2454. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2455. GPBDataType keyDataType = field.mapKeyDataType;
  2456. __block size_t result = 0;
  2457. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2458. NSNumber *aValue,
  2459. BOOL *stop) {
  2460. #pragma unused(stop)
  2461. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2462. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2463. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2464. }];
  2465. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2466. result += tagSize * count;
  2467. return result;
  2468. }
  2469. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2470. asField:(GPBFieldDescriptor *)field {
  2471. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2472. GPBDataType keyDataType = field.mapKeyDataType;
  2473. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2474. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2475. NSNumber *aValue,
  2476. BOOL *stop) {
  2477. #pragma unused(stop)
  2478. // Write the tag.
  2479. [outputStream writeInt32NoTag:tag];
  2480. // Write the size of the message.
  2481. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2482. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2483. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2484. // Write the fields.
  2485. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2486. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2487. }];
  2488. }
  2489. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2490. forGPBGenericValueKey:(GPBGenericValue *)key {
  2491. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2492. }
  2493. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2494. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2495. #pragma unused(stop)
  2496. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2497. }];
  2498. }
  2499. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2500. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2501. if (wrapped && value) {
  2502. *value = [wrapped floatValue];
  2503. }
  2504. return (wrapped != NULL);
  2505. }
  2506. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2507. if (otherDictionary) {
  2508. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2509. if (_autocreator) {
  2510. GPBAutocreatedDictionaryModified(_autocreator, self);
  2511. }
  2512. }
  2513. }
  2514. - (void)setValue:(float)value forKey:(uint32_t)key {
  2515. [_dictionary setObject:@(value) forKey:@(key)];
  2516. if (_autocreator) {
  2517. GPBAutocreatedDictionaryModified(_autocreator, self);
  2518. }
  2519. }
  2520. - (void)removeValueForKey:(uint32_t)aKey {
  2521. [_dictionary removeObjectForKey:@(aKey)];
  2522. }
  2523. - (void)removeAll {
  2524. [_dictionary removeAllObjects];
  2525. }
  2526. @end
  2527. #pragma mark - UInt32 -> Double
  2528. @implementation GPBUInt32DoubleDictionary {
  2529. @package
  2530. NSMutableDictionary *_dictionary;
  2531. }
  2532. + (instancetype)dictionary {
  2533. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2534. }
  2535. + (instancetype)dictionaryWithValue:(double)value
  2536. forKey:(uint32_t)key {
  2537. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2538. // on to get the type correct.
  2539. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2540. forKeys:&key
  2541. count:1] autorelease];
  2542. }
  2543. + (instancetype)dictionaryWithValues:(const double [])values
  2544. forKeys:(const uint32_t [])keys
  2545. count:(NSUInteger)count {
  2546. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2547. // on to get the type correct.
  2548. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2549. forKeys:keys
  2550. count:count] autorelease];
  2551. }
  2552. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2553. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2554. // on to get the type correct.
  2555. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2556. }
  2557. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2558. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2559. }
  2560. - (instancetype)init {
  2561. return [self initWithValues:NULL forKeys:NULL count:0];
  2562. }
  2563. - (instancetype)initWithValues:(const double [])values
  2564. forKeys:(const uint32_t [])keys
  2565. count:(NSUInteger)count {
  2566. self = [super init];
  2567. if (self) {
  2568. _dictionary = [[NSMutableDictionary alloc] init];
  2569. if (count && values && keys) {
  2570. for (NSUInteger i = 0; i < count; ++i) {
  2571. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2572. }
  2573. }
  2574. }
  2575. return self;
  2576. }
  2577. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2578. self = [self initWithValues:NULL forKeys:NULL count:0];
  2579. if (self) {
  2580. if (dictionary) {
  2581. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2582. }
  2583. }
  2584. return self;
  2585. }
  2586. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2587. #pragma unused(numItems)
  2588. return [self initWithValues:NULL forKeys:NULL count:0];
  2589. }
  2590. - (void)dealloc {
  2591. NSAssert(!_autocreator,
  2592. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2593. [self class], _autocreator);
  2594. [_dictionary release];
  2595. [super dealloc];
  2596. }
  2597. - (instancetype)copyWithZone:(NSZone *)zone {
  2598. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2599. }
  2600. - (BOOL)isEqual:(id)other {
  2601. if (self == other) {
  2602. return YES;
  2603. }
  2604. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2605. return NO;
  2606. }
  2607. GPBUInt32DoubleDictionary *otherDictionary = other;
  2608. return [_dictionary isEqual:otherDictionary->_dictionary];
  2609. }
  2610. - (NSUInteger)hash {
  2611. return _dictionary.count;
  2612. }
  2613. - (NSString *)description {
  2614. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2615. }
  2616. - (NSUInteger)count {
  2617. return _dictionary.count;
  2618. }
  2619. - (void)enumerateKeysAndValuesUsingBlock:
  2620. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2621. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2622. NSNumber *aValue,
  2623. BOOL *stop) {
  2624. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2625. }];
  2626. }
  2627. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2628. NSUInteger count = _dictionary.count;
  2629. if (count == 0) {
  2630. return 0;
  2631. }
  2632. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2633. GPBDataType keyDataType = field.mapKeyDataType;
  2634. __block size_t result = 0;
  2635. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2636. NSNumber *aValue,
  2637. BOOL *stop) {
  2638. #pragma unused(stop)
  2639. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2640. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2641. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2642. }];
  2643. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2644. result += tagSize * count;
  2645. return result;
  2646. }
  2647. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2648. asField:(GPBFieldDescriptor *)field {
  2649. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2650. GPBDataType keyDataType = field.mapKeyDataType;
  2651. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2652. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2653. NSNumber *aValue,
  2654. BOOL *stop) {
  2655. #pragma unused(stop)
  2656. // Write the tag.
  2657. [outputStream writeInt32NoTag:tag];
  2658. // Write the size of the message.
  2659. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2660. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2661. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2662. // Write the fields.
  2663. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2664. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2665. }];
  2666. }
  2667. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2668. forGPBGenericValueKey:(GPBGenericValue *)key {
  2669. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2670. }
  2671. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2672. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2673. #pragma unused(stop)
  2674. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2675. }];
  2676. }
  2677. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2678. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2679. if (wrapped && value) {
  2680. *value = [wrapped doubleValue];
  2681. }
  2682. return (wrapped != NULL);
  2683. }
  2684. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2685. if (otherDictionary) {
  2686. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2687. if (_autocreator) {
  2688. GPBAutocreatedDictionaryModified(_autocreator, self);
  2689. }
  2690. }
  2691. }
  2692. - (void)setValue:(double)value forKey:(uint32_t)key {
  2693. [_dictionary setObject:@(value) forKey:@(key)];
  2694. if (_autocreator) {
  2695. GPBAutocreatedDictionaryModified(_autocreator, self);
  2696. }
  2697. }
  2698. - (void)removeValueForKey:(uint32_t)aKey {
  2699. [_dictionary removeObjectForKey:@(aKey)];
  2700. }
  2701. - (void)removeAll {
  2702. [_dictionary removeAllObjects];
  2703. }
  2704. @end
  2705. #pragma mark - UInt32 -> Enum
  2706. @implementation GPBUInt32EnumDictionary {
  2707. @package
  2708. NSMutableDictionary *_dictionary;
  2709. GPBEnumValidationFunc _validationFunc;
  2710. }
  2711. @synthesize validationFunc = _validationFunc;
  2712. + (instancetype)dictionary {
  2713. return [[[self alloc] initWithValidationFunction:NULL
  2714. rawValues:NULL
  2715. forKeys:NULL
  2716. count:0] autorelease];
  2717. }
  2718. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2719. return [[[self alloc] initWithValidationFunction:func
  2720. rawValues:NULL
  2721. forKeys:NULL
  2722. count:0] autorelease];
  2723. }
  2724. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2725. rawValue:(int32_t)rawValue
  2726. forKey:(uint32_t)key {
  2727. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2728. // on to get the type correct.
  2729. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2730. rawValues:&rawValue
  2731. forKeys:&key
  2732. count:1] autorelease];
  2733. }
  2734. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2735. rawValues:(const int32_t [])rawValues
  2736. forKeys:(const uint32_t [])keys
  2737. count:(NSUInteger)count {
  2738. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2739. // on to get the type correct.
  2740. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2741. rawValues:rawValues
  2742. forKeys:keys
  2743. count:count] autorelease];
  2744. }
  2745. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2746. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2747. // on to get the type correct.
  2748. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2749. }
  2750. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2751. capacity:(NSUInteger)numItems {
  2752. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2753. }
  2754. - (instancetype)init {
  2755. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2756. }
  2757. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2758. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2759. }
  2760. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2761. rawValues:(const int32_t [])rawValues
  2762. forKeys:(const uint32_t [])keys
  2763. count:(NSUInteger)count {
  2764. self = [super init];
  2765. if (self) {
  2766. _dictionary = [[NSMutableDictionary alloc] init];
  2767. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2768. if (count && rawValues && keys) {
  2769. for (NSUInteger i = 0; i < count; ++i) {
  2770. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2771. }
  2772. }
  2773. }
  2774. return self;
  2775. }
  2776. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2777. self = [self initWithValidationFunction:dictionary.validationFunc
  2778. rawValues:NULL
  2779. forKeys:NULL
  2780. count:0];
  2781. if (self) {
  2782. if (dictionary) {
  2783. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2784. }
  2785. }
  2786. return self;
  2787. }
  2788. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2789. capacity:(NSUInteger)numItems {
  2790. #pragma unused(numItems)
  2791. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2792. }
  2793. - (void)dealloc {
  2794. NSAssert(!_autocreator,
  2795. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2796. [self class], _autocreator);
  2797. [_dictionary release];
  2798. [super dealloc];
  2799. }
  2800. - (instancetype)copyWithZone:(NSZone *)zone {
  2801. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2802. }
  2803. - (BOOL)isEqual:(id)other {
  2804. if (self == other) {
  2805. return YES;
  2806. }
  2807. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2808. return NO;
  2809. }
  2810. GPBUInt32EnumDictionary *otherDictionary = other;
  2811. return [_dictionary isEqual:otherDictionary->_dictionary];
  2812. }
  2813. - (NSUInteger)hash {
  2814. return _dictionary.count;
  2815. }
  2816. - (NSString *)description {
  2817. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2818. }
  2819. - (NSUInteger)count {
  2820. return _dictionary.count;
  2821. }
  2822. - (void)enumerateKeysAndRawValuesUsingBlock:
  2823. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2824. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2825. NSNumber *aValue,
  2826. BOOL *stop) {
  2827. block([aKey unsignedIntValue], [aValue intValue], stop);
  2828. }];
  2829. }
  2830. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2831. NSUInteger count = _dictionary.count;
  2832. if (count == 0) {
  2833. return 0;
  2834. }
  2835. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2836. GPBDataType keyDataType = field.mapKeyDataType;
  2837. __block size_t result = 0;
  2838. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2839. NSNumber *aValue,
  2840. BOOL *stop) {
  2841. #pragma unused(stop)
  2842. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2843. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2844. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2845. }];
  2846. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2847. result += tagSize * count;
  2848. return result;
  2849. }
  2850. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2851. asField:(GPBFieldDescriptor *)field {
  2852. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2853. GPBDataType keyDataType = field.mapKeyDataType;
  2854. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2855. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2856. NSNumber *aValue,
  2857. BOOL *stop) {
  2858. #pragma unused(stop)
  2859. // Write the tag.
  2860. [outputStream writeInt32NoTag:tag];
  2861. // Write the size of the message.
  2862. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2863. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2864. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2865. // Write the fields.
  2866. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2867. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2868. }];
  2869. }
  2870. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2871. forKey:(GPBGenericValue *)key
  2872. keyDataType:(GPBDataType)keyDataType {
  2873. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2874. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2875. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2876. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2877. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2878. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2879. [outputStream release];
  2880. return data;
  2881. }
  2882. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2883. forGPBGenericValueKey:(GPBGenericValue *)key {
  2884. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2885. }
  2886. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2887. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2888. #pragma unused(stop)
  2889. block([NSString stringWithFormat:@"%u", key], @(value));
  2890. }];
  2891. }
  2892. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2893. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2894. if (wrapped && value) {
  2895. int32_t result = [wrapped intValue];
  2896. if (!_validationFunc(result)) {
  2897. result = kGPBUnrecognizedEnumeratorValue;
  2898. }
  2899. *value = result;
  2900. }
  2901. return (wrapped != NULL);
  2902. }
  2903. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2904. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2905. if (wrapped && rawValue) {
  2906. *rawValue = [wrapped intValue];
  2907. }
  2908. return (wrapped != NULL);
  2909. }
  2910. - (void)enumerateKeysAndValuesUsingBlock:
  2911. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2912. GPBEnumValidationFunc func = _validationFunc;
  2913. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2914. NSNumber *aValue,
  2915. BOOL *stop) {
  2916. int32_t unwrapped = [aValue intValue];
  2917. if (!func(unwrapped)) {
  2918. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2919. }
  2920. block([aKey unsignedIntValue], unwrapped, stop);
  2921. }];
  2922. }
  2923. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2924. if (otherDictionary) {
  2925. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2926. if (_autocreator) {
  2927. GPBAutocreatedDictionaryModified(_autocreator, self);
  2928. }
  2929. }
  2930. }
  2931. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2932. [_dictionary setObject:@(value) forKey:@(key)];
  2933. if (_autocreator) {
  2934. GPBAutocreatedDictionaryModified(_autocreator, self);
  2935. }
  2936. }
  2937. - (void)removeValueForKey:(uint32_t)aKey {
  2938. [_dictionary removeObjectForKey:@(aKey)];
  2939. }
  2940. - (void)removeAll {
  2941. [_dictionary removeAllObjects];
  2942. }
  2943. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2944. if (!_validationFunc(value)) {
  2945. [NSException raise:NSInvalidArgumentException
  2946. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2947. value];
  2948. }
  2949. [_dictionary setObject:@(value) forKey:@(key)];
  2950. if (_autocreator) {
  2951. GPBAutocreatedDictionaryModified(_autocreator, self);
  2952. }
  2953. }
  2954. @end
  2955. #pragma mark - UInt32 -> Object
  2956. @implementation GPBUInt32ObjectDictionary {
  2957. @package
  2958. NSMutableDictionary *_dictionary;
  2959. }
  2960. + (instancetype)dictionary {
  2961. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  2962. }
  2963. + (instancetype)dictionaryWithObject:(id)object
  2964. forKey:(uint32_t)key {
  2965. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2966. // on to get the type correct.
  2967. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  2968. forKeys:&key
  2969. count:1] autorelease];
  2970. }
  2971. + (instancetype)dictionaryWithObjects:(const id [])objects
  2972. forKeys:(const uint32_t [])keys
  2973. count:(NSUInteger)count {
  2974. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2975. // on to get the type correct.
  2976. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  2977. forKeys:keys
  2978. count:count] autorelease];
  2979. }
  2980. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2981. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2982. // on to get the type correct.
  2983. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2984. }
  2985. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2986. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2987. }
  2988. - (instancetype)init {
  2989. return [self initWithObjects:NULL forKeys:NULL count:0];
  2990. }
  2991. - (instancetype)initWithObjects:(const id [])objects
  2992. forKeys:(const uint32_t [])keys
  2993. count:(NSUInteger)count {
  2994. self = [super init];
  2995. if (self) {
  2996. _dictionary = [[NSMutableDictionary alloc] init];
  2997. if (count && objects && keys) {
  2998. for (NSUInteger i = 0; i < count; ++i) {
  2999. if (!objects[i]) {
  3000. [NSException raise:NSInvalidArgumentException
  3001. format:@"Attempting to add nil object to a Dictionary"];
  3002. }
  3003. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  3004. }
  3005. }
  3006. }
  3007. return self;
  3008. }
  3009. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  3010. self = [self initWithObjects:NULL forKeys:NULL count:0];
  3011. if (self) {
  3012. if (dictionary) {
  3013. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3014. }
  3015. }
  3016. return self;
  3017. }
  3018. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3019. #pragma unused(numItems)
  3020. return [self initWithObjects:NULL forKeys:NULL count:0];
  3021. }
  3022. - (void)dealloc {
  3023. NSAssert(!_autocreator,
  3024. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3025. [self class], _autocreator);
  3026. [_dictionary release];
  3027. [super dealloc];
  3028. }
  3029. - (instancetype)copyWithZone:(NSZone *)zone {
  3030. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  3031. }
  3032. - (BOOL)isEqual:(id)other {
  3033. if (self == other) {
  3034. return YES;
  3035. }
  3036. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  3037. return NO;
  3038. }
  3039. GPBUInt32ObjectDictionary *otherDictionary = other;
  3040. return [_dictionary isEqual:otherDictionary->_dictionary];
  3041. }
  3042. - (NSUInteger)hash {
  3043. return _dictionary.count;
  3044. }
  3045. - (NSString *)description {
  3046. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3047. }
  3048. - (NSUInteger)count {
  3049. return _dictionary.count;
  3050. }
  3051. - (void)enumerateKeysAndObjectsUsingBlock:
  3052. (void (^)(uint32_t key, id object, BOOL *stop))block {
  3053. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3054. id aObject,
  3055. BOOL *stop) {
  3056. block([aKey unsignedIntValue], aObject, stop);
  3057. }];
  3058. }
  3059. - (BOOL)isInitialized {
  3060. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  3061. if (!msg.initialized) {
  3062. return NO;
  3063. }
  3064. }
  3065. return YES;
  3066. }
  3067. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  3068. GPBUInt32ObjectDictionary *newDict =
  3069. [[GPBUInt32ObjectDictionary alloc] init];
  3070. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  3071. GPBMessage *msg,
  3072. BOOL *stop) {
  3073. #pragma unused(stop)
  3074. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3075. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  3076. [copiedMsg release];
  3077. }];
  3078. return newDict;
  3079. }
  3080. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3081. NSUInteger count = _dictionary.count;
  3082. if (count == 0) {
  3083. return 0;
  3084. }
  3085. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3086. GPBDataType keyDataType = field.mapKeyDataType;
  3087. __block size_t result = 0;
  3088. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3089. id aObject,
  3090. BOOL *stop) {
  3091. #pragma unused(stop)
  3092. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3093. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3094. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3095. }];
  3096. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3097. result += tagSize * count;
  3098. return result;
  3099. }
  3100. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3101. asField:(GPBFieldDescriptor *)field {
  3102. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3103. GPBDataType keyDataType = field.mapKeyDataType;
  3104. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3105. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3106. id aObject,
  3107. BOOL *stop) {
  3108. #pragma unused(stop)
  3109. // Write the tag.
  3110. [outputStream writeInt32NoTag:tag];
  3111. // Write the size of the message.
  3112. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3113. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3114. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3115. // Write the fields.
  3116. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3117. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  3118. }];
  3119. }
  3120. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3121. forGPBGenericValueKey:(GPBGenericValue *)key {
  3122. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3123. }
  3124. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3125. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3126. #pragma unused(stop)
  3127. block([NSString stringWithFormat:@"%u", key], object);
  3128. }];
  3129. }
  3130. - (id)objectForKey:(uint32_t)key {
  3131. id result = [_dictionary objectForKey:@(key)];
  3132. return result;
  3133. }
  3134. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3135. if (otherDictionary) {
  3136. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3137. if (_autocreator) {
  3138. GPBAutocreatedDictionaryModified(_autocreator, self);
  3139. }
  3140. }
  3141. }
  3142. - (void)setObject:(id)object forKey:(uint32_t)key {
  3143. if (!object) {
  3144. [NSException raise:NSInvalidArgumentException
  3145. format:@"Attempting to add nil object to a Dictionary"];
  3146. }
  3147. [_dictionary setObject:object forKey:@(key)];
  3148. if (_autocreator) {
  3149. GPBAutocreatedDictionaryModified(_autocreator, self);
  3150. }
  3151. }
  3152. - (void)removeObjectForKey:(uint32_t)aKey {
  3153. [_dictionary removeObjectForKey:@(aKey)];
  3154. }
  3155. - (void)removeAll {
  3156. [_dictionary removeAllObjects];
  3157. }
  3158. @end
  3159. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3160. // This block of code is generated, do not edit it directly.
  3161. #pragma mark - Int32 -> UInt32
  3162. @implementation GPBInt32UInt32Dictionary {
  3163. @package
  3164. NSMutableDictionary *_dictionary;
  3165. }
  3166. + (instancetype)dictionary {
  3167. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3168. }
  3169. + (instancetype)dictionaryWithValue:(uint32_t)value
  3170. forKey:(int32_t)key {
  3171. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3172. // on to get the type correct.
  3173. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  3174. forKeys:&key
  3175. count:1] autorelease];
  3176. }
  3177. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  3178. forKeys:(const int32_t [])keys
  3179. count:(NSUInteger)count {
  3180. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3181. // on to get the type correct.
  3182. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  3183. forKeys:keys
  3184. count:count] autorelease];
  3185. }
  3186. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3187. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3188. // on to get the type correct.
  3189. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3190. }
  3191. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3192. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3193. }
  3194. - (instancetype)init {
  3195. return [self initWithValues:NULL forKeys:NULL count:0];
  3196. }
  3197. - (instancetype)initWithValues:(const uint32_t [])values
  3198. forKeys:(const int32_t [])keys
  3199. count:(NSUInteger)count {
  3200. self = [super init];
  3201. if (self) {
  3202. _dictionary = [[NSMutableDictionary alloc] init];
  3203. if (count && values && keys) {
  3204. for (NSUInteger i = 0; i < count; ++i) {
  3205. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3206. }
  3207. }
  3208. }
  3209. return self;
  3210. }
  3211. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3212. self = [self initWithValues:NULL forKeys:NULL count:0];
  3213. if (self) {
  3214. if (dictionary) {
  3215. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3216. }
  3217. }
  3218. return self;
  3219. }
  3220. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3221. #pragma unused(numItems)
  3222. return [self initWithValues:NULL forKeys:NULL count:0];
  3223. }
  3224. - (void)dealloc {
  3225. NSAssert(!_autocreator,
  3226. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3227. [self class], _autocreator);
  3228. [_dictionary release];
  3229. [super dealloc];
  3230. }
  3231. - (instancetype)copyWithZone:(NSZone *)zone {
  3232. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3233. }
  3234. - (BOOL)isEqual:(id)other {
  3235. if (self == other) {
  3236. return YES;
  3237. }
  3238. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3239. return NO;
  3240. }
  3241. GPBInt32UInt32Dictionary *otherDictionary = other;
  3242. return [_dictionary isEqual:otherDictionary->_dictionary];
  3243. }
  3244. - (NSUInteger)hash {
  3245. return _dictionary.count;
  3246. }
  3247. - (NSString *)description {
  3248. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3249. }
  3250. - (NSUInteger)count {
  3251. return _dictionary.count;
  3252. }
  3253. - (void)enumerateKeysAndValuesUsingBlock:
  3254. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3255. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3256. NSNumber *aValue,
  3257. BOOL *stop) {
  3258. block([aKey intValue], [aValue unsignedIntValue], stop);
  3259. }];
  3260. }
  3261. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3262. NSUInteger count = _dictionary.count;
  3263. if (count == 0) {
  3264. return 0;
  3265. }
  3266. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3267. GPBDataType keyDataType = field.mapKeyDataType;
  3268. __block size_t result = 0;
  3269. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3270. NSNumber *aValue,
  3271. BOOL *stop) {
  3272. #pragma unused(stop)
  3273. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3274. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3275. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3276. }];
  3277. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3278. result += tagSize * count;
  3279. return result;
  3280. }
  3281. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3282. asField:(GPBFieldDescriptor *)field {
  3283. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3284. GPBDataType keyDataType = field.mapKeyDataType;
  3285. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3286. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3287. NSNumber *aValue,
  3288. BOOL *stop) {
  3289. #pragma unused(stop)
  3290. // Write the tag.
  3291. [outputStream writeInt32NoTag:tag];
  3292. // Write the size of the message.
  3293. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3294. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3295. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3296. // Write the fields.
  3297. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3298. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3299. }];
  3300. }
  3301. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3302. forGPBGenericValueKey:(GPBGenericValue *)key {
  3303. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3304. }
  3305. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3306. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3307. #pragma unused(stop)
  3308. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3309. }];
  3310. }
  3311. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3312. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3313. if (wrapped && value) {
  3314. *value = [wrapped unsignedIntValue];
  3315. }
  3316. return (wrapped != NULL);
  3317. }
  3318. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3319. if (otherDictionary) {
  3320. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3321. if (_autocreator) {
  3322. GPBAutocreatedDictionaryModified(_autocreator, self);
  3323. }
  3324. }
  3325. }
  3326. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3327. [_dictionary setObject:@(value) forKey:@(key)];
  3328. if (_autocreator) {
  3329. GPBAutocreatedDictionaryModified(_autocreator, self);
  3330. }
  3331. }
  3332. - (void)removeValueForKey:(int32_t)aKey {
  3333. [_dictionary removeObjectForKey:@(aKey)];
  3334. }
  3335. - (void)removeAll {
  3336. [_dictionary removeAllObjects];
  3337. }
  3338. @end
  3339. #pragma mark - Int32 -> Int32
  3340. @implementation GPBInt32Int32Dictionary {
  3341. @package
  3342. NSMutableDictionary *_dictionary;
  3343. }
  3344. + (instancetype)dictionary {
  3345. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3346. }
  3347. + (instancetype)dictionaryWithValue:(int32_t)value
  3348. forKey:(int32_t)key {
  3349. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3350. // on to get the type correct.
  3351. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3352. forKeys:&key
  3353. count:1] autorelease];
  3354. }
  3355. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3356. forKeys:(const int32_t [])keys
  3357. count:(NSUInteger)count {
  3358. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3359. // on to get the type correct.
  3360. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3361. forKeys:keys
  3362. count:count] autorelease];
  3363. }
  3364. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3365. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3366. // on to get the type correct.
  3367. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3368. }
  3369. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3370. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3371. }
  3372. - (instancetype)init {
  3373. return [self initWithValues:NULL forKeys:NULL count:0];
  3374. }
  3375. - (instancetype)initWithValues:(const int32_t [])values
  3376. forKeys:(const int32_t [])keys
  3377. count:(NSUInteger)count {
  3378. self = [super init];
  3379. if (self) {
  3380. _dictionary = [[NSMutableDictionary alloc] init];
  3381. if (count && values && keys) {
  3382. for (NSUInteger i = 0; i < count; ++i) {
  3383. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3384. }
  3385. }
  3386. }
  3387. return self;
  3388. }
  3389. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3390. self = [self initWithValues:NULL forKeys:NULL count:0];
  3391. if (self) {
  3392. if (dictionary) {
  3393. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3394. }
  3395. }
  3396. return self;
  3397. }
  3398. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3399. #pragma unused(numItems)
  3400. return [self initWithValues:NULL forKeys:NULL count:0];
  3401. }
  3402. - (void)dealloc {
  3403. NSAssert(!_autocreator,
  3404. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3405. [self class], _autocreator);
  3406. [_dictionary release];
  3407. [super dealloc];
  3408. }
  3409. - (instancetype)copyWithZone:(NSZone *)zone {
  3410. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3411. }
  3412. - (BOOL)isEqual:(id)other {
  3413. if (self == other) {
  3414. return YES;
  3415. }
  3416. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3417. return NO;
  3418. }
  3419. GPBInt32Int32Dictionary *otherDictionary = other;
  3420. return [_dictionary isEqual:otherDictionary->_dictionary];
  3421. }
  3422. - (NSUInteger)hash {
  3423. return _dictionary.count;
  3424. }
  3425. - (NSString *)description {
  3426. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3427. }
  3428. - (NSUInteger)count {
  3429. return _dictionary.count;
  3430. }
  3431. - (void)enumerateKeysAndValuesUsingBlock:
  3432. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3433. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3434. NSNumber *aValue,
  3435. BOOL *stop) {
  3436. block([aKey intValue], [aValue intValue], stop);
  3437. }];
  3438. }
  3439. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3440. NSUInteger count = _dictionary.count;
  3441. if (count == 0) {
  3442. return 0;
  3443. }
  3444. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3445. GPBDataType keyDataType = field.mapKeyDataType;
  3446. __block size_t result = 0;
  3447. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3448. NSNumber *aValue,
  3449. BOOL *stop) {
  3450. #pragma unused(stop)
  3451. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3452. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3453. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3454. }];
  3455. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3456. result += tagSize * count;
  3457. return result;
  3458. }
  3459. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3460. asField:(GPBFieldDescriptor *)field {
  3461. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3462. GPBDataType keyDataType = field.mapKeyDataType;
  3463. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3464. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3465. NSNumber *aValue,
  3466. BOOL *stop) {
  3467. #pragma unused(stop)
  3468. // Write the tag.
  3469. [outputStream writeInt32NoTag:tag];
  3470. // Write the size of the message.
  3471. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3472. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3473. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3474. // Write the fields.
  3475. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3476. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3477. }];
  3478. }
  3479. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3480. forGPBGenericValueKey:(GPBGenericValue *)key {
  3481. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3482. }
  3483. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3484. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3485. #pragma unused(stop)
  3486. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3487. }];
  3488. }
  3489. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3490. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3491. if (wrapped && value) {
  3492. *value = [wrapped intValue];
  3493. }
  3494. return (wrapped != NULL);
  3495. }
  3496. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3497. if (otherDictionary) {
  3498. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3499. if (_autocreator) {
  3500. GPBAutocreatedDictionaryModified(_autocreator, self);
  3501. }
  3502. }
  3503. }
  3504. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3505. [_dictionary setObject:@(value) forKey:@(key)];
  3506. if (_autocreator) {
  3507. GPBAutocreatedDictionaryModified(_autocreator, self);
  3508. }
  3509. }
  3510. - (void)removeValueForKey:(int32_t)aKey {
  3511. [_dictionary removeObjectForKey:@(aKey)];
  3512. }
  3513. - (void)removeAll {
  3514. [_dictionary removeAllObjects];
  3515. }
  3516. @end
  3517. #pragma mark - Int32 -> UInt64
  3518. @implementation GPBInt32UInt64Dictionary {
  3519. @package
  3520. NSMutableDictionary *_dictionary;
  3521. }
  3522. + (instancetype)dictionary {
  3523. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3524. }
  3525. + (instancetype)dictionaryWithValue:(uint64_t)value
  3526. forKey:(int32_t)key {
  3527. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3528. // on to get the type correct.
  3529. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3530. forKeys:&key
  3531. count:1] autorelease];
  3532. }
  3533. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3534. forKeys:(const int32_t [])keys
  3535. count:(NSUInteger)count {
  3536. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3537. // on to get the type correct.
  3538. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3539. forKeys:keys
  3540. count:count] autorelease];
  3541. }
  3542. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3543. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3544. // on to get the type correct.
  3545. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3546. }
  3547. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3548. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3549. }
  3550. - (instancetype)init {
  3551. return [self initWithValues:NULL forKeys:NULL count:0];
  3552. }
  3553. - (instancetype)initWithValues:(const uint64_t [])values
  3554. forKeys:(const int32_t [])keys
  3555. count:(NSUInteger)count {
  3556. self = [super init];
  3557. if (self) {
  3558. _dictionary = [[NSMutableDictionary alloc] init];
  3559. if (count && values && keys) {
  3560. for (NSUInteger i = 0; i < count; ++i) {
  3561. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3562. }
  3563. }
  3564. }
  3565. return self;
  3566. }
  3567. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3568. self = [self initWithValues:NULL forKeys:NULL count:0];
  3569. if (self) {
  3570. if (dictionary) {
  3571. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3572. }
  3573. }
  3574. return self;
  3575. }
  3576. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3577. #pragma unused(numItems)
  3578. return [self initWithValues:NULL forKeys:NULL count:0];
  3579. }
  3580. - (void)dealloc {
  3581. NSAssert(!_autocreator,
  3582. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3583. [self class], _autocreator);
  3584. [_dictionary release];
  3585. [super dealloc];
  3586. }
  3587. - (instancetype)copyWithZone:(NSZone *)zone {
  3588. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3589. }
  3590. - (BOOL)isEqual:(id)other {
  3591. if (self == other) {
  3592. return YES;
  3593. }
  3594. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3595. return NO;
  3596. }
  3597. GPBInt32UInt64Dictionary *otherDictionary = other;
  3598. return [_dictionary isEqual:otherDictionary->_dictionary];
  3599. }
  3600. - (NSUInteger)hash {
  3601. return _dictionary.count;
  3602. }
  3603. - (NSString *)description {
  3604. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3605. }
  3606. - (NSUInteger)count {
  3607. return _dictionary.count;
  3608. }
  3609. - (void)enumerateKeysAndValuesUsingBlock:
  3610. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3611. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3612. NSNumber *aValue,
  3613. BOOL *stop) {
  3614. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3615. }];
  3616. }
  3617. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3618. NSUInteger count = _dictionary.count;
  3619. if (count == 0) {
  3620. return 0;
  3621. }
  3622. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3623. GPBDataType keyDataType = field.mapKeyDataType;
  3624. __block size_t result = 0;
  3625. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3626. NSNumber *aValue,
  3627. BOOL *stop) {
  3628. #pragma unused(stop)
  3629. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3630. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3631. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3632. }];
  3633. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3634. result += tagSize * count;
  3635. return result;
  3636. }
  3637. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3638. asField:(GPBFieldDescriptor *)field {
  3639. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3640. GPBDataType keyDataType = field.mapKeyDataType;
  3641. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3642. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3643. NSNumber *aValue,
  3644. BOOL *stop) {
  3645. #pragma unused(stop)
  3646. // Write the tag.
  3647. [outputStream writeInt32NoTag:tag];
  3648. // Write the size of the message.
  3649. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3650. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3651. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3652. // Write the fields.
  3653. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3654. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3655. }];
  3656. }
  3657. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3658. forGPBGenericValueKey:(GPBGenericValue *)key {
  3659. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3660. }
  3661. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3662. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3663. #pragma unused(stop)
  3664. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3665. }];
  3666. }
  3667. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3668. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3669. if (wrapped && value) {
  3670. *value = [wrapped unsignedLongLongValue];
  3671. }
  3672. return (wrapped != NULL);
  3673. }
  3674. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3675. if (otherDictionary) {
  3676. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3677. if (_autocreator) {
  3678. GPBAutocreatedDictionaryModified(_autocreator, self);
  3679. }
  3680. }
  3681. }
  3682. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3683. [_dictionary setObject:@(value) forKey:@(key)];
  3684. if (_autocreator) {
  3685. GPBAutocreatedDictionaryModified(_autocreator, self);
  3686. }
  3687. }
  3688. - (void)removeValueForKey:(int32_t)aKey {
  3689. [_dictionary removeObjectForKey:@(aKey)];
  3690. }
  3691. - (void)removeAll {
  3692. [_dictionary removeAllObjects];
  3693. }
  3694. @end
  3695. #pragma mark - Int32 -> Int64
  3696. @implementation GPBInt32Int64Dictionary {
  3697. @package
  3698. NSMutableDictionary *_dictionary;
  3699. }
  3700. + (instancetype)dictionary {
  3701. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3702. }
  3703. + (instancetype)dictionaryWithValue:(int64_t)value
  3704. forKey:(int32_t)key {
  3705. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3706. // on to get the type correct.
  3707. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3708. forKeys:&key
  3709. count:1] autorelease];
  3710. }
  3711. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3712. forKeys:(const int32_t [])keys
  3713. count:(NSUInteger)count {
  3714. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3715. // on to get the type correct.
  3716. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3717. forKeys:keys
  3718. count:count] autorelease];
  3719. }
  3720. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3721. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3722. // on to get the type correct.
  3723. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3724. }
  3725. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3726. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3727. }
  3728. - (instancetype)init {
  3729. return [self initWithValues:NULL forKeys:NULL count:0];
  3730. }
  3731. - (instancetype)initWithValues:(const int64_t [])values
  3732. forKeys:(const int32_t [])keys
  3733. count:(NSUInteger)count {
  3734. self = [super init];
  3735. if (self) {
  3736. _dictionary = [[NSMutableDictionary alloc] init];
  3737. if (count && values && keys) {
  3738. for (NSUInteger i = 0; i < count; ++i) {
  3739. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3740. }
  3741. }
  3742. }
  3743. return self;
  3744. }
  3745. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3746. self = [self initWithValues:NULL forKeys:NULL count:0];
  3747. if (self) {
  3748. if (dictionary) {
  3749. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3750. }
  3751. }
  3752. return self;
  3753. }
  3754. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3755. #pragma unused(numItems)
  3756. return [self initWithValues:NULL forKeys:NULL count:0];
  3757. }
  3758. - (void)dealloc {
  3759. NSAssert(!_autocreator,
  3760. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3761. [self class], _autocreator);
  3762. [_dictionary release];
  3763. [super dealloc];
  3764. }
  3765. - (instancetype)copyWithZone:(NSZone *)zone {
  3766. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3767. }
  3768. - (BOOL)isEqual:(id)other {
  3769. if (self == other) {
  3770. return YES;
  3771. }
  3772. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3773. return NO;
  3774. }
  3775. GPBInt32Int64Dictionary *otherDictionary = other;
  3776. return [_dictionary isEqual:otherDictionary->_dictionary];
  3777. }
  3778. - (NSUInteger)hash {
  3779. return _dictionary.count;
  3780. }
  3781. - (NSString *)description {
  3782. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3783. }
  3784. - (NSUInteger)count {
  3785. return _dictionary.count;
  3786. }
  3787. - (void)enumerateKeysAndValuesUsingBlock:
  3788. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3789. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3790. NSNumber *aValue,
  3791. BOOL *stop) {
  3792. block([aKey intValue], [aValue longLongValue], stop);
  3793. }];
  3794. }
  3795. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3796. NSUInteger count = _dictionary.count;
  3797. if (count == 0) {
  3798. return 0;
  3799. }
  3800. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3801. GPBDataType keyDataType = field.mapKeyDataType;
  3802. __block size_t result = 0;
  3803. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3804. NSNumber *aValue,
  3805. BOOL *stop) {
  3806. #pragma unused(stop)
  3807. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3808. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3809. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3810. }];
  3811. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3812. result += tagSize * count;
  3813. return result;
  3814. }
  3815. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3816. asField:(GPBFieldDescriptor *)field {
  3817. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3818. GPBDataType keyDataType = field.mapKeyDataType;
  3819. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3820. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3821. NSNumber *aValue,
  3822. BOOL *stop) {
  3823. #pragma unused(stop)
  3824. // Write the tag.
  3825. [outputStream writeInt32NoTag:tag];
  3826. // Write the size of the message.
  3827. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3828. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3829. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3830. // Write the fields.
  3831. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3832. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3833. }];
  3834. }
  3835. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3836. forGPBGenericValueKey:(GPBGenericValue *)key {
  3837. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3838. }
  3839. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3840. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3841. #pragma unused(stop)
  3842. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3843. }];
  3844. }
  3845. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3846. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3847. if (wrapped && value) {
  3848. *value = [wrapped longLongValue];
  3849. }
  3850. return (wrapped != NULL);
  3851. }
  3852. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3853. if (otherDictionary) {
  3854. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3855. if (_autocreator) {
  3856. GPBAutocreatedDictionaryModified(_autocreator, self);
  3857. }
  3858. }
  3859. }
  3860. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3861. [_dictionary setObject:@(value) forKey:@(key)];
  3862. if (_autocreator) {
  3863. GPBAutocreatedDictionaryModified(_autocreator, self);
  3864. }
  3865. }
  3866. - (void)removeValueForKey:(int32_t)aKey {
  3867. [_dictionary removeObjectForKey:@(aKey)];
  3868. }
  3869. - (void)removeAll {
  3870. [_dictionary removeAllObjects];
  3871. }
  3872. @end
  3873. #pragma mark - Int32 -> Bool
  3874. @implementation GPBInt32BoolDictionary {
  3875. @package
  3876. NSMutableDictionary *_dictionary;
  3877. }
  3878. + (instancetype)dictionary {
  3879. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3880. }
  3881. + (instancetype)dictionaryWithValue:(BOOL)value
  3882. forKey:(int32_t)key {
  3883. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3884. // on to get the type correct.
  3885. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3886. forKeys:&key
  3887. count:1] autorelease];
  3888. }
  3889. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3890. forKeys:(const int32_t [])keys
  3891. count:(NSUInteger)count {
  3892. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3893. // on to get the type correct.
  3894. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3895. forKeys:keys
  3896. count:count] autorelease];
  3897. }
  3898. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3899. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3900. // on to get the type correct.
  3901. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3902. }
  3903. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3904. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3905. }
  3906. - (instancetype)init {
  3907. return [self initWithValues:NULL forKeys:NULL count:0];
  3908. }
  3909. - (instancetype)initWithValues:(const BOOL [])values
  3910. forKeys:(const int32_t [])keys
  3911. count:(NSUInteger)count {
  3912. self = [super init];
  3913. if (self) {
  3914. _dictionary = [[NSMutableDictionary alloc] init];
  3915. if (count && values && keys) {
  3916. for (NSUInteger i = 0; i < count; ++i) {
  3917. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3918. }
  3919. }
  3920. }
  3921. return self;
  3922. }
  3923. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3924. self = [self initWithValues:NULL forKeys:NULL count:0];
  3925. if (self) {
  3926. if (dictionary) {
  3927. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3928. }
  3929. }
  3930. return self;
  3931. }
  3932. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3933. #pragma unused(numItems)
  3934. return [self initWithValues:NULL forKeys:NULL count:0];
  3935. }
  3936. - (void)dealloc {
  3937. NSAssert(!_autocreator,
  3938. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3939. [self class], _autocreator);
  3940. [_dictionary release];
  3941. [super dealloc];
  3942. }
  3943. - (instancetype)copyWithZone:(NSZone *)zone {
  3944. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3945. }
  3946. - (BOOL)isEqual:(id)other {
  3947. if (self == other) {
  3948. return YES;
  3949. }
  3950. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3951. return NO;
  3952. }
  3953. GPBInt32BoolDictionary *otherDictionary = other;
  3954. return [_dictionary isEqual:otherDictionary->_dictionary];
  3955. }
  3956. - (NSUInteger)hash {
  3957. return _dictionary.count;
  3958. }
  3959. - (NSString *)description {
  3960. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3961. }
  3962. - (NSUInteger)count {
  3963. return _dictionary.count;
  3964. }
  3965. - (void)enumerateKeysAndValuesUsingBlock:
  3966. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3967. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3968. NSNumber *aValue,
  3969. BOOL *stop) {
  3970. block([aKey intValue], [aValue boolValue], stop);
  3971. }];
  3972. }
  3973. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3974. NSUInteger count = _dictionary.count;
  3975. if (count == 0) {
  3976. return 0;
  3977. }
  3978. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3979. GPBDataType keyDataType = field.mapKeyDataType;
  3980. __block size_t result = 0;
  3981. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3982. NSNumber *aValue,
  3983. BOOL *stop) {
  3984. #pragma unused(stop)
  3985. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3986. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3987. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3988. }];
  3989. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3990. result += tagSize * count;
  3991. return result;
  3992. }
  3993. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3994. asField:(GPBFieldDescriptor *)field {
  3995. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3996. GPBDataType keyDataType = field.mapKeyDataType;
  3997. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3998. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3999. NSNumber *aValue,
  4000. BOOL *stop) {
  4001. #pragma unused(stop)
  4002. // Write the tag.
  4003. [outputStream writeInt32NoTag:tag];
  4004. // Write the size of the message.
  4005. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4006. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  4007. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4008. // Write the fields.
  4009. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4010. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  4011. }];
  4012. }
  4013. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4014. forGPBGenericValueKey:(GPBGenericValue *)key {
  4015. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  4016. }
  4017. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4018. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  4019. #pragma unused(stop)
  4020. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  4021. }];
  4022. }
  4023. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  4024. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4025. if (wrapped && value) {
  4026. *value = [wrapped boolValue];
  4027. }
  4028. return (wrapped != NULL);
  4029. }
  4030. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  4031. if (otherDictionary) {
  4032. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4033. if (_autocreator) {
  4034. GPBAutocreatedDictionaryModified(_autocreator, self);
  4035. }
  4036. }
  4037. }
  4038. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  4039. [_dictionary setObject:@(value) forKey:@(key)];
  4040. if (_autocreator) {
  4041. GPBAutocreatedDictionaryModified(_autocreator, self);
  4042. }
  4043. }
  4044. - (void)removeValueForKey:(int32_t)aKey {
  4045. [_dictionary removeObjectForKey:@(aKey)];
  4046. }
  4047. - (void)removeAll {
  4048. [_dictionary removeAllObjects];
  4049. }
  4050. @end
  4051. #pragma mark - Int32 -> Float
  4052. @implementation GPBInt32FloatDictionary {
  4053. @package
  4054. NSMutableDictionary *_dictionary;
  4055. }
  4056. + (instancetype)dictionary {
  4057. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4058. }
  4059. + (instancetype)dictionaryWithValue:(float)value
  4060. forKey:(int32_t)key {
  4061. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4062. // on to get the type correct.
  4063. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  4064. forKeys:&key
  4065. count:1] autorelease];
  4066. }
  4067. + (instancetype)dictionaryWithValues:(const float [])values
  4068. forKeys:(const int32_t [])keys
  4069. count:(NSUInteger)count {
  4070. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4071. // on to get the type correct.
  4072. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  4073. forKeys:keys
  4074. count:count] autorelease];
  4075. }
  4076. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4077. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4078. // on to get the type correct.
  4079. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4080. }
  4081. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4082. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4083. }
  4084. - (instancetype)init {
  4085. return [self initWithValues:NULL forKeys:NULL count:0];
  4086. }
  4087. - (instancetype)initWithValues:(const float [])values
  4088. forKeys:(const int32_t [])keys
  4089. count:(NSUInteger)count {
  4090. self = [super init];
  4091. if (self) {
  4092. _dictionary = [[NSMutableDictionary alloc] init];
  4093. if (count && values && keys) {
  4094. for (NSUInteger i = 0; i < count; ++i) {
  4095. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4096. }
  4097. }
  4098. }
  4099. return self;
  4100. }
  4101. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4102. self = [self initWithValues:NULL forKeys:NULL count:0];
  4103. if (self) {
  4104. if (dictionary) {
  4105. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4106. }
  4107. }
  4108. return self;
  4109. }
  4110. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4111. #pragma unused(numItems)
  4112. return [self initWithValues:NULL forKeys:NULL count:0];
  4113. }
  4114. - (void)dealloc {
  4115. NSAssert(!_autocreator,
  4116. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4117. [self class], _autocreator);
  4118. [_dictionary release];
  4119. [super dealloc];
  4120. }
  4121. - (instancetype)copyWithZone:(NSZone *)zone {
  4122. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4123. }
  4124. - (BOOL)isEqual:(id)other {
  4125. if (self == other) {
  4126. return YES;
  4127. }
  4128. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4129. return NO;
  4130. }
  4131. GPBInt32FloatDictionary *otherDictionary = other;
  4132. return [_dictionary isEqual:otherDictionary->_dictionary];
  4133. }
  4134. - (NSUInteger)hash {
  4135. return _dictionary.count;
  4136. }
  4137. - (NSString *)description {
  4138. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4139. }
  4140. - (NSUInteger)count {
  4141. return _dictionary.count;
  4142. }
  4143. - (void)enumerateKeysAndValuesUsingBlock:
  4144. (void (^)(int32_t key, float value, BOOL *stop))block {
  4145. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4146. NSNumber *aValue,
  4147. BOOL *stop) {
  4148. block([aKey intValue], [aValue floatValue], stop);
  4149. }];
  4150. }
  4151. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4152. NSUInteger count = _dictionary.count;
  4153. if (count == 0) {
  4154. return 0;
  4155. }
  4156. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4157. GPBDataType keyDataType = field.mapKeyDataType;
  4158. __block size_t result = 0;
  4159. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4160. NSNumber *aValue,
  4161. BOOL *stop) {
  4162. #pragma unused(stop)
  4163. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4164. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4165. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4166. }];
  4167. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4168. result += tagSize * count;
  4169. return result;
  4170. }
  4171. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4172. asField:(GPBFieldDescriptor *)field {
  4173. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4174. GPBDataType keyDataType = field.mapKeyDataType;
  4175. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4176. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4177. NSNumber *aValue,
  4178. BOOL *stop) {
  4179. #pragma unused(stop)
  4180. // Write the tag.
  4181. [outputStream writeInt32NoTag:tag];
  4182. // Write the size of the message.
  4183. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4184. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4185. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4186. // Write the fields.
  4187. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4188. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4189. }];
  4190. }
  4191. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4192. forGPBGenericValueKey:(GPBGenericValue *)key {
  4193. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4194. }
  4195. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4196. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4197. #pragma unused(stop)
  4198. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4199. }];
  4200. }
  4201. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  4202. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4203. if (wrapped && value) {
  4204. *value = [wrapped floatValue];
  4205. }
  4206. return (wrapped != NULL);
  4207. }
  4208. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4209. if (otherDictionary) {
  4210. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4211. if (_autocreator) {
  4212. GPBAutocreatedDictionaryModified(_autocreator, self);
  4213. }
  4214. }
  4215. }
  4216. - (void)setValue:(float)value forKey:(int32_t)key {
  4217. [_dictionary setObject:@(value) forKey:@(key)];
  4218. if (_autocreator) {
  4219. GPBAutocreatedDictionaryModified(_autocreator, self);
  4220. }
  4221. }
  4222. - (void)removeValueForKey:(int32_t)aKey {
  4223. [_dictionary removeObjectForKey:@(aKey)];
  4224. }
  4225. - (void)removeAll {
  4226. [_dictionary removeAllObjects];
  4227. }
  4228. @end
  4229. #pragma mark - Int32 -> Double
  4230. @implementation GPBInt32DoubleDictionary {
  4231. @package
  4232. NSMutableDictionary *_dictionary;
  4233. }
  4234. + (instancetype)dictionary {
  4235. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4236. }
  4237. + (instancetype)dictionaryWithValue:(double)value
  4238. forKey:(int32_t)key {
  4239. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4240. // on to get the type correct.
  4241. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  4242. forKeys:&key
  4243. count:1] autorelease];
  4244. }
  4245. + (instancetype)dictionaryWithValues:(const double [])values
  4246. forKeys:(const int32_t [])keys
  4247. count:(NSUInteger)count {
  4248. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4249. // on to get the type correct.
  4250. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  4251. forKeys:keys
  4252. count:count] autorelease];
  4253. }
  4254. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4255. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4256. // on to get the type correct.
  4257. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4258. }
  4259. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4260. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4261. }
  4262. - (instancetype)init {
  4263. return [self initWithValues:NULL forKeys:NULL count:0];
  4264. }
  4265. - (instancetype)initWithValues:(const double [])values
  4266. forKeys:(const int32_t [])keys
  4267. count:(NSUInteger)count {
  4268. self = [super init];
  4269. if (self) {
  4270. _dictionary = [[NSMutableDictionary alloc] init];
  4271. if (count && values && keys) {
  4272. for (NSUInteger i = 0; i < count; ++i) {
  4273. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4274. }
  4275. }
  4276. }
  4277. return self;
  4278. }
  4279. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4280. self = [self initWithValues:NULL forKeys:NULL count:0];
  4281. if (self) {
  4282. if (dictionary) {
  4283. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4284. }
  4285. }
  4286. return self;
  4287. }
  4288. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4289. #pragma unused(numItems)
  4290. return [self initWithValues:NULL forKeys:NULL count:0];
  4291. }
  4292. - (void)dealloc {
  4293. NSAssert(!_autocreator,
  4294. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4295. [self class], _autocreator);
  4296. [_dictionary release];
  4297. [super dealloc];
  4298. }
  4299. - (instancetype)copyWithZone:(NSZone *)zone {
  4300. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4301. }
  4302. - (BOOL)isEqual:(id)other {
  4303. if (self == other) {
  4304. return YES;
  4305. }
  4306. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4307. return NO;
  4308. }
  4309. GPBInt32DoubleDictionary *otherDictionary = other;
  4310. return [_dictionary isEqual:otherDictionary->_dictionary];
  4311. }
  4312. - (NSUInteger)hash {
  4313. return _dictionary.count;
  4314. }
  4315. - (NSString *)description {
  4316. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4317. }
  4318. - (NSUInteger)count {
  4319. return _dictionary.count;
  4320. }
  4321. - (void)enumerateKeysAndValuesUsingBlock:
  4322. (void (^)(int32_t key, double value, BOOL *stop))block {
  4323. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4324. NSNumber *aValue,
  4325. BOOL *stop) {
  4326. block([aKey intValue], [aValue doubleValue], stop);
  4327. }];
  4328. }
  4329. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4330. NSUInteger count = _dictionary.count;
  4331. if (count == 0) {
  4332. return 0;
  4333. }
  4334. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4335. GPBDataType keyDataType = field.mapKeyDataType;
  4336. __block size_t result = 0;
  4337. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4338. NSNumber *aValue,
  4339. BOOL *stop) {
  4340. #pragma unused(stop)
  4341. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4342. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4343. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4344. }];
  4345. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4346. result += tagSize * count;
  4347. return result;
  4348. }
  4349. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4350. asField:(GPBFieldDescriptor *)field {
  4351. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4352. GPBDataType keyDataType = field.mapKeyDataType;
  4353. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4354. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4355. NSNumber *aValue,
  4356. BOOL *stop) {
  4357. #pragma unused(stop)
  4358. // Write the tag.
  4359. [outputStream writeInt32NoTag:tag];
  4360. // Write the size of the message.
  4361. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4362. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4363. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4364. // Write the fields.
  4365. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4366. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4367. }];
  4368. }
  4369. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4370. forGPBGenericValueKey:(GPBGenericValue *)key {
  4371. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4372. }
  4373. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4374. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4375. #pragma unused(stop)
  4376. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4377. }];
  4378. }
  4379. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4380. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4381. if (wrapped && value) {
  4382. *value = [wrapped doubleValue];
  4383. }
  4384. return (wrapped != NULL);
  4385. }
  4386. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4387. if (otherDictionary) {
  4388. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4389. if (_autocreator) {
  4390. GPBAutocreatedDictionaryModified(_autocreator, self);
  4391. }
  4392. }
  4393. }
  4394. - (void)setValue:(double)value forKey:(int32_t)key {
  4395. [_dictionary setObject:@(value) forKey:@(key)];
  4396. if (_autocreator) {
  4397. GPBAutocreatedDictionaryModified(_autocreator, self);
  4398. }
  4399. }
  4400. - (void)removeValueForKey:(int32_t)aKey {
  4401. [_dictionary removeObjectForKey:@(aKey)];
  4402. }
  4403. - (void)removeAll {
  4404. [_dictionary removeAllObjects];
  4405. }
  4406. @end
  4407. #pragma mark - Int32 -> Enum
  4408. @implementation GPBInt32EnumDictionary {
  4409. @package
  4410. NSMutableDictionary *_dictionary;
  4411. GPBEnumValidationFunc _validationFunc;
  4412. }
  4413. @synthesize validationFunc = _validationFunc;
  4414. + (instancetype)dictionary {
  4415. return [[[self alloc] initWithValidationFunction:NULL
  4416. rawValues:NULL
  4417. forKeys:NULL
  4418. count:0] autorelease];
  4419. }
  4420. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4421. return [[[self alloc] initWithValidationFunction:func
  4422. rawValues:NULL
  4423. forKeys:NULL
  4424. count:0] autorelease];
  4425. }
  4426. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4427. rawValue:(int32_t)rawValue
  4428. forKey:(int32_t)key {
  4429. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4430. // on to get the type correct.
  4431. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4432. rawValues:&rawValue
  4433. forKeys:&key
  4434. count:1] autorelease];
  4435. }
  4436. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4437. rawValues:(const int32_t [])rawValues
  4438. forKeys:(const int32_t [])keys
  4439. count:(NSUInteger)count {
  4440. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4441. // on to get the type correct.
  4442. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4443. rawValues:rawValues
  4444. forKeys:keys
  4445. count:count] autorelease];
  4446. }
  4447. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4448. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4449. // on to get the type correct.
  4450. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4451. }
  4452. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4453. capacity:(NSUInteger)numItems {
  4454. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4455. }
  4456. - (instancetype)init {
  4457. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4458. }
  4459. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4460. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4461. }
  4462. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4463. rawValues:(const int32_t [])rawValues
  4464. forKeys:(const int32_t [])keys
  4465. count:(NSUInteger)count {
  4466. self = [super init];
  4467. if (self) {
  4468. _dictionary = [[NSMutableDictionary alloc] init];
  4469. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4470. if (count && rawValues && keys) {
  4471. for (NSUInteger i = 0; i < count; ++i) {
  4472. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4473. }
  4474. }
  4475. }
  4476. return self;
  4477. }
  4478. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4479. self = [self initWithValidationFunction:dictionary.validationFunc
  4480. rawValues:NULL
  4481. forKeys:NULL
  4482. count:0];
  4483. if (self) {
  4484. if (dictionary) {
  4485. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4486. }
  4487. }
  4488. return self;
  4489. }
  4490. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4491. capacity:(NSUInteger)numItems {
  4492. #pragma unused(numItems)
  4493. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4494. }
  4495. - (void)dealloc {
  4496. NSAssert(!_autocreator,
  4497. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4498. [self class], _autocreator);
  4499. [_dictionary release];
  4500. [super dealloc];
  4501. }
  4502. - (instancetype)copyWithZone:(NSZone *)zone {
  4503. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4504. }
  4505. - (BOOL)isEqual:(id)other {
  4506. if (self == other) {
  4507. return YES;
  4508. }
  4509. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4510. return NO;
  4511. }
  4512. GPBInt32EnumDictionary *otherDictionary = other;
  4513. return [_dictionary isEqual:otherDictionary->_dictionary];
  4514. }
  4515. - (NSUInteger)hash {
  4516. return _dictionary.count;
  4517. }
  4518. - (NSString *)description {
  4519. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4520. }
  4521. - (NSUInteger)count {
  4522. return _dictionary.count;
  4523. }
  4524. - (void)enumerateKeysAndRawValuesUsingBlock:
  4525. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4526. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4527. NSNumber *aValue,
  4528. BOOL *stop) {
  4529. block([aKey intValue], [aValue intValue], stop);
  4530. }];
  4531. }
  4532. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4533. NSUInteger count = _dictionary.count;
  4534. if (count == 0) {
  4535. return 0;
  4536. }
  4537. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4538. GPBDataType keyDataType = field.mapKeyDataType;
  4539. __block size_t result = 0;
  4540. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4541. NSNumber *aValue,
  4542. BOOL *stop) {
  4543. #pragma unused(stop)
  4544. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4545. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4546. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4547. }];
  4548. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4549. result += tagSize * count;
  4550. return result;
  4551. }
  4552. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4553. asField:(GPBFieldDescriptor *)field {
  4554. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4555. GPBDataType keyDataType = field.mapKeyDataType;
  4556. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4557. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4558. NSNumber *aValue,
  4559. BOOL *stop) {
  4560. #pragma unused(stop)
  4561. // Write the tag.
  4562. [outputStream writeInt32NoTag:tag];
  4563. // Write the size of the message.
  4564. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4565. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4566. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4567. // Write the fields.
  4568. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4569. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4570. }];
  4571. }
  4572. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4573. forKey:(GPBGenericValue *)key
  4574. keyDataType:(GPBDataType)keyDataType {
  4575. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4576. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4577. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4578. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4579. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4580. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4581. [outputStream release];
  4582. return data;
  4583. }
  4584. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4585. forGPBGenericValueKey:(GPBGenericValue *)key {
  4586. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4587. }
  4588. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4589. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4590. #pragma unused(stop)
  4591. block([NSString stringWithFormat:@"%d", key], @(value));
  4592. }];
  4593. }
  4594. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4595. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4596. if (wrapped && value) {
  4597. int32_t result = [wrapped intValue];
  4598. if (!_validationFunc(result)) {
  4599. result = kGPBUnrecognizedEnumeratorValue;
  4600. }
  4601. *value = result;
  4602. }
  4603. return (wrapped != NULL);
  4604. }
  4605. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4606. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4607. if (wrapped && rawValue) {
  4608. *rawValue = [wrapped intValue];
  4609. }
  4610. return (wrapped != NULL);
  4611. }
  4612. - (void)enumerateKeysAndValuesUsingBlock:
  4613. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4614. GPBEnumValidationFunc func = _validationFunc;
  4615. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4616. NSNumber *aValue,
  4617. BOOL *stop) {
  4618. int32_t unwrapped = [aValue intValue];
  4619. if (!func(unwrapped)) {
  4620. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4621. }
  4622. block([aKey intValue], unwrapped, stop);
  4623. }];
  4624. }
  4625. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4626. if (otherDictionary) {
  4627. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4628. if (_autocreator) {
  4629. GPBAutocreatedDictionaryModified(_autocreator, self);
  4630. }
  4631. }
  4632. }
  4633. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4634. [_dictionary setObject:@(value) forKey:@(key)];
  4635. if (_autocreator) {
  4636. GPBAutocreatedDictionaryModified(_autocreator, self);
  4637. }
  4638. }
  4639. - (void)removeValueForKey:(int32_t)aKey {
  4640. [_dictionary removeObjectForKey:@(aKey)];
  4641. }
  4642. - (void)removeAll {
  4643. [_dictionary removeAllObjects];
  4644. }
  4645. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4646. if (!_validationFunc(value)) {
  4647. [NSException raise:NSInvalidArgumentException
  4648. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4649. value];
  4650. }
  4651. [_dictionary setObject:@(value) forKey:@(key)];
  4652. if (_autocreator) {
  4653. GPBAutocreatedDictionaryModified(_autocreator, self);
  4654. }
  4655. }
  4656. @end
  4657. #pragma mark - Int32 -> Object
  4658. @implementation GPBInt32ObjectDictionary {
  4659. @package
  4660. NSMutableDictionary *_dictionary;
  4661. }
  4662. + (instancetype)dictionary {
  4663. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4664. }
  4665. + (instancetype)dictionaryWithObject:(id)object
  4666. forKey:(int32_t)key {
  4667. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4668. // on to get the type correct.
  4669. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4670. forKeys:&key
  4671. count:1] autorelease];
  4672. }
  4673. + (instancetype)dictionaryWithObjects:(const id [])objects
  4674. forKeys:(const int32_t [])keys
  4675. count:(NSUInteger)count {
  4676. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4677. // on to get the type correct.
  4678. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4679. forKeys:keys
  4680. count:count] autorelease];
  4681. }
  4682. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4683. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4684. // on to get the type correct.
  4685. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4686. }
  4687. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4688. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4689. }
  4690. - (instancetype)init {
  4691. return [self initWithObjects:NULL forKeys:NULL count:0];
  4692. }
  4693. - (instancetype)initWithObjects:(const id [])objects
  4694. forKeys:(const int32_t [])keys
  4695. count:(NSUInteger)count {
  4696. self = [super init];
  4697. if (self) {
  4698. _dictionary = [[NSMutableDictionary alloc] init];
  4699. if (count && objects && keys) {
  4700. for (NSUInteger i = 0; i < count; ++i) {
  4701. if (!objects[i]) {
  4702. [NSException raise:NSInvalidArgumentException
  4703. format:@"Attempting to add nil object to a Dictionary"];
  4704. }
  4705. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4706. }
  4707. }
  4708. }
  4709. return self;
  4710. }
  4711. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4712. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4713. if (self) {
  4714. if (dictionary) {
  4715. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4716. }
  4717. }
  4718. return self;
  4719. }
  4720. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4721. #pragma unused(numItems)
  4722. return [self initWithObjects:NULL forKeys:NULL count:0];
  4723. }
  4724. - (void)dealloc {
  4725. NSAssert(!_autocreator,
  4726. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4727. [self class], _autocreator);
  4728. [_dictionary release];
  4729. [super dealloc];
  4730. }
  4731. - (instancetype)copyWithZone:(NSZone *)zone {
  4732. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4733. }
  4734. - (BOOL)isEqual:(id)other {
  4735. if (self == other) {
  4736. return YES;
  4737. }
  4738. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4739. return NO;
  4740. }
  4741. GPBInt32ObjectDictionary *otherDictionary = other;
  4742. return [_dictionary isEqual:otherDictionary->_dictionary];
  4743. }
  4744. - (NSUInteger)hash {
  4745. return _dictionary.count;
  4746. }
  4747. - (NSString *)description {
  4748. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4749. }
  4750. - (NSUInteger)count {
  4751. return _dictionary.count;
  4752. }
  4753. - (void)enumerateKeysAndObjectsUsingBlock:
  4754. (void (^)(int32_t key, id object, BOOL *stop))block {
  4755. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4756. id aObject,
  4757. BOOL *stop) {
  4758. block([aKey intValue], aObject, stop);
  4759. }];
  4760. }
  4761. - (BOOL)isInitialized {
  4762. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4763. if (!msg.initialized) {
  4764. return NO;
  4765. }
  4766. }
  4767. return YES;
  4768. }
  4769. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4770. GPBInt32ObjectDictionary *newDict =
  4771. [[GPBInt32ObjectDictionary alloc] init];
  4772. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4773. GPBMessage *msg,
  4774. BOOL *stop) {
  4775. #pragma unused(stop)
  4776. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4777. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4778. [copiedMsg release];
  4779. }];
  4780. return newDict;
  4781. }
  4782. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4783. NSUInteger count = _dictionary.count;
  4784. if (count == 0) {
  4785. return 0;
  4786. }
  4787. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4788. GPBDataType keyDataType = field.mapKeyDataType;
  4789. __block size_t result = 0;
  4790. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4791. id aObject,
  4792. BOOL *stop) {
  4793. #pragma unused(stop)
  4794. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4795. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4796. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4797. }];
  4798. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4799. result += tagSize * count;
  4800. return result;
  4801. }
  4802. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4803. asField:(GPBFieldDescriptor *)field {
  4804. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4805. GPBDataType keyDataType = field.mapKeyDataType;
  4806. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4807. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4808. id aObject,
  4809. BOOL *stop) {
  4810. #pragma unused(stop)
  4811. // Write the tag.
  4812. [outputStream writeInt32NoTag:tag];
  4813. // Write the size of the message.
  4814. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4815. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4816. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4817. // Write the fields.
  4818. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4819. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  4820. }];
  4821. }
  4822. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4823. forGPBGenericValueKey:(GPBGenericValue *)key {
  4824. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4825. }
  4826. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4827. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4828. #pragma unused(stop)
  4829. block([NSString stringWithFormat:@"%d", key], object);
  4830. }];
  4831. }
  4832. - (id)objectForKey:(int32_t)key {
  4833. id result = [_dictionary objectForKey:@(key)];
  4834. return result;
  4835. }
  4836. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4837. if (otherDictionary) {
  4838. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4839. if (_autocreator) {
  4840. GPBAutocreatedDictionaryModified(_autocreator, self);
  4841. }
  4842. }
  4843. }
  4844. - (void)setObject:(id)object forKey:(int32_t)key {
  4845. if (!object) {
  4846. [NSException raise:NSInvalidArgumentException
  4847. format:@"Attempting to add nil object to a Dictionary"];
  4848. }
  4849. [_dictionary setObject:object forKey:@(key)];
  4850. if (_autocreator) {
  4851. GPBAutocreatedDictionaryModified(_autocreator, self);
  4852. }
  4853. }
  4854. - (void)removeObjectForKey:(int32_t)aKey {
  4855. [_dictionary removeObjectForKey:@(aKey)];
  4856. }
  4857. - (void)removeAll {
  4858. [_dictionary removeAllObjects];
  4859. }
  4860. @end
  4861. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4862. // This block of code is generated, do not edit it directly.
  4863. #pragma mark - UInt64 -> UInt32
  4864. @implementation GPBUInt64UInt32Dictionary {
  4865. @package
  4866. NSMutableDictionary *_dictionary;
  4867. }
  4868. + (instancetype)dictionary {
  4869. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4870. }
  4871. + (instancetype)dictionaryWithValue:(uint32_t)value
  4872. forKey:(uint64_t)key {
  4873. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4874. // on to get the type correct.
  4875. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4876. forKeys:&key
  4877. count:1] autorelease];
  4878. }
  4879. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4880. forKeys:(const uint64_t [])keys
  4881. count:(NSUInteger)count {
  4882. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4883. // on to get the type correct.
  4884. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4885. forKeys:keys
  4886. count:count] autorelease];
  4887. }
  4888. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4889. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4890. // on to get the type correct.
  4891. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4892. }
  4893. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4894. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4895. }
  4896. - (instancetype)init {
  4897. return [self initWithValues:NULL forKeys:NULL count:0];
  4898. }
  4899. - (instancetype)initWithValues:(const uint32_t [])values
  4900. forKeys:(const uint64_t [])keys
  4901. count:(NSUInteger)count {
  4902. self = [super init];
  4903. if (self) {
  4904. _dictionary = [[NSMutableDictionary alloc] init];
  4905. if (count && values && keys) {
  4906. for (NSUInteger i = 0; i < count; ++i) {
  4907. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4908. }
  4909. }
  4910. }
  4911. return self;
  4912. }
  4913. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4914. self = [self initWithValues:NULL forKeys:NULL count:0];
  4915. if (self) {
  4916. if (dictionary) {
  4917. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4918. }
  4919. }
  4920. return self;
  4921. }
  4922. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4923. #pragma unused(numItems)
  4924. return [self initWithValues:NULL forKeys:NULL count:0];
  4925. }
  4926. - (void)dealloc {
  4927. NSAssert(!_autocreator,
  4928. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4929. [self class], _autocreator);
  4930. [_dictionary release];
  4931. [super dealloc];
  4932. }
  4933. - (instancetype)copyWithZone:(NSZone *)zone {
  4934. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4935. }
  4936. - (BOOL)isEqual:(id)other {
  4937. if (self == other) {
  4938. return YES;
  4939. }
  4940. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4941. return NO;
  4942. }
  4943. GPBUInt64UInt32Dictionary *otherDictionary = other;
  4944. return [_dictionary isEqual:otherDictionary->_dictionary];
  4945. }
  4946. - (NSUInteger)hash {
  4947. return _dictionary.count;
  4948. }
  4949. - (NSString *)description {
  4950. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4951. }
  4952. - (NSUInteger)count {
  4953. return _dictionary.count;
  4954. }
  4955. - (void)enumerateKeysAndValuesUsingBlock:
  4956. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4957. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4958. NSNumber *aValue,
  4959. BOOL *stop) {
  4960. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4961. }];
  4962. }
  4963. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4964. NSUInteger count = _dictionary.count;
  4965. if (count == 0) {
  4966. return 0;
  4967. }
  4968. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4969. GPBDataType keyDataType = field.mapKeyDataType;
  4970. __block size_t result = 0;
  4971. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4972. NSNumber *aValue,
  4973. BOOL *stop) {
  4974. #pragma unused(stop)
  4975. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4976. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4977. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4978. }];
  4979. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4980. result += tagSize * count;
  4981. return result;
  4982. }
  4983. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4984. asField:(GPBFieldDescriptor *)field {
  4985. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4986. GPBDataType keyDataType = field.mapKeyDataType;
  4987. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4988. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4989. NSNumber *aValue,
  4990. BOOL *stop) {
  4991. #pragma unused(stop)
  4992. // Write the tag.
  4993. [outputStream writeInt32NoTag:tag];
  4994. // Write the size of the message.
  4995. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4996. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4997. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4998. // Write the fields.
  4999. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5000. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  5001. }];
  5002. }
  5003. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5004. forGPBGenericValueKey:(GPBGenericValue *)key {
  5005. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  5006. }
  5007. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5008. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  5009. #pragma unused(stop)
  5010. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  5011. }];
  5012. }
  5013. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  5014. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5015. if (wrapped && value) {
  5016. *value = [wrapped unsignedIntValue];
  5017. }
  5018. return (wrapped != NULL);
  5019. }
  5020. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  5021. if (otherDictionary) {
  5022. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5023. if (_autocreator) {
  5024. GPBAutocreatedDictionaryModified(_autocreator, self);
  5025. }
  5026. }
  5027. }
  5028. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  5029. [_dictionary setObject:@(value) forKey:@(key)];
  5030. if (_autocreator) {
  5031. GPBAutocreatedDictionaryModified(_autocreator, self);
  5032. }
  5033. }
  5034. - (void)removeValueForKey:(uint64_t)aKey {
  5035. [_dictionary removeObjectForKey:@(aKey)];
  5036. }
  5037. - (void)removeAll {
  5038. [_dictionary removeAllObjects];
  5039. }
  5040. @end
  5041. #pragma mark - UInt64 -> Int32
  5042. @implementation GPBUInt64Int32Dictionary {
  5043. @package
  5044. NSMutableDictionary *_dictionary;
  5045. }
  5046. + (instancetype)dictionary {
  5047. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5048. }
  5049. + (instancetype)dictionaryWithValue:(int32_t)value
  5050. forKey:(uint64_t)key {
  5051. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5052. // on to get the type correct.
  5053. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  5054. forKeys:&key
  5055. count:1] autorelease];
  5056. }
  5057. + (instancetype)dictionaryWithValues:(const int32_t [])values
  5058. forKeys:(const uint64_t [])keys
  5059. count:(NSUInteger)count {
  5060. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5061. // on to get the type correct.
  5062. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  5063. forKeys:keys
  5064. count:count] autorelease];
  5065. }
  5066. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5067. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5068. // on to get the type correct.
  5069. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5070. }
  5071. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5072. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5073. }
  5074. - (instancetype)init {
  5075. return [self initWithValues:NULL forKeys:NULL count:0];
  5076. }
  5077. - (instancetype)initWithValues:(const int32_t [])values
  5078. forKeys:(const uint64_t [])keys
  5079. count:(NSUInteger)count {
  5080. self = [super init];
  5081. if (self) {
  5082. _dictionary = [[NSMutableDictionary alloc] init];
  5083. if (count && values && keys) {
  5084. for (NSUInteger i = 0; i < count; ++i) {
  5085. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5086. }
  5087. }
  5088. }
  5089. return self;
  5090. }
  5091. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5092. self = [self initWithValues:NULL forKeys:NULL count:0];
  5093. if (self) {
  5094. if (dictionary) {
  5095. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5096. }
  5097. }
  5098. return self;
  5099. }
  5100. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5101. #pragma unused(numItems)
  5102. return [self initWithValues:NULL forKeys:NULL count:0];
  5103. }
  5104. - (void)dealloc {
  5105. NSAssert(!_autocreator,
  5106. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5107. [self class], _autocreator);
  5108. [_dictionary release];
  5109. [super dealloc];
  5110. }
  5111. - (instancetype)copyWithZone:(NSZone *)zone {
  5112. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5113. }
  5114. - (BOOL)isEqual:(id)other {
  5115. if (self == other) {
  5116. return YES;
  5117. }
  5118. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5119. return NO;
  5120. }
  5121. GPBUInt64Int32Dictionary *otherDictionary = other;
  5122. return [_dictionary isEqual:otherDictionary->_dictionary];
  5123. }
  5124. - (NSUInteger)hash {
  5125. return _dictionary.count;
  5126. }
  5127. - (NSString *)description {
  5128. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5129. }
  5130. - (NSUInteger)count {
  5131. return _dictionary.count;
  5132. }
  5133. - (void)enumerateKeysAndValuesUsingBlock:
  5134. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5135. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5136. NSNumber *aValue,
  5137. BOOL *stop) {
  5138. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5139. }];
  5140. }
  5141. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5142. NSUInteger count = _dictionary.count;
  5143. if (count == 0) {
  5144. return 0;
  5145. }
  5146. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5147. GPBDataType keyDataType = field.mapKeyDataType;
  5148. __block size_t result = 0;
  5149. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5150. NSNumber *aValue,
  5151. BOOL *stop) {
  5152. #pragma unused(stop)
  5153. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5154. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5155. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5156. }];
  5157. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5158. result += tagSize * count;
  5159. return result;
  5160. }
  5161. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5162. asField:(GPBFieldDescriptor *)field {
  5163. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5164. GPBDataType keyDataType = field.mapKeyDataType;
  5165. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5166. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5167. NSNumber *aValue,
  5168. BOOL *stop) {
  5169. #pragma unused(stop)
  5170. // Write the tag.
  5171. [outputStream writeInt32NoTag:tag];
  5172. // Write the size of the message.
  5173. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5174. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5175. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5176. // Write the fields.
  5177. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5178. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5179. }];
  5180. }
  5181. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5182. forGPBGenericValueKey:(GPBGenericValue *)key {
  5183. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5184. }
  5185. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5186. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5187. #pragma unused(stop)
  5188. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5189. }];
  5190. }
  5191. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5192. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5193. if (wrapped && value) {
  5194. *value = [wrapped intValue];
  5195. }
  5196. return (wrapped != NULL);
  5197. }
  5198. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5199. if (otherDictionary) {
  5200. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5201. if (_autocreator) {
  5202. GPBAutocreatedDictionaryModified(_autocreator, self);
  5203. }
  5204. }
  5205. }
  5206. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5207. [_dictionary setObject:@(value) forKey:@(key)];
  5208. if (_autocreator) {
  5209. GPBAutocreatedDictionaryModified(_autocreator, self);
  5210. }
  5211. }
  5212. - (void)removeValueForKey:(uint64_t)aKey {
  5213. [_dictionary removeObjectForKey:@(aKey)];
  5214. }
  5215. - (void)removeAll {
  5216. [_dictionary removeAllObjects];
  5217. }
  5218. @end
  5219. #pragma mark - UInt64 -> UInt64
  5220. @implementation GPBUInt64UInt64Dictionary {
  5221. @package
  5222. NSMutableDictionary *_dictionary;
  5223. }
  5224. + (instancetype)dictionary {
  5225. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5226. }
  5227. + (instancetype)dictionaryWithValue:(uint64_t)value
  5228. forKey:(uint64_t)key {
  5229. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5230. // on to get the type correct.
  5231. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  5232. forKeys:&key
  5233. count:1] autorelease];
  5234. }
  5235. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  5236. forKeys:(const uint64_t [])keys
  5237. count:(NSUInteger)count {
  5238. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5239. // on to get the type correct.
  5240. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  5241. forKeys:keys
  5242. count:count] autorelease];
  5243. }
  5244. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5245. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5246. // on to get the type correct.
  5247. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5248. }
  5249. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5250. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5251. }
  5252. - (instancetype)init {
  5253. return [self initWithValues:NULL forKeys:NULL count:0];
  5254. }
  5255. - (instancetype)initWithValues:(const uint64_t [])values
  5256. forKeys:(const uint64_t [])keys
  5257. count:(NSUInteger)count {
  5258. self = [super init];
  5259. if (self) {
  5260. _dictionary = [[NSMutableDictionary alloc] init];
  5261. if (count && values && keys) {
  5262. for (NSUInteger i = 0; i < count; ++i) {
  5263. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5264. }
  5265. }
  5266. }
  5267. return self;
  5268. }
  5269. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5270. self = [self initWithValues:NULL forKeys:NULL count:0];
  5271. if (self) {
  5272. if (dictionary) {
  5273. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5274. }
  5275. }
  5276. return self;
  5277. }
  5278. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5279. #pragma unused(numItems)
  5280. return [self initWithValues:NULL forKeys:NULL count:0];
  5281. }
  5282. - (void)dealloc {
  5283. NSAssert(!_autocreator,
  5284. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5285. [self class], _autocreator);
  5286. [_dictionary release];
  5287. [super dealloc];
  5288. }
  5289. - (instancetype)copyWithZone:(NSZone *)zone {
  5290. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5291. }
  5292. - (BOOL)isEqual:(id)other {
  5293. if (self == other) {
  5294. return YES;
  5295. }
  5296. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5297. return NO;
  5298. }
  5299. GPBUInt64UInt64Dictionary *otherDictionary = other;
  5300. return [_dictionary isEqual:otherDictionary->_dictionary];
  5301. }
  5302. - (NSUInteger)hash {
  5303. return _dictionary.count;
  5304. }
  5305. - (NSString *)description {
  5306. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5307. }
  5308. - (NSUInteger)count {
  5309. return _dictionary.count;
  5310. }
  5311. - (void)enumerateKeysAndValuesUsingBlock:
  5312. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5313. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5314. NSNumber *aValue,
  5315. BOOL *stop) {
  5316. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5317. }];
  5318. }
  5319. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5320. NSUInteger count = _dictionary.count;
  5321. if (count == 0) {
  5322. return 0;
  5323. }
  5324. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5325. GPBDataType keyDataType = field.mapKeyDataType;
  5326. __block size_t result = 0;
  5327. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5328. NSNumber *aValue,
  5329. BOOL *stop) {
  5330. #pragma unused(stop)
  5331. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5332. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5333. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5334. }];
  5335. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5336. result += tagSize * count;
  5337. return result;
  5338. }
  5339. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5340. asField:(GPBFieldDescriptor *)field {
  5341. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5342. GPBDataType keyDataType = field.mapKeyDataType;
  5343. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5344. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5345. NSNumber *aValue,
  5346. BOOL *stop) {
  5347. #pragma unused(stop)
  5348. // Write the tag.
  5349. [outputStream writeInt32NoTag:tag];
  5350. // Write the size of the message.
  5351. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5352. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5353. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5354. // Write the fields.
  5355. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5356. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5357. }];
  5358. }
  5359. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5360. forGPBGenericValueKey:(GPBGenericValue *)key {
  5361. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5362. }
  5363. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5364. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5365. #pragma unused(stop)
  5366. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5367. }];
  5368. }
  5369. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  5370. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5371. if (wrapped && value) {
  5372. *value = [wrapped unsignedLongLongValue];
  5373. }
  5374. return (wrapped != NULL);
  5375. }
  5376. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5377. if (otherDictionary) {
  5378. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5379. if (_autocreator) {
  5380. GPBAutocreatedDictionaryModified(_autocreator, self);
  5381. }
  5382. }
  5383. }
  5384. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5385. [_dictionary setObject:@(value) forKey:@(key)];
  5386. if (_autocreator) {
  5387. GPBAutocreatedDictionaryModified(_autocreator, self);
  5388. }
  5389. }
  5390. - (void)removeValueForKey:(uint64_t)aKey {
  5391. [_dictionary removeObjectForKey:@(aKey)];
  5392. }
  5393. - (void)removeAll {
  5394. [_dictionary removeAllObjects];
  5395. }
  5396. @end
  5397. #pragma mark - UInt64 -> Int64
  5398. @implementation GPBUInt64Int64Dictionary {
  5399. @package
  5400. NSMutableDictionary *_dictionary;
  5401. }
  5402. + (instancetype)dictionary {
  5403. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5404. }
  5405. + (instancetype)dictionaryWithValue:(int64_t)value
  5406. forKey:(uint64_t)key {
  5407. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5408. // on to get the type correct.
  5409. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5410. forKeys:&key
  5411. count:1] autorelease];
  5412. }
  5413. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5414. forKeys:(const uint64_t [])keys
  5415. count:(NSUInteger)count {
  5416. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5417. // on to get the type correct.
  5418. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5419. forKeys:keys
  5420. count:count] autorelease];
  5421. }
  5422. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5423. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5424. // on to get the type correct.
  5425. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5426. }
  5427. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5428. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5429. }
  5430. - (instancetype)init {
  5431. return [self initWithValues:NULL forKeys:NULL count:0];
  5432. }
  5433. - (instancetype)initWithValues:(const int64_t [])values
  5434. forKeys:(const uint64_t [])keys
  5435. count:(NSUInteger)count {
  5436. self = [super init];
  5437. if (self) {
  5438. _dictionary = [[NSMutableDictionary alloc] init];
  5439. if (count && values && keys) {
  5440. for (NSUInteger i = 0; i < count; ++i) {
  5441. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5442. }
  5443. }
  5444. }
  5445. return self;
  5446. }
  5447. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5448. self = [self initWithValues:NULL forKeys:NULL count:0];
  5449. if (self) {
  5450. if (dictionary) {
  5451. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5452. }
  5453. }
  5454. return self;
  5455. }
  5456. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5457. #pragma unused(numItems)
  5458. return [self initWithValues:NULL forKeys:NULL count:0];
  5459. }
  5460. - (void)dealloc {
  5461. NSAssert(!_autocreator,
  5462. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5463. [self class], _autocreator);
  5464. [_dictionary release];
  5465. [super dealloc];
  5466. }
  5467. - (instancetype)copyWithZone:(NSZone *)zone {
  5468. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5469. }
  5470. - (BOOL)isEqual:(id)other {
  5471. if (self == other) {
  5472. return YES;
  5473. }
  5474. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5475. return NO;
  5476. }
  5477. GPBUInt64Int64Dictionary *otherDictionary = other;
  5478. return [_dictionary isEqual:otherDictionary->_dictionary];
  5479. }
  5480. - (NSUInteger)hash {
  5481. return _dictionary.count;
  5482. }
  5483. - (NSString *)description {
  5484. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5485. }
  5486. - (NSUInteger)count {
  5487. return _dictionary.count;
  5488. }
  5489. - (void)enumerateKeysAndValuesUsingBlock:
  5490. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5491. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5492. NSNumber *aValue,
  5493. BOOL *stop) {
  5494. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5495. }];
  5496. }
  5497. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5498. NSUInteger count = _dictionary.count;
  5499. if (count == 0) {
  5500. return 0;
  5501. }
  5502. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5503. GPBDataType keyDataType = field.mapKeyDataType;
  5504. __block size_t result = 0;
  5505. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5506. NSNumber *aValue,
  5507. BOOL *stop) {
  5508. #pragma unused(stop)
  5509. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5510. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5511. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5512. }];
  5513. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5514. result += tagSize * count;
  5515. return result;
  5516. }
  5517. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5518. asField:(GPBFieldDescriptor *)field {
  5519. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5520. GPBDataType keyDataType = field.mapKeyDataType;
  5521. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5522. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5523. NSNumber *aValue,
  5524. BOOL *stop) {
  5525. #pragma unused(stop)
  5526. // Write the tag.
  5527. [outputStream writeInt32NoTag:tag];
  5528. // Write the size of the message.
  5529. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5530. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5531. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5532. // Write the fields.
  5533. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5534. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5535. }];
  5536. }
  5537. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5538. forGPBGenericValueKey:(GPBGenericValue *)key {
  5539. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5540. }
  5541. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5542. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5543. #pragma unused(stop)
  5544. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5545. }];
  5546. }
  5547. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5548. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5549. if (wrapped && value) {
  5550. *value = [wrapped longLongValue];
  5551. }
  5552. return (wrapped != NULL);
  5553. }
  5554. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5555. if (otherDictionary) {
  5556. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5557. if (_autocreator) {
  5558. GPBAutocreatedDictionaryModified(_autocreator, self);
  5559. }
  5560. }
  5561. }
  5562. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5563. [_dictionary setObject:@(value) forKey:@(key)];
  5564. if (_autocreator) {
  5565. GPBAutocreatedDictionaryModified(_autocreator, self);
  5566. }
  5567. }
  5568. - (void)removeValueForKey:(uint64_t)aKey {
  5569. [_dictionary removeObjectForKey:@(aKey)];
  5570. }
  5571. - (void)removeAll {
  5572. [_dictionary removeAllObjects];
  5573. }
  5574. @end
  5575. #pragma mark - UInt64 -> Bool
  5576. @implementation GPBUInt64BoolDictionary {
  5577. @package
  5578. NSMutableDictionary *_dictionary;
  5579. }
  5580. + (instancetype)dictionary {
  5581. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5582. }
  5583. + (instancetype)dictionaryWithValue:(BOOL)value
  5584. forKey:(uint64_t)key {
  5585. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5586. // on to get the type correct.
  5587. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5588. forKeys:&key
  5589. count:1] autorelease];
  5590. }
  5591. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5592. forKeys:(const uint64_t [])keys
  5593. count:(NSUInteger)count {
  5594. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5595. // on to get the type correct.
  5596. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5597. forKeys:keys
  5598. count:count] autorelease];
  5599. }
  5600. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5601. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5602. // on to get the type correct.
  5603. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5604. }
  5605. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5606. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5607. }
  5608. - (instancetype)init {
  5609. return [self initWithValues:NULL forKeys:NULL count:0];
  5610. }
  5611. - (instancetype)initWithValues:(const BOOL [])values
  5612. forKeys:(const uint64_t [])keys
  5613. count:(NSUInteger)count {
  5614. self = [super init];
  5615. if (self) {
  5616. _dictionary = [[NSMutableDictionary alloc] init];
  5617. if (count && values && keys) {
  5618. for (NSUInteger i = 0; i < count; ++i) {
  5619. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5620. }
  5621. }
  5622. }
  5623. return self;
  5624. }
  5625. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5626. self = [self initWithValues:NULL forKeys:NULL count:0];
  5627. if (self) {
  5628. if (dictionary) {
  5629. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5630. }
  5631. }
  5632. return self;
  5633. }
  5634. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5635. #pragma unused(numItems)
  5636. return [self initWithValues:NULL forKeys:NULL count:0];
  5637. }
  5638. - (void)dealloc {
  5639. NSAssert(!_autocreator,
  5640. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5641. [self class], _autocreator);
  5642. [_dictionary release];
  5643. [super dealloc];
  5644. }
  5645. - (instancetype)copyWithZone:(NSZone *)zone {
  5646. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5647. }
  5648. - (BOOL)isEqual:(id)other {
  5649. if (self == other) {
  5650. return YES;
  5651. }
  5652. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5653. return NO;
  5654. }
  5655. GPBUInt64BoolDictionary *otherDictionary = other;
  5656. return [_dictionary isEqual:otherDictionary->_dictionary];
  5657. }
  5658. - (NSUInteger)hash {
  5659. return _dictionary.count;
  5660. }
  5661. - (NSString *)description {
  5662. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5663. }
  5664. - (NSUInteger)count {
  5665. return _dictionary.count;
  5666. }
  5667. - (void)enumerateKeysAndValuesUsingBlock:
  5668. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5669. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5670. NSNumber *aValue,
  5671. BOOL *stop) {
  5672. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5673. }];
  5674. }
  5675. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5676. NSUInteger count = _dictionary.count;
  5677. if (count == 0) {
  5678. return 0;
  5679. }
  5680. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5681. GPBDataType keyDataType = field.mapKeyDataType;
  5682. __block size_t result = 0;
  5683. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5684. NSNumber *aValue,
  5685. BOOL *stop) {
  5686. #pragma unused(stop)
  5687. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5688. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5689. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5690. }];
  5691. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5692. result += tagSize * count;
  5693. return result;
  5694. }
  5695. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5696. asField:(GPBFieldDescriptor *)field {
  5697. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5698. GPBDataType keyDataType = field.mapKeyDataType;
  5699. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5700. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5701. NSNumber *aValue,
  5702. BOOL *stop) {
  5703. #pragma unused(stop)
  5704. // Write the tag.
  5705. [outputStream writeInt32NoTag:tag];
  5706. // Write the size of the message.
  5707. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5708. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5709. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5710. // Write the fields.
  5711. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5712. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5713. }];
  5714. }
  5715. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5716. forGPBGenericValueKey:(GPBGenericValue *)key {
  5717. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5718. }
  5719. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5720. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5721. #pragma unused(stop)
  5722. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5723. }];
  5724. }
  5725. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5726. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5727. if (wrapped && value) {
  5728. *value = [wrapped boolValue];
  5729. }
  5730. return (wrapped != NULL);
  5731. }
  5732. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5733. if (otherDictionary) {
  5734. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5735. if (_autocreator) {
  5736. GPBAutocreatedDictionaryModified(_autocreator, self);
  5737. }
  5738. }
  5739. }
  5740. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5741. [_dictionary setObject:@(value) forKey:@(key)];
  5742. if (_autocreator) {
  5743. GPBAutocreatedDictionaryModified(_autocreator, self);
  5744. }
  5745. }
  5746. - (void)removeValueForKey:(uint64_t)aKey {
  5747. [_dictionary removeObjectForKey:@(aKey)];
  5748. }
  5749. - (void)removeAll {
  5750. [_dictionary removeAllObjects];
  5751. }
  5752. @end
  5753. #pragma mark - UInt64 -> Float
  5754. @implementation GPBUInt64FloatDictionary {
  5755. @package
  5756. NSMutableDictionary *_dictionary;
  5757. }
  5758. + (instancetype)dictionary {
  5759. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5760. }
  5761. + (instancetype)dictionaryWithValue:(float)value
  5762. forKey:(uint64_t)key {
  5763. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5764. // on to get the type correct.
  5765. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5766. forKeys:&key
  5767. count:1] autorelease];
  5768. }
  5769. + (instancetype)dictionaryWithValues:(const float [])values
  5770. forKeys:(const uint64_t [])keys
  5771. count:(NSUInteger)count {
  5772. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5773. // on to get the type correct.
  5774. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5775. forKeys:keys
  5776. count:count] autorelease];
  5777. }
  5778. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5779. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5780. // on to get the type correct.
  5781. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5782. }
  5783. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5784. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5785. }
  5786. - (instancetype)init {
  5787. return [self initWithValues:NULL forKeys:NULL count:0];
  5788. }
  5789. - (instancetype)initWithValues:(const float [])values
  5790. forKeys:(const uint64_t [])keys
  5791. count:(NSUInteger)count {
  5792. self = [super init];
  5793. if (self) {
  5794. _dictionary = [[NSMutableDictionary alloc] init];
  5795. if (count && values && keys) {
  5796. for (NSUInteger i = 0; i < count; ++i) {
  5797. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5798. }
  5799. }
  5800. }
  5801. return self;
  5802. }
  5803. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5804. self = [self initWithValues:NULL forKeys:NULL count:0];
  5805. if (self) {
  5806. if (dictionary) {
  5807. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5808. }
  5809. }
  5810. return self;
  5811. }
  5812. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5813. #pragma unused(numItems)
  5814. return [self initWithValues:NULL forKeys:NULL count:0];
  5815. }
  5816. - (void)dealloc {
  5817. NSAssert(!_autocreator,
  5818. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5819. [self class], _autocreator);
  5820. [_dictionary release];
  5821. [super dealloc];
  5822. }
  5823. - (instancetype)copyWithZone:(NSZone *)zone {
  5824. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5825. }
  5826. - (BOOL)isEqual:(id)other {
  5827. if (self == other) {
  5828. return YES;
  5829. }
  5830. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5831. return NO;
  5832. }
  5833. GPBUInt64FloatDictionary *otherDictionary = other;
  5834. return [_dictionary isEqual:otherDictionary->_dictionary];
  5835. }
  5836. - (NSUInteger)hash {
  5837. return _dictionary.count;
  5838. }
  5839. - (NSString *)description {
  5840. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5841. }
  5842. - (NSUInteger)count {
  5843. return _dictionary.count;
  5844. }
  5845. - (void)enumerateKeysAndValuesUsingBlock:
  5846. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5847. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5848. NSNumber *aValue,
  5849. BOOL *stop) {
  5850. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5851. }];
  5852. }
  5853. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5854. NSUInteger count = _dictionary.count;
  5855. if (count == 0) {
  5856. return 0;
  5857. }
  5858. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5859. GPBDataType keyDataType = field.mapKeyDataType;
  5860. __block size_t result = 0;
  5861. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5862. NSNumber *aValue,
  5863. BOOL *stop) {
  5864. #pragma unused(stop)
  5865. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5866. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5867. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5868. }];
  5869. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5870. result += tagSize * count;
  5871. return result;
  5872. }
  5873. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5874. asField:(GPBFieldDescriptor *)field {
  5875. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5876. GPBDataType keyDataType = field.mapKeyDataType;
  5877. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5878. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5879. NSNumber *aValue,
  5880. BOOL *stop) {
  5881. #pragma unused(stop)
  5882. // Write the tag.
  5883. [outputStream writeInt32NoTag:tag];
  5884. // Write the size of the message.
  5885. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5886. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5887. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5888. // Write the fields.
  5889. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5890. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5891. }];
  5892. }
  5893. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5894. forGPBGenericValueKey:(GPBGenericValue *)key {
  5895. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5896. }
  5897. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5898. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5899. #pragma unused(stop)
  5900. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5901. }];
  5902. }
  5903. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5904. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5905. if (wrapped && value) {
  5906. *value = [wrapped floatValue];
  5907. }
  5908. return (wrapped != NULL);
  5909. }
  5910. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5911. if (otherDictionary) {
  5912. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5913. if (_autocreator) {
  5914. GPBAutocreatedDictionaryModified(_autocreator, self);
  5915. }
  5916. }
  5917. }
  5918. - (void)setValue:(float)value forKey:(uint64_t)key {
  5919. [_dictionary setObject:@(value) forKey:@(key)];
  5920. if (_autocreator) {
  5921. GPBAutocreatedDictionaryModified(_autocreator, self);
  5922. }
  5923. }
  5924. - (void)removeValueForKey:(uint64_t)aKey {
  5925. [_dictionary removeObjectForKey:@(aKey)];
  5926. }
  5927. - (void)removeAll {
  5928. [_dictionary removeAllObjects];
  5929. }
  5930. @end
  5931. #pragma mark - UInt64 -> Double
  5932. @implementation GPBUInt64DoubleDictionary {
  5933. @package
  5934. NSMutableDictionary *_dictionary;
  5935. }
  5936. + (instancetype)dictionary {
  5937. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5938. }
  5939. + (instancetype)dictionaryWithValue:(double)value
  5940. forKey:(uint64_t)key {
  5941. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5942. // on to get the type correct.
  5943. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5944. forKeys:&key
  5945. count:1] autorelease];
  5946. }
  5947. + (instancetype)dictionaryWithValues:(const double [])values
  5948. forKeys:(const uint64_t [])keys
  5949. count:(NSUInteger)count {
  5950. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5951. // on to get the type correct.
  5952. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5953. forKeys:keys
  5954. count:count] autorelease];
  5955. }
  5956. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5957. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5958. // on to get the type correct.
  5959. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5960. }
  5961. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5962. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5963. }
  5964. - (instancetype)init {
  5965. return [self initWithValues:NULL forKeys:NULL count:0];
  5966. }
  5967. - (instancetype)initWithValues:(const double [])values
  5968. forKeys:(const uint64_t [])keys
  5969. count:(NSUInteger)count {
  5970. self = [super init];
  5971. if (self) {
  5972. _dictionary = [[NSMutableDictionary alloc] init];
  5973. if (count && values && keys) {
  5974. for (NSUInteger i = 0; i < count; ++i) {
  5975. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5976. }
  5977. }
  5978. }
  5979. return self;
  5980. }
  5981. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5982. self = [self initWithValues:NULL forKeys:NULL count:0];
  5983. if (self) {
  5984. if (dictionary) {
  5985. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5986. }
  5987. }
  5988. return self;
  5989. }
  5990. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5991. #pragma unused(numItems)
  5992. return [self initWithValues:NULL forKeys:NULL count:0];
  5993. }
  5994. - (void)dealloc {
  5995. NSAssert(!_autocreator,
  5996. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5997. [self class], _autocreator);
  5998. [_dictionary release];
  5999. [super dealloc];
  6000. }
  6001. - (instancetype)copyWithZone:(NSZone *)zone {
  6002. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6003. }
  6004. - (BOOL)isEqual:(id)other {
  6005. if (self == other) {
  6006. return YES;
  6007. }
  6008. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  6009. return NO;
  6010. }
  6011. GPBUInt64DoubleDictionary *otherDictionary = other;
  6012. return [_dictionary isEqual:otherDictionary->_dictionary];
  6013. }
  6014. - (NSUInteger)hash {
  6015. return _dictionary.count;
  6016. }
  6017. - (NSString *)description {
  6018. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6019. }
  6020. - (NSUInteger)count {
  6021. return _dictionary.count;
  6022. }
  6023. - (void)enumerateKeysAndValuesUsingBlock:
  6024. (void (^)(uint64_t key, double value, BOOL *stop))block {
  6025. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6026. NSNumber *aValue,
  6027. BOOL *stop) {
  6028. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  6029. }];
  6030. }
  6031. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6032. NSUInteger count = _dictionary.count;
  6033. if (count == 0) {
  6034. return 0;
  6035. }
  6036. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6037. GPBDataType keyDataType = field.mapKeyDataType;
  6038. __block size_t result = 0;
  6039. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6040. NSNumber *aValue,
  6041. BOOL *stop) {
  6042. #pragma unused(stop)
  6043. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6044. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6045. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6046. }];
  6047. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6048. result += tagSize * count;
  6049. return result;
  6050. }
  6051. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6052. asField:(GPBFieldDescriptor *)field {
  6053. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6054. GPBDataType keyDataType = field.mapKeyDataType;
  6055. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6056. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6057. NSNumber *aValue,
  6058. BOOL *stop) {
  6059. #pragma unused(stop)
  6060. // Write the tag.
  6061. [outputStream writeInt32NoTag:tag];
  6062. // Write the size of the message.
  6063. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6064. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6065. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6066. // Write the fields.
  6067. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6068. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6069. }];
  6070. }
  6071. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6072. forGPBGenericValueKey:(GPBGenericValue *)key {
  6073. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  6074. }
  6075. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6076. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  6077. #pragma unused(stop)
  6078. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6079. }];
  6080. }
  6081. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  6082. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6083. if (wrapped && value) {
  6084. *value = [wrapped doubleValue];
  6085. }
  6086. return (wrapped != NULL);
  6087. }
  6088. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  6089. if (otherDictionary) {
  6090. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6091. if (_autocreator) {
  6092. GPBAutocreatedDictionaryModified(_autocreator, self);
  6093. }
  6094. }
  6095. }
  6096. - (void)setValue:(double)value forKey:(uint64_t)key {
  6097. [_dictionary setObject:@(value) forKey:@(key)];
  6098. if (_autocreator) {
  6099. GPBAutocreatedDictionaryModified(_autocreator, self);
  6100. }
  6101. }
  6102. - (void)removeValueForKey:(uint64_t)aKey {
  6103. [_dictionary removeObjectForKey:@(aKey)];
  6104. }
  6105. - (void)removeAll {
  6106. [_dictionary removeAllObjects];
  6107. }
  6108. @end
  6109. #pragma mark - UInt64 -> Enum
  6110. @implementation GPBUInt64EnumDictionary {
  6111. @package
  6112. NSMutableDictionary *_dictionary;
  6113. GPBEnumValidationFunc _validationFunc;
  6114. }
  6115. @synthesize validationFunc = _validationFunc;
  6116. + (instancetype)dictionary {
  6117. return [[[self alloc] initWithValidationFunction:NULL
  6118. rawValues:NULL
  6119. forKeys:NULL
  6120. count:0] autorelease];
  6121. }
  6122. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6123. return [[[self alloc] initWithValidationFunction:func
  6124. rawValues:NULL
  6125. forKeys:NULL
  6126. count:0] autorelease];
  6127. }
  6128. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6129. rawValue:(int32_t)rawValue
  6130. forKey:(uint64_t)key {
  6131. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6132. // on to get the type correct.
  6133. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6134. rawValues:&rawValue
  6135. forKeys:&key
  6136. count:1] autorelease];
  6137. }
  6138. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6139. rawValues:(const int32_t [])rawValues
  6140. forKeys:(const uint64_t [])keys
  6141. count:(NSUInteger)count {
  6142. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6143. // on to get the type correct.
  6144. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6145. rawValues:rawValues
  6146. forKeys:keys
  6147. count:count] autorelease];
  6148. }
  6149. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6150. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6151. // on to get the type correct.
  6152. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6153. }
  6154. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6155. capacity:(NSUInteger)numItems {
  6156. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6157. }
  6158. - (instancetype)init {
  6159. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6160. }
  6161. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6162. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6163. }
  6164. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6165. rawValues:(const int32_t [])rawValues
  6166. forKeys:(const uint64_t [])keys
  6167. count:(NSUInteger)count {
  6168. self = [super init];
  6169. if (self) {
  6170. _dictionary = [[NSMutableDictionary alloc] init];
  6171. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6172. if (count && rawValues && keys) {
  6173. for (NSUInteger i = 0; i < count; ++i) {
  6174. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6175. }
  6176. }
  6177. }
  6178. return self;
  6179. }
  6180. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6181. self = [self initWithValidationFunction:dictionary.validationFunc
  6182. rawValues:NULL
  6183. forKeys:NULL
  6184. count:0];
  6185. if (self) {
  6186. if (dictionary) {
  6187. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6188. }
  6189. }
  6190. return self;
  6191. }
  6192. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6193. capacity:(NSUInteger)numItems {
  6194. #pragma unused(numItems)
  6195. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6196. }
  6197. - (void)dealloc {
  6198. NSAssert(!_autocreator,
  6199. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6200. [self class], _autocreator);
  6201. [_dictionary release];
  6202. [super dealloc];
  6203. }
  6204. - (instancetype)copyWithZone:(NSZone *)zone {
  6205. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6206. }
  6207. - (BOOL)isEqual:(id)other {
  6208. if (self == other) {
  6209. return YES;
  6210. }
  6211. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6212. return NO;
  6213. }
  6214. GPBUInt64EnumDictionary *otherDictionary = other;
  6215. return [_dictionary isEqual:otherDictionary->_dictionary];
  6216. }
  6217. - (NSUInteger)hash {
  6218. return _dictionary.count;
  6219. }
  6220. - (NSString *)description {
  6221. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6222. }
  6223. - (NSUInteger)count {
  6224. return _dictionary.count;
  6225. }
  6226. - (void)enumerateKeysAndRawValuesUsingBlock:
  6227. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6228. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6229. NSNumber *aValue,
  6230. BOOL *stop) {
  6231. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6232. }];
  6233. }
  6234. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6235. NSUInteger count = _dictionary.count;
  6236. if (count == 0) {
  6237. return 0;
  6238. }
  6239. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6240. GPBDataType keyDataType = field.mapKeyDataType;
  6241. __block size_t result = 0;
  6242. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6243. NSNumber *aValue,
  6244. BOOL *stop) {
  6245. #pragma unused(stop)
  6246. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6247. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6248. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6249. }];
  6250. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6251. result += tagSize * count;
  6252. return result;
  6253. }
  6254. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6255. asField:(GPBFieldDescriptor *)field {
  6256. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6257. GPBDataType keyDataType = field.mapKeyDataType;
  6258. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6259. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6260. NSNumber *aValue,
  6261. BOOL *stop) {
  6262. #pragma unused(stop)
  6263. // Write the tag.
  6264. [outputStream writeInt32NoTag:tag];
  6265. // Write the size of the message.
  6266. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6267. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6268. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6269. // Write the fields.
  6270. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6271. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6272. }];
  6273. }
  6274. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6275. forKey:(GPBGenericValue *)key
  6276. keyDataType:(GPBDataType)keyDataType {
  6277. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6278. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6279. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6280. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6281. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6282. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6283. [outputStream release];
  6284. return data;
  6285. }
  6286. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6287. forGPBGenericValueKey:(GPBGenericValue *)key {
  6288. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6289. }
  6290. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6291. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6292. #pragma unused(stop)
  6293. block([NSString stringWithFormat:@"%llu", key], @(value));
  6294. }];
  6295. }
  6296. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  6297. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6298. if (wrapped && value) {
  6299. int32_t result = [wrapped intValue];
  6300. if (!_validationFunc(result)) {
  6301. result = kGPBUnrecognizedEnumeratorValue;
  6302. }
  6303. *value = result;
  6304. }
  6305. return (wrapped != NULL);
  6306. }
  6307. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  6308. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6309. if (wrapped && rawValue) {
  6310. *rawValue = [wrapped intValue];
  6311. }
  6312. return (wrapped != NULL);
  6313. }
  6314. - (void)enumerateKeysAndValuesUsingBlock:
  6315. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6316. GPBEnumValidationFunc func = _validationFunc;
  6317. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6318. NSNumber *aValue,
  6319. BOOL *stop) {
  6320. int32_t unwrapped = [aValue intValue];
  6321. if (!func(unwrapped)) {
  6322. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6323. }
  6324. block([aKey unsignedLongLongValue], unwrapped, stop);
  6325. }];
  6326. }
  6327. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6328. if (otherDictionary) {
  6329. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6330. if (_autocreator) {
  6331. GPBAutocreatedDictionaryModified(_autocreator, self);
  6332. }
  6333. }
  6334. }
  6335. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6336. [_dictionary setObject:@(value) forKey:@(key)];
  6337. if (_autocreator) {
  6338. GPBAutocreatedDictionaryModified(_autocreator, self);
  6339. }
  6340. }
  6341. - (void)removeValueForKey:(uint64_t)aKey {
  6342. [_dictionary removeObjectForKey:@(aKey)];
  6343. }
  6344. - (void)removeAll {
  6345. [_dictionary removeAllObjects];
  6346. }
  6347. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  6348. if (!_validationFunc(value)) {
  6349. [NSException raise:NSInvalidArgumentException
  6350. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6351. value];
  6352. }
  6353. [_dictionary setObject:@(value) forKey:@(key)];
  6354. if (_autocreator) {
  6355. GPBAutocreatedDictionaryModified(_autocreator, self);
  6356. }
  6357. }
  6358. @end
  6359. #pragma mark - UInt64 -> Object
  6360. @implementation GPBUInt64ObjectDictionary {
  6361. @package
  6362. NSMutableDictionary *_dictionary;
  6363. }
  6364. + (instancetype)dictionary {
  6365. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6366. }
  6367. + (instancetype)dictionaryWithObject:(id)object
  6368. forKey:(uint64_t)key {
  6369. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6370. // on to get the type correct.
  6371. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6372. forKeys:&key
  6373. count:1] autorelease];
  6374. }
  6375. + (instancetype)dictionaryWithObjects:(const id [])objects
  6376. forKeys:(const uint64_t [])keys
  6377. count:(NSUInteger)count {
  6378. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6379. // on to get the type correct.
  6380. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6381. forKeys:keys
  6382. count:count] autorelease];
  6383. }
  6384. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6385. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6386. // on to get the type correct.
  6387. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6388. }
  6389. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6390. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6391. }
  6392. - (instancetype)init {
  6393. return [self initWithObjects:NULL forKeys:NULL count:0];
  6394. }
  6395. - (instancetype)initWithObjects:(const id [])objects
  6396. forKeys:(const uint64_t [])keys
  6397. count:(NSUInteger)count {
  6398. self = [super init];
  6399. if (self) {
  6400. _dictionary = [[NSMutableDictionary alloc] init];
  6401. if (count && objects && keys) {
  6402. for (NSUInteger i = 0; i < count; ++i) {
  6403. if (!objects[i]) {
  6404. [NSException raise:NSInvalidArgumentException
  6405. format:@"Attempting to add nil object to a Dictionary"];
  6406. }
  6407. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6408. }
  6409. }
  6410. }
  6411. return self;
  6412. }
  6413. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6414. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6415. if (self) {
  6416. if (dictionary) {
  6417. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6418. }
  6419. }
  6420. return self;
  6421. }
  6422. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6423. #pragma unused(numItems)
  6424. return [self initWithObjects:NULL forKeys:NULL count:0];
  6425. }
  6426. - (void)dealloc {
  6427. NSAssert(!_autocreator,
  6428. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6429. [self class], _autocreator);
  6430. [_dictionary release];
  6431. [super dealloc];
  6432. }
  6433. - (instancetype)copyWithZone:(NSZone *)zone {
  6434. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6435. }
  6436. - (BOOL)isEqual:(id)other {
  6437. if (self == other) {
  6438. return YES;
  6439. }
  6440. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6441. return NO;
  6442. }
  6443. GPBUInt64ObjectDictionary *otherDictionary = other;
  6444. return [_dictionary isEqual:otherDictionary->_dictionary];
  6445. }
  6446. - (NSUInteger)hash {
  6447. return _dictionary.count;
  6448. }
  6449. - (NSString *)description {
  6450. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6451. }
  6452. - (NSUInteger)count {
  6453. return _dictionary.count;
  6454. }
  6455. - (void)enumerateKeysAndObjectsUsingBlock:
  6456. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6457. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6458. id aObject,
  6459. BOOL *stop) {
  6460. block([aKey unsignedLongLongValue], aObject, stop);
  6461. }];
  6462. }
  6463. - (BOOL)isInitialized {
  6464. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6465. if (!msg.initialized) {
  6466. return NO;
  6467. }
  6468. }
  6469. return YES;
  6470. }
  6471. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6472. GPBUInt64ObjectDictionary *newDict =
  6473. [[GPBUInt64ObjectDictionary alloc] init];
  6474. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6475. GPBMessage *msg,
  6476. BOOL *stop) {
  6477. #pragma unused(stop)
  6478. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6479. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6480. [copiedMsg release];
  6481. }];
  6482. return newDict;
  6483. }
  6484. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6485. NSUInteger count = _dictionary.count;
  6486. if (count == 0) {
  6487. return 0;
  6488. }
  6489. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6490. GPBDataType keyDataType = field.mapKeyDataType;
  6491. __block size_t result = 0;
  6492. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6493. id aObject,
  6494. BOOL *stop) {
  6495. #pragma unused(stop)
  6496. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6497. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6498. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6499. }];
  6500. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6501. result += tagSize * count;
  6502. return result;
  6503. }
  6504. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6505. asField:(GPBFieldDescriptor *)field {
  6506. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6507. GPBDataType keyDataType = field.mapKeyDataType;
  6508. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6509. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6510. id aObject,
  6511. BOOL *stop) {
  6512. #pragma unused(stop)
  6513. // Write the tag.
  6514. [outputStream writeInt32NoTag:tag];
  6515. // Write the size of the message.
  6516. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6517. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6518. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6519. // Write the fields.
  6520. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6521. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  6522. }];
  6523. }
  6524. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6525. forGPBGenericValueKey:(GPBGenericValue *)key {
  6526. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6527. }
  6528. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6529. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6530. #pragma unused(stop)
  6531. block([NSString stringWithFormat:@"%llu", key], object);
  6532. }];
  6533. }
  6534. - (id)objectForKey:(uint64_t)key {
  6535. id result = [_dictionary objectForKey:@(key)];
  6536. return result;
  6537. }
  6538. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6539. if (otherDictionary) {
  6540. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6541. if (_autocreator) {
  6542. GPBAutocreatedDictionaryModified(_autocreator, self);
  6543. }
  6544. }
  6545. }
  6546. - (void)setObject:(id)object forKey:(uint64_t)key {
  6547. if (!object) {
  6548. [NSException raise:NSInvalidArgumentException
  6549. format:@"Attempting to add nil object to a Dictionary"];
  6550. }
  6551. [_dictionary setObject:object forKey:@(key)];
  6552. if (_autocreator) {
  6553. GPBAutocreatedDictionaryModified(_autocreator, self);
  6554. }
  6555. }
  6556. - (void)removeObjectForKey:(uint64_t)aKey {
  6557. [_dictionary removeObjectForKey:@(aKey)];
  6558. }
  6559. - (void)removeAll {
  6560. [_dictionary removeAllObjects];
  6561. }
  6562. @end
  6563. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6564. // This block of code is generated, do not edit it directly.
  6565. #pragma mark - Int64 -> UInt32
  6566. @implementation GPBInt64UInt32Dictionary {
  6567. @package
  6568. NSMutableDictionary *_dictionary;
  6569. }
  6570. + (instancetype)dictionary {
  6571. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6572. }
  6573. + (instancetype)dictionaryWithValue:(uint32_t)value
  6574. forKey:(int64_t)key {
  6575. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6576. // on to get the type correct.
  6577. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6578. forKeys:&key
  6579. count:1] autorelease];
  6580. }
  6581. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6582. forKeys:(const int64_t [])keys
  6583. count:(NSUInteger)count {
  6584. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6585. // on to get the type correct.
  6586. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6587. forKeys:keys
  6588. count:count] autorelease];
  6589. }
  6590. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6591. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6592. // on to get the type correct.
  6593. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6594. }
  6595. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6596. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6597. }
  6598. - (instancetype)init {
  6599. return [self initWithValues:NULL forKeys:NULL count:0];
  6600. }
  6601. - (instancetype)initWithValues:(const uint32_t [])values
  6602. forKeys:(const int64_t [])keys
  6603. count:(NSUInteger)count {
  6604. self = [super init];
  6605. if (self) {
  6606. _dictionary = [[NSMutableDictionary alloc] init];
  6607. if (count && values && keys) {
  6608. for (NSUInteger i = 0; i < count; ++i) {
  6609. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6610. }
  6611. }
  6612. }
  6613. return self;
  6614. }
  6615. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6616. self = [self initWithValues:NULL forKeys:NULL count:0];
  6617. if (self) {
  6618. if (dictionary) {
  6619. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6620. }
  6621. }
  6622. return self;
  6623. }
  6624. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6625. #pragma unused(numItems)
  6626. return [self initWithValues:NULL forKeys:NULL count:0];
  6627. }
  6628. - (void)dealloc {
  6629. NSAssert(!_autocreator,
  6630. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6631. [self class], _autocreator);
  6632. [_dictionary release];
  6633. [super dealloc];
  6634. }
  6635. - (instancetype)copyWithZone:(NSZone *)zone {
  6636. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6637. }
  6638. - (BOOL)isEqual:(id)other {
  6639. if (self == other) {
  6640. return YES;
  6641. }
  6642. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6643. return NO;
  6644. }
  6645. GPBInt64UInt32Dictionary *otherDictionary = other;
  6646. return [_dictionary isEqual:otherDictionary->_dictionary];
  6647. }
  6648. - (NSUInteger)hash {
  6649. return _dictionary.count;
  6650. }
  6651. - (NSString *)description {
  6652. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6653. }
  6654. - (NSUInteger)count {
  6655. return _dictionary.count;
  6656. }
  6657. - (void)enumerateKeysAndValuesUsingBlock:
  6658. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6659. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6660. NSNumber *aValue,
  6661. BOOL *stop) {
  6662. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6663. }];
  6664. }
  6665. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6666. NSUInteger count = _dictionary.count;
  6667. if (count == 0) {
  6668. return 0;
  6669. }
  6670. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6671. GPBDataType keyDataType = field.mapKeyDataType;
  6672. __block size_t result = 0;
  6673. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6674. NSNumber *aValue,
  6675. BOOL *stop) {
  6676. #pragma unused(stop)
  6677. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6678. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6679. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6680. }];
  6681. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6682. result += tagSize * count;
  6683. return result;
  6684. }
  6685. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6686. asField:(GPBFieldDescriptor *)field {
  6687. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6688. GPBDataType keyDataType = field.mapKeyDataType;
  6689. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6690. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6691. NSNumber *aValue,
  6692. BOOL *stop) {
  6693. #pragma unused(stop)
  6694. // Write the tag.
  6695. [outputStream writeInt32NoTag:tag];
  6696. // Write the size of the message.
  6697. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6698. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6699. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6700. // Write the fields.
  6701. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6702. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6703. }];
  6704. }
  6705. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6706. forGPBGenericValueKey:(GPBGenericValue *)key {
  6707. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6708. }
  6709. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6710. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6711. #pragma unused(stop)
  6712. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6713. }];
  6714. }
  6715. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6716. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6717. if (wrapped && value) {
  6718. *value = [wrapped unsignedIntValue];
  6719. }
  6720. return (wrapped != NULL);
  6721. }
  6722. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6723. if (otherDictionary) {
  6724. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6725. if (_autocreator) {
  6726. GPBAutocreatedDictionaryModified(_autocreator, self);
  6727. }
  6728. }
  6729. }
  6730. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6731. [_dictionary setObject:@(value) forKey:@(key)];
  6732. if (_autocreator) {
  6733. GPBAutocreatedDictionaryModified(_autocreator, self);
  6734. }
  6735. }
  6736. - (void)removeValueForKey:(int64_t)aKey {
  6737. [_dictionary removeObjectForKey:@(aKey)];
  6738. }
  6739. - (void)removeAll {
  6740. [_dictionary removeAllObjects];
  6741. }
  6742. @end
  6743. #pragma mark - Int64 -> Int32
  6744. @implementation GPBInt64Int32Dictionary {
  6745. @package
  6746. NSMutableDictionary *_dictionary;
  6747. }
  6748. + (instancetype)dictionary {
  6749. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6750. }
  6751. + (instancetype)dictionaryWithValue:(int32_t)value
  6752. forKey:(int64_t)key {
  6753. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6754. // on to get the type correct.
  6755. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6756. forKeys:&key
  6757. count:1] autorelease];
  6758. }
  6759. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6760. forKeys:(const int64_t [])keys
  6761. count:(NSUInteger)count {
  6762. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6763. // on to get the type correct.
  6764. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6765. forKeys:keys
  6766. count:count] autorelease];
  6767. }
  6768. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6769. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6770. // on to get the type correct.
  6771. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6772. }
  6773. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6774. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6775. }
  6776. - (instancetype)init {
  6777. return [self initWithValues:NULL forKeys:NULL count:0];
  6778. }
  6779. - (instancetype)initWithValues:(const int32_t [])values
  6780. forKeys:(const int64_t [])keys
  6781. count:(NSUInteger)count {
  6782. self = [super init];
  6783. if (self) {
  6784. _dictionary = [[NSMutableDictionary alloc] init];
  6785. if (count && values && keys) {
  6786. for (NSUInteger i = 0; i < count; ++i) {
  6787. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6788. }
  6789. }
  6790. }
  6791. return self;
  6792. }
  6793. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6794. self = [self initWithValues:NULL forKeys:NULL count:0];
  6795. if (self) {
  6796. if (dictionary) {
  6797. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6798. }
  6799. }
  6800. return self;
  6801. }
  6802. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6803. #pragma unused(numItems)
  6804. return [self initWithValues:NULL forKeys:NULL count:0];
  6805. }
  6806. - (void)dealloc {
  6807. NSAssert(!_autocreator,
  6808. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6809. [self class], _autocreator);
  6810. [_dictionary release];
  6811. [super dealloc];
  6812. }
  6813. - (instancetype)copyWithZone:(NSZone *)zone {
  6814. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6815. }
  6816. - (BOOL)isEqual:(id)other {
  6817. if (self == other) {
  6818. return YES;
  6819. }
  6820. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6821. return NO;
  6822. }
  6823. GPBInt64Int32Dictionary *otherDictionary = other;
  6824. return [_dictionary isEqual:otherDictionary->_dictionary];
  6825. }
  6826. - (NSUInteger)hash {
  6827. return _dictionary.count;
  6828. }
  6829. - (NSString *)description {
  6830. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6831. }
  6832. - (NSUInteger)count {
  6833. return _dictionary.count;
  6834. }
  6835. - (void)enumerateKeysAndValuesUsingBlock:
  6836. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6837. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6838. NSNumber *aValue,
  6839. BOOL *stop) {
  6840. block([aKey longLongValue], [aValue intValue], stop);
  6841. }];
  6842. }
  6843. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6844. NSUInteger count = _dictionary.count;
  6845. if (count == 0) {
  6846. return 0;
  6847. }
  6848. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6849. GPBDataType keyDataType = field.mapKeyDataType;
  6850. __block size_t result = 0;
  6851. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6852. NSNumber *aValue,
  6853. BOOL *stop) {
  6854. #pragma unused(stop)
  6855. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6856. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6857. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6858. }];
  6859. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6860. result += tagSize * count;
  6861. return result;
  6862. }
  6863. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6864. asField:(GPBFieldDescriptor *)field {
  6865. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6866. GPBDataType keyDataType = field.mapKeyDataType;
  6867. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6868. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6869. NSNumber *aValue,
  6870. BOOL *stop) {
  6871. #pragma unused(stop)
  6872. // Write the tag.
  6873. [outputStream writeInt32NoTag:tag];
  6874. // Write the size of the message.
  6875. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6876. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6877. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6878. // Write the fields.
  6879. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6880. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6881. }];
  6882. }
  6883. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6884. forGPBGenericValueKey:(GPBGenericValue *)key {
  6885. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6886. }
  6887. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6888. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6889. #pragma unused(stop)
  6890. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6891. }];
  6892. }
  6893. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6894. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6895. if (wrapped && value) {
  6896. *value = [wrapped intValue];
  6897. }
  6898. return (wrapped != NULL);
  6899. }
  6900. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6901. if (otherDictionary) {
  6902. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6903. if (_autocreator) {
  6904. GPBAutocreatedDictionaryModified(_autocreator, self);
  6905. }
  6906. }
  6907. }
  6908. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6909. [_dictionary setObject:@(value) forKey:@(key)];
  6910. if (_autocreator) {
  6911. GPBAutocreatedDictionaryModified(_autocreator, self);
  6912. }
  6913. }
  6914. - (void)removeValueForKey:(int64_t)aKey {
  6915. [_dictionary removeObjectForKey:@(aKey)];
  6916. }
  6917. - (void)removeAll {
  6918. [_dictionary removeAllObjects];
  6919. }
  6920. @end
  6921. #pragma mark - Int64 -> UInt64
  6922. @implementation GPBInt64UInt64Dictionary {
  6923. @package
  6924. NSMutableDictionary *_dictionary;
  6925. }
  6926. + (instancetype)dictionary {
  6927. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6928. }
  6929. + (instancetype)dictionaryWithValue:(uint64_t)value
  6930. forKey:(int64_t)key {
  6931. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6932. // on to get the type correct.
  6933. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6934. forKeys:&key
  6935. count:1] autorelease];
  6936. }
  6937. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6938. forKeys:(const int64_t [])keys
  6939. count:(NSUInteger)count {
  6940. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6941. // on to get the type correct.
  6942. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6943. forKeys:keys
  6944. count:count] autorelease];
  6945. }
  6946. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6947. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6948. // on to get the type correct.
  6949. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6950. }
  6951. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6952. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6953. }
  6954. - (instancetype)init {
  6955. return [self initWithValues:NULL forKeys:NULL count:0];
  6956. }
  6957. - (instancetype)initWithValues:(const uint64_t [])values
  6958. forKeys:(const int64_t [])keys
  6959. count:(NSUInteger)count {
  6960. self = [super init];
  6961. if (self) {
  6962. _dictionary = [[NSMutableDictionary alloc] init];
  6963. if (count && values && keys) {
  6964. for (NSUInteger i = 0; i < count; ++i) {
  6965. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6966. }
  6967. }
  6968. }
  6969. return self;
  6970. }
  6971. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6972. self = [self initWithValues:NULL forKeys:NULL count:0];
  6973. if (self) {
  6974. if (dictionary) {
  6975. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6976. }
  6977. }
  6978. return self;
  6979. }
  6980. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6981. #pragma unused(numItems)
  6982. return [self initWithValues:NULL forKeys:NULL count:0];
  6983. }
  6984. - (void)dealloc {
  6985. NSAssert(!_autocreator,
  6986. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6987. [self class], _autocreator);
  6988. [_dictionary release];
  6989. [super dealloc];
  6990. }
  6991. - (instancetype)copyWithZone:(NSZone *)zone {
  6992. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6993. }
  6994. - (BOOL)isEqual:(id)other {
  6995. if (self == other) {
  6996. return YES;
  6997. }
  6998. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6999. return NO;
  7000. }
  7001. GPBInt64UInt64Dictionary *otherDictionary = other;
  7002. return [_dictionary isEqual:otherDictionary->_dictionary];
  7003. }
  7004. - (NSUInteger)hash {
  7005. return _dictionary.count;
  7006. }
  7007. - (NSString *)description {
  7008. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7009. }
  7010. - (NSUInteger)count {
  7011. return _dictionary.count;
  7012. }
  7013. - (void)enumerateKeysAndValuesUsingBlock:
  7014. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  7015. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7016. NSNumber *aValue,
  7017. BOOL *stop) {
  7018. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  7019. }];
  7020. }
  7021. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7022. NSUInteger count = _dictionary.count;
  7023. if (count == 0) {
  7024. return 0;
  7025. }
  7026. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7027. GPBDataType keyDataType = field.mapKeyDataType;
  7028. __block size_t result = 0;
  7029. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7030. NSNumber *aValue,
  7031. BOOL *stop) {
  7032. #pragma unused(stop)
  7033. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7034. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7035. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7036. }];
  7037. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7038. result += tagSize * count;
  7039. return result;
  7040. }
  7041. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7042. asField:(GPBFieldDescriptor *)field {
  7043. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7044. GPBDataType keyDataType = field.mapKeyDataType;
  7045. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7046. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7047. NSNumber *aValue,
  7048. BOOL *stop) {
  7049. #pragma unused(stop)
  7050. // Write the tag.
  7051. [outputStream writeInt32NoTag:tag];
  7052. // Write the size of the message.
  7053. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7054. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7055. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7056. // Write the fields.
  7057. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7058. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7059. }];
  7060. }
  7061. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7062. forGPBGenericValueKey:(GPBGenericValue *)key {
  7063. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  7064. }
  7065. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7066. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  7067. #pragma unused(stop)
  7068. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  7069. }];
  7070. }
  7071. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  7072. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7073. if (wrapped && value) {
  7074. *value = [wrapped unsignedLongLongValue];
  7075. }
  7076. return (wrapped != NULL);
  7077. }
  7078. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  7079. if (otherDictionary) {
  7080. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7081. if (_autocreator) {
  7082. GPBAutocreatedDictionaryModified(_autocreator, self);
  7083. }
  7084. }
  7085. }
  7086. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  7087. [_dictionary setObject:@(value) forKey:@(key)];
  7088. if (_autocreator) {
  7089. GPBAutocreatedDictionaryModified(_autocreator, self);
  7090. }
  7091. }
  7092. - (void)removeValueForKey:(int64_t)aKey {
  7093. [_dictionary removeObjectForKey:@(aKey)];
  7094. }
  7095. - (void)removeAll {
  7096. [_dictionary removeAllObjects];
  7097. }
  7098. @end
  7099. #pragma mark - Int64 -> Int64
  7100. @implementation GPBInt64Int64Dictionary {
  7101. @package
  7102. NSMutableDictionary *_dictionary;
  7103. }
  7104. + (instancetype)dictionary {
  7105. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7106. }
  7107. + (instancetype)dictionaryWithValue:(int64_t)value
  7108. forKey:(int64_t)key {
  7109. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7110. // on to get the type correct.
  7111. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  7112. forKeys:&key
  7113. count:1] autorelease];
  7114. }
  7115. + (instancetype)dictionaryWithValues:(const int64_t [])values
  7116. forKeys:(const int64_t [])keys
  7117. count:(NSUInteger)count {
  7118. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7119. // on to get the type correct.
  7120. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  7121. forKeys:keys
  7122. count:count] autorelease];
  7123. }
  7124. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7125. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7126. // on to get the type correct.
  7127. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7128. }
  7129. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7130. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7131. }
  7132. - (instancetype)init {
  7133. return [self initWithValues:NULL forKeys:NULL count:0];
  7134. }
  7135. - (instancetype)initWithValues:(const int64_t [])values
  7136. forKeys:(const int64_t [])keys
  7137. count:(NSUInteger)count {
  7138. self = [super init];
  7139. if (self) {
  7140. _dictionary = [[NSMutableDictionary alloc] init];
  7141. if (count && values && keys) {
  7142. for (NSUInteger i = 0; i < count; ++i) {
  7143. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7144. }
  7145. }
  7146. }
  7147. return self;
  7148. }
  7149. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7150. self = [self initWithValues:NULL forKeys:NULL count:0];
  7151. if (self) {
  7152. if (dictionary) {
  7153. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7154. }
  7155. }
  7156. return self;
  7157. }
  7158. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7159. #pragma unused(numItems)
  7160. return [self initWithValues:NULL forKeys:NULL count:0];
  7161. }
  7162. - (void)dealloc {
  7163. NSAssert(!_autocreator,
  7164. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7165. [self class], _autocreator);
  7166. [_dictionary release];
  7167. [super dealloc];
  7168. }
  7169. - (instancetype)copyWithZone:(NSZone *)zone {
  7170. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7171. }
  7172. - (BOOL)isEqual:(id)other {
  7173. if (self == other) {
  7174. return YES;
  7175. }
  7176. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7177. return NO;
  7178. }
  7179. GPBInt64Int64Dictionary *otherDictionary = other;
  7180. return [_dictionary isEqual:otherDictionary->_dictionary];
  7181. }
  7182. - (NSUInteger)hash {
  7183. return _dictionary.count;
  7184. }
  7185. - (NSString *)description {
  7186. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7187. }
  7188. - (NSUInteger)count {
  7189. return _dictionary.count;
  7190. }
  7191. - (void)enumerateKeysAndValuesUsingBlock:
  7192. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7193. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7194. NSNumber *aValue,
  7195. BOOL *stop) {
  7196. block([aKey longLongValue], [aValue longLongValue], stop);
  7197. }];
  7198. }
  7199. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7200. NSUInteger count = _dictionary.count;
  7201. if (count == 0) {
  7202. return 0;
  7203. }
  7204. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7205. GPBDataType keyDataType = field.mapKeyDataType;
  7206. __block size_t result = 0;
  7207. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7208. NSNumber *aValue,
  7209. BOOL *stop) {
  7210. #pragma unused(stop)
  7211. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7212. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7213. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7214. }];
  7215. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7216. result += tagSize * count;
  7217. return result;
  7218. }
  7219. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7220. asField:(GPBFieldDescriptor *)field {
  7221. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7222. GPBDataType keyDataType = field.mapKeyDataType;
  7223. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7224. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7225. NSNumber *aValue,
  7226. BOOL *stop) {
  7227. #pragma unused(stop)
  7228. // Write the tag.
  7229. [outputStream writeInt32NoTag:tag];
  7230. // Write the size of the message.
  7231. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7232. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7233. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7234. // Write the fields.
  7235. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7236. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7237. }];
  7238. }
  7239. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7240. forGPBGenericValueKey:(GPBGenericValue *)key {
  7241. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7242. }
  7243. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7244. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7245. #pragma unused(stop)
  7246. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7247. }];
  7248. }
  7249. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  7250. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7251. if (wrapped && value) {
  7252. *value = [wrapped longLongValue];
  7253. }
  7254. return (wrapped != NULL);
  7255. }
  7256. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7257. if (otherDictionary) {
  7258. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7259. if (_autocreator) {
  7260. GPBAutocreatedDictionaryModified(_autocreator, self);
  7261. }
  7262. }
  7263. }
  7264. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  7265. [_dictionary setObject:@(value) forKey:@(key)];
  7266. if (_autocreator) {
  7267. GPBAutocreatedDictionaryModified(_autocreator, self);
  7268. }
  7269. }
  7270. - (void)removeValueForKey:(int64_t)aKey {
  7271. [_dictionary removeObjectForKey:@(aKey)];
  7272. }
  7273. - (void)removeAll {
  7274. [_dictionary removeAllObjects];
  7275. }
  7276. @end
  7277. #pragma mark - Int64 -> Bool
  7278. @implementation GPBInt64BoolDictionary {
  7279. @package
  7280. NSMutableDictionary *_dictionary;
  7281. }
  7282. + (instancetype)dictionary {
  7283. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7284. }
  7285. + (instancetype)dictionaryWithValue:(BOOL)value
  7286. forKey:(int64_t)key {
  7287. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7288. // on to get the type correct.
  7289. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  7290. forKeys:&key
  7291. count:1] autorelease];
  7292. }
  7293. + (instancetype)dictionaryWithValues:(const BOOL [])values
  7294. forKeys:(const int64_t [])keys
  7295. count:(NSUInteger)count {
  7296. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7297. // on to get the type correct.
  7298. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  7299. forKeys:keys
  7300. count:count] autorelease];
  7301. }
  7302. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7303. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7304. // on to get the type correct.
  7305. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7306. }
  7307. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7308. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7309. }
  7310. - (instancetype)init {
  7311. return [self initWithValues:NULL forKeys:NULL count:0];
  7312. }
  7313. - (instancetype)initWithValues:(const BOOL [])values
  7314. forKeys:(const int64_t [])keys
  7315. count:(NSUInteger)count {
  7316. self = [super init];
  7317. if (self) {
  7318. _dictionary = [[NSMutableDictionary alloc] init];
  7319. if (count && values && keys) {
  7320. for (NSUInteger i = 0; i < count; ++i) {
  7321. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7322. }
  7323. }
  7324. }
  7325. return self;
  7326. }
  7327. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7328. self = [self initWithValues:NULL forKeys:NULL count:0];
  7329. if (self) {
  7330. if (dictionary) {
  7331. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7332. }
  7333. }
  7334. return self;
  7335. }
  7336. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7337. #pragma unused(numItems)
  7338. return [self initWithValues:NULL forKeys:NULL count:0];
  7339. }
  7340. - (void)dealloc {
  7341. NSAssert(!_autocreator,
  7342. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7343. [self class], _autocreator);
  7344. [_dictionary release];
  7345. [super dealloc];
  7346. }
  7347. - (instancetype)copyWithZone:(NSZone *)zone {
  7348. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7349. }
  7350. - (BOOL)isEqual:(id)other {
  7351. if (self == other) {
  7352. return YES;
  7353. }
  7354. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7355. return NO;
  7356. }
  7357. GPBInt64BoolDictionary *otherDictionary = other;
  7358. return [_dictionary isEqual:otherDictionary->_dictionary];
  7359. }
  7360. - (NSUInteger)hash {
  7361. return _dictionary.count;
  7362. }
  7363. - (NSString *)description {
  7364. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7365. }
  7366. - (NSUInteger)count {
  7367. return _dictionary.count;
  7368. }
  7369. - (void)enumerateKeysAndValuesUsingBlock:
  7370. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7371. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7372. NSNumber *aValue,
  7373. BOOL *stop) {
  7374. block([aKey longLongValue], [aValue boolValue], stop);
  7375. }];
  7376. }
  7377. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7378. NSUInteger count = _dictionary.count;
  7379. if (count == 0) {
  7380. return 0;
  7381. }
  7382. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7383. GPBDataType keyDataType = field.mapKeyDataType;
  7384. __block size_t result = 0;
  7385. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7386. NSNumber *aValue,
  7387. BOOL *stop) {
  7388. #pragma unused(stop)
  7389. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7390. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7391. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7392. }];
  7393. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7394. result += tagSize * count;
  7395. return result;
  7396. }
  7397. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7398. asField:(GPBFieldDescriptor *)field {
  7399. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7400. GPBDataType keyDataType = field.mapKeyDataType;
  7401. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7402. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7403. NSNumber *aValue,
  7404. BOOL *stop) {
  7405. #pragma unused(stop)
  7406. // Write the tag.
  7407. [outputStream writeInt32NoTag:tag];
  7408. // Write the size of the message.
  7409. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7410. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7411. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7412. // Write the fields.
  7413. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7414. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7415. }];
  7416. }
  7417. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7418. forGPBGenericValueKey:(GPBGenericValue *)key {
  7419. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7420. }
  7421. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7422. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7423. #pragma unused(stop)
  7424. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7425. }];
  7426. }
  7427. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  7428. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7429. if (wrapped && value) {
  7430. *value = [wrapped boolValue];
  7431. }
  7432. return (wrapped != NULL);
  7433. }
  7434. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7435. if (otherDictionary) {
  7436. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7437. if (_autocreator) {
  7438. GPBAutocreatedDictionaryModified(_autocreator, self);
  7439. }
  7440. }
  7441. }
  7442. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  7443. [_dictionary setObject:@(value) forKey:@(key)];
  7444. if (_autocreator) {
  7445. GPBAutocreatedDictionaryModified(_autocreator, self);
  7446. }
  7447. }
  7448. - (void)removeValueForKey:(int64_t)aKey {
  7449. [_dictionary removeObjectForKey:@(aKey)];
  7450. }
  7451. - (void)removeAll {
  7452. [_dictionary removeAllObjects];
  7453. }
  7454. @end
  7455. #pragma mark - Int64 -> Float
  7456. @implementation GPBInt64FloatDictionary {
  7457. @package
  7458. NSMutableDictionary *_dictionary;
  7459. }
  7460. + (instancetype)dictionary {
  7461. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7462. }
  7463. + (instancetype)dictionaryWithValue:(float)value
  7464. forKey:(int64_t)key {
  7465. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7466. // on to get the type correct.
  7467. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  7468. forKeys:&key
  7469. count:1] autorelease];
  7470. }
  7471. + (instancetype)dictionaryWithValues:(const float [])values
  7472. forKeys:(const int64_t [])keys
  7473. count:(NSUInteger)count {
  7474. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7475. // on to get the type correct.
  7476. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  7477. forKeys:keys
  7478. count:count] autorelease];
  7479. }
  7480. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7481. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7482. // on to get the type correct.
  7483. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7484. }
  7485. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7486. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7487. }
  7488. - (instancetype)init {
  7489. return [self initWithValues:NULL forKeys:NULL count:0];
  7490. }
  7491. - (instancetype)initWithValues:(const float [])values
  7492. forKeys:(const int64_t [])keys
  7493. count:(NSUInteger)count {
  7494. self = [super init];
  7495. if (self) {
  7496. _dictionary = [[NSMutableDictionary alloc] init];
  7497. if (count && values && keys) {
  7498. for (NSUInteger i = 0; i < count; ++i) {
  7499. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7500. }
  7501. }
  7502. }
  7503. return self;
  7504. }
  7505. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7506. self = [self initWithValues:NULL forKeys:NULL count:0];
  7507. if (self) {
  7508. if (dictionary) {
  7509. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7510. }
  7511. }
  7512. return self;
  7513. }
  7514. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7515. #pragma unused(numItems)
  7516. return [self initWithValues:NULL forKeys:NULL count:0];
  7517. }
  7518. - (void)dealloc {
  7519. NSAssert(!_autocreator,
  7520. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7521. [self class], _autocreator);
  7522. [_dictionary release];
  7523. [super dealloc];
  7524. }
  7525. - (instancetype)copyWithZone:(NSZone *)zone {
  7526. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7527. }
  7528. - (BOOL)isEqual:(id)other {
  7529. if (self == other) {
  7530. return YES;
  7531. }
  7532. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7533. return NO;
  7534. }
  7535. GPBInt64FloatDictionary *otherDictionary = other;
  7536. return [_dictionary isEqual:otherDictionary->_dictionary];
  7537. }
  7538. - (NSUInteger)hash {
  7539. return _dictionary.count;
  7540. }
  7541. - (NSString *)description {
  7542. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7543. }
  7544. - (NSUInteger)count {
  7545. return _dictionary.count;
  7546. }
  7547. - (void)enumerateKeysAndValuesUsingBlock:
  7548. (void (^)(int64_t key, float value, BOOL *stop))block {
  7549. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7550. NSNumber *aValue,
  7551. BOOL *stop) {
  7552. block([aKey longLongValue], [aValue floatValue], stop);
  7553. }];
  7554. }
  7555. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7556. NSUInteger count = _dictionary.count;
  7557. if (count == 0) {
  7558. return 0;
  7559. }
  7560. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7561. GPBDataType keyDataType = field.mapKeyDataType;
  7562. __block size_t result = 0;
  7563. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7564. NSNumber *aValue,
  7565. BOOL *stop) {
  7566. #pragma unused(stop)
  7567. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7568. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7570. }];
  7571. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7572. result += tagSize * count;
  7573. return result;
  7574. }
  7575. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7576. asField:(GPBFieldDescriptor *)field {
  7577. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7578. GPBDataType keyDataType = field.mapKeyDataType;
  7579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7580. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7581. NSNumber *aValue,
  7582. BOOL *stop) {
  7583. #pragma unused(stop)
  7584. // Write the tag.
  7585. [outputStream writeInt32NoTag:tag];
  7586. // Write the size of the message.
  7587. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7588. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7589. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7590. // Write the fields.
  7591. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7592. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7593. }];
  7594. }
  7595. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7596. forGPBGenericValueKey:(GPBGenericValue *)key {
  7597. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7598. }
  7599. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7600. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7601. #pragma unused(stop)
  7602. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7603. }];
  7604. }
  7605. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7606. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7607. if (wrapped && value) {
  7608. *value = [wrapped floatValue];
  7609. }
  7610. return (wrapped != NULL);
  7611. }
  7612. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7613. if (otherDictionary) {
  7614. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7615. if (_autocreator) {
  7616. GPBAutocreatedDictionaryModified(_autocreator, self);
  7617. }
  7618. }
  7619. }
  7620. - (void)setValue:(float)value forKey:(int64_t)key {
  7621. [_dictionary setObject:@(value) forKey:@(key)];
  7622. if (_autocreator) {
  7623. GPBAutocreatedDictionaryModified(_autocreator, self);
  7624. }
  7625. }
  7626. - (void)removeValueForKey:(int64_t)aKey {
  7627. [_dictionary removeObjectForKey:@(aKey)];
  7628. }
  7629. - (void)removeAll {
  7630. [_dictionary removeAllObjects];
  7631. }
  7632. @end
  7633. #pragma mark - Int64 -> Double
  7634. @implementation GPBInt64DoubleDictionary {
  7635. @package
  7636. NSMutableDictionary *_dictionary;
  7637. }
  7638. + (instancetype)dictionary {
  7639. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7640. }
  7641. + (instancetype)dictionaryWithValue:(double)value
  7642. forKey:(int64_t)key {
  7643. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7644. // on to get the type correct.
  7645. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7646. forKeys:&key
  7647. count:1] autorelease];
  7648. }
  7649. + (instancetype)dictionaryWithValues:(const double [])values
  7650. forKeys:(const int64_t [])keys
  7651. count:(NSUInteger)count {
  7652. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7653. // on to get the type correct.
  7654. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7655. forKeys:keys
  7656. count:count] autorelease];
  7657. }
  7658. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7659. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7660. // on to get the type correct.
  7661. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7662. }
  7663. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7664. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7665. }
  7666. - (instancetype)init {
  7667. return [self initWithValues:NULL forKeys:NULL count:0];
  7668. }
  7669. - (instancetype)initWithValues:(const double [])values
  7670. forKeys:(const int64_t [])keys
  7671. count:(NSUInteger)count {
  7672. self = [super init];
  7673. if (self) {
  7674. _dictionary = [[NSMutableDictionary alloc] init];
  7675. if (count && values && keys) {
  7676. for (NSUInteger i = 0; i < count; ++i) {
  7677. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7678. }
  7679. }
  7680. }
  7681. return self;
  7682. }
  7683. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7684. self = [self initWithValues:NULL forKeys:NULL count:0];
  7685. if (self) {
  7686. if (dictionary) {
  7687. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7688. }
  7689. }
  7690. return self;
  7691. }
  7692. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7693. #pragma unused(numItems)
  7694. return [self initWithValues:NULL forKeys:NULL count:0];
  7695. }
  7696. - (void)dealloc {
  7697. NSAssert(!_autocreator,
  7698. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7699. [self class], _autocreator);
  7700. [_dictionary release];
  7701. [super dealloc];
  7702. }
  7703. - (instancetype)copyWithZone:(NSZone *)zone {
  7704. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7705. }
  7706. - (BOOL)isEqual:(id)other {
  7707. if (self == other) {
  7708. return YES;
  7709. }
  7710. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7711. return NO;
  7712. }
  7713. GPBInt64DoubleDictionary *otherDictionary = other;
  7714. return [_dictionary isEqual:otherDictionary->_dictionary];
  7715. }
  7716. - (NSUInteger)hash {
  7717. return _dictionary.count;
  7718. }
  7719. - (NSString *)description {
  7720. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7721. }
  7722. - (NSUInteger)count {
  7723. return _dictionary.count;
  7724. }
  7725. - (void)enumerateKeysAndValuesUsingBlock:
  7726. (void (^)(int64_t key, double value, BOOL *stop))block {
  7727. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7728. NSNumber *aValue,
  7729. BOOL *stop) {
  7730. block([aKey longLongValue], [aValue doubleValue], stop);
  7731. }];
  7732. }
  7733. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7734. NSUInteger count = _dictionary.count;
  7735. if (count == 0) {
  7736. return 0;
  7737. }
  7738. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7739. GPBDataType keyDataType = field.mapKeyDataType;
  7740. __block size_t result = 0;
  7741. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7742. NSNumber *aValue,
  7743. BOOL *stop) {
  7744. #pragma unused(stop)
  7745. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7746. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7747. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7748. }];
  7749. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7750. result += tagSize * count;
  7751. return result;
  7752. }
  7753. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7754. asField:(GPBFieldDescriptor *)field {
  7755. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7756. GPBDataType keyDataType = field.mapKeyDataType;
  7757. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7758. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7759. NSNumber *aValue,
  7760. BOOL *stop) {
  7761. #pragma unused(stop)
  7762. // Write the tag.
  7763. [outputStream writeInt32NoTag:tag];
  7764. // Write the size of the message.
  7765. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7766. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7767. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7768. // Write the fields.
  7769. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7770. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7771. }];
  7772. }
  7773. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7774. forGPBGenericValueKey:(GPBGenericValue *)key {
  7775. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7776. }
  7777. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7778. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7779. #pragma unused(stop)
  7780. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7781. }];
  7782. }
  7783. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7784. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7785. if (wrapped && value) {
  7786. *value = [wrapped doubleValue];
  7787. }
  7788. return (wrapped != NULL);
  7789. }
  7790. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7791. if (otherDictionary) {
  7792. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7793. if (_autocreator) {
  7794. GPBAutocreatedDictionaryModified(_autocreator, self);
  7795. }
  7796. }
  7797. }
  7798. - (void)setValue:(double)value forKey:(int64_t)key {
  7799. [_dictionary setObject:@(value) forKey:@(key)];
  7800. if (_autocreator) {
  7801. GPBAutocreatedDictionaryModified(_autocreator, self);
  7802. }
  7803. }
  7804. - (void)removeValueForKey:(int64_t)aKey {
  7805. [_dictionary removeObjectForKey:@(aKey)];
  7806. }
  7807. - (void)removeAll {
  7808. [_dictionary removeAllObjects];
  7809. }
  7810. @end
  7811. #pragma mark - Int64 -> Enum
  7812. @implementation GPBInt64EnumDictionary {
  7813. @package
  7814. NSMutableDictionary *_dictionary;
  7815. GPBEnumValidationFunc _validationFunc;
  7816. }
  7817. @synthesize validationFunc = _validationFunc;
  7818. + (instancetype)dictionary {
  7819. return [[[self alloc] initWithValidationFunction:NULL
  7820. rawValues:NULL
  7821. forKeys:NULL
  7822. count:0] autorelease];
  7823. }
  7824. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7825. return [[[self alloc] initWithValidationFunction:func
  7826. rawValues:NULL
  7827. forKeys:NULL
  7828. count:0] autorelease];
  7829. }
  7830. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7831. rawValue:(int32_t)rawValue
  7832. forKey:(int64_t)key {
  7833. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7834. // on to get the type correct.
  7835. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7836. rawValues:&rawValue
  7837. forKeys:&key
  7838. count:1] autorelease];
  7839. }
  7840. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7841. rawValues:(const int32_t [])rawValues
  7842. forKeys:(const int64_t [])keys
  7843. count:(NSUInteger)count {
  7844. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7845. // on to get the type correct.
  7846. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7847. rawValues:rawValues
  7848. forKeys:keys
  7849. count:count] autorelease];
  7850. }
  7851. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7852. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7853. // on to get the type correct.
  7854. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7855. }
  7856. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7857. capacity:(NSUInteger)numItems {
  7858. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7859. }
  7860. - (instancetype)init {
  7861. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7862. }
  7863. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7864. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7865. }
  7866. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7867. rawValues:(const int32_t [])rawValues
  7868. forKeys:(const int64_t [])keys
  7869. count:(NSUInteger)count {
  7870. self = [super init];
  7871. if (self) {
  7872. _dictionary = [[NSMutableDictionary alloc] init];
  7873. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7874. if (count && rawValues && keys) {
  7875. for (NSUInteger i = 0; i < count; ++i) {
  7876. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7877. }
  7878. }
  7879. }
  7880. return self;
  7881. }
  7882. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7883. self = [self initWithValidationFunction:dictionary.validationFunc
  7884. rawValues:NULL
  7885. forKeys:NULL
  7886. count:0];
  7887. if (self) {
  7888. if (dictionary) {
  7889. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7890. }
  7891. }
  7892. return self;
  7893. }
  7894. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7895. capacity:(NSUInteger)numItems {
  7896. #pragma unused(numItems)
  7897. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7898. }
  7899. - (void)dealloc {
  7900. NSAssert(!_autocreator,
  7901. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7902. [self class], _autocreator);
  7903. [_dictionary release];
  7904. [super dealloc];
  7905. }
  7906. - (instancetype)copyWithZone:(NSZone *)zone {
  7907. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7908. }
  7909. - (BOOL)isEqual:(id)other {
  7910. if (self == other) {
  7911. return YES;
  7912. }
  7913. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7914. return NO;
  7915. }
  7916. GPBInt64EnumDictionary *otherDictionary = other;
  7917. return [_dictionary isEqual:otherDictionary->_dictionary];
  7918. }
  7919. - (NSUInteger)hash {
  7920. return _dictionary.count;
  7921. }
  7922. - (NSString *)description {
  7923. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7924. }
  7925. - (NSUInteger)count {
  7926. return _dictionary.count;
  7927. }
  7928. - (void)enumerateKeysAndRawValuesUsingBlock:
  7929. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7930. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7931. NSNumber *aValue,
  7932. BOOL *stop) {
  7933. block([aKey longLongValue], [aValue intValue], stop);
  7934. }];
  7935. }
  7936. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7937. NSUInteger count = _dictionary.count;
  7938. if (count == 0) {
  7939. return 0;
  7940. }
  7941. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7942. GPBDataType keyDataType = field.mapKeyDataType;
  7943. __block size_t result = 0;
  7944. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7945. NSNumber *aValue,
  7946. BOOL *stop) {
  7947. #pragma unused(stop)
  7948. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7949. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7950. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7951. }];
  7952. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7953. result += tagSize * count;
  7954. return result;
  7955. }
  7956. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7957. asField:(GPBFieldDescriptor *)field {
  7958. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7959. GPBDataType keyDataType = field.mapKeyDataType;
  7960. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7961. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7962. NSNumber *aValue,
  7963. BOOL *stop) {
  7964. #pragma unused(stop)
  7965. // Write the tag.
  7966. [outputStream writeInt32NoTag:tag];
  7967. // Write the size of the message.
  7968. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7969. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7970. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7971. // Write the fields.
  7972. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7973. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7974. }];
  7975. }
  7976. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7977. forKey:(GPBGenericValue *)key
  7978. keyDataType:(GPBDataType)keyDataType {
  7979. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7980. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7981. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7982. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7983. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7984. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7985. [outputStream release];
  7986. return data;
  7987. }
  7988. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7989. forGPBGenericValueKey:(GPBGenericValue *)key {
  7990. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7991. }
  7992. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7993. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7994. #pragma unused(stop)
  7995. block([NSString stringWithFormat:@"%lld", key], @(value));
  7996. }];
  7997. }
  7998. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7999. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8000. if (wrapped && value) {
  8001. int32_t result = [wrapped intValue];
  8002. if (!_validationFunc(result)) {
  8003. result = kGPBUnrecognizedEnumeratorValue;
  8004. }
  8005. *value = result;
  8006. }
  8007. return (wrapped != NULL);
  8008. }
  8009. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  8010. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8011. if (wrapped && rawValue) {
  8012. *rawValue = [wrapped intValue];
  8013. }
  8014. return (wrapped != NULL);
  8015. }
  8016. - (void)enumerateKeysAndValuesUsingBlock:
  8017. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  8018. GPBEnumValidationFunc func = _validationFunc;
  8019. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8020. NSNumber *aValue,
  8021. BOOL *stop) {
  8022. int32_t unwrapped = [aValue intValue];
  8023. if (!func(unwrapped)) {
  8024. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8025. }
  8026. block([aKey longLongValue], unwrapped, stop);
  8027. }];
  8028. }
  8029. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  8030. if (otherDictionary) {
  8031. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8032. if (_autocreator) {
  8033. GPBAutocreatedDictionaryModified(_autocreator, self);
  8034. }
  8035. }
  8036. }
  8037. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  8038. [_dictionary setObject:@(value) forKey:@(key)];
  8039. if (_autocreator) {
  8040. GPBAutocreatedDictionaryModified(_autocreator, self);
  8041. }
  8042. }
  8043. - (void)removeValueForKey:(int64_t)aKey {
  8044. [_dictionary removeObjectForKey:@(aKey)];
  8045. }
  8046. - (void)removeAll {
  8047. [_dictionary removeAllObjects];
  8048. }
  8049. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  8050. if (!_validationFunc(value)) {
  8051. [NSException raise:NSInvalidArgumentException
  8052. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  8053. value];
  8054. }
  8055. [_dictionary setObject:@(value) forKey:@(key)];
  8056. if (_autocreator) {
  8057. GPBAutocreatedDictionaryModified(_autocreator, self);
  8058. }
  8059. }
  8060. @end
  8061. #pragma mark - Int64 -> Object
  8062. @implementation GPBInt64ObjectDictionary {
  8063. @package
  8064. NSMutableDictionary *_dictionary;
  8065. }
  8066. + (instancetype)dictionary {
  8067. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  8068. }
  8069. + (instancetype)dictionaryWithObject:(id)object
  8070. forKey:(int64_t)key {
  8071. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8072. // on to get the type correct.
  8073. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  8074. forKeys:&key
  8075. count:1] autorelease];
  8076. }
  8077. + (instancetype)dictionaryWithObjects:(const id [])objects
  8078. forKeys:(const int64_t [])keys
  8079. count:(NSUInteger)count {
  8080. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8081. // on to get the type correct.
  8082. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  8083. forKeys:keys
  8084. count:count] autorelease];
  8085. }
  8086. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8087. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8088. // on to get the type correct.
  8089. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8090. }
  8091. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8092. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8093. }
  8094. - (instancetype)init {
  8095. return [self initWithObjects:NULL forKeys:NULL count:0];
  8096. }
  8097. - (instancetype)initWithObjects:(const id [])objects
  8098. forKeys:(const int64_t [])keys
  8099. count:(NSUInteger)count {
  8100. self = [super init];
  8101. if (self) {
  8102. _dictionary = [[NSMutableDictionary alloc] init];
  8103. if (count && objects && keys) {
  8104. for (NSUInteger i = 0; i < count; ++i) {
  8105. if (!objects[i]) {
  8106. [NSException raise:NSInvalidArgumentException
  8107. format:@"Attempting to add nil object to a Dictionary"];
  8108. }
  8109. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  8110. }
  8111. }
  8112. }
  8113. return self;
  8114. }
  8115. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8116. self = [self initWithObjects:NULL forKeys:NULL count:0];
  8117. if (self) {
  8118. if (dictionary) {
  8119. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8120. }
  8121. }
  8122. return self;
  8123. }
  8124. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8125. #pragma unused(numItems)
  8126. return [self initWithObjects:NULL forKeys:NULL count:0];
  8127. }
  8128. - (void)dealloc {
  8129. NSAssert(!_autocreator,
  8130. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8131. [self class], _autocreator);
  8132. [_dictionary release];
  8133. [super dealloc];
  8134. }
  8135. - (instancetype)copyWithZone:(NSZone *)zone {
  8136. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8137. }
  8138. - (BOOL)isEqual:(id)other {
  8139. if (self == other) {
  8140. return YES;
  8141. }
  8142. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8143. return NO;
  8144. }
  8145. GPBInt64ObjectDictionary *otherDictionary = other;
  8146. return [_dictionary isEqual:otherDictionary->_dictionary];
  8147. }
  8148. - (NSUInteger)hash {
  8149. return _dictionary.count;
  8150. }
  8151. - (NSString *)description {
  8152. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8153. }
  8154. - (NSUInteger)count {
  8155. return _dictionary.count;
  8156. }
  8157. - (void)enumerateKeysAndObjectsUsingBlock:
  8158. (void (^)(int64_t key, id object, BOOL *stop))block {
  8159. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8160. id aObject,
  8161. BOOL *stop) {
  8162. block([aKey longLongValue], aObject, stop);
  8163. }];
  8164. }
  8165. - (BOOL)isInitialized {
  8166. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8167. if (!msg.initialized) {
  8168. return NO;
  8169. }
  8170. }
  8171. return YES;
  8172. }
  8173. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8174. GPBInt64ObjectDictionary *newDict =
  8175. [[GPBInt64ObjectDictionary alloc] init];
  8176. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8177. GPBMessage *msg,
  8178. BOOL *stop) {
  8179. #pragma unused(stop)
  8180. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8181. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8182. [copiedMsg release];
  8183. }];
  8184. return newDict;
  8185. }
  8186. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8187. NSUInteger count = _dictionary.count;
  8188. if (count == 0) {
  8189. return 0;
  8190. }
  8191. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8192. GPBDataType keyDataType = field.mapKeyDataType;
  8193. __block size_t result = 0;
  8194. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8195. id aObject,
  8196. BOOL *stop) {
  8197. #pragma unused(stop)
  8198. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8199. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8200. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8201. }];
  8202. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8203. result += tagSize * count;
  8204. return result;
  8205. }
  8206. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8207. asField:(GPBFieldDescriptor *)field {
  8208. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8209. GPBDataType keyDataType = field.mapKeyDataType;
  8210. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8211. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8212. id aObject,
  8213. BOOL *stop) {
  8214. #pragma unused(stop)
  8215. // Write the tag.
  8216. [outputStream writeInt32NoTag:tag];
  8217. // Write the size of the message.
  8218. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8219. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8220. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8221. // Write the fields.
  8222. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8223. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  8224. }];
  8225. }
  8226. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8227. forGPBGenericValueKey:(GPBGenericValue *)key {
  8228. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8229. }
  8230. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8231. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8232. #pragma unused(stop)
  8233. block([NSString stringWithFormat:@"%lld", key], object);
  8234. }];
  8235. }
  8236. - (id)objectForKey:(int64_t)key {
  8237. id result = [_dictionary objectForKey:@(key)];
  8238. return result;
  8239. }
  8240. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8241. if (otherDictionary) {
  8242. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8243. if (_autocreator) {
  8244. GPBAutocreatedDictionaryModified(_autocreator, self);
  8245. }
  8246. }
  8247. }
  8248. - (void)setObject:(id)object forKey:(int64_t)key {
  8249. if (!object) {
  8250. [NSException raise:NSInvalidArgumentException
  8251. format:@"Attempting to add nil object to a Dictionary"];
  8252. }
  8253. [_dictionary setObject:object forKey:@(key)];
  8254. if (_autocreator) {
  8255. GPBAutocreatedDictionaryModified(_autocreator, self);
  8256. }
  8257. }
  8258. - (void)removeObjectForKey:(int64_t)aKey {
  8259. [_dictionary removeObjectForKey:@(aKey)];
  8260. }
  8261. - (void)removeAll {
  8262. [_dictionary removeAllObjects];
  8263. }
  8264. @end
  8265. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8266. // This block of code is generated, do not edit it directly.
  8267. #pragma mark - String -> UInt32
  8268. @implementation GPBStringUInt32Dictionary {
  8269. @package
  8270. NSMutableDictionary *_dictionary;
  8271. }
  8272. + (instancetype)dictionary {
  8273. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8274. }
  8275. + (instancetype)dictionaryWithValue:(uint32_t)value
  8276. forKey:(NSString *)key {
  8277. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8278. // on to get the type correct.
  8279. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  8280. forKeys:&key
  8281. count:1] autorelease];
  8282. }
  8283. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  8284. forKeys:(const NSString * [])keys
  8285. count:(NSUInteger)count {
  8286. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8287. // on to get the type correct.
  8288. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  8289. forKeys:keys
  8290. count:count] autorelease];
  8291. }
  8292. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8293. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8294. // on to get the type correct.
  8295. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8296. }
  8297. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8298. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8299. }
  8300. - (instancetype)init {
  8301. return [self initWithValues:NULL forKeys:NULL count:0];
  8302. }
  8303. - (instancetype)initWithValues:(const uint32_t [])values
  8304. forKeys:(const NSString * [])keys
  8305. count:(NSUInteger)count {
  8306. self = [super init];
  8307. if (self) {
  8308. _dictionary = [[NSMutableDictionary alloc] init];
  8309. if (count && values && keys) {
  8310. for (NSUInteger i = 0; i < count; ++i) {
  8311. if (!keys[i]) {
  8312. [NSException raise:NSInvalidArgumentException
  8313. format:@"Attempting to add nil key to a Dictionary"];
  8314. }
  8315. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8316. }
  8317. }
  8318. }
  8319. return self;
  8320. }
  8321. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)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 [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8343. }
  8344. - (BOOL)isEqual:(id)other {
  8345. if (self == other) {
  8346. return YES;
  8347. }
  8348. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8349. return NO;
  8350. }
  8351. GPBStringUInt32Dictionary *otherDictionary = other;
  8352. return [_dictionary isEqual:otherDictionary->_dictionary];
  8353. }
  8354. - (NSUInteger)hash {
  8355. return _dictionary.count;
  8356. }
  8357. - (NSString *)description {
  8358. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8359. }
  8360. - (NSUInteger)count {
  8361. return _dictionary.count;
  8362. }
  8363. - (void)enumerateKeysAndValuesUsingBlock:
  8364. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8365. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8366. NSNumber *aValue,
  8367. BOOL *stop) {
  8368. block(aKey, [aValue unsignedIntValue], stop);
  8369. }];
  8370. }
  8371. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8372. NSUInteger count = _dictionary.count;
  8373. if (count == 0) {
  8374. return 0;
  8375. }
  8376. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8377. GPBDataType keyDataType = field.mapKeyDataType;
  8378. __block size_t result = 0;
  8379. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8380. NSNumber *aValue,
  8381. BOOL *stop) {
  8382. #pragma unused(stop)
  8383. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8384. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8385. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8386. }];
  8387. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8388. result += tagSize * count;
  8389. return result;
  8390. }
  8391. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8392. asField:(GPBFieldDescriptor *)field {
  8393. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8394. GPBDataType keyDataType = field.mapKeyDataType;
  8395. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8396. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8397. NSNumber *aValue,
  8398. BOOL *stop) {
  8399. #pragma unused(stop)
  8400. // Write the tag.
  8401. [outputStream writeInt32NoTag:tag];
  8402. // Write the size of the message.
  8403. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8404. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8405. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8406. // Write the fields.
  8407. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8408. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8409. }];
  8410. }
  8411. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8412. forGPBGenericValueKey:(GPBGenericValue *)key {
  8413. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8414. }
  8415. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8416. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8417. #pragma unused(stop)
  8418. block(key, [NSString stringWithFormat:@"%u", value]);
  8419. }];
  8420. }
  8421. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  8422. NSNumber *wrapped = [_dictionary objectForKey:key];
  8423. if (wrapped && value) {
  8424. *value = [wrapped unsignedIntValue];
  8425. }
  8426. return (wrapped != NULL);
  8427. }
  8428. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8429. if (otherDictionary) {
  8430. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8431. if (_autocreator) {
  8432. GPBAutocreatedDictionaryModified(_autocreator, self);
  8433. }
  8434. }
  8435. }
  8436. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  8437. if (!key) {
  8438. [NSException raise:NSInvalidArgumentException
  8439. format:@"Attempting to add nil key to a Dictionary"];
  8440. }
  8441. [_dictionary setObject:@(value) forKey:key];
  8442. if (_autocreator) {
  8443. GPBAutocreatedDictionaryModified(_autocreator, self);
  8444. }
  8445. }
  8446. - (void)removeValueForKey:(NSString *)aKey {
  8447. [_dictionary removeObjectForKey:aKey];
  8448. }
  8449. - (void)removeAll {
  8450. [_dictionary removeAllObjects];
  8451. }
  8452. @end
  8453. #pragma mark - String -> Int32
  8454. @implementation GPBStringInt32Dictionary {
  8455. @package
  8456. NSMutableDictionary *_dictionary;
  8457. }
  8458. + (instancetype)dictionary {
  8459. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8460. }
  8461. + (instancetype)dictionaryWithValue:(int32_t)value
  8462. forKey:(NSString *)key {
  8463. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8464. // on to get the type correct.
  8465. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  8466. forKeys:&key
  8467. count:1] autorelease];
  8468. }
  8469. + (instancetype)dictionaryWithValues:(const int32_t [])values
  8470. forKeys:(const NSString * [])keys
  8471. count:(NSUInteger)count {
  8472. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8473. // on to get the type correct.
  8474. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  8475. forKeys:keys
  8476. count:count] autorelease];
  8477. }
  8478. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8479. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8480. // on to get the type correct.
  8481. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8482. }
  8483. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8484. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8485. }
  8486. - (instancetype)init {
  8487. return [self initWithValues:NULL forKeys:NULL count:0];
  8488. }
  8489. - (instancetype)initWithValues:(const int32_t [])values
  8490. forKeys:(const NSString * [])keys
  8491. count:(NSUInteger)count {
  8492. self = [super init];
  8493. if (self) {
  8494. _dictionary = [[NSMutableDictionary alloc] init];
  8495. if (count && values && keys) {
  8496. for (NSUInteger i = 0; i < count; ++i) {
  8497. if (!keys[i]) {
  8498. [NSException raise:NSInvalidArgumentException
  8499. format:@"Attempting to add nil key to a Dictionary"];
  8500. }
  8501. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8502. }
  8503. }
  8504. }
  8505. return self;
  8506. }
  8507. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8508. self = [self initWithValues:NULL forKeys:NULL count:0];
  8509. if (self) {
  8510. if (dictionary) {
  8511. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8512. }
  8513. }
  8514. return self;
  8515. }
  8516. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8517. #pragma unused(numItems)
  8518. return [self initWithValues:NULL forKeys:NULL count:0];
  8519. }
  8520. - (void)dealloc {
  8521. NSAssert(!_autocreator,
  8522. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8523. [self class], _autocreator);
  8524. [_dictionary release];
  8525. [super dealloc];
  8526. }
  8527. - (instancetype)copyWithZone:(NSZone *)zone {
  8528. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8529. }
  8530. - (BOOL)isEqual:(id)other {
  8531. if (self == other) {
  8532. return YES;
  8533. }
  8534. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8535. return NO;
  8536. }
  8537. GPBStringInt32Dictionary *otherDictionary = other;
  8538. return [_dictionary isEqual:otherDictionary->_dictionary];
  8539. }
  8540. - (NSUInteger)hash {
  8541. return _dictionary.count;
  8542. }
  8543. - (NSString *)description {
  8544. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8545. }
  8546. - (NSUInteger)count {
  8547. return _dictionary.count;
  8548. }
  8549. - (void)enumerateKeysAndValuesUsingBlock:
  8550. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8551. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8552. NSNumber *aValue,
  8553. BOOL *stop) {
  8554. block(aKey, [aValue intValue], stop);
  8555. }];
  8556. }
  8557. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8558. NSUInteger count = _dictionary.count;
  8559. if (count == 0) {
  8560. return 0;
  8561. }
  8562. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8563. GPBDataType keyDataType = field.mapKeyDataType;
  8564. __block size_t result = 0;
  8565. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8566. NSNumber *aValue,
  8567. BOOL *stop) {
  8568. #pragma unused(stop)
  8569. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8570. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8571. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8572. }];
  8573. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8574. result += tagSize * count;
  8575. return result;
  8576. }
  8577. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8578. asField:(GPBFieldDescriptor *)field {
  8579. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8580. GPBDataType keyDataType = field.mapKeyDataType;
  8581. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8582. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8583. NSNumber *aValue,
  8584. BOOL *stop) {
  8585. #pragma unused(stop)
  8586. // Write the tag.
  8587. [outputStream writeInt32NoTag:tag];
  8588. // Write the size of the message.
  8589. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8590. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8591. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8592. // Write the fields.
  8593. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8594. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8595. }];
  8596. }
  8597. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8598. forGPBGenericValueKey:(GPBGenericValue *)key {
  8599. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8600. }
  8601. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8602. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8603. #pragma unused(stop)
  8604. block(key, [NSString stringWithFormat:@"%d", value]);
  8605. }];
  8606. }
  8607. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8608. NSNumber *wrapped = [_dictionary objectForKey:key];
  8609. if (wrapped && value) {
  8610. *value = [wrapped intValue];
  8611. }
  8612. return (wrapped != NULL);
  8613. }
  8614. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8615. if (otherDictionary) {
  8616. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8617. if (_autocreator) {
  8618. GPBAutocreatedDictionaryModified(_autocreator, self);
  8619. }
  8620. }
  8621. }
  8622. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8623. if (!key) {
  8624. [NSException raise:NSInvalidArgumentException
  8625. format:@"Attempting to add nil key to a Dictionary"];
  8626. }
  8627. [_dictionary setObject:@(value) forKey:key];
  8628. if (_autocreator) {
  8629. GPBAutocreatedDictionaryModified(_autocreator, self);
  8630. }
  8631. }
  8632. - (void)removeValueForKey:(NSString *)aKey {
  8633. [_dictionary removeObjectForKey:aKey];
  8634. }
  8635. - (void)removeAll {
  8636. [_dictionary removeAllObjects];
  8637. }
  8638. @end
  8639. #pragma mark - String -> UInt64
  8640. @implementation GPBStringUInt64Dictionary {
  8641. @package
  8642. NSMutableDictionary *_dictionary;
  8643. }
  8644. + (instancetype)dictionary {
  8645. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8646. }
  8647. + (instancetype)dictionaryWithValue:(uint64_t)value
  8648. forKey:(NSString *)key {
  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 [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8652. forKeys:&key
  8653. count:1] autorelease];
  8654. }
  8655. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8656. forKeys:(const NSString * [])keys
  8657. count:(NSUInteger)count {
  8658. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8659. // on to get the type correct.
  8660. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8661. forKeys:keys
  8662. count:count] autorelease];
  8663. }
  8664. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8665. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8666. // on to get the type correct.
  8667. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8668. }
  8669. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8670. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8671. }
  8672. - (instancetype)init {
  8673. return [self initWithValues:NULL forKeys:NULL count:0];
  8674. }
  8675. - (instancetype)initWithValues:(const uint64_t [])values
  8676. forKeys:(const NSString * [])keys
  8677. count:(NSUInteger)count {
  8678. self = [super init];
  8679. if (self) {
  8680. _dictionary = [[NSMutableDictionary alloc] init];
  8681. if (count && values && keys) {
  8682. for (NSUInteger i = 0; i < count; ++i) {
  8683. if (!keys[i]) {
  8684. [NSException raise:NSInvalidArgumentException
  8685. format:@"Attempting to add nil key to a Dictionary"];
  8686. }
  8687. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8688. }
  8689. }
  8690. }
  8691. return self;
  8692. }
  8693. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8694. self = [self initWithValues:NULL forKeys:NULL count:0];
  8695. if (self) {
  8696. if (dictionary) {
  8697. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8698. }
  8699. }
  8700. return self;
  8701. }
  8702. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8703. #pragma unused(numItems)
  8704. return [self initWithValues:NULL forKeys:NULL count:0];
  8705. }
  8706. - (void)dealloc {
  8707. NSAssert(!_autocreator,
  8708. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8709. [self class], _autocreator);
  8710. [_dictionary release];
  8711. [super dealloc];
  8712. }
  8713. - (instancetype)copyWithZone:(NSZone *)zone {
  8714. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8715. }
  8716. - (BOOL)isEqual:(id)other {
  8717. if (self == other) {
  8718. return YES;
  8719. }
  8720. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8721. return NO;
  8722. }
  8723. GPBStringUInt64Dictionary *otherDictionary = other;
  8724. return [_dictionary isEqual:otherDictionary->_dictionary];
  8725. }
  8726. - (NSUInteger)hash {
  8727. return _dictionary.count;
  8728. }
  8729. - (NSString *)description {
  8730. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8731. }
  8732. - (NSUInteger)count {
  8733. return _dictionary.count;
  8734. }
  8735. - (void)enumerateKeysAndValuesUsingBlock:
  8736. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8737. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8738. NSNumber *aValue,
  8739. BOOL *stop) {
  8740. block(aKey, [aValue unsignedLongLongValue], stop);
  8741. }];
  8742. }
  8743. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8744. NSUInteger count = _dictionary.count;
  8745. if (count == 0) {
  8746. return 0;
  8747. }
  8748. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8749. GPBDataType keyDataType = field.mapKeyDataType;
  8750. __block size_t result = 0;
  8751. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8752. NSNumber *aValue,
  8753. BOOL *stop) {
  8754. #pragma unused(stop)
  8755. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8756. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8757. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8758. }];
  8759. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8760. result += tagSize * count;
  8761. return result;
  8762. }
  8763. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8764. asField:(GPBFieldDescriptor *)field {
  8765. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8766. GPBDataType keyDataType = field.mapKeyDataType;
  8767. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8768. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8769. NSNumber *aValue,
  8770. BOOL *stop) {
  8771. #pragma unused(stop)
  8772. // Write the tag.
  8773. [outputStream writeInt32NoTag:tag];
  8774. // Write the size of the message.
  8775. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8776. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8777. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8778. // Write the fields.
  8779. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8780. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8781. }];
  8782. }
  8783. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8784. forGPBGenericValueKey:(GPBGenericValue *)key {
  8785. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8786. }
  8787. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8788. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8789. #pragma unused(stop)
  8790. block(key, [NSString stringWithFormat:@"%llu", value]);
  8791. }];
  8792. }
  8793. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8794. NSNumber *wrapped = [_dictionary objectForKey:key];
  8795. if (wrapped && value) {
  8796. *value = [wrapped unsignedLongLongValue];
  8797. }
  8798. return (wrapped != NULL);
  8799. }
  8800. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8801. if (otherDictionary) {
  8802. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8803. if (_autocreator) {
  8804. GPBAutocreatedDictionaryModified(_autocreator, self);
  8805. }
  8806. }
  8807. }
  8808. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8809. if (!key) {
  8810. [NSException raise:NSInvalidArgumentException
  8811. format:@"Attempting to add nil key to a Dictionary"];
  8812. }
  8813. [_dictionary setObject:@(value) forKey:key];
  8814. if (_autocreator) {
  8815. GPBAutocreatedDictionaryModified(_autocreator, self);
  8816. }
  8817. }
  8818. - (void)removeValueForKey:(NSString *)aKey {
  8819. [_dictionary removeObjectForKey:aKey];
  8820. }
  8821. - (void)removeAll {
  8822. [_dictionary removeAllObjects];
  8823. }
  8824. @end
  8825. #pragma mark - String -> Int64
  8826. @implementation GPBStringInt64Dictionary {
  8827. @package
  8828. NSMutableDictionary *_dictionary;
  8829. }
  8830. + (instancetype)dictionary {
  8831. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8832. }
  8833. + (instancetype)dictionaryWithValue:(int64_t)value
  8834. forKey:(NSString *)key {
  8835. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8836. // on to get the type correct.
  8837. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8838. forKeys:&key
  8839. count:1] autorelease];
  8840. }
  8841. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8842. forKeys:(const NSString * [])keys
  8843. count:(NSUInteger)count {
  8844. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8845. // on to get the type correct.
  8846. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8847. forKeys:keys
  8848. count:count] autorelease];
  8849. }
  8850. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8851. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8852. // on to get the type correct.
  8853. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8854. }
  8855. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8856. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8857. }
  8858. - (instancetype)init {
  8859. return [self initWithValues:NULL forKeys:NULL count:0];
  8860. }
  8861. - (instancetype)initWithValues:(const int64_t [])values
  8862. forKeys:(const NSString * [])keys
  8863. count:(NSUInteger)count {
  8864. self = [super init];
  8865. if (self) {
  8866. _dictionary = [[NSMutableDictionary alloc] init];
  8867. if (count && values && keys) {
  8868. for (NSUInteger i = 0; i < count; ++i) {
  8869. if (!keys[i]) {
  8870. [NSException raise:NSInvalidArgumentException
  8871. format:@"Attempting to add nil key to a Dictionary"];
  8872. }
  8873. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8874. }
  8875. }
  8876. }
  8877. return self;
  8878. }
  8879. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8880. self = [self initWithValues:NULL forKeys:NULL count:0];
  8881. if (self) {
  8882. if (dictionary) {
  8883. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8884. }
  8885. }
  8886. return self;
  8887. }
  8888. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8889. #pragma unused(numItems)
  8890. return [self initWithValues:NULL forKeys:NULL count:0];
  8891. }
  8892. - (void)dealloc {
  8893. NSAssert(!_autocreator,
  8894. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8895. [self class], _autocreator);
  8896. [_dictionary release];
  8897. [super dealloc];
  8898. }
  8899. - (instancetype)copyWithZone:(NSZone *)zone {
  8900. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8901. }
  8902. - (BOOL)isEqual:(id)other {
  8903. if (self == other) {
  8904. return YES;
  8905. }
  8906. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8907. return NO;
  8908. }
  8909. GPBStringInt64Dictionary *otherDictionary = other;
  8910. return [_dictionary isEqual:otherDictionary->_dictionary];
  8911. }
  8912. - (NSUInteger)hash {
  8913. return _dictionary.count;
  8914. }
  8915. - (NSString *)description {
  8916. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8917. }
  8918. - (NSUInteger)count {
  8919. return _dictionary.count;
  8920. }
  8921. - (void)enumerateKeysAndValuesUsingBlock:
  8922. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8923. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8924. NSNumber *aValue,
  8925. BOOL *stop) {
  8926. block(aKey, [aValue longLongValue], stop);
  8927. }];
  8928. }
  8929. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8930. NSUInteger count = _dictionary.count;
  8931. if (count == 0) {
  8932. return 0;
  8933. }
  8934. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8935. GPBDataType keyDataType = field.mapKeyDataType;
  8936. __block size_t result = 0;
  8937. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8938. NSNumber *aValue,
  8939. BOOL *stop) {
  8940. #pragma unused(stop)
  8941. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8942. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8943. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8944. }];
  8945. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8946. result += tagSize * count;
  8947. return result;
  8948. }
  8949. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8950. asField:(GPBFieldDescriptor *)field {
  8951. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8952. GPBDataType keyDataType = field.mapKeyDataType;
  8953. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8954. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8955. NSNumber *aValue,
  8956. BOOL *stop) {
  8957. #pragma unused(stop)
  8958. // Write the tag.
  8959. [outputStream writeInt32NoTag:tag];
  8960. // Write the size of the message.
  8961. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8962. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8963. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8964. // Write the fields.
  8965. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8966. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8967. }];
  8968. }
  8969. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8970. forGPBGenericValueKey:(GPBGenericValue *)key {
  8971. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8972. }
  8973. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8974. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8975. #pragma unused(stop)
  8976. block(key, [NSString stringWithFormat:@"%lld", value]);
  8977. }];
  8978. }
  8979. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8980. NSNumber *wrapped = [_dictionary objectForKey:key];
  8981. if (wrapped && value) {
  8982. *value = [wrapped longLongValue];
  8983. }
  8984. return (wrapped != NULL);
  8985. }
  8986. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8987. if (otherDictionary) {
  8988. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8989. if (_autocreator) {
  8990. GPBAutocreatedDictionaryModified(_autocreator, self);
  8991. }
  8992. }
  8993. }
  8994. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8995. if (!key) {
  8996. [NSException raise:NSInvalidArgumentException
  8997. format:@"Attempting to add nil key to a Dictionary"];
  8998. }
  8999. [_dictionary setObject:@(value) forKey:key];
  9000. if (_autocreator) {
  9001. GPBAutocreatedDictionaryModified(_autocreator, self);
  9002. }
  9003. }
  9004. - (void)removeValueForKey:(NSString *)aKey {
  9005. [_dictionary removeObjectForKey:aKey];
  9006. }
  9007. - (void)removeAll {
  9008. [_dictionary removeAllObjects];
  9009. }
  9010. @end
  9011. #pragma mark - String -> Bool
  9012. @implementation GPBStringBoolDictionary {
  9013. @package
  9014. NSMutableDictionary *_dictionary;
  9015. }
  9016. + (instancetype)dictionary {
  9017. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9018. }
  9019. + (instancetype)dictionaryWithValue:(BOOL)value
  9020. forKey:(NSString *)key {
  9021. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9022. // on to get the type correct.
  9023. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  9024. forKeys:&key
  9025. count:1] autorelease];
  9026. }
  9027. + (instancetype)dictionaryWithValues:(const BOOL [])values
  9028. forKeys:(const NSString * [])keys
  9029. count:(NSUInteger)count {
  9030. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9031. // on to get the type correct.
  9032. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  9033. forKeys:keys
  9034. count:count] autorelease];
  9035. }
  9036. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9037. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9038. // on to get the type correct.
  9039. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9040. }
  9041. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9042. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9043. }
  9044. - (instancetype)init {
  9045. return [self initWithValues:NULL forKeys:NULL count:0];
  9046. }
  9047. - (instancetype)initWithValues:(const BOOL [])values
  9048. forKeys:(const NSString * [])keys
  9049. count:(NSUInteger)count {
  9050. self = [super init];
  9051. if (self) {
  9052. _dictionary = [[NSMutableDictionary alloc] init];
  9053. if (count && values && keys) {
  9054. for (NSUInteger i = 0; i < count; ++i) {
  9055. if (!keys[i]) {
  9056. [NSException raise:NSInvalidArgumentException
  9057. format:@"Attempting to add nil key to a Dictionary"];
  9058. }
  9059. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9060. }
  9061. }
  9062. }
  9063. return self;
  9064. }
  9065. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9066. self = [self initWithValues:NULL forKeys:NULL count:0];
  9067. if (self) {
  9068. if (dictionary) {
  9069. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9070. }
  9071. }
  9072. return self;
  9073. }
  9074. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9075. #pragma unused(numItems)
  9076. return [self initWithValues:NULL forKeys:NULL count:0];
  9077. }
  9078. - (void)dealloc {
  9079. NSAssert(!_autocreator,
  9080. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9081. [self class], _autocreator);
  9082. [_dictionary release];
  9083. [super dealloc];
  9084. }
  9085. - (instancetype)copyWithZone:(NSZone *)zone {
  9086. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9087. }
  9088. - (BOOL)isEqual:(id)other {
  9089. if (self == other) {
  9090. return YES;
  9091. }
  9092. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  9093. return NO;
  9094. }
  9095. GPBStringBoolDictionary *otherDictionary = other;
  9096. return [_dictionary isEqual:otherDictionary->_dictionary];
  9097. }
  9098. - (NSUInteger)hash {
  9099. return _dictionary.count;
  9100. }
  9101. - (NSString *)description {
  9102. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9103. }
  9104. - (NSUInteger)count {
  9105. return _dictionary.count;
  9106. }
  9107. - (void)enumerateKeysAndValuesUsingBlock:
  9108. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  9109. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9110. NSNumber *aValue,
  9111. BOOL *stop) {
  9112. block(aKey, [aValue boolValue], stop);
  9113. }];
  9114. }
  9115. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9116. NSUInteger count = _dictionary.count;
  9117. if (count == 0) {
  9118. return 0;
  9119. }
  9120. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9121. GPBDataType keyDataType = field.mapKeyDataType;
  9122. __block size_t result = 0;
  9123. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9124. NSNumber *aValue,
  9125. BOOL *stop) {
  9126. #pragma unused(stop)
  9127. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9128. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9129. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9130. }];
  9131. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9132. result += tagSize * count;
  9133. return result;
  9134. }
  9135. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9136. asField:(GPBFieldDescriptor *)field {
  9137. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9138. GPBDataType keyDataType = field.mapKeyDataType;
  9139. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9140. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9141. NSNumber *aValue,
  9142. BOOL *stop) {
  9143. #pragma unused(stop)
  9144. // Write the tag.
  9145. [outputStream writeInt32NoTag:tag];
  9146. // Write the size of the message.
  9147. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9148. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9149. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9150. // Write the fields.
  9151. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9152. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  9153. }];
  9154. }
  9155. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9156. forGPBGenericValueKey:(GPBGenericValue *)key {
  9157. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  9158. }
  9159. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9160. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  9161. #pragma unused(stop)
  9162. block(key, (value ? @"true" : @"false"));
  9163. }];
  9164. }
  9165. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  9166. NSNumber *wrapped = [_dictionary objectForKey:key];
  9167. if (wrapped && value) {
  9168. *value = [wrapped boolValue];
  9169. }
  9170. return (wrapped != NULL);
  9171. }
  9172. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9173. if (otherDictionary) {
  9174. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9175. if (_autocreator) {
  9176. GPBAutocreatedDictionaryModified(_autocreator, self);
  9177. }
  9178. }
  9179. }
  9180. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  9181. if (!key) {
  9182. [NSException raise:NSInvalidArgumentException
  9183. format:@"Attempting to add nil key to a Dictionary"];
  9184. }
  9185. [_dictionary setObject:@(value) forKey:key];
  9186. if (_autocreator) {
  9187. GPBAutocreatedDictionaryModified(_autocreator, self);
  9188. }
  9189. }
  9190. - (void)removeValueForKey:(NSString *)aKey {
  9191. [_dictionary removeObjectForKey:aKey];
  9192. }
  9193. - (void)removeAll {
  9194. [_dictionary removeAllObjects];
  9195. }
  9196. @end
  9197. #pragma mark - String -> Float
  9198. @implementation GPBStringFloatDictionary {
  9199. @package
  9200. NSMutableDictionary *_dictionary;
  9201. }
  9202. + (instancetype)dictionary {
  9203. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9204. }
  9205. + (instancetype)dictionaryWithValue:(float)value
  9206. forKey:(NSString *)key {
  9207. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9208. // on to get the type correct.
  9209. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  9210. forKeys:&key
  9211. count:1] autorelease];
  9212. }
  9213. + (instancetype)dictionaryWithValues:(const float [])values
  9214. forKeys:(const NSString * [])keys
  9215. count:(NSUInteger)count {
  9216. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9217. // on to get the type correct.
  9218. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  9219. forKeys:keys
  9220. count:count] autorelease];
  9221. }
  9222. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9223. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9224. // on to get the type correct.
  9225. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9226. }
  9227. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9228. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9229. }
  9230. - (instancetype)init {
  9231. return [self initWithValues:NULL forKeys:NULL count:0];
  9232. }
  9233. - (instancetype)initWithValues:(const float [])values
  9234. forKeys:(const NSString * [])keys
  9235. count:(NSUInteger)count {
  9236. self = [super init];
  9237. if (self) {
  9238. _dictionary = [[NSMutableDictionary alloc] init];
  9239. if (count && values && keys) {
  9240. for (NSUInteger i = 0; i < count; ++i) {
  9241. if (!keys[i]) {
  9242. [NSException raise:NSInvalidArgumentException
  9243. format:@"Attempting to add nil key to a Dictionary"];
  9244. }
  9245. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9246. }
  9247. }
  9248. }
  9249. return self;
  9250. }
  9251. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9252. self = [self initWithValues:NULL forKeys:NULL count:0];
  9253. if (self) {
  9254. if (dictionary) {
  9255. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9256. }
  9257. }
  9258. return self;
  9259. }
  9260. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9261. #pragma unused(numItems)
  9262. return [self initWithValues:NULL forKeys:NULL count:0];
  9263. }
  9264. - (void)dealloc {
  9265. NSAssert(!_autocreator,
  9266. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9267. [self class], _autocreator);
  9268. [_dictionary release];
  9269. [super dealloc];
  9270. }
  9271. - (instancetype)copyWithZone:(NSZone *)zone {
  9272. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9273. }
  9274. - (BOOL)isEqual:(id)other {
  9275. if (self == other) {
  9276. return YES;
  9277. }
  9278. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9279. return NO;
  9280. }
  9281. GPBStringFloatDictionary *otherDictionary = other;
  9282. return [_dictionary isEqual:otherDictionary->_dictionary];
  9283. }
  9284. - (NSUInteger)hash {
  9285. return _dictionary.count;
  9286. }
  9287. - (NSString *)description {
  9288. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9289. }
  9290. - (NSUInteger)count {
  9291. return _dictionary.count;
  9292. }
  9293. - (void)enumerateKeysAndValuesUsingBlock:
  9294. (void (^)(NSString *key, float value, BOOL *stop))block {
  9295. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9296. NSNumber *aValue,
  9297. BOOL *stop) {
  9298. block(aKey, [aValue floatValue], stop);
  9299. }];
  9300. }
  9301. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9302. NSUInteger count = _dictionary.count;
  9303. if (count == 0) {
  9304. return 0;
  9305. }
  9306. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9307. GPBDataType keyDataType = field.mapKeyDataType;
  9308. __block size_t result = 0;
  9309. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9310. NSNumber *aValue,
  9311. BOOL *stop) {
  9312. #pragma unused(stop)
  9313. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9314. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9315. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9316. }];
  9317. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9318. result += tagSize * count;
  9319. return result;
  9320. }
  9321. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9322. asField:(GPBFieldDescriptor *)field {
  9323. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9324. GPBDataType keyDataType = field.mapKeyDataType;
  9325. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9326. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9327. NSNumber *aValue,
  9328. BOOL *stop) {
  9329. #pragma unused(stop)
  9330. // Write the tag.
  9331. [outputStream writeInt32NoTag:tag];
  9332. // Write the size of the message.
  9333. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9334. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9335. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9336. // Write the fields.
  9337. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9338. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9339. }];
  9340. }
  9341. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9342. forGPBGenericValueKey:(GPBGenericValue *)key {
  9343. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9344. }
  9345. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9346. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9347. #pragma unused(stop)
  9348. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9349. }];
  9350. }
  9351. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  9352. NSNumber *wrapped = [_dictionary objectForKey:key];
  9353. if (wrapped && value) {
  9354. *value = [wrapped floatValue];
  9355. }
  9356. return (wrapped != NULL);
  9357. }
  9358. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9359. if (otherDictionary) {
  9360. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9361. if (_autocreator) {
  9362. GPBAutocreatedDictionaryModified(_autocreator, self);
  9363. }
  9364. }
  9365. }
  9366. - (void)setValue:(float)value forKey:(NSString *)key {
  9367. if (!key) {
  9368. [NSException raise:NSInvalidArgumentException
  9369. format:@"Attempting to add nil key to a Dictionary"];
  9370. }
  9371. [_dictionary setObject:@(value) forKey:key];
  9372. if (_autocreator) {
  9373. GPBAutocreatedDictionaryModified(_autocreator, self);
  9374. }
  9375. }
  9376. - (void)removeValueForKey:(NSString *)aKey {
  9377. [_dictionary removeObjectForKey:aKey];
  9378. }
  9379. - (void)removeAll {
  9380. [_dictionary removeAllObjects];
  9381. }
  9382. @end
  9383. #pragma mark - String -> Double
  9384. @implementation GPBStringDoubleDictionary {
  9385. @package
  9386. NSMutableDictionary *_dictionary;
  9387. }
  9388. + (instancetype)dictionary {
  9389. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9390. }
  9391. + (instancetype)dictionaryWithValue:(double)value
  9392. forKey:(NSString *)key {
  9393. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9394. // on to get the type correct.
  9395. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  9396. forKeys:&key
  9397. count:1] autorelease];
  9398. }
  9399. + (instancetype)dictionaryWithValues:(const double [])values
  9400. forKeys:(const NSString * [])keys
  9401. count:(NSUInteger)count {
  9402. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9403. // on to get the type correct.
  9404. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  9405. forKeys:keys
  9406. count:count] autorelease];
  9407. }
  9408. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9409. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9410. // on to get the type correct.
  9411. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9412. }
  9413. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9414. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9415. }
  9416. - (instancetype)init {
  9417. return [self initWithValues:NULL forKeys:NULL count:0];
  9418. }
  9419. - (instancetype)initWithValues:(const double [])values
  9420. forKeys:(const NSString * [])keys
  9421. count:(NSUInteger)count {
  9422. self = [super init];
  9423. if (self) {
  9424. _dictionary = [[NSMutableDictionary alloc] init];
  9425. if (count && values && keys) {
  9426. for (NSUInteger i = 0; i < count; ++i) {
  9427. if (!keys[i]) {
  9428. [NSException raise:NSInvalidArgumentException
  9429. format:@"Attempting to add nil key to a Dictionary"];
  9430. }
  9431. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9432. }
  9433. }
  9434. }
  9435. return self;
  9436. }
  9437. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9438. self = [self initWithValues:NULL forKeys:NULL count:0];
  9439. if (self) {
  9440. if (dictionary) {
  9441. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9442. }
  9443. }
  9444. return self;
  9445. }
  9446. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9447. #pragma unused(numItems)
  9448. return [self initWithValues:NULL forKeys:NULL count:0];
  9449. }
  9450. - (void)dealloc {
  9451. NSAssert(!_autocreator,
  9452. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9453. [self class], _autocreator);
  9454. [_dictionary release];
  9455. [super dealloc];
  9456. }
  9457. - (instancetype)copyWithZone:(NSZone *)zone {
  9458. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9459. }
  9460. - (BOOL)isEqual:(id)other {
  9461. if (self == other) {
  9462. return YES;
  9463. }
  9464. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9465. return NO;
  9466. }
  9467. GPBStringDoubleDictionary *otherDictionary = other;
  9468. return [_dictionary isEqual:otherDictionary->_dictionary];
  9469. }
  9470. - (NSUInteger)hash {
  9471. return _dictionary.count;
  9472. }
  9473. - (NSString *)description {
  9474. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9475. }
  9476. - (NSUInteger)count {
  9477. return _dictionary.count;
  9478. }
  9479. - (void)enumerateKeysAndValuesUsingBlock:
  9480. (void (^)(NSString *key, double value, BOOL *stop))block {
  9481. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9482. NSNumber *aValue,
  9483. BOOL *stop) {
  9484. block(aKey, [aValue doubleValue], stop);
  9485. }];
  9486. }
  9487. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9488. NSUInteger count = _dictionary.count;
  9489. if (count == 0) {
  9490. return 0;
  9491. }
  9492. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9493. GPBDataType keyDataType = field.mapKeyDataType;
  9494. __block size_t result = 0;
  9495. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9496. NSNumber *aValue,
  9497. BOOL *stop) {
  9498. #pragma unused(stop)
  9499. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9500. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9501. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9502. }];
  9503. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9504. result += tagSize * count;
  9505. return result;
  9506. }
  9507. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9508. asField:(GPBFieldDescriptor *)field {
  9509. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9510. GPBDataType keyDataType = field.mapKeyDataType;
  9511. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9512. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9513. NSNumber *aValue,
  9514. BOOL *stop) {
  9515. #pragma unused(stop)
  9516. // Write the tag.
  9517. [outputStream writeInt32NoTag:tag];
  9518. // Write the size of the message.
  9519. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9520. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9521. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9522. // Write the fields.
  9523. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9524. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9525. }];
  9526. }
  9527. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9528. forGPBGenericValueKey:(GPBGenericValue *)key {
  9529. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9530. }
  9531. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9532. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9533. #pragma unused(stop)
  9534. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9535. }];
  9536. }
  9537. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  9538. NSNumber *wrapped = [_dictionary objectForKey:key];
  9539. if (wrapped && value) {
  9540. *value = [wrapped doubleValue];
  9541. }
  9542. return (wrapped != NULL);
  9543. }
  9544. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9545. if (otherDictionary) {
  9546. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9547. if (_autocreator) {
  9548. GPBAutocreatedDictionaryModified(_autocreator, self);
  9549. }
  9550. }
  9551. }
  9552. - (void)setValue:(double)value forKey:(NSString *)key {
  9553. if (!key) {
  9554. [NSException raise:NSInvalidArgumentException
  9555. format:@"Attempting to add nil key to a Dictionary"];
  9556. }
  9557. [_dictionary setObject:@(value) forKey:key];
  9558. if (_autocreator) {
  9559. GPBAutocreatedDictionaryModified(_autocreator, self);
  9560. }
  9561. }
  9562. - (void)removeValueForKey:(NSString *)aKey {
  9563. [_dictionary removeObjectForKey:aKey];
  9564. }
  9565. - (void)removeAll {
  9566. [_dictionary removeAllObjects];
  9567. }
  9568. @end
  9569. #pragma mark - String -> Enum
  9570. @implementation GPBStringEnumDictionary {
  9571. @package
  9572. NSMutableDictionary *_dictionary;
  9573. GPBEnumValidationFunc _validationFunc;
  9574. }
  9575. @synthesize validationFunc = _validationFunc;
  9576. + (instancetype)dictionary {
  9577. return [[[self alloc] initWithValidationFunction:NULL
  9578. rawValues:NULL
  9579. forKeys:NULL
  9580. count:0] autorelease];
  9581. }
  9582. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9583. return [[[self alloc] initWithValidationFunction:func
  9584. rawValues:NULL
  9585. forKeys:NULL
  9586. count:0] autorelease];
  9587. }
  9588. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9589. rawValue:(int32_t)rawValue
  9590. forKey:(NSString *)key {
  9591. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9592. // on to get the type correct.
  9593. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9594. rawValues:&rawValue
  9595. forKeys:&key
  9596. count:1] autorelease];
  9597. }
  9598. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9599. rawValues:(const int32_t [])rawValues
  9600. forKeys:(const NSString * [])keys
  9601. count:(NSUInteger)count {
  9602. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9603. // on to get the type correct.
  9604. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9605. rawValues:rawValues
  9606. forKeys:keys
  9607. count:count] autorelease];
  9608. }
  9609. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9610. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9611. // on to get the type correct.
  9612. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9613. }
  9614. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9615. capacity:(NSUInteger)numItems {
  9616. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9617. }
  9618. - (instancetype)init {
  9619. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9620. }
  9621. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9622. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9623. }
  9624. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9625. rawValues:(const int32_t [])rawValues
  9626. forKeys:(const NSString * [])keys
  9627. count:(NSUInteger)count {
  9628. self = [super init];
  9629. if (self) {
  9630. _dictionary = [[NSMutableDictionary alloc] init];
  9631. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9632. if (count && rawValues && keys) {
  9633. for (NSUInteger i = 0; i < count; ++i) {
  9634. if (!keys[i]) {
  9635. [NSException raise:NSInvalidArgumentException
  9636. format:@"Attempting to add nil key to a Dictionary"];
  9637. }
  9638. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9639. }
  9640. }
  9641. }
  9642. return self;
  9643. }
  9644. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9645. self = [self initWithValidationFunction:dictionary.validationFunc
  9646. rawValues:NULL
  9647. forKeys:NULL
  9648. count:0];
  9649. if (self) {
  9650. if (dictionary) {
  9651. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9652. }
  9653. }
  9654. return self;
  9655. }
  9656. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9657. capacity:(NSUInteger)numItems {
  9658. #pragma unused(numItems)
  9659. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9660. }
  9661. - (void)dealloc {
  9662. NSAssert(!_autocreator,
  9663. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9664. [self class], _autocreator);
  9665. [_dictionary release];
  9666. [super dealloc];
  9667. }
  9668. - (instancetype)copyWithZone:(NSZone *)zone {
  9669. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9670. }
  9671. - (BOOL)isEqual:(id)other {
  9672. if (self == other) {
  9673. return YES;
  9674. }
  9675. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9676. return NO;
  9677. }
  9678. GPBStringEnumDictionary *otherDictionary = other;
  9679. return [_dictionary isEqual:otherDictionary->_dictionary];
  9680. }
  9681. - (NSUInteger)hash {
  9682. return _dictionary.count;
  9683. }
  9684. - (NSString *)description {
  9685. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9686. }
  9687. - (NSUInteger)count {
  9688. return _dictionary.count;
  9689. }
  9690. - (void)enumerateKeysAndRawValuesUsingBlock:
  9691. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9692. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9693. NSNumber *aValue,
  9694. BOOL *stop) {
  9695. block(aKey, [aValue intValue], stop);
  9696. }];
  9697. }
  9698. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9699. NSUInteger count = _dictionary.count;
  9700. if (count == 0) {
  9701. return 0;
  9702. }
  9703. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9704. GPBDataType keyDataType = field.mapKeyDataType;
  9705. __block size_t result = 0;
  9706. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9707. NSNumber *aValue,
  9708. BOOL *stop) {
  9709. #pragma unused(stop)
  9710. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9711. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9712. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9713. }];
  9714. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9715. result += tagSize * count;
  9716. return result;
  9717. }
  9718. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9719. asField:(GPBFieldDescriptor *)field {
  9720. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9721. GPBDataType keyDataType = field.mapKeyDataType;
  9722. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9723. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9724. NSNumber *aValue,
  9725. BOOL *stop) {
  9726. #pragma unused(stop)
  9727. // Write the tag.
  9728. [outputStream writeInt32NoTag:tag];
  9729. // Write the size of the message.
  9730. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9731. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9732. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9733. // Write the fields.
  9734. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9735. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9736. }];
  9737. }
  9738. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9739. forKey:(GPBGenericValue *)key
  9740. keyDataType:(GPBDataType)keyDataType {
  9741. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9742. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9743. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9744. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9745. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9746. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9747. [outputStream release];
  9748. return data;
  9749. }
  9750. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9751. forGPBGenericValueKey:(GPBGenericValue *)key {
  9752. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9753. }
  9754. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9755. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9756. #pragma unused(stop)
  9757. block(key, @(value));
  9758. }];
  9759. }
  9760. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9761. NSNumber *wrapped = [_dictionary objectForKey:key];
  9762. if (wrapped && value) {
  9763. int32_t result = [wrapped intValue];
  9764. if (!_validationFunc(result)) {
  9765. result = kGPBUnrecognizedEnumeratorValue;
  9766. }
  9767. *value = result;
  9768. }
  9769. return (wrapped != NULL);
  9770. }
  9771. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9772. NSNumber *wrapped = [_dictionary objectForKey:key];
  9773. if (wrapped && rawValue) {
  9774. *rawValue = [wrapped intValue];
  9775. }
  9776. return (wrapped != NULL);
  9777. }
  9778. - (void)enumerateKeysAndValuesUsingBlock:
  9779. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9780. GPBEnumValidationFunc func = _validationFunc;
  9781. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9782. NSNumber *aValue,
  9783. BOOL *stop) {
  9784. int32_t unwrapped = [aValue intValue];
  9785. if (!func(unwrapped)) {
  9786. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9787. }
  9788. block(aKey, unwrapped, stop);
  9789. }];
  9790. }
  9791. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9792. if (otherDictionary) {
  9793. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9794. if (_autocreator) {
  9795. GPBAutocreatedDictionaryModified(_autocreator, self);
  9796. }
  9797. }
  9798. }
  9799. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9800. if (!key) {
  9801. [NSException raise:NSInvalidArgumentException
  9802. format:@"Attempting to add nil key to a Dictionary"];
  9803. }
  9804. [_dictionary setObject:@(value) forKey:key];
  9805. if (_autocreator) {
  9806. GPBAutocreatedDictionaryModified(_autocreator, self);
  9807. }
  9808. }
  9809. - (void)removeValueForKey:(NSString *)aKey {
  9810. [_dictionary removeObjectForKey:aKey];
  9811. }
  9812. - (void)removeAll {
  9813. [_dictionary removeAllObjects];
  9814. }
  9815. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9816. if (!key) {
  9817. [NSException raise:NSInvalidArgumentException
  9818. format:@"Attempting to add nil key to a Dictionary"];
  9819. }
  9820. if (!_validationFunc(value)) {
  9821. [NSException raise:NSInvalidArgumentException
  9822. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9823. value];
  9824. }
  9825. [_dictionary setObject:@(value) forKey:key];
  9826. if (_autocreator) {
  9827. GPBAutocreatedDictionaryModified(_autocreator, self);
  9828. }
  9829. }
  9830. @end
  9831. //%PDDM-EXPAND-END (5 expansions)
  9832. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9833. // This block of code is generated, do not edit it directly.
  9834. #pragma mark - Bool -> UInt32
  9835. @implementation GPBBoolUInt32Dictionary {
  9836. @package
  9837. uint32_t _values[2];
  9838. BOOL _valueSet[2];
  9839. }
  9840. + (instancetype)dictionary {
  9841. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9842. }
  9843. + (instancetype)dictionaryWithValue:(uint32_t)value
  9844. forKey:(BOOL)key {
  9845. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9846. // on to get the type correct.
  9847. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9848. forKeys:&key
  9849. count:1] autorelease];
  9850. }
  9851. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9852. forKeys:(const BOOL [])keys
  9853. count:(NSUInteger)count {
  9854. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9855. // on to get the type correct.
  9856. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9857. forKeys:keys
  9858. count:count] autorelease];
  9859. }
  9860. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9861. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9862. // on to get the type correct.
  9863. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9864. }
  9865. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9866. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9867. }
  9868. - (instancetype)init {
  9869. return [self initWithValues:NULL forKeys:NULL count:0];
  9870. }
  9871. - (instancetype)initWithValues:(const uint32_t [])values
  9872. forKeys:(const BOOL [])keys
  9873. count:(NSUInteger)count {
  9874. self = [super init];
  9875. if (self) {
  9876. for (NSUInteger i = 0; i < count; ++i) {
  9877. int idx = keys[i] ? 1 : 0;
  9878. _values[idx] = values[i];
  9879. _valueSet[idx] = YES;
  9880. }
  9881. }
  9882. return self;
  9883. }
  9884. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9885. self = [self initWithValues:NULL forKeys:NULL count:0];
  9886. if (self) {
  9887. if (dictionary) {
  9888. for (int i = 0; i < 2; ++i) {
  9889. if (dictionary->_valueSet[i]) {
  9890. _values[i] = dictionary->_values[i];
  9891. _valueSet[i] = YES;
  9892. }
  9893. }
  9894. }
  9895. }
  9896. return self;
  9897. }
  9898. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9899. #pragma unused(numItems)
  9900. return [self initWithValues:NULL forKeys:NULL count:0];
  9901. }
  9902. #if !defined(NS_BLOCK_ASSERTIONS)
  9903. - (void)dealloc {
  9904. NSAssert(!_autocreator,
  9905. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9906. [self class], _autocreator);
  9907. [super dealloc];
  9908. }
  9909. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9910. - (instancetype)copyWithZone:(NSZone *)zone {
  9911. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9912. }
  9913. - (BOOL)isEqual:(id)other {
  9914. if (self == other) {
  9915. return YES;
  9916. }
  9917. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9918. return NO;
  9919. }
  9920. GPBBoolUInt32Dictionary *otherDictionary = other;
  9921. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9922. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9923. return NO;
  9924. }
  9925. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9926. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9927. return NO;
  9928. }
  9929. return YES;
  9930. }
  9931. - (NSUInteger)hash {
  9932. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9933. }
  9934. - (NSString *)description {
  9935. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9936. if (_valueSet[0]) {
  9937. [result appendFormat:@"NO: %u", _values[0]];
  9938. }
  9939. if (_valueSet[1]) {
  9940. [result appendFormat:@"YES: %u", _values[1]];
  9941. }
  9942. [result appendString:@" }"];
  9943. return result;
  9944. }
  9945. - (NSUInteger)count {
  9946. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9947. }
  9948. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9949. int idx = (key ? 1 : 0);
  9950. if (_valueSet[idx]) {
  9951. if (value) {
  9952. *value = _values[idx];
  9953. }
  9954. return YES;
  9955. }
  9956. return NO;
  9957. }
  9958. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9959. forGPBGenericValueKey:(GPBGenericValue *)key {
  9960. int idx = (key->valueBool ? 1 : 0);
  9961. _values[idx] = value->valueUInt32;
  9962. _valueSet[idx] = YES;
  9963. }
  9964. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9965. if (_valueSet[0]) {
  9966. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9967. }
  9968. if (_valueSet[1]) {
  9969. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9970. }
  9971. }
  9972. - (void)enumerateKeysAndValuesUsingBlock:
  9973. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9974. BOOL stop = NO;
  9975. if (_valueSet[0]) {
  9976. block(NO, _values[0], &stop);
  9977. }
  9978. if (!stop && _valueSet[1]) {
  9979. block(YES, _values[1], &stop);
  9980. }
  9981. }
  9982. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9983. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9984. NSUInteger count = 0;
  9985. size_t result = 0;
  9986. for (int i = 0; i < 2; ++i) {
  9987. if (_valueSet[i]) {
  9988. ++count;
  9989. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9990. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9991. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9992. }
  9993. }
  9994. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9995. result += tagSize * count;
  9996. return result;
  9997. }
  9998. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9999. asField:(GPBFieldDescriptor *)field {
  10000. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10001. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10002. for (int i = 0; i < 2; ++i) {
  10003. if (_valueSet[i]) {
  10004. // Write the tag.
  10005. [outputStream writeInt32NoTag:tag];
  10006. // Write the size of the message.
  10007. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10008. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10009. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10010. // Write the fields.
  10011. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10012. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10013. }
  10014. }
  10015. }
  10016. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  10017. if (otherDictionary) {
  10018. for (int i = 0; i < 2; ++i) {
  10019. if (otherDictionary->_valueSet[i]) {
  10020. _valueSet[i] = YES;
  10021. _values[i] = otherDictionary->_values[i];
  10022. }
  10023. }
  10024. if (_autocreator) {
  10025. GPBAutocreatedDictionaryModified(_autocreator, self);
  10026. }
  10027. }
  10028. }
  10029. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  10030. int idx = (key ? 1 : 0);
  10031. _values[idx] = value;
  10032. _valueSet[idx] = YES;
  10033. if (_autocreator) {
  10034. GPBAutocreatedDictionaryModified(_autocreator, self);
  10035. }
  10036. }
  10037. - (void)removeValueForKey:(BOOL)aKey {
  10038. _valueSet[aKey ? 1 : 0] = NO;
  10039. }
  10040. - (void)removeAll {
  10041. _valueSet[0] = NO;
  10042. _valueSet[1] = NO;
  10043. }
  10044. @end
  10045. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  10046. // This block of code is generated, do not edit it directly.
  10047. #pragma mark - Bool -> Int32
  10048. @implementation GPBBoolInt32Dictionary {
  10049. @package
  10050. int32_t _values[2];
  10051. BOOL _valueSet[2];
  10052. }
  10053. + (instancetype)dictionary {
  10054. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10055. }
  10056. + (instancetype)dictionaryWithValue:(int32_t)value
  10057. forKey:(BOOL)key {
  10058. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10059. // on to get the type correct.
  10060. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  10061. forKeys:&key
  10062. count:1] autorelease];
  10063. }
  10064. + (instancetype)dictionaryWithValues:(const int32_t [])values
  10065. forKeys:(const BOOL [])keys
  10066. count:(NSUInteger)count {
  10067. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10068. // on to get the type correct.
  10069. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  10070. forKeys:keys
  10071. count:count] autorelease];
  10072. }
  10073. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10074. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10075. // on to get the type correct.
  10076. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10077. }
  10078. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10079. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10080. }
  10081. - (instancetype)init {
  10082. return [self initWithValues:NULL forKeys:NULL count:0];
  10083. }
  10084. - (instancetype)initWithValues:(const int32_t [])values
  10085. forKeys:(const BOOL [])keys
  10086. count:(NSUInteger)count {
  10087. self = [super init];
  10088. if (self) {
  10089. for (NSUInteger i = 0; i < count; ++i) {
  10090. int idx = keys[i] ? 1 : 0;
  10091. _values[idx] = values[i];
  10092. _valueSet[idx] = YES;
  10093. }
  10094. }
  10095. return self;
  10096. }
  10097. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10098. self = [self initWithValues:NULL forKeys:NULL count:0];
  10099. if (self) {
  10100. if (dictionary) {
  10101. for (int i = 0; i < 2; ++i) {
  10102. if (dictionary->_valueSet[i]) {
  10103. _values[i] = dictionary->_values[i];
  10104. _valueSet[i] = YES;
  10105. }
  10106. }
  10107. }
  10108. }
  10109. return self;
  10110. }
  10111. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10112. #pragma unused(numItems)
  10113. return [self initWithValues:NULL forKeys:NULL count:0];
  10114. }
  10115. #if !defined(NS_BLOCK_ASSERTIONS)
  10116. - (void)dealloc {
  10117. NSAssert(!_autocreator,
  10118. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10119. [self class], _autocreator);
  10120. [super dealloc];
  10121. }
  10122. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10123. - (instancetype)copyWithZone:(NSZone *)zone {
  10124. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10125. }
  10126. - (BOOL)isEqual:(id)other {
  10127. if (self == other) {
  10128. return YES;
  10129. }
  10130. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  10131. return NO;
  10132. }
  10133. GPBBoolInt32Dictionary *otherDictionary = other;
  10134. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10135. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10136. return NO;
  10137. }
  10138. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10139. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10140. return NO;
  10141. }
  10142. return YES;
  10143. }
  10144. - (NSUInteger)hash {
  10145. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10146. }
  10147. - (NSString *)description {
  10148. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10149. if (_valueSet[0]) {
  10150. [result appendFormat:@"NO: %d", _values[0]];
  10151. }
  10152. if (_valueSet[1]) {
  10153. [result appendFormat:@"YES: %d", _values[1]];
  10154. }
  10155. [result appendString:@" }"];
  10156. return result;
  10157. }
  10158. - (NSUInteger)count {
  10159. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10160. }
  10161. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  10162. int idx = (key ? 1 : 0);
  10163. if (_valueSet[idx]) {
  10164. if (value) {
  10165. *value = _values[idx];
  10166. }
  10167. return YES;
  10168. }
  10169. return NO;
  10170. }
  10171. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10172. forGPBGenericValueKey:(GPBGenericValue *)key {
  10173. int idx = (key->valueBool ? 1 : 0);
  10174. _values[idx] = value->valueInt32;
  10175. _valueSet[idx] = YES;
  10176. }
  10177. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10178. if (_valueSet[0]) {
  10179. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  10180. }
  10181. if (_valueSet[1]) {
  10182. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  10183. }
  10184. }
  10185. - (void)enumerateKeysAndValuesUsingBlock:
  10186. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10187. BOOL stop = NO;
  10188. if (_valueSet[0]) {
  10189. block(NO, _values[0], &stop);
  10190. }
  10191. if (!stop && _valueSet[1]) {
  10192. block(YES, _values[1], &stop);
  10193. }
  10194. }
  10195. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10196. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10197. NSUInteger count = 0;
  10198. size_t result = 0;
  10199. for (int i = 0; i < 2; ++i) {
  10200. if (_valueSet[i]) {
  10201. ++count;
  10202. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10203. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10204. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10205. }
  10206. }
  10207. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10208. result += tagSize * count;
  10209. return result;
  10210. }
  10211. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10212. asField:(GPBFieldDescriptor *)field {
  10213. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10214. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10215. for (int i = 0; i < 2; ++i) {
  10216. if (_valueSet[i]) {
  10217. // Write the tag.
  10218. [outputStream writeInt32NoTag:tag];
  10219. // Write the size of the message.
  10220. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10221. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10222. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10223. // Write the fields.
  10224. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10225. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10226. }
  10227. }
  10228. }
  10229. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10230. if (otherDictionary) {
  10231. for (int i = 0; i < 2; ++i) {
  10232. if (otherDictionary->_valueSet[i]) {
  10233. _valueSet[i] = YES;
  10234. _values[i] = otherDictionary->_values[i];
  10235. }
  10236. }
  10237. if (_autocreator) {
  10238. GPBAutocreatedDictionaryModified(_autocreator, self);
  10239. }
  10240. }
  10241. }
  10242. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  10243. int idx = (key ? 1 : 0);
  10244. _values[idx] = value;
  10245. _valueSet[idx] = YES;
  10246. if (_autocreator) {
  10247. GPBAutocreatedDictionaryModified(_autocreator, self);
  10248. }
  10249. }
  10250. - (void)removeValueForKey:(BOOL)aKey {
  10251. _valueSet[aKey ? 1 : 0] = NO;
  10252. }
  10253. - (void)removeAll {
  10254. _valueSet[0] = NO;
  10255. _valueSet[1] = NO;
  10256. }
  10257. @end
  10258. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10259. // This block of code is generated, do not edit it directly.
  10260. #pragma mark - Bool -> UInt64
  10261. @implementation GPBBoolUInt64Dictionary {
  10262. @package
  10263. uint64_t _values[2];
  10264. BOOL _valueSet[2];
  10265. }
  10266. + (instancetype)dictionary {
  10267. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10268. }
  10269. + (instancetype)dictionaryWithValue:(uint64_t)value
  10270. forKey:(BOOL)key {
  10271. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10272. // on to get the type correct.
  10273. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  10274. forKeys:&key
  10275. count:1] autorelease];
  10276. }
  10277. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  10278. forKeys:(const BOOL [])keys
  10279. count:(NSUInteger)count {
  10280. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10281. // on to get the type correct.
  10282. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  10283. forKeys:keys
  10284. count:count] autorelease];
  10285. }
  10286. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10287. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10288. // on to get the type correct.
  10289. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10290. }
  10291. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10292. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10293. }
  10294. - (instancetype)init {
  10295. return [self initWithValues:NULL forKeys:NULL count:0];
  10296. }
  10297. - (instancetype)initWithValues:(const uint64_t [])values
  10298. forKeys:(const BOOL [])keys
  10299. count:(NSUInteger)count {
  10300. self = [super init];
  10301. if (self) {
  10302. for (NSUInteger i = 0; i < count; ++i) {
  10303. int idx = keys[i] ? 1 : 0;
  10304. _values[idx] = values[i];
  10305. _valueSet[idx] = YES;
  10306. }
  10307. }
  10308. return self;
  10309. }
  10310. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10311. self = [self initWithValues:NULL forKeys:NULL count:0];
  10312. if (self) {
  10313. if (dictionary) {
  10314. for (int i = 0; i < 2; ++i) {
  10315. if (dictionary->_valueSet[i]) {
  10316. _values[i] = dictionary->_values[i];
  10317. _valueSet[i] = YES;
  10318. }
  10319. }
  10320. }
  10321. }
  10322. return self;
  10323. }
  10324. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10325. #pragma unused(numItems)
  10326. return [self initWithValues:NULL forKeys:NULL count:0];
  10327. }
  10328. #if !defined(NS_BLOCK_ASSERTIONS)
  10329. - (void)dealloc {
  10330. NSAssert(!_autocreator,
  10331. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10332. [self class], _autocreator);
  10333. [super dealloc];
  10334. }
  10335. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10336. - (instancetype)copyWithZone:(NSZone *)zone {
  10337. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10338. }
  10339. - (BOOL)isEqual:(id)other {
  10340. if (self == other) {
  10341. return YES;
  10342. }
  10343. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10344. return NO;
  10345. }
  10346. GPBBoolUInt64Dictionary *otherDictionary = other;
  10347. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10348. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10349. return NO;
  10350. }
  10351. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10352. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10353. return NO;
  10354. }
  10355. return YES;
  10356. }
  10357. - (NSUInteger)hash {
  10358. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10359. }
  10360. - (NSString *)description {
  10361. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10362. if (_valueSet[0]) {
  10363. [result appendFormat:@"NO: %llu", _values[0]];
  10364. }
  10365. if (_valueSet[1]) {
  10366. [result appendFormat:@"YES: %llu", _values[1]];
  10367. }
  10368. [result appendString:@" }"];
  10369. return result;
  10370. }
  10371. - (NSUInteger)count {
  10372. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10373. }
  10374. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  10375. int idx = (key ? 1 : 0);
  10376. if (_valueSet[idx]) {
  10377. if (value) {
  10378. *value = _values[idx];
  10379. }
  10380. return YES;
  10381. }
  10382. return NO;
  10383. }
  10384. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10385. forGPBGenericValueKey:(GPBGenericValue *)key {
  10386. int idx = (key->valueBool ? 1 : 0);
  10387. _values[idx] = value->valueUInt64;
  10388. _valueSet[idx] = YES;
  10389. }
  10390. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10391. if (_valueSet[0]) {
  10392. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10393. }
  10394. if (_valueSet[1]) {
  10395. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10396. }
  10397. }
  10398. - (void)enumerateKeysAndValuesUsingBlock:
  10399. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10400. BOOL stop = NO;
  10401. if (_valueSet[0]) {
  10402. block(NO, _values[0], &stop);
  10403. }
  10404. if (!stop && _valueSet[1]) {
  10405. block(YES, _values[1], &stop);
  10406. }
  10407. }
  10408. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10409. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10410. NSUInteger count = 0;
  10411. size_t result = 0;
  10412. for (int i = 0; i < 2; ++i) {
  10413. if (_valueSet[i]) {
  10414. ++count;
  10415. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10416. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10417. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10418. }
  10419. }
  10420. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10421. result += tagSize * count;
  10422. return result;
  10423. }
  10424. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10425. asField:(GPBFieldDescriptor *)field {
  10426. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10427. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10428. for (int i = 0; i < 2; ++i) {
  10429. if (_valueSet[i]) {
  10430. // Write the tag.
  10431. [outputStream writeInt32NoTag:tag];
  10432. // Write the size of the message.
  10433. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10434. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10435. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10436. // Write the fields.
  10437. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10438. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10439. }
  10440. }
  10441. }
  10442. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10443. if (otherDictionary) {
  10444. for (int i = 0; i < 2; ++i) {
  10445. if (otherDictionary->_valueSet[i]) {
  10446. _valueSet[i] = YES;
  10447. _values[i] = otherDictionary->_values[i];
  10448. }
  10449. }
  10450. if (_autocreator) {
  10451. GPBAutocreatedDictionaryModified(_autocreator, self);
  10452. }
  10453. }
  10454. }
  10455. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  10456. int idx = (key ? 1 : 0);
  10457. _values[idx] = value;
  10458. _valueSet[idx] = YES;
  10459. if (_autocreator) {
  10460. GPBAutocreatedDictionaryModified(_autocreator, self);
  10461. }
  10462. }
  10463. - (void)removeValueForKey:(BOOL)aKey {
  10464. _valueSet[aKey ? 1 : 0] = NO;
  10465. }
  10466. - (void)removeAll {
  10467. _valueSet[0] = NO;
  10468. _valueSet[1] = NO;
  10469. }
  10470. @end
  10471. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10472. // This block of code is generated, do not edit it directly.
  10473. #pragma mark - Bool -> Int64
  10474. @implementation GPBBoolInt64Dictionary {
  10475. @package
  10476. int64_t _values[2];
  10477. BOOL _valueSet[2];
  10478. }
  10479. + (instancetype)dictionary {
  10480. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10481. }
  10482. + (instancetype)dictionaryWithValue:(int64_t)value
  10483. forKey:(BOOL)key {
  10484. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10485. // on to get the type correct.
  10486. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  10487. forKeys:&key
  10488. count:1] autorelease];
  10489. }
  10490. + (instancetype)dictionaryWithValues:(const int64_t [])values
  10491. forKeys:(const BOOL [])keys
  10492. count:(NSUInteger)count {
  10493. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10494. // on to get the type correct.
  10495. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  10496. forKeys:keys
  10497. count:count] autorelease];
  10498. }
  10499. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10500. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10501. // on to get the type correct.
  10502. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10503. }
  10504. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10505. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10506. }
  10507. - (instancetype)init {
  10508. return [self initWithValues:NULL forKeys:NULL count:0];
  10509. }
  10510. - (instancetype)initWithValues:(const int64_t [])values
  10511. forKeys:(const BOOL [])keys
  10512. count:(NSUInteger)count {
  10513. self = [super init];
  10514. if (self) {
  10515. for (NSUInteger i = 0; i < count; ++i) {
  10516. int idx = keys[i] ? 1 : 0;
  10517. _values[idx] = values[i];
  10518. _valueSet[idx] = YES;
  10519. }
  10520. }
  10521. return self;
  10522. }
  10523. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10524. self = [self initWithValues:NULL forKeys:NULL count:0];
  10525. if (self) {
  10526. if (dictionary) {
  10527. for (int i = 0; i < 2; ++i) {
  10528. if (dictionary->_valueSet[i]) {
  10529. _values[i] = dictionary->_values[i];
  10530. _valueSet[i] = YES;
  10531. }
  10532. }
  10533. }
  10534. }
  10535. return self;
  10536. }
  10537. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10538. #pragma unused(numItems)
  10539. return [self initWithValues:NULL forKeys:NULL count:0];
  10540. }
  10541. #if !defined(NS_BLOCK_ASSERTIONS)
  10542. - (void)dealloc {
  10543. NSAssert(!_autocreator,
  10544. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10545. [self class], _autocreator);
  10546. [super dealloc];
  10547. }
  10548. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10549. - (instancetype)copyWithZone:(NSZone *)zone {
  10550. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10551. }
  10552. - (BOOL)isEqual:(id)other {
  10553. if (self == other) {
  10554. return YES;
  10555. }
  10556. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10557. return NO;
  10558. }
  10559. GPBBoolInt64Dictionary *otherDictionary = other;
  10560. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10561. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10562. return NO;
  10563. }
  10564. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10565. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10566. return NO;
  10567. }
  10568. return YES;
  10569. }
  10570. - (NSUInteger)hash {
  10571. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10572. }
  10573. - (NSString *)description {
  10574. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10575. if (_valueSet[0]) {
  10576. [result appendFormat:@"NO: %lld", _values[0]];
  10577. }
  10578. if (_valueSet[1]) {
  10579. [result appendFormat:@"YES: %lld", _values[1]];
  10580. }
  10581. [result appendString:@" }"];
  10582. return result;
  10583. }
  10584. - (NSUInteger)count {
  10585. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10586. }
  10587. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  10588. int idx = (key ? 1 : 0);
  10589. if (_valueSet[idx]) {
  10590. if (value) {
  10591. *value = _values[idx];
  10592. }
  10593. return YES;
  10594. }
  10595. return NO;
  10596. }
  10597. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10598. forGPBGenericValueKey:(GPBGenericValue *)key {
  10599. int idx = (key->valueBool ? 1 : 0);
  10600. _values[idx] = value->valueInt64;
  10601. _valueSet[idx] = YES;
  10602. }
  10603. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10604. if (_valueSet[0]) {
  10605. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10606. }
  10607. if (_valueSet[1]) {
  10608. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10609. }
  10610. }
  10611. - (void)enumerateKeysAndValuesUsingBlock:
  10612. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10613. BOOL stop = NO;
  10614. if (_valueSet[0]) {
  10615. block(NO, _values[0], &stop);
  10616. }
  10617. if (!stop && _valueSet[1]) {
  10618. block(YES, _values[1], &stop);
  10619. }
  10620. }
  10621. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10622. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10623. NSUInteger count = 0;
  10624. size_t result = 0;
  10625. for (int i = 0; i < 2; ++i) {
  10626. if (_valueSet[i]) {
  10627. ++count;
  10628. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10629. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10630. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10631. }
  10632. }
  10633. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10634. result += tagSize * count;
  10635. return result;
  10636. }
  10637. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10638. asField:(GPBFieldDescriptor *)field {
  10639. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10640. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10641. for (int i = 0; i < 2; ++i) {
  10642. if (_valueSet[i]) {
  10643. // Write the tag.
  10644. [outputStream writeInt32NoTag:tag];
  10645. // Write the size of the message.
  10646. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10647. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10648. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10649. // Write the fields.
  10650. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10651. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10652. }
  10653. }
  10654. }
  10655. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10656. if (otherDictionary) {
  10657. for (int i = 0; i < 2; ++i) {
  10658. if (otherDictionary->_valueSet[i]) {
  10659. _valueSet[i] = YES;
  10660. _values[i] = otherDictionary->_values[i];
  10661. }
  10662. }
  10663. if (_autocreator) {
  10664. GPBAutocreatedDictionaryModified(_autocreator, self);
  10665. }
  10666. }
  10667. }
  10668. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  10669. int idx = (key ? 1 : 0);
  10670. _values[idx] = value;
  10671. _valueSet[idx] = YES;
  10672. if (_autocreator) {
  10673. GPBAutocreatedDictionaryModified(_autocreator, self);
  10674. }
  10675. }
  10676. - (void)removeValueForKey:(BOOL)aKey {
  10677. _valueSet[aKey ? 1 : 0] = NO;
  10678. }
  10679. - (void)removeAll {
  10680. _valueSet[0] = NO;
  10681. _valueSet[1] = NO;
  10682. }
  10683. @end
  10684. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10685. // This block of code is generated, do not edit it directly.
  10686. #pragma mark - Bool -> Bool
  10687. @implementation GPBBoolBoolDictionary {
  10688. @package
  10689. BOOL _values[2];
  10690. BOOL _valueSet[2];
  10691. }
  10692. + (instancetype)dictionary {
  10693. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10694. }
  10695. + (instancetype)dictionaryWithValue:(BOOL)value
  10696. forKey:(BOOL)key {
  10697. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10698. // on to get the type correct.
  10699. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  10700. forKeys:&key
  10701. count:1] autorelease];
  10702. }
  10703. + (instancetype)dictionaryWithValues:(const BOOL [])values
  10704. forKeys:(const BOOL [])keys
  10705. count:(NSUInteger)count {
  10706. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10707. // on to get the type correct.
  10708. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  10709. forKeys:keys
  10710. count:count] autorelease];
  10711. }
  10712. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10713. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10714. // on to get the type correct.
  10715. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10716. }
  10717. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10718. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10719. }
  10720. - (instancetype)init {
  10721. return [self initWithValues:NULL forKeys:NULL count:0];
  10722. }
  10723. - (instancetype)initWithValues:(const BOOL [])values
  10724. forKeys:(const BOOL [])keys
  10725. count:(NSUInteger)count {
  10726. self = [super init];
  10727. if (self) {
  10728. for (NSUInteger i = 0; i < count; ++i) {
  10729. int idx = keys[i] ? 1 : 0;
  10730. _values[idx] = values[i];
  10731. _valueSet[idx] = YES;
  10732. }
  10733. }
  10734. return self;
  10735. }
  10736. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10737. self = [self initWithValues:NULL forKeys:NULL count:0];
  10738. if (self) {
  10739. if (dictionary) {
  10740. for (int i = 0; i < 2; ++i) {
  10741. if (dictionary->_valueSet[i]) {
  10742. _values[i] = dictionary->_values[i];
  10743. _valueSet[i] = YES;
  10744. }
  10745. }
  10746. }
  10747. }
  10748. return self;
  10749. }
  10750. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10751. #pragma unused(numItems)
  10752. return [self initWithValues:NULL forKeys:NULL count:0];
  10753. }
  10754. #if !defined(NS_BLOCK_ASSERTIONS)
  10755. - (void)dealloc {
  10756. NSAssert(!_autocreator,
  10757. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10758. [self class], _autocreator);
  10759. [super dealloc];
  10760. }
  10761. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10762. - (instancetype)copyWithZone:(NSZone *)zone {
  10763. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10764. }
  10765. - (BOOL)isEqual:(id)other {
  10766. if (self == other) {
  10767. return YES;
  10768. }
  10769. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10770. return NO;
  10771. }
  10772. GPBBoolBoolDictionary *otherDictionary = other;
  10773. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10774. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10775. return NO;
  10776. }
  10777. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10778. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10779. return NO;
  10780. }
  10781. return YES;
  10782. }
  10783. - (NSUInteger)hash {
  10784. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10785. }
  10786. - (NSString *)description {
  10787. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10788. if (_valueSet[0]) {
  10789. [result appendFormat:@"NO: %d", _values[0]];
  10790. }
  10791. if (_valueSet[1]) {
  10792. [result appendFormat:@"YES: %d", _values[1]];
  10793. }
  10794. [result appendString:@" }"];
  10795. return result;
  10796. }
  10797. - (NSUInteger)count {
  10798. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10799. }
  10800. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10801. int idx = (key ? 1 : 0);
  10802. if (_valueSet[idx]) {
  10803. if (value) {
  10804. *value = _values[idx];
  10805. }
  10806. return YES;
  10807. }
  10808. return NO;
  10809. }
  10810. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10811. forGPBGenericValueKey:(GPBGenericValue *)key {
  10812. int idx = (key->valueBool ? 1 : 0);
  10813. _values[idx] = value->valueBool;
  10814. _valueSet[idx] = YES;
  10815. }
  10816. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10817. if (_valueSet[0]) {
  10818. block(@"false", (_values[0] ? @"true" : @"false"));
  10819. }
  10820. if (_valueSet[1]) {
  10821. block(@"true", (_values[1] ? @"true" : @"false"));
  10822. }
  10823. }
  10824. - (void)enumerateKeysAndValuesUsingBlock:
  10825. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10826. BOOL stop = NO;
  10827. if (_valueSet[0]) {
  10828. block(NO, _values[0], &stop);
  10829. }
  10830. if (!stop && _valueSet[1]) {
  10831. block(YES, _values[1], &stop);
  10832. }
  10833. }
  10834. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10835. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10836. NSUInteger count = 0;
  10837. size_t result = 0;
  10838. for (int i = 0; i < 2; ++i) {
  10839. if (_valueSet[i]) {
  10840. ++count;
  10841. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10842. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10843. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10844. }
  10845. }
  10846. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10847. result += tagSize * count;
  10848. return result;
  10849. }
  10850. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10851. asField:(GPBFieldDescriptor *)field {
  10852. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10853. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10854. for (int i = 0; i < 2; ++i) {
  10855. if (_valueSet[i]) {
  10856. // Write the tag.
  10857. [outputStream writeInt32NoTag:tag];
  10858. // Write the size of the message.
  10859. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10860. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10861. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10862. // Write the fields.
  10863. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10864. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10865. }
  10866. }
  10867. }
  10868. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10869. if (otherDictionary) {
  10870. for (int i = 0; i < 2; ++i) {
  10871. if (otherDictionary->_valueSet[i]) {
  10872. _valueSet[i] = YES;
  10873. _values[i] = otherDictionary->_values[i];
  10874. }
  10875. }
  10876. if (_autocreator) {
  10877. GPBAutocreatedDictionaryModified(_autocreator, self);
  10878. }
  10879. }
  10880. }
  10881. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10882. int idx = (key ? 1 : 0);
  10883. _values[idx] = value;
  10884. _valueSet[idx] = YES;
  10885. if (_autocreator) {
  10886. GPBAutocreatedDictionaryModified(_autocreator, self);
  10887. }
  10888. }
  10889. - (void)removeValueForKey:(BOOL)aKey {
  10890. _valueSet[aKey ? 1 : 0] = NO;
  10891. }
  10892. - (void)removeAll {
  10893. _valueSet[0] = NO;
  10894. _valueSet[1] = NO;
  10895. }
  10896. @end
  10897. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10898. // This block of code is generated, do not edit it directly.
  10899. #pragma mark - Bool -> Float
  10900. @implementation GPBBoolFloatDictionary {
  10901. @package
  10902. float _values[2];
  10903. BOOL _valueSet[2];
  10904. }
  10905. + (instancetype)dictionary {
  10906. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10907. }
  10908. + (instancetype)dictionaryWithValue:(float)value
  10909. forKey:(BOOL)key {
  10910. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10911. // on to get the type correct.
  10912. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10913. forKeys:&key
  10914. count:1] autorelease];
  10915. }
  10916. + (instancetype)dictionaryWithValues:(const float [])values
  10917. forKeys:(const BOOL [])keys
  10918. count:(NSUInteger)count {
  10919. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10920. // on to get the type correct.
  10921. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10922. forKeys:keys
  10923. count:count] autorelease];
  10924. }
  10925. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10926. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10927. // on to get the type correct.
  10928. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10929. }
  10930. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10931. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10932. }
  10933. - (instancetype)init {
  10934. return [self initWithValues:NULL forKeys:NULL count:0];
  10935. }
  10936. - (instancetype)initWithValues:(const float [])values
  10937. forKeys:(const BOOL [])keys
  10938. count:(NSUInteger)count {
  10939. self = [super init];
  10940. if (self) {
  10941. for (NSUInteger i = 0; i < count; ++i) {
  10942. int idx = keys[i] ? 1 : 0;
  10943. _values[idx] = values[i];
  10944. _valueSet[idx] = YES;
  10945. }
  10946. }
  10947. return self;
  10948. }
  10949. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10950. self = [self initWithValues:NULL forKeys:NULL count:0];
  10951. if (self) {
  10952. if (dictionary) {
  10953. for (int i = 0; i < 2; ++i) {
  10954. if (dictionary->_valueSet[i]) {
  10955. _values[i] = dictionary->_values[i];
  10956. _valueSet[i] = YES;
  10957. }
  10958. }
  10959. }
  10960. }
  10961. return self;
  10962. }
  10963. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10964. #pragma unused(numItems)
  10965. return [self initWithValues:NULL forKeys:NULL count:0];
  10966. }
  10967. #if !defined(NS_BLOCK_ASSERTIONS)
  10968. - (void)dealloc {
  10969. NSAssert(!_autocreator,
  10970. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10971. [self class], _autocreator);
  10972. [super dealloc];
  10973. }
  10974. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10975. - (instancetype)copyWithZone:(NSZone *)zone {
  10976. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10977. }
  10978. - (BOOL)isEqual:(id)other {
  10979. if (self == other) {
  10980. return YES;
  10981. }
  10982. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10983. return NO;
  10984. }
  10985. GPBBoolFloatDictionary *otherDictionary = other;
  10986. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10987. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10988. return NO;
  10989. }
  10990. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10991. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10992. return NO;
  10993. }
  10994. return YES;
  10995. }
  10996. - (NSUInteger)hash {
  10997. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10998. }
  10999. - (NSString *)description {
  11000. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11001. if (_valueSet[0]) {
  11002. [result appendFormat:@"NO: %f", _values[0]];
  11003. }
  11004. if (_valueSet[1]) {
  11005. [result appendFormat:@"YES: %f", _values[1]];
  11006. }
  11007. [result appendString:@" }"];
  11008. return result;
  11009. }
  11010. - (NSUInteger)count {
  11011. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11012. }
  11013. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  11014. int idx = (key ? 1 : 0);
  11015. if (_valueSet[idx]) {
  11016. if (value) {
  11017. *value = _values[idx];
  11018. }
  11019. return YES;
  11020. }
  11021. return NO;
  11022. }
  11023. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11024. forGPBGenericValueKey:(GPBGenericValue *)key {
  11025. int idx = (key->valueBool ? 1 : 0);
  11026. _values[idx] = value->valueFloat;
  11027. _valueSet[idx] = YES;
  11028. }
  11029. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11030. if (_valueSet[0]) {
  11031. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  11032. }
  11033. if (_valueSet[1]) {
  11034. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  11035. }
  11036. }
  11037. - (void)enumerateKeysAndValuesUsingBlock:
  11038. (void (^)(BOOL key, float value, BOOL *stop))block {
  11039. BOOL stop = NO;
  11040. if (_valueSet[0]) {
  11041. block(NO, _values[0], &stop);
  11042. }
  11043. if (!stop && _valueSet[1]) {
  11044. block(YES, _values[1], &stop);
  11045. }
  11046. }
  11047. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11048. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11049. NSUInteger count = 0;
  11050. size_t result = 0;
  11051. for (int i = 0; i < 2; ++i) {
  11052. if (_valueSet[i]) {
  11053. ++count;
  11054. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11055. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11056. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11057. }
  11058. }
  11059. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11060. result += tagSize * count;
  11061. return result;
  11062. }
  11063. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11064. asField:(GPBFieldDescriptor *)field {
  11065. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11066. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11067. for (int i = 0; i < 2; ++i) {
  11068. if (_valueSet[i]) {
  11069. // Write the tag.
  11070. [outputStream writeInt32NoTag:tag];
  11071. // Write the size of the message.
  11072. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11073. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11074. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11075. // Write the fields.
  11076. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11077. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11078. }
  11079. }
  11080. }
  11081. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  11082. if (otherDictionary) {
  11083. for (int i = 0; i < 2; ++i) {
  11084. if (otherDictionary->_valueSet[i]) {
  11085. _valueSet[i] = YES;
  11086. _values[i] = otherDictionary->_values[i];
  11087. }
  11088. }
  11089. if (_autocreator) {
  11090. GPBAutocreatedDictionaryModified(_autocreator, self);
  11091. }
  11092. }
  11093. }
  11094. - (void)setValue:(float)value forKey:(BOOL)key {
  11095. int idx = (key ? 1 : 0);
  11096. _values[idx] = value;
  11097. _valueSet[idx] = YES;
  11098. if (_autocreator) {
  11099. GPBAutocreatedDictionaryModified(_autocreator, self);
  11100. }
  11101. }
  11102. - (void)removeValueForKey:(BOOL)aKey {
  11103. _valueSet[aKey ? 1 : 0] = NO;
  11104. }
  11105. - (void)removeAll {
  11106. _valueSet[0] = NO;
  11107. _valueSet[1] = NO;
  11108. }
  11109. @end
  11110. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  11111. // This block of code is generated, do not edit it directly.
  11112. #pragma mark - Bool -> Double
  11113. @implementation GPBBoolDoubleDictionary {
  11114. @package
  11115. double _values[2];
  11116. BOOL _valueSet[2];
  11117. }
  11118. + (instancetype)dictionary {
  11119. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  11120. }
  11121. + (instancetype)dictionaryWithValue:(double)value
  11122. forKey:(BOOL)key {
  11123. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11124. // on to get the type correct.
  11125. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  11126. forKeys:&key
  11127. count:1] autorelease];
  11128. }
  11129. + (instancetype)dictionaryWithValues:(const double [])values
  11130. forKeys:(const BOOL [])keys
  11131. count:(NSUInteger)count {
  11132. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11133. // on to get the type correct.
  11134. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  11135. forKeys:keys
  11136. count:count] autorelease];
  11137. }
  11138. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11139. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11140. // on to get the type correct.
  11141. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11142. }
  11143. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11144. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11145. }
  11146. - (instancetype)init {
  11147. return [self initWithValues:NULL forKeys:NULL count:0];
  11148. }
  11149. - (instancetype)initWithValues:(const double [])values
  11150. forKeys:(const BOOL [])keys
  11151. count:(NSUInteger)count {
  11152. self = [super init];
  11153. if (self) {
  11154. for (NSUInteger i = 0; i < count; ++i) {
  11155. int idx = keys[i] ? 1 : 0;
  11156. _values[idx] = values[i];
  11157. _valueSet[idx] = YES;
  11158. }
  11159. }
  11160. return self;
  11161. }
  11162. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11163. self = [self initWithValues:NULL forKeys:NULL count:0];
  11164. if (self) {
  11165. if (dictionary) {
  11166. for (int i = 0; i < 2; ++i) {
  11167. if (dictionary->_valueSet[i]) {
  11168. _values[i] = dictionary->_values[i];
  11169. _valueSet[i] = YES;
  11170. }
  11171. }
  11172. }
  11173. }
  11174. return self;
  11175. }
  11176. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11177. #pragma unused(numItems)
  11178. return [self initWithValues:NULL forKeys:NULL count:0];
  11179. }
  11180. #if !defined(NS_BLOCK_ASSERTIONS)
  11181. - (void)dealloc {
  11182. NSAssert(!_autocreator,
  11183. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11184. [self class], _autocreator);
  11185. [super dealloc];
  11186. }
  11187. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11188. - (instancetype)copyWithZone:(NSZone *)zone {
  11189. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  11190. }
  11191. - (BOOL)isEqual:(id)other {
  11192. if (self == other) {
  11193. return YES;
  11194. }
  11195. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  11196. return NO;
  11197. }
  11198. GPBBoolDoubleDictionary *otherDictionary = other;
  11199. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11200. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11201. return NO;
  11202. }
  11203. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11204. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11205. return NO;
  11206. }
  11207. return YES;
  11208. }
  11209. - (NSUInteger)hash {
  11210. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11211. }
  11212. - (NSString *)description {
  11213. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11214. if (_valueSet[0]) {
  11215. [result appendFormat:@"NO: %lf", _values[0]];
  11216. }
  11217. if (_valueSet[1]) {
  11218. [result appendFormat:@"YES: %lf", _values[1]];
  11219. }
  11220. [result appendString:@" }"];
  11221. return result;
  11222. }
  11223. - (NSUInteger)count {
  11224. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11225. }
  11226. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  11227. int idx = (key ? 1 : 0);
  11228. if (_valueSet[idx]) {
  11229. if (value) {
  11230. *value = _values[idx];
  11231. }
  11232. return YES;
  11233. }
  11234. return NO;
  11235. }
  11236. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11237. forGPBGenericValueKey:(GPBGenericValue *)key {
  11238. int idx = (key->valueBool ? 1 : 0);
  11239. _values[idx] = value->valueDouble;
  11240. _valueSet[idx] = YES;
  11241. }
  11242. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11243. if (_valueSet[0]) {
  11244. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11245. }
  11246. if (_valueSet[1]) {
  11247. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11248. }
  11249. }
  11250. - (void)enumerateKeysAndValuesUsingBlock:
  11251. (void (^)(BOOL key, double value, BOOL *stop))block {
  11252. BOOL stop = NO;
  11253. if (_valueSet[0]) {
  11254. block(NO, _values[0], &stop);
  11255. }
  11256. if (!stop && _valueSet[1]) {
  11257. block(YES, _values[1], &stop);
  11258. }
  11259. }
  11260. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11261. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11262. NSUInteger count = 0;
  11263. size_t result = 0;
  11264. for (int i = 0; i < 2; ++i) {
  11265. if (_valueSet[i]) {
  11266. ++count;
  11267. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11268. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11269. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11270. }
  11271. }
  11272. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11273. result += tagSize * count;
  11274. return result;
  11275. }
  11276. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11277. asField:(GPBFieldDescriptor *)field {
  11278. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11279. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11280. for (int i = 0; i < 2; ++i) {
  11281. if (_valueSet[i]) {
  11282. // Write the tag.
  11283. [outputStream writeInt32NoTag:tag];
  11284. // Write the size of the message.
  11285. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11286. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11287. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11288. // Write the fields.
  11289. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11290. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11291. }
  11292. }
  11293. }
  11294. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11295. if (otherDictionary) {
  11296. for (int i = 0; i < 2; ++i) {
  11297. if (otherDictionary->_valueSet[i]) {
  11298. _valueSet[i] = YES;
  11299. _values[i] = otherDictionary->_values[i];
  11300. }
  11301. }
  11302. if (_autocreator) {
  11303. GPBAutocreatedDictionaryModified(_autocreator, self);
  11304. }
  11305. }
  11306. }
  11307. - (void)setValue:(double)value forKey:(BOOL)key {
  11308. int idx = (key ? 1 : 0);
  11309. _values[idx] = value;
  11310. _valueSet[idx] = YES;
  11311. if (_autocreator) {
  11312. GPBAutocreatedDictionaryModified(_autocreator, self);
  11313. }
  11314. }
  11315. - (void)removeValueForKey:(BOOL)aKey {
  11316. _valueSet[aKey ? 1 : 0] = NO;
  11317. }
  11318. - (void)removeAll {
  11319. _valueSet[0] = NO;
  11320. _valueSet[1] = NO;
  11321. }
  11322. @end
  11323. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11324. // This block of code is generated, do not edit it directly.
  11325. #pragma mark - Bool -> Object
  11326. @implementation GPBBoolObjectDictionary {
  11327. @package
  11328. id _values[2];
  11329. }
  11330. + (instancetype)dictionary {
  11331. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11332. }
  11333. + (instancetype)dictionaryWithObject:(id)object
  11334. forKey:(BOOL)key {
  11335. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11336. // on to get the type correct.
  11337. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11338. forKeys:&key
  11339. count:1] autorelease];
  11340. }
  11341. + (instancetype)dictionaryWithObjects:(const id [])objects
  11342. forKeys:(const BOOL [])keys
  11343. count:(NSUInteger)count {
  11344. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11345. // on to get the type correct.
  11346. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11347. forKeys:keys
  11348. count:count] autorelease];
  11349. }
  11350. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11351. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11352. // on to get the type correct.
  11353. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11354. }
  11355. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11356. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11357. }
  11358. - (instancetype)init {
  11359. return [self initWithObjects:NULL forKeys:NULL count:0];
  11360. }
  11361. - (instancetype)initWithObjects:(const id [])objects
  11362. forKeys:(const BOOL [])keys
  11363. count:(NSUInteger)count {
  11364. self = [super init];
  11365. if (self) {
  11366. for (NSUInteger i = 0; i < count; ++i) {
  11367. if (!objects[i]) {
  11368. [NSException raise:NSInvalidArgumentException
  11369. format:@"Attempting to add nil object to a Dictionary"];
  11370. }
  11371. int idx = keys[i] ? 1 : 0;
  11372. [_values[idx] release];
  11373. _values[idx] = (id)[objects[i] retain];
  11374. }
  11375. }
  11376. return self;
  11377. }
  11378. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11379. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11380. if (self) {
  11381. if (dictionary) {
  11382. _values[0] = [dictionary->_values[0] retain];
  11383. _values[1] = [dictionary->_values[1] retain];
  11384. }
  11385. }
  11386. return self;
  11387. }
  11388. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11389. #pragma unused(numItems)
  11390. return [self initWithObjects:NULL forKeys:NULL count:0];
  11391. }
  11392. - (void)dealloc {
  11393. NSAssert(!_autocreator,
  11394. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11395. [self class], _autocreator);
  11396. [_values[0] release];
  11397. [_values[1] release];
  11398. [super dealloc];
  11399. }
  11400. - (instancetype)copyWithZone:(NSZone *)zone {
  11401. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11402. }
  11403. - (BOOL)isEqual:(id)other {
  11404. if (self == other) {
  11405. return YES;
  11406. }
  11407. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11408. return NO;
  11409. }
  11410. GPBBoolObjectDictionary *otherDictionary = other;
  11411. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  11412. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  11413. return NO;
  11414. }
  11415. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  11416. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  11417. return NO;
  11418. }
  11419. return YES;
  11420. }
  11421. - (NSUInteger)hash {
  11422. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11423. }
  11424. - (NSString *)description {
  11425. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11426. if ((_values[0] != nil)) {
  11427. [result appendFormat:@"NO: %@", _values[0]];
  11428. }
  11429. if ((_values[1] != nil)) {
  11430. [result appendFormat:@"YES: %@", _values[1]];
  11431. }
  11432. [result appendString:@" }"];
  11433. return result;
  11434. }
  11435. - (NSUInteger)count {
  11436. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11437. }
  11438. - (id)objectForKey:(BOOL)key {
  11439. return _values[key ? 1 : 0];
  11440. }
  11441. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11442. forGPBGenericValueKey:(GPBGenericValue *)key {
  11443. int idx = (key->valueBool ? 1 : 0);
  11444. [_values[idx] release];
  11445. _values[idx] = [value->valueString retain];
  11446. }
  11447. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11448. if (_values[0] != nil) {
  11449. block(@"false", _values[0]);
  11450. }
  11451. if ((_values[1] != nil)) {
  11452. block(@"true", _values[1]);
  11453. }
  11454. }
  11455. - (void)enumerateKeysAndObjectsUsingBlock:
  11456. (void (^)(BOOL key, id object, BOOL *stop))block {
  11457. BOOL stop = NO;
  11458. if (_values[0] != nil) {
  11459. block(NO, _values[0], &stop);
  11460. }
  11461. if (!stop && (_values[1] != nil)) {
  11462. block(YES, _values[1], &stop);
  11463. }
  11464. }
  11465. - (BOOL)isInitialized {
  11466. if (_values[0] && ![_values[0] isInitialized]) {
  11467. return NO;
  11468. }
  11469. if (_values[1] && ![_values[1] isInitialized]) {
  11470. return NO;
  11471. }
  11472. return YES;
  11473. }
  11474. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11475. GPBBoolObjectDictionary *newDict =
  11476. [[GPBBoolObjectDictionary alloc] init];
  11477. for (int i = 0; i < 2; ++i) {
  11478. if (_values[i] != nil) {
  11479. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11480. }
  11481. }
  11482. return newDict;
  11483. }
  11484. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11485. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11486. NSUInteger count = 0;
  11487. size_t result = 0;
  11488. for (int i = 0; i < 2; ++i) {
  11489. if (_values[i] != nil) {
  11490. ++count;
  11491. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11492. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11493. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11494. }
  11495. }
  11496. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11497. result += tagSize * count;
  11498. return result;
  11499. }
  11500. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11501. asField:(GPBFieldDescriptor *)field {
  11502. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11503. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11504. for (int i = 0; i < 2; ++i) {
  11505. if (_values[i] != nil) {
  11506. // Write the tag.
  11507. [outputStream writeInt32NoTag:tag];
  11508. // Write the size of the message.
  11509. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11510. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11511. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11512. // Write the fields.
  11513. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11514. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11515. }
  11516. }
  11517. }
  11518. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11519. if (otherDictionary) {
  11520. for (int i = 0; i < 2; ++i) {
  11521. if (otherDictionary->_values[i] != nil) {
  11522. [_values[i] release];
  11523. _values[i] = [otherDictionary->_values[i] retain];
  11524. }
  11525. }
  11526. if (_autocreator) {
  11527. GPBAutocreatedDictionaryModified(_autocreator, self);
  11528. }
  11529. }
  11530. }
  11531. - (void)setObject:(id)object forKey:(BOOL)key {
  11532. if (!object) {
  11533. [NSException raise:NSInvalidArgumentException
  11534. format:@"Attempting to add nil object to a Dictionary"];
  11535. }
  11536. int idx = (key ? 1 : 0);
  11537. [_values[idx] release];
  11538. _values[idx] = [object retain];
  11539. if (_autocreator) {
  11540. GPBAutocreatedDictionaryModified(_autocreator, self);
  11541. }
  11542. }
  11543. - (void)removeObjectForKey:(BOOL)aKey {
  11544. int idx = (aKey ? 1 : 0);
  11545. [_values[idx] release];
  11546. _values[idx] = nil;
  11547. }
  11548. - (void)removeAll {
  11549. for (int i = 0; i < 2; ++i) {
  11550. [_values[i] release];
  11551. _values[i] = nil;
  11552. }
  11553. }
  11554. @end
  11555. //%PDDM-EXPAND-END (8 expansions)
  11556. #pragma mark - Bool -> Enum
  11557. @implementation GPBBoolEnumDictionary {
  11558. @package
  11559. GPBEnumValidationFunc _validationFunc;
  11560. int32_t _values[2];
  11561. BOOL _valueSet[2];
  11562. }
  11563. @synthesize validationFunc = _validationFunc;
  11564. + (instancetype)dictionary {
  11565. return [[[self alloc] initWithValidationFunction:NULL
  11566. rawValues:NULL
  11567. forKeys:NULL
  11568. count:0] autorelease];
  11569. }
  11570. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11571. return [[[self alloc] initWithValidationFunction:func
  11572. rawValues:NULL
  11573. forKeys:NULL
  11574. count:0] autorelease];
  11575. }
  11576. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11577. rawValue:(int32_t)rawValue
  11578. forKey:(BOOL)key {
  11579. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11580. // on to get the type correct.
  11581. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11582. rawValues:&rawValue
  11583. forKeys:&key
  11584. count:1] autorelease];
  11585. }
  11586. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11587. rawValues:(const int32_t [])values
  11588. forKeys:(const BOOL [])keys
  11589. count:(NSUInteger)count {
  11590. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11591. // on to get the type correct.
  11592. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11593. rawValues:values
  11594. forKeys:keys
  11595. count:count] autorelease];
  11596. }
  11597. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11598. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11599. // on to get the type correct.
  11600. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11601. }
  11602. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11603. capacity:(NSUInteger)numItems {
  11604. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11605. }
  11606. - (instancetype)init {
  11607. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11608. }
  11609. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11610. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11611. }
  11612. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11613. rawValues:(const int32_t [])rawValues
  11614. forKeys:(const BOOL [])keys
  11615. count:(NSUInteger)count {
  11616. self = [super init];
  11617. if (self) {
  11618. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11619. for (NSUInteger i = 0; i < count; ++i) {
  11620. int idx = keys[i] ? 1 : 0;
  11621. _values[idx] = rawValues[i];
  11622. _valueSet[idx] = YES;
  11623. }
  11624. }
  11625. return self;
  11626. }
  11627. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11628. self = [self initWithValidationFunction:dictionary.validationFunc
  11629. rawValues:NULL
  11630. forKeys:NULL
  11631. count:0];
  11632. if (self) {
  11633. if (dictionary) {
  11634. for (int i = 0; i < 2; ++i) {
  11635. if (dictionary->_valueSet[i]) {
  11636. _values[i] = dictionary->_values[i];
  11637. _valueSet[i] = YES;
  11638. }
  11639. }
  11640. }
  11641. }
  11642. return self;
  11643. }
  11644. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11645. capacity:(NSUInteger)numItems {
  11646. #pragma unused(numItems)
  11647. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11648. }
  11649. #if !defined(NS_BLOCK_ASSERTIONS)
  11650. - (void)dealloc {
  11651. NSAssert(!_autocreator,
  11652. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11653. [self class], _autocreator);
  11654. [super dealloc];
  11655. }
  11656. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11657. - (instancetype)copyWithZone:(NSZone *)zone {
  11658. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11659. }
  11660. - (BOOL)isEqual:(id)other {
  11661. if (self == other) {
  11662. return YES;
  11663. }
  11664. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11665. return NO;
  11666. }
  11667. GPBBoolEnumDictionary *otherDictionary = other;
  11668. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11669. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11670. return NO;
  11671. }
  11672. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11673. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11674. return NO;
  11675. }
  11676. return YES;
  11677. }
  11678. - (NSUInteger)hash {
  11679. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11680. }
  11681. - (NSString *)description {
  11682. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11683. if (_valueSet[0]) {
  11684. [result appendFormat:@"NO: %d", _values[0]];
  11685. }
  11686. if (_valueSet[1]) {
  11687. [result appendFormat:@"YES: %d", _values[1]];
  11688. }
  11689. [result appendString:@" }"];
  11690. return result;
  11691. }
  11692. - (NSUInteger)count {
  11693. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11694. }
  11695. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  11696. int idx = (key ? 1 : 0);
  11697. if (_valueSet[idx]) {
  11698. if (value) {
  11699. int32_t result = _values[idx];
  11700. if (!_validationFunc(result)) {
  11701. result = kGPBUnrecognizedEnumeratorValue;
  11702. }
  11703. *value = result;
  11704. }
  11705. return YES;
  11706. }
  11707. return NO;
  11708. }
  11709. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  11710. int idx = (key ? 1 : 0);
  11711. if (_valueSet[idx]) {
  11712. if (rawValue) {
  11713. *rawValue = _values[idx];
  11714. }
  11715. return YES;
  11716. }
  11717. return NO;
  11718. }
  11719. - (void)enumerateKeysAndValuesUsingBlock:
  11720. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11721. BOOL stop = NO;
  11722. if (_valueSet[0]) {
  11723. block(NO, _values[0], &stop);
  11724. }
  11725. if (!stop && _valueSet[1]) {
  11726. block(YES, _values[1], &stop);
  11727. }
  11728. }
  11729. - (void)enumerateKeysAndRawValuesUsingBlock:
  11730. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11731. BOOL stop = NO;
  11732. GPBEnumValidationFunc func = _validationFunc;
  11733. int32_t validatedValue;
  11734. if (_valueSet[0]) {
  11735. validatedValue = _values[0];
  11736. if (!func(validatedValue)) {
  11737. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11738. }
  11739. block(NO, validatedValue, &stop);
  11740. }
  11741. if (!stop && _valueSet[1]) {
  11742. validatedValue = _values[1];
  11743. if (!func(validatedValue)) {
  11744. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11745. }
  11746. block(YES, validatedValue, &stop);
  11747. }
  11748. }
  11749. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11750. // This block of code is generated, do not edit it directly.
  11751. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11752. forKey:(GPBGenericValue *)key
  11753. keyDataType:(GPBDataType)keyDataType {
  11754. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11755. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11756. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11757. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11758. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11759. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11760. [outputStream release];
  11761. return data;
  11762. }
  11763. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11764. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11765. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11766. NSUInteger count = 0;
  11767. size_t result = 0;
  11768. for (int i = 0; i < 2; ++i) {
  11769. if (_valueSet[i]) {
  11770. ++count;
  11771. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11772. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11773. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11774. }
  11775. }
  11776. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11777. result += tagSize * count;
  11778. return result;
  11779. }
  11780. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11781. asField:(GPBFieldDescriptor *)field {
  11782. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11783. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11784. for (int i = 0; i < 2; ++i) {
  11785. if (_valueSet[i]) {
  11786. // Write the tag.
  11787. [outputStream writeInt32NoTag:tag];
  11788. // Write the size of the message.
  11789. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11790. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11791. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11792. // Write the fields.
  11793. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11794. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11795. }
  11796. }
  11797. }
  11798. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11799. if (_valueSet[0]) {
  11800. block(@"false", @(_values[0]));
  11801. }
  11802. if (_valueSet[1]) {
  11803. block(@"true", @(_values[1]));
  11804. }
  11805. }
  11806. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11807. forGPBGenericValueKey:(GPBGenericValue *)key {
  11808. int idx = (key->valueBool ? 1 : 0);
  11809. _values[idx] = value->valueInt32;
  11810. _valueSet[idx] = YES;
  11811. }
  11812. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11813. if (otherDictionary) {
  11814. for (int i = 0; i < 2; ++i) {
  11815. if (otherDictionary->_valueSet[i]) {
  11816. _valueSet[i] = YES;
  11817. _values[i] = otherDictionary->_values[i];
  11818. }
  11819. }
  11820. if (_autocreator) {
  11821. GPBAutocreatedDictionaryModified(_autocreator, self);
  11822. }
  11823. }
  11824. }
  11825. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  11826. if (!_validationFunc(value)) {
  11827. [NSException raise:NSInvalidArgumentException
  11828. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11829. value];
  11830. }
  11831. int idx = (key ? 1 : 0);
  11832. _values[idx] = value;
  11833. _valueSet[idx] = YES;
  11834. if (_autocreator) {
  11835. GPBAutocreatedDictionaryModified(_autocreator, self);
  11836. }
  11837. }
  11838. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11839. int idx = (key ? 1 : 0);
  11840. _values[idx] = rawValue;
  11841. _valueSet[idx] = YES;
  11842. if (_autocreator) {
  11843. GPBAutocreatedDictionaryModified(_autocreator, self);
  11844. }
  11845. }
  11846. - (void)removeValueForKey:(BOOL)aKey {
  11847. _valueSet[aKey ? 1 : 0] = NO;
  11848. }
  11849. - (void)removeAll {
  11850. _valueSet[0] = NO;
  11851. _valueSet[1] = NO;
  11852. }
  11853. @end
  11854. #pragma mark - NSDictionary Subclass
  11855. @implementation GPBAutocreatedDictionary {
  11856. NSMutableDictionary *_dictionary;
  11857. }
  11858. - (void)dealloc {
  11859. NSAssert(!_autocreator,
  11860. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11861. [self class], _autocreator);
  11862. [_dictionary release];
  11863. [super dealloc];
  11864. }
  11865. #pragma mark Required NSDictionary overrides
  11866. - (instancetype)initWithObjects:(const id [])objects
  11867. forKeys:(const id<NSCopying> [])keys
  11868. count:(NSUInteger)count {
  11869. self = [super init];
  11870. if (self) {
  11871. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11872. forKeys:keys
  11873. count:count];
  11874. }
  11875. return self;
  11876. }
  11877. - (NSUInteger)count {
  11878. return [_dictionary count];
  11879. }
  11880. - (id)objectForKey:(id)aKey {
  11881. return [_dictionary objectForKey:aKey];
  11882. }
  11883. - (NSEnumerator *)keyEnumerator {
  11884. if (_dictionary == nil) {
  11885. _dictionary = [[NSMutableDictionary alloc] init];
  11886. }
  11887. return [_dictionary keyEnumerator];
  11888. }
  11889. #pragma mark Required NSMutableDictionary overrides
  11890. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11891. // since we only autocreate empty dictionaries.
  11892. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11893. if (_dictionary == nil) {
  11894. _dictionary = [[NSMutableDictionary alloc] init];
  11895. }
  11896. [_dictionary setObject:anObject forKey:aKey];
  11897. if (_autocreator) {
  11898. GPBAutocreatedDictionaryModified(_autocreator, self);
  11899. }
  11900. }
  11901. - (void)removeObjectForKey:(id)aKey {
  11902. [_dictionary removeObjectForKey:aKey];
  11903. }
  11904. #pragma mark Extra things hooked
  11905. - (id)copyWithZone:(NSZone *)zone {
  11906. if (_dictionary == nil) {
  11907. _dictionary = [[NSMutableDictionary alloc] init];
  11908. }
  11909. return [_dictionary copyWithZone:zone];
  11910. }
  11911. - (id)mutableCopyWithZone:(NSZone *)zone {
  11912. if (_dictionary == nil) {
  11913. _dictionary = [[NSMutableDictionary alloc] init];
  11914. }
  11915. return [_dictionary mutableCopyWithZone:zone];
  11916. }
  11917. - (id)objectForKeyedSubscript:(id)key {
  11918. return [_dictionary objectForKeyedSubscript:key];
  11919. }
  11920. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11921. if (_dictionary == nil) {
  11922. _dictionary = [[NSMutableDictionary alloc] init];
  11923. }
  11924. [_dictionary setObject:obj forKeyedSubscript:key];
  11925. if (_autocreator) {
  11926. GPBAutocreatedDictionaryModified(_autocreator, self);
  11927. }
  11928. }
  11929. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11930. id obj,
  11931. BOOL *stop))block {
  11932. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11933. }
  11934. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11935. usingBlock:(void (^)(id key,
  11936. id obj,
  11937. BOOL *stop))block {
  11938. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11939. }
  11940. @end
  11941. #pragma clang diagnostic pop