GPBDictionary.m 463 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555
  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. // Used to include code only visible to specific versions of the static
  45. // analyzer. Useful for wrapping code that only exists to silence the analyzer.
  46. // Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
  47. // END_APPLE_BUILD_VERSION using:
  48. // xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
  49. // Example usage:
  50. // #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
  51. #define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
  52. (defined(__clang_analyzer__) && \
  53. (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
  54. __apple_build_version__ <= END_APPLE_BUILD_VERSION))
  55. enum {
  56. kMapKeyFieldNumber = 1,
  57. kMapValueFieldNumber = 2,
  58. };
  59. static BOOL DictDefault_IsValidValue(int32_t value) {
  60. // Anything but the bad value marker is allowed.
  61. return (value != kGPBUnrecognizedEnumeratorValue);
  62. }
  63. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  64. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  65. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  66. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  67. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  68. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  69. //% } else {
  70. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  71. //% return 0;
  72. //% }
  73. //%}
  74. //%
  75. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  76. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  77. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  78. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  79. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  80. //% } else {
  81. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  82. //% }
  83. //%}
  84. //%
  85. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  86. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  87. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  88. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  89. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  90. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  91. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  92. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  93. //% } else {
  94. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  95. //% return 0;
  96. //% }
  97. //%}
  98. //%
  99. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  100. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  101. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  102. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  103. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  104. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  105. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  106. //% } else {
  107. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  108. //% }
  109. //%}
  110. //%
  111. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  112. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  113. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  114. //% #pragma unused(dataType) // For when asserts are off in release.
  115. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  116. //%}
  117. //%
  118. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  119. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  120. //% #pragma unused(dataType) // For when asserts are off in release.
  121. //% [stream write##VALUE_NAME##:fieldNum value:value];
  122. //%}
  123. //%
  124. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  125. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  126. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  127. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  128. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  129. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  130. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  131. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  132. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  133. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  134. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  135. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  136. // This block of code is generated, do not edit it directly.
  137. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  138. if (dataType == GPBDataTypeInt32) {
  139. return GPBComputeInt32Size(fieldNum, value);
  140. } else if (dataType == GPBDataTypeSInt32) {
  141. return GPBComputeSInt32Size(fieldNum, value);
  142. } else if (dataType == GPBDataTypeSFixed32) {
  143. return GPBComputeSFixed32Size(fieldNum, value);
  144. } else {
  145. NSCAssert(NO, @"Unexpected type %d", dataType);
  146. return 0;
  147. }
  148. }
  149. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  150. if (dataType == GPBDataTypeInt32) {
  151. [stream writeInt32:fieldNum value:value];
  152. } else if (dataType == GPBDataTypeSInt32) {
  153. [stream writeSInt32:fieldNum value:value];
  154. } else if (dataType == GPBDataTypeSFixed32) {
  155. [stream writeSFixed32:fieldNum value:value];
  156. } else {
  157. NSCAssert(NO, @"Unexpected type %d", dataType);
  158. }
  159. }
  160. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  161. if (dataType == GPBDataTypeUInt32) {
  162. return GPBComputeUInt32Size(fieldNum, value);
  163. } else if (dataType == GPBDataTypeFixed32) {
  164. return GPBComputeFixed32Size(fieldNum, value);
  165. } else {
  166. NSCAssert(NO, @"Unexpected type %d", dataType);
  167. return 0;
  168. }
  169. }
  170. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  171. if (dataType == GPBDataTypeUInt32) {
  172. [stream writeUInt32:fieldNum value:value];
  173. } else if (dataType == GPBDataTypeFixed32) {
  174. [stream writeFixed32:fieldNum value:value];
  175. } else {
  176. NSCAssert(NO, @"Unexpected type %d", dataType);
  177. }
  178. }
  179. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  180. if (dataType == GPBDataTypeInt64) {
  181. return GPBComputeInt64Size(fieldNum, value);
  182. } else if (dataType == GPBDataTypeSInt64) {
  183. return GPBComputeSInt64Size(fieldNum, value);
  184. } else if (dataType == GPBDataTypeSFixed64) {
  185. return GPBComputeSFixed64Size(fieldNum, value);
  186. } else {
  187. NSCAssert(NO, @"Unexpected type %d", dataType);
  188. return 0;
  189. }
  190. }
  191. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  192. if (dataType == GPBDataTypeInt64) {
  193. [stream writeInt64:fieldNum value:value];
  194. } else if (dataType == GPBDataTypeSInt64) {
  195. [stream writeSInt64:fieldNum value:value];
  196. } else if (dataType == GPBDataTypeSFixed64) {
  197. [stream writeSFixed64:fieldNum value:value];
  198. } else {
  199. NSCAssert(NO, @"Unexpected type %d", dataType);
  200. }
  201. }
  202. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  203. if (dataType == GPBDataTypeUInt64) {
  204. return GPBComputeUInt64Size(fieldNum, value);
  205. } else if (dataType == GPBDataTypeFixed64) {
  206. return GPBComputeFixed64Size(fieldNum, value);
  207. } else {
  208. NSCAssert(NO, @"Unexpected type %d", dataType);
  209. return 0;
  210. }
  211. }
  212. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  213. if (dataType == GPBDataTypeUInt64) {
  214. [stream writeUInt64:fieldNum value:value];
  215. } else if (dataType == GPBDataTypeFixed64) {
  216. [stream writeFixed64:fieldNum value:value];
  217. } else {
  218. NSCAssert(NO, @"Unexpected type %d", dataType);
  219. }
  220. }
  221. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  222. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  223. #pragma unused(dataType) // For when asserts are off in release.
  224. return GPBComputeBoolSize(fieldNum, value);
  225. }
  226. static void WriteDictBoolField(GPBCodedOutputStream *stream, 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. [stream writeBool:fieldNum value:value];
  230. }
  231. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  232. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  233. #pragma unused(dataType) // For when asserts are off in release.
  234. return GPBComputeEnumSize(fieldNum, value);
  235. }
  236. static void WriteDictEnumField(GPBCodedOutputStream *stream, 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. [stream writeEnum:fieldNum value:value];
  240. }
  241. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  242. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  243. #pragma unused(dataType) // For when asserts are off in release.
  244. return GPBComputeFloatSize(fieldNum, value);
  245. }
  246. static void WriteDictFloatField(GPBCodedOutputStream *stream, 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. [stream writeFloat:fieldNum value:value];
  250. }
  251. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  252. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  253. #pragma unused(dataType) // For when asserts are off in release.
  254. return GPBComputeDoubleSize(fieldNum, value);
  255. }
  256. static void WriteDictDoubleField(GPBCodedOutputStream *stream, 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. [stream writeDouble:fieldNum value:value];
  260. }
  261. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  262. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  263. #pragma unused(dataType) // For when asserts are off in release.
  264. return GPBComputeStringSize(fieldNum, value);
  265. }
  266. static void WriteDictStringField(GPBCodedOutputStream *stream, 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. [stream writeString:fieldNum value:value];
  270. }
  271. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  272. if (dataType == GPBDataTypeMessage) {
  273. return GPBComputeMessageSize(fieldNum, value);
  274. } else if (dataType == GPBDataTypeString) {
  275. return GPBComputeStringSize(fieldNum, value);
  276. } else if (dataType == GPBDataTypeBytes) {
  277. return GPBComputeBytesSize(fieldNum, value);
  278. } else {
  279. NSCAssert(NO, @"Unexpected type %d", dataType);
  280. return 0;
  281. }
  282. }
  283. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  284. if (dataType == GPBDataTypeMessage) {
  285. [stream writeMessage:fieldNum value:value];
  286. } else if (dataType == GPBDataTypeString) {
  287. [stream writeString:fieldNum value:value];
  288. } else if (dataType == GPBDataTypeBytes) {
  289. [stream writeBytes:fieldNum value:value];
  290. } else {
  291. NSCAssert(NO, @"Unexpected type %d", dataType);
  292. }
  293. }
  294. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  295. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  296. GPBDataType mapValueType = GPBGetFieldDataType(field);
  297. __block size_t result = 0;
  298. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  299. #pragma unused(stop)
  300. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  301. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  302. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  303. }];
  304. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  305. result += tagSize * dict.count;
  306. return result;
  307. }
  308. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  309. NSDictionary *dict,
  310. GPBFieldDescriptor *field) {
  311. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  312. GPBDataType mapValueType = GPBGetFieldDataType(field);
  313. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  314. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  315. #pragma unused(stop)
  316. // Write the tag.
  317. [outputStream writeInt32NoTag:tag];
  318. // Write the size of the message.
  319. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  320. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  321. // Write the size and fields.
  322. [outputStream writeInt32NoTag:(int32_t)msgSize];
  323. [outputStream writeString:kMapKeyFieldNumber value:key];
  324. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  325. }];
  326. }
  327. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  328. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  329. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  330. #pragma unused(field) // For when asserts are off in release.
  331. for (GPBMessage *msg in [dict objectEnumerator]) {
  332. if (!msg.initialized) {
  333. return NO;
  334. }
  335. }
  336. return YES;
  337. }
  338. // Note: if the type is an object, it the retain pass back to the caller.
  339. static void ReadValue(GPBCodedInputStream *stream,
  340. GPBGenericValue *valueToFill,
  341. GPBDataType type,
  342. GPBExtensionRegistry *registry,
  343. GPBFieldDescriptor *field) {
  344. switch (type) {
  345. case GPBDataTypeBool:
  346. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  347. break;
  348. case GPBDataTypeFixed32:
  349. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  350. break;
  351. case GPBDataTypeSFixed32:
  352. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  353. break;
  354. case GPBDataTypeFloat:
  355. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  356. break;
  357. case GPBDataTypeFixed64:
  358. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  359. break;
  360. case GPBDataTypeSFixed64:
  361. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  362. break;
  363. case GPBDataTypeDouble:
  364. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  365. break;
  366. case GPBDataTypeInt32:
  367. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  368. break;
  369. case GPBDataTypeInt64:
  370. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  371. break;
  372. case GPBDataTypeSInt32:
  373. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  374. break;
  375. case GPBDataTypeSInt64:
  376. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  377. break;
  378. case GPBDataTypeUInt32:
  379. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  380. break;
  381. case GPBDataTypeUInt64:
  382. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  383. break;
  384. case GPBDataTypeBytes:
  385. [valueToFill->valueData release];
  386. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  387. break;
  388. case GPBDataTypeString:
  389. [valueToFill->valueString release];
  390. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  391. break;
  392. case GPBDataTypeMessage: {
  393. GPBMessage *message = [[field.msgClass alloc] init];
  394. [stream readMessage:message extensionRegistry:registry];
  395. [valueToFill->valueMessage release];
  396. valueToFill->valueMessage = message;
  397. break;
  398. }
  399. case GPBDataTypeGroup:
  400. NSCAssert(NO, @"Can't happen");
  401. break;
  402. case GPBDataTypeEnum:
  403. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  404. break;
  405. }
  406. }
  407. void GPBDictionaryReadEntry(id mapDictionary,
  408. GPBCodedInputStream *stream,
  409. GPBExtensionRegistry *registry,
  410. GPBFieldDescriptor *field,
  411. GPBMessage *parentMessage) {
  412. GPBDataType keyDataType = field.mapKeyDataType;
  413. GPBDataType valueDataType = GPBGetFieldDataType(field);
  414. GPBGenericValue key;
  415. GPBGenericValue value;
  416. // Zero them (but pick up any enum default for proto2).
  417. key.valueString = value.valueString = nil;
  418. if (valueDataType == GPBDataTypeEnum) {
  419. value = field.defaultValue;
  420. }
  421. GPBCodedInputStreamState *state = &stream->state_;
  422. uint32_t keyTag =
  423. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  424. uint32_t valueTag =
  425. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  426. BOOL hitError = NO;
  427. while (YES) {
  428. uint32_t tag = GPBCodedInputStreamReadTag(state);
  429. if (tag == keyTag) {
  430. ReadValue(stream, &key, keyDataType, registry, field);
  431. } else if (tag == valueTag) {
  432. ReadValue(stream, &value, valueDataType, registry, field);
  433. } else if (tag == 0) {
  434. // zero signals EOF / limit reached
  435. break;
  436. } else { // Unknown
  437. if (![stream skipField:tag]){
  438. hitError = YES;
  439. break;
  440. }
  441. }
  442. }
  443. if (!hitError) {
  444. // Handle the special defaults and/or missing key/value.
  445. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  446. key.valueString = [@"" retain];
  447. }
  448. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  449. switch (valueDataType) {
  450. case GPBDataTypeString:
  451. value.valueString = [@"" retain];
  452. break;
  453. case GPBDataTypeBytes:
  454. value.valueData = [GPBEmptyNSData() retain];
  455. break;
  456. #if defined(__clang_analyzer__)
  457. case GPBDataTypeGroup:
  458. // Maps can't really have Groups as the value type, but this case is needed
  459. // so the analyzer won't report the posibility of send nil in for the value
  460. // in the NSMutableDictionary case below.
  461. #endif
  462. case GPBDataTypeMessage: {
  463. value.valueMessage = [[field.msgClass alloc] init];
  464. break;
  465. }
  466. default:
  467. // Nothing
  468. break;
  469. }
  470. }
  471. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  472. #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
  473. // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
  474. // be raised as needed for new Xcodes.
  475. //
  476. // This is only needed on a "shallow" analyze; on a "deep" analyze, the
  477. // existing code path gets this correct. In shallow, the analyzer decides
  478. // GPBDataTypeIsObject(valueDataType) is both false and true on a single
  479. // path through this function, allowing nil to be used for the
  480. // setObject:forKey:.
  481. if (value.valueString == nil) {
  482. value.valueString = [@"" retain];
  483. }
  484. #endif
  485. // mapDictionary is an NSMutableDictionary
  486. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  487. forKey:key.valueString];
  488. } else {
  489. if (valueDataType == GPBDataTypeEnum) {
  490. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  491. [field isValidEnumValue:value.valueEnum]) {
  492. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  493. } else {
  494. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  495. forKey:&key
  496. keyDataType:keyDataType];
  497. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  498. }
  499. } else {
  500. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  501. }
  502. }
  503. }
  504. if (GPBDataTypeIsObject(keyDataType)) {
  505. [key.valueString release];
  506. }
  507. if (GPBDataTypeIsObject(valueDataType)) {
  508. [value.valueString release];
  509. }
  510. }
  511. //
  512. // Macros for the common basic cases.
  513. //
  514. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  515. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  516. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  517. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  518. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  519. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  520. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  521. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  522. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  523. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  524. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  525. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  526. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  527. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, value)
  528. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  529. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, object)
  530. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME)
  531. //%#pragma mark - KEY_NAME -> VALUE_NAME
  532. //%
  533. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  534. //% @package
  535. //% NSMutableDictionary *_dictionary;
  536. //%}
  537. //%
  538. //%+ (instancetype)dictionary {
  539. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  540. //%}
  541. //%
  542. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)##VNAME
  543. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  544. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  545. //% // on to get the type correct.
  546. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  547. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  548. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  549. //%}
  550. //%
  551. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  552. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  553. //% ##VNAME$S## count:(NSUInteger)count {
  554. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  555. //% // on to get the type correct.
  556. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  557. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  558. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  559. //%}
  560. //%
  561. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  562. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  563. //% // on to get the type correct.
  564. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  565. //%}
  566. //%
  567. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  568. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  569. //%}
  570. //%
  571. //%- (instancetype)init {
  572. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  573. //%}
  574. //%
  575. //%- (instancetype)initWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  576. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  577. //% ##VNAME$S## count:(NSUInteger)count {
  578. //% self = [super init];
  579. //% if (self) {
  580. //% _dictionary = [[NSMutableDictionary alloc] init];
  581. //% if (count && VNAME##s && keys) {
  582. //% for (NSUInteger i = 0; i < count; ++i) {
  583. //%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])];
  584. //% }
  585. //% }
  586. //% }
  587. //% return self;
  588. //%}
  589. //%
  590. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  591. //% self = [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  592. //% if (self) {
  593. //% if (dictionary) {
  594. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  595. //% }
  596. //% }
  597. //% return self;
  598. //%}
  599. //%
  600. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  601. //% #pragma unused(numItems)
  602. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  603. //%}
  604. //%
  605. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  606. //%
  607. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  608. //%
  609. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, )
  610. //%
  611. //%@end
  612. //%
  613. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  614. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  615. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  616. //%#pragma mark - KEY_NAME -> VALUE_NAME
  617. //%
  618. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  619. //% @package
  620. //% NSMutableDictionary *_dictionary;
  621. //% GPBEnumValidationFunc _validationFunc;
  622. //%}
  623. //%
  624. //%@synthesize validationFunc = _validationFunc;
  625. //%
  626. //%+ (instancetype)dictionary {
  627. //% return [[[self alloc] initWithValidationFunction:NULL
  628. //% rawValues:NULL
  629. //% forKeys:NULL
  630. //% count:0] autorelease];
  631. //%}
  632. //%
  633. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  634. //% return [[[self alloc] initWithValidationFunction:func
  635. //% rawValues:NULL
  636. //% forKeys:NULL
  637. //% count:0] autorelease];
  638. //%}
  639. //%
  640. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  641. //% rawValue:(VALUE_TYPE)rawValue
  642. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  643. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  644. //% // on to get the type correct.
  645. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  646. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  647. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  648. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  649. //%}
  650. //%
  651. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  652. //% rawValues:(const VALUE_TYPE [])rawValues
  653. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  654. //% count:(NSUInteger)count {
  655. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  656. //% // on to get the type correct.
  657. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  658. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  659. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  660. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  661. //%}
  662. //%
  663. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  664. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  665. //% // on to get the type correct.
  666. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  667. //%}
  668. //%
  669. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  670. //% capacity:(NSUInteger)numItems {
  671. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  672. //%}
  673. //%
  674. //%- (instancetype)init {
  675. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  676. //%}
  677. //%
  678. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  679. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  680. //%}
  681. //%
  682. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  683. //% rawValues:(const VALUE_TYPE [])rawValues
  684. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  685. //% count:(NSUInteger)count {
  686. //% self = [super init];
  687. //% if (self) {
  688. //% _dictionary = [[NSMutableDictionary alloc] init];
  689. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  690. //% if (count && rawValues && keys) {
  691. //% for (NSUInteger i = 0; i < count; ++i) {
  692. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  693. //% }
  694. //% }
  695. //% }
  696. //% return self;
  697. //%}
  698. //%
  699. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  700. //% self = [self initWithValidationFunction:dictionary.validationFunc
  701. //% rawValues:NULL
  702. //% forKeys:NULL
  703. //% count:0];
  704. //% if (self) {
  705. //% if (dictionary) {
  706. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  707. //% }
  708. //% }
  709. //% return self;
  710. //%}
  711. //%
  712. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  713. //% capacity:(NSUInteger)numItems {
  714. //% #pragma unused(numItems)
  715. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  716. //%}
  717. //%
  718. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  719. //%
  720. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key value:(VALUE_TYPE *)value {
  721. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  722. //% if (wrapped && value) {
  723. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  724. //% if (!_validationFunc(result)) {
  725. //% result = kGPBUnrecognizedEnumeratorValue;
  726. //% }
  727. //% *value = result;
  728. //% }
  729. //% return (wrapped != NULL);
  730. //%}
  731. //%
  732. //%- (BOOL)valueForKey:(KEY_TYPE##KisP$S##KisP)key rawValue:(VALUE_TYPE *)rawValue {
  733. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  734. //% if (wrapped && rawValue) {
  735. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  736. //% }
  737. //% return (wrapped != NULL);
  738. //%}
  739. //%
  740. //%- (void)enumerateKeysAndValuesUsingBlock:
  741. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  742. //% GPBEnumValidationFunc func = _validationFunc;
  743. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  744. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  745. //% BOOL *stop) {
  746. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  747. //% if (!func(unwrapped)) {
  748. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  749. //% }
  750. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  751. //% }];
  752. //%}
  753. //%
  754. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, value, Raw)
  755. //%
  756. //%- (void)setValue:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  757. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  758. //% [NSException raise:NSInvalidArgumentException
  759. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  760. //% value];
  761. //% }
  762. //%
  763. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  764. //% if (_autocreator) {
  765. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  766. //% }
  767. //%}
  768. //%
  769. //%@end
  770. //%
  771. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  772. //%- (void)dealloc {
  773. //% NSAssert(!_autocreator,
  774. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  775. //% [self class], _autocreator);
  776. //% [_dictionary release];
  777. //% [super dealloc];
  778. //%}
  779. //%
  780. //%- (instancetype)copyWithZone:(NSZone *)zone {
  781. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  782. //%}
  783. //%
  784. //%- (BOOL)isEqual:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)other {
  785. //% if (self == other) {
  786. //% return YES;
  787. //% }
  788. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  789. //% return NO;
  790. //% }
  791. //% return [_dictionary isEqual:other->_dictionary];
  792. //%}
  793. //%
  794. //%- (NSUInteger)hash {
  795. //% return _dictionary.count;
  796. //%}
  797. //%
  798. //%- (NSString *)description {
  799. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  800. //%}
  801. //%
  802. //%- (NSUInteger)count {
  803. //% return _dictionary.count;
  804. //%}
  805. //%
  806. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:
  807. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop))block {
  808. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  809. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  810. //% BOOL *stop) {
  811. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME$u), stop);
  812. //% }];
  813. //%}
  814. //%
  815. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  816. //% NSUInteger count = _dictionary.count;
  817. //% if (count == 0) {
  818. //% return 0;
  819. //% }
  820. //%
  821. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  822. //% GPBDataType keyDataType = field.mapKeyDataType;
  823. //% __block size_t result = 0;
  824. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  825. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u##,
  826. //% BOOL *stop) {
  827. //% #pragma unused(stop)
  828. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  829. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  830. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  831. //% }];
  832. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  833. //% result += tagSize * count;
  834. //% return result;
  835. //%}
  836. //%
  837. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  838. //% asField:(GPBFieldDescriptor *)field {
  839. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  840. //% GPBDataType keyDataType = field.mapKeyDataType;
  841. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  842. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  843. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME$u,
  844. //% BOOL *stop) {
  845. //% #pragma unused(stop)
  846. //% // Write the tag.
  847. //% [outputStream writeInt32NoTag:tag];
  848. //% // Write the size of the message.
  849. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  850. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  851. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  852. //% // Write the fields.
  853. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  854. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME$u), kMapValueFieldNumber, valueDataType);
  855. //% }];
  856. //%}
  857. //%
  858. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  859. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  860. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  861. //%}
  862. //%
  863. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  864. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME$u##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME, BOOL *stop) {
  865. //% #pragma unused(stop)
  866. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME));
  867. //% }];
  868. //%}
  869. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, ACCESSOR_NAME)
  870. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  871. //% if (otherDictionary) {
  872. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  873. //% if (_autocreator) {
  874. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  875. //% }
  876. //% }
  877. //%}
  878. //%
  879. //%- (void)set##ACCESSOR_NAME##VNAME$u##:(VALUE_TYPE)VNAME forKey:(KEY_TYPE##KisP$S##KisP)key {
  880. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME) forKey:WRAPPED##KHELPER(key)];
  881. //% if (_autocreator) {
  882. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  883. //% }
  884. //%}
  885. //%
  886. //%- (void)remove##VNAME$u##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  887. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  888. //%}
  889. //%
  890. //%- (void)removeAll {
  891. //% [_dictionary removeAllObjects];
  892. //%}
  893. //
  894. // Custom Generation for Bool keys
  895. //
  896. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  897. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, value)
  898. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  899. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, object)
  900. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME)
  901. //%#pragma mark - Bool -> VALUE_NAME
  902. //%
  903. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  904. //% @package
  905. //% VALUE_TYPE _values[2];
  906. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  907. //%
  908. //%+ (instancetype)dictionary {
  909. //% return [[[self alloc] initWith##VNAME$u##s:NULL forKeys:NULL count:0] autorelease];
  910. //%}
  911. //%
  912. //%+ (instancetype)dictionaryWith##VNAME$u##:(VALUE_TYPE)VNAME
  913. //% ##VNAME$S## forKey:(BOOL)key {
  914. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  915. //% // on to get the type correct.
  916. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:&##VNAME
  917. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  918. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  919. //%}
  920. //%
  921. //%+ (instancetype)dictionaryWith##VNAME$u##s:(const VALUE_TYPE [])##VNAME##s
  922. //% ##VNAME$S## forKeys:(const BOOL [])keys
  923. //% ##VNAME$S## count:(NSUInteger)count {
  924. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME$u##s:forKeys:count:
  925. //% // on to get the type correct.
  926. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME$u##s:##VNAME##s
  927. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  928. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  929. //%}
  930. //%
  931. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  932. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  933. //% // on to get the type correct.
  934. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  935. //%}
  936. //%
  937. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  938. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  939. //%}
  940. //%
  941. //%- (instancetype)init {
  942. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  943. //%}
  944. //%
  945. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  946. //%
  947. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  948. //% #pragma unused(numItems)
  949. //% return [self initWith##VNAME$u##s:NULL forKeys:NULL count:0];
  950. //%}
  951. //%
  952. //%BOOL_DICT_DEALLOC##HELPER()
  953. //%
  954. //%- (instancetype)copyWithZone:(NSZone *)zone {
  955. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  956. //%}
  957. //%
  958. //%- (BOOL)isEqual:(GPBBool##VALUE_NAME##Dictionary *)other {
  959. //% if (self == other) {
  960. //% return YES;
  961. //% }
  962. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  963. //% return NO;
  964. //% }
  965. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, other->)) ||
  966. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, other->))) {
  967. //% return NO;
  968. //% }
  969. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], other->_values[0]))) ||
  970. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], other->_values[1])))) {
  971. //% return NO;
  972. //% }
  973. //% return YES;
  974. //%}
  975. //%
  976. //%- (NSUInteger)hash {
  977. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  978. //%}
  979. //%
  980. //%- (NSString *)description {
  981. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  982. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  983. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  984. //% }
  985. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  986. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  987. //% }
  988. //% [result appendString:@" }"];
  989. //% return result;
  990. //%}
  991. //%
  992. //%- (NSUInteger)count {
  993. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  994. //%}
  995. //%
  996. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_TYPE)
  997. //%
  998. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  999. //%
  1000. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1001. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1002. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  1003. //% }
  1004. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  1005. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  1006. //% }
  1007. //%}
  1008. //%
  1009. //%- (void)enumerateKeysAnd##VNAME$u##sUsingBlock:
  1010. //% (void (^)(BOOL key, VALUE_TYPE VNAME, BOOL *stop))block {
  1011. //% BOOL stop = NO;
  1012. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1013. //% block(NO, _values[0], &stop);
  1014. //% }
  1015. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  1016. //% block(YES, _values[1], &stop);
  1017. //% }
  1018. //%}
  1019. //%
  1020. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1021. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1022. //% NSUInteger count = 0;
  1023. //% size_t result = 0;
  1024. //% for (int i = 0; i < 2; ++i) {
  1025. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1026. //% ++count;
  1027. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1028. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1029. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1030. //% }
  1031. //% }
  1032. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1033. //% result += tagSize * count;
  1034. //% return result;
  1035. //%}
  1036. //%
  1037. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1038. //% asField:(GPBFieldDescriptor *)field {
  1039. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1040. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1041. //% for (int i = 0; i < 2; ++i) {
  1042. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1043. //% // Write the tag.
  1044. //% [outputStream writeInt32NoTag:tag];
  1045. //% // Write the size of the message.
  1046. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1047. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1048. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1049. //% // Write the fields.
  1050. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1051. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1052. //% }
  1053. //% }
  1054. //%}
  1055. //%
  1056. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1057. //%
  1058. //%@end
  1059. //%
  1060. //
  1061. // Helpers for PODs
  1062. //
  1063. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1064. //%- (BOOL)valueForKey:(KEY_TYPE)key value:(VALUE_TYPE *)value {
  1065. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1066. //% if (wrapped && value) {
  1067. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1068. //% }
  1069. //% return (wrapped != NULL);
  1070. //%}
  1071. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1072. //%@(VALUE)
  1073. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1074. //%[VALUE unsignedIntValue]
  1075. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1076. //%[VALUE intValue]
  1077. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1078. //%[VALUE unsignedLongLongValue]
  1079. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1080. //%[VALUE longLongValue]
  1081. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1082. //%[VALUE boolValue]
  1083. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1084. //%[VALUE floatValue]
  1085. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1086. //%[VALUE doubleValue]
  1087. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1088. //%[VALUE intValue]
  1089. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1090. //%[NSString stringWithFormat:@"%u", VALUE]
  1091. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1092. //%[NSString stringWithFormat:@"%d", VALUE]
  1093. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1094. //%[NSString stringWithFormat:@"%llu", VALUE]
  1095. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1096. //%[NSString stringWithFormat:@"%lld", VALUE]
  1097. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1098. //%(VALUE ? @"true" : @"false")
  1099. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1100. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1101. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1102. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1103. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1104. //%@(VALUE)
  1105. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1106. //%NSNumber *
  1107. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1108. //%VAL1 != VAL2
  1109. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1110. // Empty
  1111. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1112. // Empty
  1113. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1114. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1115. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1116. // Empty
  1117. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1118. // Empty
  1119. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1120. // Empty
  1121. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1122. // Empty
  1123. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1124. // Empty
  1125. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1126. // Empty
  1127. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1128. // Empty
  1129. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1130. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1131. //% forKey:(GPBGenericValue *)key
  1132. //% keyDataType:(GPBDataType)keyDataType {
  1133. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1134. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1135. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1136. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1137. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1138. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1139. //% [outputStream release];
  1140. //% return data;
  1141. //%}
  1142. //%
  1143. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1144. //%value##VALUE_NAME
  1145. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1146. // Empty
  1147. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1148. // Empty
  1149. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1150. //% BOOL _valueSet[2];
  1151. //%
  1152. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1153. //%- (instancetype)initWithValues:(const VALUE_TYPE [])values
  1154. //% forKeys:(const BOOL [])keys
  1155. //% count:(NSUInteger)count {
  1156. //% self = [super init];
  1157. //% if (self) {
  1158. //% for (NSUInteger i = 0; i < count; ++i) {
  1159. //% int idx = keys[i] ? 1 : 0;
  1160. //% _values[idx] = values[i];
  1161. //% _valueSet[idx] = YES;
  1162. //% }
  1163. //% }
  1164. //% return self;
  1165. //%}
  1166. //%
  1167. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1168. //% self = [self initWithValues:NULL forKeys:NULL count:0];
  1169. //% if (self) {
  1170. //% if (dictionary) {
  1171. //% for (int i = 0; i < 2; ++i) {
  1172. //% if (dictionary->_valueSet[i]) {
  1173. //% _values[i] = dictionary->_values[i];
  1174. //% _valueSet[i] = YES;
  1175. //% }
  1176. //% }
  1177. //% }
  1178. //% }
  1179. //% return self;
  1180. //%}
  1181. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1182. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1183. //%- (void)dealloc {
  1184. //% NSAssert(!_autocreator,
  1185. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1186. //% [self class], _autocreator);
  1187. //% [super dealloc];
  1188. //%}
  1189. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1190. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1191. //%BOOL_DICT_HASPOD(IDX, REF)
  1192. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1193. //%REF##_valueSet[IDX]
  1194. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_TYPE)
  1195. //%- (BOOL)valueForKey:(BOOL)key value:(VALUE_TYPE *)value {
  1196. //% int idx = (key ? 1 : 0);
  1197. //% if (_valueSet[idx]) {
  1198. //% if (value) {
  1199. //% *value = _values[idx];
  1200. //% }
  1201. //% return YES;
  1202. //% }
  1203. //% return NO;
  1204. //%}
  1205. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1206. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1207. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1208. //% int idx = (key->valueBool ? 1 : 0);
  1209. //% _values[idx] = value->value##VALUE_NAME;
  1210. //% _valueSet[idx] = YES;
  1211. //%}
  1212. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1213. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1214. //% if (otherDictionary) {
  1215. //% for (int i = 0; i < 2; ++i) {
  1216. //% if (otherDictionary->_valueSet[i]) {
  1217. //% _valueSet[i] = YES;
  1218. //% _values[i] = otherDictionary->_values[i];
  1219. //% }
  1220. //% }
  1221. //% if (_autocreator) {
  1222. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1223. //% }
  1224. //% }
  1225. //%}
  1226. //%
  1227. //%- (void)setValue:(VALUE_TYPE)value forKey:(BOOL)key {
  1228. //% int idx = (key ? 1 : 0);
  1229. //% _values[idx] = value;
  1230. //% _valueSet[idx] = YES;
  1231. //% if (_autocreator) {
  1232. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1233. //% }
  1234. //%}
  1235. //%
  1236. //%- (void)removeValueForKey:(BOOL)aKey {
  1237. //% _valueSet[aKey ? 1 : 0] = NO;
  1238. //%}
  1239. //%
  1240. //%- (void)removeAll {
  1241. //% _valueSet[0] = NO;
  1242. //% _valueSet[1] = NO;
  1243. //%}
  1244. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1245. //%STR_FORMAT_##VALUE_NAME()
  1246. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1247. //%%u
  1248. //%PDDM-DEFINE STR_FORMAT_Int32()
  1249. //%%d
  1250. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1251. //%%llu
  1252. //%PDDM-DEFINE STR_FORMAT_Int64()
  1253. //%%lld
  1254. //%PDDM-DEFINE STR_FORMAT_Bool()
  1255. //%%d
  1256. //%PDDM-DEFINE STR_FORMAT_Float()
  1257. //%%f
  1258. //%PDDM-DEFINE STR_FORMAT_Double()
  1259. //%%lf
  1260. //
  1261. // Helpers for Objects
  1262. //
  1263. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1264. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1265. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1266. //% return result;
  1267. //%}
  1268. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1269. //%VALUE
  1270. //%PDDM-DEFINE UNWRAPString(VALUE)
  1271. //%VALUE
  1272. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1273. //%VALUE
  1274. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1275. //%VALUE
  1276. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1277. //%VALUE
  1278. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1279. //%ENUM_TYPEOBJECT_##TYPE()
  1280. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1281. //%NSString *
  1282. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1283. //%id ##
  1284. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1285. //%![VAL1 isEqual:VAL2]
  1286. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1287. //%- (BOOL)isInitialized {
  1288. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1289. //% if (!msg.initialized) {
  1290. //% return NO;
  1291. //% }
  1292. //% }
  1293. //% return YES;
  1294. //%}
  1295. //%
  1296. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1297. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1298. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1299. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1300. //% GPBMessage *msg,
  1301. //% BOOL *stop) {
  1302. //% #pragma unused(stop)
  1303. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1304. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1305. //% [copiedMsg release];
  1306. //% }];
  1307. //% return newDict;
  1308. //%}
  1309. //%
  1310. //%
  1311. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1312. //%- (BOOL)isInitialized {
  1313. //% if (_values[0] && ![_values[0] isInitialized]) {
  1314. //% return NO;
  1315. //% }
  1316. //% if (_values[1] && ![_values[1] isInitialized]) {
  1317. //% return NO;
  1318. //% }
  1319. //% return YES;
  1320. //%}
  1321. //%
  1322. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1323. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1324. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1325. //% for (int i = 0; i < 2; ++i) {
  1326. //% if (_values[i] != nil) {
  1327. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1328. //% }
  1329. //% }
  1330. //% return newDict;
  1331. //%}
  1332. //%
  1333. //%
  1334. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1335. // Empty
  1336. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1337. //%valueString
  1338. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1339. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1340. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1341. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1342. //%##EXTRA_INDENT$S## }
  1343. //%
  1344. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1345. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1346. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1347. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1348. //%##EXTRA_INDENT$S## }
  1349. //%
  1350. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1351. // Empty
  1352. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1353. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1354. //% forKeys:(const BOOL [])keys
  1355. //% count:(NSUInteger)count {
  1356. //% self = [super init];
  1357. //% if (self) {
  1358. //% for (NSUInteger i = 0; i < count; ++i) {
  1359. //% if (!objects[i]) {
  1360. //% [NSException raise:NSInvalidArgumentException
  1361. //% format:@"Attempting to add nil object to a Dictionary"];
  1362. //% }
  1363. //% int idx = keys[i] ? 1 : 0;
  1364. //% [_values[idx] release];
  1365. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1366. //% }
  1367. //% }
  1368. //% return self;
  1369. //%}
  1370. //%
  1371. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1372. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1373. //% if (self) {
  1374. //% if (dictionary) {
  1375. //% _values[0] = [dictionary->_values[0] retain];
  1376. //% _values[1] = [dictionary->_values[1] retain];
  1377. //% }
  1378. //% }
  1379. //% return self;
  1380. //%}
  1381. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1382. //%- (void)dealloc {
  1383. //% NSAssert(!_autocreator,
  1384. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1385. //% [self class], _autocreator);
  1386. //% [_values[0] release];
  1387. //% [_values[1] release];
  1388. //% [super dealloc];
  1389. //%}
  1390. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1391. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1392. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1393. //%REF##_values[IDX] != nil
  1394. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_TYPE)
  1395. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1396. //% return _values[key ? 1 : 0];
  1397. //%}
  1398. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1399. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1400. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1401. //% int idx = (key->valueBool ? 1 : 0);
  1402. //% [_values[idx] release];
  1403. //% _values[idx] = [value->valueString retain];
  1404. //%}
  1405. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1406. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1407. //% if (otherDictionary) {
  1408. //% for (int i = 0; i < 2; ++i) {
  1409. //% if (otherDictionary->_values[i] != nil) {
  1410. //% [_values[i] release];
  1411. //% _values[i] = [otherDictionary->_values[i] retain];
  1412. //% }
  1413. //% }
  1414. //% if (_autocreator) {
  1415. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1416. //% }
  1417. //% }
  1418. //%}
  1419. //%
  1420. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1421. //% if (!object) {
  1422. //% [NSException raise:NSInvalidArgumentException
  1423. //% format:@"Attempting to add nil object to a Dictionary"];
  1424. //% }
  1425. //% int idx = (key ? 1 : 0);
  1426. //% [_values[idx] release];
  1427. //% _values[idx] = [object retain];
  1428. //% if (_autocreator) {
  1429. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1430. //% }
  1431. //%}
  1432. //%
  1433. //%- (void)removeObjectForKey:(BOOL)aKey {
  1434. //% int idx = (aKey ? 1 : 0);
  1435. //% [_values[idx] release];
  1436. //% _values[idx] = nil;
  1437. //%}
  1438. //%
  1439. //%- (void)removeAll {
  1440. //% for (int i = 0; i < 2; ++i) {
  1441. //% [_values[i] release];
  1442. //% _values[i] = nil;
  1443. //% }
  1444. //%}
  1445. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1446. //%%@
  1447. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1448. // This block of code is generated, do not edit it directly.
  1449. #pragma mark - UInt32 -> UInt32
  1450. @implementation GPBUInt32UInt32Dictionary {
  1451. @package
  1452. NSMutableDictionary *_dictionary;
  1453. }
  1454. + (instancetype)dictionary {
  1455. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1456. }
  1457. + (instancetype)dictionaryWithValue:(uint32_t)value
  1458. forKey:(uint32_t)key {
  1459. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1460. // on to get the type correct.
  1461. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  1462. forKeys:&key
  1463. count:1] autorelease];
  1464. }
  1465. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  1466. forKeys:(const uint32_t [])keys
  1467. count:(NSUInteger)count {
  1468. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1469. // on to get the type correct.
  1470. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithValues:values
  1471. forKeys:keys
  1472. count:count] autorelease];
  1473. }
  1474. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1475. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1476. // on to get the type correct.
  1477. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1478. }
  1479. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1480. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1481. }
  1482. - (instancetype)init {
  1483. return [self initWithValues:NULL forKeys:NULL count:0];
  1484. }
  1485. - (instancetype)initWithValues:(const uint32_t [])values
  1486. forKeys:(const uint32_t [])keys
  1487. count:(NSUInteger)count {
  1488. self = [super init];
  1489. if (self) {
  1490. _dictionary = [[NSMutableDictionary alloc] init];
  1491. if (count && values && keys) {
  1492. for (NSUInteger i = 0; i < count; ++i) {
  1493. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1494. }
  1495. }
  1496. }
  1497. return self;
  1498. }
  1499. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1500. self = [self initWithValues:NULL forKeys:NULL count:0];
  1501. if (self) {
  1502. if (dictionary) {
  1503. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1504. }
  1505. }
  1506. return self;
  1507. }
  1508. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1509. #pragma unused(numItems)
  1510. return [self initWithValues:NULL forKeys:NULL count:0];
  1511. }
  1512. - (void)dealloc {
  1513. NSAssert(!_autocreator,
  1514. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1515. [self class], _autocreator);
  1516. [_dictionary release];
  1517. [super dealloc];
  1518. }
  1519. - (instancetype)copyWithZone:(NSZone *)zone {
  1520. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1521. }
  1522. - (BOOL)isEqual:(GPBUInt32UInt32Dictionary *)other {
  1523. if (self == other) {
  1524. return YES;
  1525. }
  1526. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1527. return NO;
  1528. }
  1529. return [_dictionary isEqual:other->_dictionary];
  1530. }
  1531. - (NSUInteger)hash {
  1532. return _dictionary.count;
  1533. }
  1534. - (NSString *)description {
  1535. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1536. }
  1537. - (NSUInteger)count {
  1538. return _dictionary.count;
  1539. }
  1540. - (void)enumerateKeysAndValuesUsingBlock:
  1541. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1542. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1543. NSNumber *aValue,
  1544. BOOL *stop) {
  1545. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1546. }];
  1547. }
  1548. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1549. NSUInteger count = _dictionary.count;
  1550. if (count == 0) {
  1551. return 0;
  1552. }
  1553. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1554. GPBDataType keyDataType = field.mapKeyDataType;
  1555. __block size_t result = 0;
  1556. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1557. NSNumber *aValue,
  1558. BOOL *stop) {
  1559. #pragma unused(stop)
  1560. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1561. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1562. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1563. }];
  1564. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1565. result += tagSize * count;
  1566. return result;
  1567. }
  1568. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1569. asField:(GPBFieldDescriptor *)field {
  1570. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1571. GPBDataType keyDataType = field.mapKeyDataType;
  1572. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1573. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1574. NSNumber *aValue,
  1575. BOOL *stop) {
  1576. #pragma unused(stop)
  1577. // Write the tag.
  1578. [outputStream writeInt32NoTag:tag];
  1579. // Write the size of the message.
  1580. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1581. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1582. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1583. // Write the fields.
  1584. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1585. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1586. }];
  1587. }
  1588. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1589. forGPBGenericValueKey:(GPBGenericValue *)key {
  1590. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1591. }
  1592. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1593. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1594. #pragma unused(stop)
  1595. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1596. }];
  1597. }
  1598. - (BOOL)valueForKey:(uint32_t)key value:(uint32_t *)value {
  1599. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1600. if (wrapped && value) {
  1601. *value = [wrapped unsignedIntValue];
  1602. }
  1603. return (wrapped != NULL);
  1604. }
  1605. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1606. if (otherDictionary) {
  1607. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1608. if (_autocreator) {
  1609. GPBAutocreatedDictionaryModified(_autocreator, self);
  1610. }
  1611. }
  1612. }
  1613. - (void)setValue:(uint32_t)value forKey:(uint32_t)key {
  1614. [_dictionary setObject:@(value) forKey:@(key)];
  1615. if (_autocreator) {
  1616. GPBAutocreatedDictionaryModified(_autocreator, self);
  1617. }
  1618. }
  1619. - (void)removeValueForKey:(uint32_t)aKey {
  1620. [_dictionary removeObjectForKey:@(aKey)];
  1621. }
  1622. - (void)removeAll {
  1623. [_dictionary removeAllObjects];
  1624. }
  1625. @end
  1626. #pragma mark - UInt32 -> Int32
  1627. @implementation GPBUInt32Int32Dictionary {
  1628. @package
  1629. NSMutableDictionary *_dictionary;
  1630. }
  1631. + (instancetype)dictionary {
  1632. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1633. }
  1634. + (instancetype)dictionaryWithValue:(int32_t)value
  1635. forKey:(uint32_t)key {
  1636. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1637. // on to get the type correct.
  1638. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:&value
  1639. forKeys:&key
  1640. count:1] autorelease];
  1641. }
  1642. + (instancetype)dictionaryWithValues:(const int32_t [])values
  1643. forKeys:(const uint32_t [])keys
  1644. count:(NSUInteger)count {
  1645. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1646. // on to get the type correct.
  1647. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithValues:values
  1648. forKeys:keys
  1649. count:count] autorelease];
  1650. }
  1651. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1652. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1653. // on to get the type correct.
  1654. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1655. }
  1656. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1657. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1658. }
  1659. - (instancetype)init {
  1660. return [self initWithValues:NULL forKeys:NULL count:0];
  1661. }
  1662. - (instancetype)initWithValues:(const int32_t [])values
  1663. forKeys:(const uint32_t [])keys
  1664. count:(NSUInteger)count {
  1665. self = [super init];
  1666. if (self) {
  1667. _dictionary = [[NSMutableDictionary alloc] init];
  1668. if (count && values && keys) {
  1669. for (NSUInteger i = 0; i < count; ++i) {
  1670. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1671. }
  1672. }
  1673. }
  1674. return self;
  1675. }
  1676. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1677. self = [self initWithValues:NULL forKeys:NULL count:0];
  1678. if (self) {
  1679. if (dictionary) {
  1680. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1681. }
  1682. }
  1683. return self;
  1684. }
  1685. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1686. #pragma unused(numItems)
  1687. return [self initWithValues:NULL forKeys:NULL count:0];
  1688. }
  1689. - (void)dealloc {
  1690. NSAssert(!_autocreator,
  1691. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1692. [self class], _autocreator);
  1693. [_dictionary release];
  1694. [super dealloc];
  1695. }
  1696. - (instancetype)copyWithZone:(NSZone *)zone {
  1697. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1698. }
  1699. - (BOOL)isEqual:(GPBUInt32Int32Dictionary *)other {
  1700. if (self == other) {
  1701. return YES;
  1702. }
  1703. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1704. return NO;
  1705. }
  1706. return [_dictionary isEqual:other->_dictionary];
  1707. }
  1708. - (NSUInteger)hash {
  1709. return _dictionary.count;
  1710. }
  1711. - (NSString *)description {
  1712. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1713. }
  1714. - (NSUInteger)count {
  1715. return _dictionary.count;
  1716. }
  1717. - (void)enumerateKeysAndValuesUsingBlock:
  1718. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1719. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1720. NSNumber *aValue,
  1721. BOOL *stop) {
  1722. block([aKey unsignedIntValue], [aValue intValue], stop);
  1723. }];
  1724. }
  1725. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1726. NSUInteger count = _dictionary.count;
  1727. if (count == 0) {
  1728. return 0;
  1729. }
  1730. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1731. GPBDataType keyDataType = field.mapKeyDataType;
  1732. __block size_t result = 0;
  1733. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1734. NSNumber *aValue,
  1735. BOOL *stop) {
  1736. #pragma unused(stop)
  1737. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1738. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1739. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1740. }];
  1741. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1742. result += tagSize * count;
  1743. return result;
  1744. }
  1745. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1746. asField:(GPBFieldDescriptor *)field {
  1747. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1748. GPBDataType keyDataType = field.mapKeyDataType;
  1749. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1750. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1751. NSNumber *aValue,
  1752. BOOL *stop) {
  1753. #pragma unused(stop)
  1754. // Write the tag.
  1755. [outputStream writeInt32NoTag:tag];
  1756. // Write the size of the message.
  1757. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1758. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1759. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1760. // Write the fields.
  1761. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1762. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1763. }];
  1764. }
  1765. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1766. forGPBGenericValueKey:(GPBGenericValue *)key {
  1767. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1768. }
  1769. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1770. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1771. #pragma unused(stop)
  1772. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1773. }];
  1774. }
  1775. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  1776. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1777. if (wrapped && value) {
  1778. *value = [wrapped intValue];
  1779. }
  1780. return (wrapped != NULL);
  1781. }
  1782. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1783. if (otherDictionary) {
  1784. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1785. if (_autocreator) {
  1786. GPBAutocreatedDictionaryModified(_autocreator, self);
  1787. }
  1788. }
  1789. }
  1790. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  1791. [_dictionary setObject:@(value) forKey:@(key)];
  1792. if (_autocreator) {
  1793. GPBAutocreatedDictionaryModified(_autocreator, self);
  1794. }
  1795. }
  1796. - (void)removeValueForKey:(uint32_t)aKey {
  1797. [_dictionary removeObjectForKey:@(aKey)];
  1798. }
  1799. - (void)removeAll {
  1800. [_dictionary removeAllObjects];
  1801. }
  1802. @end
  1803. #pragma mark - UInt32 -> UInt64
  1804. @implementation GPBUInt32UInt64Dictionary {
  1805. @package
  1806. NSMutableDictionary *_dictionary;
  1807. }
  1808. + (instancetype)dictionary {
  1809. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1810. }
  1811. + (instancetype)dictionaryWithValue:(uint64_t)value
  1812. forKey:(uint32_t)key {
  1813. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1814. // on to get the type correct.
  1815. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  1816. forKeys:&key
  1817. count:1] autorelease];
  1818. }
  1819. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  1820. forKeys:(const uint32_t [])keys
  1821. count:(NSUInteger)count {
  1822. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1823. // on to get the type correct.
  1824. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithValues:values
  1825. forKeys:keys
  1826. count:count] autorelease];
  1827. }
  1828. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1829. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1830. // on to get the type correct.
  1831. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1832. }
  1833. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1834. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1835. }
  1836. - (instancetype)init {
  1837. return [self initWithValues:NULL forKeys:NULL count:0];
  1838. }
  1839. - (instancetype)initWithValues:(const uint64_t [])values
  1840. forKeys:(const uint32_t [])keys
  1841. count:(NSUInteger)count {
  1842. self = [super init];
  1843. if (self) {
  1844. _dictionary = [[NSMutableDictionary alloc] init];
  1845. if (count && values && keys) {
  1846. for (NSUInteger i = 0; i < count; ++i) {
  1847. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1848. }
  1849. }
  1850. }
  1851. return self;
  1852. }
  1853. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1854. self = [self initWithValues:NULL forKeys:NULL count:0];
  1855. if (self) {
  1856. if (dictionary) {
  1857. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1858. }
  1859. }
  1860. return self;
  1861. }
  1862. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1863. #pragma unused(numItems)
  1864. return [self initWithValues:NULL forKeys:NULL count:0];
  1865. }
  1866. - (void)dealloc {
  1867. NSAssert(!_autocreator,
  1868. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1869. [self class], _autocreator);
  1870. [_dictionary release];
  1871. [super dealloc];
  1872. }
  1873. - (instancetype)copyWithZone:(NSZone *)zone {
  1874. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1875. }
  1876. - (BOOL)isEqual:(GPBUInt32UInt64Dictionary *)other {
  1877. if (self == other) {
  1878. return YES;
  1879. }
  1880. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1881. return NO;
  1882. }
  1883. return [_dictionary isEqual:other->_dictionary];
  1884. }
  1885. - (NSUInteger)hash {
  1886. return _dictionary.count;
  1887. }
  1888. - (NSString *)description {
  1889. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1890. }
  1891. - (NSUInteger)count {
  1892. return _dictionary.count;
  1893. }
  1894. - (void)enumerateKeysAndValuesUsingBlock:
  1895. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1896. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1897. NSNumber *aValue,
  1898. BOOL *stop) {
  1899. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1900. }];
  1901. }
  1902. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1903. NSUInteger count = _dictionary.count;
  1904. if (count == 0) {
  1905. return 0;
  1906. }
  1907. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1908. GPBDataType keyDataType = field.mapKeyDataType;
  1909. __block size_t result = 0;
  1910. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1911. NSNumber *aValue,
  1912. BOOL *stop) {
  1913. #pragma unused(stop)
  1914. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1915. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1916. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1917. }];
  1918. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1919. result += tagSize * count;
  1920. return result;
  1921. }
  1922. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1923. asField:(GPBFieldDescriptor *)field {
  1924. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1925. GPBDataType keyDataType = field.mapKeyDataType;
  1926. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1927. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1928. NSNumber *aValue,
  1929. BOOL *stop) {
  1930. #pragma unused(stop)
  1931. // Write the tag.
  1932. [outputStream writeInt32NoTag:tag];
  1933. // Write the size of the message.
  1934. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1935. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1936. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1937. // Write the fields.
  1938. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1939. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1940. }];
  1941. }
  1942. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1943. forGPBGenericValueKey:(GPBGenericValue *)key {
  1944. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1945. }
  1946. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1947. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1948. #pragma unused(stop)
  1949. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1950. }];
  1951. }
  1952. - (BOOL)valueForKey:(uint32_t)key value:(uint64_t *)value {
  1953. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1954. if (wrapped && value) {
  1955. *value = [wrapped unsignedLongLongValue];
  1956. }
  1957. return (wrapped != NULL);
  1958. }
  1959. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1960. if (otherDictionary) {
  1961. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1962. if (_autocreator) {
  1963. GPBAutocreatedDictionaryModified(_autocreator, self);
  1964. }
  1965. }
  1966. }
  1967. - (void)setValue:(uint64_t)value forKey:(uint32_t)key {
  1968. [_dictionary setObject:@(value) forKey:@(key)];
  1969. if (_autocreator) {
  1970. GPBAutocreatedDictionaryModified(_autocreator, self);
  1971. }
  1972. }
  1973. - (void)removeValueForKey:(uint32_t)aKey {
  1974. [_dictionary removeObjectForKey:@(aKey)];
  1975. }
  1976. - (void)removeAll {
  1977. [_dictionary removeAllObjects];
  1978. }
  1979. @end
  1980. #pragma mark - UInt32 -> Int64
  1981. @implementation GPBUInt32Int64Dictionary {
  1982. @package
  1983. NSMutableDictionary *_dictionary;
  1984. }
  1985. + (instancetype)dictionary {
  1986. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  1987. }
  1988. + (instancetype)dictionaryWithValue:(int64_t)value
  1989. forKey:(uint32_t)key {
  1990. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  1991. // on to get the type correct.
  1992. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:&value
  1993. forKeys:&key
  1994. count:1] autorelease];
  1995. }
  1996. + (instancetype)dictionaryWithValues:(const int64_t [])values
  1997. forKeys:(const uint32_t [])keys
  1998. count:(NSUInteger)count {
  1999. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2000. // on to get the type correct.
  2001. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithValues:values
  2002. forKeys:keys
  2003. count:count] autorelease];
  2004. }
  2005. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2006. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2007. // on to get the type correct.
  2008. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2009. }
  2010. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2011. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2012. }
  2013. - (instancetype)init {
  2014. return [self initWithValues:NULL forKeys:NULL count:0];
  2015. }
  2016. - (instancetype)initWithValues:(const int64_t [])values
  2017. forKeys:(const uint32_t [])keys
  2018. count:(NSUInteger)count {
  2019. self = [super init];
  2020. if (self) {
  2021. _dictionary = [[NSMutableDictionary alloc] init];
  2022. if (count && values && keys) {
  2023. for (NSUInteger i = 0; i < count; ++i) {
  2024. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2025. }
  2026. }
  2027. }
  2028. return self;
  2029. }
  2030. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2031. self = [self initWithValues:NULL forKeys:NULL count:0];
  2032. if (self) {
  2033. if (dictionary) {
  2034. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2035. }
  2036. }
  2037. return self;
  2038. }
  2039. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2040. #pragma unused(numItems)
  2041. return [self initWithValues:NULL forKeys:NULL count:0];
  2042. }
  2043. - (void)dealloc {
  2044. NSAssert(!_autocreator,
  2045. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2046. [self class], _autocreator);
  2047. [_dictionary release];
  2048. [super dealloc];
  2049. }
  2050. - (instancetype)copyWithZone:(NSZone *)zone {
  2051. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2052. }
  2053. - (BOOL)isEqual:(GPBUInt32Int64Dictionary *)other {
  2054. if (self == other) {
  2055. return YES;
  2056. }
  2057. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2058. return NO;
  2059. }
  2060. return [_dictionary isEqual:other->_dictionary];
  2061. }
  2062. - (NSUInteger)hash {
  2063. return _dictionary.count;
  2064. }
  2065. - (NSString *)description {
  2066. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2067. }
  2068. - (NSUInteger)count {
  2069. return _dictionary.count;
  2070. }
  2071. - (void)enumerateKeysAndValuesUsingBlock:
  2072. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2073. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2074. NSNumber *aValue,
  2075. BOOL *stop) {
  2076. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2077. }];
  2078. }
  2079. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2080. NSUInteger count = _dictionary.count;
  2081. if (count == 0) {
  2082. return 0;
  2083. }
  2084. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2085. GPBDataType keyDataType = field.mapKeyDataType;
  2086. __block size_t result = 0;
  2087. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2088. NSNumber *aValue,
  2089. BOOL *stop) {
  2090. #pragma unused(stop)
  2091. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2092. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2093. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2094. }];
  2095. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2096. result += tagSize * count;
  2097. return result;
  2098. }
  2099. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2100. asField:(GPBFieldDescriptor *)field {
  2101. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2102. GPBDataType keyDataType = field.mapKeyDataType;
  2103. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2104. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2105. NSNumber *aValue,
  2106. BOOL *stop) {
  2107. #pragma unused(stop)
  2108. // Write the tag.
  2109. [outputStream writeInt32NoTag:tag];
  2110. // Write the size of the message.
  2111. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2112. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2113. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2114. // Write the fields.
  2115. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2116. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2117. }];
  2118. }
  2119. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2120. forGPBGenericValueKey:(GPBGenericValue *)key {
  2121. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2122. }
  2123. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2124. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2125. #pragma unused(stop)
  2126. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2127. }];
  2128. }
  2129. - (BOOL)valueForKey:(uint32_t)key value:(int64_t *)value {
  2130. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2131. if (wrapped && value) {
  2132. *value = [wrapped longLongValue];
  2133. }
  2134. return (wrapped != NULL);
  2135. }
  2136. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2137. if (otherDictionary) {
  2138. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2139. if (_autocreator) {
  2140. GPBAutocreatedDictionaryModified(_autocreator, self);
  2141. }
  2142. }
  2143. }
  2144. - (void)setValue:(int64_t)value forKey:(uint32_t)key {
  2145. [_dictionary setObject:@(value) forKey:@(key)];
  2146. if (_autocreator) {
  2147. GPBAutocreatedDictionaryModified(_autocreator, self);
  2148. }
  2149. }
  2150. - (void)removeValueForKey:(uint32_t)aKey {
  2151. [_dictionary removeObjectForKey:@(aKey)];
  2152. }
  2153. - (void)removeAll {
  2154. [_dictionary removeAllObjects];
  2155. }
  2156. @end
  2157. #pragma mark - UInt32 -> Bool
  2158. @implementation GPBUInt32BoolDictionary {
  2159. @package
  2160. NSMutableDictionary *_dictionary;
  2161. }
  2162. + (instancetype)dictionary {
  2163. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2164. }
  2165. + (instancetype)dictionaryWithValue:(BOOL)value
  2166. forKey:(uint32_t)key {
  2167. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2168. // on to get the type correct.
  2169. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:&value
  2170. forKeys:&key
  2171. count:1] autorelease];
  2172. }
  2173. + (instancetype)dictionaryWithValues:(const BOOL [])values
  2174. forKeys:(const uint32_t [])keys
  2175. count:(NSUInteger)count {
  2176. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2177. // on to get the type correct.
  2178. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithValues:values
  2179. forKeys:keys
  2180. count:count] autorelease];
  2181. }
  2182. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2183. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2184. // on to get the type correct.
  2185. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2186. }
  2187. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2188. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2189. }
  2190. - (instancetype)init {
  2191. return [self initWithValues:NULL forKeys:NULL count:0];
  2192. }
  2193. - (instancetype)initWithValues:(const BOOL [])values
  2194. forKeys:(const uint32_t [])keys
  2195. count:(NSUInteger)count {
  2196. self = [super init];
  2197. if (self) {
  2198. _dictionary = [[NSMutableDictionary alloc] init];
  2199. if (count && values && keys) {
  2200. for (NSUInteger i = 0; i < count; ++i) {
  2201. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2202. }
  2203. }
  2204. }
  2205. return self;
  2206. }
  2207. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2208. self = [self initWithValues:NULL forKeys:NULL count:0];
  2209. if (self) {
  2210. if (dictionary) {
  2211. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2212. }
  2213. }
  2214. return self;
  2215. }
  2216. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2217. #pragma unused(numItems)
  2218. return [self initWithValues:NULL forKeys:NULL count:0];
  2219. }
  2220. - (void)dealloc {
  2221. NSAssert(!_autocreator,
  2222. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2223. [self class], _autocreator);
  2224. [_dictionary release];
  2225. [super dealloc];
  2226. }
  2227. - (instancetype)copyWithZone:(NSZone *)zone {
  2228. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2229. }
  2230. - (BOOL)isEqual:(GPBUInt32BoolDictionary *)other {
  2231. if (self == other) {
  2232. return YES;
  2233. }
  2234. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2235. return NO;
  2236. }
  2237. return [_dictionary isEqual:other->_dictionary];
  2238. }
  2239. - (NSUInteger)hash {
  2240. return _dictionary.count;
  2241. }
  2242. - (NSString *)description {
  2243. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2244. }
  2245. - (NSUInteger)count {
  2246. return _dictionary.count;
  2247. }
  2248. - (void)enumerateKeysAndValuesUsingBlock:
  2249. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2250. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2251. NSNumber *aValue,
  2252. BOOL *stop) {
  2253. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2254. }];
  2255. }
  2256. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2257. NSUInteger count = _dictionary.count;
  2258. if (count == 0) {
  2259. return 0;
  2260. }
  2261. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2262. GPBDataType keyDataType = field.mapKeyDataType;
  2263. __block size_t result = 0;
  2264. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2265. NSNumber *aValue,
  2266. BOOL *stop) {
  2267. #pragma unused(stop)
  2268. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2269. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2270. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2271. }];
  2272. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2273. result += tagSize * count;
  2274. return result;
  2275. }
  2276. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2277. asField:(GPBFieldDescriptor *)field {
  2278. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2279. GPBDataType keyDataType = field.mapKeyDataType;
  2280. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2281. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2282. NSNumber *aValue,
  2283. BOOL *stop) {
  2284. #pragma unused(stop)
  2285. // Write the tag.
  2286. [outputStream writeInt32NoTag:tag];
  2287. // Write the size of the message.
  2288. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2289. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2290. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2291. // Write the fields.
  2292. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2293. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2294. }];
  2295. }
  2296. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2297. forGPBGenericValueKey:(GPBGenericValue *)key {
  2298. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2299. }
  2300. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2301. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2302. #pragma unused(stop)
  2303. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2304. }];
  2305. }
  2306. - (BOOL)valueForKey:(uint32_t)key value:(BOOL *)value {
  2307. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2308. if (wrapped && value) {
  2309. *value = [wrapped boolValue];
  2310. }
  2311. return (wrapped != NULL);
  2312. }
  2313. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2314. if (otherDictionary) {
  2315. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2316. if (_autocreator) {
  2317. GPBAutocreatedDictionaryModified(_autocreator, self);
  2318. }
  2319. }
  2320. }
  2321. - (void)setValue:(BOOL)value forKey:(uint32_t)key {
  2322. [_dictionary setObject:@(value) forKey:@(key)];
  2323. if (_autocreator) {
  2324. GPBAutocreatedDictionaryModified(_autocreator, self);
  2325. }
  2326. }
  2327. - (void)removeValueForKey:(uint32_t)aKey {
  2328. [_dictionary removeObjectForKey:@(aKey)];
  2329. }
  2330. - (void)removeAll {
  2331. [_dictionary removeAllObjects];
  2332. }
  2333. @end
  2334. #pragma mark - UInt32 -> Float
  2335. @implementation GPBUInt32FloatDictionary {
  2336. @package
  2337. NSMutableDictionary *_dictionary;
  2338. }
  2339. + (instancetype)dictionary {
  2340. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2341. }
  2342. + (instancetype)dictionaryWithValue:(float)value
  2343. forKey:(uint32_t)key {
  2344. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2345. // on to get the type correct.
  2346. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:&value
  2347. forKeys:&key
  2348. count:1] autorelease];
  2349. }
  2350. + (instancetype)dictionaryWithValues:(const float [])values
  2351. forKeys:(const uint32_t [])keys
  2352. count:(NSUInteger)count {
  2353. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2354. // on to get the type correct.
  2355. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithValues:values
  2356. forKeys:keys
  2357. count:count] autorelease];
  2358. }
  2359. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2360. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2361. // on to get the type correct.
  2362. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2363. }
  2364. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2365. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2366. }
  2367. - (instancetype)init {
  2368. return [self initWithValues:NULL forKeys:NULL count:0];
  2369. }
  2370. - (instancetype)initWithValues:(const float [])values
  2371. forKeys:(const uint32_t [])keys
  2372. count:(NSUInteger)count {
  2373. self = [super init];
  2374. if (self) {
  2375. _dictionary = [[NSMutableDictionary alloc] init];
  2376. if (count && values && keys) {
  2377. for (NSUInteger i = 0; i < count; ++i) {
  2378. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2379. }
  2380. }
  2381. }
  2382. return self;
  2383. }
  2384. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2385. self = [self initWithValues:NULL forKeys:NULL count:0];
  2386. if (self) {
  2387. if (dictionary) {
  2388. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2389. }
  2390. }
  2391. return self;
  2392. }
  2393. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2394. #pragma unused(numItems)
  2395. return [self initWithValues:NULL forKeys:NULL count:0];
  2396. }
  2397. - (void)dealloc {
  2398. NSAssert(!_autocreator,
  2399. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2400. [self class], _autocreator);
  2401. [_dictionary release];
  2402. [super dealloc];
  2403. }
  2404. - (instancetype)copyWithZone:(NSZone *)zone {
  2405. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2406. }
  2407. - (BOOL)isEqual:(GPBUInt32FloatDictionary *)other {
  2408. if (self == other) {
  2409. return YES;
  2410. }
  2411. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2412. return NO;
  2413. }
  2414. return [_dictionary isEqual:other->_dictionary];
  2415. }
  2416. - (NSUInteger)hash {
  2417. return _dictionary.count;
  2418. }
  2419. - (NSString *)description {
  2420. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2421. }
  2422. - (NSUInteger)count {
  2423. return _dictionary.count;
  2424. }
  2425. - (void)enumerateKeysAndValuesUsingBlock:
  2426. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2427. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2428. NSNumber *aValue,
  2429. BOOL *stop) {
  2430. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2431. }];
  2432. }
  2433. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2434. NSUInteger count = _dictionary.count;
  2435. if (count == 0) {
  2436. return 0;
  2437. }
  2438. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2439. GPBDataType keyDataType = field.mapKeyDataType;
  2440. __block size_t result = 0;
  2441. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2442. NSNumber *aValue,
  2443. BOOL *stop) {
  2444. #pragma unused(stop)
  2445. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2446. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2447. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2448. }];
  2449. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2450. result += tagSize * count;
  2451. return result;
  2452. }
  2453. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2454. asField:(GPBFieldDescriptor *)field {
  2455. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2456. GPBDataType keyDataType = field.mapKeyDataType;
  2457. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2458. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2459. NSNumber *aValue,
  2460. BOOL *stop) {
  2461. #pragma unused(stop)
  2462. // Write the tag.
  2463. [outputStream writeInt32NoTag:tag];
  2464. // Write the size of the message.
  2465. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2466. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2467. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2468. // Write the fields.
  2469. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2470. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2471. }];
  2472. }
  2473. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2474. forGPBGenericValueKey:(GPBGenericValue *)key {
  2475. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2476. }
  2477. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2478. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2479. #pragma unused(stop)
  2480. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2481. }];
  2482. }
  2483. - (BOOL)valueForKey:(uint32_t)key value:(float *)value {
  2484. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2485. if (wrapped && value) {
  2486. *value = [wrapped floatValue];
  2487. }
  2488. return (wrapped != NULL);
  2489. }
  2490. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2491. if (otherDictionary) {
  2492. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2493. if (_autocreator) {
  2494. GPBAutocreatedDictionaryModified(_autocreator, self);
  2495. }
  2496. }
  2497. }
  2498. - (void)setValue:(float)value forKey:(uint32_t)key {
  2499. [_dictionary setObject:@(value) forKey:@(key)];
  2500. if (_autocreator) {
  2501. GPBAutocreatedDictionaryModified(_autocreator, self);
  2502. }
  2503. }
  2504. - (void)removeValueForKey:(uint32_t)aKey {
  2505. [_dictionary removeObjectForKey:@(aKey)];
  2506. }
  2507. - (void)removeAll {
  2508. [_dictionary removeAllObjects];
  2509. }
  2510. @end
  2511. #pragma mark - UInt32 -> Double
  2512. @implementation GPBUInt32DoubleDictionary {
  2513. @package
  2514. NSMutableDictionary *_dictionary;
  2515. }
  2516. + (instancetype)dictionary {
  2517. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  2518. }
  2519. + (instancetype)dictionaryWithValue:(double)value
  2520. forKey:(uint32_t)key {
  2521. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2522. // on to get the type correct.
  2523. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:&value
  2524. forKeys:&key
  2525. count:1] autorelease];
  2526. }
  2527. + (instancetype)dictionaryWithValues:(const double [])values
  2528. forKeys:(const uint32_t [])keys
  2529. count:(NSUInteger)count {
  2530. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2531. // on to get the type correct.
  2532. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithValues:values
  2533. forKeys:keys
  2534. count:count] autorelease];
  2535. }
  2536. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2537. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2538. // on to get the type correct.
  2539. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2540. }
  2541. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2542. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2543. }
  2544. - (instancetype)init {
  2545. return [self initWithValues:NULL forKeys:NULL count:0];
  2546. }
  2547. - (instancetype)initWithValues:(const double [])values
  2548. forKeys:(const uint32_t [])keys
  2549. count:(NSUInteger)count {
  2550. self = [super init];
  2551. if (self) {
  2552. _dictionary = [[NSMutableDictionary alloc] init];
  2553. if (count && values && keys) {
  2554. for (NSUInteger i = 0; i < count; ++i) {
  2555. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2556. }
  2557. }
  2558. }
  2559. return self;
  2560. }
  2561. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2562. self = [self initWithValues:NULL forKeys:NULL count:0];
  2563. if (self) {
  2564. if (dictionary) {
  2565. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2566. }
  2567. }
  2568. return self;
  2569. }
  2570. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2571. #pragma unused(numItems)
  2572. return [self initWithValues:NULL forKeys:NULL count:0];
  2573. }
  2574. - (void)dealloc {
  2575. NSAssert(!_autocreator,
  2576. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2577. [self class], _autocreator);
  2578. [_dictionary release];
  2579. [super dealloc];
  2580. }
  2581. - (instancetype)copyWithZone:(NSZone *)zone {
  2582. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2583. }
  2584. - (BOOL)isEqual:(GPBUInt32DoubleDictionary *)other {
  2585. if (self == other) {
  2586. return YES;
  2587. }
  2588. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2589. return NO;
  2590. }
  2591. return [_dictionary isEqual:other->_dictionary];
  2592. }
  2593. - (NSUInteger)hash {
  2594. return _dictionary.count;
  2595. }
  2596. - (NSString *)description {
  2597. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2598. }
  2599. - (NSUInteger)count {
  2600. return _dictionary.count;
  2601. }
  2602. - (void)enumerateKeysAndValuesUsingBlock:
  2603. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2604. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2605. NSNumber *aValue,
  2606. BOOL *stop) {
  2607. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2608. }];
  2609. }
  2610. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2611. NSUInteger count = _dictionary.count;
  2612. if (count == 0) {
  2613. return 0;
  2614. }
  2615. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2616. GPBDataType keyDataType = field.mapKeyDataType;
  2617. __block size_t result = 0;
  2618. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2619. NSNumber *aValue,
  2620. BOOL *stop) {
  2621. #pragma unused(stop)
  2622. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2623. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2624. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2625. }];
  2626. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2627. result += tagSize * count;
  2628. return result;
  2629. }
  2630. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2631. asField:(GPBFieldDescriptor *)field {
  2632. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2633. GPBDataType keyDataType = field.mapKeyDataType;
  2634. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2635. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2636. NSNumber *aValue,
  2637. BOOL *stop) {
  2638. #pragma unused(stop)
  2639. // Write the tag.
  2640. [outputStream writeInt32NoTag:tag];
  2641. // Write the size of the message.
  2642. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2643. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2644. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2645. // Write the fields.
  2646. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2647. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2648. }];
  2649. }
  2650. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2651. forGPBGenericValueKey:(GPBGenericValue *)key {
  2652. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2653. }
  2654. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2655. [self enumerateKeysAndValuesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2656. #pragma unused(stop)
  2657. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2658. }];
  2659. }
  2660. - (BOOL)valueForKey:(uint32_t)key value:(double *)value {
  2661. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2662. if (wrapped && value) {
  2663. *value = [wrapped doubleValue];
  2664. }
  2665. return (wrapped != NULL);
  2666. }
  2667. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2668. if (otherDictionary) {
  2669. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2670. if (_autocreator) {
  2671. GPBAutocreatedDictionaryModified(_autocreator, self);
  2672. }
  2673. }
  2674. }
  2675. - (void)setValue:(double)value forKey:(uint32_t)key {
  2676. [_dictionary setObject:@(value) forKey:@(key)];
  2677. if (_autocreator) {
  2678. GPBAutocreatedDictionaryModified(_autocreator, self);
  2679. }
  2680. }
  2681. - (void)removeValueForKey:(uint32_t)aKey {
  2682. [_dictionary removeObjectForKey:@(aKey)];
  2683. }
  2684. - (void)removeAll {
  2685. [_dictionary removeAllObjects];
  2686. }
  2687. @end
  2688. #pragma mark - UInt32 -> Enum
  2689. @implementation GPBUInt32EnumDictionary {
  2690. @package
  2691. NSMutableDictionary *_dictionary;
  2692. GPBEnumValidationFunc _validationFunc;
  2693. }
  2694. @synthesize validationFunc = _validationFunc;
  2695. + (instancetype)dictionary {
  2696. return [[[self alloc] initWithValidationFunction:NULL
  2697. rawValues:NULL
  2698. forKeys:NULL
  2699. count:0] autorelease];
  2700. }
  2701. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2702. return [[[self alloc] initWithValidationFunction:func
  2703. rawValues:NULL
  2704. forKeys:NULL
  2705. count:0] autorelease];
  2706. }
  2707. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2708. rawValue:(int32_t)rawValue
  2709. forKey:(uint32_t)key {
  2710. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2711. // on to get the type correct.
  2712. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2713. rawValues:&rawValue
  2714. forKeys:&key
  2715. count:1] autorelease];
  2716. }
  2717. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2718. rawValues:(const int32_t [])rawValues
  2719. forKeys:(const uint32_t [])keys
  2720. count:(NSUInteger)count {
  2721. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2722. // on to get the type correct.
  2723. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2724. rawValues:rawValues
  2725. forKeys:keys
  2726. count:count] autorelease];
  2727. }
  2728. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2729. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2730. // on to get the type correct.
  2731. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2732. }
  2733. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2734. capacity:(NSUInteger)numItems {
  2735. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2736. }
  2737. - (instancetype)init {
  2738. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2739. }
  2740. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2741. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2742. }
  2743. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2744. rawValues:(const int32_t [])rawValues
  2745. forKeys:(const uint32_t [])keys
  2746. count:(NSUInteger)count {
  2747. self = [super init];
  2748. if (self) {
  2749. _dictionary = [[NSMutableDictionary alloc] init];
  2750. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2751. if (count && rawValues && keys) {
  2752. for (NSUInteger i = 0; i < count; ++i) {
  2753. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2754. }
  2755. }
  2756. }
  2757. return self;
  2758. }
  2759. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2760. self = [self initWithValidationFunction:dictionary.validationFunc
  2761. rawValues:NULL
  2762. forKeys:NULL
  2763. count:0];
  2764. if (self) {
  2765. if (dictionary) {
  2766. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2767. }
  2768. }
  2769. return self;
  2770. }
  2771. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2772. capacity:(NSUInteger)numItems {
  2773. #pragma unused(numItems)
  2774. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2775. }
  2776. - (void)dealloc {
  2777. NSAssert(!_autocreator,
  2778. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2779. [self class], _autocreator);
  2780. [_dictionary release];
  2781. [super dealloc];
  2782. }
  2783. - (instancetype)copyWithZone:(NSZone *)zone {
  2784. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2785. }
  2786. - (BOOL)isEqual:(GPBUInt32EnumDictionary *)other {
  2787. if (self == other) {
  2788. return YES;
  2789. }
  2790. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2791. return NO;
  2792. }
  2793. return [_dictionary isEqual:other->_dictionary];
  2794. }
  2795. - (NSUInteger)hash {
  2796. return _dictionary.count;
  2797. }
  2798. - (NSString *)description {
  2799. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2800. }
  2801. - (NSUInteger)count {
  2802. return _dictionary.count;
  2803. }
  2804. - (void)enumerateKeysAndRawValuesUsingBlock:
  2805. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2806. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2807. NSNumber *aValue,
  2808. BOOL *stop) {
  2809. block([aKey unsignedIntValue], [aValue intValue], stop);
  2810. }];
  2811. }
  2812. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2813. NSUInteger count = _dictionary.count;
  2814. if (count == 0) {
  2815. return 0;
  2816. }
  2817. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2818. GPBDataType keyDataType = field.mapKeyDataType;
  2819. __block size_t result = 0;
  2820. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2821. NSNumber *aValue,
  2822. BOOL *stop) {
  2823. #pragma unused(stop)
  2824. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2825. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2826. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2827. }];
  2828. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2829. result += tagSize * count;
  2830. return result;
  2831. }
  2832. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2833. asField:(GPBFieldDescriptor *)field {
  2834. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2835. GPBDataType keyDataType = field.mapKeyDataType;
  2836. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2837. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2838. NSNumber *aValue,
  2839. BOOL *stop) {
  2840. #pragma unused(stop)
  2841. // Write the tag.
  2842. [outputStream writeInt32NoTag:tag];
  2843. // Write the size of the message.
  2844. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2845. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2846. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2847. // Write the fields.
  2848. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2849. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2850. }];
  2851. }
  2852. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2853. forKey:(GPBGenericValue *)key
  2854. keyDataType:(GPBDataType)keyDataType {
  2855. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2856. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2857. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2858. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2859. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2860. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2861. [outputStream release];
  2862. return data;
  2863. }
  2864. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2865. forGPBGenericValueKey:(GPBGenericValue *)key {
  2866. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2867. }
  2868. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2869. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2870. #pragma unused(stop)
  2871. block([NSString stringWithFormat:@"%u", key], @(value));
  2872. }];
  2873. }
  2874. - (BOOL)valueForKey:(uint32_t)key value:(int32_t *)value {
  2875. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2876. if (wrapped && value) {
  2877. int32_t result = [wrapped intValue];
  2878. if (!_validationFunc(result)) {
  2879. result = kGPBUnrecognizedEnumeratorValue;
  2880. }
  2881. *value = result;
  2882. }
  2883. return (wrapped != NULL);
  2884. }
  2885. - (BOOL)valueForKey:(uint32_t)key rawValue:(int32_t *)rawValue {
  2886. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2887. if (wrapped && rawValue) {
  2888. *rawValue = [wrapped intValue];
  2889. }
  2890. return (wrapped != NULL);
  2891. }
  2892. - (void)enumerateKeysAndValuesUsingBlock:
  2893. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2894. GPBEnumValidationFunc func = _validationFunc;
  2895. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2896. NSNumber *aValue,
  2897. BOOL *stop) {
  2898. int32_t unwrapped = [aValue intValue];
  2899. if (!func(unwrapped)) {
  2900. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2901. }
  2902. block([aKey unsignedIntValue], unwrapped, stop);
  2903. }];
  2904. }
  2905. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2906. if (otherDictionary) {
  2907. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2908. if (_autocreator) {
  2909. GPBAutocreatedDictionaryModified(_autocreator, self);
  2910. }
  2911. }
  2912. }
  2913. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2914. [_dictionary setObject:@(value) forKey:@(key)];
  2915. if (_autocreator) {
  2916. GPBAutocreatedDictionaryModified(_autocreator, self);
  2917. }
  2918. }
  2919. - (void)removeValueForKey:(uint32_t)aKey {
  2920. [_dictionary removeObjectForKey:@(aKey)];
  2921. }
  2922. - (void)removeAll {
  2923. [_dictionary removeAllObjects];
  2924. }
  2925. - (void)setValue:(int32_t)value forKey:(uint32_t)key {
  2926. if (!_validationFunc(value)) {
  2927. [NSException raise:NSInvalidArgumentException
  2928. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2929. value];
  2930. }
  2931. [_dictionary setObject:@(value) forKey:@(key)];
  2932. if (_autocreator) {
  2933. GPBAutocreatedDictionaryModified(_autocreator, self);
  2934. }
  2935. }
  2936. @end
  2937. #pragma mark - UInt32 -> Object
  2938. @implementation GPBUInt32ObjectDictionary {
  2939. @package
  2940. NSMutableDictionary *_dictionary;
  2941. }
  2942. + (instancetype)dictionary {
  2943. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  2944. }
  2945. + (instancetype)dictionaryWithObject:(id)object
  2946. forKey:(uint32_t)key {
  2947. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2948. // on to get the type correct.
  2949. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  2950. forKeys:&key
  2951. count:1] autorelease];
  2952. }
  2953. + (instancetype)dictionaryWithObjects:(const id [])objects
  2954. forKeys:(const uint32_t [])keys
  2955. count:(NSUInteger)count {
  2956. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2957. // on to get the type correct.
  2958. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  2959. forKeys:keys
  2960. count:count] autorelease];
  2961. }
  2962. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2963. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2964. // on to get the type correct.
  2965. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2966. }
  2967. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2968. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2969. }
  2970. - (instancetype)init {
  2971. return [self initWithObjects:NULL forKeys:NULL count:0];
  2972. }
  2973. - (instancetype)initWithObjects:(const id [])objects
  2974. forKeys:(const uint32_t [])keys
  2975. count:(NSUInteger)count {
  2976. self = [super init];
  2977. if (self) {
  2978. _dictionary = [[NSMutableDictionary alloc] init];
  2979. if (count && objects && keys) {
  2980. for (NSUInteger i = 0; i < count; ++i) {
  2981. if (!objects[i]) {
  2982. [NSException raise:NSInvalidArgumentException
  2983. format:@"Attempting to add nil object to a Dictionary"];
  2984. }
  2985. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  2986. }
  2987. }
  2988. }
  2989. return self;
  2990. }
  2991. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2992. self = [self initWithObjects:NULL forKeys:NULL count:0];
  2993. if (self) {
  2994. if (dictionary) {
  2995. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2996. }
  2997. }
  2998. return self;
  2999. }
  3000. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3001. #pragma unused(numItems)
  3002. return [self initWithObjects:NULL forKeys:NULL count:0];
  3003. }
  3004. - (void)dealloc {
  3005. NSAssert(!_autocreator,
  3006. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3007. [self class], _autocreator);
  3008. [_dictionary release];
  3009. [super dealloc];
  3010. }
  3011. - (instancetype)copyWithZone:(NSZone *)zone {
  3012. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  3013. }
  3014. - (BOOL)isEqual:(GPBUInt32ObjectDictionary *)other {
  3015. if (self == other) {
  3016. return YES;
  3017. }
  3018. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  3019. return NO;
  3020. }
  3021. return [_dictionary isEqual:other->_dictionary];
  3022. }
  3023. - (NSUInteger)hash {
  3024. return _dictionary.count;
  3025. }
  3026. - (NSString *)description {
  3027. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3028. }
  3029. - (NSUInteger)count {
  3030. return _dictionary.count;
  3031. }
  3032. - (void)enumerateKeysAndObjectsUsingBlock:
  3033. (void (^)(uint32_t key, id object, BOOL *stop))block {
  3034. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3035. id aObject,
  3036. BOOL *stop) {
  3037. block([aKey unsignedIntValue], aObject, stop);
  3038. }];
  3039. }
  3040. - (BOOL)isInitialized {
  3041. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  3042. if (!msg.initialized) {
  3043. return NO;
  3044. }
  3045. }
  3046. return YES;
  3047. }
  3048. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  3049. GPBUInt32ObjectDictionary *newDict =
  3050. [[GPBUInt32ObjectDictionary alloc] init];
  3051. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  3052. GPBMessage *msg,
  3053. BOOL *stop) {
  3054. #pragma unused(stop)
  3055. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3056. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  3057. [copiedMsg release];
  3058. }];
  3059. return newDict;
  3060. }
  3061. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3062. NSUInteger count = _dictionary.count;
  3063. if (count == 0) {
  3064. return 0;
  3065. }
  3066. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3067. GPBDataType keyDataType = field.mapKeyDataType;
  3068. __block size_t result = 0;
  3069. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3070. id aObject,
  3071. BOOL *stop) {
  3072. #pragma unused(stop)
  3073. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3074. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3075. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3076. }];
  3077. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3078. result += tagSize * count;
  3079. return result;
  3080. }
  3081. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3082. asField:(GPBFieldDescriptor *)field {
  3083. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3084. GPBDataType keyDataType = field.mapKeyDataType;
  3085. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3086. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3087. id aObject,
  3088. BOOL *stop) {
  3089. #pragma unused(stop)
  3090. // Write the tag.
  3091. [outputStream writeInt32NoTag:tag];
  3092. // Write the size of the message.
  3093. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3094. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3095. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3096. // Write the fields.
  3097. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3098. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  3099. }];
  3100. }
  3101. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3102. forGPBGenericValueKey:(GPBGenericValue *)key {
  3103. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3104. }
  3105. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3106. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3107. #pragma unused(stop)
  3108. block([NSString stringWithFormat:@"%u", key], object);
  3109. }];
  3110. }
  3111. - (id)objectForKey:(uint32_t)key {
  3112. id result = [_dictionary objectForKey:@(key)];
  3113. return result;
  3114. }
  3115. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3116. if (otherDictionary) {
  3117. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3118. if (_autocreator) {
  3119. GPBAutocreatedDictionaryModified(_autocreator, self);
  3120. }
  3121. }
  3122. }
  3123. - (void)setObject:(id)object forKey:(uint32_t)key {
  3124. if (!object) {
  3125. [NSException raise:NSInvalidArgumentException
  3126. format:@"Attempting to add nil object to a Dictionary"];
  3127. }
  3128. [_dictionary setObject:object forKey:@(key)];
  3129. if (_autocreator) {
  3130. GPBAutocreatedDictionaryModified(_autocreator, self);
  3131. }
  3132. }
  3133. - (void)removeObjectForKey:(uint32_t)aKey {
  3134. [_dictionary removeObjectForKey:@(aKey)];
  3135. }
  3136. - (void)removeAll {
  3137. [_dictionary removeAllObjects];
  3138. }
  3139. @end
  3140. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3141. // This block of code is generated, do not edit it directly.
  3142. #pragma mark - Int32 -> UInt32
  3143. @implementation GPBInt32UInt32Dictionary {
  3144. @package
  3145. NSMutableDictionary *_dictionary;
  3146. }
  3147. + (instancetype)dictionary {
  3148. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3149. }
  3150. + (instancetype)dictionaryWithValue:(uint32_t)value
  3151. forKey:(int32_t)key {
  3152. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3153. // on to get the type correct.
  3154. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:&value
  3155. forKeys:&key
  3156. count:1] autorelease];
  3157. }
  3158. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  3159. forKeys:(const int32_t [])keys
  3160. count:(NSUInteger)count {
  3161. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3162. // on to get the type correct.
  3163. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithValues:values
  3164. forKeys:keys
  3165. count:count] autorelease];
  3166. }
  3167. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3168. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3169. // on to get the type correct.
  3170. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3171. }
  3172. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3173. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3174. }
  3175. - (instancetype)init {
  3176. return [self initWithValues:NULL forKeys:NULL count:0];
  3177. }
  3178. - (instancetype)initWithValues:(const uint32_t [])values
  3179. forKeys:(const int32_t [])keys
  3180. count:(NSUInteger)count {
  3181. self = [super init];
  3182. if (self) {
  3183. _dictionary = [[NSMutableDictionary alloc] init];
  3184. if (count && values && keys) {
  3185. for (NSUInteger i = 0; i < count; ++i) {
  3186. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3187. }
  3188. }
  3189. }
  3190. return self;
  3191. }
  3192. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3193. self = [self initWithValues:NULL forKeys:NULL count:0];
  3194. if (self) {
  3195. if (dictionary) {
  3196. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3197. }
  3198. }
  3199. return self;
  3200. }
  3201. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3202. #pragma unused(numItems)
  3203. return [self initWithValues:NULL forKeys:NULL count:0];
  3204. }
  3205. - (void)dealloc {
  3206. NSAssert(!_autocreator,
  3207. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3208. [self class], _autocreator);
  3209. [_dictionary release];
  3210. [super dealloc];
  3211. }
  3212. - (instancetype)copyWithZone:(NSZone *)zone {
  3213. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3214. }
  3215. - (BOOL)isEqual:(GPBInt32UInt32Dictionary *)other {
  3216. if (self == other) {
  3217. return YES;
  3218. }
  3219. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3220. return NO;
  3221. }
  3222. return [_dictionary isEqual:other->_dictionary];
  3223. }
  3224. - (NSUInteger)hash {
  3225. return _dictionary.count;
  3226. }
  3227. - (NSString *)description {
  3228. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3229. }
  3230. - (NSUInteger)count {
  3231. return _dictionary.count;
  3232. }
  3233. - (void)enumerateKeysAndValuesUsingBlock:
  3234. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3235. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3236. NSNumber *aValue,
  3237. BOOL *stop) {
  3238. block([aKey intValue], [aValue unsignedIntValue], stop);
  3239. }];
  3240. }
  3241. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3242. NSUInteger count = _dictionary.count;
  3243. if (count == 0) {
  3244. return 0;
  3245. }
  3246. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3247. GPBDataType keyDataType = field.mapKeyDataType;
  3248. __block size_t result = 0;
  3249. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3250. NSNumber *aValue,
  3251. BOOL *stop) {
  3252. #pragma unused(stop)
  3253. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3254. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3255. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3256. }];
  3257. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3258. result += tagSize * count;
  3259. return result;
  3260. }
  3261. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3262. asField:(GPBFieldDescriptor *)field {
  3263. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3264. GPBDataType keyDataType = field.mapKeyDataType;
  3265. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3266. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3267. NSNumber *aValue,
  3268. BOOL *stop) {
  3269. #pragma unused(stop)
  3270. // Write the tag.
  3271. [outputStream writeInt32NoTag:tag];
  3272. // Write the size of the message.
  3273. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3274. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3275. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3276. // Write the fields.
  3277. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3278. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3279. }];
  3280. }
  3281. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3282. forGPBGenericValueKey:(GPBGenericValue *)key {
  3283. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3284. }
  3285. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3286. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3287. #pragma unused(stop)
  3288. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3289. }];
  3290. }
  3291. - (BOOL)valueForKey:(int32_t)key value:(uint32_t *)value {
  3292. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3293. if (wrapped && value) {
  3294. *value = [wrapped unsignedIntValue];
  3295. }
  3296. return (wrapped != NULL);
  3297. }
  3298. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3299. if (otherDictionary) {
  3300. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3301. if (_autocreator) {
  3302. GPBAutocreatedDictionaryModified(_autocreator, self);
  3303. }
  3304. }
  3305. }
  3306. - (void)setValue:(uint32_t)value forKey:(int32_t)key {
  3307. [_dictionary setObject:@(value) forKey:@(key)];
  3308. if (_autocreator) {
  3309. GPBAutocreatedDictionaryModified(_autocreator, self);
  3310. }
  3311. }
  3312. - (void)removeValueForKey:(int32_t)aKey {
  3313. [_dictionary removeObjectForKey:@(aKey)];
  3314. }
  3315. - (void)removeAll {
  3316. [_dictionary removeAllObjects];
  3317. }
  3318. @end
  3319. #pragma mark - Int32 -> Int32
  3320. @implementation GPBInt32Int32Dictionary {
  3321. @package
  3322. NSMutableDictionary *_dictionary;
  3323. }
  3324. + (instancetype)dictionary {
  3325. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3326. }
  3327. + (instancetype)dictionaryWithValue:(int32_t)value
  3328. forKey:(int32_t)key {
  3329. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3330. // on to get the type correct.
  3331. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:&value
  3332. forKeys:&key
  3333. count:1] autorelease];
  3334. }
  3335. + (instancetype)dictionaryWithValues:(const int32_t [])values
  3336. forKeys:(const int32_t [])keys
  3337. count:(NSUInteger)count {
  3338. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3339. // on to get the type correct.
  3340. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithValues:values
  3341. forKeys:keys
  3342. count:count] autorelease];
  3343. }
  3344. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3345. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3346. // on to get the type correct.
  3347. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3348. }
  3349. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3350. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3351. }
  3352. - (instancetype)init {
  3353. return [self initWithValues:NULL forKeys:NULL count:0];
  3354. }
  3355. - (instancetype)initWithValues:(const int32_t [])values
  3356. forKeys:(const int32_t [])keys
  3357. count:(NSUInteger)count {
  3358. self = [super init];
  3359. if (self) {
  3360. _dictionary = [[NSMutableDictionary alloc] init];
  3361. if (count && values && keys) {
  3362. for (NSUInteger i = 0; i < count; ++i) {
  3363. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3364. }
  3365. }
  3366. }
  3367. return self;
  3368. }
  3369. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3370. self = [self initWithValues:NULL forKeys:NULL count:0];
  3371. if (self) {
  3372. if (dictionary) {
  3373. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3374. }
  3375. }
  3376. return self;
  3377. }
  3378. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3379. #pragma unused(numItems)
  3380. return [self initWithValues:NULL forKeys:NULL count:0];
  3381. }
  3382. - (void)dealloc {
  3383. NSAssert(!_autocreator,
  3384. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3385. [self class], _autocreator);
  3386. [_dictionary release];
  3387. [super dealloc];
  3388. }
  3389. - (instancetype)copyWithZone:(NSZone *)zone {
  3390. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3391. }
  3392. - (BOOL)isEqual:(GPBInt32Int32Dictionary *)other {
  3393. if (self == other) {
  3394. return YES;
  3395. }
  3396. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3397. return NO;
  3398. }
  3399. return [_dictionary isEqual:other->_dictionary];
  3400. }
  3401. - (NSUInteger)hash {
  3402. return _dictionary.count;
  3403. }
  3404. - (NSString *)description {
  3405. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3406. }
  3407. - (NSUInteger)count {
  3408. return _dictionary.count;
  3409. }
  3410. - (void)enumerateKeysAndValuesUsingBlock:
  3411. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3412. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3413. NSNumber *aValue,
  3414. BOOL *stop) {
  3415. block([aKey intValue], [aValue intValue], stop);
  3416. }];
  3417. }
  3418. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3419. NSUInteger count = _dictionary.count;
  3420. if (count == 0) {
  3421. return 0;
  3422. }
  3423. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3424. GPBDataType keyDataType = field.mapKeyDataType;
  3425. __block size_t result = 0;
  3426. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3427. NSNumber *aValue,
  3428. BOOL *stop) {
  3429. #pragma unused(stop)
  3430. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3431. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3432. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3433. }];
  3434. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3435. result += tagSize * count;
  3436. return result;
  3437. }
  3438. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3439. asField:(GPBFieldDescriptor *)field {
  3440. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3441. GPBDataType keyDataType = field.mapKeyDataType;
  3442. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3443. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3444. NSNumber *aValue,
  3445. BOOL *stop) {
  3446. #pragma unused(stop)
  3447. // Write the tag.
  3448. [outputStream writeInt32NoTag:tag];
  3449. // Write the size of the message.
  3450. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3451. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3452. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3453. // Write the fields.
  3454. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3455. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3456. }];
  3457. }
  3458. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3459. forGPBGenericValueKey:(GPBGenericValue *)key {
  3460. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3461. }
  3462. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3463. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3464. #pragma unused(stop)
  3465. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3466. }];
  3467. }
  3468. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  3469. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3470. if (wrapped && value) {
  3471. *value = [wrapped intValue];
  3472. }
  3473. return (wrapped != NULL);
  3474. }
  3475. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3476. if (otherDictionary) {
  3477. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3478. if (_autocreator) {
  3479. GPBAutocreatedDictionaryModified(_autocreator, self);
  3480. }
  3481. }
  3482. }
  3483. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  3484. [_dictionary setObject:@(value) forKey:@(key)];
  3485. if (_autocreator) {
  3486. GPBAutocreatedDictionaryModified(_autocreator, self);
  3487. }
  3488. }
  3489. - (void)removeValueForKey:(int32_t)aKey {
  3490. [_dictionary removeObjectForKey:@(aKey)];
  3491. }
  3492. - (void)removeAll {
  3493. [_dictionary removeAllObjects];
  3494. }
  3495. @end
  3496. #pragma mark - Int32 -> UInt64
  3497. @implementation GPBInt32UInt64Dictionary {
  3498. @package
  3499. NSMutableDictionary *_dictionary;
  3500. }
  3501. + (instancetype)dictionary {
  3502. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3503. }
  3504. + (instancetype)dictionaryWithValue:(uint64_t)value
  3505. forKey:(int32_t)key {
  3506. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3507. // on to get the type correct.
  3508. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:&value
  3509. forKeys:&key
  3510. count:1] autorelease];
  3511. }
  3512. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  3513. forKeys:(const int32_t [])keys
  3514. count:(NSUInteger)count {
  3515. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3516. // on to get the type correct.
  3517. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithValues:values
  3518. forKeys:keys
  3519. count:count] autorelease];
  3520. }
  3521. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3522. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3523. // on to get the type correct.
  3524. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3525. }
  3526. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3527. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3528. }
  3529. - (instancetype)init {
  3530. return [self initWithValues:NULL forKeys:NULL count:0];
  3531. }
  3532. - (instancetype)initWithValues:(const uint64_t [])values
  3533. forKeys:(const int32_t [])keys
  3534. count:(NSUInteger)count {
  3535. self = [super init];
  3536. if (self) {
  3537. _dictionary = [[NSMutableDictionary alloc] init];
  3538. if (count && values && keys) {
  3539. for (NSUInteger i = 0; i < count; ++i) {
  3540. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3541. }
  3542. }
  3543. }
  3544. return self;
  3545. }
  3546. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3547. self = [self initWithValues:NULL forKeys:NULL count:0];
  3548. if (self) {
  3549. if (dictionary) {
  3550. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3551. }
  3552. }
  3553. return self;
  3554. }
  3555. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3556. #pragma unused(numItems)
  3557. return [self initWithValues:NULL forKeys:NULL count:0];
  3558. }
  3559. - (void)dealloc {
  3560. NSAssert(!_autocreator,
  3561. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3562. [self class], _autocreator);
  3563. [_dictionary release];
  3564. [super dealloc];
  3565. }
  3566. - (instancetype)copyWithZone:(NSZone *)zone {
  3567. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3568. }
  3569. - (BOOL)isEqual:(GPBInt32UInt64Dictionary *)other {
  3570. if (self == other) {
  3571. return YES;
  3572. }
  3573. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3574. return NO;
  3575. }
  3576. return [_dictionary isEqual:other->_dictionary];
  3577. }
  3578. - (NSUInteger)hash {
  3579. return _dictionary.count;
  3580. }
  3581. - (NSString *)description {
  3582. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3583. }
  3584. - (NSUInteger)count {
  3585. return _dictionary.count;
  3586. }
  3587. - (void)enumerateKeysAndValuesUsingBlock:
  3588. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3589. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3590. NSNumber *aValue,
  3591. BOOL *stop) {
  3592. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3593. }];
  3594. }
  3595. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3596. NSUInteger count = _dictionary.count;
  3597. if (count == 0) {
  3598. return 0;
  3599. }
  3600. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3601. GPBDataType keyDataType = field.mapKeyDataType;
  3602. __block size_t result = 0;
  3603. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3604. NSNumber *aValue,
  3605. BOOL *stop) {
  3606. #pragma unused(stop)
  3607. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3608. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3609. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3610. }];
  3611. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3612. result += tagSize * count;
  3613. return result;
  3614. }
  3615. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3616. asField:(GPBFieldDescriptor *)field {
  3617. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3618. GPBDataType keyDataType = field.mapKeyDataType;
  3619. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3620. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3621. NSNumber *aValue,
  3622. BOOL *stop) {
  3623. #pragma unused(stop)
  3624. // Write the tag.
  3625. [outputStream writeInt32NoTag:tag];
  3626. // Write the size of the message.
  3627. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3628. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3629. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3630. // Write the fields.
  3631. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3632. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3633. }];
  3634. }
  3635. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3636. forGPBGenericValueKey:(GPBGenericValue *)key {
  3637. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3638. }
  3639. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3640. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3641. #pragma unused(stop)
  3642. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3643. }];
  3644. }
  3645. - (BOOL)valueForKey:(int32_t)key value:(uint64_t *)value {
  3646. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3647. if (wrapped && value) {
  3648. *value = [wrapped unsignedLongLongValue];
  3649. }
  3650. return (wrapped != NULL);
  3651. }
  3652. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3653. if (otherDictionary) {
  3654. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3655. if (_autocreator) {
  3656. GPBAutocreatedDictionaryModified(_autocreator, self);
  3657. }
  3658. }
  3659. }
  3660. - (void)setValue:(uint64_t)value forKey:(int32_t)key {
  3661. [_dictionary setObject:@(value) forKey:@(key)];
  3662. if (_autocreator) {
  3663. GPBAutocreatedDictionaryModified(_autocreator, self);
  3664. }
  3665. }
  3666. - (void)removeValueForKey:(int32_t)aKey {
  3667. [_dictionary removeObjectForKey:@(aKey)];
  3668. }
  3669. - (void)removeAll {
  3670. [_dictionary removeAllObjects];
  3671. }
  3672. @end
  3673. #pragma mark - Int32 -> Int64
  3674. @implementation GPBInt32Int64Dictionary {
  3675. @package
  3676. NSMutableDictionary *_dictionary;
  3677. }
  3678. + (instancetype)dictionary {
  3679. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3680. }
  3681. + (instancetype)dictionaryWithValue:(int64_t)value
  3682. forKey:(int32_t)key {
  3683. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3684. // on to get the type correct.
  3685. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:&value
  3686. forKeys:&key
  3687. count:1] autorelease];
  3688. }
  3689. + (instancetype)dictionaryWithValues:(const int64_t [])values
  3690. forKeys:(const int32_t [])keys
  3691. count:(NSUInteger)count {
  3692. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3693. // on to get the type correct.
  3694. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithValues:values
  3695. forKeys:keys
  3696. count:count] autorelease];
  3697. }
  3698. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3699. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3700. // on to get the type correct.
  3701. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3702. }
  3703. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3704. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3705. }
  3706. - (instancetype)init {
  3707. return [self initWithValues:NULL forKeys:NULL count:0];
  3708. }
  3709. - (instancetype)initWithValues:(const int64_t [])values
  3710. forKeys:(const int32_t [])keys
  3711. count:(NSUInteger)count {
  3712. self = [super init];
  3713. if (self) {
  3714. _dictionary = [[NSMutableDictionary alloc] init];
  3715. if (count && values && keys) {
  3716. for (NSUInteger i = 0; i < count; ++i) {
  3717. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3718. }
  3719. }
  3720. }
  3721. return self;
  3722. }
  3723. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3724. self = [self initWithValues:NULL forKeys:NULL count:0];
  3725. if (self) {
  3726. if (dictionary) {
  3727. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3728. }
  3729. }
  3730. return self;
  3731. }
  3732. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3733. #pragma unused(numItems)
  3734. return [self initWithValues:NULL forKeys:NULL count:0];
  3735. }
  3736. - (void)dealloc {
  3737. NSAssert(!_autocreator,
  3738. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3739. [self class], _autocreator);
  3740. [_dictionary release];
  3741. [super dealloc];
  3742. }
  3743. - (instancetype)copyWithZone:(NSZone *)zone {
  3744. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3745. }
  3746. - (BOOL)isEqual:(GPBInt32Int64Dictionary *)other {
  3747. if (self == other) {
  3748. return YES;
  3749. }
  3750. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3751. return NO;
  3752. }
  3753. return [_dictionary isEqual:other->_dictionary];
  3754. }
  3755. - (NSUInteger)hash {
  3756. return _dictionary.count;
  3757. }
  3758. - (NSString *)description {
  3759. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3760. }
  3761. - (NSUInteger)count {
  3762. return _dictionary.count;
  3763. }
  3764. - (void)enumerateKeysAndValuesUsingBlock:
  3765. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3766. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3767. NSNumber *aValue,
  3768. BOOL *stop) {
  3769. block([aKey intValue], [aValue longLongValue], stop);
  3770. }];
  3771. }
  3772. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3773. NSUInteger count = _dictionary.count;
  3774. if (count == 0) {
  3775. return 0;
  3776. }
  3777. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3778. GPBDataType keyDataType = field.mapKeyDataType;
  3779. __block size_t result = 0;
  3780. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3781. NSNumber *aValue,
  3782. BOOL *stop) {
  3783. #pragma unused(stop)
  3784. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3785. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3786. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3787. }];
  3788. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3789. result += tagSize * count;
  3790. return result;
  3791. }
  3792. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3793. asField:(GPBFieldDescriptor *)field {
  3794. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3795. GPBDataType keyDataType = field.mapKeyDataType;
  3796. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3797. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3798. NSNumber *aValue,
  3799. BOOL *stop) {
  3800. #pragma unused(stop)
  3801. // Write the tag.
  3802. [outputStream writeInt32NoTag:tag];
  3803. // Write the size of the message.
  3804. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3805. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3806. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3807. // Write the fields.
  3808. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3809. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3810. }];
  3811. }
  3812. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3813. forGPBGenericValueKey:(GPBGenericValue *)key {
  3814. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3815. }
  3816. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3817. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3818. #pragma unused(stop)
  3819. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3820. }];
  3821. }
  3822. - (BOOL)valueForKey:(int32_t)key value:(int64_t *)value {
  3823. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3824. if (wrapped && value) {
  3825. *value = [wrapped longLongValue];
  3826. }
  3827. return (wrapped != NULL);
  3828. }
  3829. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3830. if (otherDictionary) {
  3831. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3832. if (_autocreator) {
  3833. GPBAutocreatedDictionaryModified(_autocreator, self);
  3834. }
  3835. }
  3836. }
  3837. - (void)setValue:(int64_t)value forKey:(int32_t)key {
  3838. [_dictionary setObject:@(value) forKey:@(key)];
  3839. if (_autocreator) {
  3840. GPBAutocreatedDictionaryModified(_autocreator, self);
  3841. }
  3842. }
  3843. - (void)removeValueForKey:(int32_t)aKey {
  3844. [_dictionary removeObjectForKey:@(aKey)];
  3845. }
  3846. - (void)removeAll {
  3847. [_dictionary removeAllObjects];
  3848. }
  3849. @end
  3850. #pragma mark - Int32 -> Bool
  3851. @implementation GPBInt32BoolDictionary {
  3852. @package
  3853. NSMutableDictionary *_dictionary;
  3854. }
  3855. + (instancetype)dictionary {
  3856. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  3857. }
  3858. + (instancetype)dictionaryWithValue:(BOOL)value
  3859. forKey:(int32_t)key {
  3860. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3861. // on to get the type correct.
  3862. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:&value
  3863. forKeys:&key
  3864. count:1] autorelease];
  3865. }
  3866. + (instancetype)dictionaryWithValues:(const BOOL [])values
  3867. forKeys:(const int32_t [])keys
  3868. count:(NSUInteger)count {
  3869. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  3870. // on to get the type correct.
  3871. return [[(GPBInt32BoolDictionary*)[self alloc] initWithValues:values
  3872. forKeys:keys
  3873. count:count] autorelease];
  3874. }
  3875. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3876. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3877. // on to get the type correct.
  3878. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3879. }
  3880. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3881. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3882. }
  3883. - (instancetype)init {
  3884. return [self initWithValues:NULL forKeys:NULL count:0];
  3885. }
  3886. - (instancetype)initWithValues:(const BOOL [])values
  3887. forKeys:(const int32_t [])keys
  3888. count:(NSUInteger)count {
  3889. self = [super init];
  3890. if (self) {
  3891. _dictionary = [[NSMutableDictionary alloc] init];
  3892. if (count && values && keys) {
  3893. for (NSUInteger i = 0; i < count; ++i) {
  3894. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3895. }
  3896. }
  3897. }
  3898. return self;
  3899. }
  3900. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3901. self = [self initWithValues:NULL forKeys:NULL count:0];
  3902. if (self) {
  3903. if (dictionary) {
  3904. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3905. }
  3906. }
  3907. return self;
  3908. }
  3909. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3910. #pragma unused(numItems)
  3911. return [self initWithValues:NULL forKeys:NULL count:0];
  3912. }
  3913. - (void)dealloc {
  3914. NSAssert(!_autocreator,
  3915. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3916. [self class], _autocreator);
  3917. [_dictionary release];
  3918. [super dealloc];
  3919. }
  3920. - (instancetype)copyWithZone:(NSZone *)zone {
  3921. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3922. }
  3923. - (BOOL)isEqual:(GPBInt32BoolDictionary *)other {
  3924. if (self == other) {
  3925. return YES;
  3926. }
  3927. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3928. return NO;
  3929. }
  3930. return [_dictionary isEqual:other->_dictionary];
  3931. }
  3932. - (NSUInteger)hash {
  3933. return _dictionary.count;
  3934. }
  3935. - (NSString *)description {
  3936. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3937. }
  3938. - (NSUInteger)count {
  3939. return _dictionary.count;
  3940. }
  3941. - (void)enumerateKeysAndValuesUsingBlock:
  3942. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3943. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3944. NSNumber *aValue,
  3945. BOOL *stop) {
  3946. block([aKey intValue], [aValue boolValue], stop);
  3947. }];
  3948. }
  3949. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3950. NSUInteger count = _dictionary.count;
  3951. if (count == 0) {
  3952. return 0;
  3953. }
  3954. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3955. GPBDataType keyDataType = field.mapKeyDataType;
  3956. __block size_t result = 0;
  3957. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3958. NSNumber *aValue,
  3959. BOOL *stop) {
  3960. #pragma unused(stop)
  3961. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3962. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3963. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3964. }];
  3965. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3966. result += tagSize * count;
  3967. return result;
  3968. }
  3969. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3970. asField:(GPBFieldDescriptor *)field {
  3971. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3972. GPBDataType keyDataType = field.mapKeyDataType;
  3973. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3974. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3975. NSNumber *aValue,
  3976. BOOL *stop) {
  3977. #pragma unused(stop)
  3978. // Write the tag.
  3979. [outputStream writeInt32NoTag:tag];
  3980. // Write the size of the message.
  3981. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3982. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3983. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3984. // Write the fields.
  3985. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3986. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  3987. }];
  3988. }
  3989. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3990. forGPBGenericValueKey:(GPBGenericValue *)key {
  3991. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3992. }
  3993. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3994. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3995. #pragma unused(stop)
  3996. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3997. }];
  3998. }
  3999. - (BOOL)valueForKey:(int32_t)key value:(BOOL *)value {
  4000. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4001. if (wrapped && value) {
  4002. *value = [wrapped boolValue];
  4003. }
  4004. return (wrapped != NULL);
  4005. }
  4006. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  4007. if (otherDictionary) {
  4008. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4009. if (_autocreator) {
  4010. GPBAutocreatedDictionaryModified(_autocreator, self);
  4011. }
  4012. }
  4013. }
  4014. - (void)setValue:(BOOL)value forKey:(int32_t)key {
  4015. [_dictionary setObject:@(value) forKey:@(key)];
  4016. if (_autocreator) {
  4017. GPBAutocreatedDictionaryModified(_autocreator, self);
  4018. }
  4019. }
  4020. - (void)removeValueForKey:(int32_t)aKey {
  4021. [_dictionary removeObjectForKey:@(aKey)];
  4022. }
  4023. - (void)removeAll {
  4024. [_dictionary removeAllObjects];
  4025. }
  4026. @end
  4027. #pragma mark - Int32 -> Float
  4028. @implementation GPBInt32FloatDictionary {
  4029. @package
  4030. NSMutableDictionary *_dictionary;
  4031. }
  4032. + (instancetype)dictionary {
  4033. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4034. }
  4035. + (instancetype)dictionaryWithValue:(float)value
  4036. forKey:(int32_t)key {
  4037. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4038. // on to get the type correct.
  4039. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:&value
  4040. forKeys:&key
  4041. count:1] autorelease];
  4042. }
  4043. + (instancetype)dictionaryWithValues:(const float [])values
  4044. forKeys:(const int32_t [])keys
  4045. count:(NSUInteger)count {
  4046. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4047. // on to get the type correct.
  4048. return [[(GPBInt32FloatDictionary*)[self alloc] initWithValues:values
  4049. forKeys:keys
  4050. count:count] autorelease];
  4051. }
  4052. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4053. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4054. // on to get the type correct.
  4055. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4056. }
  4057. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4058. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4059. }
  4060. - (instancetype)init {
  4061. return [self initWithValues:NULL forKeys:NULL count:0];
  4062. }
  4063. - (instancetype)initWithValues:(const float [])values
  4064. forKeys:(const int32_t [])keys
  4065. count:(NSUInteger)count {
  4066. self = [super init];
  4067. if (self) {
  4068. _dictionary = [[NSMutableDictionary alloc] init];
  4069. if (count && values && keys) {
  4070. for (NSUInteger i = 0; i < count; ++i) {
  4071. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4072. }
  4073. }
  4074. }
  4075. return self;
  4076. }
  4077. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4078. self = [self initWithValues:NULL forKeys:NULL count:0];
  4079. if (self) {
  4080. if (dictionary) {
  4081. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4082. }
  4083. }
  4084. return self;
  4085. }
  4086. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4087. #pragma unused(numItems)
  4088. return [self initWithValues:NULL forKeys:NULL count:0];
  4089. }
  4090. - (void)dealloc {
  4091. NSAssert(!_autocreator,
  4092. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4093. [self class], _autocreator);
  4094. [_dictionary release];
  4095. [super dealloc];
  4096. }
  4097. - (instancetype)copyWithZone:(NSZone *)zone {
  4098. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4099. }
  4100. - (BOOL)isEqual:(GPBInt32FloatDictionary *)other {
  4101. if (self == other) {
  4102. return YES;
  4103. }
  4104. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4105. return NO;
  4106. }
  4107. return [_dictionary isEqual:other->_dictionary];
  4108. }
  4109. - (NSUInteger)hash {
  4110. return _dictionary.count;
  4111. }
  4112. - (NSString *)description {
  4113. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4114. }
  4115. - (NSUInteger)count {
  4116. return _dictionary.count;
  4117. }
  4118. - (void)enumerateKeysAndValuesUsingBlock:
  4119. (void (^)(int32_t key, float value, BOOL *stop))block {
  4120. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4121. NSNumber *aValue,
  4122. BOOL *stop) {
  4123. block([aKey intValue], [aValue floatValue], stop);
  4124. }];
  4125. }
  4126. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4127. NSUInteger count = _dictionary.count;
  4128. if (count == 0) {
  4129. return 0;
  4130. }
  4131. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4132. GPBDataType keyDataType = field.mapKeyDataType;
  4133. __block size_t result = 0;
  4134. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4135. NSNumber *aValue,
  4136. BOOL *stop) {
  4137. #pragma unused(stop)
  4138. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4139. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4140. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4141. }];
  4142. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4143. result += tagSize * count;
  4144. return result;
  4145. }
  4146. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4147. asField:(GPBFieldDescriptor *)field {
  4148. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4149. GPBDataType keyDataType = field.mapKeyDataType;
  4150. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4151. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4152. NSNumber *aValue,
  4153. BOOL *stop) {
  4154. #pragma unused(stop)
  4155. // Write the tag.
  4156. [outputStream writeInt32NoTag:tag];
  4157. // Write the size of the message.
  4158. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4159. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4160. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4161. // Write the fields.
  4162. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4163. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4164. }];
  4165. }
  4166. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4167. forGPBGenericValueKey:(GPBGenericValue *)key {
  4168. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4169. }
  4170. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4171. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4172. #pragma unused(stop)
  4173. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4174. }];
  4175. }
  4176. - (BOOL)valueForKey:(int32_t)key value:(float *)value {
  4177. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4178. if (wrapped && value) {
  4179. *value = [wrapped floatValue];
  4180. }
  4181. return (wrapped != NULL);
  4182. }
  4183. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4184. if (otherDictionary) {
  4185. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4186. if (_autocreator) {
  4187. GPBAutocreatedDictionaryModified(_autocreator, self);
  4188. }
  4189. }
  4190. }
  4191. - (void)setValue:(float)value forKey:(int32_t)key {
  4192. [_dictionary setObject:@(value) forKey:@(key)];
  4193. if (_autocreator) {
  4194. GPBAutocreatedDictionaryModified(_autocreator, self);
  4195. }
  4196. }
  4197. - (void)removeValueForKey:(int32_t)aKey {
  4198. [_dictionary removeObjectForKey:@(aKey)];
  4199. }
  4200. - (void)removeAll {
  4201. [_dictionary removeAllObjects];
  4202. }
  4203. @end
  4204. #pragma mark - Int32 -> Double
  4205. @implementation GPBInt32DoubleDictionary {
  4206. @package
  4207. NSMutableDictionary *_dictionary;
  4208. }
  4209. + (instancetype)dictionary {
  4210. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4211. }
  4212. + (instancetype)dictionaryWithValue:(double)value
  4213. forKey:(int32_t)key {
  4214. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4215. // on to get the type correct.
  4216. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:&value
  4217. forKeys:&key
  4218. count:1] autorelease];
  4219. }
  4220. + (instancetype)dictionaryWithValues:(const double [])values
  4221. forKeys:(const int32_t [])keys
  4222. count:(NSUInteger)count {
  4223. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4224. // on to get the type correct.
  4225. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithValues:values
  4226. forKeys:keys
  4227. count:count] autorelease];
  4228. }
  4229. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4230. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4231. // on to get the type correct.
  4232. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4233. }
  4234. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4235. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4236. }
  4237. - (instancetype)init {
  4238. return [self initWithValues:NULL forKeys:NULL count:0];
  4239. }
  4240. - (instancetype)initWithValues:(const double [])values
  4241. forKeys:(const int32_t [])keys
  4242. count:(NSUInteger)count {
  4243. self = [super init];
  4244. if (self) {
  4245. _dictionary = [[NSMutableDictionary alloc] init];
  4246. if (count && values && keys) {
  4247. for (NSUInteger i = 0; i < count; ++i) {
  4248. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4249. }
  4250. }
  4251. }
  4252. return self;
  4253. }
  4254. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4255. self = [self initWithValues:NULL forKeys:NULL count:0];
  4256. if (self) {
  4257. if (dictionary) {
  4258. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4259. }
  4260. }
  4261. return self;
  4262. }
  4263. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4264. #pragma unused(numItems)
  4265. return [self initWithValues:NULL forKeys:NULL count:0];
  4266. }
  4267. - (void)dealloc {
  4268. NSAssert(!_autocreator,
  4269. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4270. [self class], _autocreator);
  4271. [_dictionary release];
  4272. [super dealloc];
  4273. }
  4274. - (instancetype)copyWithZone:(NSZone *)zone {
  4275. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4276. }
  4277. - (BOOL)isEqual:(GPBInt32DoubleDictionary *)other {
  4278. if (self == other) {
  4279. return YES;
  4280. }
  4281. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4282. return NO;
  4283. }
  4284. return [_dictionary isEqual:other->_dictionary];
  4285. }
  4286. - (NSUInteger)hash {
  4287. return _dictionary.count;
  4288. }
  4289. - (NSString *)description {
  4290. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4291. }
  4292. - (NSUInteger)count {
  4293. return _dictionary.count;
  4294. }
  4295. - (void)enumerateKeysAndValuesUsingBlock:
  4296. (void (^)(int32_t key, double value, BOOL *stop))block {
  4297. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4298. NSNumber *aValue,
  4299. BOOL *stop) {
  4300. block([aKey intValue], [aValue doubleValue], stop);
  4301. }];
  4302. }
  4303. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4304. NSUInteger count = _dictionary.count;
  4305. if (count == 0) {
  4306. return 0;
  4307. }
  4308. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4309. GPBDataType keyDataType = field.mapKeyDataType;
  4310. __block size_t result = 0;
  4311. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4312. NSNumber *aValue,
  4313. BOOL *stop) {
  4314. #pragma unused(stop)
  4315. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4316. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4317. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4318. }];
  4319. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4320. result += tagSize * count;
  4321. return result;
  4322. }
  4323. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4324. asField:(GPBFieldDescriptor *)field {
  4325. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4326. GPBDataType keyDataType = field.mapKeyDataType;
  4327. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4328. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4329. NSNumber *aValue,
  4330. BOOL *stop) {
  4331. #pragma unused(stop)
  4332. // Write the tag.
  4333. [outputStream writeInt32NoTag:tag];
  4334. // Write the size of the message.
  4335. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4336. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4337. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4338. // Write the fields.
  4339. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4340. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4341. }];
  4342. }
  4343. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4344. forGPBGenericValueKey:(GPBGenericValue *)key {
  4345. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4346. }
  4347. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4348. [self enumerateKeysAndValuesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4349. #pragma unused(stop)
  4350. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4351. }];
  4352. }
  4353. - (BOOL)valueForKey:(int32_t)key value:(double *)value {
  4354. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4355. if (wrapped && value) {
  4356. *value = [wrapped doubleValue];
  4357. }
  4358. return (wrapped != NULL);
  4359. }
  4360. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4361. if (otherDictionary) {
  4362. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4363. if (_autocreator) {
  4364. GPBAutocreatedDictionaryModified(_autocreator, self);
  4365. }
  4366. }
  4367. }
  4368. - (void)setValue:(double)value forKey:(int32_t)key {
  4369. [_dictionary setObject:@(value) forKey:@(key)];
  4370. if (_autocreator) {
  4371. GPBAutocreatedDictionaryModified(_autocreator, self);
  4372. }
  4373. }
  4374. - (void)removeValueForKey:(int32_t)aKey {
  4375. [_dictionary removeObjectForKey:@(aKey)];
  4376. }
  4377. - (void)removeAll {
  4378. [_dictionary removeAllObjects];
  4379. }
  4380. @end
  4381. #pragma mark - Int32 -> Enum
  4382. @implementation GPBInt32EnumDictionary {
  4383. @package
  4384. NSMutableDictionary *_dictionary;
  4385. GPBEnumValidationFunc _validationFunc;
  4386. }
  4387. @synthesize validationFunc = _validationFunc;
  4388. + (instancetype)dictionary {
  4389. return [[[self alloc] initWithValidationFunction:NULL
  4390. rawValues:NULL
  4391. forKeys:NULL
  4392. count:0] autorelease];
  4393. }
  4394. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4395. return [[[self alloc] initWithValidationFunction:func
  4396. rawValues:NULL
  4397. forKeys:NULL
  4398. count:0] autorelease];
  4399. }
  4400. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4401. rawValue:(int32_t)rawValue
  4402. forKey:(int32_t)key {
  4403. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4404. // on to get the type correct.
  4405. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4406. rawValues:&rawValue
  4407. forKeys:&key
  4408. count:1] autorelease];
  4409. }
  4410. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4411. rawValues:(const int32_t [])rawValues
  4412. forKeys:(const int32_t [])keys
  4413. count:(NSUInteger)count {
  4414. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4415. // on to get the type correct.
  4416. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4417. rawValues:rawValues
  4418. forKeys:keys
  4419. count:count] autorelease];
  4420. }
  4421. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4422. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4423. // on to get the type correct.
  4424. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4425. }
  4426. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4427. capacity:(NSUInteger)numItems {
  4428. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4429. }
  4430. - (instancetype)init {
  4431. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4432. }
  4433. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4434. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4435. }
  4436. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4437. rawValues:(const int32_t [])rawValues
  4438. forKeys:(const int32_t [])keys
  4439. count:(NSUInteger)count {
  4440. self = [super init];
  4441. if (self) {
  4442. _dictionary = [[NSMutableDictionary alloc] init];
  4443. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4444. if (count && rawValues && keys) {
  4445. for (NSUInteger i = 0; i < count; ++i) {
  4446. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4447. }
  4448. }
  4449. }
  4450. return self;
  4451. }
  4452. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4453. self = [self initWithValidationFunction:dictionary.validationFunc
  4454. rawValues:NULL
  4455. forKeys:NULL
  4456. count:0];
  4457. if (self) {
  4458. if (dictionary) {
  4459. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4460. }
  4461. }
  4462. return self;
  4463. }
  4464. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4465. capacity:(NSUInteger)numItems {
  4466. #pragma unused(numItems)
  4467. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4468. }
  4469. - (void)dealloc {
  4470. NSAssert(!_autocreator,
  4471. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4472. [self class], _autocreator);
  4473. [_dictionary release];
  4474. [super dealloc];
  4475. }
  4476. - (instancetype)copyWithZone:(NSZone *)zone {
  4477. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4478. }
  4479. - (BOOL)isEqual:(GPBInt32EnumDictionary *)other {
  4480. if (self == other) {
  4481. return YES;
  4482. }
  4483. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4484. return NO;
  4485. }
  4486. return [_dictionary isEqual:other->_dictionary];
  4487. }
  4488. - (NSUInteger)hash {
  4489. return _dictionary.count;
  4490. }
  4491. - (NSString *)description {
  4492. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4493. }
  4494. - (NSUInteger)count {
  4495. return _dictionary.count;
  4496. }
  4497. - (void)enumerateKeysAndRawValuesUsingBlock:
  4498. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4499. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4500. NSNumber *aValue,
  4501. BOOL *stop) {
  4502. block([aKey intValue], [aValue intValue], stop);
  4503. }];
  4504. }
  4505. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4506. NSUInteger count = _dictionary.count;
  4507. if (count == 0) {
  4508. return 0;
  4509. }
  4510. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4511. GPBDataType keyDataType = field.mapKeyDataType;
  4512. __block size_t result = 0;
  4513. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4514. NSNumber *aValue,
  4515. BOOL *stop) {
  4516. #pragma unused(stop)
  4517. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4518. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4519. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4520. }];
  4521. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4522. result += tagSize * count;
  4523. return result;
  4524. }
  4525. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4526. asField:(GPBFieldDescriptor *)field {
  4527. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4528. GPBDataType keyDataType = field.mapKeyDataType;
  4529. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4530. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4531. NSNumber *aValue,
  4532. BOOL *stop) {
  4533. #pragma unused(stop)
  4534. // Write the tag.
  4535. [outputStream writeInt32NoTag:tag];
  4536. // Write the size of the message.
  4537. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4538. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4539. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4540. // Write the fields.
  4541. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4542. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4543. }];
  4544. }
  4545. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4546. forKey:(GPBGenericValue *)key
  4547. keyDataType:(GPBDataType)keyDataType {
  4548. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4549. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4550. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4551. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4552. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4553. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4554. [outputStream release];
  4555. return data;
  4556. }
  4557. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4558. forGPBGenericValueKey:(GPBGenericValue *)key {
  4559. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4560. }
  4561. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4562. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4563. #pragma unused(stop)
  4564. block([NSString stringWithFormat:@"%d", key], @(value));
  4565. }];
  4566. }
  4567. - (BOOL)valueForKey:(int32_t)key value:(int32_t *)value {
  4568. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4569. if (wrapped && value) {
  4570. int32_t result = [wrapped intValue];
  4571. if (!_validationFunc(result)) {
  4572. result = kGPBUnrecognizedEnumeratorValue;
  4573. }
  4574. *value = result;
  4575. }
  4576. return (wrapped != NULL);
  4577. }
  4578. - (BOOL)valueForKey:(int32_t)key rawValue:(int32_t *)rawValue {
  4579. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4580. if (wrapped && rawValue) {
  4581. *rawValue = [wrapped intValue];
  4582. }
  4583. return (wrapped != NULL);
  4584. }
  4585. - (void)enumerateKeysAndValuesUsingBlock:
  4586. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4587. GPBEnumValidationFunc func = _validationFunc;
  4588. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4589. NSNumber *aValue,
  4590. BOOL *stop) {
  4591. int32_t unwrapped = [aValue intValue];
  4592. if (!func(unwrapped)) {
  4593. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4594. }
  4595. block([aKey intValue], unwrapped, stop);
  4596. }];
  4597. }
  4598. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4599. if (otherDictionary) {
  4600. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4601. if (_autocreator) {
  4602. GPBAutocreatedDictionaryModified(_autocreator, self);
  4603. }
  4604. }
  4605. }
  4606. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4607. [_dictionary setObject:@(value) forKey:@(key)];
  4608. if (_autocreator) {
  4609. GPBAutocreatedDictionaryModified(_autocreator, self);
  4610. }
  4611. }
  4612. - (void)removeValueForKey:(int32_t)aKey {
  4613. [_dictionary removeObjectForKey:@(aKey)];
  4614. }
  4615. - (void)removeAll {
  4616. [_dictionary removeAllObjects];
  4617. }
  4618. - (void)setValue:(int32_t)value forKey:(int32_t)key {
  4619. if (!_validationFunc(value)) {
  4620. [NSException raise:NSInvalidArgumentException
  4621. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4622. value];
  4623. }
  4624. [_dictionary setObject:@(value) forKey:@(key)];
  4625. if (_autocreator) {
  4626. GPBAutocreatedDictionaryModified(_autocreator, self);
  4627. }
  4628. }
  4629. @end
  4630. #pragma mark - Int32 -> Object
  4631. @implementation GPBInt32ObjectDictionary {
  4632. @package
  4633. NSMutableDictionary *_dictionary;
  4634. }
  4635. + (instancetype)dictionary {
  4636. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4637. }
  4638. + (instancetype)dictionaryWithObject:(id)object
  4639. forKey:(int32_t)key {
  4640. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4641. // on to get the type correct.
  4642. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4643. forKeys:&key
  4644. count:1] autorelease];
  4645. }
  4646. + (instancetype)dictionaryWithObjects:(const id [])objects
  4647. forKeys:(const int32_t [])keys
  4648. count:(NSUInteger)count {
  4649. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4650. // on to get the type correct.
  4651. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4652. forKeys:keys
  4653. count:count] autorelease];
  4654. }
  4655. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4656. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4657. // on to get the type correct.
  4658. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4659. }
  4660. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4661. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4662. }
  4663. - (instancetype)init {
  4664. return [self initWithObjects:NULL forKeys:NULL count:0];
  4665. }
  4666. - (instancetype)initWithObjects:(const id [])objects
  4667. forKeys:(const int32_t [])keys
  4668. count:(NSUInteger)count {
  4669. self = [super init];
  4670. if (self) {
  4671. _dictionary = [[NSMutableDictionary alloc] init];
  4672. if (count && objects && keys) {
  4673. for (NSUInteger i = 0; i < count; ++i) {
  4674. if (!objects[i]) {
  4675. [NSException raise:NSInvalidArgumentException
  4676. format:@"Attempting to add nil object to a Dictionary"];
  4677. }
  4678. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4679. }
  4680. }
  4681. }
  4682. return self;
  4683. }
  4684. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4685. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4686. if (self) {
  4687. if (dictionary) {
  4688. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4689. }
  4690. }
  4691. return self;
  4692. }
  4693. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4694. #pragma unused(numItems)
  4695. return [self initWithObjects:NULL forKeys:NULL count:0];
  4696. }
  4697. - (void)dealloc {
  4698. NSAssert(!_autocreator,
  4699. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4700. [self class], _autocreator);
  4701. [_dictionary release];
  4702. [super dealloc];
  4703. }
  4704. - (instancetype)copyWithZone:(NSZone *)zone {
  4705. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4706. }
  4707. - (BOOL)isEqual:(GPBInt32ObjectDictionary *)other {
  4708. if (self == other) {
  4709. return YES;
  4710. }
  4711. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4712. return NO;
  4713. }
  4714. return [_dictionary isEqual:other->_dictionary];
  4715. }
  4716. - (NSUInteger)hash {
  4717. return _dictionary.count;
  4718. }
  4719. - (NSString *)description {
  4720. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4721. }
  4722. - (NSUInteger)count {
  4723. return _dictionary.count;
  4724. }
  4725. - (void)enumerateKeysAndObjectsUsingBlock:
  4726. (void (^)(int32_t key, id object, BOOL *stop))block {
  4727. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4728. id aObject,
  4729. BOOL *stop) {
  4730. block([aKey intValue], aObject, stop);
  4731. }];
  4732. }
  4733. - (BOOL)isInitialized {
  4734. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4735. if (!msg.initialized) {
  4736. return NO;
  4737. }
  4738. }
  4739. return YES;
  4740. }
  4741. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4742. GPBInt32ObjectDictionary *newDict =
  4743. [[GPBInt32ObjectDictionary alloc] init];
  4744. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4745. GPBMessage *msg,
  4746. BOOL *stop) {
  4747. #pragma unused(stop)
  4748. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4749. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4750. [copiedMsg release];
  4751. }];
  4752. return newDict;
  4753. }
  4754. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4755. NSUInteger count = _dictionary.count;
  4756. if (count == 0) {
  4757. return 0;
  4758. }
  4759. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4760. GPBDataType keyDataType = field.mapKeyDataType;
  4761. __block size_t result = 0;
  4762. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4763. id aObject,
  4764. BOOL *stop) {
  4765. #pragma unused(stop)
  4766. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4767. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4768. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4769. }];
  4770. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4771. result += tagSize * count;
  4772. return result;
  4773. }
  4774. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4775. asField:(GPBFieldDescriptor *)field {
  4776. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4777. GPBDataType keyDataType = field.mapKeyDataType;
  4778. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4779. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4780. id aObject,
  4781. BOOL *stop) {
  4782. #pragma unused(stop)
  4783. // Write the tag.
  4784. [outputStream writeInt32NoTag:tag];
  4785. // Write the size of the message.
  4786. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4787. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4788. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4789. // Write the fields.
  4790. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4791. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  4792. }];
  4793. }
  4794. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4795. forGPBGenericValueKey:(GPBGenericValue *)key {
  4796. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4797. }
  4798. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4799. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4800. #pragma unused(stop)
  4801. block([NSString stringWithFormat:@"%d", key], object);
  4802. }];
  4803. }
  4804. - (id)objectForKey:(int32_t)key {
  4805. id result = [_dictionary objectForKey:@(key)];
  4806. return result;
  4807. }
  4808. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4809. if (otherDictionary) {
  4810. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4811. if (_autocreator) {
  4812. GPBAutocreatedDictionaryModified(_autocreator, self);
  4813. }
  4814. }
  4815. }
  4816. - (void)setObject:(id)object forKey:(int32_t)key {
  4817. if (!object) {
  4818. [NSException raise:NSInvalidArgumentException
  4819. format:@"Attempting to add nil object to a Dictionary"];
  4820. }
  4821. [_dictionary setObject:object forKey:@(key)];
  4822. if (_autocreator) {
  4823. GPBAutocreatedDictionaryModified(_autocreator, self);
  4824. }
  4825. }
  4826. - (void)removeObjectForKey:(int32_t)aKey {
  4827. [_dictionary removeObjectForKey:@(aKey)];
  4828. }
  4829. - (void)removeAll {
  4830. [_dictionary removeAllObjects];
  4831. }
  4832. @end
  4833. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4834. // This block of code is generated, do not edit it directly.
  4835. #pragma mark - UInt64 -> UInt32
  4836. @implementation GPBUInt64UInt32Dictionary {
  4837. @package
  4838. NSMutableDictionary *_dictionary;
  4839. }
  4840. + (instancetype)dictionary {
  4841. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  4842. }
  4843. + (instancetype)dictionaryWithValue:(uint32_t)value
  4844. forKey:(uint64_t)key {
  4845. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4846. // on to get the type correct.
  4847. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  4848. forKeys:&key
  4849. count:1] autorelease];
  4850. }
  4851. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  4852. forKeys:(const uint64_t [])keys
  4853. count:(NSUInteger)count {
  4854. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4855. // on to get the type correct.
  4856. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithValues:values
  4857. forKeys:keys
  4858. count:count] autorelease];
  4859. }
  4860. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4861. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4862. // on to get the type correct.
  4863. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4864. }
  4865. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4866. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4867. }
  4868. - (instancetype)init {
  4869. return [self initWithValues:NULL forKeys:NULL count:0];
  4870. }
  4871. - (instancetype)initWithValues:(const uint32_t [])values
  4872. forKeys:(const uint64_t [])keys
  4873. count:(NSUInteger)count {
  4874. self = [super init];
  4875. if (self) {
  4876. _dictionary = [[NSMutableDictionary alloc] init];
  4877. if (count && values && keys) {
  4878. for (NSUInteger i = 0; i < count; ++i) {
  4879. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4880. }
  4881. }
  4882. }
  4883. return self;
  4884. }
  4885. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4886. self = [self initWithValues:NULL forKeys:NULL count:0];
  4887. if (self) {
  4888. if (dictionary) {
  4889. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4890. }
  4891. }
  4892. return self;
  4893. }
  4894. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4895. #pragma unused(numItems)
  4896. return [self initWithValues:NULL forKeys:NULL count:0];
  4897. }
  4898. - (void)dealloc {
  4899. NSAssert(!_autocreator,
  4900. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4901. [self class], _autocreator);
  4902. [_dictionary release];
  4903. [super dealloc];
  4904. }
  4905. - (instancetype)copyWithZone:(NSZone *)zone {
  4906. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4907. }
  4908. - (BOOL)isEqual:(GPBUInt64UInt32Dictionary *)other {
  4909. if (self == other) {
  4910. return YES;
  4911. }
  4912. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4913. return NO;
  4914. }
  4915. return [_dictionary isEqual:other->_dictionary];
  4916. }
  4917. - (NSUInteger)hash {
  4918. return _dictionary.count;
  4919. }
  4920. - (NSString *)description {
  4921. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4922. }
  4923. - (NSUInteger)count {
  4924. return _dictionary.count;
  4925. }
  4926. - (void)enumerateKeysAndValuesUsingBlock:
  4927. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4928. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4929. NSNumber *aValue,
  4930. BOOL *stop) {
  4931. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4932. }];
  4933. }
  4934. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4935. NSUInteger count = _dictionary.count;
  4936. if (count == 0) {
  4937. return 0;
  4938. }
  4939. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4940. GPBDataType keyDataType = field.mapKeyDataType;
  4941. __block size_t result = 0;
  4942. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4943. NSNumber *aValue,
  4944. BOOL *stop) {
  4945. #pragma unused(stop)
  4946. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4947. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4948. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4949. }];
  4950. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4951. result += tagSize * count;
  4952. return result;
  4953. }
  4954. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4955. asField:(GPBFieldDescriptor *)field {
  4956. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4957. GPBDataType keyDataType = field.mapKeyDataType;
  4958. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4959. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4960. NSNumber *aValue,
  4961. BOOL *stop) {
  4962. #pragma unused(stop)
  4963. // Write the tag.
  4964. [outputStream writeInt32NoTag:tag];
  4965. // Write the size of the message.
  4966. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4967. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4968. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4969. // Write the fields.
  4970. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4971. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4972. }];
  4973. }
  4974. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4975. forGPBGenericValueKey:(GPBGenericValue *)key {
  4976. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4977. }
  4978. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4979. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4980. #pragma unused(stop)
  4981. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4982. }];
  4983. }
  4984. - (BOOL)valueForKey:(uint64_t)key value:(uint32_t *)value {
  4985. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4986. if (wrapped && value) {
  4987. *value = [wrapped unsignedIntValue];
  4988. }
  4989. return (wrapped != NULL);
  4990. }
  4991. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4992. if (otherDictionary) {
  4993. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4994. if (_autocreator) {
  4995. GPBAutocreatedDictionaryModified(_autocreator, self);
  4996. }
  4997. }
  4998. }
  4999. - (void)setValue:(uint32_t)value forKey:(uint64_t)key {
  5000. [_dictionary setObject:@(value) forKey:@(key)];
  5001. if (_autocreator) {
  5002. GPBAutocreatedDictionaryModified(_autocreator, self);
  5003. }
  5004. }
  5005. - (void)removeValueForKey:(uint64_t)aKey {
  5006. [_dictionary removeObjectForKey:@(aKey)];
  5007. }
  5008. - (void)removeAll {
  5009. [_dictionary removeAllObjects];
  5010. }
  5011. @end
  5012. #pragma mark - UInt64 -> Int32
  5013. @implementation GPBUInt64Int32Dictionary {
  5014. @package
  5015. NSMutableDictionary *_dictionary;
  5016. }
  5017. + (instancetype)dictionary {
  5018. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5019. }
  5020. + (instancetype)dictionaryWithValue:(int32_t)value
  5021. forKey:(uint64_t)key {
  5022. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5023. // on to get the type correct.
  5024. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:&value
  5025. forKeys:&key
  5026. count:1] autorelease];
  5027. }
  5028. + (instancetype)dictionaryWithValues:(const int32_t [])values
  5029. forKeys:(const uint64_t [])keys
  5030. count:(NSUInteger)count {
  5031. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5032. // on to get the type correct.
  5033. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithValues:values
  5034. forKeys:keys
  5035. count:count] autorelease];
  5036. }
  5037. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5038. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5039. // on to get the type correct.
  5040. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5041. }
  5042. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5043. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5044. }
  5045. - (instancetype)init {
  5046. return [self initWithValues:NULL forKeys:NULL count:0];
  5047. }
  5048. - (instancetype)initWithValues:(const int32_t [])values
  5049. forKeys:(const uint64_t [])keys
  5050. count:(NSUInteger)count {
  5051. self = [super init];
  5052. if (self) {
  5053. _dictionary = [[NSMutableDictionary alloc] init];
  5054. if (count && values && keys) {
  5055. for (NSUInteger i = 0; i < count; ++i) {
  5056. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5057. }
  5058. }
  5059. }
  5060. return self;
  5061. }
  5062. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5063. self = [self initWithValues:NULL forKeys:NULL count:0];
  5064. if (self) {
  5065. if (dictionary) {
  5066. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5067. }
  5068. }
  5069. return self;
  5070. }
  5071. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5072. #pragma unused(numItems)
  5073. return [self initWithValues:NULL forKeys:NULL count:0];
  5074. }
  5075. - (void)dealloc {
  5076. NSAssert(!_autocreator,
  5077. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5078. [self class], _autocreator);
  5079. [_dictionary release];
  5080. [super dealloc];
  5081. }
  5082. - (instancetype)copyWithZone:(NSZone *)zone {
  5083. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5084. }
  5085. - (BOOL)isEqual:(GPBUInt64Int32Dictionary *)other {
  5086. if (self == other) {
  5087. return YES;
  5088. }
  5089. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5090. return NO;
  5091. }
  5092. return [_dictionary isEqual:other->_dictionary];
  5093. }
  5094. - (NSUInteger)hash {
  5095. return _dictionary.count;
  5096. }
  5097. - (NSString *)description {
  5098. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5099. }
  5100. - (NSUInteger)count {
  5101. return _dictionary.count;
  5102. }
  5103. - (void)enumerateKeysAndValuesUsingBlock:
  5104. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5105. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5106. NSNumber *aValue,
  5107. BOOL *stop) {
  5108. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5109. }];
  5110. }
  5111. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5112. NSUInteger count = _dictionary.count;
  5113. if (count == 0) {
  5114. return 0;
  5115. }
  5116. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5117. GPBDataType keyDataType = field.mapKeyDataType;
  5118. __block size_t result = 0;
  5119. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5120. NSNumber *aValue,
  5121. BOOL *stop) {
  5122. #pragma unused(stop)
  5123. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5124. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5125. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5126. }];
  5127. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5128. result += tagSize * count;
  5129. return result;
  5130. }
  5131. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5132. asField:(GPBFieldDescriptor *)field {
  5133. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5134. GPBDataType keyDataType = field.mapKeyDataType;
  5135. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5136. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5137. NSNumber *aValue,
  5138. BOOL *stop) {
  5139. #pragma unused(stop)
  5140. // Write the tag.
  5141. [outputStream writeInt32NoTag:tag];
  5142. // Write the size of the message.
  5143. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5144. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5145. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5146. // Write the fields.
  5147. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5148. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5149. }];
  5150. }
  5151. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5152. forGPBGenericValueKey:(GPBGenericValue *)key {
  5153. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5154. }
  5155. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5156. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5157. #pragma unused(stop)
  5158. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5159. }];
  5160. }
  5161. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  5162. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5163. if (wrapped && value) {
  5164. *value = [wrapped intValue];
  5165. }
  5166. return (wrapped != NULL);
  5167. }
  5168. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5169. if (otherDictionary) {
  5170. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5171. if (_autocreator) {
  5172. GPBAutocreatedDictionaryModified(_autocreator, self);
  5173. }
  5174. }
  5175. }
  5176. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  5177. [_dictionary setObject:@(value) forKey:@(key)];
  5178. if (_autocreator) {
  5179. GPBAutocreatedDictionaryModified(_autocreator, self);
  5180. }
  5181. }
  5182. - (void)removeValueForKey:(uint64_t)aKey {
  5183. [_dictionary removeObjectForKey:@(aKey)];
  5184. }
  5185. - (void)removeAll {
  5186. [_dictionary removeAllObjects];
  5187. }
  5188. @end
  5189. #pragma mark - UInt64 -> UInt64
  5190. @implementation GPBUInt64UInt64Dictionary {
  5191. @package
  5192. NSMutableDictionary *_dictionary;
  5193. }
  5194. + (instancetype)dictionary {
  5195. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5196. }
  5197. + (instancetype)dictionaryWithValue:(uint64_t)value
  5198. forKey:(uint64_t)key {
  5199. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5200. // on to get the type correct.
  5201. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  5202. forKeys:&key
  5203. count:1] autorelease];
  5204. }
  5205. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  5206. forKeys:(const uint64_t [])keys
  5207. count:(NSUInteger)count {
  5208. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5209. // on to get the type correct.
  5210. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithValues:values
  5211. forKeys:keys
  5212. count:count] autorelease];
  5213. }
  5214. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5215. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5216. // on to get the type correct.
  5217. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5218. }
  5219. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5220. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5221. }
  5222. - (instancetype)init {
  5223. return [self initWithValues:NULL forKeys:NULL count:0];
  5224. }
  5225. - (instancetype)initWithValues:(const uint64_t [])values
  5226. forKeys:(const uint64_t [])keys
  5227. count:(NSUInteger)count {
  5228. self = [super init];
  5229. if (self) {
  5230. _dictionary = [[NSMutableDictionary alloc] init];
  5231. if (count && values && keys) {
  5232. for (NSUInteger i = 0; i < count; ++i) {
  5233. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5234. }
  5235. }
  5236. }
  5237. return self;
  5238. }
  5239. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5240. self = [self initWithValues:NULL forKeys:NULL count:0];
  5241. if (self) {
  5242. if (dictionary) {
  5243. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5244. }
  5245. }
  5246. return self;
  5247. }
  5248. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5249. #pragma unused(numItems)
  5250. return [self initWithValues:NULL forKeys:NULL count:0];
  5251. }
  5252. - (void)dealloc {
  5253. NSAssert(!_autocreator,
  5254. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5255. [self class], _autocreator);
  5256. [_dictionary release];
  5257. [super dealloc];
  5258. }
  5259. - (instancetype)copyWithZone:(NSZone *)zone {
  5260. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5261. }
  5262. - (BOOL)isEqual:(GPBUInt64UInt64Dictionary *)other {
  5263. if (self == other) {
  5264. return YES;
  5265. }
  5266. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5267. return NO;
  5268. }
  5269. return [_dictionary isEqual:other->_dictionary];
  5270. }
  5271. - (NSUInteger)hash {
  5272. return _dictionary.count;
  5273. }
  5274. - (NSString *)description {
  5275. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5276. }
  5277. - (NSUInteger)count {
  5278. return _dictionary.count;
  5279. }
  5280. - (void)enumerateKeysAndValuesUsingBlock:
  5281. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5282. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5283. NSNumber *aValue,
  5284. BOOL *stop) {
  5285. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5286. }];
  5287. }
  5288. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5289. NSUInteger count = _dictionary.count;
  5290. if (count == 0) {
  5291. return 0;
  5292. }
  5293. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5294. GPBDataType keyDataType = field.mapKeyDataType;
  5295. __block size_t result = 0;
  5296. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5297. NSNumber *aValue,
  5298. BOOL *stop) {
  5299. #pragma unused(stop)
  5300. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5301. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5302. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5303. }];
  5304. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5305. result += tagSize * count;
  5306. return result;
  5307. }
  5308. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5309. asField:(GPBFieldDescriptor *)field {
  5310. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5311. GPBDataType keyDataType = field.mapKeyDataType;
  5312. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5313. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5314. NSNumber *aValue,
  5315. BOOL *stop) {
  5316. #pragma unused(stop)
  5317. // Write the tag.
  5318. [outputStream writeInt32NoTag:tag];
  5319. // Write the size of the message.
  5320. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5321. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5322. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5323. // Write the fields.
  5324. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5325. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5326. }];
  5327. }
  5328. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5329. forGPBGenericValueKey:(GPBGenericValue *)key {
  5330. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5331. }
  5332. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5333. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5334. #pragma unused(stop)
  5335. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5336. }];
  5337. }
  5338. - (BOOL)valueForKey:(uint64_t)key value:(uint64_t *)value {
  5339. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5340. if (wrapped && value) {
  5341. *value = [wrapped unsignedLongLongValue];
  5342. }
  5343. return (wrapped != NULL);
  5344. }
  5345. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5346. if (otherDictionary) {
  5347. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5348. if (_autocreator) {
  5349. GPBAutocreatedDictionaryModified(_autocreator, self);
  5350. }
  5351. }
  5352. }
  5353. - (void)setValue:(uint64_t)value forKey:(uint64_t)key {
  5354. [_dictionary setObject:@(value) forKey:@(key)];
  5355. if (_autocreator) {
  5356. GPBAutocreatedDictionaryModified(_autocreator, self);
  5357. }
  5358. }
  5359. - (void)removeValueForKey:(uint64_t)aKey {
  5360. [_dictionary removeObjectForKey:@(aKey)];
  5361. }
  5362. - (void)removeAll {
  5363. [_dictionary removeAllObjects];
  5364. }
  5365. @end
  5366. #pragma mark - UInt64 -> Int64
  5367. @implementation GPBUInt64Int64Dictionary {
  5368. @package
  5369. NSMutableDictionary *_dictionary;
  5370. }
  5371. + (instancetype)dictionary {
  5372. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5373. }
  5374. + (instancetype)dictionaryWithValue:(int64_t)value
  5375. forKey:(uint64_t)key {
  5376. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5377. // on to get the type correct.
  5378. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:&value
  5379. forKeys:&key
  5380. count:1] autorelease];
  5381. }
  5382. + (instancetype)dictionaryWithValues:(const int64_t [])values
  5383. forKeys:(const uint64_t [])keys
  5384. count:(NSUInteger)count {
  5385. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5386. // on to get the type correct.
  5387. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithValues:values
  5388. forKeys:keys
  5389. count:count] autorelease];
  5390. }
  5391. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5392. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5393. // on to get the type correct.
  5394. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5395. }
  5396. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5397. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5398. }
  5399. - (instancetype)init {
  5400. return [self initWithValues:NULL forKeys:NULL count:0];
  5401. }
  5402. - (instancetype)initWithValues:(const int64_t [])values
  5403. forKeys:(const uint64_t [])keys
  5404. count:(NSUInteger)count {
  5405. self = [super init];
  5406. if (self) {
  5407. _dictionary = [[NSMutableDictionary alloc] init];
  5408. if (count && values && keys) {
  5409. for (NSUInteger i = 0; i < count; ++i) {
  5410. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5411. }
  5412. }
  5413. }
  5414. return self;
  5415. }
  5416. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5417. self = [self initWithValues:NULL forKeys:NULL count:0];
  5418. if (self) {
  5419. if (dictionary) {
  5420. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5421. }
  5422. }
  5423. return self;
  5424. }
  5425. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5426. #pragma unused(numItems)
  5427. return [self initWithValues:NULL forKeys:NULL count:0];
  5428. }
  5429. - (void)dealloc {
  5430. NSAssert(!_autocreator,
  5431. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5432. [self class], _autocreator);
  5433. [_dictionary release];
  5434. [super dealloc];
  5435. }
  5436. - (instancetype)copyWithZone:(NSZone *)zone {
  5437. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5438. }
  5439. - (BOOL)isEqual:(GPBUInt64Int64Dictionary *)other {
  5440. if (self == other) {
  5441. return YES;
  5442. }
  5443. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5444. return NO;
  5445. }
  5446. return [_dictionary isEqual:other->_dictionary];
  5447. }
  5448. - (NSUInteger)hash {
  5449. return _dictionary.count;
  5450. }
  5451. - (NSString *)description {
  5452. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5453. }
  5454. - (NSUInteger)count {
  5455. return _dictionary.count;
  5456. }
  5457. - (void)enumerateKeysAndValuesUsingBlock:
  5458. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5460. NSNumber *aValue,
  5461. BOOL *stop) {
  5462. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5463. }];
  5464. }
  5465. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5466. NSUInteger count = _dictionary.count;
  5467. if (count == 0) {
  5468. return 0;
  5469. }
  5470. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5471. GPBDataType keyDataType = field.mapKeyDataType;
  5472. __block size_t result = 0;
  5473. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5474. NSNumber *aValue,
  5475. BOOL *stop) {
  5476. #pragma unused(stop)
  5477. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5478. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5479. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5480. }];
  5481. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5482. result += tagSize * count;
  5483. return result;
  5484. }
  5485. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5486. asField:(GPBFieldDescriptor *)field {
  5487. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5488. GPBDataType keyDataType = field.mapKeyDataType;
  5489. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5490. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5491. NSNumber *aValue,
  5492. BOOL *stop) {
  5493. #pragma unused(stop)
  5494. // Write the tag.
  5495. [outputStream writeInt32NoTag:tag];
  5496. // Write the size of the message.
  5497. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5498. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5499. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5500. // Write the fields.
  5501. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5502. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5503. }];
  5504. }
  5505. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5506. forGPBGenericValueKey:(GPBGenericValue *)key {
  5507. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5508. }
  5509. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5510. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5511. #pragma unused(stop)
  5512. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5513. }];
  5514. }
  5515. - (BOOL)valueForKey:(uint64_t)key value:(int64_t *)value {
  5516. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5517. if (wrapped && value) {
  5518. *value = [wrapped longLongValue];
  5519. }
  5520. return (wrapped != NULL);
  5521. }
  5522. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5523. if (otherDictionary) {
  5524. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5525. if (_autocreator) {
  5526. GPBAutocreatedDictionaryModified(_autocreator, self);
  5527. }
  5528. }
  5529. }
  5530. - (void)setValue:(int64_t)value forKey:(uint64_t)key {
  5531. [_dictionary setObject:@(value) forKey:@(key)];
  5532. if (_autocreator) {
  5533. GPBAutocreatedDictionaryModified(_autocreator, self);
  5534. }
  5535. }
  5536. - (void)removeValueForKey:(uint64_t)aKey {
  5537. [_dictionary removeObjectForKey:@(aKey)];
  5538. }
  5539. - (void)removeAll {
  5540. [_dictionary removeAllObjects];
  5541. }
  5542. @end
  5543. #pragma mark - UInt64 -> Bool
  5544. @implementation GPBUInt64BoolDictionary {
  5545. @package
  5546. NSMutableDictionary *_dictionary;
  5547. }
  5548. + (instancetype)dictionary {
  5549. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5550. }
  5551. + (instancetype)dictionaryWithValue:(BOOL)value
  5552. forKey:(uint64_t)key {
  5553. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5554. // on to get the type correct.
  5555. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:&value
  5556. forKeys:&key
  5557. count:1] autorelease];
  5558. }
  5559. + (instancetype)dictionaryWithValues:(const BOOL [])values
  5560. forKeys:(const uint64_t [])keys
  5561. count:(NSUInteger)count {
  5562. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5563. // on to get the type correct.
  5564. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithValues:values
  5565. forKeys:keys
  5566. count:count] autorelease];
  5567. }
  5568. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5569. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5570. // on to get the type correct.
  5571. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5572. }
  5573. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5574. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5575. }
  5576. - (instancetype)init {
  5577. return [self initWithValues:NULL forKeys:NULL count:0];
  5578. }
  5579. - (instancetype)initWithValues:(const BOOL [])values
  5580. forKeys:(const uint64_t [])keys
  5581. count:(NSUInteger)count {
  5582. self = [super init];
  5583. if (self) {
  5584. _dictionary = [[NSMutableDictionary alloc] init];
  5585. if (count && values && keys) {
  5586. for (NSUInteger i = 0; i < count; ++i) {
  5587. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5588. }
  5589. }
  5590. }
  5591. return self;
  5592. }
  5593. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5594. self = [self initWithValues:NULL forKeys:NULL count:0];
  5595. if (self) {
  5596. if (dictionary) {
  5597. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5598. }
  5599. }
  5600. return self;
  5601. }
  5602. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5603. #pragma unused(numItems)
  5604. return [self initWithValues:NULL forKeys:NULL count:0];
  5605. }
  5606. - (void)dealloc {
  5607. NSAssert(!_autocreator,
  5608. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5609. [self class], _autocreator);
  5610. [_dictionary release];
  5611. [super dealloc];
  5612. }
  5613. - (instancetype)copyWithZone:(NSZone *)zone {
  5614. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5615. }
  5616. - (BOOL)isEqual:(GPBUInt64BoolDictionary *)other {
  5617. if (self == other) {
  5618. return YES;
  5619. }
  5620. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5621. return NO;
  5622. }
  5623. return [_dictionary isEqual:other->_dictionary];
  5624. }
  5625. - (NSUInteger)hash {
  5626. return _dictionary.count;
  5627. }
  5628. - (NSString *)description {
  5629. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5630. }
  5631. - (NSUInteger)count {
  5632. return _dictionary.count;
  5633. }
  5634. - (void)enumerateKeysAndValuesUsingBlock:
  5635. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5636. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5637. NSNumber *aValue,
  5638. BOOL *stop) {
  5639. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5640. }];
  5641. }
  5642. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5643. NSUInteger count = _dictionary.count;
  5644. if (count == 0) {
  5645. return 0;
  5646. }
  5647. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5648. GPBDataType keyDataType = field.mapKeyDataType;
  5649. __block size_t result = 0;
  5650. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5651. NSNumber *aValue,
  5652. BOOL *stop) {
  5653. #pragma unused(stop)
  5654. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5655. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5656. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5657. }];
  5658. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5659. result += tagSize * count;
  5660. return result;
  5661. }
  5662. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5663. asField:(GPBFieldDescriptor *)field {
  5664. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5665. GPBDataType keyDataType = field.mapKeyDataType;
  5666. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5667. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5668. NSNumber *aValue,
  5669. BOOL *stop) {
  5670. #pragma unused(stop)
  5671. // Write the tag.
  5672. [outputStream writeInt32NoTag:tag];
  5673. // Write the size of the message.
  5674. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5675. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5676. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5677. // Write the fields.
  5678. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5679. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5680. }];
  5681. }
  5682. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5683. forGPBGenericValueKey:(GPBGenericValue *)key {
  5684. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5685. }
  5686. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5687. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5688. #pragma unused(stop)
  5689. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5690. }];
  5691. }
  5692. - (BOOL)valueForKey:(uint64_t)key value:(BOOL *)value {
  5693. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5694. if (wrapped && value) {
  5695. *value = [wrapped boolValue];
  5696. }
  5697. return (wrapped != NULL);
  5698. }
  5699. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5700. if (otherDictionary) {
  5701. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5702. if (_autocreator) {
  5703. GPBAutocreatedDictionaryModified(_autocreator, self);
  5704. }
  5705. }
  5706. }
  5707. - (void)setValue:(BOOL)value forKey:(uint64_t)key {
  5708. [_dictionary setObject:@(value) forKey:@(key)];
  5709. if (_autocreator) {
  5710. GPBAutocreatedDictionaryModified(_autocreator, self);
  5711. }
  5712. }
  5713. - (void)removeValueForKey:(uint64_t)aKey {
  5714. [_dictionary removeObjectForKey:@(aKey)];
  5715. }
  5716. - (void)removeAll {
  5717. [_dictionary removeAllObjects];
  5718. }
  5719. @end
  5720. #pragma mark - UInt64 -> Float
  5721. @implementation GPBUInt64FloatDictionary {
  5722. @package
  5723. NSMutableDictionary *_dictionary;
  5724. }
  5725. + (instancetype)dictionary {
  5726. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5727. }
  5728. + (instancetype)dictionaryWithValue:(float)value
  5729. forKey:(uint64_t)key {
  5730. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5731. // on to get the type correct.
  5732. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:&value
  5733. forKeys:&key
  5734. count:1] autorelease];
  5735. }
  5736. + (instancetype)dictionaryWithValues:(const float [])values
  5737. forKeys:(const uint64_t [])keys
  5738. count:(NSUInteger)count {
  5739. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5740. // on to get the type correct.
  5741. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithValues:values
  5742. forKeys:keys
  5743. count:count] autorelease];
  5744. }
  5745. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5746. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5747. // on to get the type correct.
  5748. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5749. }
  5750. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5751. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5752. }
  5753. - (instancetype)init {
  5754. return [self initWithValues:NULL forKeys:NULL count:0];
  5755. }
  5756. - (instancetype)initWithValues:(const float [])values
  5757. forKeys:(const uint64_t [])keys
  5758. count:(NSUInteger)count {
  5759. self = [super init];
  5760. if (self) {
  5761. _dictionary = [[NSMutableDictionary alloc] init];
  5762. if (count && values && keys) {
  5763. for (NSUInteger i = 0; i < count; ++i) {
  5764. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5765. }
  5766. }
  5767. }
  5768. return self;
  5769. }
  5770. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5771. self = [self initWithValues:NULL forKeys:NULL count:0];
  5772. if (self) {
  5773. if (dictionary) {
  5774. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5775. }
  5776. }
  5777. return self;
  5778. }
  5779. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5780. #pragma unused(numItems)
  5781. return [self initWithValues:NULL forKeys:NULL count:0];
  5782. }
  5783. - (void)dealloc {
  5784. NSAssert(!_autocreator,
  5785. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5786. [self class], _autocreator);
  5787. [_dictionary release];
  5788. [super dealloc];
  5789. }
  5790. - (instancetype)copyWithZone:(NSZone *)zone {
  5791. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5792. }
  5793. - (BOOL)isEqual:(GPBUInt64FloatDictionary *)other {
  5794. if (self == other) {
  5795. return YES;
  5796. }
  5797. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5798. return NO;
  5799. }
  5800. return [_dictionary isEqual:other->_dictionary];
  5801. }
  5802. - (NSUInteger)hash {
  5803. return _dictionary.count;
  5804. }
  5805. - (NSString *)description {
  5806. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5807. }
  5808. - (NSUInteger)count {
  5809. return _dictionary.count;
  5810. }
  5811. - (void)enumerateKeysAndValuesUsingBlock:
  5812. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5813. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5814. NSNumber *aValue,
  5815. BOOL *stop) {
  5816. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5817. }];
  5818. }
  5819. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5820. NSUInteger count = _dictionary.count;
  5821. if (count == 0) {
  5822. return 0;
  5823. }
  5824. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5825. GPBDataType keyDataType = field.mapKeyDataType;
  5826. __block size_t result = 0;
  5827. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5828. NSNumber *aValue,
  5829. BOOL *stop) {
  5830. #pragma unused(stop)
  5831. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5832. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5833. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5834. }];
  5835. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5836. result += tagSize * count;
  5837. return result;
  5838. }
  5839. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5840. asField:(GPBFieldDescriptor *)field {
  5841. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5842. GPBDataType keyDataType = field.mapKeyDataType;
  5843. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5844. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5845. NSNumber *aValue,
  5846. BOOL *stop) {
  5847. #pragma unused(stop)
  5848. // Write the tag.
  5849. [outputStream writeInt32NoTag:tag];
  5850. // Write the size of the message.
  5851. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5852. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5853. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5854. // Write the fields.
  5855. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5856. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5857. }];
  5858. }
  5859. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5860. forGPBGenericValueKey:(GPBGenericValue *)key {
  5861. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5862. }
  5863. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5864. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5865. #pragma unused(stop)
  5866. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5867. }];
  5868. }
  5869. - (BOOL)valueForKey:(uint64_t)key value:(float *)value {
  5870. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5871. if (wrapped && value) {
  5872. *value = [wrapped floatValue];
  5873. }
  5874. return (wrapped != NULL);
  5875. }
  5876. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5877. if (otherDictionary) {
  5878. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5879. if (_autocreator) {
  5880. GPBAutocreatedDictionaryModified(_autocreator, self);
  5881. }
  5882. }
  5883. }
  5884. - (void)setValue:(float)value forKey:(uint64_t)key {
  5885. [_dictionary setObject:@(value) forKey:@(key)];
  5886. if (_autocreator) {
  5887. GPBAutocreatedDictionaryModified(_autocreator, self);
  5888. }
  5889. }
  5890. - (void)removeValueForKey:(uint64_t)aKey {
  5891. [_dictionary removeObjectForKey:@(aKey)];
  5892. }
  5893. - (void)removeAll {
  5894. [_dictionary removeAllObjects];
  5895. }
  5896. @end
  5897. #pragma mark - UInt64 -> Double
  5898. @implementation GPBUInt64DoubleDictionary {
  5899. @package
  5900. NSMutableDictionary *_dictionary;
  5901. }
  5902. + (instancetype)dictionary {
  5903. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  5904. }
  5905. + (instancetype)dictionaryWithValue:(double)value
  5906. forKey:(uint64_t)key {
  5907. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5908. // on to get the type correct.
  5909. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:&value
  5910. forKeys:&key
  5911. count:1] autorelease];
  5912. }
  5913. + (instancetype)dictionaryWithValues:(const double [])values
  5914. forKeys:(const uint64_t [])keys
  5915. count:(NSUInteger)count {
  5916. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  5917. // on to get the type correct.
  5918. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithValues:values
  5919. forKeys:keys
  5920. count:count] autorelease];
  5921. }
  5922. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5923. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5924. // on to get the type correct.
  5925. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5926. }
  5927. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5928. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5929. }
  5930. - (instancetype)init {
  5931. return [self initWithValues:NULL forKeys:NULL count:0];
  5932. }
  5933. - (instancetype)initWithValues:(const double [])values
  5934. forKeys:(const uint64_t [])keys
  5935. count:(NSUInteger)count {
  5936. self = [super init];
  5937. if (self) {
  5938. _dictionary = [[NSMutableDictionary alloc] init];
  5939. if (count && values && keys) {
  5940. for (NSUInteger i = 0; i < count; ++i) {
  5941. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5942. }
  5943. }
  5944. }
  5945. return self;
  5946. }
  5947. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5948. self = [self initWithValues:NULL forKeys:NULL count:0];
  5949. if (self) {
  5950. if (dictionary) {
  5951. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5952. }
  5953. }
  5954. return self;
  5955. }
  5956. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5957. #pragma unused(numItems)
  5958. return [self initWithValues:NULL forKeys:NULL count:0];
  5959. }
  5960. - (void)dealloc {
  5961. NSAssert(!_autocreator,
  5962. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5963. [self class], _autocreator);
  5964. [_dictionary release];
  5965. [super dealloc];
  5966. }
  5967. - (instancetype)copyWithZone:(NSZone *)zone {
  5968. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5969. }
  5970. - (BOOL)isEqual:(GPBUInt64DoubleDictionary *)other {
  5971. if (self == other) {
  5972. return YES;
  5973. }
  5974. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5975. return NO;
  5976. }
  5977. return [_dictionary isEqual:other->_dictionary];
  5978. }
  5979. - (NSUInteger)hash {
  5980. return _dictionary.count;
  5981. }
  5982. - (NSString *)description {
  5983. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5984. }
  5985. - (NSUInteger)count {
  5986. return _dictionary.count;
  5987. }
  5988. - (void)enumerateKeysAndValuesUsingBlock:
  5989. (void (^)(uint64_t key, double value, BOOL *stop))block {
  5990. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5991. NSNumber *aValue,
  5992. BOOL *stop) {
  5993. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  5994. }];
  5995. }
  5996. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5997. NSUInteger count = _dictionary.count;
  5998. if (count == 0) {
  5999. return 0;
  6000. }
  6001. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6002. GPBDataType keyDataType = field.mapKeyDataType;
  6003. __block size_t result = 0;
  6004. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6005. NSNumber *aValue,
  6006. BOOL *stop) {
  6007. #pragma unused(stop)
  6008. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6009. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6010. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6011. }];
  6012. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6013. result += tagSize * count;
  6014. return result;
  6015. }
  6016. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6017. asField:(GPBFieldDescriptor *)field {
  6018. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6019. GPBDataType keyDataType = field.mapKeyDataType;
  6020. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6021. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6022. NSNumber *aValue,
  6023. BOOL *stop) {
  6024. #pragma unused(stop)
  6025. // Write the tag.
  6026. [outputStream writeInt32NoTag:tag];
  6027. // Write the size of the message.
  6028. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6029. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6030. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6031. // Write the fields.
  6032. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6033. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6034. }];
  6035. }
  6036. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6037. forGPBGenericValueKey:(GPBGenericValue *)key {
  6038. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  6039. }
  6040. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6041. [self enumerateKeysAndValuesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  6042. #pragma unused(stop)
  6043. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6044. }];
  6045. }
  6046. - (BOOL)valueForKey:(uint64_t)key value:(double *)value {
  6047. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6048. if (wrapped && value) {
  6049. *value = [wrapped doubleValue];
  6050. }
  6051. return (wrapped != NULL);
  6052. }
  6053. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  6054. if (otherDictionary) {
  6055. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6056. if (_autocreator) {
  6057. GPBAutocreatedDictionaryModified(_autocreator, self);
  6058. }
  6059. }
  6060. }
  6061. - (void)setValue:(double)value forKey:(uint64_t)key {
  6062. [_dictionary setObject:@(value) forKey:@(key)];
  6063. if (_autocreator) {
  6064. GPBAutocreatedDictionaryModified(_autocreator, self);
  6065. }
  6066. }
  6067. - (void)removeValueForKey:(uint64_t)aKey {
  6068. [_dictionary removeObjectForKey:@(aKey)];
  6069. }
  6070. - (void)removeAll {
  6071. [_dictionary removeAllObjects];
  6072. }
  6073. @end
  6074. #pragma mark - UInt64 -> Enum
  6075. @implementation GPBUInt64EnumDictionary {
  6076. @package
  6077. NSMutableDictionary *_dictionary;
  6078. GPBEnumValidationFunc _validationFunc;
  6079. }
  6080. @synthesize validationFunc = _validationFunc;
  6081. + (instancetype)dictionary {
  6082. return [[[self alloc] initWithValidationFunction:NULL
  6083. rawValues:NULL
  6084. forKeys:NULL
  6085. count:0] autorelease];
  6086. }
  6087. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6088. return [[[self alloc] initWithValidationFunction:func
  6089. rawValues:NULL
  6090. forKeys:NULL
  6091. count:0] autorelease];
  6092. }
  6093. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6094. rawValue:(int32_t)rawValue
  6095. forKey:(uint64_t)key {
  6096. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6097. // on to get the type correct.
  6098. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6099. rawValues:&rawValue
  6100. forKeys:&key
  6101. count:1] autorelease];
  6102. }
  6103. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6104. rawValues:(const int32_t [])rawValues
  6105. forKeys:(const uint64_t [])keys
  6106. count:(NSUInteger)count {
  6107. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6108. // on to get the type correct.
  6109. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6110. rawValues:rawValues
  6111. forKeys:keys
  6112. count:count] autorelease];
  6113. }
  6114. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6115. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6116. // on to get the type correct.
  6117. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6118. }
  6119. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6120. capacity:(NSUInteger)numItems {
  6121. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6122. }
  6123. - (instancetype)init {
  6124. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6125. }
  6126. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6127. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6128. }
  6129. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6130. rawValues:(const int32_t [])rawValues
  6131. forKeys:(const uint64_t [])keys
  6132. count:(NSUInteger)count {
  6133. self = [super init];
  6134. if (self) {
  6135. _dictionary = [[NSMutableDictionary alloc] init];
  6136. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6137. if (count && rawValues && keys) {
  6138. for (NSUInteger i = 0; i < count; ++i) {
  6139. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6140. }
  6141. }
  6142. }
  6143. return self;
  6144. }
  6145. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6146. self = [self initWithValidationFunction:dictionary.validationFunc
  6147. rawValues:NULL
  6148. forKeys:NULL
  6149. count:0];
  6150. if (self) {
  6151. if (dictionary) {
  6152. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6153. }
  6154. }
  6155. return self;
  6156. }
  6157. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6158. capacity:(NSUInteger)numItems {
  6159. #pragma unused(numItems)
  6160. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6161. }
  6162. - (void)dealloc {
  6163. NSAssert(!_autocreator,
  6164. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6165. [self class], _autocreator);
  6166. [_dictionary release];
  6167. [super dealloc];
  6168. }
  6169. - (instancetype)copyWithZone:(NSZone *)zone {
  6170. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6171. }
  6172. - (BOOL)isEqual:(GPBUInt64EnumDictionary *)other {
  6173. if (self == other) {
  6174. return YES;
  6175. }
  6176. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6177. return NO;
  6178. }
  6179. return [_dictionary isEqual:other->_dictionary];
  6180. }
  6181. - (NSUInteger)hash {
  6182. return _dictionary.count;
  6183. }
  6184. - (NSString *)description {
  6185. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6186. }
  6187. - (NSUInteger)count {
  6188. return _dictionary.count;
  6189. }
  6190. - (void)enumerateKeysAndRawValuesUsingBlock:
  6191. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6192. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6193. NSNumber *aValue,
  6194. BOOL *stop) {
  6195. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6196. }];
  6197. }
  6198. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6199. NSUInteger count = _dictionary.count;
  6200. if (count == 0) {
  6201. return 0;
  6202. }
  6203. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6204. GPBDataType keyDataType = field.mapKeyDataType;
  6205. __block size_t result = 0;
  6206. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6207. NSNumber *aValue,
  6208. BOOL *stop) {
  6209. #pragma unused(stop)
  6210. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6211. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6212. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6213. }];
  6214. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6215. result += tagSize * count;
  6216. return result;
  6217. }
  6218. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6219. asField:(GPBFieldDescriptor *)field {
  6220. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6221. GPBDataType keyDataType = field.mapKeyDataType;
  6222. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6223. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6224. NSNumber *aValue,
  6225. BOOL *stop) {
  6226. #pragma unused(stop)
  6227. // Write the tag.
  6228. [outputStream writeInt32NoTag:tag];
  6229. // Write the size of the message.
  6230. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6231. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6232. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6233. // Write the fields.
  6234. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6235. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6236. }];
  6237. }
  6238. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6239. forKey:(GPBGenericValue *)key
  6240. keyDataType:(GPBDataType)keyDataType {
  6241. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6242. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6243. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6244. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6245. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6246. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6247. [outputStream release];
  6248. return data;
  6249. }
  6250. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6251. forGPBGenericValueKey:(GPBGenericValue *)key {
  6252. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6253. }
  6254. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6255. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6256. #pragma unused(stop)
  6257. block([NSString stringWithFormat:@"%llu", key], @(value));
  6258. }];
  6259. }
  6260. - (BOOL)valueForKey:(uint64_t)key value:(int32_t *)value {
  6261. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6262. if (wrapped && value) {
  6263. int32_t result = [wrapped intValue];
  6264. if (!_validationFunc(result)) {
  6265. result = kGPBUnrecognizedEnumeratorValue;
  6266. }
  6267. *value = result;
  6268. }
  6269. return (wrapped != NULL);
  6270. }
  6271. - (BOOL)valueForKey:(uint64_t)key rawValue:(int32_t *)rawValue {
  6272. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6273. if (wrapped && rawValue) {
  6274. *rawValue = [wrapped intValue];
  6275. }
  6276. return (wrapped != NULL);
  6277. }
  6278. - (void)enumerateKeysAndValuesUsingBlock:
  6279. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6280. GPBEnumValidationFunc func = _validationFunc;
  6281. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6282. NSNumber *aValue,
  6283. BOOL *stop) {
  6284. int32_t unwrapped = [aValue intValue];
  6285. if (!func(unwrapped)) {
  6286. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6287. }
  6288. block([aKey unsignedLongLongValue], unwrapped, stop);
  6289. }];
  6290. }
  6291. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6292. if (otherDictionary) {
  6293. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6294. if (_autocreator) {
  6295. GPBAutocreatedDictionaryModified(_autocreator, self);
  6296. }
  6297. }
  6298. }
  6299. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6300. [_dictionary setObject:@(value) forKey:@(key)];
  6301. if (_autocreator) {
  6302. GPBAutocreatedDictionaryModified(_autocreator, self);
  6303. }
  6304. }
  6305. - (void)removeValueForKey:(uint64_t)aKey {
  6306. [_dictionary removeObjectForKey:@(aKey)];
  6307. }
  6308. - (void)removeAll {
  6309. [_dictionary removeAllObjects];
  6310. }
  6311. - (void)setValue:(int32_t)value forKey:(uint64_t)key {
  6312. if (!_validationFunc(value)) {
  6313. [NSException raise:NSInvalidArgumentException
  6314. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6315. value];
  6316. }
  6317. [_dictionary setObject:@(value) forKey:@(key)];
  6318. if (_autocreator) {
  6319. GPBAutocreatedDictionaryModified(_autocreator, self);
  6320. }
  6321. }
  6322. @end
  6323. #pragma mark - UInt64 -> Object
  6324. @implementation GPBUInt64ObjectDictionary {
  6325. @package
  6326. NSMutableDictionary *_dictionary;
  6327. }
  6328. + (instancetype)dictionary {
  6329. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6330. }
  6331. + (instancetype)dictionaryWithObject:(id)object
  6332. forKey:(uint64_t)key {
  6333. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6334. // on to get the type correct.
  6335. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6336. forKeys:&key
  6337. count:1] autorelease];
  6338. }
  6339. + (instancetype)dictionaryWithObjects:(const id [])objects
  6340. forKeys:(const uint64_t [])keys
  6341. count:(NSUInteger)count {
  6342. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6343. // on to get the type correct.
  6344. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6345. forKeys:keys
  6346. count:count] autorelease];
  6347. }
  6348. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6349. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6350. // on to get the type correct.
  6351. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6352. }
  6353. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6354. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6355. }
  6356. - (instancetype)init {
  6357. return [self initWithObjects:NULL forKeys:NULL count:0];
  6358. }
  6359. - (instancetype)initWithObjects:(const id [])objects
  6360. forKeys:(const uint64_t [])keys
  6361. count:(NSUInteger)count {
  6362. self = [super init];
  6363. if (self) {
  6364. _dictionary = [[NSMutableDictionary alloc] init];
  6365. if (count && objects && keys) {
  6366. for (NSUInteger i = 0; i < count; ++i) {
  6367. if (!objects[i]) {
  6368. [NSException raise:NSInvalidArgumentException
  6369. format:@"Attempting to add nil object to a Dictionary"];
  6370. }
  6371. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6372. }
  6373. }
  6374. }
  6375. return self;
  6376. }
  6377. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6378. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6379. if (self) {
  6380. if (dictionary) {
  6381. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6382. }
  6383. }
  6384. return self;
  6385. }
  6386. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6387. #pragma unused(numItems)
  6388. return [self initWithObjects:NULL forKeys:NULL count:0];
  6389. }
  6390. - (void)dealloc {
  6391. NSAssert(!_autocreator,
  6392. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6393. [self class], _autocreator);
  6394. [_dictionary release];
  6395. [super dealloc];
  6396. }
  6397. - (instancetype)copyWithZone:(NSZone *)zone {
  6398. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6399. }
  6400. - (BOOL)isEqual:(GPBUInt64ObjectDictionary *)other {
  6401. if (self == other) {
  6402. return YES;
  6403. }
  6404. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6405. return NO;
  6406. }
  6407. return [_dictionary isEqual:other->_dictionary];
  6408. }
  6409. - (NSUInteger)hash {
  6410. return _dictionary.count;
  6411. }
  6412. - (NSString *)description {
  6413. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6414. }
  6415. - (NSUInteger)count {
  6416. return _dictionary.count;
  6417. }
  6418. - (void)enumerateKeysAndObjectsUsingBlock:
  6419. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6420. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6421. id aObject,
  6422. BOOL *stop) {
  6423. block([aKey unsignedLongLongValue], aObject, stop);
  6424. }];
  6425. }
  6426. - (BOOL)isInitialized {
  6427. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6428. if (!msg.initialized) {
  6429. return NO;
  6430. }
  6431. }
  6432. return YES;
  6433. }
  6434. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6435. GPBUInt64ObjectDictionary *newDict =
  6436. [[GPBUInt64ObjectDictionary alloc] init];
  6437. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6438. GPBMessage *msg,
  6439. BOOL *stop) {
  6440. #pragma unused(stop)
  6441. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6442. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6443. [copiedMsg release];
  6444. }];
  6445. return newDict;
  6446. }
  6447. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6448. NSUInteger count = _dictionary.count;
  6449. if (count == 0) {
  6450. return 0;
  6451. }
  6452. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6453. GPBDataType keyDataType = field.mapKeyDataType;
  6454. __block size_t result = 0;
  6455. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6456. id aObject,
  6457. BOOL *stop) {
  6458. #pragma unused(stop)
  6459. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6460. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6461. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6462. }];
  6463. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6464. result += tagSize * count;
  6465. return result;
  6466. }
  6467. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6468. asField:(GPBFieldDescriptor *)field {
  6469. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6470. GPBDataType keyDataType = field.mapKeyDataType;
  6471. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6472. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6473. id aObject,
  6474. BOOL *stop) {
  6475. #pragma unused(stop)
  6476. // Write the tag.
  6477. [outputStream writeInt32NoTag:tag];
  6478. // Write the size of the message.
  6479. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6480. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6481. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6482. // Write the fields.
  6483. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6484. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  6485. }];
  6486. }
  6487. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6488. forGPBGenericValueKey:(GPBGenericValue *)key {
  6489. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6490. }
  6491. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6492. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6493. #pragma unused(stop)
  6494. block([NSString stringWithFormat:@"%llu", key], object);
  6495. }];
  6496. }
  6497. - (id)objectForKey:(uint64_t)key {
  6498. id result = [_dictionary objectForKey:@(key)];
  6499. return result;
  6500. }
  6501. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6502. if (otherDictionary) {
  6503. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6504. if (_autocreator) {
  6505. GPBAutocreatedDictionaryModified(_autocreator, self);
  6506. }
  6507. }
  6508. }
  6509. - (void)setObject:(id)object forKey:(uint64_t)key {
  6510. if (!object) {
  6511. [NSException raise:NSInvalidArgumentException
  6512. format:@"Attempting to add nil object to a Dictionary"];
  6513. }
  6514. [_dictionary setObject:object forKey:@(key)];
  6515. if (_autocreator) {
  6516. GPBAutocreatedDictionaryModified(_autocreator, self);
  6517. }
  6518. }
  6519. - (void)removeObjectForKey:(uint64_t)aKey {
  6520. [_dictionary removeObjectForKey:@(aKey)];
  6521. }
  6522. - (void)removeAll {
  6523. [_dictionary removeAllObjects];
  6524. }
  6525. @end
  6526. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6527. // This block of code is generated, do not edit it directly.
  6528. #pragma mark - Int64 -> UInt32
  6529. @implementation GPBInt64UInt32Dictionary {
  6530. @package
  6531. NSMutableDictionary *_dictionary;
  6532. }
  6533. + (instancetype)dictionary {
  6534. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6535. }
  6536. + (instancetype)dictionaryWithValue:(uint32_t)value
  6537. forKey:(int64_t)key {
  6538. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6539. // on to get the type correct.
  6540. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:&value
  6541. forKeys:&key
  6542. count:1] autorelease];
  6543. }
  6544. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  6545. forKeys:(const int64_t [])keys
  6546. count:(NSUInteger)count {
  6547. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6548. // on to get the type correct.
  6549. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithValues:values
  6550. forKeys:keys
  6551. count:count] autorelease];
  6552. }
  6553. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6554. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6555. // on to get the type correct.
  6556. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6557. }
  6558. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6559. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6560. }
  6561. - (instancetype)init {
  6562. return [self initWithValues:NULL forKeys:NULL count:0];
  6563. }
  6564. - (instancetype)initWithValues:(const uint32_t [])values
  6565. forKeys:(const int64_t [])keys
  6566. count:(NSUInteger)count {
  6567. self = [super init];
  6568. if (self) {
  6569. _dictionary = [[NSMutableDictionary alloc] init];
  6570. if (count && values && keys) {
  6571. for (NSUInteger i = 0; i < count; ++i) {
  6572. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6573. }
  6574. }
  6575. }
  6576. return self;
  6577. }
  6578. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6579. self = [self initWithValues:NULL forKeys:NULL count:0];
  6580. if (self) {
  6581. if (dictionary) {
  6582. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6583. }
  6584. }
  6585. return self;
  6586. }
  6587. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6588. #pragma unused(numItems)
  6589. return [self initWithValues:NULL forKeys:NULL count:0];
  6590. }
  6591. - (void)dealloc {
  6592. NSAssert(!_autocreator,
  6593. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6594. [self class], _autocreator);
  6595. [_dictionary release];
  6596. [super dealloc];
  6597. }
  6598. - (instancetype)copyWithZone:(NSZone *)zone {
  6599. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6600. }
  6601. - (BOOL)isEqual:(GPBInt64UInt32Dictionary *)other {
  6602. if (self == other) {
  6603. return YES;
  6604. }
  6605. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6606. return NO;
  6607. }
  6608. return [_dictionary isEqual:other->_dictionary];
  6609. }
  6610. - (NSUInteger)hash {
  6611. return _dictionary.count;
  6612. }
  6613. - (NSString *)description {
  6614. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6615. }
  6616. - (NSUInteger)count {
  6617. return _dictionary.count;
  6618. }
  6619. - (void)enumerateKeysAndValuesUsingBlock:
  6620. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6621. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6622. NSNumber *aValue,
  6623. BOOL *stop) {
  6624. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6625. }];
  6626. }
  6627. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6628. NSUInteger count = _dictionary.count;
  6629. if (count == 0) {
  6630. return 0;
  6631. }
  6632. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6633. GPBDataType keyDataType = field.mapKeyDataType;
  6634. __block size_t result = 0;
  6635. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6636. NSNumber *aValue,
  6637. BOOL *stop) {
  6638. #pragma unused(stop)
  6639. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6640. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6641. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6642. }];
  6643. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6644. result += tagSize * count;
  6645. return result;
  6646. }
  6647. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6648. asField:(GPBFieldDescriptor *)field {
  6649. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6650. GPBDataType keyDataType = field.mapKeyDataType;
  6651. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6652. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6653. NSNumber *aValue,
  6654. BOOL *stop) {
  6655. #pragma unused(stop)
  6656. // Write the tag.
  6657. [outputStream writeInt32NoTag:tag];
  6658. // Write the size of the message.
  6659. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6660. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6661. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6662. // Write the fields.
  6663. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6664. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6665. }];
  6666. }
  6667. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6668. forGPBGenericValueKey:(GPBGenericValue *)key {
  6669. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6670. }
  6671. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6672. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6673. #pragma unused(stop)
  6674. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6675. }];
  6676. }
  6677. - (BOOL)valueForKey:(int64_t)key value:(uint32_t *)value {
  6678. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6679. if (wrapped && value) {
  6680. *value = [wrapped unsignedIntValue];
  6681. }
  6682. return (wrapped != NULL);
  6683. }
  6684. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6685. if (otherDictionary) {
  6686. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6687. if (_autocreator) {
  6688. GPBAutocreatedDictionaryModified(_autocreator, self);
  6689. }
  6690. }
  6691. }
  6692. - (void)setValue:(uint32_t)value forKey:(int64_t)key {
  6693. [_dictionary setObject:@(value) forKey:@(key)];
  6694. if (_autocreator) {
  6695. GPBAutocreatedDictionaryModified(_autocreator, self);
  6696. }
  6697. }
  6698. - (void)removeValueForKey:(int64_t)aKey {
  6699. [_dictionary removeObjectForKey:@(aKey)];
  6700. }
  6701. - (void)removeAll {
  6702. [_dictionary removeAllObjects];
  6703. }
  6704. @end
  6705. #pragma mark - Int64 -> Int32
  6706. @implementation GPBInt64Int32Dictionary {
  6707. @package
  6708. NSMutableDictionary *_dictionary;
  6709. }
  6710. + (instancetype)dictionary {
  6711. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6712. }
  6713. + (instancetype)dictionaryWithValue:(int32_t)value
  6714. forKey:(int64_t)key {
  6715. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6716. // on to get the type correct.
  6717. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:&value
  6718. forKeys:&key
  6719. count:1] autorelease];
  6720. }
  6721. + (instancetype)dictionaryWithValues:(const int32_t [])values
  6722. forKeys:(const int64_t [])keys
  6723. count:(NSUInteger)count {
  6724. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6725. // on to get the type correct.
  6726. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithValues:values
  6727. forKeys:keys
  6728. count:count] autorelease];
  6729. }
  6730. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6731. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6732. // on to get the type correct.
  6733. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6734. }
  6735. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6736. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6737. }
  6738. - (instancetype)init {
  6739. return [self initWithValues:NULL forKeys:NULL count:0];
  6740. }
  6741. - (instancetype)initWithValues:(const int32_t [])values
  6742. forKeys:(const int64_t [])keys
  6743. count:(NSUInteger)count {
  6744. self = [super init];
  6745. if (self) {
  6746. _dictionary = [[NSMutableDictionary alloc] init];
  6747. if (count && values && keys) {
  6748. for (NSUInteger i = 0; i < count; ++i) {
  6749. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6750. }
  6751. }
  6752. }
  6753. return self;
  6754. }
  6755. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6756. self = [self initWithValues:NULL forKeys:NULL count:0];
  6757. if (self) {
  6758. if (dictionary) {
  6759. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6760. }
  6761. }
  6762. return self;
  6763. }
  6764. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6765. #pragma unused(numItems)
  6766. return [self initWithValues:NULL forKeys:NULL count:0];
  6767. }
  6768. - (void)dealloc {
  6769. NSAssert(!_autocreator,
  6770. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6771. [self class], _autocreator);
  6772. [_dictionary release];
  6773. [super dealloc];
  6774. }
  6775. - (instancetype)copyWithZone:(NSZone *)zone {
  6776. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6777. }
  6778. - (BOOL)isEqual:(GPBInt64Int32Dictionary *)other {
  6779. if (self == other) {
  6780. return YES;
  6781. }
  6782. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6783. return NO;
  6784. }
  6785. return [_dictionary isEqual:other->_dictionary];
  6786. }
  6787. - (NSUInteger)hash {
  6788. return _dictionary.count;
  6789. }
  6790. - (NSString *)description {
  6791. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6792. }
  6793. - (NSUInteger)count {
  6794. return _dictionary.count;
  6795. }
  6796. - (void)enumerateKeysAndValuesUsingBlock:
  6797. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6798. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6799. NSNumber *aValue,
  6800. BOOL *stop) {
  6801. block([aKey longLongValue], [aValue intValue], stop);
  6802. }];
  6803. }
  6804. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6805. NSUInteger count = _dictionary.count;
  6806. if (count == 0) {
  6807. return 0;
  6808. }
  6809. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6810. GPBDataType keyDataType = field.mapKeyDataType;
  6811. __block size_t result = 0;
  6812. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6813. NSNumber *aValue,
  6814. BOOL *stop) {
  6815. #pragma unused(stop)
  6816. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6817. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6818. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6819. }];
  6820. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6821. result += tagSize * count;
  6822. return result;
  6823. }
  6824. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6825. asField:(GPBFieldDescriptor *)field {
  6826. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6827. GPBDataType keyDataType = field.mapKeyDataType;
  6828. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6829. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6830. NSNumber *aValue,
  6831. BOOL *stop) {
  6832. #pragma unused(stop)
  6833. // Write the tag.
  6834. [outputStream writeInt32NoTag:tag];
  6835. // Write the size of the message.
  6836. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6837. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6838. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6839. // Write the fields.
  6840. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6841. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6842. }];
  6843. }
  6844. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6845. forGPBGenericValueKey:(GPBGenericValue *)key {
  6846. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6847. }
  6848. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6849. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6850. #pragma unused(stop)
  6851. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6852. }];
  6853. }
  6854. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  6855. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6856. if (wrapped && value) {
  6857. *value = [wrapped intValue];
  6858. }
  6859. return (wrapped != NULL);
  6860. }
  6861. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6862. if (otherDictionary) {
  6863. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6864. if (_autocreator) {
  6865. GPBAutocreatedDictionaryModified(_autocreator, self);
  6866. }
  6867. }
  6868. }
  6869. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  6870. [_dictionary setObject:@(value) forKey:@(key)];
  6871. if (_autocreator) {
  6872. GPBAutocreatedDictionaryModified(_autocreator, self);
  6873. }
  6874. }
  6875. - (void)removeValueForKey:(int64_t)aKey {
  6876. [_dictionary removeObjectForKey:@(aKey)];
  6877. }
  6878. - (void)removeAll {
  6879. [_dictionary removeAllObjects];
  6880. }
  6881. @end
  6882. #pragma mark - Int64 -> UInt64
  6883. @implementation GPBInt64UInt64Dictionary {
  6884. @package
  6885. NSMutableDictionary *_dictionary;
  6886. }
  6887. + (instancetype)dictionary {
  6888. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  6889. }
  6890. + (instancetype)dictionaryWithValue:(uint64_t)value
  6891. forKey:(int64_t)key {
  6892. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6893. // on to get the type correct.
  6894. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:&value
  6895. forKeys:&key
  6896. count:1] autorelease];
  6897. }
  6898. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  6899. forKeys:(const int64_t [])keys
  6900. count:(NSUInteger)count {
  6901. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6902. // on to get the type correct.
  6903. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithValues:values
  6904. forKeys:keys
  6905. count:count] autorelease];
  6906. }
  6907. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6908. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6909. // on to get the type correct.
  6910. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6911. }
  6912. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6913. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6914. }
  6915. - (instancetype)init {
  6916. return [self initWithValues:NULL forKeys:NULL count:0];
  6917. }
  6918. - (instancetype)initWithValues:(const uint64_t [])values
  6919. forKeys:(const int64_t [])keys
  6920. count:(NSUInteger)count {
  6921. self = [super init];
  6922. if (self) {
  6923. _dictionary = [[NSMutableDictionary alloc] init];
  6924. if (count && values && keys) {
  6925. for (NSUInteger i = 0; i < count; ++i) {
  6926. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6927. }
  6928. }
  6929. }
  6930. return self;
  6931. }
  6932. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6933. self = [self initWithValues:NULL forKeys:NULL count:0];
  6934. if (self) {
  6935. if (dictionary) {
  6936. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6937. }
  6938. }
  6939. return self;
  6940. }
  6941. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6942. #pragma unused(numItems)
  6943. return [self initWithValues:NULL forKeys:NULL count:0];
  6944. }
  6945. - (void)dealloc {
  6946. NSAssert(!_autocreator,
  6947. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6948. [self class], _autocreator);
  6949. [_dictionary release];
  6950. [super dealloc];
  6951. }
  6952. - (instancetype)copyWithZone:(NSZone *)zone {
  6953. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6954. }
  6955. - (BOOL)isEqual:(GPBInt64UInt64Dictionary *)other {
  6956. if (self == other) {
  6957. return YES;
  6958. }
  6959. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6960. return NO;
  6961. }
  6962. return [_dictionary isEqual:other->_dictionary];
  6963. }
  6964. - (NSUInteger)hash {
  6965. return _dictionary.count;
  6966. }
  6967. - (NSString *)description {
  6968. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6969. }
  6970. - (NSUInteger)count {
  6971. return _dictionary.count;
  6972. }
  6973. - (void)enumerateKeysAndValuesUsingBlock:
  6974. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  6975. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6976. NSNumber *aValue,
  6977. BOOL *stop) {
  6978. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  6979. }];
  6980. }
  6981. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6982. NSUInteger count = _dictionary.count;
  6983. if (count == 0) {
  6984. return 0;
  6985. }
  6986. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6987. GPBDataType keyDataType = field.mapKeyDataType;
  6988. __block size_t result = 0;
  6989. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6990. NSNumber *aValue,
  6991. BOOL *stop) {
  6992. #pragma unused(stop)
  6993. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6994. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6995. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6996. }];
  6997. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6998. result += tagSize * count;
  6999. return result;
  7000. }
  7001. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7002. asField:(GPBFieldDescriptor *)field {
  7003. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7004. GPBDataType keyDataType = field.mapKeyDataType;
  7005. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7006. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7007. NSNumber *aValue,
  7008. BOOL *stop) {
  7009. #pragma unused(stop)
  7010. // Write the tag.
  7011. [outputStream writeInt32NoTag:tag];
  7012. // Write the size of the message.
  7013. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7014. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7015. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7016. // Write the fields.
  7017. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7018. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7019. }];
  7020. }
  7021. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7022. forGPBGenericValueKey:(GPBGenericValue *)key {
  7023. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  7024. }
  7025. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7026. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  7027. #pragma unused(stop)
  7028. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  7029. }];
  7030. }
  7031. - (BOOL)valueForKey:(int64_t)key value:(uint64_t *)value {
  7032. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7033. if (wrapped && value) {
  7034. *value = [wrapped unsignedLongLongValue];
  7035. }
  7036. return (wrapped != NULL);
  7037. }
  7038. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  7039. if (otherDictionary) {
  7040. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7041. if (_autocreator) {
  7042. GPBAutocreatedDictionaryModified(_autocreator, self);
  7043. }
  7044. }
  7045. }
  7046. - (void)setValue:(uint64_t)value forKey:(int64_t)key {
  7047. [_dictionary setObject:@(value) forKey:@(key)];
  7048. if (_autocreator) {
  7049. GPBAutocreatedDictionaryModified(_autocreator, self);
  7050. }
  7051. }
  7052. - (void)removeValueForKey:(int64_t)aKey {
  7053. [_dictionary removeObjectForKey:@(aKey)];
  7054. }
  7055. - (void)removeAll {
  7056. [_dictionary removeAllObjects];
  7057. }
  7058. @end
  7059. #pragma mark - Int64 -> Int64
  7060. @implementation GPBInt64Int64Dictionary {
  7061. @package
  7062. NSMutableDictionary *_dictionary;
  7063. }
  7064. + (instancetype)dictionary {
  7065. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7066. }
  7067. + (instancetype)dictionaryWithValue:(int64_t)value
  7068. forKey:(int64_t)key {
  7069. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7070. // on to get the type correct.
  7071. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:&value
  7072. forKeys:&key
  7073. count:1] autorelease];
  7074. }
  7075. + (instancetype)dictionaryWithValues:(const int64_t [])values
  7076. forKeys:(const int64_t [])keys
  7077. count:(NSUInteger)count {
  7078. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7079. // on to get the type correct.
  7080. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithValues:values
  7081. forKeys:keys
  7082. count:count] autorelease];
  7083. }
  7084. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7085. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7086. // on to get the type correct.
  7087. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7088. }
  7089. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7090. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7091. }
  7092. - (instancetype)init {
  7093. return [self initWithValues:NULL forKeys:NULL count:0];
  7094. }
  7095. - (instancetype)initWithValues:(const int64_t [])values
  7096. forKeys:(const int64_t [])keys
  7097. count:(NSUInteger)count {
  7098. self = [super init];
  7099. if (self) {
  7100. _dictionary = [[NSMutableDictionary alloc] init];
  7101. if (count && values && keys) {
  7102. for (NSUInteger i = 0; i < count; ++i) {
  7103. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7104. }
  7105. }
  7106. }
  7107. return self;
  7108. }
  7109. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7110. self = [self initWithValues:NULL forKeys:NULL count:0];
  7111. if (self) {
  7112. if (dictionary) {
  7113. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7114. }
  7115. }
  7116. return self;
  7117. }
  7118. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7119. #pragma unused(numItems)
  7120. return [self initWithValues:NULL forKeys:NULL count:0];
  7121. }
  7122. - (void)dealloc {
  7123. NSAssert(!_autocreator,
  7124. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7125. [self class], _autocreator);
  7126. [_dictionary release];
  7127. [super dealloc];
  7128. }
  7129. - (instancetype)copyWithZone:(NSZone *)zone {
  7130. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7131. }
  7132. - (BOOL)isEqual:(GPBInt64Int64Dictionary *)other {
  7133. if (self == other) {
  7134. return YES;
  7135. }
  7136. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7137. return NO;
  7138. }
  7139. return [_dictionary isEqual:other->_dictionary];
  7140. }
  7141. - (NSUInteger)hash {
  7142. return _dictionary.count;
  7143. }
  7144. - (NSString *)description {
  7145. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7146. }
  7147. - (NSUInteger)count {
  7148. return _dictionary.count;
  7149. }
  7150. - (void)enumerateKeysAndValuesUsingBlock:
  7151. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7152. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7153. NSNumber *aValue,
  7154. BOOL *stop) {
  7155. block([aKey longLongValue], [aValue longLongValue], stop);
  7156. }];
  7157. }
  7158. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7159. NSUInteger count = _dictionary.count;
  7160. if (count == 0) {
  7161. return 0;
  7162. }
  7163. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7164. GPBDataType keyDataType = field.mapKeyDataType;
  7165. __block size_t result = 0;
  7166. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7167. NSNumber *aValue,
  7168. BOOL *stop) {
  7169. #pragma unused(stop)
  7170. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7171. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7172. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7173. }];
  7174. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7175. result += tagSize * count;
  7176. return result;
  7177. }
  7178. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7179. asField:(GPBFieldDescriptor *)field {
  7180. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7181. GPBDataType keyDataType = field.mapKeyDataType;
  7182. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7183. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7184. NSNumber *aValue,
  7185. BOOL *stop) {
  7186. #pragma unused(stop)
  7187. // Write the tag.
  7188. [outputStream writeInt32NoTag:tag];
  7189. // Write the size of the message.
  7190. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7191. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7192. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7193. // Write the fields.
  7194. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7195. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7196. }];
  7197. }
  7198. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7199. forGPBGenericValueKey:(GPBGenericValue *)key {
  7200. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7201. }
  7202. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7203. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7204. #pragma unused(stop)
  7205. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7206. }];
  7207. }
  7208. - (BOOL)valueForKey:(int64_t)key value:(int64_t *)value {
  7209. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7210. if (wrapped && value) {
  7211. *value = [wrapped longLongValue];
  7212. }
  7213. return (wrapped != NULL);
  7214. }
  7215. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7216. if (otherDictionary) {
  7217. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7218. if (_autocreator) {
  7219. GPBAutocreatedDictionaryModified(_autocreator, self);
  7220. }
  7221. }
  7222. }
  7223. - (void)setValue:(int64_t)value forKey:(int64_t)key {
  7224. [_dictionary setObject:@(value) forKey:@(key)];
  7225. if (_autocreator) {
  7226. GPBAutocreatedDictionaryModified(_autocreator, self);
  7227. }
  7228. }
  7229. - (void)removeValueForKey:(int64_t)aKey {
  7230. [_dictionary removeObjectForKey:@(aKey)];
  7231. }
  7232. - (void)removeAll {
  7233. [_dictionary removeAllObjects];
  7234. }
  7235. @end
  7236. #pragma mark - Int64 -> Bool
  7237. @implementation GPBInt64BoolDictionary {
  7238. @package
  7239. NSMutableDictionary *_dictionary;
  7240. }
  7241. + (instancetype)dictionary {
  7242. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7243. }
  7244. + (instancetype)dictionaryWithValue:(BOOL)value
  7245. forKey:(int64_t)key {
  7246. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7247. // on to get the type correct.
  7248. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:&value
  7249. forKeys:&key
  7250. count:1] autorelease];
  7251. }
  7252. + (instancetype)dictionaryWithValues:(const BOOL [])values
  7253. forKeys:(const int64_t [])keys
  7254. count:(NSUInteger)count {
  7255. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7256. // on to get the type correct.
  7257. return [[(GPBInt64BoolDictionary*)[self alloc] initWithValues:values
  7258. forKeys:keys
  7259. count:count] autorelease];
  7260. }
  7261. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7262. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7263. // on to get the type correct.
  7264. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7265. }
  7266. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7267. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7268. }
  7269. - (instancetype)init {
  7270. return [self initWithValues:NULL forKeys:NULL count:0];
  7271. }
  7272. - (instancetype)initWithValues:(const BOOL [])values
  7273. forKeys:(const int64_t [])keys
  7274. count:(NSUInteger)count {
  7275. self = [super init];
  7276. if (self) {
  7277. _dictionary = [[NSMutableDictionary alloc] init];
  7278. if (count && values && keys) {
  7279. for (NSUInteger i = 0; i < count; ++i) {
  7280. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7281. }
  7282. }
  7283. }
  7284. return self;
  7285. }
  7286. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7287. self = [self initWithValues:NULL forKeys:NULL count:0];
  7288. if (self) {
  7289. if (dictionary) {
  7290. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7291. }
  7292. }
  7293. return self;
  7294. }
  7295. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7296. #pragma unused(numItems)
  7297. return [self initWithValues:NULL forKeys:NULL count:0];
  7298. }
  7299. - (void)dealloc {
  7300. NSAssert(!_autocreator,
  7301. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7302. [self class], _autocreator);
  7303. [_dictionary release];
  7304. [super dealloc];
  7305. }
  7306. - (instancetype)copyWithZone:(NSZone *)zone {
  7307. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7308. }
  7309. - (BOOL)isEqual:(GPBInt64BoolDictionary *)other {
  7310. if (self == other) {
  7311. return YES;
  7312. }
  7313. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7314. return NO;
  7315. }
  7316. return [_dictionary isEqual:other->_dictionary];
  7317. }
  7318. - (NSUInteger)hash {
  7319. return _dictionary.count;
  7320. }
  7321. - (NSString *)description {
  7322. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7323. }
  7324. - (NSUInteger)count {
  7325. return _dictionary.count;
  7326. }
  7327. - (void)enumerateKeysAndValuesUsingBlock:
  7328. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7329. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7330. NSNumber *aValue,
  7331. BOOL *stop) {
  7332. block([aKey longLongValue], [aValue boolValue], stop);
  7333. }];
  7334. }
  7335. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7336. NSUInteger count = _dictionary.count;
  7337. if (count == 0) {
  7338. return 0;
  7339. }
  7340. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7341. GPBDataType keyDataType = field.mapKeyDataType;
  7342. __block size_t result = 0;
  7343. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7344. NSNumber *aValue,
  7345. BOOL *stop) {
  7346. #pragma unused(stop)
  7347. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7348. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7349. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7350. }];
  7351. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7352. result += tagSize * count;
  7353. return result;
  7354. }
  7355. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7356. asField:(GPBFieldDescriptor *)field {
  7357. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7358. GPBDataType keyDataType = field.mapKeyDataType;
  7359. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7360. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7361. NSNumber *aValue,
  7362. BOOL *stop) {
  7363. #pragma unused(stop)
  7364. // Write the tag.
  7365. [outputStream writeInt32NoTag:tag];
  7366. // Write the size of the message.
  7367. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7368. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7369. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7370. // Write the fields.
  7371. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7372. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7373. }];
  7374. }
  7375. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7376. forGPBGenericValueKey:(GPBGenericValue *)key {
  7377. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7378. }
  7379. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7380. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7381. #pragma unused(stop)
  7382. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7383. }];
  7384. }
  7385. - (BOOL)valueForKey:(int64_t)key value:(BOOL *)value {
  7386. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7387. if (wrapped && value) {
  7388. *value = [wrapped boolValue];
  7389. }
  7390. return (wrapped != NULL);
  7391. }
  7392. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7393. if (otherDictionary) {
  7394. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7395. if (_autocreator) {
  7396. GPBAutocreatedDictionaryModified(_autocreator, self);
  7397. }
  7398. }
  7399. }
  7400. - (void)setValue:(BOOL)value forKey:(int64_t)key {
  7401. [_dictionary setObject:@(value) forKey:@(key)];
  7402. if (_autocreator) {
  7403. GPBAutocreatedDictionaryModified(_autocreator, self);
  7404. }
  7405. }
  7406. - (void)removeValueForKey:(int64_t)aKey {
  7407. [_dictionary removeObjectForKey:@(aKey)];
  7408. }
  7409. - (void)removeAll {
  7410. [_dictionary removeAllObjects];
  7411. }
  7412. @end
  7413. #pragma mark - Int64 -> Float
  7414. @implementation GPBInt64FloatDictionary {
  7415. @package
  7416. NSMutableDictionary *_dictionary;
  7417. }
  7418. + (instancetype)dictionary {
  7419. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7420. }
  7421. + (instancetype)dictionaryWithValue:(float)value
  7422. forKey:(int64_t)key {
  7423. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7424. // on to get the type correct.
  7425. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:&value
  7426. forKeys:&key
  7427. count:1] autorelease];
  7428. }
  7429. + (instancetype)dictionaryWithValues:(const float [])values
  7430. forKeys:(const int64_t [])keys
  7431. count:(NSUInteger)count {
  7432. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7433. // on to get the type correct.
  7434. return [[(GPBInt64FloatDictionary*)[self alloc] initWithValues:values
  7435. forKeys:keys
  7436. count:count] autorelease];
  7437. }
  7438. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7439. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7440. // on to get the type correct.
  7441. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7442. }
  7443. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7444. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7445. }
  7446. - (instancetype)init {
  7447. return [self initWithValues:NULL forKeys:NULL count:0];
  7448. }
  7449. - (instancetype)initWithValues:(const float [])values
  7450. forKeys:(const int64_t [])keys
  7451. count:(NSUInteger)count {
  7452. self = [super init];
  7453. if (self) {
  7454. _dictionary = [[NSMutableDictionary alloc] init];
  7455. if (count && values && keys) {
  7456. for (NSUInteger i = 0; i < count; ++i) {
  7457. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7458. }
  7459. }
  7460. }
  7461. return self;
  7462. }
  7463. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7464. self = [self initWithValues:NULL forKeys:NULL count:0];
  7465. if (self) {
  7466. if (dictionary) {
  7467. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7468. }
  7469. }
  7470. return self;
  7471. }
  7472. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7473. #pragma unused(numItems)
  7474. return [self initWithValues:NULL forKeys:NULL count:0];
  7475. }
  7476. - (void)dealloc {
  7477. NSAssert(!_autocreator,
  7478. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7479. [self class], _autocreator);
  7480. [_dictionary release];
  7481. [super dealloc];
  7482. }
  7483. - (instancetype)copyWithZone:(NSZone *)zone {
  7484. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7485. }
  7486. - (BOOL)isEqual:(GPBInt64FloatDictionary *)other {
  7487. if (self == other) {
  7488. return YES;
  7489. }
  7490. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7491. return NO;
  7492. }
  7493. return [_dictionary isEqual:other->_dictionary];
  7494. }
  7495. - (NSUInteger)hash {
  7496. return _dictionary.count;
  7497. }
  7498. - (NSString *)description {
  7499. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7500. }
  7501. - (NSUInteger)count {
  7502. return _dictionary.count;
  7503. }
  7504. - (void)enumerateKeysAndValuesUsingBlock:
  7505. (void (^)(int64_t key, float value, BOOL *stop))block {
  7506. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7507. NSNumber *aValue,
  7508. BOOL *stop) {
  7509. block([aKey longLongValue], [aValue floatValue], stop);
  7510. }];
  7511. }
  7512. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7513. NSUInteger count = _dictionary.count;
  7514. if (count == 0) {
  7515. return 0;
  7516. }
  7517. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7518. GPBDataType keyDataType = field.mapKeyDataType;
  7519. __block size_t result = 0;
  7520. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7521. NSNumber *aValue,
  7522. BOOL *stop) {
  7523. #pragma unused(stop)
  7524. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7525. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7526. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7527. }];
  7528. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7529. result += tagSize * count;
  7530. return result;
  7531. }
  7532. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7533. asField:(GPBFieldDescriptor *)field {
  7534. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7535. GPBDataType keyDataType = field.mapKeyDataType;
  7536. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7537. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7538. NSNumber *aValue,
  7539. BOOL *stop) {
  7540. #pragma unused(stop)
  7541. // Write the tag.
  7542. [outputStream writeInt32NoTag:tag];
  7543. // Write the size of the message.
  7544. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7545. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7546. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7547. // Write the fields.
  7548. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7549. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7550. }];
  7551. }
  7552. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7553. forGPBGenericValueKey:(GPBGenericValue *)key {
  7554. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7555. }
  7556. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7557. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7558. #pragma unused(stop)
  7559. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7560. }];
  7561. }
  7562. - (BOOL)valueForKey:(int64_t)key value:(float *)value {
  7563. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7564. if (wrapped && value) {
  7565. *value = [wrapped floatValue];
  7566. }
  7567. return (wrapped != NULL);
  7568. }
  7569. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7570. if (otherDictionary) {
  7571. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7572. if (_autocreator) {
  7573. GPBAutocreatedDictionaryModified(_autocreator, self);
  7574. }
  7575. }
  7576. }
  7577. - (void)setValue:(float)value forKey:(int64_t)key {
  7578. [_dictionary setObject:@(value) forKey:@(key)];
  7579. if (_autocreator) {
  7580. GPBAutocreatedDictionaryModified(_autocreator, self);
  7581. }
  7582. }
  7583. - (void)removeValueForKey:(int64_t)aKey {
  7584. [_dictionary removeObjectForKey:@(aKey)];
  7585. }
  7586. - (void)removeAll {
  7587. [_dictionary removeAllObjects];
  7588. }
  7589. @end
  7590. #pragma mark - Int64 -> Double
  7591. @implementation GPBInt64DoubleDictionary {
  7592. @package
  7593. NSMutableDictionary *_dictionary;
  7594. }
  7595. + (instancetype)dictionary {
  7596. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  7597. }
  7598. + (instancetype)dictionaryWithValue:(double)value
  7599. forKey:(int64_t)key {
  7600. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7601. // on to get the type correct.
  7602. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:&value
  7603. forKeys:&key
  7604. count:1] autorelease];
  7605. }
  7606. + (instancetype)dictionaryWithValues:(const double [])values
  7607. forKeys:(const int64_t [])keys
  7608. count:(NSUInteger)count {
  7609. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7610. // on to get the type correct.
  7611. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithValues:values
  7612. forKeys:keys
  7613. count:count] autorelease];
  7614. }
  7615. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7616. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7617. // on to get the type correct.
  7618. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7619. }
  7620. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7621. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7622. }
  7623. - (instancetype)init {
  7624. return [self initWithValues:NULL forKeys:NULL count:0];
  7625. }
  7626. - (instancetype)initWithValues:(const double [])values
  7627. forKeys:(const int64_t [])keys
  7628. count:(NSUInteger)count {
  7629. self = [super init];
  7630. if (self) {
  7631. _dictionary = [[NSMutableDictionary alloc] init];
  7632. if (count && values && keys) {
  7633. for (NSUInteger i = 0; i < count; ++i) {
  7634. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7635. }
  7636. }
  7637. }
  7638. return self;
  7639. }
  7640. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7641. self = [self initWithValues:NULL forKeys:NULL count:0];
  7642. if (self) {
  7643. if (dictionary) {
  7644. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7645. }
  7646. }
  7647. return self;
  7648. }
  7649. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7650. #pragma unused(numItems)
  7651. return [self initWithValues:NULL forKeys:NULL count:0];
  7652. }
  7653. - (void)dealloc {
  7654. NSAssert(!_autocreator,
  7655. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7656. [self class], _autocreator);
  7657. [_dictionary release];
  7658. [super dealloc];
  7659. }
  7660. - (instancetype)copyWithZone:(NSZone *)zone {
  7661. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7662. }
  7663. - (BOOL)isEqual:(GPBInt64DoubleDictionary *)other {
  7664. if (self == other) {
  7665. return YES;
  7666. }
  7667. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7668. return NO;
  7669. }
  7670. return [_dictionary isEqual:other->_dictionary];
  7671. }
  7672. - (NSUInteger)hash {
  7673. return _dictionary.count;
  7674. }
  7675. - (NSString *)description {
  7676. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7677. }
  7678. - (NSUInteger)count {
  7679. return _dictionary.count;
  7680. }
  7681. - (void)enumerateKeysAndValuesUsingBlock:
  7682. (void (^)(int64_t key, double value, BOOL *stop))block {
  7683. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7684. NSNumber *aValue,
  7685. BOOL *stop) {
  7686. block([aKey longLongValue], [aValue doubleValue], stop);
  7687. }];
  7688. }
  7689. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7690. NSUInteger count = _dictionary.count;
  7691. if (count == 0) {
  7692. return 0;
  7693. }
  7694. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7695. GPBDataType keyDataType = field.mapKeyDataType;
  7696. __block size_t result = 0;
  7697. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7698. NSNumber *aValue,
  7699. BOOL *stop) {
  7700. #pragma unused(stop)
  7701. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7702. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7703. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7704. }];
  7705. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7706. result += tagSize * count;
  7707. return result;
  7708. }
  7709. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7710. asField:(GPBFieldDescriptor *)field {
  7711. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7712. GPBDataType keyDataType = field.mapKeyDataType;
  7713. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7714. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7715. NSNumber *aValue,
  7716. BOOL *stop) {
  7717. #pragma unused(stop)
  7718. // Write the tag.
  7719. [outputStream writeInt32NoTag:tag];
  7720. // Write the size of the message.
  7721. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7722. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7723. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7724. // Write the fields.
  7725. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7726. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7727. }];
  7728. }
  7729. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7730. forGPBGenericValueKey:(GPBGenericValue *)key {
  7731. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7732. }
  7733. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7734. [self enumerateKeysAndValuesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7735. #pragma unused(stop)
  7736. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7737. }];
  7738. }
  7739. - (BOOL)valueForKey:(int64_t)key value:(double *)value {
  7740. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7741. if (wrapped && value) {
  7742. *value = [wrapped doubleValue];
  7743. }
  7744. return (wrapped != NULL);
  7745. }
  7746. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7747. if (otherDictionary) {
  7748. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7749. if (_autocreator) {
  7750. GPBAutocreatedDictionaryModified(_autocreator, self);
  7751. }
  7752. }
  7753. }
  7754. - (void)setValue:(double)value forKey:(int64_t)key {
  7755. [_dictionary setObject:@(value) forKey:@(key)];
  7756. if (_autocreator) {
  7757. GPBAutocreatedDictionaryModified(_autocreator, self);
  7758. }
  7759. }
  7760. - (void)removeValueForKey:(int64_t)aKey {
  7761. [_dictionary removeObjectForKey:@(aKey)];
  7762. }
  7763. - (void)removeAll {
  7764. [_dictionary removeAllObjects];
  7765. }
  7766. @end
  7767. #pragma mark - Int64 -> Enum
  7768. @implementation GPBInt64EnumDictionary {
  7769. @package
  7770. NSMutableDictionary *_dictionary;
  7771. GPBEnumValidationFunc _validationFunc;
  7772. }
  7773. @synthesize validationFunc = _validationFunc;
  7774. + (instancetype)dictionary {
  7775. return [[[self alloc] initWithValidationFunction:NULL
  7776. rawValues:NULL
  7777. forKeys:NULL
  7778. count:0] autorelease];
  7779. }
  7780. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7781. return [[[self alloc] initWithValidationFunction:func
  7782. rawValues:NULL
  7783. forKeys:NULL
  7784. count:0] autorelease];
  7785. }
  7786. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7787. rawValue:(int32_t)rawValue
  7788. forKey:(int64_t)key {
  7789. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7790. // on to get the type correct.
  7791. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7792. rawValues:&rawValue
  7793. forKeys:&key
  7794. count:1] autorelease];
  7795. }
  7796. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7797. rawValues:(const int32_t [])rawValues
  7798. forKeys:(const int64_t [])keys
  7799. count:(NSUInteger)count {
  7800. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7801. // on to get the type correct.
  7802. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7803. rawValues:rawValues
  7804. forKeys:keys
  7805. count:count] autorelease];
  7806. }
  7807. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7808. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7809. // on to get the type correct.
  7810. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7811. }
  7812. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7813. capacity:(NSUInteger)numItems {
  7814. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7815. }
  7816. - (instancetype)init {
  7817. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7818. }
  7819. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7820. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7821. }
  7822. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7823. rawValues:(const int32_t [])rawValues
  7824. forKeys:(const int64_t [])keys
  7825. count:(NSUInteger)count {
  7826. self = [super init];
  7827. if (self) {
  7828. _dictionary = [[NSMutableDictionary alloc] init];
  7829. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7830. if (count && rawValues && keys) {
  7831. for (NSUInteger i = 0; i < count; ++i) {
  7832. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7833. }
  7834. }
  7835. }
  7836. return self;
  7837. }
  7838. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7839. self = [self initWithValidationFunction:dictionary.validationFunc
  7840. rawValues:NULL
  7841. forKeys:NULL
  7842. count:0];
  7843. if (self) {
  7844. if (dictionary) {
  7845. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7846. }
  7847. }
  7848. return self;
  7849. }
  7850. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7851. capacity:(NSUInteger)numItems {
  7852. #pragma unused(numItems)
  7853. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7854. }
  7855. - (void)dealloc {
  7856. NSAssert(!_autocreator,
  7857. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7858. [self class], _autocreator);
  7859. [_dictionary release];
  7860. [super dealloc];
  7861. }
  7862. - (instancetype)copyWithZone:(NSZone *)zone {
  7863. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7864. }
  7865. - (BOOL)isEqual:(GPBInt64EnumDictionary *)other {
  7866. if (self == other) {
  7867. return YES;
  7868. }
  7869. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7870. return NO;
  7871. }
  7872. return [_dictionary isEqual:other->_dictionary];
  7873. }
  7874. - (NSUInteger)hash {
  7875. return _dictionary.count;
  7876. }
  7877. - (NSString *)description {
  7878. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7879. }
  7880. - (NSUInteger)count {
  7881. return _dictionary.count;
  7882. }
  7883. - (void)enumerateKeysAndRawValuesUsingBlock:
  7884. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7885. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7886. NSNumber *aValue,
  7887. BOOL *stop) {
  7888. block([aKey longLongValue], [aValue intValue], stop);
  7889. }];
  7890. }
  7891. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7892. NSUInteger count = _dictionary.count;
  7893. if (count == 0) {
  7894. return 0;
  7895. }
  7896. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7897. GPBDataType keyDataType = field.mapKeyDataType;
  7898. __block size_t result = 0;
  7899. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7900. NSNumber *aValue,
  7901. BOOL *stop) {
  7902. #pragma unused(stop)
  7903. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7904. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7905. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7906. }];
  7907. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7908. result += tagSize * count;
  7909. return result;
  7910. }
  7911. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7912. asField:(GPBFieldDescriptor *)field {
  7913. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7914. GPBDataType keyDataType = field.mapKeyDataType;
  7915. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7916. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7917. NSNumber *aValue,
  7918. BOOL *stop) {
  7919. #pragma unused(stop)
  7920. // Write the tag.
  7921. [outputStream writeInt32NoTag:tag];
  7922. // Write the size of the message.
  7923. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7924. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7925. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7926. // Write the fields.
  7927. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7928. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7929. }];
  7930. }
  7931. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7932. forKey:(GPBGenericValue *)key
  7933. keyDataType:(GPBDataType)keyDataType {
  7934. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7935. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7936. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7937. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7938. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7939. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7940. [outputStream release];
  7941. return data;
  7942. }
  7943. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7944. forGPBGenericValueKey:(GPBGenericValue *)key {
  7945. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7946. }
  7947. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7948. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7949. #pragma unused(stop)
  7950. block([NSString stringWithFormat:@"%lld", key], @(value));
  7951. }];
  7952. }
  7953. - (BOOL)valueForKey:(int64_t)key value:(int32_t *)value {
  7954. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7955. if (wrapped && value) {
  7956. int32_t result = [wrapped intValue];
  7957. if (!_validationFunc(result)) {
  7958. result = kGPBUnrecognizedEnumeratorValue;
  7959. }
  7960. *value = result;
  7961. }
  7962. return (wrapped != NULL);
  7963. }
  7964. - (BOOL)valueForKey:(int64_t)key rawValue:(int32_t *)rawValue {
  7965. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7966. if (wrapped && rawValue) {
  7967. *rawValue = [wrapped intValue];
  7968. }
  7969. return (wrapped != NULL);
  7970. }
  7971. - (void)enumerateKeysAndValuesUsingBlock:
  7972. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7973. GPBEnumValidationFunc func = _validationFunc;
  7974. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7975. NSNumber *aValue,
  7976. BOOL *stop) {
  7977. int32_t unwrapped = [aValue intValue];
  7978. if (!func(unwrapped)) {
  7979. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7980. }
  7981. block([aKey longLongValue], unwrapped, stop);
  7982. }];
  7983. }
  7984. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7985. if (otherDictionary) {
  7986. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7987. if (_autocreator) {
  7988. GPBAutocreatedDictionaryModified(_autocreator, self);
  7989. }
  7990. }
  7991. }
  7992. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7993. [_dictionary setObject:@(value) forKey:@(key)];
  7994. if (_autocreator) {
  7995. GPBAutocreatedDictionaryModified(_autocreator, self);
  7996. }
  7997. }
  7998. - (void)removeValueForKey:(int64_t)aKey {
  7999. [_dictionary removeObjectForKey:@(aKey)];
  8000. }
  8001. - (void)removeAll {
  8002. [_dictionary removeAllObjects];
  8003. }
  8004. - (void)setValue:(int32_t)value forKey:(int64_t)key {
  8005. if (!_validationFunc(value)) {
  8006. [NSException raise:NSInvalidArgumentException
  8007. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  8008. value];
  8009. }
  8010. [_dictionary setObject:@(value) forKey:@(key)];
  8011. if (_autocreator) {
  8012. GPBAutocreatedDictionaryModified(_autocreator, self);
  8013. }
  8014. }
  8015. @end
  8016. #pragma mark - Int64 -> Object
  8017. @implementation GPBInt64ObjectDictionary {
  8018. @package
  8019. NSMutableDictionary *_dictionary;
  8020. }
  8021. + (instancetype)dictionary {
  8022. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  8023. }
  8024. + (instancetype)dictionaryWithObject:(id)object
  8025. forKey:(int64_t)key {
  8026. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8027. // on to get the type correct.
  8028. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  8029. forKeys:&key
  8030. count:1] autorelease];
  8031. }
  8032. + (instancetype)dictionaryWithObjects:(const id [])objects
  8033. forKeys:(const int64_t [])keys
  8034. count:(NSUInteger)count {
  8035. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8036. // on to get the type correct.
  8037. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  8038. forKeys:keys
  8039. count:count] autorelease];
  8040. }
  8041. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8042. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8043. // on to get the type correct.
  8044. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8045. }
  8046. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8047. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8048. }
  8049. - (instancetype)init {
  8050. return [self initWithObjects:NULL forKeys:NULL count:0];
  8051. }
  8052. - (instancetype)initWithObjects:(const id [])objects
  8053. forKeys:(const int64_t [])keys
  8054. count:(NSUInteger)count {
  8055. self = [super init];
  8056. if (self) {
  8057. _dictionary = [[NSMutableDictionary alloc] init];
  8058. if (count && objects && keys) {
  8059. for (NSUInteger i = 0; i < count; ++i) {
  8060. if (!objects[i]) {
  8061. [NSException raise:NSInvalidArgumentException
  8062. format:@"Attempting to add nil object to a Dictionary"];
  8063. }
  8064. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  8065. }
  8066. }
  8067. }
  8068. return self;
  8069. }
  8070. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8071. self = [self initWithObjects:NULL forKeys:NULL count:0];
  8072. if (self) {
  8073. if (dictionary) {
  8074. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8075. }
  8076. }
  8077. return self;
  8078. }
  8079. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8080. #pragma unused(numItems)
  8081. return [self initWithObjects:NULL forKeys:NULL count:0];
  8082. }
  8083. - (void)dealloc {
  8084. NSAssert(!_autocreator,
  8085. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8086. [self class], _autocreator);
  8087. [_dictionary release];
  8088. [super dealloc];
  8089. }
  8090. - (instancetype)copyWithZone:(NSZone *)zone {
  8091. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8092. }
  8093. - (BOOL)isEqual:(GPBInt64ObjectDictionary *)other {
  8094. if (self == other) {
  8095. return YES;
  8096. }
  8097. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8098. return NO;
  8099. }
  8100. return [_dictionary isEqual:other->_dictionary];
  8101. }
  8102. - (NSUInteger)hash {
  8103. return _dictionary.count;
  8104. }
  8105. - (NSString *)description {
  8106. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8107. }
  8108. - (NSUInteger)count {
  8109. return _dictionary.count;
  8110. }
  8111. - (void)enumerateKeysAndObjectsUsingBlock:
  8112. (void (^)(int64_t key, id object, BOOL *stop))block {
  8113. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8114. id aObject,
  8115. BOOL *stop) {
  8116. block([aKey longLongValue], aObject, stop);
  8117. }];
  8118. }
  8119. - (BOOL)isInitialized {
  8120. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8121. if (!msg.initialized) {
  8122. return NO;
  8123. }
  8124. }
  8125. return YES;
  8126. }
  8127. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8128. GPBInt64ObjectDictionary *newDict =
  8129. [[GPBInt64ObjectDictionary alloc] init];
  8130. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8131. GPBMessage *msg,
  8132. BOOL *stop) {
  8133. #pragma unused(stop)
  8134. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8135. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8136. [copiedMsg release];
  8137. }];
  8138. return newDict;
  8139. }
  8140. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8141. NSUInteger count = _dictionary.count;
  8142. if (count == 0) {
  8143. return 0;
  8144. }
  8145. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8146. GPBDataType keyDataType = field.mapKeyDataType;
  8147. __block size_t result = 0;
  8148. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8149. id aObject,
  8150. BOOL *stop) {
  8151. #pragma unused(stop)
  8152. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8153. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8154. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8155. }];
  8156. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8157. result += tagSize * count;
  8158. return result;
  8159. }
  8160. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8161. asField:(GPBFieldDescriptor *)field {
  8162. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8163. GPBDataType keyDataType = field.mapKeyDataType;
  8164. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8165. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8166. id aObject,
  8167. BOOL *stop) {
  8168. #pragma unused(stop)
  8169. // Write the tag.
  8170. [outputStream writeInt32NoTag:tag];
  8171. // Write the size of the message.
  8172. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8173. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8174. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8175. // Write the fields.
  8176. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8177. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  8178. }];
  8179. }
  8180. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8181. forGPBGenericValueKey:(GPBGenericValue *)key {
  8182. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8183. }
  8184. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8185. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8186. #pragma unused(stop)
  8187. block([NSString stringWithFormat:@"%lld", key], object);
  8188. }];
  8189. }
  8190. - (id)objectForKey:(int64_t)key {
  8191. id result = [_dictionary objectForKey:@(key)];
  8192. return result;
  8193. }
  8194. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8195. if (otherDictionary) {
  8196. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8197. if (_autocreator) {
  8198. GPBAutocreatedDictionaryModified(_autocreator, self);
  8199. }
  8200. }
  8201. }
  8202. - (void)setObject:(id)object forKey:(int64_t)key {
  8203. if (!object) {
  8204. [NSException raise:NSInvalidArgumentException
  8205. format:@"Attempting to add nil object to a Dictionary"];
  8206. }
  8207. [_dictionary setObject:object forKey:@(key)];
  8208. if (_autocreator) {
  8209. GPBAutocreatedDictionaryModified(_autocreator, self);
  8210. }
  8211. }
  8212. - (void)removeObjectForKey:(int64_t)aKey {
  8213. [_dictionary removeObjectForKey:@(aKey)];
  8214. }
  8215. - (void)removeAll {
  8216. [_dictionary removeAllObjects];
  8217. }
  8218. @end
  8219. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8220. // This block of code is generated, do not edit it directly.
  8221. #pragma mark - String -> UInt32
  8222. @implementation GPBStringUInt32Dictionary {
  8223. @package
  8224. NSMutableDictionary *_dictionary;
  8225. }
  8226. + (instancetype)dictionary {
  8227. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8228. }
  8229. + (instancetype)dictionaryWithValue:(uint32_t)value
  8230. forKey:(NSString *)key {
  8231. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8232. // on to get the type correct.
  8233. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:&value
  8234. forKeys:&key
  8235. count:1] autorelease];
  8236. }
  8237. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  8238. forKeys:(const NSString * [])keys
  8239. count:(NSUInteger)count {
  8240. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8241. // on to get the type correct.
  8242. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithValues:values
  8243. forKeys:keys
  8244. count:count] autorelease];
  8245. }
  8246. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8247. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8248. // on to get the type correct.
  8249. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8250. }
  8251. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8252. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8253. }
  8254. - (instancetype)init {
  8255. return [self initWithValues:NULL forKeys:NULL count:0];
  8256. }
  8257. - (instancetype)initWithValues:(const uint32_t [])values
  8258. forKeys:(const NSString * [])keys
  8259. count:(NSUInteger)count {
  8260. self = [super init];
  8261. if (self) {
  8262. _dictionary = [[NSMutableDictionary alloc] init];
  8263. if (count && values && keys) {
  8264. for (NSUInteger i = 0; i < count; ++i) {
  8265. if (!keys[i]) {
  8266. [NSException raise:NSInvalidArgumentException
  8267. format:@"Attempting to add nil key to a Dictionary"];
  8268. }
  8269. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8270. }
  8271. }
  8272. }
  8273. return self;
  8274. }
  8275. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8276. self = [self initWithValues:NULL forKeys:NULL count:0];
  8277. if (self) {
  8278. if (dictionary) {
  8279. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8280. }
  8281. }
  8282. return self;
  8283. }
  8284. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8285. #pragma unused(numItems)
  8286. return [self initWithValues:NULL forKeys:NULL count:0];
  8287. }
  8288. - (void)dealloc {
  8289. NSAssert(!_autocreator,
  8290. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8291. [self class], _autocreator);
  8292. [_dictionary release];
  8293. [super dealloc];
  8294. }
  8295. - (instancetype)copyWithZone:(NSZone *)zone {
  8296. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8297. }
  8298. - (BOOL)isEqual:(GPBStringUInt32Dictionary *)other {
  8299. if (self == other) {
  8300. return YES;
  8301. }
  8302. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8303. return NO;
  8304. }
  8305. return [_dictionary isEqual:other->_dictionary];
  8306. }
  8307. - (NSUInteger)hash {
  8308. return _dictionary.count;
  8309. }
  8310. - (NSString *)description {
  8311. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8312. }
  8313. - (NSUInteger)count {
  8314. return _dictionary.count;
  8315. }
  8316. - (void)enumerateKeysAndValuesUsingBlock:
  8317. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8318. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8319. NSNumber *aValue,
  8320. BOOL *stop) {
  8321. block(aKey, [aValue unsignedIntValue], stop);
  8322. }];
  8323. }
  8324. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8325. NSUInteger count = _dictionary.count;
  8326. if (count == 0) {
  8327. return 0;
  8328. }
  8329. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8330. GPBDataType keyDataType = field.mapKeyDataType;
  8331. __block size_t result = 0;
  8332. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8333. NSNumber *aValue,
  8334. BOOL *stop) {
  8335. #pragma unused(stop)
  8336. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8337. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8338. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8339. }];
  8340. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8341. result += tagSize * count;
  8342. return result;
  8343. }
  8344. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8345. asField:(GPBFieldDescriptor *)field {
  8346. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8347. GPBDataType keyDataType = field.mapKeyDataType;
  8348. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8349. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8350. NSNumber *aValue,
  8351. BOOL *stop) {
  8352. #pragma unused(stop)
  8353. // Write the tag.
  8354. [outputStream writeInt32NoTag:tag];
  8355. // Write the size of the message.
  8356. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8357. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8358. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8359. // Write the fields.
  8360. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8361. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8362. }];
  8363. }
  8364. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8365. forGPBGenericValueKey:(GPBGenericValue *)key {
  8366. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8367. }
  8368. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8369. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8370. #pragma unused(stop)
  8371. block(key, [NSString stringWithFormat:@"%u", value]);
  8372. }];
  8373. }
  8374. - (BOOL)valueForKey:(NSString *)key value:(uint32_t *)value {
  8375. NSNumber *wrapped = [_dictionary objectForKey:key];
  8376. if (wrapped && value) {
  8377. *value = [wrapped unsignedIntValue];
  8378. }
  8379. return (wrapped != NULL);
  8380. }
  8381. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8382. if (otherDictionary) {
  8383. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8384. if (_autocreator) {
  8385. GPBAutocreatedDictionaryModified(_autocreator, self);
  8386. }
  8387. }
  8388. }
  8389. - (void)setValue:(uint32_t)value forKey:(NSString *)key {
  8390. if (!key) {
  8391. [NSException raise:NSInvalidArgumentException
  8392. format:@"Attempting to add nil key to a Dictionary"];
  8393. }
  8394. [_dictionary setObject:@(value) forKey:key];
  8395. if (_autocreator) {
  8396. GPBAutocreatedDictionaryModified(_autocreator, self);
  8397. }
  8398. }
  8399. - (void)removeValueForKey:(NSString *)aKey {
  8400. [_dictionary removeObjectForKey:aKey];
  8401. }
  8402. - (void)removeAll {
  8403. [_dictionary removeAllObjects];
  8404. }
  8405. @end
  8406. #pragma mark - String -> Int32
  8407. @implementation GPBStringInt32Dictionary {
  8408. @package
  8409. NSMutableDictionary *_dictionary;
  8410. }
  8411. + (instancetype)dictionary {
  8412. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8413. }
  8414. + (instancetype)dictionaryWithValue:(int32_t)value
  8415. forKey:(NSString *)key {
  8416. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8417. // on to get the type correct.
  8418. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:&value
  8419. forKeys:&key
  8420. count:1] autorelease];
  8421. }
  8422. + (instancetype)dictionaryWithValues:(const int32_t [])values
  8423. forKeys:(const NSString * [])keys
  8424. count:(NSUInteger)count {
  8425. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8426. // on to get the type correct.
  8427. return [[(GPBStringInt32Dictionary*)[self alloc] initWithValues:values
  8428. forKeys:keys
  8429. count:count] autorelease];
  8430. }
  8431. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8432. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8433. // on to get the type correct.
  8434. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8435. }
  8436. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8437. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8438. }
  8439. - (instancetype)init {
  8440. return [self initWithValues:NULL forKeys:NULL count:0];
  8441. }
  8442. - (instancetype)initWithValues:(const int32_t [])values
  8443. forKeys:(const NSString * [])keys
  8444. count:(NSUInteger)count {
  8445. self = [super init];
  8446. if (self) {
  8447. _dictionary = [[NSMutableDictionary alloc] init];
  8448. if (count && values && keys) {
  8449. for (NSUInteger i = 0; i < count; ++i) {
  8450. if (!keys[i]) {
  8451. [NSException raise:NSInvalidArgumentException
  8452. format:@"Attempting to add nil key to a Dictionary"];
  8453. }
  8454. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8455. }
  8456. }
  8457. }
  8458. return self;
  8459. }
  8460. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8461. self = [self initWithValues:NULL forKeys:NULL count:0];
  8462. if (self) {
  8463. if (dictionary) {
  8464. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8465. }
  8466. }
  8467. return self;
  8468. }
  8469. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8470. #pragma unused(numItems)
  8471. return [self initWithValues:NULL forKeys:NULL count:0];
  8472. }
  8473. - (void)dealloc {
  8474. NSAssert(!_autocreator,
  8475. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8476. [self class], _autocreator);
  8477. [_dictionary release];
  8478. [super dealloc];
  8479. }
  8480. - (instancetype)copyWithZone:(NSZone *)zone {
  8481. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8482. }
  8483. - (BOOL)isEqual:(GPBStringInt32Dictionary *)other {
  8484. if (self == other) {
  8485. return YES;
  8486. }
  8487. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8488. return NO;
  8489. }
  8490. return [_dictionary isEqual:other->_dictionary];
  8491. }
  8492. - (NSUInteger)hash {
  8493. return _dictionary.count;
  8494. }
  8495. - (NSString *)description {
  8496. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8497. }
  8498. - (NSUInteger)count {
  8499. return _dictionary.count;
  8500. }
  8501. - (void)enumerateKeysAndValuesUsingBlock:
  8502. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8503. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8504. NSNumber *aValue,
  8505. BOOL *stop) {
  8506. block(aKey, [aValue intValue], stop);
  8507. }];
  8508. }
  8509. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8510. NSUInteger count = _dictionary.count;
  8511. if (count == 0) {
  8512. return 0;
  8513. }
  8514. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8515. GPBDataType keyDataType = field.mapKeyDataType;
  8516. __block size_t result = 0;
  8517. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8518. NSNumber *aValue,
  8519. BOOL *stop) {
  8520. #pragma unused(stop)
  8521. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8522. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8523. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8524. }];
  8525. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8526. result += tagSize * count;
  8527. return result;
  8528. }
  8529. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8530. asField:(GPBFieldDescriptor *)field {
  8531. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8532. GPBDataType keyDataType = field.mapKeyDataType;
  8533. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8534. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8535. NSNumber *aValue,
  8536. BOOL *stop) {
  8537. #pragma unused(stop)
  8538. // Write the tag.
  8539. [outputStream writeInt32NoTag:tag];
  8540. // Write the size of the message.
  8541. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8542. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8543. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8544. // Write the fields.
  8545. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8546. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8547. }];
  8548. }
  8549. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8550. forGPBGenericValueKey:(GPBGenericValue *)key {
  8551. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8552. }
  8553. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8554. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8555. #pragma unused(stop)
  8556. block(key, [NSString stringWithFormat:@"%d", value]);
  8557. }];
  8558. }
  8559. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  8560. NSNumber *wrapped = [_dictionary objectForKey:key];
  8561. if (wrapped && value) {
  8562. *value = [wrapped intValue];
  8563. }
  8564. return (wrapped != NULL);
  8565. }
  8566. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8567. if (otherDictionary) {
  8568. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8569. if (_autocreator) {
  8570. GPBAutocreatedDictionaryModified(_autocreator, self);
  8571. }
  8572. }
  8573. }
  8574. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  8575. if (!key) {
  8576. [NSException raise:NSInvalidArgumentException
  8577. format:@"Attempting to add nil key to a Dictionary"];
  8578. }
  8579. [_dictionary setObject:@(value) forKey:key];
  8580. if (_autocreator) {
  8581. GPBAutocreatedDictionaryModified(_autocreator, self);
  8582. }
  8583. }
  8584. - (void)removeValueForKey:(NSString *)aKey {
  8585. [_dictionary removeObjectForKey:aKey];
  8586. }
  8587. - (void)removeAll {
  8588. [_dictionary removeAllObjects];
  8589. }
  8590. @end
  8591. #pragma mark - String -> UInt64
  8592. @implementation GPBStringUInt64Dictionary {
  8593. @package
  8594. NSMutableDictionary *_dictionary;
  8595. }
  8596. + (instancetype)dictionary {
  8597. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8598. }
  8599. + (instancetype)dictionaryWithValue:(uint64_t)value
  8600. forKey:(NSString *)key {
  8601. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8602. // on to get the type correct.
  8603. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:&value
  8604. forKeys:&key
  8605. count:1] autorelease];
  8606. }
  8607. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  8608. forKeys:(const NSString * [])keys
  8609. count:(NSUInteger)count {
  8610. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8611. // on to get the type correct.
  8612. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithValues:values
  8613. forKeys:keys
  8614. count:count] autorelease];
  8615. }
  8616. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8617. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8618. // on to get the type correct.
  8619. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8620. }
  8621. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8622. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8623. }
  8624. - (instancetype)init {
  8625. return [self initWithValues:NULL forKeys:NULL count:0];
  8626. }
  8627. - (instancetype)initWithValues:(const uint64_t [])values
  8628. forKeys:(const NSString * [])keys
  8629. count:(NSUInteger)count {
  8630. self = [super init];
  8631. if (self) {
  8632. _dictionary = [[NSMutableDictionary alloc] init];
  8633. if (count && values && keys) {
  8634. for (NSUInteger i = 0; i < count; ++i) {
  8635. if (!keys[i]) {
  8636. [NSException raise:NSInvalidArgumentException
  8637. format:@"Attempting to add nil key to a Dictionary"];
  8638. }
  8639. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8640. }
  8641. }
  8642. }
  8643. return self;
  8644. }
  8645. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8646. self = [self initWithValues:NULL forKeys:NULL count:0];
  8647. if (self) {
  8648. if (dictionary) {
  8649. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8650. }
  8651. }
  8652. return self;
  8653. }
  8654. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8655. #pragma unused(numItems)
  8656. return [self initWithValues:NULL forKeys:NULL count:0];
  8657. }
  8658. - (void)dealloc {
  8659. NSAssert(!_autocreator,
  8660. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8661. [self class], _autocreator);
  8662. [_dictionary release];
  8663. [super dealloc];
  8664. }
  8665. - (instancetype)copyWithZone:(NSZone *)zone {
  8666. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8667. }
  8668. - (BOOL)isEqual:(GPBStringUInt64Dictionary *)other {
  8669. if (self == other) {
  8670. return YES;
  8671. }
  8672. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8673. return NO;
  8674. }
  8675. return [_dictionary isEqual:other->_dictionary];
  8676. }
  8677. - (NSUInteger)hash {
  8678. return _dictionary.count;
  8679. }
  8680. - (NSString *)description {
  8681. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8682. }
  8683. - (NSUInteger)count {
  8684. return _dictionary.count;
  8685. }
  8686. - (void)enumerateKeysAndValuesUsingBlock:
  8687. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8688. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8689. NSNumber *aValue,
  8690. BOOL *stop) {
  8691. block(aKey, [aValue unsignedLongLongValue], stop);
  8692. }];
  8693. }
  8694. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8695. NSUInteger count = _dictionary.count;
  8696. if (count == 0) {
  8697. return 0;
  8698. }
  8699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8700. GPBDataType keyDataType = field.mapKeyDataType;
  8701. __block size_t result = 0;
  8702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8703. NSNumber *aValue,
  8704. BOOL *stop) {
  8705. #pragma unused(stop)
  8706. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8707. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8708. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8709. }];
  8710. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8711. result += tagSize * count;
  8712. return result;
  8713. }
  8714. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8715. asField:(GPBFieldDescriptor *)field {
  8716. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8717. GPBDataType keyDataType = field.mapKeyDataType;
  8718. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8719. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8720. NSNumber *aValue,
  8721. BOOL *stop) {
  8722. #pragma unused(stop)
  8723. // Write the tag.
  8724. [outputStream writeInt32NoTag:tag];
  8725. // Write the size of the message.
  8726. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8727. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8728. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8729. // Write the fields.
  8730. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8731. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8732. }];
  8733. }
  8734. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8735. forGPBGenericValueKey:(GPBGenericValue *)key {
  8736. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8737. }
  8738. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8739. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8740. #pragma unused(stop)
  8741. block(key, [NSString stringWithFormat:@"%llu", value]);
  8742. }];
  8743. }
  8744. - (BOOL)valueForKey:(NSString *)key value:(uint64_t *)value {
  8745. NSNumber *wrapped = [_dictionary objectForKey:key];
  8746. if (wrapped && value) {
  8747. *value = [wrapped unsignedLongLongValue];
  8748. }
  8749. return (wrapped != NULL);
  8750. }
  8751. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8752. if (otherDictionary) {
  8753. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8754. if (_autocreator) {
  8755. GPBAutocreatedDictionaryModified(_autocreator, self);
  8756. }
  8757. }
  8758. }
  8759. - (void)setValue:(uint64_t)value forKey:(NSString *)key {
  8760. if (!key) {
  8761. [NSException raise:NSInvalidArgumentException
  8762. format:@"Attempting to add nil key to a Dictionary"];
  8763. }
  8764. [_dictionary setObject:@(value) forKey:key];
  8765. if (_autocreator) {
  8766. GPBAutocreatedDictionaryModified(_autocreator, self);
  8767. }
  8768. }
  8769. - (void)removeValueForKey:(NSString *)aKey {
  8770. [_dictionary removeObjectForKey:aKey];
  8771. }
  8772. - (void)removeAll {
  8773. [_dictionary removeAllObjects];
  8774. }
  8775. @end
  8776. #pragma mark - String -> Int64
  8777. @implementation GPBStringInt64Dictionary {
  8778. @package
  8779. NSMutableDictionary *_dictionary;
  8780. }
  8781. + (instancetype)dictionary {
  8782. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8783. }
  8784. + (instancetype)dictionaryWithValue:(int64_t)value
  8785. forKey:(NSString *)key {
  8786. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8787. // on to get the type correct.
  8788. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:&value
  8789. forKeys:&key
  8790. count:1] autorelease];
  8791. }
  8792. + (instancetype)dictionaryWithValues:(const int64_t [])values
  8793. forKeys:(const NSString * [])keys
  8794. count:(NSUInteger)count {
  8795. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8796. // on to get the type correct.
  8797. return [[(GPBStringInt64Dictionary*)[self alloc] initWithValues:values
  8798. forKeys:keys
  8799. count:count] autorelease];
  8800. }
  8801. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8802. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8803. // on to get the type correct.
  8804. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8805. }
  8806. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8807. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8808. }
  8809. - (instancetype)init {
  8810. return [self initWithValues:NULL forKeys:NULL count:0];
  8811. }
  8812. - (instancetype)initWithValues:(const int64_t [])values
  8813. forKeys:(const NSString * [])keys
  8814. count:(NSUInteger)count {
  8815. self = [super init];
  8816. if (self) {
  8817. _dictionary = [[NSMutableDictionary alloc] init];
  8818. if (count && values && keys) {
  8819. for (NSUInteger i = 0; i < count; ++i) {
  8820. if (!keys[i]) {
  8821. [NSException raise:NSInvalidArgumentException
  8822. format:@"Attempting to add nil key to a Dictionary"];
  8823. }
  8824. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8825. }
  8826. }
  8827. }
  8828. return self;
  8829. }
  8830. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8831. self = [self initWithValues:NULL forKeys:NULL count:0];
  8832. if (self) {
  8833. if (dictionary) {
  8834. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8835. }
  8836. }
  8837. return self;
  8838. }
  8839. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8840. #pragma unused(numItems)
  8841. return [self initWithValues:NULL forKeys:NULL count:0];
  8842. }
  8843. - (void)dealloc {
  8844. NSAssert(!_autocreator,
  8845. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8846. [self class], _autocreator);
  8847. [_dictionary release];
  8848. [super dealloc];
  8849. }
  8850. - (instancetype)copyWithZone:(NSZone *)zone {
  8851. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8852. }
  8853. - (BOOL)isEqual:(GPBStringInt64Dictionary *)other {
  8854. if (self == other) {
  8855. return YES;
  8856. }
  8857. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8858. return NO;
  8859. }
  8860. return [_dictionary isEqual:other->_dictionary];
  8861. }
  8862. - (NSUInteger)hash {
  8863. return _dictionary.count;
  8864. }
  8865. - (NSString *)description {
  8866. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8867. }
  8868. - (NSUInteger)count {
  8869. return _dictionary.count;
  8870. }
  8871. - (void)enumerateKeysAndValuesUsingBlock:
  8872. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8873. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8874. NSNumber *aValue,
  8875. BOOL *stop) {
  8876. block(aKey, [aValue longLongValue], stop);
  8877. }];
  8878. }
  8879. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8880. NSUInteger count = _dictionary.count;
  8881. if (count == 0) {
  8882. return 0;
  8883. }
  8884. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8885. GPBDataType keyDataType = field.mapKeyDataType;
  8886. __block size_t result = 0;
  8887. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8888. NSNumber *aValue,
  8889. BOOL *stop) {
  8890. #pragma unused(stop)
  8891. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8892. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8893. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8894. }];
  8895. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8896. result += tagSize * count;
  8897. return result;
  8898. }
  8899. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8900. asField:(GPBFieldDescriptor *)field {
  8901. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8902. GPBDataType keyDataType = field.mapKeyDataType;
  8903. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8904. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8905. NSNumber *aValue,
  8906. BOOL *stop) {
  8907. #pragma unused(stop)
  8908. // Write the tag.
  8909. [outputStream writeInt32NoTag:tag];
  8910. // Write the size of the message.
  8911. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8912. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8913. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8914. // Write the fields.
  8915. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8916. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8917. }];
  8918. }
  8919. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8920. forGPBGenericValueKey:(GPBGenericValue *)key {
  8921. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8922. }
  8923. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8924. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8925. #pragma unused(stop)
  8926. block(key, [NSString stringWithFormat:@"%lld", value]);
  8927. }];
  8928. }
  8929. - (BOOL)valueForKey:(NSString *)key value:(int64_t *)value {
  8930. NSNumber *wrapped = [_dictionary objectForKey:key];
  8931. if (wrapped && value) {
  8932. *value = [wrapped longLongValue];
  8933. }
  8934. return (wrapped != NULL);
  8935. }
  8936. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8937. if (otherDictionary) {
  8938. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8939. if (_autocreator) {
  8940. GPBAutocreatedDictionaryModified(_autocreator, self);
  8941. }
  8942. }
  8943. }
  8944. - (void)setValue:(int64_t)value forKey:(NSString *)key {
  8945. if (!key) {
  8946. [NSException raise:NSInvalidArgumentException
  8947. format:@"Attempting to add nil key to a Dictionary"];
  8948. }
  8949. [_dictionary setObject:@(value) forKey:key];
  8950. if (_autocreator) {
  8951. GPBAutocreatedDictionaryModified(_autocreator, self);
  8952. }
  8953. }
  8954. - (void)removeValueForKey:(NSString *)aKey {
  8955. [_dictionary removeObjectForKey:aKey];
  8956. }
  8957. - (void)removeAll {
  8958. [_dictionary removeAllObjects];
  8959. }
  8960. @end
  8961. #pragma mark - String -> Bool
  8962. @implementation GPBStringBoolDictionary {
  8963. @package
  8964. NSMutableDictionary *_dictionary;
  8965. }
  8966. + (instancetype)dictionary {
  8967. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  8968. }
  8969. + (instancetype)dictionaryWithValue:(BOOL)value
  8970. forKey:(NSString *)key {
  8971. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8972. // on to get the type correct.
  8973. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:&value
  8974. forKeys:&key
  8975. count:1] autorelease];
  8976. }
  8977. + (instancetype)dictionaryWithValues:(const BOOL [])values
  8978. forKeys:(const NSString * [])keys
  8979. count:(NSUInteger)count {
  8980. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  8981. // on to get the type correct.
  8982. return [[(GPBStringBoolDictionary*)[self alloc] initWithValues:values
  8983. forKeys:keys
  8984. count:count] autorelease];
  8985. }
  8986. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8987. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8988. // on to get the type correct.
  8989. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8990. }
  8991. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8992. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8993. }
  8994. - (instancetype)init {
  8995. return [self initWithValues:NULL forKeys:NULL count:0];
  8996. }
  8997. - (instancetype)initWithValues:(const BOOL [])values
  8998. forKeys:(const NSString * [])keys
  8999. count:(NSUInteger)count {
  9000. self = [super init];
  9001. if (self) {
  9002. _dictionary = [[NSMutableDictionary alloc] init];
  9003. if (count && values && keys) {
  9004. for (NSUInteger i = 0; i < count; ++i) {
  9005. if (!keys[i]) {
  9006. [NSException raise:NSInvalidArgumentException
  9007. format:@"Attempting to add nil key to a Dictionary"];
  9008. }
  9009. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9010. }
  9011. }
  9012. }
  9013. return self;
  9014. }
  9015. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9016. self = [self initWithValues:NULL forKeys:NULL count:0];
  9017. if (self) {
  9018. if (dictionary) {
  9019. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9020. }
  9021. }
  9022. return self;
  9023. }
  9024. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9025. #pragma unused(numItems)
  9026. return [self initWithValues:NULL forKeys:NULL count:0];
  9027. }
  9028. - (void)dealloc {
  9029. NSAssert(!_autocreator,
  9030. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9031. [self class], _autocreator);
  9032. [_dictionary release];
  9033. [super dealloc];
  9034. }
  9035. - (instancetype)copyWithZone:(NSZone *)zone {
  9036. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9037. }
  9038. - (BOOL)isEqual:(GPBStringBoolDictionary *)other {
  9039. if (self == other) {
  9040. return YES;
  9041. }
  9042. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  9043. return NO;
  9044. }
  9045. return [_dictionary isEqual:other->_dictionary];
  9046. }
  9047. - (NSUInteger)hash {
  9048. return _dictionary.count;
  9049. }
  9050. - (NSString *)description {
  9051. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9052. }
  9053. - (NSUInteger)count {
  9054. return _dictionary.count;
  9055. }
  9056. - (void)enumerateKeysAndValuesUsingBlock:
  9057. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  9058. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9059. NSNumber *aValue,
  9060. BOOL *stop) {
  9061. block(aKey, [aValue boolValue], stop);
  9062. }];
  9063. }
  9064. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9065. NSUInteger count = _dictionary.count;
  9066. if (count == 0) {
  9067. return 0;
  9068. }
  9069. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9070. GPBDataType keyDataType = field.mapKeyDataType;
  9071. __block size_t result = 0;
  9072. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9073. NSNumber *aValue,
  9074. BOOL *stop) {
  9075. #pragma unused(stop)
  9076. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9077. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9078. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9079. }];
  9080. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9081. result += tagSize * count;
  9082. return result;
  9083. }
  9084. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9085. asField:(GPBFieldDescriptor *)field {
  9086. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9087. GPBDataType keyDataType = field.mapKeyDataType;
  9088. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9089. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9090. NSNumber *aValue,
  9091. BOOL *stop) {
  9092. #pragma unused(stop)
  9093. // Write the tag.
  9094. [outputStream writeInt32NoTag:tag];
  9095. // Write the size of the message.
  9096. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9097. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9098. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9099. // Write the fields.
  9100. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9101. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  9102. }];
  9103. }
  9104. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9105. forGPBGenericValueKey:(GPBGenericValue *)key {
  9106. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  9107. }
  9108. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9109. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  9110. #pragma unused(stop)
  9111. block(key, (value ? @"true" : @"false"));
  9112. }];
  9113. }
  9114. - (BOOL)valueForKey:(NSString *)key value:(BOOL *)value {
  9115. NSNumber *wrapped = [_dictionary objectForKey:key];
  9116. if (wrapped && value) {
  9117. *value = [wrapped boolValue];
  9118. }
  9119. return (wrapped != NULL);
  9120. }
  9121. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9122. if (otherDictionary) {
  9123. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9124. if (_autocreator) {
  9125. GPBAutocreatedDictionaryModified(_autocreator, self);
  9126. }
  9127. }
  9128. }
  9129. - (void)setValue:(BOOL)value forKey:(NSString *)key {
  9130. if (!key) {
  9131. [NSException raise:NSInvalidArgumentException
  9132. format:@"Attempting to add nil key to a Dictionary"];
  9133. }
  9134. [_dictionary setObject:@(value) forKey:key];
  9135. if (_autocreator) {
  9136. GPBAutocreatedDictionaryModified(_autocreator, self);
  9137. }
  9138. }
  9139. - (void)removeValueForKey:(NSString *)aKey {
  9140. [_dictionary removeObjectForKey:aKey];
  9141. }
  9142. - (void)removeAll {
  9143. [_dictionary removeAllObjects];
  9144. }
  9145. @end
  9146. #pragma mark - String -> Float
  9147. @implementation GPBStringFloatDictionary {
  9148. @package
  9149. NSMutableDictionary *_dictionary;
  9150. }
  9151. + (instancetype)dictionary {
  9152. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9153. }
  9154. + (instancetype)dictionaryWithValue:(float)value
  9155. forKey:(NSString *)key {
  9156. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9157. // on to get the type correct.
  9158. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:&value
  9159. forKeys:&key
  9160. count:1] autorelease];
  9161. }
  9162. + (instancetype)dictionaryWithValues:(const float [])values
  9163. forKeys:(const NSString * [])keys
  9164. count:(NSUInteger)count {
  9165. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9166. // on to get the type correct.
  9167. return [[(GPBStringFloatDictionary*)[self alloc] initWithValues:values
  9168. forKeys:keys
  9169. count:count] autorelease];
  9170. }
  9171. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9172. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9173. // on to get the type correct.
  9174. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9175. }
  9176. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9177. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9178. }
  9179. - (instancetype)init {
  9180. return [self initWithValues:NULL forKeys:NULL count:0];
  9181. }
  9182. - (instancetype)initWithValues:(const float [])values
  9183. forKeys:(const NSString * [])keys
  9184. count:(NSUInteger)count {
  9185. self = [super init];
  9186. if (self) {
  9187. _dictionary = [[NSMutableDictionary alloc] init];
  9188. if (count && values && keys) {
  9189. for (NSUInteger i = 0; i < count; ++i) {
  9190. if (!keys[i]) {
  9191. [NSException raise:NSInvalidArgumentException
  9192. format:@"Attempting to add nil key to a Dictionary"];
  9193. }
  9194. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9195. }
  9196. }
  9197. }
  9198. return self;
  9199. }
  9200. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9201. self = [self initWithValues:NULL forKeys:NULL count:0];
  9202. if (self) {
  9203. if (dictionary) {
  9204. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9205. }
  9206. }
  9207. return self;
  9208. }
  9209. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9210. #pragma unused(numItems)
  9211. return [self initWithValues:NULL forKeys:NULL count:0];
  9212. }
  9213. - (void)dealloc {
  9214. NSAssert(!_autocreator,
  9215. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9216. [self class], _autocreator);
  9217. [_dictionary release];
  9218. [super dealloc];
  9219. }
  9220. - (instancetype)copyWithZone:(NSZone *)zone {
  9221. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9222. }
  9223. - (BOOL)isEqual:(GPBStringFloatDictionary *)other {
  9224. if (self == other) {
  9225. return YES;
  9226. }
  9227. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9228. return NO;
  9229. }
  9230. return [_dictionary isEqual:other->_dictionary];
  9231. }
  9232. - (NSUInteger)hash {
  9233. return _dictionary.count;
  9234. }
  9235. - (NSString *)description {
  9236. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9237. }
  9238. - (NSUInteger)count {
  9239. return _dictionary.count;
  9240. }
  9241. - (void)enumerateKeysAndValuesUsingBlock:
  9242. (void (^)(NSString *key, float value, BOOL *stop))block {
  9243. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9244. NSNumber *aValue,
  9245. BOOL *stop) {
  9246. block(aKey, [aValue floatValue], stop);
  9247. }];
  9248. }
  9249. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9250. NSUInteger count = _dictionary.count;
  9251. if (count == 0) {
  9252. return 0;
  9253. }
  9254. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9255. GPBDataType keyDataType = field.mapKeyDataType;
  9256. __block size_t result = 0;
  9257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9258. NSNumber *aValue,
  9259. BOOL *stop) {
  9260. #pragma unused(stop)
  9261. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9262. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9263. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9264. }];
  9265. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9266. result += tagSize * count;
  9267. return result;
  9268. }
  9269. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9270. asField:(GPBFieldDescriptor *)field {
  9271. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9272. GPBDataType keyDataType = field.mapKeyDataType;
  9273. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9274. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9275. NSNumber *aValue,
  9276. BOOL *stop) {
  9277. #pragma unused(stop)
  9278. // Write the tag.
  9279. [outputStream writeInt32NoTag:tag];
  9280. // Write the size of the message.
  9281. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9282. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9283. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9284. // Write the fields.
  9285. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9286. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9287. }];
  9288. }
  9289. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9290. forGPBGenericValueKey:(GPBGenericValue *)key {
  9291. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9292. }
  9293. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9294. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9295. #pragma unused(stop)
  9296. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9297. }];
  9298. }
  9299. - (BOOL)valueForKey:(NSString *)key value:(float *)value {
  9300. NSNumber *wrapped = [_dictionary objectForKey:key];
  9301. if (wrapped && value) {
  9302. *value = [wrapped floatValue];
  9303. }
  9304. return (wrapped != NULL);
  9305. }
  9306. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9307. if (otherDictionary) {
  9308. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9309. if (_autocreator) {
  9310. GPBAutocreatedDictionaryModified(_autocreator, self);
  9311. }
  9312. }
  9313. }
  9314. - (void)setValue:(float)value forKey:(NSString *)key {
  9315. if (!key) {
  9316. [NSException raise:NSInvalidArgumentException
  9317. format:@"Attempting to add nil key to a Dictionary"];
  9318. }
  9319. [_dictionary setObject:@(value) forKey:key];
  9320. if (_autocreator) {
  9321. GPBAutocreatedDictionaryModified(_autocreator, self);
  9322. }
  9323. }
  9324. - (void)removeValueForKey:(NSString *)aKey {
  9325. [_dictionary removeObjectForKey:aKey];
  9326. }
  9327. - (void)removeAll {
  9328. [_dictionary removeAllObjects];
  9329. }
  9330. @end
  9331. #pragma mark - String -> Double
  9332. @implementation GPBStringDoubleDictionary {
  9333. @package
  9334. NSMutableDictionary *_dictionary;
  9335. }
  9336. + (instancetype)dictionary {
  9337. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9338. }
  9339. + (instancetype)dictionaryWithValue:(double)value
  9340. forKey:(NSString *)key {
  9341. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9342. // on to get the type correct.
  9343. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:&value
  9344. forKeys:&key
  9345. count:1] autorelease];
  9346. }
  9347. + (instancetype)dictionaryWithValues:(const double [])values
  9348. forKeys:(const NSString * [])keys
  9349. count:(NSUInteger)count {
  9350. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9351. // on to get the type correct.
  9352. return [[(GPBStringDoubleDictionary*)[self alloc] initWithValues:values
  9353. forKeys:keys
  9354. count:count] autorelease];
  9355. }
  9356. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9357. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9358. // on to get the type correct.
  9359. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9360. }
  9361. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9362. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9363. }
  9364. - (instancetype)init {
  9365. return [self initWithValues:NULL forKeys:NULL count:0];
  9366. }
  9367. - (instancetype)initWithValues:(const double [])values
  9368. forKeys:(const NSString * [])keys
  9369. count:(NSUInteger)count {
  9370. self = [super init];
  9371. if (self) {
  9372. _dictionary = [[NSMutableDictionary alloc] init];
  9373. if (count && values && keys) {
  9374. for (NSUInteger i = 0; i < count; ++i) {
  9375. if (!keys[i]) {
  9376. [NSException raise:NSInvalidArgumentException
  9377. format:@"Attempting to add nil key to a Dictionary"];
  9378. }
  9379. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9380. }
  9381. }
  9382. }
  9383. return self;
  9384. }
  9385. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9386. self = [self initWithValues:NULL forKeys:NULL count:0];
  9387. if (self) {
  9388. if (dictionary) {
  9389. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9390. }
  9391. }
  9392. return self;
  9393. }
  9394. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9395. #pragma unused(numItems)
  9396. return [self initWithValues:NULL forKeys:NULL count:0];
  9397. }
  9398. - (void)dealloc {
  9399. NSAssert(!_autocreator,
  9400. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9401. [self class], _autocreator);
  9402. [_dictionary release];
  9403. [super dealloc];
  9404. }
  9405. - (instancetype)copyWithZone:(NSZone *)zone {
  9406. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9407. }
  9408. - (BOOL)isEqual:(GPBStringDoubleDictionary *)other {
  9409. if (self == other) {
  9410. return YES;
  9411. }
  9412. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9413. return NO;
  9414. }
  9415. return [_dictionary isEqual:other->_dictionary];
  9416. }
  9417. - (NSUInteger)hash {
  9418. return _dictionary.count;
  9419. }
  9420. - (NSString *)description {
  9421. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9422. }
  9423. - (NSUInteger)count {
  9424. return _dictionary.count;
  9425. }
  9426. - (void)enumerateKeysAndValuesUsingBlock:
  9427. (void (^)(NSString *key, double value, BOOL *stop))block {
  9428. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9429. NSNumber *aValue,
  9430. BOOL *stop) {
  9431. block(aKey, [aValue doubleValue], stop);
  9432. }];
  9433. }
  9434. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9435. NSUInteger count = _dictionary.count;
  9436. if (count == 0) {
  9437. return 0;
  9438. }
  9439. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9440. GPBDataType keyDataType = field.mapKeyDataType;
  9441. __block size_t result = 0;
  9442. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9443. NSNumber *aValue,
  9444. BOOL *stop) {
  9445. #pragma unused(stop)
  9446. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9447. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9448. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9449. }];
  9450. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9451. result += tagSize * count;
  9452. return result;
  9453. }
  9454. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9455. asField:(GPBFieldDescriptor *)field {
  9456. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9457. GPBDataType keyDataType = field.mapKeyDataType;
  9458. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9460. NSNumber *aValue,
  9461. BOOL *stop) {
  9462. #pragma unused(stop)
  9463. // Write the tag.
  9464. [outputStream writeInt32NoTag:tag];
  9465. // Write the size of the message.
  9466. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9467. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9468. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9469. // Write the fields.
  9470. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9471. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9472. }];
  9473. }
  9474. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9475. forGPBGenericValueKey:(GPBGenericValue *)key {
  9476. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9477. }
  9478. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9479. [self enumerateKeysAndValuesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9480. #pragma unused(stop)
  9481. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9482. }];
  9483. }
  9484. - (BOOL)valueForKey:(NSString *)key value:(double *)value {
  9485. NSNumber *wrapped = [_dictionary objectForKey:key];
  9486. if (wrapped && value) {
  9487. *value = [wrapped doubleValue];
  9488. }
  9489. return (wrapped != NULL);
  9490. }
  9491. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9492. if (otherDictionary) {
  9493. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9494. if (_autocreator) {
  9495. GPBAutocreatedDictionaryModified(_autocreator, self);
  9496. }
  9497. }
  9498. }
  9499. - (void)setValue:(double)value forKey:(NSString *)key {
  9500. if (!key) {
  9501. [NSException raise:NSInvalidArgumentException
  9502. format:@"Attempting to add nil key to a Dictionary"];
  9503. }
  9504. [_dictionary setObject:@(value) forKey:key];
  9505. if (_autocreator) {
  9506. GPBAutocreatedDictionaryModified(_autocreator, self);
  9507. }
  9508. }
  9509. - (void)removeValueForKey:(NSString *)aKey {
  9510. [_dictionary removeObjectForKey:aKey];
  9511. }
  9512. - (void)removeAll {
  9513. [_dictionary removeAllObjects];
  9514. }
  9515. @end
  9516. #pragma mark - String -> Enum
  9517. @implementation GPBStringEnumDictionary {
  9518. @package
  9519. NSMutableDictionary *_dictionary;
  9520. GPBEnumValidationFunc _validationFunc;
  9521. }
  9522. @synthesize validationFunc = _validationFunc;
  9523. + (instancetype)dictionary {
  9524. return [[[self alloc] initWithValidationFunction:NULL
  9525. rawValues:NULL
  9526. forKeys:NULL
  9527. count:0] autorelease];
  9528. }
  9529. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9530. return [[[self alloc] initWithValidationFunction:func
  9531. rawValues:NULL
  9532. forKeys:NULL
  9533. count:0] autorelease];
  9534. }
  9535. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9536. rawValue:(int32_t)rawValue
  9537. forKey:(NSString *)key {
  9538. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9539. // on to get the type correct.
  9540. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9541. rawValues:&rawValue
  9542. forKeys:&key
  9543. count:1] autorelease];
  9544. }
  9545. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9546. rawValues:(const int32_t [])rawValues
  9547. forKeys:(const NSString * [])keys
  9548. count:(NSUInteger)count {
  9549. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9550. // on to get the type correct.
  9551. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9552. rawValues:rawValues
  9553. forKeys:keys
  9554. count:count] autorelease];
  9555. }
  9556. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9557. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9558. // on to get the type correct.
  9559. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9560. }
  9561. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9562. capacity:(NSUInteger)numItems {
  9563. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9564. }
  9565. - (instancetype)init {
  9566. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9567. }
  9568. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9569. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9570. }
  9571. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9572. rawValues:(const int32_t [])rawValues
  9573. forKeys:(const NSString * [])keys
  9574. count:(NSUInteger)count {
  9575. self = [super init];
  9576. if (self) {
  9577. _dictionary = [[NSMutableDictionary alloc] init];
  9578. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9579. if (count && rawValues && keys) {
  9580. for (NSUInteger i = 0; i < count; ++i) {
  9581. if (!keys[i]) {
  9582. [NSException raise:NSInvalidArgumentException
  9583. format:@"Attempting to add nil key to a Dictionary"];
  9584. }
  9585. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9586. }
  9587. }
  9588. }
  9589. return self;
  9590. }
  9591. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9592. self = [self initWithValidationFunction:dictionary.validationFunc
  9593. rawValues:NULL
  9594. forKeys:NULL
  9595. count:0];
  9596. if (self) {
  9597. if (dictionary) {
  9598. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9599. }
  9600. }
  9601. return self;
  9602. }
  9603. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9604. capacity:(NSUInteger)numItems {
  9605. #pragma unused(numItems)
  9606. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9607. }
  9608. - (void)dealloc {
  9609. NSAssert(!_autocreator,
  9610. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9611. [self class], _autocreator);
  9612. [_dictionary release];
  9613. [super dealloc];
  9614. }
  9615. - (instancetype)copyWithZone:(NSZone *)zone {
  9616. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9617. }
  9618. - (BOOL)isEqual:(GPBStringEnumDictionary *)other {
  9619. if (self == other) {
  9620. return YES;
  9621. }
  9622. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9623. return NO;
  9624. }
  9625. return [_dictionary isEqual:other->_dictionary];
  9626. }
  9627. - (NSUInteger)hash {
  9628. return _dictionary.count;
  9629. }
  9630. - (NSString *)description {
  9631. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9632. }
  9633. - (NSUInteger)count {
  9634. return _dictionary.count;
  9635. }
  9636. - (void)enumerateKeysAndRawValuesUsingBlock:
  9637. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9638. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9639. NSNumber *aValue,
  9640. BOOL *stop) {
  9641. block(aKey, [aValue intValue], stop);
  9642. }];
  9643. }
  9644. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9645. NSUInteger count = _dictionary.count;
  9646. if (count == 0) {
  9647. return 0;
  9648. }
  9649. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9650. GPBDataType keyDataType = field.mapKeyDataType;
  9651. __block size_t result = 0;
  9652. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9653. NSNumber *aValue,
  9654. BOOL *stop) {
  9655. #pragma unused(stop)
  9656. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9657. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9658. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9659. }];
  9660. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9661. result += tagSize * count;
  9662. return result;
  9663. }
  9664. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9665. asField:(GPBFieldDescriptor *)field {
  9666. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9667. GPBDataType keyDataType = field.mapKeyDataType;
  9668. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9669. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9670. NSNumber *aValue,
  9671. BOOL *stop) {
  9672. #pragma unused(stop)
  9673. // Write the tag.
  9674. [outputStream writeInt32NoTag:tag];
  9675. // Write the size of the message.
  9676. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9677. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9678. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9679. // Write the fields.
  9680. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9681. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9682. }];
  9683. }
  9684. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9685. forKey:(GPBGenericValue *)key
  9686. keyDataType:(GPBDataType)keyDataType {
  9687. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9688. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9689. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9690. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9691. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9692. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9693. [outputStream release];
  9694. return data;
  9695. }
  9696. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9697. forGPBGenericValueKey:(GPBGenericValue *)key {
  9698. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9699. }
  9700. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9701. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9702. #pragma unused(stop)
  9703. block(key, @(value));
  9704. }];
  9705. }
  9706. - (BOOL)valueForKey:(NSString *)key value:(int32_t *)value {
  9707. NSNumber *wrapped = [_dictionary objectForKey:key];
  9708. if (wrapped && value) {
  9709. int32_t result = [wrapped intValue];
  9710. if (!_validationFunc(result)) {
  9711. result = kGPBUnrecognizedEnumeratorValue;
  9712. }
  9713. *value = result;
  9714. }
  9715. return (wrapped != NULL);
  9716. }
  9717. - (BOOL)valueForKey:(NSString *)key rawValue:(int32_t *)rawValue {
  9718. NSNumber *wrapped = [_dictionary objectForKey:key];
  9719. if (wrapped && rawValue) {
  9720. *rawValue = [wrapped intValue];
  9721. }
  9722. return (wrapped != NULL);
  9723. }
  9724. - (void)enumerateKeysAndValuesUsingBlock:
  9725. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9726. GPBEnumValidationFunc func = _validationFunc;
  9727. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9728. NSNumber *aValue,
  9729. BOOL *stop) {
  9730. int32_t unwrapped = [aValue intValue];
  9731. if (!func(unwrapped)) {
  9732. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9733. }
  9734. block(aKey, unwrapped, stop);
  9735. }];
  9736. }
  9737. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9738. if (otherDictionary) {
  9739. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9740. if (_autocreator) {
  9741. GPBAutocreatedDictionaryModified(_autocreator, self);
  9742. }
  9743. }
  9744. }
  9745. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9746. if (!key) {
  9747. [NSException raise:NSInvalidArgumentException
  9748. format:@"Attempting to add nil key to a Dictionary"];
  9749. }
  9750. [_dictionary setObject:@(value) forKey:key];
  9751. if (_autocreator) {
  9752. GPBAutocreatedDictionaryModified(_autocreator, self);
  9753. }
  9754. }
  9755. - (void)removeValueForKey:(NSString *)aKey {
  9756. [_dictionary removeObjectForKey:aKey];
  9757. }
  9758. - (void)removeAll {
  9759. [_dictionary removeAllObjects];
  9760. }
  9761. - (void)setValue:(int32_t)value forKey:(NSString *)key {
  9762. if (!key) {
  9763. [NSException raise:NSInvalidArgumentException
  9764. format:@"Attempting to add nil key to a Dictionary"];
  9765. }
  9766. if (!_validationFunc(value)) {
  9767. [NSException raise:NSInvalidArgumentException
  9768. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9769. value];
  9770. }
  9771. [_dictionary setObject:@(value) forKey:key];
  9772. if (_autocreator) {
  9773. GPBAutocreatedDictionaryModified(_autocreator, self);
  9774. }
  9775. }
  9776. @end
  9777. //%PDDM-EXPAND-END (5 expansions)
  9778. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9779. // This block of code is generated, do not edit it directly.
  9780. #pragma mark - Bool -> UInt32
  9781. @implementation GPBBoolUInt32Dictionary {
  9782. @package
  9783. uint32_t _values[2];
  9784. BOOL _valueSet[2];
  9785. }
  9786. + (instancetype)dictionary {
  9787. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  9788. }
  9789. + (instancetype)dictionaryWithValue:(uint32_t)value
  9790. forKey:(BOOL)key {
  9791. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9792. // on to get the type correct.
  9793. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:&value
  9794. forKeys:&key
  9795. count:1] autorelease];
  9796. }
  9797. + (instancetype)dictionaryWithValues:(const uint32_t [])values
  9798. forKeys:(const BOOL [])keys
  9799. count:(NSUInteger)count {
  9800. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9801. // on to get the type correct.
  9802. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithValues:values
  9803. forKeys:keys
  9804. count:count] autorelease];
  9805. }
  9806. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9807. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9808. // on to get the type correct.
  9809. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9810. }
  9811. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9812. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9813. }
  9814. - (instancetype)init {
  9815. return [self initWithValues:NULL forKeys:NULL count:0];
  9816. }
  9817. - (instancetype)initWithValues:(const uint32_t [])values
  9818. forKeys:(const BOOL [])keys
  9819. count:(NSUInteger)count {
  9820. self = [super init];
  9821. if (self) {
  9822. for (NSUInteger i = 0; i < count; ++i) {
  9823. int idx = keys[i] ? 1 : 0;
  9824. _values[idx] = values[i];
  9825. _valueSet[idx] = YES;
  9826. }
  9827. }
  9828. return self;
  9829. }
  9830. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9831. self = [self initWithValues:NULL forKeys:NULL count:0];
  9832. if (self) {
  9833. if (dictionary) {
  9834. for (int i = 0; i < 2; ++i) {
  9835. if (dictionary->_valueSet[i]) {
  9836. _values[i] = dictionary->_values[i];
  9837. _valueSet[i] = YES;
  9838. }
  9839. }
  9840. }
  9841. }
  9842. return self;
  9843. }
  9844. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9845. #pragma unused(numItems)
  9846. return [self initWithValues:NULL forKeys:NULL count:0];
  9847. }
  9848. #if !defined(NS_BLOCK_ASSERTIONS)
  9849. - (void)dealloc {
  9850. NSAssert(!_autocreator,
  9851. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9852. [self class], _autocreator);
  9853. [super dealloc];
  9854. }
  9855. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9856. - (instancetype)copyWithZone:(NSZone *)zone {
  9857. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9858. }
  9859. - (BOOL)isEqual:(GPBBoolUInt32Dictionary *)other {
  9860. if (self == other) {
  9861. return YES;
  9862. }
  9863. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9864. return NO;
  9865. }
  9866. if ((_valueSet[0] != other->_valueSet[0]) ||
  9867. (_valueSet[1] != other->_valueSet[1])) {
  9868. return NO;
  9869. }
  9870. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  9871. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  9872. return NO;
  9873. }
  9874. return YES;
  9875. }
  9876. - (NSUInteger)hash {
  9877. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9878. }
  9879. - (NSString *)description {
  9880. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9881. if (_valueSet[0]) {
  9882. [result appendFormat:@"NO: %u", _values[0]];
  9883. }
  9884. if (_valueSet[1]) {
  9885. [result appendFormat:@"YES: %u", _values[1]];
  9886. }
  9887. [result appendString:@" }"];
  9888. return result;
  9889. }
  9890. - (NSUInteger)count {
  9891. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9892. }
  9893. - (BOOL)valueForKey:(BOOL)key value:(uint32_t *)value {
  9894. int idx = (key ? 1 : 0);
  9895. if (_valueSet[idx]) {
  9896. if (value) {
  9897. *value = _values[idx];
  9898. }
  9899. return YES;
  9900. }
  9901. return NO;
  9902. }
  9903. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9904. forGPBGenericValueKey:(GPBGenericValue *)key {
  9905. int idx = (key->valueBool ? 1 : 0);
  9906. _values[idx] = value->valueUInt32;
  9907. _valueSet[idx] = YES;
  9908. }
  9909. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9910. if (_valueSet[0]) {
  9911. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9912. }
  9913. if (_valueSet[1]) {
  9914. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9915. }
  9916. }
  9917. - (void)enumerateKeysAndValuesUsingBlock:
  9918. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9919. BOOL stop = NO;
  9920. if (_valueSet[0]) {
  9921. block(NO, _values[0], &stop);
  9922. }
  9923. if (!stop && _valueSet[1]) {
  9924. block(YES, _values[1], &stop);
  9925. }
  9926. }
  9927. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9928. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9929. NSUInteger count = 0;
  9930. size_t result = 0;
  9931. for (int i = 0; i < 2; ++i) {
  9932. if (_valueSet[i]) {
  9933. ++count;
  9934. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9935. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9936. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9937. }
  9938. }
  9939. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9940. result += tagSize * count;
  9941. return result;
  9942. }
  9943. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9944. asField:(GPBFieldDescriptor *)field {
  9945. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9946. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9947. for (int i = 0; i < 2; ++i) {
  9948. if (_valueSet[i]) {
  9949. // Write the tag.
  9950. [outputStream writeInt32NoTag:tag];
  9951. // Write the size of the message.
  9952. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9953. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9954. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9955. // Write the fields.
  9956. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9957. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9958. }
  9959. }
  9960. }
  9961. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9962. if (otherDictionary) {
  9963. for (int i = 0; i < 2; ++i) {
  9964. if (otherDictionary->_valueSet[i]) {
  9965. _valueSet[i] = YES;
  9966. _values[i] = otherDictionary->_values[i];
  9967. }
  9968. }
  9969. if (_autocreator) {
  9970. GPBAutocreatedDictionaryModified(_autocreator, self);
  9971. }
  9972. }
  9973. }
  9974. - (void)setValue:(uint32_t)value forKey:(BOOL)key {
  9975. int idx = (key ? 1 : 0);
  9976. _values[idx] = value;
  9977. _valueSet[idx] = YES;
  9978. if (_autocreator) {
  9979. GPBAutocreatedDictionaryModified(_autocreator, self);
  9980. }
  9981. }
  9982. - (void)removeValueForKey:(BOOL)aKey {
  9983. _valueSet[aKey ? 1 : 0] = NO;
  9984. }
  9985. - (void)removeAll {
  9986. _valueSet[0] = NO;
  9987. _valueSet[1] = NO;
  9988. }
  9989. @end
  9990. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9991. // This block of code is generated, do not edit it directly.
  9992. #pragma mark - Bool -> Int32
  9993. @implementation GPBBoolInt32Dictionary {
  9994. @package
  9995. int32_t _values[2];
  9996. BOOL _valueSet[2];
  9997. }
  9998. + (instancetype)dictionary {
  9999. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10000. }
  10001. + (instancetype)dictionaryWithValue:(int32_t)value
  10002. forKey:(BOOL)key {
  10003. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10004. // on to get the type correct.
  10005. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:&value
  10006. forKeys:&key
  10007. count:1] autorelease];
  10008. }
  10009. + (instancetype)dictionaryWithValues:(const int32_t [])values
  10010. forKeys:(const BOOL [])keys
  10011. count:(NSUInteger)count {
  10012. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10013. // on to get the type correct.
  10014. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithValues:values
  10015. forKeys:keys
  10016. count:count] autorelease];
  10017. }
  10018. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10019. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10020. // on to get the type correct.
  10021. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10022. }
  10023. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10024. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10025. }
  10026. - (instancetype)init {
  10027. return [self initWithValues:NULL forKeys:NULL count:0];
  10028. }
  10029. - (instancetype)initWithValues:(const int32_t [])values
  10030. forKeys:(const BOOL [])keys
  10031. count:(NSUInteger)count {
  10032. self = [super init];
  10033. if (self) {
  10034. for (NSUInteger i = 0; i < count; ++i) {
  10035. int idx = keys[i] ? 1 : 0;
  10036. _values[idx] = values[i];
  10037. _valueSet[idx] = YES;
  10038. }
  10039. }
  10040. return self;
  10041. }
  10042. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10043. self = [self initWithValues:NULL forKeys:NULL count:0];
  10044. if (self) {
  10045. if (dictionary) {
  10046. for (int i = 0; i < 2; ++i) {
  10047. if (dictionary->_valueSet[i]) {
  10048. _values[i] = dictionary->_values[i];
  10049. _valueSet[i] = YES;
  10050. }
  10051. }
  10052. }
  10053. }
  10054. return self;
  10055. }
  10056. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10057. #pragma unused(numItems)
  10058. return [self initWithValues:NULL forKeys:NULL count:0];
  10059. }
  10060. #if !defined(NS_BLOCK_ASSERTIONS)
  10061. - (void)dealloc {
  10062. NSAssert(!_autocreator,
  10063. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10064. [self class], _autocreator);
  10065. [super dealloc];
  10066. }
  10067. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10068. - (instancetype)copyWithZone:(NSZone *)zone {
  10069. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10070. }
  10071. - (BOOL)isEqual:(GPBBoolInt32Dictionary *)other {
  10072. if (self == other) {
  10073. return YES;
  10074. }
  10075. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  10076. return NO;
  10077. }
  10078. if ((_valueSet[0] != other->_valueSet[0]) ||
  10079. (_valueSet[1] != other->_valueSet[1])) {
  10080. return NO;
  10081. }
  10082. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10083. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10084. return NO;
  10085. }
  10086. return YES;
  10087. }
  10088. - (NSUInteger)hash {
  10089. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10090. }
  10091. - (NSString *)description {
  10092. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10093. if (_valueSet[0]) {
  10094. [result appendFormat:@"NO: %d", _values[0]];
  10095. }
  10096. if (_valueSet[1]) {
  10097. [result appendFormat:@"YES: %d", _values[1]];
  10098. }
  10099. [result appendString:@" }"];
  10100. return result;
  10101. }
  10102. - (NSUInteger)count {
  10103. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10104. }
  10105. - (BOOL)valueForKey:(BOOL)key value:(int32_t *)value {
  10106. int idx = (key ? 1 : 0);
  10107. if (_valueSet[idx]) {
  10108. if (value) {
  10109. *value = _values[idx];
  10110. }
  10111. return YES;
  10112. }
  10113. return NO;
  10114. }
  10115. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10116. forGPBGenericValueKey:(GPBGenericValue *)key {
  10117. int idx = (key->valueBool ? 1 : 0);
  10118. _values[idx] = value->valueInt32;
  10119. _valueSet[idx] = YES;
  10120. }
  10121. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10122. if (_valueSet[0]) {
  10123. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  10124. }
  10125. if (_valueSet[1]) {
  10126. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  10127. }
  10128. }
  10129. - (void)enumerateKeysAndValuesUsingBlock:
  10130. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10131. BOOL stop = NO;
  10132. if (_valueSet[0]) {
  10133. block(NO, _values[0], &stop);
  10134. }
  10135. if (!stop && _valueSet[1]) {
  10136. block(YES, _values[1], &stop);
  10137. }
  10138. }
  10139. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10140. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10141. NSUInteger count = 0;
  10142. size_t result = 0;
  10143. for (int i = 0; i < 2; ++i) {
  10144. if (_valueSet[i]) {
  10145. ++count;
  10146. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10147. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10148. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10149. }
  10150. }
  10151. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10152. result += tagSize * count;
  10153. return result;
  10154. }
  10155. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10156. asField:(GPBFieldDescriptor *)field {
  10157. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10158. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10159. for (int i = 0; i < 2; ++i) {
  10160. if (_valueSet[i]) {
  10161. // Write the tag.
  10162. [outputStream writeInt32NoTag:tag];
  10163. // Write the size of the message.
  10164. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10165. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10166. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10167. // Write the fields.
  10168. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10169. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10170. }
  10171. }
  10172. }
  10173. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10174. if (otherDictionary) {
  10175. for (int i = 0; i < 2; ++i) {
  10176. if (otherDictionary->_valueSet[i]) {
  10177. _valueSet[i] = YES;
  10178. _values[i] = otherDictionary->_values[i];
  10179. }
  10180. }
  10181. if (_autocreator) {
  10182. GPBAutocreatedDictionaryModified(_autocreator, self);
  10183. }
  10184. }
  10185. }
  10186. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  10187. int idx = (key ? 1 : 0);
  10188. _values[idx] = value;
  10189. _valueSet[idx] = YES;
  10190. if (_autocreator) {
  10191. GPBAutocreatedDictionaryModified(_autocreator, self);
  10192. }
  10193. }
  10194. - (void)removeValueForKey:(BOOL)aKey {
  10195. _valueSet[aKey ? 1 : 0] = NO;
  10196. }
  10197. - (void)removeAll {
  10198. _valueSet[0] = NO;
  10199. _valueSet[1] = NO;
  10200. }
  10201. @end
  10202. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10203. // This block of code is generated, do not edit it directly.
  10204. #pragma mark - Bool -> UInt64
  10205. @implementation GPBBoolUInt64Dictionary {
  10206. @package
  10207. uint64_t _values[2];
  10208. BOOL _valueSet[2];
  10209. }
  10210. + (instancetype)dictionary {
  10211. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10212. }
  10213. + (instancetype)dictionaryWithValue:(uint64_t)value
  10214. forKey:(BOOL)key {
  10215. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10216. // on to get the type correct.
  10217. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:&value
  10218. forKeys:&key
  10219. count:1] autorelease];
  10220. }
  10221. + (instancetype)dictionaryWithValues:(const uint64_t [])values
  10222. forKeys:(const BOOL [])keys
  10223. count:(NSUInteger)count {
  10224. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10225. // on to get the type correct.
  10226. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithValues:values
  10227. forKeys:keys
  10228. count:count] autorelease];
  10229. }
  10230. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10231. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10232. // on to get the type correct.
  10233. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10234. }
  10235. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10236. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10237. }
  10238. - (instancetype)init {
  10239. return [self initWithValues:NULL forKeys:NULL count:0];
  10240. }
  10241. - (instancetype)initWithValues:(const uint64_t [])values
  10242. forKeys:(const BOOL [])keys
  10243. count:(NSUInteger)count {
  10244. self = [super init];
  10245. if (self) {
  10246. for (NSUInteger i = 0; i < count; ++i) {
  10247. int idx = keys[i] ? 1 : 0;
  10248. _values[idx] = values[i];
  10249. _valueSet[idx] = YES;
  10250. }
  10251. }
  10252. return self;
  10253. }
  10254. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10255. self = [self initWithValues:NULL forKeys:NULL count:0];
  10256. if (self) {
  10257. if (dictionary) {
  10258. for (int i = 0; i < 2; ++i) {
  10259. if (dictionary->_valueSet[i]) {
  10260. _values[i] = dictionary->_values[i];
  10261. _valueSet[i] = YES;
  10262. }
  10263. }
  10264. }
  10265. }
  10266. return self;
  10267. }
  10268. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10269. #pragma unused(numItems)
  10270. return [self initWithValues:NULL forKeys:NULL count:0];
  10271. }
  10272. #if !defined(NS_BLOCK_ASSERTIONS)
  10273. - (void)dealloc {
  10274. NSAssert(!_autocreator,
  10275. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10276. [self class], _autocreator);
  10277. [super dealloc];
  10278. }
  10279. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10280. - (instancetype)copyWithZone:(NSZone *)zone {
  10281. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10282. }
  10283. - (BOOL)isEqual:(GPBBoolUInt64Dictionary *)other {
  10284. if (self == other) {
  10285. return YES;
  10286. }
  10287. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10288. return NO;
  10289. }
  10290. if ((_valueSet[0] != other->_valueSet[0]) ||
  10291. (_valueSet[1] != other->_valueSet[1])) {
  10292. return NO;
  10293. }
  10294. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10295. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10296. return NO;
  10297. }
  10298. return YES;
  10299. }
  10300. - (NSUInteger)hash {
  10301. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10302. }
  10303. - (NSString *)description {
  10304. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10305. if (_valueSet[0]) {
  10306. [result appendFormat:@"NO: %llu", _values[0]];
  10307. }
  10308. if (_valueSet[1]) {
  10309. [result appendFormat:@"YES: %llu", _values[1]];
  10310. }
  10311. [result appendString:@" }"];
  10312. return result;
  10313. }
  10314. - (NSUInteger)count {
  10315. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10316. }
  10317. - (BOOL)valueForKey:(BOOL)key value:(uint64_t *)value {
  10318. int idx = (key ? 1 : 0);
  10319. if (_valueSet[idx]) {
  10320. if (value) {
  10321. *value = _values[idx];
  10322. }
  10323. return YES;
  10324. }
  10325. return NO;
  10326. }
  10327. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10328. forGPBGenericValueKey:(GPBGenericValue *)key {
  10329. int idx = (key->valueBool ? 1 : 0);
  10330. _values[idx] = value->valueUInt64;
  10331. _valueSet[idx] = YES;
  10332. }
  10333. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10334. if (_valueSet[0]) {
  10335. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10336. }
  10337. if (_valueSet[1]) {
  10338. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10339. }
  10340. }
  10341. - (void)enumerateKeysAndValuesUsingBlock:
  10342. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10343. BOOL stop = NO;
  10344. if (_valueSet[0]) {
  10345. block(NO, _values[0], &stop);
  10346. }
  10347. if (!stop && _valueSet[1]) {
  10348. block(YES, _values[1], &stop);
  10349. }
  10350. }
  10351. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10352. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10353. NSUInteger count = 0;
  10354. size_t result = 0;
  10355. for (int i = 0; i < 2; ++i) {
  10356. if (_valueSet[i]) {
  10357. ++count;
  10358. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10359. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10360. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10361. }
  10362. }
  10363. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10364. result += tagSize * count;
  10365. return result;
  10366. }
  10367. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10368. asField:(GPBFieldDescriptor *)field {
  10369. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10370. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10371. for (int i = 0; i < 2; ++i) {
  10372. if (_valueSet[i]) {
  10373. // Write the tag.
  10374. [outputStream writeInt32NoTag:tag];
  10375. // Write the size of the message.
  10376. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10377. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10378. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10379. // Write the fields.
  10380. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10381. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10382. }
  10383. }
  10384. }
  10385. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10386. if (otherDictionary) {
  10387. for (int i = 0; i < 2; ++i) {
  10388. if (otherDictionary->_valueSet[i]) {
  10389. _valueSet[i] = YES;
  10390. _values[i] = otherDictionary->_values[i];
  10391. }
  10392. }
  10393. if (_autocreator) {
  10394. GPBAutocreatedDictionaryModified(_autocreator, self);
  10395. }
  10396. }
  10397. }
  10398. - (void)setValue:(uint64_t)value forKey:(BOOL)key {
  10399. int idx = (key ? 1 : 0);
  10400. _values[idx] = value;
  10401. _valueSet[idx] = YES;
  10402. if (_autocreator) {
  10403. GPBAutocreatedDictionaryModified(_autocreator, self);
  10404. }
  10405. }
  10406. - (void)removeValueForKey:(BOOL)aKey {
  10407. _valueSet[aKey ? 1 : 0] = NO;
  10408. }
  10409. - (void)removeAll {
  10410. _valueSet[0] = NO;
  10411. _valueSet[1] = NO;
  10412. }
  10413. @end
  10414. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10415. // This block of code is generated, do not edit it directly.
  10416. #pragma mark - Bool -> Int64
  10417. @implementation GPBBoolInt64Dictionary {
  10418. @package
  10419. int64_t _values[2];
  10420. BOOL _valueSet[2];
  10421. }
  10422. + (instancetype)dictionary {
  10423. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10424. }
  10425. + (instancetype)dictionaryWithValue:(int64_t)value
  10426. forKey:(BOOL)key {
  10427. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10428. // on to get the type correct.
  10429. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:&value
  10430. forKeys:&key
  10431. count:1] autorelease];
  10432. }
  10433. + (instancetype)dictionaryWithValues:(const int64_t [])values
  10434. forKeys:(const BOOL [])keys
  10435. count:(NSUInteger)count {
  10436. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10437. // on to get the type correct.
  10438. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithValues:values
  10439. forKeys:keys
  10440. count:count] autorelease];
  10441. }
  10442. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10443. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10444. // on to get the type correct.
  10445. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10446. }
  10447. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10448. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10449. }
  10450. - (instancetype)init {
  10451. return [self initWithValues:NULL forKeys:NULL count:0];
  10452. }
  10453. - (instancetype)initWithValues:(const int64_t [])values
  10454. forKeys:(const BOOL [])keys
  10455. count:(NSUInteger)count {
  10456. self = [super init];
  10457. if (self) {
  10458. for (NSUInteger i = 0; i < count; ++i) {
  10459. int idx = keys[i] ? 1 : 0;
  10460. _values[idx] = values[i];
  10461. _valueSet[idx] = YES;
  10462. }
  10463. }
  10464. return self;
  10465. }
  10466. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10467. self = [self initWithValues:NULL forKeys:NULL count:0];
  10468. if (self) {
  10469. if (dictionary) {
  10470. for (int i = 0; i < 2; ++i) {
  10471. if (dictionary->_valueSet[i]) {
  10472. _values[i] = dictionary->_values[i];
  10473. _valueSet[i] = YES;
  10474. }
  10475. }
  10476. }
  10477. }
  10478. return self;
  10479. }
  10480. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10481. #pragma unused(numItems)
  10482. return [self initWithValues:NULL forKeys:NULL count:0];
  10483. }
  10484. #if !defined(NS_BLOCK_ASSERTIONS)
  10485. - (void)dealloc {
  10486. NSAssert(!_autocreator,
  10487. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10488. [self class], _autocreator);
  10489. [super dealloc];
  10490. }
  10491. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10492. - (instancetype)copyWithZone:(NSZone *)zone {
  10493. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10494. }
  10495. - (BOOL)isEqual:(GPBBoolInt64Dictionary *)other {
  10496. if (self == other) {
  10497. return YES;
  10498. }
  10499. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10500. return NO;
  10501. }
  10502. if ((_valueSet[0] != other->_valueSet[0]) ||
  10503. (_valueSet[1] != other->_valueSet[1])) {
  10504. return NO;
  10505. }
  10506. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10507. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10508. return NO;
  10509. }
  10510. return YES;
  10511. }
  10512. - (NSUInteger)hash {
  10513. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10514. }
  10515. - (NSString *)description {
  10516. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10517. if (_valueSet[0]) {
  10518. [result appendFormat:@"NO: %lld", _values[0]];
  10519. }
  10520. if (_valueSet[1]) {
  10521. [result appendFormat:@"YES: %lld", _values[1]];
  10522. }
  10523. [result appendString:@" }"];
  10524. return result;
  10525. }
  10526. - (NSUInteger)count {
  10527. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10528. }
  10529. - (BOOL)valueForKey:(BOOL)key value:(int64_t *)value {
  10530. int idx = (key ? 1 : 0);
  10531. if (_valueSet[idx]) {
  10532. if (value) {
  10533. *value = _values[idx];
  10534. }
  10535. return YES;
  10536. }
  10537. return NO;
  10538. }
  10539. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10540. forGPBGenericValueKey:(GPBGenericValue *)key {
  10541. int idx = (key->valueBool ? 1 : 0);
  10542. _values[idx] = value->valueInt64;
  10543. _valueSet[idx] = YES;
  10544. }
  10545. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10546. if (_valueSet[0]) {
  10547. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10548. }
  10549. if (_valueSet[1]) {
  10550. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10551. }
  10552. }
  10553. - (void)enumerateKeysAndValuesUsingBlock:
  10554. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10555. BOOL stop = NO;
  10556. if (_valueSet[0]) {
  10557. block(NO, _values[0], &stop);
  10558. }
  10559. if (!stop && _valueSet[1]) {
  10560. block(YES, _values[1], &stop);
  10561. }
  10562. }
  10563. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10564. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10565. NSUInteger count = 0;
  10566. size_t result = 0;
  10567. for (int i = 0; i < 2; ++i) {
  10568. if (_valueSet[i]) {
  10569. ++count;
  10570. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10571. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10572. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10573. }
  10574. }
  10575. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10576. result += tagSize * count;
  10577. return result;
  10578. }
  10579. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10580. asField:(GPBFieldDescriptor *)field {
  10581. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10582. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10583. for (int i = 0; i < 2; ++i) {
  10584. if (_valueSet[i]) {
  10585. // Write the tag.
  10586. [outputStream writeInt32NoTag:tag];
  10587. // Write the size of the message.
  10588. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10589. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10590. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10591. // Write the fields.
  10592. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10593. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10594. }
  10595. }
  10596. }
  10597. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10598. if (otherDictionary) {
  10599. for (int i = 0; i < 2; ++i) {
  10600. if (otherDictionary->_valueSet[i]) {
  10601. _valueSet[i] = YES;
  10602. _values[i] = otherDictionary->_values[i];
  10603. }
  10604. }
  10605. if (_autocreator) {
  10606. GPBAutocreatedDictionaryModified(_autocreator, self);
  10607. }
  10608. }
  10609. }
  10610. - (void)setValue:(int64_t)value forKey:(BOOL)key {
  10611. int idx = (key ? 1 : 0);
  10612. _values[idx] = value;
  10613. _valueSet[idx] = YES;
  10614. if (_autocreator) {
  10615. GPBAutocreatedDictionaryModified(_autocreator, self);
  10616. }
  10617. }
  10618. - (void)removeValueForKey:(BOOL)aKey {
  10619. _valueSet[aKey ? 1 : 0] = NO;
  10620. }
  10621. - (void)removeAll {
  10622. _valueSet[0] = NO;
  10623. _valueSet[1] = NO;
  10624. }
  10625. @end
  10626. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10627. // This block of code is generated, do not edit it directly.
  10628. #pragma mark - Bool -> Bool
  10629. @implementation GPBBoolBoolDictionary {
  10630. @package
  10631. BOOL _values[2];
  10632. BOOL _valueSet[2];
  10633. }
  10634. + (instancetype)dictionary {
  10635. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10636. }
  10637. + (instancetype)dictionaryWithValue:(BOOL)value
  10638. forKey:(BOOL)key {
  10639. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10640. // on to get the type correct.
  10641. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:&value
  10642. forKeys:&key
  10643. count:1] autorelease];
  10644. }
  10645. + (instancetype)dictionaryWithValues:(const BOOL [])values
  10646. forKeys:(const BOOL [])keys
  10647. count:(NSUInteger)count {
  10648. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10649. // on to get the type correct.
  10650. return [[(GPBBoolBoolDictionary*)[self alloc] initWithValues:values
  10651. forKeys:keys
  10652. count:count] autorelease];
  10653. }
  10654. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10655. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10656. // on to get the type correct.
  10657. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10658. }
  10659. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10660. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10661. }
  10662. - (instancetype)init {
  10663. return [self initWithValues:NULL forKeys:NULL count:0];
  10664. }
  10665. - (instancetype)initWithValues:(const BOOL [])values
  10666. forKeys:(const BOOL [])keys
  10667. count:(NSUInteger)count {
  10668. self = [super init];
  10669. if (self) {
  10670. for (NSUInteger i = 0; i < count; ++i) {
  10671. int idx = keys[i] ? 1 : 0;
  10672. _values[idx] = values[i];
  10673. _valueSet[idx] = YES;
  10674. }
  10675. }
  10676. return self;
  10677. }
  10678. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10679. self = [self initWithValues:NULL forKeys:NULL count:0];
  10680. if (self) {
  10681. if (dictionary) {
  10682. for (int i = 0; i < 2; ++i) {
  10683. if (dictionary->_valueSet[i]) {
  10684. _values[i] = dictionary->_values[i];
  10685. _valueSet[i] = YES;
  10686. }
  10687. }
  10688. }
  10689. }
  10690. return self;
  10691. }
  10692. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10693. #pragma unused(numItems)
  10694. return [self initWithValues:NULL forKeys:NULL count:0];
  10695. }
  10696. #if !defined(NS_BLOCK_ASSERTIONS)
  10697. - (void)dealloc {
  10698. NSAssert(!_autocreator,
  10699. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10700. [self class], _autocreator);
  10701. [super dealloc];
  10702. }
  10703. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10704. - (instancetype)copyWithZone:(NSZone *)zone {
  10705. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10706. }
  10707. - (BOOL)isEqual:(GPBBoolBoolDictionary *)other {
  10708. if (self == other) {
  10709. return YES;
  10710. }
  10711. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10712. return NO;
  10713. }
  10714. if ((_valueSet[0] != other->_valueSet[0]) ||
  10715. (_valueSet[1] != other->_valueSet[1])) {
  10716. return NO;
  10717. }
  10718. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10719. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10720. return NO;
  10721. }
  10722. return YES;
  10723. }
  10724. - (NSUInteger)hash {
  10725. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10726. }
  10727. - (NSString *)description {
  10728. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10729. if (_valueSet[0]) {
  10730. [result appendFormat:@"NO: %d", _values[0]];
  10731. }
  10732. if (_valueSet[1]) {
  10733. [result appendFormat:@"YES: %d", _values[1]];
  10734. }
  10735. [result appendString:@" }"];
  10736. return result;
  10737. }
  10738. - (NSUInteger)count {
  10739. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10740. }
  10741. - (BOOL)valueForKey:(BOOL)key value:(BOOL *)value {
  10742. int idx = (key ? 1 : 0);
  10743. if (_valueSet[idx]) {
  10744. if (value) {
  10745. *value = _values[idx];
  10746. }
  10747. return YES;
  10748. }
  10749. return NO;
  10750. }
  10751. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10752. forGPBGenericValueKey:(GPBGenericValue *)key {
  10753. int idx = (key->valueBool ? 1 : 0);
  10754. _values[idx] = value->valueBool;
  10755. _valueSet[idx] = YES;
  10756. }
  10757. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10758. if (_valueSet[0]) {
  10759. block(@"false", (_values[0] ? @"true" : @"false"));
  10760. }
  10761. if (_valueSet[1]) {
  10762. block(@"true", (_values[1] ? @"true" : @"false"));
  10763. }
  10764. }
  10765. - (void)enumerateKeysAndValuesUsingBlock:
  10766. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10767. BOOL stop = NO;
  10768. if (_valueSet[0]) {
  10769. block(NO, _values[0], &stop);
  10770. }
  10771. if (!stop && _valueSet[1]) {
  10772. block(YES, _values[1], &stop);
  10773. }
  10774. }
  10775. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10776. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10777. NSUInteger count = 0;
  10778. size_t result = 0;
  10779. for (int i = 0; i < 2; ++i) {
  10780. if (_valueSet[i]) {
  10781. ++count;
  10782. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10783. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10784. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10785. }
  10786. }
  10787. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10788. result += tagSize * count;
  10789. return result;
  10790. }
  10791. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10792. asField:(GPBFieldDescriptor *)field {
  10793. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10794. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10795. for (int i = 0; i < 2; ++i) {
  10796. if (_valueSet[i]) {
  10797. // Write the tag.
  10798. [outputStream writeInt32NoTag:tag];
  10799. // Write the size of the message.
  10800. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10801. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10802. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10803. // Write the fields.
  10804. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10805. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10806. }
  10807. }
  10808. }
  10809. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10810. if (otherDictionary) {
  10811. for (int i = 0; i < 2; ++i) {
  10812. if (otherDictionary->_valueSet[i]) {
  10813. _valueSet[i] = YES;
  10814. _values[i] = otherDictionary->_values[i];
  10815. }
  10816. }
  10817. if (_autocreator) {
  10818. GPBAutocreatedDictionaryModified(_autocreator, self);
  10819. }
  10820. }
  10821. }
  10822. - (void)setValue:(BOOL)value forKey:(BOOL)key {
  10823. int idx = (key ? 1 : 0);
  10824. _values[idx] = value;
  10825. _valueSet[idx] = YES;
  10826. if (_autocreator) {
  10827. GPBAutocreatedDictionaryModified(_autocreator, self);
  10828. }
  10829. }
  10830. - (void)removeValueForKey:(BOOL)aKey {
  10831. _valueSet[aKey ? 1 : 0] = NO;
  10832. }
  10833. - (void)removeAll {
  10834. _valueSet[0] = NO;
  10835. _valueSet[1] = NO;
  10836. }
  10837. @end
  10838. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10839. // This block of code is generated, do not edit it directly.
  10840. #pragma mark - Bool -> Float
  10841. @implementation GPBBoolFloatDictionary {
  10842. @package
  10843. float _values[2];
  10844. BOOL _valueSet[2];
  10845. }
  10846. + (instancetype)dictionary {
  10847. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  10848. }
  10849. + (instancetype)dictionaryWithValue:(float)value
  10850. forKey:(BOOL)key {
  10851. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10852. // on to get the type correct.
  10853. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:&value
  10854. forKeys:&key
  10855. count:1] autorelease];
  10856. }
  10857. + (instancetype)dictionaryWithValues:(const float [])values
  10858. forKeys:(const BOOL [])keys
  10859. count:(NSUInteger)count {
  10860. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  10861. // on to get the type correct.
  10862. return [[(GPBBoolFloatDictionary*)[self alloc] initWithValues:values
  10863. forKeys:keys
  10864. count:count] autorelease];
  10865. }
  10866. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10867. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10868. // on to get the type correct.
  10869. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10870. }
  10871. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10872. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10873. }
  10874. - (instancetype)init {
  10875. return [self initWithValues:NULL forKeys:NULL count:0];
  10876. }
  10877. - (instancetype)initWithValues:(const float [])values
  10878. forKeys:(const BOOL [])keys
  10879. count:(NSUInteger)count {
  10880. self = [super init];
  10881. if (self) {
  10882. for (NSUInteger i = 0; i < count; ++i) {
  10883. int idx = keys[i] ? 1 : 0;
  10884. _values[idx] = values[i];
  10885. _valueSet[idx] = YES;
  10886. }
  10887. }
  10888. return self;
  10889. }
  10890. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10891. self = [self initWithValues:NULL forKeys:NULL count:0];
  10892. if (self) {
  10893. if (dictionary) {
  10894. for (int i = 0; i < 2; ++i) {
  10895. if (dictionary->_valueSet[i]) {
  10896. _values[i] = dictionary->_values[i];
  10897. _valueSet[i] = YES;
  10898. }
  10899. }
  10900. }
  10901. }
  10902. return self;
  10903. }
  10904. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10905. #pragma unused(numItems)
  10906. return [self initWithValues:NULL forKeys:NULL count:0];
  10907. }
  10908. #if !defined(NS_BLOCK_ASSERTIONS)
  10909. - (void)dealloc {
  10910. NSAssert(!_autocreator,
  10911. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10912. [self class], _autocreator);
  10913. [super dealloc];
  10914. }
  10915. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10916. - (instancetype)copyWithZone:(NSZone *)zone {
  10917. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10918. }
  10919. - (BOOL)isEqual:(GPBBoolFloatDictionary *)other {
  10920. if (self == other) {
  10921. return YES;
  10922. }
  10923. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10924. return NO;
  10925. }
  10926. if ((_valueSet[0] != other->_valueSet[0]) ||
  10927. (_valueSet[1] != other->_valueSet[1])) {
  10928. return NO;
  10929. }
  10930. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  10931. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  10932. return NO;
  10933. }
  10934. return YES;
  10935. }
  10936. - (NSUInteger)hash {
  10937. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10938. }
  10939. - (NSString *)description {
  10940. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10941. if (_valueSet[0]) {
  10942. [result appendFormat:@"NO: %f", _values[0]];
  10943. }
  10944. if (_valueSet[1]) {
  10945. [result appendFormat:@"YES: %f", _values[1]];
  10946. }
  10947. [result appendString:@" }"];
  10948. return result;
  10949. }
  10950. - (NSUInteger)count {
  10951. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10952. }
  10953. - (BOOL)valueForKey:(BOOL)key value:(float *)value {
  10954. int idx = (key ? 1 : 0);
  10955. if (_valueSet[idx]) {
  10956. if (value) {
  10957. *value = _values[idx];
  10958. }
  10959. return YES;
  10960. }
  10961. return NO;
  10962. }
  10963. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10964. forGPBGenericValueKey:(GPBGenericValue *)key {
  10965. int idx = (key->valueBool ? 1 : 0);
  10966. _values[idx] = value->valueFloat;
  10967. _valueSet[idx] = YES;
  10968. }
  10969. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10970. if (_valueSet[0]) {
  10971. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  10972. }
  10973. if (_valueSet[1]) {
  10974. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  10975. }
  10976. }
  10977. - (void)enumerateKeysAndValuesUsingBlock:
  10978. (void (^)(BOOL key, float value, BOOL *stop))block {
  10979. BOOL stop = NO;
  10980. if (_valueSet[0]) {
  10981. block(NO, _values[0], &stop);
  10982. }
  10983. if (!stop && _valueSet[1]) {
  10984. block(YES, _values[1], &stop);
  10985. }
  10986. }
  10987. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10988. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10989. NSUInteger count = 0;
  10990. size_t result = 0;
  10991. for (int i = 0; i < 2; ++i) {
  10992. if (_valueSet[i]) {
  10993. ++count;
  10994. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10995. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10996. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10997. }
  10998. }
  10999. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11000. result += tagSize * count;
  11001. return result;
  11002. }
  11003. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11004. asField:(GPBFieldDescriptor *)field {
  11005. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11006. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11007. for (int i = 0; i < 2; ++i) {
  11008. if (_valueSet[i]) {
  11009. // Write the tag.
  11010. [outputStream writeInt32NoTag:tag];
  11011. // Write the size of the message.
  11012. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11013. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11014. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11015. // Write the fields.
  11016. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11017. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11018. }
  11019. }
  11020. }
  11021. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  11022. if (otherDictionary) {
  11023. for (int i = 0; i < 2; ++i) {
  11024. if (otherDictionary->_valueSet[i]) {
  11025. _valueSet[i] = YES;
  11026. _values[i] = otherDictionary->_values[i];
  11027. }
  11028. }
  11029. if (_autocreator) {
  11030. GPBAutocreatedDictionaryModified(_autocreator, self);
  11031. }
  11032. }
  11033. }
  11034. - (void)setValue:(float)value forKey:(BOOL)key {
  11035. int idx = (key ? 1 : 0);
  11036. _values[idx] = value;
  11037. _valueSet[idx] = YES;
  11038. if (_autocreator) {
  11039. GPBAutocreatedDictionaryModified(_autocreator, self);
  11040. }
  11041. }
  11042. - (void)removeValueForKey:(BOOL)aKey {
  11043. _valueSet[aKey ? 1 : 0] = NO;
  11044. }
  11045. - (void)removeAll {
  11046. _valueSet[0] = NO;
  11047. _valueSet[1] = NO;
  11048. }
  11049. @end
  11050. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  11051. // This block of code is generated, do not edit it directly.
  11052. #pragma mark - Bool -> Double
  11053. @implementation GPBBoolDoubleDictionary {
  11054. @package
  11055. double _values[2];
  11056. BOOL _valueSet[2];
  11057. }
  11058. + (instancetype)dictionary {
  11059. return [[[self alloc] initWithValues:NULL forKeys:NULL count:0] autorelease];
  11060. }
  11061. + (instancetype)dictionaryWithValue:(double)value
  11062. forKey:(BOOL)key {
  11063. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11064. // on to get the type correct.
  11065. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:&value
  11066. forKeys:&key
  11067. count:1] autorelease];
  11068. }
  11069. + (instancetype)dictionaryWithValues:(const double [])values
  11070. forKeys:(const BOOL [])keys
  11071. count:(NSUInteger)count {
  11072. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11073. // on to get the type correct.
  11074. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithValues:values
  11075. forKeys:keys
  11076. count:count] autorelease];
  11077. }
  11078. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11079. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11080. // on to get the type correct.
  11081. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11082. }
  11083. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11084. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11085. }
  11086. - (instancetype)init {
  11087. return [self initWithValues:NULL forKeys:NULL count:0];
  11088. }
  11089. - (instancetype)initWithValues:(const double [])values
  11090. forKeys:(const BOOL [])keys
  11091. count:(NSUInteger)count {
  11092. self = [super init];
  11093. if (self) {
  11094. for (NSUInteger i = 0; i < count; ++i) {
  11095. int idx = keys[i] ? 1 : 0;
  11096. _values[idx] = values[i];
  11097. _valueSet[idx] = YES;
  11098. }
  11099. }
  11100. return self;
  11101. }
  11102. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11103. self = [self initWithValues:NULL forKeys:NULL count:0];
  11104. if (self) {
  11105. if (dictionary) {
  11106. for (int i = 0; i < 2; ++i) {
  11107. if (dictionary->_valueSet[i]) {
  11108. _values[i] = dictionary->_values[i];
  11109. _valueSet[i] = YES;
  11110. }
  11111. }
  11112. }
  11113. }
  11114. return self;
  11115. }
  11116. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11117. #pragma unused(numItems)
  11118. return [self initWithValues:NULL forKeys:NULL count:0];
  11119. }
  11120. #if !defined(NS_BLOCK_ASSERTIONS)
  11121. - (void)dealloc {
  11122. NSAssert(!_autocreator,
  11123. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11124. [self class], _autocreator);
  11125. [super dealloc];
  11126. }
  11127. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11128. - (instancetype)copyWithZone:(NSZone *)zone {
  11129. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  11130. }
  11131. - (BOOL)isEqual:(GPBBoolDoubleDictionary *)other {
  11132. if (self == other) {
  11133. return YES;
  11134. }
  11135. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  11136. return NO;
  11137. }
  11138. if ((_valueSet[0] != other->_valueSet[0]) ||
  11139. (_valueSet[1] != other->_valueSet[1])) {
  11140. return NO;
  11141. }
  11142. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11143. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11144. return NO;
  11145. }
  11146. return YES;
  11147. }
  11148. - (NSUInteger)hash {
  11149. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11150. }
  11151. - (NSString *)description {
  11152. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11153. if (_valueSet[0]) {
  11154. [result appendFormat:@"NO: %lf", _values[0]];
  11155. }
  11156. if (_valueSet[1]) {
  11157. [result appendFormat:@"YES: %lf", _values[1]];
  11158. }
  11159. [result appendString:@" }"];
  11160. return result;
  11161. }
  11162. - (NSUInteger)count {
  11163. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11164. }
  11165. - (BOOL)valueForKey:(BOOL)key value:(double *)value {
  11166. int idx = (key ? 1 : 0);
  11167. if (_valueSet[idx]) {
  11168. if (value) {
  11169. *value = _values[idx];
  11170. }
  11171. return YES;
  11172. }
  11173. return NO;
  11174. }
  11175. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11176. forGPBGenericValueKey:(GPBGenericValue *)key {
  11177. int idx = (key->valueBool ? 1 : 0);
  11178. _values[idx] = value->valueDouble;
  11179. _valueSet[idx] = YES;
  11180. }
  11181. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11182. if (_valueSet[0]) {
  11183. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11184. }
  11185. if (_valueSet[1]) {
  11186. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11187. }
  11188. }
  11189. - (void)enumerateKeysAndValuesUsingBlock:
  11190. (void (^)(BOOL key, double value, BOOL *stop))block {
  11191. BOOL stop = NO;
  11192. if (_valueSet[0]) {
  11193. block(NO, _values[0], &stop);
  11194. }
  11195. if (!stop && _valueSet[1]) {
  11196. block(YES, _values[1], &stop);
  11197. }
  11198. }
  11199. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11200. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11201. NSUInteger count = 0;
  11202. size_t result = 0;
  11203. for (int i = 0; i < 2; ++i) {
  11204. if (_valueSet[i]) {
  11205. ++count;
  11206. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11207. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11208. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11209. }
  11210. }
  11211. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11212. result += tagSize * count;
  11213. return result;
  11214. }
  11215. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11216. asField:(GPBFieldDescriptor *)field {
  11217. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11218. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11219. for (int i = 0; i < 2; ++i) {
  11220. if (_valueSet[i]) {
  11221. // Write the tag.
  11222. [outputStream writeInt32NoTag:tag];
  11223. // Write the size of the message.
  11224. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11225. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11226. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11227. // Write the fields.
  11228. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11229. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11230. }
  11231. }
  11232. }
  11233. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11234. if (otherDictionary) {
  11235. for (int i = 0; i < 2; ++i) {
  11236. if (otherDictionary->_valueSet[i]) {
  11237. _valueSet[i] = YES;
  11238. _values[i] = otherDictionary->_values[i];
  11239. }
  11240. }
  11241. if (_autocreator) {
  11242. GPBAutocreatedDictionaryModified(_autocreator, self);
  11243. }
  11244. }
  11245. }
  11246. - (void)setValue:(double)value forKey:(BOOL)key {
  11247. int idx = (key ? 1 : 0);
  11248. _values[idx] = value;
  11249. _valueSet[idx] = YES;
  11250. if (_autocreator) {
  11251. GPBAutocreatedDictionaryModified(_autocreator, self);
  11252. }
  11253. }
  11254. - (void)removeValueForKey:(BOOL)aKey {
  11255. _valueSet[aKey ? 1 : 0] = NO;
  11256. }
  11257. - (void)removeAll {
  11258. _valueSet[0] = NO;
  11259. _valueSet[1] = NO;
  11260. }
  11261. @end
  11262. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11263. // This block of code is generated, do not edit it directly.
  11264. #pragma mark - Bool -> Object
  11265. @implementation GPBBoolObjectDictionary {
  11266. @package
  11267. id _values[2];
  11268. }
  11269. + (instancetype)dictionary {
  11270. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11271. }
  11272. + (instancetype)dictionaryWithObject:(id)object
  11273. forKey:(BOOL)key {
  11274. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11275. // on to get the type correct.
  11276. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11277. forKeys:&key
  11278. count:1] autorelease];
  11279. }
  11280. + (instancetype)dictionaryWithObjects:(const id [])objects
  11281. forKeys:(const BOOL [])keys
  11282. count:(NSUInteger)count {
  11283. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11284. // on to get the type correct.
  11285. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11286. forKeys:keys
  11287. count:count] autorelease];
  11288. }
  11289. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11290. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11291. // on to get the type correct.
  11292. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11293. }
  11294. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11295. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11296. }
  11297. - (instancetype)init {
  11298. return [self initWithObjects:NULL forKeys:NULL count:0];
  11299. }
  11300. - (instancetype)initWithObjects:(const id [])objects
  11301. forKeys:(const BOOL [])keys
  11302. count:(NSUInteger)count {
  11303. self = [super init];
  11304. if (self) {
  11305. for (NSUInteger i = 0; i < count; ++i) {
  11306. if (!objects[i]) {
  11307. [NSException raise:NSInvalidArgumentException
  11308. format:@"Attempting to add nil object to a Dictionary"];
  11309. }
  11310. int idx = keys[i] ? 1 : 0;
  11311. [_values[idx] release];
  11312. _values[idx] = (id)[objects[i] retain];
  11313. }
  11314. }
  11315. return self;
  11316. }
  11317. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11318. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11319. if (self) {
  11320. if (dictionary) {
  11321. _values[0] = [dictionary->_values[0] retain];
  11322. _values[1] = [dictionary->_values[1] retain];
  11323. }
  11324. }
  11325. return self;
  11326. }
  11327. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11328. #pragma unused(numItems)
  11329. return [self initWithObjects:NULL forKeys:NULL count:0];
  11330. }
  11331. - (void)dealloc {
  11332. NSAssert(!_autocreator,
  11333. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11334. [self class], _autocreator);
  11335. [_values[0] release];
  11336. [_values[1] release];
  11337. [super dealloc];
  11338. }
  11339. - (instancetype)copyWithZone:(NSZone *)zone {
  11340. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11341. }
  11342. - (BOOL)isEqual:(GPBBoolObjectDictionary *)other {
  11343. if (self == other) {
  11344. return YES;
  11345. }
  11346. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11347. return NO;
  11348. }
  11349. if (((_values[0] != nil) != (other->_values[0] != nil)) ||
  11350. ((_values[1] != nil) != (other->_values[1] != nil))) {
  11351. return NO;
  11352. }
  11353. if (((_values[0] != nil) && (![_values[0] isEqual:other->_values[0]])) ||
  11354. ((_values[1] != nil) && (![_values[1] isEqual:other->_values[1]]))) {
  11355. return NO;
  11356. }
  11357. return YES;
  11358. }
  11359. - (NSUInteger)hash {
  11360. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11361. }
  11362. - (NSString *)description {
  11363. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11364. if ((_values[0] != nil)) {
  11365. [result appendFormat:@"NO: %@", _values[0]];
  11366. }
  11367. if ((_values[1] != nil)) {
  11368. [result appendFormat:@"YES: %@", _values[1]];
  11369. }
  11370. [result appendString:@" }"];
  11371. return result;
  11372. }
  11373. - (NSUInteger)count {
  11374. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11375. }
  11376. - (id)objectForKey:(BOOL)key {
  11377. return _values[key ? 1 : 0];
  11378. }
  11379. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11380. forGPBGenericValueKey:(GPBGenericValue *)key {
  11381. int idx = (key->valueBool ? 1 : 0);
  11382. [_values[idx] release];
  11383. _values[idx] = [value->valueString retain];
  11384. }
  11385. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11386. if (_values[0] != nil) {
  11387. block(@"false", _values[0]);
  11388. }
  11389. if ((_values[1] != nil)) {
  11390. block(@"true", _values[1]);
  11391. }
  11392. }
  11393. - (void)enumerateKeysAndObjectsUsingBlock:
  11394. (void (^)(BOOL key, id object, BOOL *stop))block {
  11395. BOOL stop = NO;
  11396. if (_values[0] != nil) {
  11397. block(NO, _values[0], &stop);
  11398. }
  11399. if (!stop && (_values[1] != nil)) {
  11400. block(YES, _values[1], &stop);
  11401. }
  11402. }
  11403. - (BOOL)isInitialized {
  11404. if (_values[0] && ![_values[0] isInitialized]) {
  11405. return NO;
  11406. }
  11407. if (_values[1] && ![_values[1] isInitialized]) {
  11408. return NO;
  11409. }
  11410. return YES;
  11411. }
  11412. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11413. GPBBoolObjectDictionary *newDict =
  11414. [[GPBBoolObjectDictionary alloc] init];
  11415. for (int i = 0; i < 2; ++i) {
  11416. if (_values[i] != nil) {
  11417. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11418. }
  11419. }
  11420. return newDict;
  11421. }
  11422. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11423. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11424. NSUInteger count = 0;
  11425. size_t result = 0;
  11426. for (int i = 0; i < 2; ++i) {
  11427. if (_values[i] != nil) {
  11428. ++count;
  11429. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11430. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11431. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11432. }
  11433. }
  11434. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11435. result += tagSize * count;
  11436. return result;
  11437. }
  11438. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11439. asField:(GPBFieldDescriptor *)field {
  11440. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11441. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11442. for (int i = 0; i < 2; ++i) {
  11443. if (_values[i] != nil) {
  11444. // Write the tag.
  11445. [outputStream writeInt32NoTag:tag];
  11446. // Write the size of the message.
  11447. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11448. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11449. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11450. // Write the fields.
  11451. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11452. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11453. }
  11454. }
  11455. }
  11456. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11457. if (otherDictionary) {
  11458. for (int i = 0; i < 2; ++i) {
  11459. if (otherDictionary->_values[i] != nil) {
  11460. [_values[i] release];
  11461. _values[i] = [otherDictionary->_values[i] retain];
  11462. }
  11463. }
  11464. if (_autocreator) {
  11465. GPBAutocreatedDictionaryModified(_autocreator, self);
  11466. }
  11467. }
  11468. }
  11469. - (void)setObject:(id)object forKey:(BOOL)key {
  11470. if (!object) {
  11471. [NSException raise:NSInvalidArgumentException
  11472. format:@"Attempting to add nil object to a Dictionary"];
  11473. }
  11474. int idx = (key ? 1 : 0);
  11475. [_values[idx] release];
  11476. _values[idx] = [object retain];
  11477. if (_autocreator) {
  11478. GPBAutocreatedDictionaryModified(_autocreator, self);
  11479. }
  11480. }
  11481. - (void)removeObjectForKey:(BOOL)aKey {
  11482. int idx = (aKey ? 1 : 0);
  11483. [_values[idx] release];
  11484. _values[idx] = nil;
  11485. }
  11486. - (void)removeAll {
  11487. for (int i = 0; i < 2; ++i) {
  11488. [_values[i] release];
  11489. _values[i] = nil;
  11490. }
  11491. }
  11492. @end
  11493. //%PDDM-EXPAND-END (8 expansions)
  11494. #pragma mark - Bool -> Enum
  11495. @implementation GPBBoolEnumDictionary {
  11496. @package
  11497. GPBEnumValidationFunc _validationFunc;
  11498. int32_t _values[2];
  11499. BOOL _valueSet[2];
  11500. }
  11501. @synthesize validationFunc = _validationFunc;
  11502. + (instancetype)dictionary {
  11503. return [[[self alloc] initWithValidationFunction:NULL
  11504. rawValues:NULL
  11505. forKeys:NULL
  11506. count:0] autorelease];
  11507. }
  11508. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11509. return [[[self alloc] initWithValidationFunction:func
  11510. rawValues:NULL
  11511. forKeys:NULL
  11512. count:0] autorelease];
  11513. }
  11514. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11515. rawValue:(int32_t)rawValue
  11516. forKey:(BOOL)key {
  11517. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11518. // on to get the type correct.
  11519. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11520. rawValues:&rawValue
  11521. forKeys:&key
  11522. count:1] autorelease];
  11523. }
  11524. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11525. rawValues:(const int32_t [])values
  11526. forKeys:(const BOOL [])keys
  11527. count:(NSUInteger)count {
  11528. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11529. // on to get the type correct.
  11530. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11531. rawValues:values
  11532. forKeys:keys
  11533. count:count] autorelease];
  11534. }
  11535. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11536. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11537. // on to get the type correct.
  11538. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11539. }
  11540. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11541. capacity:(NSUInteger)numItems {
  11542. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11543. }
  11544. - (instancetype)init {
  11545. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11546. }
  11547. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11548. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11549. }
  11550. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11551. rawValues:(const int32_t [])rawValues
  11552. forKeys:(const BOOL [])keys
  11553. count:(NSUInteger)count {
  11554. self = [super init];
  11555. if (self) {
  11556. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11557. for (NSUInteger i = 0; i < count; ++i) {
  11558. int idx = keys[i] ? 1 : 0;
  11559. _values[idx] = rawValues[i];
  11560. _valueSet[idx] = YES;
  11561. }
  11562. }
  11563. return self;
  11564. }
  11565. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11566. self = [self initWithValidationFunction:dictionary.validationFunc
  11567. rawValues:NULL
  11568. forKeys:NULL
  11569. count:0];
  11570. if (self) {
  11571. if (dictionary) {
  11572. for (int i = 0; i < 2; ++i) {
  11573. if (dictionary->_valueSet[i]) {
  11574. _values[i] = dictionary->_values[i];
  11575. _valueSet[i] = YES;
  11576. }
  11577. }
  11578. }
  11579. }
  11580. return self;
  11581. }
  11582. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11583. capacity:(NSUInteger)numItems {
  11584. #pragma unused(numItems)
  11585. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11586. }
  11587. #if !defined(NS_BLOCK_ASSERTIONS)
  11588. - (void)dealloc {
  11589. NSAssert(!_autocreator,
  11590. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11591. [self class], _autocreator);
  11592. [super dealloc];
  11593. }
  11594. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11595. - (instancetype)copyWithZone:(NSZone *)zone {
  11596. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11597. }
  11598. - (BOOL)isEqual:(GPBBoolEnumDictionary *)other {
  11599. if (self == other) {
  11600. return YES;
  11601. }
  11602. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11603. return NO;
  11604. }
  11605. if ((_valueSet[0] != other->_valueSet[0]) ||
  11606. (_valueSet[1] != other->_valueSet[1])) {
  11607. return NO;
  11608. }
  11609. if ((_valueSet[0] && (_values[0] != other->_values[0])) ||
  11610. (_valueSet[1] && (_values[1] != other->_values[1]))) {
  11611. return NO;
  11612. }
  11613. return YES;
  11614. }
  11615. - (NSUInteger)hash {
  11616. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11617. }
  11618. - (NSString *)description {
  11619. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11620. if (_valueSet[0]) {
  11621. [result appendFormat:@"NO: %d", _values[0]];
  11622. }
  11623. if (_valueSet[1]) {
  11624. [result appendFormat:@"YES: %d", _values[1]];
  11625. }
  11626. [result appendString:@" }"];
  11627. return result;
  11628. }
  11629. - (NSUInteger)count {
  11630. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11631. }
  11632. - (BOOL)valueForKey:(BOOL)key value:(int32_t*)value {
  11633. int idx = (key ? 1 : 0);
  11634. if (_valueSet[idx]) {
  11635. if (value) {
  11636. int32_t result = _values[idx];
  11637. if (!_validationFunc(result)) {
  11638. result = kGPBUnrecognizedEnumeratorValue;
  11639. }
  11640. *value = result;
  11641. }
  11642. return YES;
  11643. }
  11644. return NO;
  11645. }
  11646. - (BOOL)valueForKey:(BOOL)key rawValue:(int32_t*)rawValue {
  11647. int idx = (key ? 1 : 0);
  11648. if (_valueSet[idx]) {
  11649. if (rawValue) {
  11650. *rawValue = _values[idx];
  11651. }
  11652. return YES;
  11653. }
  11654. return NO;
  11655. }
  11656. - (void)enumerateKeysAndValuesUsingBlock:
  11657. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11658. BOOL stop = NO;
  11659. if (_valueSet[0]) {
  11660. block(NO, _values[0], &stop);
  11661. }
  11662. if (!stop && _valueSet[1]) {
  11663. block(YES, _values[1], &stop);
  11664. }
  11665. }
  11666. - (void)enumerateKeysAndRawValuesUsingBlock:
  11667. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11668. BOOL stop = NO;
  11669. GPBEnumValidationFunc func = _validationFunc;
  11670. int32_t validatedValue;
  11671. if (_valueSet[0]) {
  11672. validatedValue = _values[0];
  11673. if (!func(validatedValue)) {
  11674. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11675. }
  11676. block(NO, validatedValue, &stop);
  11677. }
  11678. if (!stop && _valueSet[1]) {
  11679. validatedValue = _values[1];
  11680. if (!func(validatedValue)) {
  11681. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11682. }
  11683. block(YES, validatedValue, &stop);
  11684. }
  11685. }
  11686. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11687. // This block of code is generated, do not edit it directly.
  11688. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11689. forKey:(GPBGenericValue *)key
  11690. keyDataType:(GPBDataType)keyDataType {
  11691. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11692. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11693. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11694. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11695. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11696. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11697. [outputStream release];
  11698. return data;
  11699. }
  11700. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11701. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11702. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11703. NSUInteger count = 0;
  11704. size_t result = 0;
  11705. for (int i = 0; i < 2; ++i) {
  11706. if (_valueSet[i]) {
  11707. ++count;
  11708. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11709. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11710. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11711. }
  11712. }
  11713. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11714. result += tagSize * count;
  11715. return result;
  11716. }
  11717. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11718. asField:(GPBFieldDescriptor *)field {
  11719. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11720. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11721. for (int i = 0; i < 2; ++i) {
  11722. if (_valueSet[i]) {
  11723. // Write the tag.
  11724. [outputStream writeInt32NoTag:tag];
  11725. // Write the size of the message.
  11726. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11727. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11728. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11729. // Write the fields.
  11730. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11731. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11732. }
  11733. }
  11734. }
  11735. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11736. if (_valueSet[0]) {
  11737. block(@"false", @(_values[0]));
  11738. }
  11739. if (_valueSet[1]) {
  11740. block(@"true", @(_values[1]));
  11741. }
  11742. }
  11743. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11744. forGPBGenericValueKey:(GPBGenericValue *)key {
  11745. int idx = (key->valueBool ? 1 : 0);
  11746. _values[idx] = value->valueInt32;
  11747. _valueSet[idx] = YES;
  11748. }
  11749. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11750. if (otherDictionary) {
  11751. for (int i = 0; i < 2; ++i) {
  11752. if (otherDictionary->_valueSet[i]) {
  11753. _valueSet[i] = YES;
  11754. _values[i] = otherDictionary->_values[i];
  11755. }
  11756. }
  11757. if (_autocreator) {
  11758. GPBAutocreatedDictionaryModified(_autocreator, self);
  11759. }
  11760. }
  11761. }
  11762. - (void)setValue:(int32_t)value forKey:(BOOL)key {
  11763. if (!_validationFunc(value)) {
  11764. [NSException raise:NSInvalidArgumentException
  11765. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11766. value];
  11767. }
  11768. int idx = (key ? 1 : 0);
  11769. _values[idx] = value;
  11770. _valueSet[idx] = YES;
  11771. if (_autocreator) {
  11772. GPBAutocreatedDictionaryModified(_autocreator, self);
  11773. }
  11774. }
  11775. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11776. int idx = (key ? 1 : 0);
  11777. _values[idx] = rawValue;
  11778. _valueSet[idx] = YES;
  11779. if (_autocreator) {
  11780. GPBAutocreatedDictionaryModified(_autocreator, self);
  11781. }
  11782. }
  11783. - (void)removeValueForKey:(BOOL)aKey {
  11784. _valueSet[aKey ? 1 : 0] = NO;
  11785. }
  11786. - (void)removeAll {
  11787. _valueSet[0] = NO;
  11788. _valueSet[1] = NO;
  11789. }
  11790. @end
  11791. #pragma mark - NSDictionary Subclass
  11792. @implementation GPBAutocreatedDictionary {
  11793. NSMutableDictionary *_dictionary;
  11794. }
  11795. - (void)dealloc {
  11796. NSAssert(!_autocreator,
  11797. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11798. [self class], _autocreator);
  11799. [_dictionary release];
  11800. [super dealloc];
  11801. }
  11802. #pragma mark Required NSDictionary overrides
  11803. - (instancetype)initWithObjects:(const id [])objects
  11804. forKeys:(const id<NSCopying> [])keys
  11805. count:(NSUInteger)count {
  11806. self = [super init];
  11807. if (self) {
  11808. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11809. forKeys:keys
  11810. count:count];
  11811. }
  11812. return self;
  11813. }
  11814. - (NSUInteger)count {
  11815. return [_dictionary count];
  11816. }
  11817. - (id)objectForKey:(id)aKey {
  11818. return [_dictionary objectForKey:aKey];
  11819. }
  11820. - (NSEnumerator *)keyEnumerator {
  11821. if (_dictionary == nil) {
  11822. _dictionary = [[NSMutableDictionary alloc] init];
  11823. }
  11824. return [_dictionary keyEnumerator];
  11825. }
  11826. #pragma mark Required NSMutableDictionary overrides
  11827. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11828. // since we only autocreate empty dictionaries.
  11829. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11830. if (_dictionary == nil) {
  11831. _dictionary = [[NSMutableDictionary alloc] init];
  11832. }
  11833. [_dictionary setObject:anObject forKey:aKey];
  11834. if (_autocreator) {
  11835. GPBAutocreatedDictionaryModified(_autocreator, self);
  11836. }
  11837. }
  11838. - (void)removeObjectForKey:(id)aKey {
  11839. [_dictionary removeObjectForKey:aKey];
  11840. }
  11841. #pragma mark Extra things hooked
  11842. - (id)copyWithZone:(NSZone *)zone {
  11843. if (_dictionary == nil) {
  11844. _dictionary = [[NSMutableDictionary alloc] init];
  11845. }
  11846. return [_dictionary copyWithZone:zone];
  11847. }
  11848. - (id)mutableCopyWithZone:(NSZone *)zone {
  11849. if (_dictionary == nil) {
  11850. _dictionary = [[NSMutableDictionary alloc] init];
  11851. }
  11852. return [_dictionary mutableCopyWithZone:zone];
  11853. }
  11854. - (id)objectForKeyedSubscript:(id)key {
  11855. return [_dictionary objectForKeyedSubscript:key];
  11856. }
  11857. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11858. if (_dictionary == nil) {
  11859. _dictionary = [[NSMutableDictionary alloc] init];
  11860. }
  11861. [_dictionary setObject:obj forKeyedSubscript:key];
  11862. if (_autocreator) {
  11863. GPBAutocreatedDictionaryModified(_autocreator, self);
  11864. }
  11865. }
  11866. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11867. id obj,
  11868. BOOL *stop))block {
  11869. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11870. }
  11871. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11872. usingBlock:(void (^)(id key,
  11873. id obj,
  11874. BOOL *stop))block {
  11875. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11876. }
  11877. @end