GPBDictionary.m 467 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBDictionary_PackagePrivate.h"
  31. #import "GPBCodedInputStream_PackagePrivate.h"
  32. #import "GPBCodedOutputStream_PackagePrivate.h"
  33. #import "GPBDescriptor_PackagePrivate.h"
  34. #import "GPBMessage_PackagePrivate.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. // ------------------------------ NOTE ------------------------------
  37. // At the moment, this is all using NSNumbers in NSDictionaries under
  38. // the hood, but it is all hidden so we can come back and optimize
  39. // with direct CFDictionary usage later. The reason that wasn't
  40. // done yet is needing to support 32bit iOS builds. Otherwise
  41. // it would be pretty simple to store all this data in CFDictionaries
  42. // directly.
  43. // ------------------------------------------------------------------
  44. // Direct access is use for speed, to avoid even internally declaring things
  45. // read/write, etc. The warning is enabled in the project to ensure code calling
  46. // protos can turn on -Wdirect-ivar-access without issues.
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  49. // Used to include code only visible to specific versions of the static
  50. // analyzer. Useful for wrapping code that only exists to silence the analyzer.
  51. // Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
  52. // END_APPLE_BUILD_VERSION using:
  53. // xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
  54. // Example usage:
  55. // #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
  56. #define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
  57. (defined(__clang_analyzer__) && \
  58. (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
  59. __apple_build_version__ <= END_APPLE_BUILD_VERSION))
  60. enum {
  61. kMapKeyFieldNumber = 1,
  62. kMapValueFieldNumber = 2,
  63. };
  64. static BOOL DictDefault_IsValidValue(int32_t value) {
  65. // Anything but the bad value marker is allowed.
  66. return (value != kGPBUnrecognizedEnumeratorValue);
  67. }
  68. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  69. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  70. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  71. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  72. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  73. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  74. //% } else {
  75. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  76. //% return 0;
  77. //% }
  78. //%}
  79. //%
  80. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  81. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  82. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  83. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  84. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  85. //% } else {
  86. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  87. //% }
  88. //%}
  89. //%
  90. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  91. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  92. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  93. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  94. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  95. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  96. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  97. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  98. //% } else {
  99. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  100. //% return 0;
  101. //% }
  102. //%}
  103. //%
  104. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  105. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  106. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  107. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  108. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  109. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  110. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  111. //% } else {
  112. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  113. //% }
  114. //%}
  115. //%
  116. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  117. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  118. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  119. //% #pragma unused(dataType) // For when asserts are off in release.
  120. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  121. //%}
  122. //%
  123. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  124. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  125. //% #pragma unused(dataType) // For when asserts are off in release.
  126. //% [stream write##VALUE_NAME##:fieldNum value:value];
  127. //%}
  128. //%
  129. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  130. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  131. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  132. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  133. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  134. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  135. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  136. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  137. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  138. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  139. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  140. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  141. // This block of code is generated, do not edit it directly.
  142. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  143. if (dataType == GPBDataTypeInt32) {
  144. return GPBComputeInt32Size(fieldNum, value);
  145. } else if (dataType == GPBDataTypeSInt32) {
  146. return GPBComputeSInt32Size(fieldNum, value);
  147. } else if (dataType == GPBDataTypeSFixed32) {
  148. return GPBComputeSFixed32Size(fieldNum, value);
  149. } else {
  150. NSCAssert(NO, @"Unexpected type %d", dataType);
  151. return 0;
  152. }
  153. }
  154. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  155. if (dataType == GPBDataTypeInt32) {
  156. [stream writeInt32:fieldNum value:value];
  157. } else if (dataType == GPBDataTypeSInt32) {
  158. [stream writeSInt32:fieldNum value:value];
  159. } else if (dataType == GPBDataTypeSFixed32) {
  160. [stream writeSFixed32:fieldNum value:value];
  161. } else {
  162. NSCAssert(NO, @"Unexpected type %d", dataType);
  163. }
  164. }
  165. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  166. if (dataType == GPBDataTypeUInt32) {
  167. return GPBComputeUInt32Size(fieldNum, value);
  168. } else if (dataType == GPBDataTypeFixed32) {
  169. return GPBComputeFixed32Size(fieldNum, value);
  170. } else {
  171. NSCAssert(NO, @"Unexpected type %d", dataType);
  172. return 0;
  173. }
  174. }
  175. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  176. if (dataType == GPBDataTypeUInt32) {
  177. [stream writeUInt32:fieldNum value:value];
  178. } else if (dataType == GPBDataTypeFixed32) {
  179. [stream writeFixed32:fieldNum value:value];
  180. } else {
  181. NSCAssert(NO, @"Unexpected type %d", dataType);
  182. }
  183. }
  184. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  185. if (dataType == GPBDataTypeInt64) {
  186. return GPBComputeInt64Size(fieldNum, value);
  187. } else if (dataType == GPBDataTypeSInt64) {
  188. return GPBComputeSInt64Size(fieldNum, value);
  189. } else if (dataType == GPBDataTypeSFixed64) {
  190. return GPBComputeSFixed64Size(fieldNum, value);
  191. } else {
  192. NSCAssert(NO, @"Unexpected type %d", dataType);
  193. return 0;
  194. }
  195. }
  196. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  197. if (dataType == GPBDataTypeInt64) {
  198. [stream writeInt64:fieldNum value:value];
  199. } else if (dataType == GPBDataTypeSInt64) {
  200. [stream writeSInt64:fieldNum value:value];
  201. } else if (dataType == GPBDataTypeSFixed64) {
  202. [stream writeSFixed64:fieldNum value:value];
  203. } else {
  204. NSCAssert(NO, @"Unexpected type %d", dataType);
  205. }
  206. }
  207. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  208. if (dataType == GPBDataTypeUInt64) {
  209. return GPBComputeUInt64Size(fieldNum, value);
  210. } else if (dataType == GPBDataTypeFixed64) {
  211. return GPBComputeFixed64Size(fieldNum, value);
  212. } else {
  213. NSCAssert(NO, @"Unexpected type %d", dataType);
  214. return 0;
  215. }
  216. }
  217. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  218. if (dataType == GPBDataTypeUInt64) {
  219. [stream writeUInt64:fieldNum value:value];
  220. } else if (dataType == GPBDataTypeFixed64) {
  221. [stream writeFixed64:fieldNum value:value];
  222. } else {
  223. NSCAssert(NO, @"Unexpected type %d", dataType);
  224. }
  225. }
  226. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  227. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  228. #pragma unused(dataType) // For when asserts are off in release.
  229. return GPBComputeBoolSize(fieldNum, value);
  230. }
  231. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  232. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  233. #pragma unused(dataType) // For when asserts are off in release.
  234. [stream writeBool:fieldNum value:value];
  235. }
  236. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  237. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  238. #pragma unused(dataType) // For when asserts are off in release.
  239. return GPBComputeEnumSize(fieldNum, value);
  240. }
  241. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  242. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  243. #pragma unused(dataType) // For when asserts are off in release.
  244. [stream writeEnum:fieldNum value:value];
  245. }
  246. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  247. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  248. #pragma unused(dataType) // For when asserts are off in release.
  249. return GPBComputeFloatSize(fieldNum, value);
  250. }
  251. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
  252. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  253. #pragma unused(dataType) // For when asserts are off in release.
  254. [stream writeFloat:fieldNum value:value];
  255. }
  256. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  257. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  258. #pragma unused(dataType) // For when asserts are off in release.
  259. return GPBComputeDoubleSize(fieldNum, value);
  260. }
  261. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
  262. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  263. #pragma unused(dataType) // For when asserts are off in release.
  264. [stream writeDouble:fieldNum value:value];
  265. }
  266. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  267. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  268. #pragma unused(dataType) // For when asserts are off in release.
  269. return GPBComputeStringSize(fieldNum, value);
  270. }
  271. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  272. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  273. #pragma unused(dataType) // For when asserts are off in release.
  274. [stream writeString:fieldNum value:value];
  275. }
  276. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  277. if (dataType == GPBDataTypeMessage) {
  278. return GPBComputeMessageSize(fieldNum, value);
  279. } else if (dataType == GPBDataTypeString) {
  280. return GPBComputeStringSize(fieldNum, value);
  281. } else if (dataType == GPBDataTypeBytes) {
  282. return GPBComputeBytesSize(fieldNum, value);
  283. } else {
  284. NSCAssert(NO, @"Unexpected type %d", dataType);
  285. return 0;
  286. }
  287. }
  288. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  289. if (dataType == GPBDataTypeMessage) {
  290. [stream writeMessage:fieldNum value:value];
  291. } else if (dataType == GPBDataTypeString) {
  292. [stream writeString:fieldNum value:value];
  293. } else if (dataType == GPBDataTypeBytes) {
  294. [stream writeBytes:fieldNum value:value];
  295. } else {
  296. NSCAssert(NO, @"Unexpected type %d", dataType);
  297. }
  298. }
  299. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  300. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  301. GPBDataType mapValueType = GPBGetFieldDataType(field);
  302. __block size_t result = 0;
  303. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  304. #pragma unused(stop)
  305. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  306. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  307. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  308. }];
  309. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  310. result += tagSize * dict.count;
  311. return result;
  312. }
  313. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  314. NSDictionary *dict,
  315. GPBFieldDescriptor *field) {
  316. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  317. GPBDataType mapValueType = GPBGetFieldDataType(field);
  318. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  319. [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
  320. #pragma unused(stop)
  321. // Write the tag.
  322. [outputStream writeInt32NoTag:tag];
  323. // Write the size of the message.
  324. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  325. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  326. // Write the size and fields.
  327. [outputStream writeInt32NoTag:(int32_t)msgSize];
  328. [outputStream writeString:kMapKeyFieldNumber value:key];
  329. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  330. }];
  331. }
  332. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  333. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  334. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  335. #pragma unused(field) // For when asserts are off in release.
  336. for (GPBMessage *msg in [dict objectEnumerator]) {
  337. if (!msg.initialized) {
  338. return NO;
  339. }
  340. }
  341. return YES;
  342. }
  343. // Note: if the type is an object, it the retain pass back to the caller.
  344. static void ReadValue(GPBCodedInputStream *stream,
  345. GPBGenericValue *valueToFill,
  346. GPBDataType type,
  347. GPBExtensionRegistry *registry,
  348. GPBFieldDescriptor *field) {
  349. switch (type) {
  350. case GPBDataTypeBool:
  351. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  352. break;
  353. case GPBDataTypeFixed32:
  354. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  355. break;
  356. case GPBDataTypeSFixed32:
  357. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  358. break;
  359. case GPBDataTypeFloat:
  360. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  361. break;
  362. case GPBDataTypeFixed64:
  363. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  364. break;
  365. case GPBDataTypeSFixed64:
  366. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  367. break;
  368. case GPBDataTypeDouble:
  369. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  370. break;
  371. case GPBDataTypeInt32:
  372. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  373. break;
  374. case GPBDataTypeInt64:
  375. valueToFill->valueInt64 = GPBCodedInputStreamReadInt32(&stream->state_);
  376. break;
  377. case GPBDataTypeSInt32:
  378. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  379. break;
  380. case GPBDataTypeSInt64:
  381. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  382. break;
  383. case GPBDataTypeUInt32:
  384. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  385. break;
  386. case GPBDataTypeUInt64:
  387. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  388. break;
  389. case GPBDataTypeBytes:
  390. [valueToFill->valueData release];
  391. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  392. break;
  393. case GPBDataTypeString:
  394. [valueToFill->valueString release];
  395. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  396. break;
  397. case GPBDataTypeMessage: {
  398. GPBMessage *message = [[field.msgClass alloc] init];
  399. [stream readMessage:message extensionRegistry:registry];
  400. [valueToFill->valueMessage release];
  401. valueToFill->valueMessage = message;
  402. break;
  403. }
  404. case GPBDataTypeGroup:
  405. NSCAssert(NO, @"Can't happen");
  406. break;
  407. case GPBDataTypeEnum:
  408. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  409. break;
  410. }
  411. }
  412. void GPBDictionaryReadEntry(id mapDictionary,
  413. GPBCodedInputStream *stream,
  414. GPBExtensionRegistry *registry,
  415. GPBFieldDescriptor *field,
  416. GPBMessage *parentMessage) {
  417. GPBDataType keyDataType = field.mapKeyDataType;
  418. GPBDataType valueDataType = GPBGetFieldDataType(field);
  419. GPBGenericValue key;
  420. GPBGenericValue value;
  421. // Zero them (but pick up any enum default for proto2).
  422. key.valueString = value.valueString = nil;
  423. if (valueDataType == GPBDataTypeEnum) {
  424. value = field.defaultValue;
  425. }
  426. GPBCodedInputStreamState *state = &stream->state_;
  427. uint32_t keyTag =
  428. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  429. uint32_t valueTag =
  430. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  431. BOOL hitError = NO;
  432. while (YES) {
  433. uint32_t tag = GPBCodedInputStreamReadTag(state);
  434. if (tag == keyTag) {
  435. ReadValue(stream, &key, keyDataType, registry, field);
  436. } else if (tag == valueTag) {
  437. ReadValue(stream, &value, valueDataType, registry, field);
  438. } else if (tag == 0) {
  439. // zero signals EOF / limit reached
  440. break;
  441. } else { // Unknown
  442. if (![stream skipField:tag]){
  443. hitError = YES;
  444. break;
  445. }
  446. }
  447. }
  448. if (!hitError) {
  449. // Handle the special defaults and/or missing key/value.
  450. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  451. key.valueString = [@"" retain];
  452. }
  453. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  454. #pragma clang diagnostic push
  455. #pragma clang diagnostic ignored "-Wswitch-enum"
  456. switch (valueDataType) {
  457. case GPBDataTypeString:
  458. value.valueString = [@"" retain];
  459. break;
  460. case GPBDataTypeBytes:
  461. value.valueData = [GPBEmptyNSData() retain];
  462. break;
  463. #if defined(__clang_analyzer__)
  464. case GPBDataTypeGroup:
  465. // Maps can't really have Groups as the value type, but this case is needed
  466. // so the analyzer won't report the posibility of send nil in for the value
  467. // in the NSMutableDictionary case below.
  468. #endif
  469. case GPBDataTypeMessage: {
  470. value.valueMessage = [[field.msgClass alloc] init];
  471. break;
  472. }
  473. default:
  474. // Nothing
  475. break;
  476. }
  477. #pragma clang diagnostic pop
  478. }
  479. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  480. #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
  481. // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
  482. // be raised as needed for new Xcodes.
  483. //
  484. // This is only needed on a "shallow" analyze; on a "deep" analyze, the
  485. // existing code path gets this correct. In shallow, the analyzer decides
  486. // GPBDataTypeIsObject(valueDataType) is both false and true on a single
  487. // path through this function, allowing nil to be used for the
  488. // setObject:forKey:.
  489. if (value.valueString == nil) {
  490. value.valueString = [@"" retain];
  491. }
  492. #endif
  493. // mapDictionary is an NSMutableDictionary
  494. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  495. forKey:key.valueString];
  496. } else {
  497. if (valueDataType == GPBDataTypeEnum) {
  498. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  499. [field isValidEnumValue:value.valueEnum]) {
  500. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  501. } else {
  502. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  503. forKey:&key
  504. keyDataType:keyDataType];
  505. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  506. }
  507. } else {
  508. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  509. }
  510. }
  511. }
  512. if (GPBDataTypeIsObject(keyDataType)) {
  513. [key.valueString release];
  514. }
  515. if (GPBDataTypeIsObject(valueDataType)) {
  516. [value.valueString release];
  517. }
  518. }
  519. //
  520. // Macros for the common basic cases.
  521. //
  522. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  523. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  524. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  525. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  526. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  527. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  528. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  529. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  530. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  531. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  532. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  533. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  534. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  535. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
  536. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  537. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
  538. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
  539. //%#pragma mark - KEY_NAME -> VALUE_NAME
  540. //%
  541. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  542. //% @package
  543. //% NSMutableDictionary *_dictionary;
  544. //%}
  545. //%
  546. //%+ (instancetype)dictionary {
  547. //% return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
  548. //%}
  549. //%
  550. //%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)##VNAME_VAR
  551. //% ##VNAME$S## forKey:(KEY_TYPE##KisP$S##KisP)key {
  552. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  553. //% // on to get the type correct.
  554. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
  555. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## forKeys:&key
  556. //% KEY_NAME$S VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  557. //%}
  558. //%
  559. //%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  560. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  561. //% ##VNAME$S## count:(NSUInteger)count {
  562. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  563. //% // on to get the type correct.
  564. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
  565. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  566. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  567. //%}
  568. //%
  569. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  570. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  571. //% // on to get the type correct.
  572. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  573. //%}
  574. //%
  575. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  576. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  577. //%}
  578. //%
  579. //%- (instancetype)init {
  580. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  581. //%}
  582. //%
  583. //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  584. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  585. //% ##VNAME$S## count:(NSUInteger)count {
  586. //% self = [super init];
  587. //% if (self) {
  588. //% _dictionary = [[NSMutableDictionary alloc] init];
  589. //% if (count && VNAME_VAR##s && keys) {
  590. //% for (NSUInteger i = 0; i < count; ++i) {
  591. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
  592. //% }
  593. //% }
  594. //% }
  595. //% return self;
  596. //%}
  597. //%
  598. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  599. //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  600. //% if (self) {
  601. //% if (dictionary) {
  602. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  603. //% }
  604. //% }
  605. //% return self;
  606. //%}
  607. //%
  608. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  609. //% #pragma unused(numItems)
  610. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  611. //%}
  612. //%
  613. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  614. //%
  615. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  616. //%
  617. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  618. //%
  619. //%@end
  620. //%
  621. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  622. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  623. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  624. //%#pragma mark - KEY_NAME -> VALUE_NAME
  625. //%
  626. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  627. //% @package
  628. //% NSMutableDictionary *_dictionary;
  629. //% GPBEnumValidationFunc _validationFunc;
  630. //%}
  631. //%
  632. //%@synthesize validationFunc = _validationFunc;
  633. //%
  634. //%+ (instancetype)dictionary {
  635. //% return [[[self alloc] initWithValidationFunction:NULL
  636. //% rawValues:NULL
  637. //% forKeys:NULL
  638. //% count:0] autorelease];
  639. //%}
  640. //%
  641. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  642. //% return [[[self alloc] initWithValidationFunction:func
  643. //% rawValues:NULL
  644. //% forKeys:NULL
  645. //% count:0] autorelease];
  646. //%}
  647. //%
  648. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  649. //% rawValue:(VALUE_TYPE)rawValue
  650. //% forKey:(KEY_TYPE##KisP$S##KisP)key {
  651. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  652. //% // on to get the type correct.
  653. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  654. //% KEY_NAME$S VALUE_NAME$S rawValues:&rawValue
  655. //% KEY_NAME$S VALUE_NAME$S forKeys:&key
  656. //% KEY_NAME$S VALUE_NAME$S count:1] autorelease];
  657. //%}
  658. //%
  659. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  660. //% rawValues:(const VALUE_TYPE [])rawValues
  661. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  662. //% count:(NSUInteger)count {
  663. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  664. //% // on to get the type correct.
  665. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithValidationFunction:func
  666. //% KEY_NAME$S VALUE_NAME$S rawValues:rawValues
  667. //% KEY_NAME$S VALUE_NAME$S forKeys:keys
  668. //% KEY_NAME$S VALUE_NAME$S count:count] autorelease];
  669. //%}
  670. //%
  671. //%+ (instancetype)dictionaryWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  672. //% // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  673. //% // on to get the type correct.
  674. //% return [[(GPB##KEY_NAME##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  675. //%}
  676. //%
  677. //%+ (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  678. //% capacity:(NSUInteger)numItems {
  679. //% return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  680. //%}
  681. //%
  682. //%- (instancetype)init {
  683. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  684. //%}
  685. //%
  686. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  687. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  688. //%}
  689. //%
  690. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  691. //% rawValues:(const VALUE_TYPE [])rawValues
  692. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  693. //% count:(NSUInteger)count {
  694. //% self = [super init];
  695. //% if (self) {
  696. //% _dictionary = [[NSMutableDictionary alloc] init];
  697. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  698. //% if (count && rawValues && keys) {
  699. //% for (NSUInteger i = 0; i < count; ++i) {
  700. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  701. //% }
  702. //% }
  703. //% }
  704. //% return self;
  705. //%}
  706. //%
  707. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  708. //% self = [self initWithValidationFunction:dictionary.validationFunc
  709. //% rawValues:NULL
  710. //% forKeys:NULL
  711. //% count:0];
  712. //% if (self) {
  713. //% if (dictionary) {
  714. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  715. //% }
  716. //% }
  717. //% return self;
  718. //%}
  719. //%
  720. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  721. //% capacity:(NSUInteger)numItems {
  722. //% #pragma unused(numItems)
  723. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  724. //%}
  725. //%
  726. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
  727. //%
  728. //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  729. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  730. //% if (wrapped && value) {
  731. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  732. //% if (!_validationFunc(result)) {
  733. //% result = kGPBUnrecognizedEnumeratorValue;
  734. //% }
  735. //% *value = result;
  736. //% }
  737. //% return (wrapped != NULL);
  738. //%}
  739. //%
  740. //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
  741. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  742. //% if (wrapped && rawValue) {
  743. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  744. //% }
  745. //% return (wrapped != NULL);
  746. //%}
  747. //%
  748. //%- (void)enumerateKeysAndEnumsUsingBlock:
  749. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  750. //% GPBEnumValidationFunc func = _validationFunc;
  751. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  752. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue,
  753. //% BOOL *stop) {
  754. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  755. //% if (!func(unwrapped)) {
  756. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  757. //% }
  758. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, stop);
  759. //% }];
  760. //%}
  761. //%
  762. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
  763. //%
  764. //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  765. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  766. //% [NSException raise:NSInvalidArgumentException
  767. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  768. //% value];
  769. //% }
  770. //%
  771. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  772. //% if (_autocreator) {
  773. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  774. //% }
  775. //%}
  776. //%
  777. //%@end
  778. //%
  779. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  780. //%- (void)dealloc {
  781. //% NSAssert(!_autocreator,
  782. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  783. //% [self class], _autocreator);
  784. //% [_dictionary release];
  785. //% [super dealloc];
  786. //%}
  787. //%
  788. //%- (instancetype)copyWithZone:(NSZone *)zone {
  789. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  790. //%}
  791. //%
  792. //%- (BOOL)isEqual:(id)other {
  793. //% if (self == other) {
  794. //% return YES;
  795. //% }
  796. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  797. //% return NO;
  798. //% }
  799. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  800. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  801. //%}
  802. //%
  803. //%- (NSUInteger)hash {
  804. //% return _dictionary.count;
  805. //%}
  806. //%
  807. //%- (NSString *)description {
  808. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  809. //%}
  810. //%
  811. //%- (NSUInteger)count {
  812. //% return _dictionary.count;
  813. //%}
  814. //%
  815. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
  816. //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  817. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  818. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u,
  819. //% BOOL *stop) {
  820. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), stop);
  821. //% }];
  822. //%}
  823. //%
  824. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  825. //% NSUInteger count = _dictionary.count;
  826. //% if (count == 0) {
  827. //% return 0;
  828. //% }
  829. //%
  830. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  831. //% GPBDataType keyDataType = field.mapKeyDataType;
  832. //% __block size_t result = 0;
  833. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  834. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u##,
  835. //% BOOL *stop) {
  836. //% #pragma unused(stop)
  837. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  838. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  839. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  840. //% }];
  841. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  842. //% result += tagSize * count;
  843. //% return result;
  844. //%}
  845. //%
  846. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  847. //% asField:(GPBFieldDescriptor *)field {
  848. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  849. //% GPBDataType keyDataType = field.mapKeyDataType;
  850. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  851. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(ENUM_TYPE##KHELPER(KEY_TYPE)##aKey,
  852. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u,
  853. //% BOOL *stop) {
  854. //% #pragma unused(stop)
  855. //% // Write the tag.
  856. //% [outputStream writeInt32NoTag:tag];
  857. //% // Write the size of the message.
  858. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  859. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  860. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  861. //% // Write the fields.
  862. //% WriteDict##KEY_NAME##Field(outputStream, UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  863. //% WriteDict##VALUE_NAME##Field(outputStream, UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  864. //% }];
  865. //%}
  866. //%
  867. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  868. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  869. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  870. //%}
  871. //%
  872. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  873. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) {
  874. //% #pragma unused(stop)
  875. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
  876. //% }];
  877. //%}
  878. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  879. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
  880. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
  881. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  882. //% if (otherDictionary) {
  883. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  884. //% if (_autocreator) {
  885. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  886. //% }
  887. //% }
  888. //%}
  889. //%
  890. //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
  891. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
  892. //% if (_autocreator) {
  893. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  894. //% }
  895. //%}
  896. //%
  897. //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  898. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  899. //%}
  900. //%
  901. //%- (void)removeAll {
  902. //% [_dictionary removeAllObjects];
  903. //%}
  904. //
  905. // Custom Generation for Bool keys
  906. //
  907. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  908. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
  909. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  910. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
  911. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
  912. //%#pragma mark - Bool -> VALUE_NAME
  913. //%
  914. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  915. //% @package
  916. //% VALUE_TYPE _values[2];
  917. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  918. //%
  919. //%+ (instancetype)dictionary {
  920. //% return [[[self alloc] initWith##VNAME##s:NULL forKeys:NULL count:0] autorelease];
  921. //%}
  922. //%
  923. //%+ (instancetype)dictionaryWith##VNAME##:(VALUE_TYPE)VNAME_VAR
  924. //% ##VNAME$S## forKey:(BOOL)key {
  925. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  926. //% // on to get the type correct.
  927. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:&##VNAME_VAR
  928. //% VALUE_NAME$S ##VNAME$S## forKeys:&key
  929. //% VALUE_NAME$S ##VNAME$S## count:1] autorelease];
  930. //%}
  931. //%
  932. //%+ (instancetype)dictionaryWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  933. //% ##VNAME$S## forKeys:(const BOOL [])keys
  934. //% ##VNAME$S## count:(NSUInteger)count {
  935. //% // Cast is needed so the compiler knows what class we are invoking initWith##VNAME##s:forKeys:count:
  936. //% // on to get the type correct.
  937. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWith##VNAME##s:##VNAME_VAR##s
  938. //% VALUE_NAME$S ##VNAME$S## forKeys:keys
  939. //% VALUE_NAME$S ##VNAME$S## count:count] autorelease];
  940. //%}
  941. //%
  942. //%+ (instancetype)dictionaryWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  943. //% // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  944. //% // on to get the type correct.
  945. //% return [[(GPBBool##VALUE_NAME##Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  946. //%}
  947. //%
  948. //%+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  949. //% return [[[self alloc] initWithCapacity:numItems] autorelease];
  950. //%}
  951. //%
  952. //%- (instancetype)init {
  953. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  954. //%}
  955. //%
  956. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  957. //%
  958. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  959. //% #pragma unused(numItems)
  960. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  961. //%}
  962. //%
  963. //%BOOL_DICT_DEALLOC##HELPER()
  964. //%
  965. //%- (instancetype)copyWithZone:(NSZone *)zone {
  966. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  967. //%}
  968. //%
  969. //%- (BOOL)isEqual:(id)other {
  970. //% if (self == other) {
  971. //% return YES;
  972. //% }
  973. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  974. //% return NO;
  975. //% }
  976. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  977. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  978. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  979. //% return NO;
  980. //% }
  981. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  982. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  983. //% return NO;
  984. //% }
  985. //% return YES;
  986. //%}
  987. //%
  988. //%- (NSUInteger)hash {
  989. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  990. //%}
  991. //%
  992. //%- (NSString *)description {
  993. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  994. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  995. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  996. //% }
  997. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  998. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  999. //% }
  1000. //% [result appendString:@" }"];
  1001. //% return result;
  1002. //%}
  1003. //%
  1004. //%- (NSUInteger)count {
  1005. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  1006. //%}
  1007. //%
  1008. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
  1009. //%
  1010. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  1011. //%
  1012. //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1013. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1014. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  1015. //% }
  1016. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  1017. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  1018. //% }
  1019. //%}
  1020. //%
  1021. //%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
  1022. //% (void (^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  1023. //% BOOL stop = NO;
  1024. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  1025. //% block(NO, _values[0], &stop);
  1026. //% }
  1027. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  1028. //% block(YES, _values[1], &stop);
  1029. //% }
  1030. //%}
  1031. //%
  1032. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1033. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1034. //% NSUInteger count = 0;
  1035. //% size_t result = 0;
  1036. //% for (int i = 0; i < 2; ++i) {
  1037. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1038. //% ++count;
  1039. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1040. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1041. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1042. //% }
  1043. //% }
  1044. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1045. //% result += tagSize * count;
  1046. //% return result;
  1047. //%}
  1048. //%
  1049. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1050. //% asField:(GPBFieldDescriptor *)field {
  1051. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  1052. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1053. //% for (int i = 0; i < 2; ++i) {
  1054. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  1055. //% // Write the tag.
  1056. //% [outputStream writeInt32NoTag:tag];
  1057. //% // Write the size of the message.
  1058. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1059. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  1060. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  1061. //% // Write the fields.
  1062. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  1063. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  1064. //% }
  1065. //% }
  1066. //%}
  1067. //%
  1068. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  1069. //%
  1070. //%@end
  1071. //%
  1072. //
  1073. // Helpers for PODs
  1074. //
  1075. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1076. //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
  1077. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1078. //% if (wrapped && value) {
  1079. //% *value = UNWRAP##VALUE_NAME(wrapped);
  1080. //% }
  1081. //% return (wrapped != NULL);
  1082. //%}
  1083. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  1084. //%@(VALUE)
  1085. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  1086. //%[VALUE unsignedIntValue]
  1087. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  1088. //%[VALUE intValue]
  1089. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  1090. //%[VALUE unsignedLongLongValue]
  1091. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1092. //%[VALUE longLongValue]
  1093. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1094. //%[VALUE boolValue]
  1095. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1096. //%[VALUE floatValue]
  1097. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1098. //%[VALUE doubleValue]
  1099. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1100. //%[VALUE intValue]
  1101. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1102. //%[NSString stringWithFormat:@"%u", VALUE]
  1103. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1104. //%[NSString stringWithFormat:@"%d", VALUE]
  1105. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1106. //%[NSString stringWithFormat:@"%llu", VALUE]
  1107. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1108. //%[NSString stringWithFormat:@"%lld", VALUE]
  1109. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1110. //%(VALUE ? @"true" : @"false")
  1111. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1112. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1113. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1114. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1115. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1116. //%@(VALUE)
  1117. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1118. //%NSNumber *
  1119. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1120. //%VAL1 != VAL2
  1121. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1122. // Empty
  1123. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1124. // Empty
  1125. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1126. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1127. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1128. // Empty
  1129. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1130. // Empty
  1131. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1132. // Empty
  1133. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1134. // Empty
  1135. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1136. // Empty
  1137. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1138. // Empty
  1139. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1140. // Empty
  1141. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1142. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1143. //% forKey:(GPBGenericValue *)key
  1144. //% keyDataType:(GPBDataType)keyDataType {
  1145. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1146. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1147. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1148. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1149. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1150. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1151. //% [outputStream release];
  1152. //% return data;
  1153. //%}
  1154. //%
  1155. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1156. //%value##VALUE_NAME
  1157. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1158. // Empty
  1159. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1160. // Empty
  1161. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1162. //% BOOL _valueSet[2];
  1163. //%
  1164. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1165. //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
  1166. //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys
  1167. //% ##VALUE_NAME$S## count:(NSUInteger)count {
  1168. //% self = [super init];
  1169. //% if (self) {
  1170. //% for (NSUInteger i = 0; i < count; ++i) {
  1171. //% int idx = keys[i] ? 1 : 0;
  1172. //% _values[idx] = values[i];
  1173. //% _valueSet[idx] = YES;
  1174. //% }
  1175. //% }
  1176. //% return self;
  1177. //%}
  1178. //%
  1179. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1180. //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
  1181. //% if (self) {
  1182. //% if (dictionary) {
  1183. //% for (int i = 0; i < 2; ++i) {
  1184. //% if (dictionary->_valueSet[i]) {
  1185. //% _values[i] = dictionary->_values[i];
  1186. //% _valueSet[i] = YES;
  1187. //% }
  1188. //% }
  1189. //% }
  1190. //% }
  1191. //% return self;
  1192. //%}
  1193. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1194. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1195. //%- (void)dealloc {
  1196. //% NSAssert(!_autocreator,
  1197. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1198. //% [self class], _autocreator);
  1199. //% [super dealloc];
  1200. //%}
  1201. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1202. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1203. //%BOOL_DICT_HASPOD(IDX, REF)
  1204. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1205. //%REF##_valueSet[IDX]
  1206. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
  1207. //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
  1208. //% int idx = (key ? 1 : 0);
  1209. //% if (_valueSet[idx]) {
  1210. //% if (value) {
  1211. //% *value = _values[idx];
  1212. //% }
  1213. //% return YES;
  1214. //% }
  1215. //% return NO;
  1216. //%}
  1217. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1218. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1219. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1220. //% int idx = (key->valueBool ? 1 : 0);
  1221. //% _values[idx] = value->value##VALUE_NAME;
  1222. //% _valueSet[idx] = YES;
  1223. //%}
  1224. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1225. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1226. //% if (otherDictionary) {
  1227. //% for (int i = 0; i < 2; ++i) {
  1228. //% if (otherDictionary->_valueSet[i]) {
  1229. //% _valueSet[i] = YES;
  1230. //% _values[i] = otherDictionary->_values[i];
  1231. //% }
  1232. //% }
  1233. //% if (_autocreator) {
  1234. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1235. //% }
  1236. //% }
  1237. //%}
  1238. //%
  1239. //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
  1240. //% int idx = (key ? 1 : 0);
  1241. //% _values[idx] = value;
  1242. //% _valueSet[idx] = YES;
  1243. //% if (_autocreator) {
  1244. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1245. //% }
  1246. //%}
  1247. //%
  1248. //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
  1249. //% _valueSet[aKey ? 1 : 0] = NO;
  1250. //%}
  1251. //%
  1252. //%- (void)removeAll {
  1253. //% _valueSet[0] = NO;
  1254. //% _valueSet[1] = NO;
  1255. //%}
  1256. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1257. //%STR_FORMAT_##VALUE_NAME()
  1258. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1259. //%%u
  1260. //%PDDM-DEFINE STR_FORMAT_Int32()
  1261. //%%d
  1262. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1263. //%%llu
  1264. //%PDDM-DEFINE STR_FORMAT_Int64()
  1265. //%%lld
  1266. //%PDDM-DEFINE STR_FORMAT_Bool()
  1267. //%%d
  1268. //%PDDM-DEFINE STR_FORMAT_Float()
  1269. //%%f
  1270. //%PDDM-DEFINE STR_FORMAT_Double()
  1271. //%%lf
  1272. //
  1273. // Helpers for Objects
  1274. //
  1275. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1276. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1277. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1278. //% return result;
  1279. //%}
  1280. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1281. //%VALUE
  1282. //%PDDM-DEFINE UNWRAPString(VALUE)
  1283. //%VALUE
  1284. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1285. //%VALUE
  1286. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1287. //%VALUE
  1288. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1289. //%VALUE
  1290. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1291. //%ENUM_TYPEOBJECT_##TYPE()
  1292. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1293. //%NSString *
  1294. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1295. //%id ##
  1296. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1297. //%![VAL1 isEqual:VAL2]
  1298. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1299. //%- (BOOL)isInitialized {
  1300. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1301. //% if (!msg.initialized) {
  1302. //% return NO;
  1303. //% }
  1304. //% }
  1305. //% return YES;
  1306. //%}
  1307. //%
  1308. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1309. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1310. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1311. //% [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  1312. //% GPBMessage *msg,
  1313. //% BOOL *stop) {
  1314. //% #pragma unused(stop)
  1315. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1316. //% [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  1317. //% [copiedMsg release];
  1318. //% }];
  1319. //% return newDict;
  1320. //%}
  1321. //%
  1322. //%
  1323. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1324. //%- (BOOL)isInitialized {
  1325. //% if (_values[0] && ![_values[0] isInitialized]) {
  1326. //% return NO;
  1327. //% }
  1328. //% if (_values[1] && ![_values[1] isInitialized]) {
  1329. //% return NO;
  1330. //% }
  1331. //% return YES;
  1332. //%}
  1333. //%
  1334. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1335. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1336. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1337. //% for (int i = 0; i < 2; ++i) {
  1338. //% if (_values[i] != nil) {
  1339. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1340. //% }
  1341. //% }
  1342. //% return newDict;
  1343. //%}
  1344. //%
  1345. //%
  1346. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1347. // Empty
  1348. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1349. //%valueString
  1350. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1351. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1352. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1353. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1354. //%##EXTRA_INDENT$S## }
  1355. //%
  1356. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1357. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1358. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1359. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1360. //%##EXTRA_INDENT$S## }
  1361. //%
  1362. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1363. // Empty
  1364. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1365. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1366. //% forKeys:(const BOOL [])keys
  1367. //% count:(NSUInteger)count {
  1368. //% self = [super init];
  1369. //% if (self) {
  1370. //% for (NSUInteger i = 0; i < count; ++i) {
  1371. //% if (!objects[i]) {
  1372. //% [NSException raise:NSInvalidArgumentException
  1373. //% format:@"Attempting to add nil object to a Dictionary"];
  1374. //% }
  1375. //% int idx = keys[i] ? 1 : 0;
  1376. //% [_values[idx] release];
  1377. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1378. //% }
  1379. //% }
  1380. //% return self;
  1381. //%}
  1382. //%
  1383. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1384. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1385. //% if (self) {
  1386. //% if (dictionary) {
  1387. //% _values[0] = [dictionary->_values[0] retain];
  1388. //% _values[1] = [dictionary->_values[1] retain];
  1389. //% }
  1390. //% }
  1391. //% return self;
  1392. //%}
  1393. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1394. //%- (void)dealloc {
  1395. //% NSAssert(!_autocreator,
  1396. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1397. //% [self class], _autocreator);
  1398. //% [_values[0] release];
  1399. //% [_values[1] release];
  1400. //% [super dealloc];
  1401. //%}
  1402. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1403. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1404. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1405. //%REF##_values[IDX] != nil
  1406. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
  1407. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1408. //% return _values[key ? 1 : 0];
  1409. //%}
  1410. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1411. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1412. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1413. //% int idx = (key->valueBool ? 1 : 0);
  1414. //% [_values[idx] release];
  1415. //% _values[idx] = [value->valueString retain];
  1416. //%}
  1417. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1418. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1419. //% if (otherDictionary) {
  1420. //% for (int i = 0; i < 2; ++i) {
  1421. //% if (otherDictionary->_values[i] != nil) {
  1422. //% [_values[i] release];
  1423. //% _values[i] = [otherDictionary->_values[i] retain];
  1424. //% }
  1425. //% }
  1426. //% if (_autocreator) {
  1427. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1428. //% }
  1429. //% }
  1430. //%}
  1431. //%
  1432. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1433. //% if (!object) {
  1434. //% [NSException raise:NSInvalidArgumentException
  1435. //% format:@"Attempting to add nil object to a Dictionary"];
  1436. //% }
  1437. //% int idx = (key ? 1 : 0);
  1438. //% [_values[idx] release];
  1439. //% _values[idx] = [object retain];
  1440. //% if (_autocreator) {
  1441. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1442. //% }
  1443. //%}
  1444. //%
  1445. //%- (void)removeObjectForKey:(BOOL)aKey {
  1446. //% int idx = (aKey ? 1 : 0);
  1447. //% [_values[idx] release];
  1448. //% _values[idx] = nil;
  1449. //%}
  1450. //%
  1451. //%- (void)removeAll {
  1452. //% for (int i = 0; i < 2; ++i) {
  1453. //% [_values[i] release];
  1454. //% _values[i] = nil;
  1455. //% }
  1456. //%}
  1457. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1458. //%%@
  1459. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1460. // This block of code is generated, do not edit it directly.
  1461. #pragma mark - UInt32 -> UInt32
  1462. @implementation GPBUInt32UInt32Dictionary {
  1463. @package
  1464. NSMutableDictionary *_dictionary;
  1465. }
  1466. + (instancetype)dictionary {
  1467. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  1468. }
  1469. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  1470. forKey:(uint32_t)key {
  1471. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  1472. // on to get the type correct.
  1473. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  1474. forKeys:&key
  1475. count:1] autorelease];
  1476. }
  1477. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  1478. forKeys:(const uint32_t [])keys
  1479. count:(NSUInteger)count {
  1480. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  1481. // on to get the type correct.
  1482. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
  1483. forKeys:keys
  1484. count:count] autorelease];
  1485. }
  1486. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1487. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1488. // on to get the type correct.
  1489. return [[(GPBUInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1490. }
  1491. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1492. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1493. }
  1494. - (instancetype)init {
  1495. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1496. }
  1497. - (instancetype)initWithUInt32s:(const uint32_t [])values
  1498. forKeys:(const uint32_t [])keys
  1499. count:(NSUInteger)count {
  1500. self = [super init];
  1501. if (self) {
  1502. _dictionary = [[NSMutableDictionary alloc] init];
  1503. if (count && values && keys) {
  1504. for (NSUInteger i = 0; i < count; ++i) {
  1505. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1506. }
  1507. }
  1508. }
  1509. return self;
  1510. }
  1511. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1512. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  1513. if (self) {
  1514. if (dictionary) {
  1515. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1516. }
  1517. }
  1518. return self;
  1519. }
  1520. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1521. #pragma unused(numItems)
  1522. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1523. }
  1524. - (void)dealloc {
  1525. NSAssert(!_autocreator,
  1526. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1527. [self class], _autocreator);
  1528. [_dictionary release];
  1529. [super dealloc];
  1530. }
  1531. - (instancetype)copyWithZone:(NSZone *)zone {
  1532. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1533. }
  1534. - (BOOL)isEqual:(id)other {
  1535. if (self == other) {
  1536. return YES;
  1537. }
  1538. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1539. return NO;
  1540. }
  1541. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1542. return [_dictionary isEqual:otherDictionary->_dictionary];
  1543. }
  1544. - (NSUInteger)hash {
  1545. return _dictionary.count;
  1546. }
  1547. - (NSString *)description {
  1548. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1549. }
  1550. - (NSUInteger)count {
  1551. return _dictionary.count;
  1552. }
  1553. - (void)enumerateKeysAndUInt32sUsingBlock:
  1554. (void (^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1555. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1556. NSNumber *aValue,
  1557. BOOL *stop) {
  1558. block([aKey unsignedIntValue], [aValue unsignedIntValue], stop);
  1559. }];
  1560. }
  1561. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1562. NSUInteger count = _dictionary.count;
  1563. if (count == 0) {
  1564. return 0;
  1565. }
  1566. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1567. GPBDataType keyDataType = field.mapKeyDataType;
  1568. __block size_t result = 0;
  1569. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1570. NSNumber *aValue,
  1571. BOOL *stop) {
  1572. #pragma unused(stop)
  1573. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1574. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1575. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1576. }];
  1577. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1578. result += tagSize * count;
  1579. return result;
  1580. }
  1581. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1582. asField:(GPBFieldDescriptor *)field {
  1583. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1584. GPBDataType keyDataType = field.mapKeyDataType;
  1585. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1586. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1587. NSNumber *aValue,
  1588. BOOL *stop) {
  1589. #pragma unused(stop)
  1590. // Write the tag.
  1591. [outputStream writeInt32NoTag:tag];
  1592. // Write the size of the message.
  1593. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1594. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1595. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1596. // Write the fields.
  1597. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1598. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1599. }];
  1600. }
  1601. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1602. forGPBGenericValueKey:(GPBGenericValue *)key {
  1603. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1604. }
  1605. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1606. [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1607. #pragma unused(stop)
  1608. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1609. }];
  1610. }
  1611. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
  1612. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1613. if (wrapped && value) {
  1614. *value = [wrapped unsignedIntValue];
  1615. }
  1616. return (wrapped != NULL);
  1617. }
  1618. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1619. if (otherDictionary) {
  1620. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1621. if (_autocreator) {
  1622. GPBAutocreatedDictionaryModified(_autocreator, self);
  1623. }
  1624. }
  1625. }
  1626. - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
  1627. [_dictionary setObject:@(value) forKey:@(key)];
  1628. if (_autocreator) {
  1629. GPBAutocreatedDictionaryModified(_autocreator, self);
  1630. }
  1631. }
  1632. - (void)removeUInt32ForKey:(uint32_t)aKey {
  1633. [_dictionary removeObjectForKey:@(aKey)];
  1634. }
  1635. - (void)removeAll {
  1636. [_dictionary removeAllObjects];
  1637. }
  1638. @end
  1639. #pragma mark - UInt32 -> Int32
  1640. @implementation GPBUInt32Int32Dictionary {
  1641. @package
  1642. NSMutableDictionary *_dictionary;
  1643. }
  1644. + (instancetype)dictionary {
  1645. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  1646. }
  1647. + (instancetype)dictionaryWithInt32:(int32_t)value
  1648. forKey:(uint32_t)key {
  1649. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  1650. // on to get the type correct.
  1651. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
  1652. forKeys:&key
  1653. count:1] autorelease];
  1654. }
  1655. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  1656. forKeys:(const uint32_t [])keys
  1657. count:(NSUInteger)count {
  1658. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  1659. // on to get the type correct.
  1660. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithInt32s:values
  1661. forKeys:keys
  1662. count:count] autorelease];
  1663. }
  1664. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1665. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1666. // on to get the type correct.
  1667. return [[(GPBUInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1668. }
  1669. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1670. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1671. }
  1672. - (instancetype)init {
  1673. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1674. }
  1675. - (instancetype)initWithInt32s:(const int32_t [])values
  1676. forKeys:(const uint32_t [])keys
  1677. count:(NSUInteger)count {
  1678. self = [super init];
  1679. if (self) {
  1680. _dictionary = [[NSMutableDictionary alloc] init];
  1681. if (count && values && keys) {
  1682. for (NSUInteger i = 0; i < count; ++i) {
  1683. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1684. }
  1685. }
  1686. }
  1687. return self;
  1688. }
  1689. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1690. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  1691. if (self) {
  1692. if (dictionary) {
  1693. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1694. }
  1695. }
  1696. return self;
  1697. }
  1698. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1699. #pragma unused(numItems)
  1700. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1701. }
  1702. - (void)dealloc {
  1703. NSAssert(!_autocreator,
  1704. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1705. [self class], _autocreator);
  1706. [_dictionary release];
  1707. [super dealloc];
  1708. }
  1709. - (instancetype)copyWithZone:(NSZone *)zone {
  1710. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1711. }
  1712. - (BOOL)isEqual:(id)other {
  1713. if (self == other) {
  1714. return YES;
  1715. }
  1716. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1717. return NO;
  1718. }
  1719. GPBUInt32Int32Dictionary *otherDictionary = other;
  1720. return [_dictionary isEqual:otherDictionary->_dictionary];
  1721. }
  1722. - (NSUInteger)hash {
  1723. return _dictionary.count;
  1724. }
  1725. - (NSString *)description {
  1726. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1727. }
  1728. - (NSUInteger)count {
  1729. return _dictionary.count;
  1730. }
  1731. - (void)enumerateKeysAndInt32sUsingBlock:
  1732. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  1733. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1734. NSNumber *aValue,
  1735. BOOL *stop) {
  1736. block([aKey unsignedIntValue], [aValue intValue], stop);
  1737. }];
  1738. }
  1739. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1740. NSUInteger count = _dictionary.count;
  1741. if (count == 0) {
  1742. return 0;
  1743. }
  1744. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1745. GPBDataType keyDataType = field.mapKeyDataType;
  1746. __block size_t result = 0;
  1747. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1748. NSNumber *aValue,
  1749. BOOL *stop) {
  1750. #pragma unused(stop)
  1751. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1752. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1753. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1754. }];
  1755. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1756. result += tagSize * count;
  1757. return result;
  1758. }
  1759. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1760. asField:(GPBFieldDescriptor *)field {
  1761. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1762. GPBDataType keyDataType = field.mapKeyDataType;
  1763. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1764. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1765. NSNumber *aValue,
  1766. BOOL *stop) {
  1767. #pragma unused(stop)
  1768. // Write the tag.
  1769. [outputStream writeInt32NoTag:tag];
  1770. // Write the size of the message.
  1771. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1772. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1773. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1774. // Write the fields.
  1775. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1776. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  1777. }];
  1778. }
  1779. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1780. forGPBGenericValueKey:(GPBGenericValue *)key {
  1781. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1782. }
  1783. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1784. [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1785. #pragma unused(stop)
  1786. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1787. }];
  1788. }
  1789. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
  1790. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1791. if (wrapped && value) {
  1792. *value = [wrapped intValue];
  1793. }
  1794. return (wrapped != NULL);
  1795. }
  1796. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1797. if (otherDictionary) {
  1798. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1799. if (_autocreator) {
  1800. GPBAutocreatedDictionaryModified(_autocreator, self);
  1801. }
  1802. }
  1803. }
  1804. - (void)setInt32:(int32_t)value forKey:(uint32_t)key {
  1805. [_dictionary setObject:@(value) forKey:@(key)];
  1806. if (_autocreator) {
  1807. GPBAutocreatedDictionaryModified(_autocreator, self);
  1808. }
  1809. }
  1810. - (void)removeInt32ForKey:(uint32_t)aKey {
  1811. [_dictionary removeObjectForKey:@(aKey)];
  1812. }
  1813. - (void)removeAll {
  1814. [_dictionary removeAllObjects];
  1815. }
  1816. @end
  1817. #pragma mark - UInt32 -> UInt64
  1818. @implementation GPBUInt32UInt64Dictionary {
  1819. @package
  1820. NSMutableDictionary *_dictionary;
  1821. }
  1822. + (instancetype)dictionary {
  1823. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  1824. }
  1825. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  1826. forKey:(uint32_t)key {
  1827. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  1828. // on to get the type correct.
  1829. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  1830. forKeys:&key
  1831. count:1] autorelease];
  1832. }
  1833. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  1834. forKeys:(const uint32_t [])keys
  1835. count:(NSUInteger)count {
  1836. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  1837. // on to get the type correct.
  1838. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
  1839. forKeys:keys
  1840. count:count] autorelease];
  1841. }
  1842. + (instancetype)dictionaryWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1843. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  1844. // on to get the type correct.
  1845. return [[(GPBUInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  1846. }
  1847. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  1848. return [[[self alloc] initWithCapacity:numItems] autorelease];
  1849. }
  1850. - (instancetype)init {
  1851. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1852. }
  1853. - (instancetype)initWithUInt64s:(const uint64_t [])values
  1854. forKeys:(const uint32_t [])keys
  1855. count:(NSUInteger)count {
  1856. self = [super init];
  1857. if (self) {
  1858. _dictionary = [[NSMutableDictionary alloc] init];
  1859. if (count && values && keys) {
  1860. for (NSUInteger i = 0; i < count; ++i) {
  1861. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1862. }
  1863. }
  1864. }
  1865. return self;
  1866. }
  1867. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1868. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  1869. if (self) {
  1870. if (dictionary) {
  1871. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1872. }
  1873. }
  1874. return self;
  1875. }
  1876. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1877. #pragma unused(numItems)
  1878. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1879. }
  1880. - (void)dealloc {
  1881. NSAssert(!_autocreator,
  1882. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1883. [self class], _autocreator);
  1884. [_dictionary release];
  1885. [super dealloc];
  1886. }
  1887. - (instancetype)copyWithZone:(NSZone *)zone {
  1888. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1889. }
  1890. - (BOOL)isEqual:(id)other {
  1891. if (self == other) {
  1892. return YES;
  1893. }
  1894. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1895. return NO;
  1896. }
  1897. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1898. return [_dictionary isEqual:otherDictionary->_dictionary];
  1899. }
  1900. - (NSUInteger)hash {
  1901. return _dictionary.count;
  1902. }
  1903. - (NSString *)description {
  1904. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1905. }
  1906. - (NSUInteger)count {
  1907. return _dictionary.count;
  1908. }
  1909. - (void)enumerateKeysAndUInt64sUsingBlock:
  1910. (void (^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1911. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1912. NSNumber *aValue,
  1913. BOOL *stop) {
  1914. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], stop);
  1915. }];
  1916. }
  1917. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1918. NSUInteger count = _dictionary.count;
  1919. if (count == 0) {
  1920. return 0;
  1921. }
  1922. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1923. GPBDataType keyDataType = field.mapKeyDataType;
  1924. __block size_t result = 0;
  1925. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1926. NSNumber *aValue,
  1927. BOOL *stop) {
  1928. #pragma unused(stop)
  1929. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1930. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1931. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1932. }];
  1933. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1934. result += tagSize * count;
  1935. return result;
  1936. }
  1937. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1938. asField:(GPBFieldDescriptor *)field {
  1939. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1940. GPBDataType keyDataType = field.mapKeyDataType;
  1941. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1942. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  1943. NSNumber *aValue,
  1944. BOOL *stop) {
  1945. #pragma unused(stop)
  1946. // Write the tag.
  1947. [outputStream writeInt32NoTag:tag];
  1948. // Write the size of the message.
  1949. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1950. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1951. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1952. // Write the fields.
  1953. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1954. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1955. }];
  1956. }
  1957. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1958. forGPBGenericValueKey:(GPBGenericValue *)key {
  1959. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1960. }
  1961. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  1962. [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1963. #pragma unused(stop)
  1964. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1965. }];
  1966. }
  1967. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
  1968. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1969. if (wrapped && value) {
  1970. *value = [wrapped unsignedLongLongValue];
  1971. }
  1972. return (wrapped != NULL);
  1973. }
  1974. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1975. if (otherDictionary) {
  1976. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1977. if (_autocreator) {
  1978. GPBAutocreatedDictionaryModified(_autocreator, self);
  1979. }
  1980. }
  1981. }
  1982. - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
  1983. [_dictionary setObject:@(value) forKey:@(key)];
  1984. if (_autocreator) {
  1985. GPBAutocreatedDictionaryModified(_autocreator, self);
  1986. }
  1987. }
  1988. - (void)removeUInt64ForKey:(uint32_t)aKey {
  1989. [_dictionary removeObjectForKey:@(aKey)];
  1990. }
  1991. - (void)removeAll {
  1992. [_dictionary removeAllObjects];
  1993. }
  1994. @end
  1995. #pragma mark - UInt32 -> Int64
  1996. @implementation GPBUInt32Int64Dictionary {
  1997. @package
  1998. NSMutableDictionary *_dictionary;
  1999. }
  2000. + (instancetype)dictionary {
  2001. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  2002. }
  2003. + (instancetype)dictionaryWithInt64:(int64_t)value
  2004. forKey:(uint32_t)key {
  2005. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  2006. // on to get the type correct.
  2007. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
  2008. forKeys:&key
  2009. count:1] autorelease];
  2010. }
  2011. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  2012. forKeys:(const uint32_t [])keys
  2013. count:(NSUInteger)count {
  2014. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  2015. // on to get the type correct.
  2016. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithInt64s:values
  2017. forKeys:keys
  2018. count:count] autorelease];
  2019. }
  2020. + (instancetype)dictionaryWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2021. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2022. // on to get the type correct.
  2023. return [[(GPBUInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2024. }
  2025. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2026. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2027. }
  2028. - (instancetype)init {
  2029. return [self initWithInt64s:NULL forKeys:NULL count:0];
  2030. }
  2031. - (instancetype)initWithInt64s:(const int64_t [])values
  2032. forKeys:(const uint32_t [])keys
  2033. count:(NSUInteger)count {
  2034. self = [super init];
  2035. if (self) {
  2036. _dictionary = [[NSMutableDictionary alloc] init];
  2037. if (count && values && keys) {
  2038. for (NSUInteger i = 0; i < count; ++i) {
  2039. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2040. }
  2041. }
  2042. }
  2043. return self;
  2044. }
  2045. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  2046. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  2047. if (self) {
  2048. if (dictionary) {
  2049. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2050. }
  2051. }
  2052. return self;
  2053. }
  2054. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2055. #pragma unused(numItems)
  2056. return [self initWithInt64s:NULL forKeys:NULL count:0];
  2057. }
  2058. - (void)dealloc {
  2059. NSAssert(!_autocreator,
  2060. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2061. [self class], _autocreator);
  2062. [_dictionary release];
  2063. [super dealloc];
  2064. }
  2065. - (instancetype)copyWithZone:(NSZone *)zone {
  2066. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  2067. }
  2068. - (BOOL)isEqual:(id)other {
  2069. if (self == other) {
  2070. return YES;
  2071. }
  2072. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  2073. return NO;
  2074. }
  2075. GPBUInt32Int64Dictionary *otherDictionary = other;
  2076. return [_dictionary isEqual:otherDictionary->_dictionary];
  2077. }
  2078. - (NSUInteger)hash {
  2079. return _dictionary.count;
  2080. }
  2081. - (NSString *)description {
  2082. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2083. }
  2084. - (NSUInteger)count {
  2085. return _dictionary.count;
  2086. }
  2087. - (void)enumerateKeysAndInt64sUsingBlock:
  2088. (void (^)(uint32_t key, int64_t value, BOOL *stop))block {
  2089. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2090. NSNumber *aValue,
  2091. BOOL *stop) {
  2092. block([aKey unsignedIntValue], [aValue longLongValue], stop);
  2093. }];
  2094. }
  2095. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2096. NSUInteger count = _dictionary.count;
  2097. if (count == 0) {
  2098. return 0;
  2099. }
  2100. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2101. GPBDataType keyDataType = field.mapKeyDataType;
  2102. __block size_t result = 0;
  2103. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2104. NSNumber *aValue,
  2105. BOOL *stop) {
  2106. #pragma unused(stop)
  2107. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2108. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2109. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2110. }];
  2111. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2112. result += tagSize * count;
  2113. return result;
  2114. }
  2115. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2116. asField:(GPBFieldDescriptor *)field {
  2117. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2118. GPBDataType keyDataType = field.mapKeyDataType;
  2119. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2120. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2121. NSNumber *aValue,
  2122. BOOL *stop) {
  2123. #pragma unused(stop)
  2124. // Write the tag.
  2125. [outputStream writeInt32NoTag:tag];
  2126. // Write the size of the message.
  2127. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2128. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2129. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2130. // Write the fields.
  2131. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2132. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  2133. }];
  2134. }
  2135. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2136. forGPBGenericValueKey:(GPBGenericValue *)key {
  2137. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  2138. }
  2139. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2140. [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  2141. #pragma unused(stop)
  2142. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  2143. }];
  2144. }
  2145. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
  2146. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2147. if (wrapped && value) {
  2148. *value = [wrapped longLongValue];
  2149. }
  2150. return (wrapped != NULL);
  2151. }
  2152. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  2153. if (otherDictionary) {
  2154. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2155. if (_autocreator) {
  2156. GPBAutocreatedDictionaryModified(_autocreator, self);
  2157. }
  2158. }
  2159. }
  2160. - (void)setInt64:(int64_t)value forKey:(uint32_t)key {
  2161. [_dictionary setObject:@(value) forKey:@(key)];
  2162. if (_autocreator) {
  2163. GPBAutocreatedDictionaryModified(_autocreator, self);
  2164. }
  2165. }
  2166. - (void)removeInt64ForKey:(uint32_t)aKey {
  2167. [_dictionary removeObjectForKey:@(aKey)];
  2168. }
  2169. - (void)removeAll {
  2170. [_dictionary removeAllObjects];
  2171. }
  2172. @end
  2173. #pragma mark - UInt32 -> Bool
  2174. @implementation GPBUInt32BoolDictionary {
  2175. @package
  2176. NSMutableDictionary *_dictionary;
  2177. }
  2178. + (instancetype)dictionary {
  2179. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  2180. }
  2181. + (instancetype)dictionaryWithBool:(BOOL)value
  2182. forKey:(uint32_t)key {
  2183. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  2184. // on to get the type correct.
  2185. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:&value
  2186. forKeys:&key
  2187. count:1] autorelease];
  2188. }
  2189. + (instancetype)dictionaryWithBools:(const BOOL [])values
  2190. forKeys:(const uint32_t [])keys
  2191. count:(NSUInteger)count {
  2192. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  2193. // on to get the type correct.
  2194. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithBools:values
  2195. forKeys:keys
  2196. count:count] autorelease];
  2197. }
  2198. + (instancetype)dictionaryWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2199. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2200. // on to get the type correct.
  2201. return [[(GPBUInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2202. }
  2203. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2204. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2205. }
  2206. - (instancetype)init {
  2207. return [self initWithBools:NULL forKeys:NULL count:0];
  2208. }
  2209. - (instancetype)initWithBools:(const BOOL [])values
  2210. forKeys:(const uint32_t [])keys
  2211. count:(NSUInteger)count {
  2212. self = [super init];
  2213. if (self) {
  2214. _dictionary = [[NSMutableDictionary alloc] init];
  2215. if (count && values && keys) {
  2216. for (NSUInteger i = 0; i < count; ++i) {
  2217. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2218. }
  2219. }
  2220. }
  2221. return self;
  2222. }
  2223. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2224. self = [self initWithBools:NULL forKeys:NULL count:0];
  2225. if (self) {
  2226. if (dictionary) {
  2227. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2228. }
  2229. }
  2230. return self;
  2231. }
  2232. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2233. #pragma unused(numItems)
  2234. return [self initWithBools:NULL forKeys:NULL count:0];
  2235. }
  2236. - (void)dealloc {
  2237. NSAssert(!_autocreator,
  2238. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2239. [self class], _autocreator);
  2240. [_dictionary release];
  2241. [super dealloc];
  2242. }
  2243. - (instancetype)copyWithZone:(NSZone *)zone {
  2244. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2245. }
  2246. - (BOOL)isEqual:(id)other {
  2247. if (self == other) {
  2248. return YES;
  2249. }
  2250. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2251. return NO;
  2252. }
  2253. GPBUInt32BoolDictionary *otherDictionary = other;
  2254. return [_dictionary isEqual:otherDictionary->_dictionary];
  2255. }
  2256. - (NSUInteger)hash {
  2257. return _dictionary.count;
  2258. }
  2259. - (NSString *)description {
  2260. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2261. }
  2262. - (NSUInteger)count {
  2263. return _dictionary.count;
  2264. }
  2265. - (void)enumerateKeysAndBoolsUsingBlock:
  2266. (void (^)(uint32_t key, BOOL value, BOOL *stop))block {
  2267. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2268. NSNumber *aValue,
  2269. BOOL *stop) {
  2270. block([aKey unsignedIntValue], [aValue boolValue], stop);
  2271. }];
  2272. }
  2273. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2274. NSUInteger count = _dictionary.count;
  2275. if (count == 0) {
  2276. return 0;
  2277. }
  2278. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2279. GPBDataType keyDataType = field.mapKeyDataType;
  2280. __block size_t result = 0;
  2281. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2282. NSNumber *aValue,
  2283. BOOL *stop) {
  2284. #pragma unused(stop)
  2285. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2286. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2287. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2288. }];
  2289. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2290. result += tagSize * count;
  2291. return result;
  2292. }
  2293. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2294. asField:(GPBFieldDescriptor *)field {
  2295. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2296. GPBDataType keyDataType = field.mapKeyDataType;
  2297. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2298. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2299. NSNumber *aValue,
  2300. BOOL *stop) {
  2301. #pragma unused(stop)
  2302. // Write the tag.
  2303. [outputStream writeInt32NoTag:tag];
  2304. // Write the size of the message.
  2305. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2306. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2307. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2308. // Write the fields.
  2309. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2310. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  2311. }];
  2312. }
  2313. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2314. forGPBGenericValueKey:(GPBGenericValue *)key {
  2315. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2316. }
  2317. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2318. [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2319. #pragma unused(stop)
  2320. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2321. }];
  2322. }
  2323. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
  2324. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2325. if (wrapped && value) {
  2326. *value = [wrapped boolValue];
  2327. }
  2328. return (wrapped != NULL);
  2329. }
  2330. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2331. if (otherDictionary) {
  2332. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2333. if (_autocreator) {
  2334. GPBAutocreatedDictionaryModified(_autocreator, self);
  2335. }
  2336. }
  2337. }
  2338. - (void)setBool:(BOOL)value forKey:(uint32_t)key {
  2339. [_dictionary setObject:@(value) forKey:@(key)];
  2340. if (_autocreator) {
  2341. GPBAutocreatedDictionaryModified(_autocreator, self);
  2342. }
  2343. }
  2344. - (void)removeBoolForKey:(uint32_t)aKey {
  2345. [_dictionary removeObjectForKey:@(aKey)];
  2346. }
  2347. - (void)removeAll {
  2348. [_dictionary removeAllObjects];
  2349. }
  2350. @end
  2351. #pragma mark - UInt32 -> Float
  2352. @implementation GPBUInt32FloatDictionary {
  2353. @package
  2354. NSMutableDictionary *_dictionary;
  2355. }
  2356. + (instancetype)dictionary {
  2357. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  2358. }
  2359. + (instancetype)dictionaryWithFloat:(float)value
  2360. forKey:(uint32_t)key {
  2361. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  2362. // on to get the type correct.
  2363. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:&value
  2364. forKeys:&key
  2365. count:1] autorelease];
  2366. }
  2367. + (instancetype)dictionaryWithFloats:(const float [])values
  2368. forKeys:(const uint32_t [])keys
  2369. count:(NSUInteger)count {
  2370. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  2371. // on to get the type correct.
  2372. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithFloats:values
  2373. forKeys:keys
  2374. count:count] autorelease];
  2375. }
  2376. + (instancetype)dictionaryWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2377. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2378. // on to get the type correct.
  2379. return [[(GPBUInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2380. }
  2381. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2382. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2383. }
  2384. - (instancetype)init {
  2385. return [self initWithFloats:NULL forKeys:NULL count:0];
  2386. }
  2387. - (instancetype)initWithFloats:(const float [])values
  2388. forKeys:(const uint32_t [])keys
  2389. count:(NSUInteger)count {
  2390. self = [super init];
  2391. if (self) {
  2392. _dictionary = [[NSMutableDictionary alloc] init];
  2393. if (count && values && keys) {
  2394. for (NSUInteger i = 0; i < count; ++i) {
  2395. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2396. }
  2397. }
  2398. }
  2399. return self;
  2400. }
  2401. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2402. self = [self initWithFloats:NULL forKeys:NULL count:0];
  2403. if (self) {
  2404. if (dictionary) {
  2405. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2406. }
  2407. }
  2408. return self;
  2409. }
  2410. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2411. #pragma unused(numItems)
  2412. return [self initWithFloats:NULL forKeys:NULL count:0];
  2413. }
  2414. - (void)dealloc {
  2415. NSAssert(!_autocreator,
  2416. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2417. [self class], _autocreator);
  2418. [_dictionary release];
  2419. [super dealloc];
  2420. }
  2421. - (instancetype)copyWithZone:(NSZone *)zone {
  2422. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2423. }
  2424. - (BOOL)isEqual:(id)other {
  2425. if (self == other) {
  2426. return YES;
  2427. }
  2428. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2429. return NO;
  2430. }
  2431. GPBUInt32FloatDictionary *otherDictionary = other;
  2432. return [_dictionary isEqual:otherDictionary->_dictionary];
  2433. }
  2434. - (NSUInteger)hash {
  2435. return _dictionary.count;
  2436. }
  2437. - (NSString *)description {
  2438. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2439. }
  2440. - (NSUInteger)count {
  2441. return _dictionary.count;
  2442. }
  2443. - (void)enumerateKeysAndFloatsUsingBlock:
  2444. (void (^)(uint32_t key, float value, BOOL *stop))block {
  2445. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2446. NSNumber *aValue,
  2447. BOOL *stop) {
  2448. block([aKey unsignedIntValue], [aValue floatValue], stop);
  2449. }];
  2450. }
  2451. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2452. NSUInteger count = _dictionary.count;
  2453. if (count == 0) {
  2454. return 0;
  2455. }
  2456. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2457. GPBDataType keyDataType = field.mapKeyDataType;
  2458. __block size_t result = 0;
  2459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2460. NSNumber *aValue,
  2461. BOOL *stop) {
  2462. #pragma unused(stop)
  2463. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2464. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2465. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2466. }];
  2467. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2468. result += tagSize * count;
  2469. return result;
  2470. }
  2471. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2472. asField:(GPBFieldDescriptor *)field {
  2473. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2474. GPBDataType keyDataType = field.mapKeyDataType;
  2475. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2476. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2477. NSNumber *aValue,
  2478. BOOL *stop) {
  2479. #pragma unused(stop)
  2480. // Write the tag.
  2481. [outputStream writeInt32NoTag:tag];
  2482. // Write the size of the message.
  2483. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2484. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2485. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2486. // Write the fields.
  2487. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2488. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  2489. }];
  2490. }
  2491. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2492. forGPBGenericValueKey:(GPBGenericValue *)key {
  2493. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2494. }
  2495. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2496. [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2497. #pragma unused(stop)
  2498. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2499. }];
  2500. }
  2501. - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
  2502. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2503. if (wrapped && value) {
  2504. *value = [wrapped floatValue];
  2505. }
  2506. return (wrapped != NULL);
  2507. }
  2508. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2509. if (otherDictionary) {
  2510. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2511. if (_autocreator) {
  2512. GPBAutocreatedDictionaryModified(_autocreator, self);
  2513. }
  2514. }
  2515. }
  2516. - (void)setFloat:(float)value forKey:(uint32_t)key {
  2517. [_dictionary setObject:@(value) forKey:@(key)];
  2518. if (_autocreator) {
  2519. GPBAutocreatedDictionaryModified(_autocreator, self);
  2520. }
  2521. }
  2522. - (void)removeFloatForKey:(uint32_t)aKey {
  2523. [_dictionary removeObjectForKey:@(aKey)];
  2524. }
  2525. - (void)removeAll {
  2526. [_dictionary removeAllObjects];
  2527. }
  2528. @end
  2529. #pragma mark - UInt32 -> Double
  2530. @implementation GPBUInt32DoubleDictionary {
  2531. @package
  2532. NSMutableDictionary *_dictionary;
  2533. }
  2534. + (instancetype)dictionary {
  2535. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  2536. }
  2537. + (instancetype)dictionaryWithDouble:(double)value
  2538. forKey:(uint32_t)key {
  2539. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  2540. // on to get the type correct.
  2541. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
  2542. forKeys:&key
  2543. count:1] autorelease];
  2544. }
  2545. + (instancetype)dictionaryWithDoubles:(const double [])values
  2546. forKeys:(const uint32_t [])keys
  2547. count:(NSUInteger)count {
  2548. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  2549. // on to get the type correct.
  2550. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDoubles:values
  2551. forKeys:keys
  2552. count:count] autorelease];
  2553. }
  2554. + (instancetype)dictionaryWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2555. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2556. // on to get the type correct.
  2557. return [[(GPBUInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2558. }
  2559. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2560. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2561. }
  2562. - (instancetype)init {
  2563. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2564. }
  2565. - (instancetype)initWithDoubles:(const double [])values
  2566. forKeys:(const uint32_t [])keys
  2567. count:(NSUInteger)count {
  2568. self = [super init];
  2569. if (self) {
  2570. _dictionary = [[NSMutableDictionary alloc] init];
  2571. if (count && values && keys) {
  2572. for (NSUInteger i = 0; i < count; ++i) {
  2573. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2574. }
  2575. }
  2576. }
  2577. return self;
  2578. }
  2579. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2580. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  2581. if (self) {
  2582. if (dictionary) {
  2583. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2584. }
  2585. }
  2586. return self;
  2587. }
  2588. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2589. #pragma unused(numItems)
  2590. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2591. }
  2592. - (void)dealloc {
  2593. NSAssert(!_autocreator,
  2594. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2595. [self class], _autocreator);
  2596. [_dictionary release];
  2597. [super dealloc];
  2598. }
  2599. - (instancetype)copyWithZone:(NSZone *)zone {
  2600. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2601. }
  2602. - (BOOL)isEqual:(id)other {
  2603. if (self == other) {
  2604. return YES;
  2605. }
  2606. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2607. return NO;
  2608. }
  2609. GPBUInt32DoubleDictionary *otherDictionary = other;
  2610. return [_dictionary isEqual:otherDictionary->_dictionary];
  2611. }
  2612. - (NSUInteger)hash {
  2613. return _dictionary.count;
  2614. }
  2615. - (NSString *)description {
  2616. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2617. }
  2618. - (NSUInteger)count {
  2619. return _dictionary.count;
  2620. }
  2621. - (void)enumerateKeysAndDoublesUsingBlock:
  2622. (void (^)(uint32_t key, double value, BOOL *stop))block {
  2623. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2624. NSNumber *aValue,
  2625. BOOL *stop) {
  2626. block([aKey unsignedIntValue], [aValue doubleValue], stop);
  2627. }];
  2628. }
  2629. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2630. NSUInteger count = _dictionary.count;
  2631. if (count == 0) {
  2632. return 0;
  2633. }
  2634. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2635. GPBDataType keyDataType = field.mapKeyDataType;
  2636. __block size_t result = 0;
  2637. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2638. NSNumber *aValue,
  2639. BOOL *stop) {
  2640. #pragma unused(stop)
  2641. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2642. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2643. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2644. }];
  2645. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2646. result += tagSize * count;
  2647. return result;
  2648. }
  2649. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2650. asField:(GPBFieldDescriptor *)field {
  2651. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2652. GPBDataType keyDataType = field.mapKeyDataType;
  2653. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2654. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2655. NSNumber *aValue,
  2656. BOOL *stop) {
  2657. #pragma unused(stop)
  2658. // Write the tag.
  2659. [outputStream writeInt32NoTag:tag];
  2660. // Write the size of the message.
  2661. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2662. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2663. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2664. // Write the fields.
  2665. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2666. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2667. }];
  2668. }
  2669. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2670. forGPBGenericValueKey:(GPBGenericValue *)key {
  2671. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2672. }
  2673. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2674. [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2675. #pragma unused(stop)
  2676. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2677. }];
  2678. }
  2679. - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
  2680. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2681. if (wrapped && value) {
  2682. *value = [wrapped doubleValue];
  2683. }
  2684. return (wrapped != NULL);
  2685. }
  2686. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2687. if (otherDictionary) {
  2688. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2689. if (_autocreator) {
  2690. GPBAutocreatedDictionaryModified(_autocreator, self);
  2691. }
  2692. }
  2693. }
  2694. - (void)setDouble:(double)value forKey:(uint32_t)key {
  2695. [_dictionary setObject:@(value) forKey:@(key)];
  2696. if (_autocreator) {
  2697. GPBAutocreatedDictionaryModified(_autocreator, self);
  2698. }
  2699. }
  2700. - (void)removeDoubleForKey:(uint32_t)aKey {
  2701. [_dictionary removeObjectForKey:@(aKey)];
  2702. }
  2703. - (void)removeAll {
  2704. [_dictionary removeAllObjects];
  2705. }
  2706. @end
  2707. #pragma mark - UInt32 -> Enum
  2708. @implementation GPBUInt32EnumDictionary {
  2709. @package
  2710. NSMutableDictionary *_dictionary;
  2711. GPBEnumValidationFunc _validationFunc;
  2712. }
  2713. @synthesize validationFunc = _validationFunc;
  2714. + (instancetype)dictionary {
  2715. return [[[self alloc] initWithValidationFunction:NULL
  2716. rawValues:NULL
  2717. forKeys:NULL
  2718. count:0] autorelease];
  2719. }
  2720. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  2721. return [[[self alloc] initWithValidationFunction:func
  2722. rawValues:NULL
  2723. forKeys:NULL
  2724. count:0] autorelease];
  2725. }
  2726. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2727. rawValue:(int32_t)rawValue
  2728. forKey:(uint32_t)key {
  2729. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2730. // on to get the type correct.
  2731. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2732. rawValues:&rawValue
  2733. forKeys:&key
  2734. count:1] autorelease];
  2735. }
  2736. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2737. rawValues:(const int32_t [])rawValues
  2738. forKeys:(const uint32_t [])keys
  2739. count:(NSUInteger)count {
  2740. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2741. // on to get the type correct.
  2742. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  2743. rawValues:rawValues
  2744. forKeys:keys
  2745. count:count] autorelease];
  2746. }
  2747. + (instancetype)dictionaryWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2748. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  2749. // on to get the type correct.
  2750. return [[(GPBUInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2751. }
  2752. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  2753. capacity:(NSUInteger)numItems {
  2754. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  2755. }
  2756. - (instancetype)init {
  2757. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2758. }
  2759. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2760. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2761. }
  2762. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2763. rawValues:(const int32_t [])rawValues
  2764. forKeys:(const uint32_t [])keys
  2765. count:(NSUInteger)count {
  2766. self = [super init];
  2767. if (self) {
  2768. _dictionary = [[NSMutableDictionary alloc] init];
  2769. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2770. if (count && rawValues && keys) {
  2771. for (NSUInteger i = 0; i < count; ++i) {
  2772. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2773. }
  2774. }
  2775. }
  2776. return self;
  2777. }
  2778. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2779. self = [self initWithValidationFunction:dictionary.validationFunc
  2780. rawValues:NULL
  2781. forKeys:NULL
  2782. count:0];
  2783. if (self) {
  2784. if (dictionary) {
  2785. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2786. }
  2787. }
  2788. return self;
  2789. }
  2790. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2791. capacity:(NSUInteger)numItems {
  2792. #pragma unused(numItems)
  2793. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2794. }
  2795. - (void)dealloc {
  2796. NSAssert(!_autocreator,
  2797. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2798. [self class], _autocreator);
  2799. [_dictionary release];
  2800. [super dealloc];
  2801. }
  2802. - (instancetype)copyWithZone:(NSZone *)zone {
  2803. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2804. }
  2805. - (BOOL)isEqual:(id)other {
  2806. if (self == other) {
  2807. return YES;
  2808. }
  2809. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2810. return NO;
  2811. }
  2812. GPBUInt32EnumDictionary *otherDictionary = other;
  2813. return [_dictionary isEqual:otherDictionary->_dictionary];
  2814. }
  2815. - (NSUInteger)hash {
  2816. return _dictionary.count;
  2817. }
  2818. - (NSString *)description {
  2819. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2820. }
  2821. - (NSUInteger)count {
  2822. return _dictionary.count;
  2823. }
  2824. - (void)enumerateKeysAndRawValuesUsingBlock:
  2825. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2826. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2827. NSNumber *aValue,
  2828. BOOL *stop) {
  2829. block([aKey unsignedIntValue], [aValue intValue], stop);
  2830. }];
  2831. }
  2832. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2833. NSUInteger count = _dictionary.count;
  2834. if (count == 0) {
  2835. return 0;
  2836. }
  2837. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2838. GPBDataType keyDataType = field.mapKeyDataType;
  2839. __block size_t result = 0;
  2840. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2841. NSNumber *aValue,
  2842. BOOL *stop) {
  2843. #pragma unused(stop)
  2844. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2845. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2846. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2847. }];
  2848. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2849. result += tagSize * count;
  2850. return result;
  2851. }
  2852. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2853. asField:(GPBFieldDescriptor *)field {
  2854. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2855. GPBDataType keyDataType = field.mapKeyDataType;
  2856. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2857. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2858. NSNumber *aValue,
  2859. BOOL *stop) {
  2860. #pragma unused(stop)
  2861. // Write the tag.
  2862. [outputStream writeInt32NoTag:tag];
  2863. // Write the size of the message.
  2864. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2865. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2866. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2867. // Write the fields.
  2868. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2869. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  2870. }];
  2871. }
  2872. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2873. forKey:(GPBGenericValue *)key
  2874. keyDataType:(GPBDataType)keyDataType {
  2875. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2876. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2877. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2878. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2879. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2880. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2881. [outputStream release];
  2882. return data;
  2883. }
  2884. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2885. forGPBGenericValueKey:(GPBGenericValue *)key {
  2886. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2887. }
  2888. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  2889. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2890. #pragma unused(stop)
  2891. block([NSString stringWithFormat:@"%u", key], @(value));
  2892. }];
  2893. }
  2894. - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
  2895. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2896. if (wrapped && value) {
  2897. int32_t result = [wrapped intValue];
  2898. if (!_validationFunc(result)) {
  2899. result = kGPBUnrecognizedEnumeratorValue;
  2900. }
  2901. *value = result;
  2902. }
  2903. return (wrapped != NULL);
  2904. }
  2905. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
  2906. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2907. if (wrapped && rawValue) {
  2908. *rawValue = [wrapped intValue];
  2909. }
  2910. return (wrapped != NULL);
  2911. }
  2912. - (void)enumerateKeysAndEnumsUsingBlock:
  2913. (void (^)(uint32_t key, int32_t value, BOOL *stop))block {
  2914. GPBEnumValidationFunc func = _validationFunc;
  2915. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  2916. NSNumber *aValue,
  2917. BOOL *stop) {
  2918. int32_t unwrapped = [aValue intValue];
  2919. if (!func(unwrapped)) {
  2920. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2921. }
  2922. block([aKey unsignedIntValue], unwrapped, stop);
  2923. }];
  2924. }
  2925. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2926. if (otherDictionary) {
  2927. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2928. if (_autocreator) {
  2929. GPBAutocreatedDictionaryModified(_autocreator, self);
  2930. }
  2931. }
  2932. }
  2933. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2934. [_dictionary setObject:@(value) forKey:@(key)];
  2935. if (_autocreator) {
  2936. GPBAutocreatedDictionaryModified(_autocreator, self);
  2937. }
  2938. }
  2939. - (void)removeEnumForKey:(uint32_t)aKey {
  2940. [_dictionary removeObjectForKey:@(aKey)];
  2941. }
  2942. - (void)removeAll {
  2943. [_dictionary removeAllObjects];
  2944. }
  2945. - (void)setEnum:(int32_t)value forKey:(uint32_t)key {
  2946. if (!_validationFunc(value)) {
  2947. [NSException raise:NSInvalidArgumentException
  2948. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2949. value];
  2950. }
  2951. [_dictionary setObject:@(value) forKey:@(key)];
  2952. if (_autocreator) {
  2953. GPBAutocreatedDictionaryModified(_autocreator, self);
  2954. }
  2955. }
  2956. @end
  2957. #pragma mark - UInt32 -> Object
  2958. @implementation GPBUInt32ObjectDictionary {
  2959. @package
  2960. NSMutableDictionary *_dictionary;
  2961. }
  2962. + (instancetype)dictionary {
  2963. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  2964. }
  2965. + (instancetype)dictionaryWithObject:(id)object
  2966. forKey:(uint32_t)key {
  2967. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2968. // on to get the type correct.
  2969. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  2970. forKeys:&key
  2971. count:1] autorelease];
  2972. }
  2973. + (instancetype)dictionaryWithObjects:(const id [])objects
  2974. forKeys:(const uint32_t [])keys
  2975. count:(NSUInteger)count {
  2976. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  2977. // on to get the type correct.
  2978. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  2979. forKeys:keys
  2980. count:count] autorelease];
  2981. }
  2982. + (instancetype)dictionaryWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2983. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  2984. // on to get the type correct.
  2985. return [[(GPBUInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  2986. }
  2987. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  2988. return [[[self alloc] initWithCapacity:numItems] autorelease];
  2989. }
  2990. - (instancetype)init {
  2991. return [self initWithObjects:NULL forKeys:NULL count:0];
  2992. }
  2993. - (instancetype)initWithObjects:(const id [])objects
  2994. forKeys:(const uint32_t [])keys
  2995. count:(NSUInteger)count {
  2996. self = [super init];
  2997. if (self) {
  2998. _dictionary = [[NSMutableDictionary alloc] init];
  2999. if (count && objects && keys) {
  3000. for (NSUInteger i = 0; i < count; ++i) {
  3001. if (!objects[i]) {
  3002. [NSException raise:NSInvalidArgumentException
  3003. format:@"Attempting to add nil object to a Dictionary"];
  3004. }
  3005. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  3006. }
  3007. }
  3008. }
  3009. return self;
  3010. }
  3011. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  3012. self = [self initWithObjects:NULL forKeys:NULL count:0];
  3013. if (self) {
  3014. if (dictionary) {
  3015. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3016. }
  3017. }
  3018. return self;
  3019. }
  3020. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3021. #pragma unused(numItems)
  3022. return [self initWithObjects:NULL forKeys:NULL count:0];
  3023. }
  3024. - (void)dealloc {
  3025. NSAssert(!_autocreator,
  3026. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3027. [self class], _autocreator);
  3028. [_dictionary release];
  3029. [super dealloc];
  3030. }
  3031. - (instancetype)copyWithZone:(NSZone *)zone {
  3032. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  3033. }
  3034. - (BOOL)isEqual:(id)other {
  3035. if (self == other) {
  3036. return YES;
  3037. }
  3038. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  3039. return NO;
  3040. }
  3041. GPBUInt32ObjectDictionary *otherDictionary = other;
  3042. return [_dictionary isEqual:otherDictionary->_dictionary];
  3043. }
  3044. - (NSUInteger)hash {
  3045. return _dictionary.count;
  3046. }
  3047. - (NSString *)description {
  3048. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3049. }
  3050. - (NSUInteger)count {
  3051. return _dictionary.count;
  3052. }
  3053. - (void)enumerateKeysAndObjectsUsingBlock:
  3054. (void (^)(uint32_t key, id object, BOOL *stop))block {
  3055. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3056. id aObject,
  3057. BOOL *stop) {
  3058. block([aKey unsignedIntValue], aObject, stop);
  3059. }];
  3060. }
  3061. - (BOOL)isInitialized {
  3062. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  3063. if (!msg.initialized) {
  3064. return NO;
  3065. }
  3066. }
  3067. return YES;
  3068. }
  3069. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  3070. GPBUInt32ObjectDictionary *newDict =
  3071. [[GPBUInt32ObjectDictionary alloc] init];
  3072. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  3073. GPBMessage *msg,
  3074. BOOL *stop) {
  3075. #pragma unused(stop)
  3076. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  3077. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  3078. [copiedMsg release];
  3079. }];
  3080. return newDict;
  3081. }
  3082. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3083. NSUInteger count = _dictionary.count;
  3084. if (count == 0) {
  3085. return 0;
  3086. }
  3087. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3088. GPBDataType keyDataType = field.mapKeyDataType;
  3089. __block size_t result = 0;
  3090. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3091. id aObject,
  3092. BOOL *stop) {
  3093. #pragma unused(stop)
  3094. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3095. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3096. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3097. }];
  3098. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3099. result += tagSize * count;
  3100. return result;
  3101. }
  3102. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3103. asField:(GPBFieldDescriptor *)field {
  3104. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3105. GPBDataType keyDataType = field.mapKeyDataType;
  3106. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3107. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3108. id aObject,
  3109. BOOL *stop) {
  3110. #pragma unused(stop)
  3111. // Write the tag.
  3112. [outputStream writeInt32NoTag:tag];
  3113. // Write the size of the message.
  3114. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3115. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  3116. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3117. // Write the fields.
  3118. WriteDictUInt32Field(outputStream, [aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  3119. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  3120. }];
  3121. }
  3122. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3123. forGPBGenericValueKey:(GPBGenericValue *)key {
  3124. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  3125. }
  3126. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3127. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  3128. #pragma unused(stop)
  3129. block([NSString stringWithFormat:@"%u", key], object);
  3130. }];
  3131. }
  3132. - (id)objectForKey:(uint32_t)key {
  3133. id result = [_dictionary objectForKey:@(key)];
  3134. return result;
  3135. }
  3136. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  3137. if (otherDictionary) {
  3138. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3139. if (_autocreator) {
  3140. GPBAutocreatedDictionaryModified(_autocreator, self);
  3141. }
  3142. }
  3143. }
  3144. - (void)setObject:(id)object forKey:(uint32_t)key {
  3145. if (!object) {
  3146. [NSException raise:NSInvalidArgumentException
  3147. format:@"Attempting to add nil object to a Dictionary"];
  3148. }
  3149. [_dictionary setObject:object forKey:@(key)];
  3150. if (_autocreator) {
  3151. GPBAutocreatedDictionaryModified(_autocreator, self);
  3152. }
  3153. }
  3154. - (void)removeObjectForKey:(uint32_t)aKey {
  3155. [_dictionary removeObjectForKey:@(aKey)];
  3156. }
  3157. - (void)removeAll {
  3158. [_dictionary removeAllObjects];
  3159. }
  3160. @end
  3161. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  3162. // This block of code is generated, do not edit it directly.
  3163. #pragma mark - Int32 -> UInt32
  3164. @implementation GPBInt32UInt32Dictionary {
  3165. @package
  3166. NSMutableDictionary *_dictionary;
  3167. }
  3168. + (instancetype)dictionary {
  3169. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  3170. }
  3171. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  3172. forKey:(int32_t)key {
  3173. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  3174. // on to get the type correct.
  3175. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  3176. forKeys:&key
  3177. count:1] autorelease];
  3178. }
  3179. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  3180. forKeys:(const int32_t [])keys
  3181. count:(NSUInteger)count {
  3182. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  3183. // on to get the type correct.
  3184. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithUInt32s:values
  3185. forKeys:keys
  3186. count:count] autorelease];
  3187. }
  3188. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3189. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3190. // on to get the type correct.
  3191. return [[(GPBInt32UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3192. }
  3193. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3194. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3195. }
  3196. - (instancetype)init {
  3197. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  3198. }
  3199. - (instancetype)initWithUInt32s:(const uint32_t [])values
  3200. forKeys:(const int32_t [])keys
  3201. count:(NSUInteger)count {
  3202. self = [super init];
  3203. if (self) {
  3204. _dictionary = [[NSMutableDictionary alloc] init];
  3205. if (count && values && keys) {
  3206. for (NSUInteger i = 0; i < count; ++i) {
  3207. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3208. }
  3209. }
  3210. }
  3211. return self;
  3212. }
  3213. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  3214. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  3215. if (self) {
  3216. if (dictionary) {
  3217. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3218. }
  3219. }
  3220. return self;
  3221. }
  3222. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3223. #pragma unused(numItems)
  3224. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  3225. }
  3226. - (void)dealloc {
  3227. NSAssert(!_autocreator,
  3228. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3229. [self class], _autocreator);
  3230. [_dictionary release];
  3231. [super dealloc];
  3232. }
  3233. - (instancetype)copyWithZone:(NSZone *)zone {
  3234. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  3235. }
  3236. - (BOOL)isEqual:(id)other {
  3237. if (self == other) {
  3238. return YES;
  3239. }
  3240. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  3241. return NO;
  3242. }
  3243. GPBInt32UInt32Dictionary *otherDictionary = other;
  3244. return [_dictionary isEqual:otherDictionary->_dictionary];
  3245. }
  3246. - (NSUInteger)hash {
  3247. return _dictionary.count;
  3248. }
  3249. - (NSString *)description {
  3250. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3251. }
  3252. - (NSUInteger)count {
  3253. return _dictionary.count;
  3254. }
  3255. - (void)enumerateKeysAndUInt32sUsingBlock:
  3256. (void (^)(int32_t key, uint32_t value, BOOL *stop))block {
  3257. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3258. NSNumber *aValue,
  3259. BOOL *stop) {
  3260. block([aKey intValue], [aValue unsignedIntValue], stop);
  3261. }];
  3262. }
  3263. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3264. NSUInteger count = _dictionary.count;
  3265. if (count == 0) {
  3266. return 0;
  3267. }
  3268. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3269. GPBDataType keyDataType = field.mapKeyDataType;
  3270. __block size_t result = 0;
  3271. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3272. NSNumber *aValue,
  3273. BOOL *stop) {
  3274. #pragma unused(stop)
  3275. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3276. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3277. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3278. }];
  3279. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3280. result += tagSize * count;
  3281. return result;
  3282. }
  3283. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3284. asField:(GPBFieldDescriptor *)field {
  3285. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3286. GPBDataType keyDataType = field.mapKeyDataType;
  3287. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3288. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3289. NSNumber *aValue,
  3290. BOOL *stop) {
  3291. #pragma unused(stop)
  3292. // Write the tag.
  3293. [outputStream writeInt32NoTag:tag];
  3294. // Write the size of the message.
  3295. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3296. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3297. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3298. // Write the fields.
  3299. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3300. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  3301. }];
  3302. }
  3303. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3304. forGPBGenericValueKey:(GPBGenericValue *)key {
  3305. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3306. }
  3307. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3308. [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3309. #pragma unused(stop)
  3310. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3311. }];
  3312. }
  3313. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
  3314. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3315. if (wrapped && value) {
  3316. *value = [wrapped unsignedIntValue];
  3317. }
  3318. return (wrapped != NULL);
  3319. }
  3320. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3321. if (otherDictionary) {
  3322. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3323. if (_autocreator) {
  3324. GPBAutocreatedDictionaryModified(_autocreator, self);
  3325. }
  3326. }
  3327. }
  3328. - (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
  3329. [_dictionary setObject:@(value) forKey:@(key)];
  3330. if (_autocreator) {
  3331. GPBAutocreatedDictionaryModified(_autocreator, self);
  3332. }
  3333. }
  3334. - (void)removeUInt32ForKey:(int32_t)aKey {
  3335. [_dictionary removeObjectForKey:@(aKey)];
  3336. }
  3337. - (void)removeAll {
  3338. [_dictionary removeAllObjects];
  3339. }
  3340. @end
  3341. #pragma mark - Int32 -> Int32
  3342. @implementation GPBInt32Int32Dictionary {
  3343. @package
  3344. NSMutableDictionary *_dictionary;
  3345. }
  3346. + (instancetype)dictionary {
  3347. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  3348. }
  3349. + (instancetype)dictionaryWithInt32:(int32_t)value
  3350. forKey:(int32_t)key {
  3351. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  3352. // on to get the type correct.
  3353. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:&value
  3354. forKeys:&key
  3355. count:1] autorelease];
  3356. }
  3357. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  3358. forKeys:(const int32_t [])keys
  3359. count:(NSUInteger)count {
  3360. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  3361. // on to get the type correct.
  3362. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithInt32s:values
  3363. forKeys:keys
  3364. count:count] autorelease];
  3365. }
  3366. + (instancetype)dictionaryWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3367. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3368. // on to get the type correct.
  3369. return [[(GPBInt32Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3370. }
  3371. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3372. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3373. }
  3374. - (instancetype)init {
  3375. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3376. }
  3377. - (instancetype)initWithInt32s:(const int32_t [])values
  3378. forKeys:(const int32_t [])keys
  3379. count:(NSUInteger)count {
  3380. self = [super init];
  3381. if (self) {
  3382. _dictionary = [[NSMutableDictionary alloc] init];
  3383. if (count && values && keys) {
  3384. for (NSUInteger i = 0; i < count; ++i) {
  3385. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3386. }
  3387. }
  3388. }
  3389. return self;
  3390. }
  3391. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3392. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  3393. if (self) {
  3394. if (dictionary) {
  3395. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3396. }
  3397. }
  3398. return self;
  3399. }
  3400. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3401. #pragma unused(numItems)
  3402. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3403. }
  3404. - (void)dealloc {
  3405. NSAssert(!_autocreator,
  3406. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3407. [self class], _autocreator);
  3408. [_dictionary release];
  3409. [super dealloc];
  3410. }
  3411. - (instancetype)copyWithZone:(NSZone *)zone {
  3412. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3413. }
  3414. - (BOOL)isEqual:(id)other {
  3415. if (self == other) {
  3416. return YES;
  3417. }
  3418. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3419. return NO;
  3420. }
  3421. GPBInt32Int32Dictionary *otherDictionary = other;
  3422. return [_dictionary isEqual:otherDictionary->_dictionary];
  3423. }
  3424. - (NSUInteger)hash {
  3425. return _dictionary.count;
  3426. }
  3427. - (NSString *)description {
  3428. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3429. }
  3430. - (NSUInteger)count {
  3431. return _dictionary.count;
  3432. }
  3433. - (void)enumerateKeysAndInt32sUsingBlock:
  3434. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  3435. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3436. NSNumber *aValue,
  3437. BOOL *stop) {
  3438. block([aKey intValue], [aValue intValue], stop);
  3439. }];
  3440. }
  3441. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3442. NSUInteger count = _dictionary.count;
  3443. if (count == 0) {
  3444. return 0;
  3445. }
  3446. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3447. GPBDataType keyDataType = field.mapKeyDataType;
  3448. __block size_t result = 0;
  3449. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3450. NSNumber *aValue,
  3451. BOOL *stop) {
  3452. #pragma unused(stop)
  3453. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3454. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3455. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3456. }];
  3457. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3458. result += tagSize * count;
  3459. return result;
  3460. }
  3461. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3462. asField:(GPBFieldDescriptor *)field {
  3463. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3464. GPBDataType keyDataType = field.mapKeyDataType;
  3465. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3466. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3467. NSNumber *aValue,
  3468. BOOL *stop) {
  3469. #pragma unused(stop)
  3470. // Write the tag.
  3471. [outputStream writeInt32NoTag:tag];
  3472. // Write the size of the message.
  3473. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3474. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3475. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3476. // Write the fields.
  3477. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3478. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  3479. }];
  3480. }
  3481. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3482. forGPBGenericValueKey:(GPBGenericValue *)key {
  3483. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3484. }
  3485. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3486. [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3487. #pragma unused(stop)
  3488. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3489. }];
  3490. }
  3491. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
  3492. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3493. if (wrapped && value) {
  3494. *value = [wrapped intValue];
  3495. }
  3496. return (wrapped != NULL);
  3497. }
  3498. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3499. if (otherDictionary) {
  3500. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3501. if (_autocreator) {
  3502. GPBAutocreatedDictionaryModified(_autocreator, self);
  3503. }
  3504. }
  3505. }
  3506. - (void)setInt32:(int32_t)value forKey:(int32_t)key {
  3507. [_dictionary setObject:@(value) forKey:@(key)];
  3508. if (_autocreator) {
  3509. GPBAutocreatedDictionaryModified(_autocreator, self);
  3510. }
  3511. }
  3512. - (void)removeInt32ForKey:(int32_t)aKey {
  3513. [_dictionary removeObjectForKey:@(aKey)];
  3514. }
  3515. - (void)removeAll {
  3516. [_dictionary removeAllObjects];
  3517. }
  3518. @end
  3519. #pragma mark - Int32 -> UInt64
  3520. @implementation GPBInt32UInt64Dictionary {
  3521. @package
  3522. NSMutableDictionary *_dictionary;
  3523. }
  3524. + (instancetype)dictionary {
  3525. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  3526. }
  3527. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  3528. forKey:(int32_t)key {
  3529. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  3530. // on to get the type correct.
  3531. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  3532. forKeys:&key
  3533. count:1] autorelease];
  3534. }
  3535. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  3536. forKeys:(const int32_t [])keys
  3537. count:(NSUInteger)count {
  3538. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  3539. // on to get the type correct.
  3540. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithUInt64s:values
  3541. forKeys:keys
  3542. count:count] autorelease];
  3543. }
  3544. + (instancetype)dictionaryWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3545. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3546. // on to get the type correct.
  3547. return [[(GPBInt32UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3548. }
  3549. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3550. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3551. }
  3552. - (instancetype)init {
  3553. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3554. }
  3555. - (instancetype)initWithUInt64s:(const uint64_t [])values
  3556. forKeys:(const int32_t [])keys
  3557. count:(NSUInteger)count {
  3558. self = [super init];
  3559. if (self) {
  3560. _dictionary = [[NSMutableDictionary alloc] init];
  3561. if (count && values && keys) {
  3562. for (NSUInteger i = 0; i < count; ++i) {
  3563. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3564. }
  3565. }
  3566. }
  3567. return self;
  3568. }
  3569. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3570. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  3571. if (self) {
  3572. if (dictionary) {
  3573. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3574. }
  3575. }
  3576. return self;
  3577. }
  3578. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3579. #pragma unused(numItems)
  3580. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3581. }
  3582. - (void)dealloc {
  3583. NSAssert(!_autocreator,
  3584. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3585. [self class], _autocreator);
  3586. [_dictionary release];
  3587. [super dealloc];
  3588. }
  3589. - (instancetype)copyWithZone:(NSZone *)zone {
  3590. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3591. }
  3592. - (BOOL)isEqual:(id)other {
  3593. if (self == other) {
  3594. return YES;
  3595. }
  3596. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3597. return NO;
  3598. }
  3599. GPBInt32UInt64Dictionary *otherDictionary = other;
  3600. return [_dictionary isEqual:otherDictionary->_dictionary];
  3601. }
  3602. - (NSUInteger)hash {
  3603. return _dictionary.count;
  3604. }
  3605. - (NSString *)description {
  3606. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3607. }
  3608. - (NSUInteger)count {
  3609. return _dictionary.count;
  3610. }
  3611. - (void)enumerateKeysAndUInt64sUsingBlock:
  3612. (void (^)(int32_t key, uint64_t value, BOOL *stop))block {
  3613. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3614. NSNumber *aValue,
  3615. BOOL *stop) {
  3616. block([aKey intValue], [aValue unsignedLongLongValue], stop);
  3617. }];
  3618. }
  3619. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3620. NSUInteger count = _dictionary.count;
  3621. if (count == 0) {
  3622. return 0;
  3623. }
  3624. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3625. GPBDataType keyDataType = field.mapKeyDataType;
  3626. __block size_t result = 0;
  3627. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3628. NSNumber *aValue,
  3629. BOOL *stop) {
  3630. #pragma unused(stop)
  3631. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3632. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3633. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3634. }];
  3635. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3636. result += tagSize * count;
  3637. return result;
  3638. }
  3639. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3640. asField:(GPBFieldDescriptor *)field {
  3641. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3642. GPBDataType keyDataType = field.mapKeyDataType;
  3643. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3644. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3645. NSNumber *aValue,
  3646. BOOL *stop) {
  3647. #pragma unused(stop)
  3648. // Write the tag.
  3649. [outputStream writeInt32NoTag:tag];
  3650. // Write the size of the message.
  3651. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3652. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3653. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3654. // Write the fields.
  3655. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3656. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3657. }];
  3658. }
  3659. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3660. forGPBGenericValueKey:(GPBGenericValue *)key {
  3661. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3662. }
  3663. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3664. [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3665. #pragma unused(stop)
  3666. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3667. }];
  3668. }
  3669. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
  3670. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3671. if (wrapped && value) {
  3672. *value = [wrapped unsignedLongLongValue];
  3673. }
  3674. return (wrapped != NULL);
  3675. }
  3676. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3677. if (otherDictionary) {
  3678. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3679. if (_autocreator) {
  3680. GPBAutocreatedDictionaryModified(_autocreator, self);
  3681. }
  3682. }
  3683. }
  3684. - (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
  3685. [_dictionary setObject:@(value) forKey:@(key)];
  3686. if (_autocreator) {
  3687. GPBAutocreatedDictionaryModified(_autocreator, self);
  3688. }
  3689. }
  3690. - (void)removeUInt64ForKey:(int32_t)aKey {
  3691. [_dictionary removeObjectForKey:@(aKey)];
  3692. }
  3693. - (void)removeAll {
  3694. [_dictionary removeAllObjects];
  3695. }
  3696. @end
  3697. #pragma mark - Int32 -> Int64
  3698. @implementation GPBInt32Int64Dictionary {
  3699. @package
  3700. NSMutableDictionary *_dictionary;
  3701. }
  3702. + (instancetype)dictionary {
  3703. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  3704. }
  3705. + (instancetype)dictionaryWithInt64:(int64_t)value
  3706. forKey:(int32_t)key {
  3707. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  3708. // on to get the type correct.
  3709. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:&value
  3710. forKeys:&key
  3711. count:1] autorelease];
  3712. }
  3713. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  3714. forKeys:(const int32_t [])keys
  3715. count:(NSUInteger)count {
  3716. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  3717. // on to get the type correct.
  3718. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithInt64s:values
  3719. forKeys:keys
  3720. count:count] autorelease];
  3721. }
  3722. + (instancetype)dictionaryWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3723. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3724. // on to get the type correct.
  3725. return [[(GPBInt32Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3726. }
  3727. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3728. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3729. }
  3730. - (instancetype)init {
  3731. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3732. }
  3733. - (instancetype)initWithInt64s:(const int64_t [])values
  3734. forKeys:(const int32_t [])keys
  3735. count:(NSUInteger)count {
  3736. self = [super init];
  3737. if (self) {
  3738. _dictionary = [[NSMutableDictionary alloc] init];
  3739. if (count && values && keys) {
  3740. for (NSUInteger i = 0; i < count; ++i) {
  3741. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3742. }
  3743. }
  3744. }
  3745. return self;
  3746. }
  3747. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3748. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  3749. if (self) {
  3750. if (dictionary) {
  3751. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3752. }
  3753. }
  3754. return self;
  3755. }
  3756. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3757. #pragma unused(numItems)
  3758. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3759. }
  3760. - (void)dealloc {
  3761. NSAssert(!_autocreator,
  3762. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3763. [self class], _autocreator);
  3764. [_dictionary release];
  3765. [super dealloc];
  3766. }
  3767. - (instancetype)copyWithZone:(NSZone *)zone {
  3768. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3769. }
  3770. - (BOOL)isEqual:(id)other {
  3771. if (self == other) {
  3772. return YES;
  3773. }
  3774. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3775. return NO;
  3776. }
  3777. GPBInt32Int64Dictionary *otherDictionary = other;
  3778. return [_dictionary isEqual:otherDictionary->_dictionary];
  3779. }
  3780. - (NSUInteger)hash {
  3781. return _dictionary.count;
  3782. }
  3783. - (NSString *)description {
  3784. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3785. }
  3786. - (NSUInteger)count {
  3787. return _dictionary.count;
  3788. }
  3789. - (void)enumerateKeysAndInt64sUsingBlock:
  3790. (void (^)(int32_t key, int64_t value, BOOL *stop))block {
  3791. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3792. NSNumber *aValue,
  3793. BOOL *stop) {
  3794. block([aKey intValue], [aValue longLongValue], stop);
  3795. }];
  3796. }
  3797. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3798. NSUInteger count = _dictionary.count;
  3799. if (count == 0) {
  3800. return 0;
  3801. }
  3802. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3803. GPBDataType keyDataType = field.mapKeyDataType;
  3804. __block size_t result = 0;
  3805. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3806. NSNumber *aValue,
  3807. BOOL *stop) {
  3808. #pragma unused(stop)
  3809. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3810. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3811. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3812. }];
  3813. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3814. result += tagSize * count;
  3815. return result;
  3816. }
  3817. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3818. asField:(GPBFieldDescriptor *)field {
  3819. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3820. GPBDataType keyDataType = field.mapKeyDataType;
  3821. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3822. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3823. NSNumber *aValue,
  3824. BOOL *stop) {
  3825. #pragma unused(stop)
  3826. // Write the tag.
  3827. [outputStream writeInt32NoTag:tag];
  3828. // Write the size of the message.
  3829. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3830. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3831. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3832. // Write the fields.
  3833. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  3834. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3835. }];
  3836. }
  3837. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3838. forGPBGenericValueKey:(GPBGenericValue *)key {
  3839. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3840. }
  3841. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  3842. [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3843. #pragma unused(stop)
  3844. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3845. }];
  3846. }
  3847. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
  3848. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3849. if (wrapped && value) {
  3850. *value = [wrapped longLongValue];
  3851. }
  3852. return (wrapped != NULL);
  3853. }
  3854. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3855. if (otherDictionary) {
  3856. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3857. if (_autocreator) {
  3858. GPBAutocreatedDictionaryModified(_autocreator, self);
  3859. }
  3860. }
  3861. }
  3862. - (void)setInt64:(int64_t)value forKey:(int32_t)key {
  3863. [_dictionary setObject:@(value) forKey:@(key)];
  3864. if (_autocreator) {
  3865. GPBAutocreatedDictionaryModified(_autocreator, self);
  3866. }
  3867. }
  3868. - (void)removeInt64ForKey:(int32_t)aKey {
  3869. [_dictionary removeObjectForKey:@(aKey)];
  3870. }
  3871. - (void)removeAll {
  3872. [_dictionary removeAllObjects];
  3873. }
  3874. @end
  3875. #pragma mark - Int32 -> Bool
  3876. @implementation GPBInt32BoolDictionary {
  3877. @package
  3878. NSMutableDictionary *_dictionary;
  3879. }
  3880. + (instancetype)dictionary {
  3881. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  3882. }
  3883. + (instancetype)dictionaryWithBool:(BOOL)value
  3884. forKey:(int32_t)key {
  3885. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  3886. // on to get the type correct.
  3887. return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:&value
  3888. forKeys:&key
  3889. count:1] autorelease];
  3890. }
  3891. + (instancetype)dictionaryWithBools:(const BOOL [])values
  3892. forKeys:(const int32_t [])keys
  3893. count:(NSUInteger)count {
  3894. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  3895. // on to get the type correct.
  3896. return [[(GPBInt32BoolDictionary*)[self alloc] initWithBools:values
  3897. forKeys:keys
  3898. count:count] autorelease];
  3899. }
  3900. + (instancetype)dictionaryWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3901. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  3902. // on to get the type correct.
  3903. return [[(GPBInt32BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  3904. }
  3905. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  3906. return [[[self alloc] initWithCapacity:numItems] autorelease];
  3907. }
  3908. - (instancetype)init {
  3909. return [self initWithBools:NULL forKeys:NULL count:0];
  3910. }
  3911. - (instancetype)initWithBools:(const BOOL [])values
  3912. forKeys:(const int32_t [])keys
  3913. count:(NSUInteger)count {
  3914. self = [super init];
  3915. if (self) {
  3916. _dictionary = [[NSMutableDictionary alloc] init];
  3917. if (count && values && keys) {
  3918. for (NSUInteger i = 0; i < count; ++i) {
  3919. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3920. }
  3921. }
  3922. }
  3923. return self;
  3924. }
  3925. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3926. self = [self initWithBools:NULL forKeys:NULL count:0];
  3927. if (self) {
  3928. if (dictionary) {
  3929. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3930. }
  3931. }
  3932. return self;
  3933. }
  3934. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3935. #pragma unused(numItems)
  3936. return [self initWithBools:NULL forKeys:NULL count:0];
  3937. }
  3938. - (void)dealloc {
  3939. NSAssert(!_autocreator,
  3940. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3941. [self class], _autocreator);
  3942. [_dictionary release];
  3943. [super dealloc];
  3944. }
  3945. - (instancetype)copyWithZone:(NSZone *)zone {
  3946. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3947. }
  3948. - (BOOL)isEqual:(id)other {
  3949. if (self == other) {
  3950. return YES;
  3951. }
  3952. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3953. return NO;
  3954. }
  3955. GPBInt32BoolDictionary *otherDictionary = other;
  3956. return [_dictionary isEqual:otherDictionary->_dictionary];
  3957. }
  3958. - (NSUInteger)hash {
  3959. return _dictionary.count;
  3960. }
  3961. - (NSString *)description {
  3962. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3963. }
  3964. - (NSUInteger)count {
  3965. return _dictionary.count;
  3966. }
  3967. - (void)enumerateKeysAndBoolsUsingBlock:
  3968. (void (^)(int32_t key, BOOL value, BOOL *stop))block {
  3969. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3970. NSNumber *aValue,
  3971. BOOL *stop) {
  3972. block([aKey intValue], [aValue boolValue], stop);
  3973. }];
  3974. }
  3975. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3976. NSUInteger count = _dictionary.count;
  3977. if (count == 0) {
  3978. return 0;
  3979. }
  3980. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3981. GPBDataType keyDataType = field.mapKeyDataType;
  3982. __block size_t result = 0;
  3983. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  3984. NSNumber *aValue,
  3985. BOOL *stop) {
  3986. #pragma unused(stop)
  3987. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3988. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3989. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3990. }];
  3991. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3992. result += tagSize * count;
  3993. return result;
  3994. }
  3995. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3996. asField:(GPBFieldDescriptor *)field {
  3997. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3998. GPBDataType keyDataType = field.mapKeyDataType;
  3999. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4000. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4001. NSNumber *aValue,
  4002. BOOL *stop) {
  4003. #pragma unused(stop)
  4004. // Write the tag.
  4005. [outputStream writeInt32NoTag:tag];
  4006. // Write the size of the message.
  4007. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4008. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  4009. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4010. // Write the fields.
  4011. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4012. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  4013. }];
  4014. }
  4015. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4016. forGPBGenericValueKey:(GPBGenericValue *)key {
  4017. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  4018. }
  4019. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4020. [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  4021. #pragma unused(stop)
  4022. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  4023. }];
  4024. }
  4025. - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
  4026. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4027. if (wrapped && value) {
  4028. *value = [wrapped boolValue];
  4029. }
  4030. return (wrapped != NULL);
  4031. }
  4032. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  4033. if (otherDictionary) {
  4034. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4035. if (_autocreator) {
  4036. GPBAutocreatedDictionaryModified(_autocreator, self);
  4037. }
  4038. }
  4039. }
  4040. - (void)setBool:(BOOL)value forKey:(int32_t)key {
  4041. [_dictionary setObject:@(value) forKey:@(key)];
  4042. if (_autocreator) {
  4043. GPBAutocreatedDictionaryModified(_autocreator, self);
  4044. }
  4045. }
  4046. - (void)removeBoolForKey:(int32_t)aKey {
  4047. [_dictionary removeObjectForKey:@(aKey)];
  4048. }
  4049. - (void)removeAll {
  4050. [_dictionary removeAllObjects];
  4051. }
  4052. @end
  4053. #pragma mark - Int32 -> Float
  4054. @implementation GPBInt32FloatDictionary {
  4055. @package
  4056. NSMutableDictionary *_dictionary;
  4057. }
  4058. + (instancetype)dictionary {
  4059. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  4060. }
  4061. + (instancetype)dictionaryWithFloat:(float)value
  4062. forKey:(int32_t)key {
  4063. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  4064. // on to get the type correct.
  4065. return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:&value
  4066. forKeys:&key
  4067. count:1] autorelease];
  4068. }
  4069. + (instancetype)dictionaryWithFloats:(const float [])values
  4070. forKeys:(const int32_t [])keys
  4071. count:(NSUInteger)count {
  4072. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  4073. // on to get the type correct.
  4074. return [[(GPBInt32FloatDictionary*)[self alloc] initWithFloats:values
  4075. forKeys:keys
  4076. count:count] autorelease];
  4077. }
  4078. + (instancetype)dictionaryWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4079. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4080. // on to get the type correct.
  4081. return [[(GPBInt32FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4082. }
  4083. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4084. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4085. }
  4086. - (instancetype)init {
  4087. return [self initWithFloats:NULL forKeys:NULL count:0];
  4088. }
  4089. - (instancetype)initWithFloats:(const float [])values
  4090. forKeys:(const int32_t [])keys
  4091. count:(NSUInteger)count {
  4092. self = [super init];
  4093. if (self) {
  4094. _dictionary = [[NSMutableDictionary alloc] init];
  4095. if (count && values && keys) {
  4096. for (NSUInteger i = 0; i < count; ++i) {
  4097. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4098. }
  4099. }
  4100. }
  4101. return self;
  4102. }
  4103. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  4104. self = [self initWithFloats:NULL forKeys:NULL count:0];
  4105. if (self) {
  4106. if (dictionary) {
  4107. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4108. }
  4109. }
  4110. return self;
  4111. }
  4112. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4113. #pragma unused(numItems)
  4114. return [self initWithFloats:NULL forKeys:NULL count:0];
  4115. }
  4116. - (void)dealloc {
  4117. NSAssert(!_autocreator,
  4118. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4119. [self class], _autocreator);
  4120. [_dictionary release];
  4121. [super dealloc];
  4122. }
  4123. - (instancetype)copyWithZone:(NSZone *)zone {
  4124. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  4125. }
  4126. - (BOOL)isEqual:(id)other {
  4127. if (self == other) {
  4128. return YES;
  4129. }
  4130. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  4131. return NO;
  4132. }
  4133. GPBInt32FloatDictionary *otherDictionary = other;
  4134. return [_dictionary isEqual:otherDictionary->_dictionary];
  4135. }
  4136. - (NSUInteger)hash {
  4137. return _dictionary.count;
  4138. }
  4139. - (NSString *)description {
  4140. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4141. }
  4142. - (NSUInteger)count {
  4143. return _dictionary.count;
  4144. }
  4145. - (void)enumerateKeysAndFloatsUsingBlock:
  4146. (void (^)(int32_t key, float value, BOOL *stop))block {
  4147. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4148. NSNumber *aValue,
  4149. BOOL *stop) {
  4150. block([aKey intValue], [aValue floatValue], stop);
  4151. }];
  4152. }
  4153. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4154. NSUInteger count = _dictionary.count;
  4155. if (count == 0) {
  4156. return 0;
  4157. }
  4158. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4159. GPBDataType keyDataType = field.mapKeyDataType;
  4160. __block size_t result = 0;
  4161. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4162. NSNumber *aValue,
  4163. BOOL *stop) {
  4164. #pragma unused(stop)
  4165. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4166. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4167. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4168. }];
  4169. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4170. result += tagSize * count;
  4171. return result;
  4172. }
  4173. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4174. asField:(GPBFieldDescriptor *)field {
  4175. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4176. GPBDataType keyDataType = field.mapKeyDataType;
  4177. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4178. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4179. NSNumber *aValue,
  4180. BOOL *stop) {
  4181. #pragma unused(stop)
  4182. // Write the tag.
  4183. [outputStream writeInt32NoTag:tag];
  4184. // Write the size of the message.
  4185. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4186. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  4187. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4188. // Write the fields.
  4189. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4190. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  4191. }];
  4192. }
  4193. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4194. forGPBGenericValueKey:(GPBGenericValue *)key {
  4195. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  4196. }
  4197. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4198. [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) {
  4199. #pragma unused(stop)
  4200. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  4201. }];
  4202. }
  4203. - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
  4204. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4205. if (wrapped && value) {
  4206. *value = [wrapped floatValue];
  4207. }
  4208. return (wrapped != NULL);
  4209. }
  4210. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  4211. if (otherDictionary) {
  4212. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4213. if (_autocreator) {
  4214. GPBAutocreatedDictionaryModified(_autocreator, self);
  4215. }
  4216. }
  4217. }
  4218. - (void)setFloat:(float)value forKey:(int32_t)key {
  4219. [_dictionary setObject:@(value) forKey:@(key)];
  4220. if (_autocreator) {
  4221. GPBAutocreatedDictionaryModified(_autocreator, self);
  4222. }
  4223. }
  4224. - (void)removeFloatForKey:(int32_t)aKey {
  4225. [_dictionary removeObjectForKey:@(aKey)];
  4226. }
  4227. - (void)removeAll {
  4228. [_dictionary removeAllObjects];
  4229. }
  4230. @end
  4231. #pragma mark - Int32 -> Double
  4232. @implementation GPBInt32DoubleDictionary {
  4233. @package
  4234. NSMutableDictionary *_dictionary;
  4235. }
  4236. + (instancetype)dictionary {
  4237. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  4238. }
  4239. + (instancetype)dictionaryWithDouble:(double)value
  4240. forKey:(int32_t)key {
  4241. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  4242. // on to get the type correct.
  4243. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:&value
  4244. forKeys:&key
  4245. count:1] autorelease];
  4246. }
  4247. + (instancetype)dictionaryWithDoubles:(const double [])values
  4248. forKeys:(const int32_t [])keys
  4249. count:(NSUInteger)count {
  4250. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  4251. // on to get the type correct.
  4252. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDoubles:values
  4253. forKeys:keys
  4254. count:count] autorelease];
  4255. }
  4256. + (instancetype)dictionaryWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4257. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4258. // on to get the type correct.
  4259. return [[(GPBInt32DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4260. }
  4261. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4262. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4263. }
  4264. - (instancetype)init {
  4265. return [self initWithDoubles:NULL forKeys:NULL count:0];
  4266. }
  4267. - (instancetype)initWithDoubles:(const double [])values
  4268. forKeys:(const int32_t [])keys
  4269. count:(NSUInteger)count {
  4270. self = [super init];
  4271. if (self) {
  4272. _dictionary = [[NSMutableDictionary alloc] init];
  4273. if (count && values && keys) {
  4274. for (NSUInteger i = 0; i < count; ++i) {
  4275. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4276. }
  4277. }
  4278. }
  4279. return self;
  4280. }
  4281. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  4282. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  4283. if (self) {
  4284. if (dictionary) {
  4285. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4286. }
  4287. }
  4288. return self;
  4289. }
  4290. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4291. #pragma unused(numItems)
  4292. return [self initWithDoubles:NULL forKeys:NULL count:0];
  4293. }
  4294. - (void)dealloc {
  4295. NSAssert(!_autocreator,
  4296. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4297. [self class], _autocreator);
  4298. [_dictionary release];
  4299. [super dealloc];
  4300. }
  4301. - (instancetype)copyWithZone:(NSZone *)zone {
  4302. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  4303. }
  4304. - (BOOL)isEqual:(id)other {
  4305. if (self == other) {
  4306. return YES;
  4307. }
  4308. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  4309. return NO;
  4310. }
  4311. GPBInt32DoubleDictionary *otherDictionary = other;
  4312. return [_dictionary isEqual:otherDictionary->_dictionary];
  4313. }
  4314. - (NSUInteger)hash {
  4315. return _dictionary.count;
  4316. }
  4317. - (NSString *)description {
  4318. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4319. }
  4320. - (NSUInteger)count {
  4321. return _dictionary.count;
  4322. }
  4323. - (void)enumerateKeysAndDoublesUsingBlock:
  4324. (void (^)(int32_t key, double value, BOOL *stop))block {
  4325. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4326. NSNumber *aValue,
  4327. BOOL *stop) {
  4328. block([aKey intValue], [aValue doubleValue], stop);
  4329. }];
  4330. }
  4331. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4332. NSUInteger count = _dictionary.count;
  4333. if (count == 0) {
  4334. return 0;
  4335. }
  4336. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4337. GPBDataType keyDataType = field.mapKeyDataType;
  4338. __block size_t result = 0;
  4339. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4340. NSNumber *aValue,
  4341. BOOL *stop) {
  4342. #pragma unused(stop)
  4343. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4344. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4345. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4346. }];
  4347. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4348. result += tagSize * count;
  4349. return result;
  4350. }
  4351. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4352. asField:(GPBFieldDescriptor *)field {
  4353. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4354. GPBDataType keyDataType = field.mapKeyDataType;
  4355. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4356. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4357. NSNumber *aValue,
  4358. BOOL *stop) {
  4359. #pragma unused(stop)
  4360. // Write the tag.
  4361. [outputStream writeInt32NoTag:tag];
  4362. // Write the size of the message.
  4363. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4364. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4365. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4366. // Write the fields.
  4367. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4368. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  4369. }];
  4370. }
  4371. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4372. forGPBGenericValueKey:(GPBGenericValue *)key {
  4373. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  4374. }
  4375. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4376. [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  4377. #pragma unused(stop)
  4378. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  4379. }];
  4380. }
  4381. - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
  4382. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4383. if (wrapped && value) {
  4384. *value = [wrapped doubleValue];
  4385. }
  4386. return (wrapped != NULL);
  4387. }
  4388. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  4389. if (otherDictionary) {
  4390. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4391. if (_autocreator) {
  4392. GPBAutocreatedDictionaryModified(_autocreator, self);
  4393. }
  4394. }
  4395. }
  4396. - (void)setDouble:(double)value forKey:(int32_t)key {
  4397. [_dictionary setObject:@(value) forKey:@(key)];
  4398. if (_autocreator) {
  4399. GPBAutocreatedDictionaryModified(_autocreator, self);
  4400. }
  4401. }
  4402. - (void)removeDoubleForKey:(int32_t)aKey {
  4403. [_dictionary removeObjectForKey:@(aKey)];
  4404. }
  4405. - (void)removeAll {
  4406. [_dictionary removeAllObjects];
  4407. }
  4408. @end
  4409. #pragma mark - Int32 -> Enum
  4410. @implementation GPBInt32EnumDictionary {
  4411. @package
  4412. NSMutableDictionary *_dictionary;
  4413. GPBEnumValidationFunc _validationFunc;
  4414. }
  4415. @synthesize validationFunc = _validationFunc;
  4416. + (instancetype)dictionary {
  4417. return [[[self alloc] initWithValidationFunction:NULL
  4418. rawValues:NULL
  4419. forKeys:NULL
  4420. count:0] autorelease];
  4421. }
  4422. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  4423. return [[[self alloc] initWithValidationFunction:func
  4424. rawValues:NULL
  4425. forKeys:NULL
  4426. count:0] autorelease];
  4427. }
  4428. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4429. rawValue:(int32_t)rawValue
  4430. forKey:(int32_t)key {
  4431. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4432. // on to get the type correct.
  4433. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4434. rawValues:&rawValue
  4435. forKeys:&key
  4436. count:1] autorelease];
  4437. }
  4438. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4439. rawValues:(const int32_t [])rawValues
  4440. forKeys:(const int32_t [])keys
  4441. count:(NSUInteger)count {
  4442. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4443. // on to get the type correct.
  4444. return [[(GPBInt32EnumDictionary*)[self alloc] initWithValidationFunction:func
  4445. rawValues:rawValues
  4446. forKeys:keys
  4447. count:count] autorelease];
  4448. }
  4449. + (instancetype)dictionaryWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4450. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  4451. // on to get the type correct.
  4452. return [[(GPBInt32EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4453. }
  4454. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  4455. capacity:(NSUInteger)numItems {
  4456. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  4457. }
  4458. - (instancetype)init {
  4459. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4460. }
  4461. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4462. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4463. }
  4464. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4465. rawValues:(const int32_t [])rawValues
  4466. forKeys:(const int32_t [])keys
  4467. count:(NSUInteger)count {
  4468. self = [super init];
  4469. if (self) {
  4470. _dictionary = [[NSMutableDictionary alloc] init];
  4471. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4472. if (count && rawValues && keys) {
  4473. for (NSUInteger i = 0; i < count; ++i) {
  4474. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4475. }
  4476. }
  4477. }
  4478. return self;
  4479. }
  4480. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4481. self = [self initWithValidationFunction:dictionary.validationFunc
  4482. rawValues:NULL
  4483. forKeys:NULL
  4484. count:0];
  4485. if (self) {
  4486. if (dictionary) {
  4487. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4488. }
  4489. }
  4490. return self;
  4491. }
  4492. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4493. capacity:(NSUInteger)numItems {
  4494. #pragma unused(numItems)
  4495. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4496. }
  4497. - (void)dealloc {
  4498. NSAssert(!_autocreator,
  4499. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4500. [self class], _autocreator);
  4501. [_dictionary release];
  4502. [super dealloc];
  4503. }
  4504. - (instancetype)copyWithZone:(NSZone *)zone {
  4505. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4506. }
  4507. - (BOOL)isEqual:(id)other {
  4508. if (self == other) {
  4509. return YES;
  4510. }
  4511. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4512. return NO;
  4513. }
  4514. GPBInt32EnumDictionary *otherDictionary = other;
  4515. return [_dictionary isEqual:otherDictionary->_dictionary];
  4516. }
  4517. - (NSUInteger)hash {
  4518. return _dictionary.count;
  4519. }
  4520. - (NSString *)description {
  4521. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4522. }
  4523. - (NSUInteger)count {
  4524. return _dictionary.count;
  4525. }
  4526. - (void)enumerateKeysAndRawValuesUsingBlock:
  4527. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4528. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4529. NSNumber *aValue,
  4530. BOOL *stop) {
  4531. block([aKey intValue], [aValue intValue], stop);
  4532. }];
  4533. }
  4534. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4535. NSUInteger count = _dictionary.count;
  4536. if (count == 0) {
  4537. return 0;
  4538. }
  4539. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4540. GPBDataType keyDataType = field.mapKeyDataType;
  4541. __block size_t result = 0;
  4542. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4543. NSNumber *aValue,
  4544. BOOL *stop) {
  4545. #pragma unused(stop)
  4546. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4547. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4548. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4549. }];
  4550. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4551. result += tagSize * count;
  4552. return result;
  4553. }
  4554. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4555. asField:(GPBFieldDescriptor *)field {
  4556. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4557. GPBDataType keyDataType = field.mapKeyDataType;
  4558. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4559. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4560. NSNumber *aValue,
  4561. BOOL *stop) {
  4562. #pragma unused(stop)
  4563. // Write the tag.
  4564. [outputStream writeInt32NoTag:tag];
  4565. // Write the size of the message.
  4566. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4567. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4568. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4569. // Write the fields.
  4570. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4571. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  4572. }];
  4573. }
  4574. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4575. forKey:(GPBGenericValue *)key
  4576. keyDataType:(GPBDataType)keyDataType {
  4577. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4578. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4579. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4580. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4581. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4582. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4583. [outputStream release];
  4584. return data;
  4585. }
  4586. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4587. forGPBGenericValueKey:(GPBGenericValue *)key {
  4588. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4589. }
  4590. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4591. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4592. #pragma unused(stop)
  4593. block([NSString stringWithFormat:@"%d", key], @(value));
  4594. }];
  4595. }
  4596. - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
  4597. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4598. if (wrapped && value) {
  4599. int32_t result = [wrapped intValue];
  4600. if (!_validationFunc(result)) {
  4601. result = kGPBUnrecognizedEnumeratorValue;
  4602. }
  4603. *value = result;
  4604. }
  4605. return (wrapped != NULL);
  4606. }
  4607. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
  4608. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4609. if (wrapped && rawValue) {
  4610. *rawValue = [wrapped intValue];
  4611. }
  4612. return (wrapped != NULL);
  4613. }
  4614. - (void)enumerateKeysAndEnumsUsingBlock:
  4615. (void (^)(int32_t key, int32_t value, BOOL *stop))block {
  4616. GPBEnumValidationFunc func = _validationFunc;
  4617. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4618. NSNumber *aValue,
  4619. BOOL *stop) {
  4620. int32_t unwrapped = [aValue intValue];
  4621. if (!func(unwrapped)) {
  4622. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4623. }
  4624. block([aKey intValue], unwrapped, stop);
  4625. }];
  4626. }
  4627. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4628. if (otherDictionary) {
  4629. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4630. if (_autocreator) {
  4631. GPBAutocreatedDictionaryModified(_autocreator, self);
  4632. }
  4633. }
  4634. }
  4635. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4636. [_dictionary setObject:@(value) forKey:@(key)];
  4637. if (_autocreator) {
  4638. GPBAutocreatedDictionaryModified(_autocreator, self);
  4639. }
  4640. }
  4641. - (void)removeEnumForKey:(int32_t)aKey {
  4642. [_dictionary removeObjectForKey:@(aKey)];
  4643. }
  4644. - (void)removeAll {
  4645. [_dictionary removeAllObjects];
  4646. }
  4647. - (void)setEnum:(int32_t)value forKey:(int32_t)key {
  4648. if (!_validationFunc(value)) {
  4649. [NSException raise:NSInvalidArgumentException
  4650. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4651. value];
  4652. }
  4653. [_dictionary setObject:@(value) forKey:@(key)];
  4654. if (_autocreator) {
  4655. GPBAutocreatedDictionaryModified(_autocreator, self);
  4656. }
  4657. }
  4658. @end
  4659. #pragma mark - Int32 -> Object
  4660. @implementation GPBInt32ObjectDictionary {
  4661. @package
  4662. NSMutableDictionary *_dictionary;
  4663. }
  4664. + (instancetype)dictionary {
  4665. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  4666. }
  4667. + (instancetype)dictionaryWithObject:(id)object
  4668. forKey:(int32_t)key {
  4669. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4670. // on to get the type correct.
  4671. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:&object
  4672. forKeys:&key
  4673. count:1] autorelease];
  4674. }
  4675. + (instancetype)dictionaryWithObjects:(const id [])objects
  4676. forKeys:(const int32_t [])keys
  4677. count:(NSUInteger)count {
  4678. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  4679. // on to get the type correct.
  4680. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithObjects:objects
  4681. forKeys:keys
  4682. count:count] autorelease];
  4683. }
  4684. + (instancetype)dictionaryWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4685. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4686. // on to get the type correct.
  4687. return [[(GPBInt32ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4688. }
  4689. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4690. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4691. }
  4692. - (instancetype)init {
  4693. return [self initWithObjects:NULL forKeys:NULL count:0];
  4694. }
  4695. - (instancetype)initWithObjects:(const id [])objects
  4696. forKeys:(const int32_t [])keys
  4697. count:(NSUInteger)count {
  4698. self = [super init];
  4699. if (self) {
  4700. _dictionary = [[NSMutableDictionary alloc] init];
  4701. if (count && objects && keys) {
  4702. for (NSUInteger i = 0; i < count; ++i) {
  4703. if (!objects[i]) {
  4704. [NSException raise:NSInvalidArgumentException
  4705. format:@"Attempting to add nil object to a Dictionary"];
  4706. }
  4707. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4708. }
  4709. }
  4710. }
  4711. return self;
  4712. }
  4713. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4714. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4715. if (self) {
  4716. if (dictionary) {
  4717. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4718. }
  4719. }
  4720. return self;
  4721. }
  4722. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4723. #pragma unused(numItems)
  4724. return [self initWithObjects:NULL forKeys:NULL count:0];
  4725. }
  4726. - (void)dealloc {
  4727. NSAssert(!_autocreator,
  4728. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4729. [self class], _autocreator);
  4730. [_dictionary release];
  4731. [super dealloc];
  4732. }
  4733. - (instancetype)copyWithZone:(NSZone *)zone {
  4734. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4735. }
  4736. - (BOOL)isEqual:(id)other {
  4737. if (self == other) {
  4738. return YES;
  4739. }
  4740. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4741. return NO;
  4742. }
  4743. GPBInt32ObjectDictionary *otherDictionary = other;
  4744. return [_dictionary isEqual:otherDictionary->_dictionary];
  4745. }
  4746. - (NSUInteger)hash {
  4747. return _dictionary.count;
  4748. }
  4749. - (NSString *)description {
  4750. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4751. }
  4752. - (NSUInteger)count {
  4753. return _dictionary.count;
  4754. }
  4755. - (void)enumerateKeysAndObjectsUsingBlock:
  4756. (void (^)(int32_t key, id object, BOOL *stop))block {
  4757. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4758. id aObject,
  4759. BOOL *stop) {
  4760. block([aKey intValue], aObject, stop);
  4761. }];
  4762. }
  4763. - (BOOL)isInitialized {
  4764. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4765. if (!msg.initialized) {
  4766. return NO;
  4767. }
  4768. }
  4769. return YES;
  4770. }
  4771. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4772. GPBInt32ObjectDictionary *newDict =
  4773. [[GPBInt32ObjectDictionary alloc] init];
  4774. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  4775. GPBMessage *msg,
  4776. BOOL *stop) {
  4777. #pragma unused(stop)
  4778. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4779. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  4780. [copiedMsg release];
  4781. }];
  4782. return newDict;
  4783. }
  4784. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4785. NSUInteger count = _dictionary.count;
  4786. if (count == 0) {
  4787. return 0;
  4788. }
  4789. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4790. GPBDataType keyDataType = field.mapKeyDataType;
  4791. __block size_t result = 0;
  4792. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4793. id aObject,
  4794. BOOL *stop) {
  4795. #pragma unused(stop)
  4796. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4797. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4798. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4799. }];
  4800. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4801. result += tagSize * count;
  4802. return result;
  4803. }
  4804. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4805. asField:(GPBFieldDescriptor *)field {
  4806. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4807. GPBDataType keyDataType = field.mapKeyDataType;
  4808. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4809. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4810. id aObject,
  4811. BOOL *stop) {
  4812. #pragma unused(stop)
  4813. // Write the tag.
  4814. [outputStream writeInt32NoTag:tag];
  4815. // Write the size of the message.
  4816. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4817. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4818. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4819. // Write the fields.
  4820. WriteDictInt32Field(outputStream, [aKey intValue], kMapKeyFieldNumber, keyDataType);
  4821. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  4822. }];
  4823. }
  4824. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4825. forGPBGenericValueKey:(GPBGenericValue *)key {
  4826. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4827. }
  4828. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  4829. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4830. #pragma unused(stop)
  4831. block([NSString stringWithFormat:@"%d", key], object);
  4832. }];
  4833. }
  4834. - (id)objectForKey:(int32_t)key {
  4835. id result = [_dictionary objectForKey:@(key)];
  4836. return result;
  4837. }
  4838. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4839. if (otherDictionary) {
  4840. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4841. if (_autocreator) {
  4842. GPBAutocreatedDictionaryModified(_autocreator, self);
  4843. }
  4844. }
  4845. }
  4846. - (void)setObject:(id)object forKey:(int32_t)key {
  4847. if (!object) {
  4848. [NSException raise:NSInvalidArgumentException
  4849. format:@"Attempting to add nil object to a Dictionary"];
  4850. }
  4851. [_dictionary setObject:object forKey:@(key)];
  4852. if (_autocreator) {
  4853. GPBAutocreatedDictionaryModified(_autocreator, self);
  4854. }
  4855. }
  4856. - (void)removeObjectForKey:(int32_t)aKey {
  4857. [_dictionary removeObjectForKey:@(aKey)];
  4858. }
  4859. - (void)removeAll {
  4860. [_dictionary removeAllObjects];
  4861. }
  4862. @end
  4863. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4864. // This block of code is generated, do not edit it directly.
  4865. #pragma mark - UInt64 -> UInt32
  4866. @implementation GPBUInt64UInt32Dictionary {
  4867. @package
  4868. NSMutableDictionary *_dictionary;
  4869. }
  4870. + (instancetype)dictionary {
  4871. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  4872. }
  4873. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  4874. forKey:(uint64_t)key {
  4875. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  4876. // on to get the type correct.
  4877. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  4878. forKeys:&key
  4879. count:1] autorelease];
  4880. }
  4881. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  4882. forKeys:(const uint64_t [])keys
  4883. count:(NSUInteger)count {
  4884. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  4885. // on to get the type correct.
  4886. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
  4887. forKeys:keys
  4888. count:count] autorelease];
  4889. }
  4890. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4891. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  4892. // on to get the type correct.
  4893. return [[(GPBUInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  4894. }
  4895. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  4896. return [[[self alloc] initWithCapacity:numItems] autorelease];
  4897. }
  4898. - (instancetype)init {
  4899. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4900. }
  4901. - (instancetype)initWithUInt32s:(const uint32_t [])values
  4902. forKeys:(const uint64_t [])keys
  4903. count:(NSUInteger)count {
  4904. self = [super init];
  4905. if (self) {
  4906. _dictionary = [[NSMutableDictionary alloc] init];
  4907. if (count && values && keys) {
  4908. for (NSUInteger i = 0; i < count; ++i) {
  4909. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4910. }
  4911. }
  4912. }
  4913. return self;
  4914. }
  4915. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4916. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  4917. if (self) {
  4918. if (dictionary) {
  4919. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4920. }
  4921. }
  4922. return self;
  4923. }
  4924. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4925. #pragma unused(numItems)
  4926. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4927. }
  4928. - (void)dealloc {
  4929. NSAssert(!_autocreator,
  4930. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4931. [self class], _autocreator);
  4932. [_dictionary release];
  4933. [super dealloc];
  4934. }
  4935. - (instancetype)copyWithZone:(NSZone *)zone {
  4936. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4937. }
  4938. - (BOOL)isEqual:(id)other {
  4939. if (self == other) {
  4940. return YES;
  4941. }
  4942. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4943. return NO;
  4944. }
  4945. GPBUInt64UInt32Dictionary *otherDictionary = other;
  4946. return [_dictionary isEqual:otherDictionary->_dictionary];
  4947. }
  4948. - (NSUInteger)hash {
  4949. return _dictionary.count;
  4950. }
  4951. - (NSString *)description {
  4952. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4953. }
  4954. - (NSUInteger)count {
  4955. return _dictionary.count;
  4956. }
  4957. - (void)enumerateKeysAndUInt32sUsingBlock:
  4958. (void (^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4959. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4960. NSNumber *aValue,
  4961. BOOL *stop) {
  4962. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], stop);
  4963. }];
  4964. }
  4965. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4966. NSUInteger count = _dictionary.count;
  4967. if (count == 0) {
  4968. return 0;
  4969. }
  4970. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4971. GPBDataType keyDataType = field.mapKeyDataType;
  4972. __block size_t result = 0;
  4973. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4974. NSNumber *aValue,
  4975. BOOL *stop) {
  4976. #pragma unused(stop)
  4977. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4978. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4979. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4980. }];
  4981. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4982. result += tagSize * count;
  4983. return result;
  4984. }
  4985. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4986. asField:(GPBFieldDescriptor *)field {
  4987. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4988. GPBDataType keyDataType = field.mapKeyDataType;
  4989. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4990. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  4991. NSNumber *aValue,
  4992. BOOL *stop) {
  4993. #pragma unused(stop)
  4994. // Write the tag.
  4995. [outputStream writeInt32NoTag:tag];
  4996. // Write the size of the message.
  4997. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4998. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4999. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5000. // Write the fields.
  5001. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5002. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  5003. }];
  5004. }
  5005. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5006. forGPBGenericValueKey:(GPBGenericValue *)key {
  5007. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  5008. }
  5009. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5010. [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  5011. #pragma unused(stop)
  5012. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  5013. }];
  5014. }
  5015. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
  5016. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5017. if (wrapped && value) {
  5018. *value = [wrapped unsignedIntValue];
  5019. }
  5020. return (wrapped != NULL);
  5021. }
  5022. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  5023. if (otherDictionary) {
  5024. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5025. if (_autocreator) {
  5026. GPBAutocreatedDictionaryModified(_autocreator, self);
  5027. }
  5028. }
  5029. }
  5030. - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
  5031. [_dictionary setObject:@(value) forKey:@(key)];
  5032. if (_autocreator) {
  5033. GPBAutocreatedDictionaryModified(_autocreator, self);
  5034. }
  5035. }
  5036. - (void)removeUInt32ForKey:(uint64_t)aKey {
  5037. [_dictionary removeObjectForKey:@(aKey)];
  5038. }
  5039. - (void)removeAll {
  5040. [_dictionary removeAllObjects];
  5041. }
  5042. @end
  5043. #pragma mark - UInt64 -> Int32
  5044. @implementation GPBUInt64Int32Dictionary {
  5045. @package
  5046. NSMutableDictionary *_dictionary;
  5047. }
  5048. + (instancetype)dictionary {
  5049. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  5050. }
  5051. + (instancetype)dictionaryWithInt32:(int32_t)value
  5052. forKey:(uint64_t)key {
  5053. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  5054. // on to get the type correct.
  5055. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
  5056. forKeys:&key
  5057. count:1] autorelease];
  5058. }
  5059. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  5060. forKeys:(const uint64_t [])keys
  5061. count:(NSUInteger)count {
  5062. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  5063. // on to get the type correct.
  5064. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithInt32s:values
  5065. forKeys:keys
  5066. count:count] autorelease];
  5067. }
  5068. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5069. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5070. // on to get the type correct.
  5071. return [[(GPBUInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5072. }
  5073. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5074. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5075. }
  5076. - (instancetype)init {
  5077. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5078. }
  5079. - (instancetype)initWithInt32s:(const int32_t [])values
  5080. forKeys:(const uint64_t [])keys
  5081. count:(NSUInteger)count {
  5082. self = [super init];
  5083. if (self) {
  5084. _dictionary = [[NSMutableDictionary alloc] init];
  5085. if (count && values && keys) {
  5086. for (NSUInteger i = 0; i < count; ++i) {
  5087. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5088. }
  5089. }
  5090. }
  5091. return self;
  5092. }
  5093. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  5094. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  5095. if (self) {
  5096. if (dictionary) {
  5097. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5098. }
  5099. }
  5100. return self;
  5101. }
  5102. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5103. #pragma unused(numItems)
  5104. return [self initWithInt32s:NULL forKeys:NULL count:0];
  5105. }
  5106. - (void)dealloc {
  5107. NSAssert(!_autocreator,
  5108. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5109. [self class], _autocreator);
  5110. [_dictionary release];
  5111. [super dealloc];
  5112. }
  5113. - (instancetype)copyWithZone:(NSZone *)zone {
  5114. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  5115. }
  5116. - (BOOL)isEqual:(id)other {
  5117. if (self == other) {
  5118. return YES;
  5119. }
  5120. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  5121. return NO;
  5122. }
  5123. GPBUInt64Int32Dictionary *otherDictionary = other;
  5124. return [_dictionary isEqual:otherDictionary->_dictionary];
  5125. }
  5126. - (NSUInteger)hash {
  5127. return _dictionary.count;
  5128. }
  5129. - (NSString *)description {
  5130. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5131. }
  5132. - (NSUInteger)count {
  5133. return _dictionary.count;
  5134. }
  5135. - (void)enumerateKeysAndInt32sUsingBlock:
  5136. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  5137. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5138. NSNumber *aValue,
  5139. BOOL *stop) {
  5140. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  5141. }];
  5142. }
  5143. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5144. NSUInteger count = _dictionary.count;
  5145. if (count == 0) {
  5146. return 0;
  5147. }
  5148. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5149. GPBDataType keyDataType = field.mapKeyDataType;
  5150. __block size_t result = 0;
  5151. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5152. NSNumber *aValue,
  5153. BOOL *stop) {
  5154. #pragma unused(stop)
  5155. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5156. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5157. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5158. }];
  5159. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5160. result += tagSize * count;
  5161. return result;
  5162. }
  5163. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5164. asField:(GPBFieldDescriptor *)field {
  5165. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5166. GPBDataType keyDataType = field.mapKeyDataType;
  5167. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5168. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5169. NSNumber *aValue,
  5170. BOOL *stop) {
  5171. #pragma unused(stop)
  5172. // Write the tag.
  5173. [outputStream writeInt32NoTag:tag];
  5174. // Write the size of the message.
  5175. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5176. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5177. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5178. // Write the fields.
  5179. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5180. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  5181. }];
  5182. }
  5183. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5184. forGPBGenericValueKey:(GPBGenericValue *)key {
  5185. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  5186. }
  5187. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5188. [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5189. #pragma unused(stop)
  5190. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  5191. }];
  5192. }
  5193. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
  5194. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5195. if (wrapped && value) {
  5196. *value = [wrapped intValue];
  5197. }
  5198. return (wrapped != NULL);
  5199. }
  5200. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  5201. if (otherDictionary) {
  5202. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5203. if (_autocreator) {
  5204. GPBAutocreatedDictionaryModified(_autocreator, self);
  5205. }
  5206. }
  5207. }
  5208. - (void)setInt32:(int32_t)value forKey:(uint64_t)key {
  5209. [_dictionary setObject:@(value) forKey:@(key)];
  5210. if (_autocreator) {
  5211. GPBAutocreatedDictionaryModified(_autocreator, self);
  5212. }
  5213. }
  5214. - (void)removeInt32ForKey:(uint64_t)aKey {
  5215. [_dictionary removeObjectForKey:@(aKey)];
  5216. }
  5217. - (void)removeAll {
  5218. [_dictionary removeAllObjects];
  5219. }
  5220. @end
  5221. #pragma mark - UInt64 -> UInt64
  5222. @implementation GPBUInt64UInt64Dictionary {
  5223. @package
  5224. NSMutableDictionary *_dictionary;
  5225. }
  5226. + (instancetype)dictionary {
  5227. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  5228. }
  5229. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  5230. forKey:(uint64_t)key {
  5231. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  5232. // on to get the type correct.
  5233. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  5234. forKeys:&key
  5235. count:1] autorelease];
  5236. }
  5237. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  5238. forKeys:(const uint64_t [])keys
  5239. count:(NSUInteger)count {
  5240. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  5241. // on to get the type correct.
  5242. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
  5243. forKeys:keys
  5244. count:count] autorelease];
  5245. }
  5246. + (instancetype)dictionaryWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5247. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5248. // on to get the type correct.
  5249. return [[(GPBUInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5250. }
  5251. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5252. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5253. }
  5254. - (instancetype)init {
  5255. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  5256. }
  5257. - (instancetype)initWithUInt64s:(const uint64_t [])values
  5258. forKeys:(const uint64_t [])keys
  5259. count:(NSUInteger)count {
  5260. self = [super init];
  5261. if (self) {
  5262. _dictionary = [[NSMutableDictionary alloc] init];
  5263. if (count && values && keys) {
  5264. for (NSUInteger i = 0; i < count; ++i) {
  5265. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5266. }
  5267. }
  5268. }
  5269. return self;
  5270. }
  5271. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  5272. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  5273. if (self) {
  5274. if (dictionary) {
  5275. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5276. }
  5277. }
  5278. return self;
  5279. }
  5280. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5281. #pragma unused(numItems)
  5282. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  5283. }
  5284. - (void)dealloc {
  5285. NSAssert(!_autocreator,
  5286. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5287. [self class], _autocreator);
  5288. [_dictionary release];
  5289. [super dealloc];
  5290. }
  5291. - (instancetype)copyWithZone:(NSZone *)zone {
  5292. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  5293. }
  5294. - (BOOL)isEqual:(id)other {
  5295. if (self == other) {
  5296. return YES;
  5297. }
  5298. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  5299. return NO;
  5300. }
  5301. GPBUInt64UInt64Dictionary *otherDictionary = other;
  5302. return [_dictionary isEqual:otherDictionary->_dictionary];
  5303. }
  5304. - (NSUInteger)hash {
  5305. return _dictionary.count;
  5306. }
  5307. - (NSString *)description {
  5308. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5309. }
  5310. - (NSUInteger)count {
  5311. return _dictionary.count;
  5312. }
  5313. - (void)enumerateKeysAndUInt64sUsingBlock:
  5314. (void (^)(uint64_t key, uint64_t value, BOOL *stop))block {
  5315. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5316. NSNumber *aValue,
  5317. BOOL *stop) {
  5318. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], stop);
  5319. }];
  5320. }
  5321. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5322. NSUInteger count = _dictionary.count;
  5323. if (count == 0) {
  5324. return 0;
  5325. }
  5326. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5327. GPBDataType keyDataType = field.mapKeyDataType;
  5328. __block size_t result = 0;
  5329. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5330. NSNumber *aValue,
  5331. BOOL *stop) {
  5332. #pragma unused(stop)
  5333. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5334. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5335. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5336. }];
  5337. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5338. result += tagSize * count;
  5339. return result;
  5340. }
  5341. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5342. asField:(GPBFieldDescriptor *)field {
  5343. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5344. GPBDataType keyDataType = field.mapKeyDataType;
  5345. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5346. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5347. NSNumber *aValue,
  5348. BOOL *stop) {
  5349. #pragma unused(stop)
  5350. // Write the tag.
  5351. [outputStream writeInt32NoTag:tag];
  5352. // Write the size of the message.
  5353. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5354. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5355. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5356. // Write the fields.
  5357. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5358. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  5359. }];
  5360. }
  5361. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5362. forGPBGenericValueKey:(GPBGenericValue *)key {
  5363. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  5364. }
  5365. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5366. [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  5367. #pragma unused(stop)
  5368. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  5369. }];
  5370. }
  5371. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
  5372. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5373. if (wrapped && value) {
  5374. *value = [wrapped unsignedLongLongValue];
  5375. }
  5376. return (wrapped != NULL);
  5377. }
  5378. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  5379. if (otherDictionary) {
  5380. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5381. if (_autocreator) {
  5382. GPBAutocreatedDictionaryModified(_autocreator, self);
  5383. }
  5384. }
  5385. }
  5386. - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
  5387. [_dictionary setObject:@(value) forKey:@(key)];
  5388. if (_autocreator) {
  5389. GPBAutocreatedDictionaryModified(_autocreator, self);
  5390. }
  5391. }
  5392. - (void)removeUInt64ForKey:(uint64_t)aKey {
  5393. [_dictionary removeObjectForKey:@(aKey)];
  5394. }
  5395. - (void)removeAll {
  5396. [_dictionary removeAllObjects];
  5397. }
  5398. @end
  5399. #pragma mark - UInt64 -> Int64
  5400. @implementation GPBUInt64Int64Dictionary {
  5401. @package
  5402. NSMutableDictionary *_dictionary;
  5403. }
  5404. + (instancetype)dictionary {
  5405. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  5406. }
  5407. + (instancetype)dictionaryWithInt64:(int64_t)value
  5408. forKey:(uint64_t)key {
  5409. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  5410. // on to get the type correct.
  5411. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
  5412. forKeys:&key
  5413. count:1] autorelease];
  5414. }
  5415. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  5416. forKeys:(const uint64_t [])keys
  5417. count:(NSUInteger)count {
  5418. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  5419. // on to get the type correct.
  5420. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithInt64s:values
  5421. forKeys:keys
  5422. count:count] autorelease];
  5423. }
  5424. + (instancetype)dictionaryWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5425. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5426. // on to get the type correct.
  5427. return [[(GPBUInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5428. }
  5429. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5430. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5431. }
  5432. - (instancetype)init {
  5433. return [self initWithInt64s:NULL forKeys:NULL count:0];
  5434. }
  5435. - (instancetype)initWithInt64s:(const int64_t [])values
  5436. forKeys:(const uint64_t [])keys
  5437. count:(NSUInteger)count {
  5438. self = [super init];
  5439. if (self) {
  5440. _dictionary = [[NSMutableDictionary alloc] init];
  5441. if (count && values && keys) {
  5442. for (NSUInteger i = 0; i < count; ++i) {
  5443. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5444. }
  5445. }
  5446. }
  5447. return self;
  5448. }
  5449. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  5450. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  5451. if (self) {
  5452. if (dictionary) {
  5453. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5454. }
  5455. }
  5456. return self;
  5457. }
  5458. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5459. #pragma unused(numItems)
  5460. return [self initWithInt64s:NULL forKeys:NULL count:0];
  5461. }
  5462. - (void)dealloc {
  5463. NSAssert(!_autocreator,
  5464. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5465. [self class], _autocreator);
  5466. [_dictionary release];
  5467. [super dealloc];
  5468. }
  5469. - (instancetype)copyWithZone:(NSZone *)zone {
  5470. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  5471. }
  5472. - (BOOL)isEqual:(id)other {
  5473. if (self == other) {
  5474. return YES;
  5475. }
  5476. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  5477. return NO;
  5478. }
  5479. GPBUInt64Int64Dictionary *otherDictionary = other;
  5480. return [_dictionary isEqual:otherDictionary->_dictionary];
  5481. }
  5482. - (NSUInteger)hash {
  5483. return _dictionary.count;
  5484. }
  5485. - (NSString *)description {
  5486. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5487. }
  5488. - (NSUInteger)count {
  5489. return _dictionary.count;
  5490. }
  5491. - (void)enumerateKeysAndInt64sUsingBlock:
  5492. (void (^)(uint64_t key, int64_t value, BOOL *stop))block {
  5493. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5494. NSNumber *aValue,
  5495. BOOL *stop) {
  5496. block([aKey unsignedLongLongValue], [aValue longLongValue], stop);
  5497. }];
  5498. }
  5499. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5500. NSUInteger count = _dictionary.count;
  5501. if (count == 0) {
  5502. return 0;
  5503. }
  5504. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5505. GPBDataType keyDataType = field.mapKeyDataType;
  5506. __block size_t result = 0;
  5507. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5508. NSNumber *aValue,
  5509. BOOL *stop) {
  5510. #pragma unused(stop)
  5511. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5512. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5513. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5514. }];
  5515. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5516. result += tagSize * count;
  5517. return result;
  5518. }
  5519. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5520. asField:(GPBFieldDescriptor *)field {
  5521. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5522. GPBDataType keyDataType = field.mapKeyDataType;
  5523. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5524. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5525. NSNumber *aValue,
  5526. BOOL *stop) {
  5527. #pragma unused(stop)
  5528. // Write the tag.
  5529. [outputStream writeInt32NoTag:tag];
  5530. // Write the size of the message.
  5531. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5532. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5533. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5534. // Write the fields.
  5535. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5536. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  5537. }];
  5538. }
  5539. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5540. forGPBGenericValueKey:(GPBGenericValue *)key {
  5541. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5542. }
  5543. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5544. [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5545. #pragma unused(stop)
  5546. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5547. }];
  5548. }
  5549. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
  5550. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5551. if (wrapped && value) {
  5552. *value = [wrapped longLongValue];
  5553. }
  5554. return (wrapped != NULL);
  5555. }
  5556. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5557. if (otherDictionary) {
  5558. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5559. if (_autocreator) {
  5560. GPBAutocreatedDictionaryModified(_autocreator, self);
  5561. }
  5562. }
  5563. }
  5564. - (void)setInt64:(int64_t)value forKey:(uint64_t)key {
  5565. [_dictionary setObject:@(value) forKey:@(key)];
  5566. if (_autocreator) {
  5567. GPBAutocreatedDictionaryModified(_autocreator, self);
  5568. }
  5569. }
  5570. - (void)removeInt64ForKey:(uint64_t)aKey {
  5571. [_dictionary removeObjectForKey:@(aKey)];
  5572. }
  5573. - (void)removeAll {
  5574. [_dictionary removeAllObjects];
  5575. }
  5576. @end
  5577. #pragma mark - UInt64 -> Bool
  5578. @implementation GPBUInt64BoolDictionary {
  5579. @package
  5580. NSMutableDictionary *_dictionary;
  5581. }
  5582. + (instancetype)dictionary {
  5583. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  5584. }
  5585. + (instancetype)dictionaryWithBool:(BOOL)value
  5586. forKey:(uint64_t)key {
  5587. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  5588. // on to get the type correct.
  5589. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:&value
  5590. forKeys:&key
  5591. count:1] autorelease];
  5592. }
  5593. + (instancetype)dictionaryWithBools:(const BOOL [])values
  5594. forKeys:(const uint64_t [])keys
  5595. count:(NSUInteger)count {
  5596. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  5597. // on to get the type correct.
  5598. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithBools:values
  5599. forKeys:keys
  5600. count:count] autorelease];
  5601. }
  5602. + (instancetype)dictionaryWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5603. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5604. // on to get the type correct.
  5605. return [[(GPBUInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5606. }
  5607. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5608. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5609. }
  5610. - (instancetype)init {
  5611. return [self initWithBools:NULL forKeys:NULL count:0];
  5612. }
  5613. - (instancetype)initWithBools:(const BOOL [])values
  5614. forKeys:(const uint64_t [])keys
  5615. count:(NSUInteger)count {
  5616. self = [super init];
  5617. if (self) {
  5618. _dictionary = [[NSMutableDictionary alloc] init];
  5619. if (count && values && keys) {
  5620. for (NSUInteger i = 0; i < count; ++i) {
  5621. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5622. }
  5623. }
  5624. }
  5625. return self;
  5626. }
  5627. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5628. self = [self initWithBools:NULL forKeys:NULL count:0];
  5629. if (self) {
  5630. if (dictionary) {
  5631. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5632. }
  5633. }
  5634. return self;
  5635. }
  5636. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5637. #pragma unused(numItems)
  5638. return [self initWithBools:NULL forKeys:NULL count:0];
  5639. }
  5640. - (void)dealloc {
  5641. NSAssert(!_autocreator,
  5642. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5643. [self class], _autocreator);
  5644. [_dictionary release];
  5645. [super dealloc];
  5646. }
  5647. - (instancetype)copyWithZone:(NSZone *)zone {
  5648. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5649. }
  5650. - (BOOL)isEqual:(id)other {
  5651. if (self == other) {
  5652. return YES;
  5653. }
  5654. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5655. return NO;
  5656. }
  5657. GPBUInt64BoolDictionary *otherDictionary = other;
  5658. return [_dictionary isEqual:otherDictionary->_dictionary];
  5659. }
  5660. - (NSUInteger)hash {
  5661. return _dictionary.count;
  5662. }
  5663. - (NSString *)description {
  5664. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5665. }
  5666. - (NSUInteger)count {
  5667. return _dictionary.count;
  5668. }
  5669. - (void)enumerateKeysAndBoolsUsingBlock:
  5670. (void (^)(uint64_t key, BOOL value, BOOL *stop))block {
  5671. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5672. NSNumber *aValue,
  5673. BOOL *stop) {
  5674. block([aKey unsignedLongLongValue], [aValue boolValue], stop);
  5675. }];
  5676. }
  5677. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5678. NSUInteger count = _dictionary.count;
  5679. if (count == 0) {
  5680. return 0;
  5681. }
  5682. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5683. GPBDataType keyDataType = field.mapKeyDataType;
  5684. __block size_t result = 0;
  5685. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5686. NSNumber *aValue,
  5687. BOOL *stop) {
  5688. #pragma unused(stop)
  5689. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5690. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5691. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5692. }];
  5693. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5694. result += tagSize * count;
  5695. return result;
  5696. }
  5697. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5698. asField:(GPBFieldDescriptor *)field {
  5699. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5700. GPBDataType keyDataType = field.mapKeyDataType;
  5701. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5702. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5703. NSNumber *aValue,
  5704. BOOL *stop) {
  5705. #pragma unused(stop)
  5706. // Write the tag.
  5707. [outputStream writeInt32NoTag:tag];
  5708. // Write the size of the message.
  5709. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5710. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5711. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5712. // Write the fields.
  5713. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5714. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  5715. }];
  5716. }
  5717. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5718. forGPBGenericValueKey:(GPBGenericValue *)key {
  5719. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5720. }
  5721. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5722. [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5723. #pragma unused(stop)
  5724. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5725. }];
  5726. }
  5727. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
  5728. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5729. if (wrapped && value) {
  5730. *value = [wrapped boolValue];
  5731. }
  5732. return (wrapped != NULL);
  5733. }
  5734. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5735. if (otherDictionary) {
  5736. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5737. if (_autocreator) {
  5738. GPBAutocreatedDictionaryModified(_autocreator, self);
  5739. }
  5740. }
  5741. }
  5742. - (void)setBool:(BOOL)value forKey:(uint64_t)key {
  5743. [_dictionary setObject:@(value) forKey:@(key)];
  5744. if (_autocreator) {
  5745. GPBAutocreatedDictionaryModified(_autocreator, self);
  5746. }
  5747. }
  5748. - (void)removeBoolForKey:(uint64_t)aKey {
  5749. [_dictionary removeObjectForKey:@(aKey)];
  5750. }
  5751. - (void)removeAll {
  5752. [_dictionary removeAllObjects];
  5753. }
  5754. @end
  5755. #pragma mark - UInt64 -> Float
  5756. @implementation GPBUInt64FloatDictionary {
  5757. @package
  5758. NSMutableDictionary *_dictionary;
  5759. }
  5760. + (instancetype)dictionary {
  5761. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  5762. }
  5763. + (instancetype)dictionaryWithFloat:(float)value
  5764. forKey:(uint64_t)key {
  5765. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  5766. // on to get the type correct.
  5767. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:&value
  5768. forKeys:&key
  5769. count:1] autorelease];
  5770. }
  5771. + (instancetype)dictionaryWithFloats:(const float [])values
  5772. forKeys:(const uint64_t [])keys
  5773. count:(NSUInteger)count {
  5774. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  5775. // on to get the type correct.
  5776. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithFloats:values
  5777. forKeys:keys
  5778. count:count] autorelease];
  5779. }
  5780. + (instancetype)dictionaryWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5781. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5782. // on to get the type correct.
  5783. return [[(GPBUInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5784. }
  5785. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5786. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5787. }
  5788. - (instancetype)init {
  5789. return [self initWithFloats:NULL forKeys:NULL count:0];
  5790. }
  5791. - (instancetype)initWithFloats:(const float [])values
  5792. forKeys:(const uint64_t [])keys
  5793. count:(NSUInteger)count {
  5794. self = [super init];
  5795. if (self) {
  5796. _dictionary = [[NSMutableDictionary alloc] init];
  5797. if (count && values && keys) {
  5798. for (NSUInteger i = 0; i < count; ++i) {
  5799. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5800. }
  5801. }
  5802. }
  5803. return self;
  5804. }
  5805. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5806. self = [self initWithFloats:NULL forKeys:NULL count:0];
  5807. if (self) {
  5808. if (dictionary) {
  5809. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5810. }
  5811. }
  5812. return self;
  5813. }
  5814. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5815. #pragma unused(numItems)
  5816. return [self initWithFloats:NULL forKeys:NULL count:0];
  5817. }
  5818. - (void)dealloc {
  5819. NSAssert(!_autocreator,
  5820. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5821. [self class], _autocreator);
  5822. [_dictionary release];
  5823. [super dealloc];
  5824. }
  5825. - (instancetype)copyWithZone:(NSZone *)zone {
  5826. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5827. }
  5828. - (BOOL)isEqual:(id)other {
  5829. if (self == other) {
  5830. return YES;
  5831. }
  5832. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5833. return NO;
  5834. }
  5835. GPBUInt64FloatDictionary *otherDictionary = other;
  5836. return [_dictionary isEqual:otherDictionary->_dictionary];
  5837. }
  5838. - (NSUInteger)hash {
  5839. return _dictionary.count;
  5840. }
  5841. - (NSString *)description {
  5842. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5843. }
  5844. - (NSUInteger)count {
  5845. return _dictionary.count;
  5846. }
  5847. - (void)enumerateKeysAndFloatsUsingBlock:
  5848. (void (^)(uint64_t key, float value, BOOL *stop))block {
  5849. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5850. NSNumber *aValue,
  5851. BOOL *stop) {
  5852. block([aKey unsignedLongLongValue], [aValue floatValue], stop);
  5853. }];
  5854. }
  5855. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5856. NSUInteger count = _dictionary.count;
  5857. if (count == 0) {
  5858. return 0;
  5859. }
  5860. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5861. GPBDataType keyDataType = field.mapKeyDataType;
  5862. __block size_t result = 0;
  5863. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5864. NSNumber *aValue,
  5865. BOOL *stop) {
  5866. #pragma unused(stop)
  5867. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5868. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5869. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5870. }];
  5871. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5872. result += tagSize * count;
  5873. return result;
  5874. }
  5875. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5876. asField:(GPBFieldDescriptor *)field {
  5877. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5878. GPBDataType keyDataType = field.mapKeyDataType;
  5879. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5880. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  5881. NSNumber *aValue,
  5882. BOOL *stop) {
  5883. #pragma unused(stop)
  5884. // Write the tag.
  5885. [outputStream writeInt32NoTag:tag];
  5886. // Write the size of the message.
  5887. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5888. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5889. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5890. // Write the fields.
  5891. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5892. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  5893. }];
  5894. }
  5895. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5896. forGPBGenericValueKey:(GPBGenericValue *)key {
  5897. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5898. }
  5899. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  5900. [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5901. #pragma unused(stop)
  5902. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5903. }];
  5904. }
  5905. - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
  5906. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5907. if (wrapped && value) {
  5908. *value = [wrapped floatValue];
  5909. }
  5910. return (wrapped != NULL);
  5911. }
  5912. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5913. if (otherDictionary) {
  5914. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5915. if (_autocreator) {
  5916. GPBAutocreatedDictionaryModified(_autocreator, self);
  5917. }
  5918. }
  5919. }
  5920. - (void)setFloat:(float)value forKey:(uint64_t)key {
  5921. [_dictionary setObject:@(value) forKey:@(key)];
  5922. if (_autocreator) {
  5923. GPBAutocreatedDictionaryModified(_autocreator, self);
  5924. }
  5925. }
  5926. - (void)removeFloatForKey:(uint64_t)aKey {
  5927. [_dictionary removeObjectForKey:@(aKey)];
  5928. }
  5929. - (void)removeAll {
  5930. [_dictionary removeAllObjects];
  5931. }
  5932. @end
  5933. #pragma mark - UInt64 -> Double
  5934. @implementation GPBUInt64DoubleDictionary {
  5935. @package
  5936. NSMutableDictionary *_dictionary;
  5937. }
  5938. + (instancetype)dictionary {
  5939. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  5940. }
  5941. + (instancetype)dictionaryWithDouble:(double)value
  5942. forKey:(uint64_t)key {
  5943. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  5944. // on to get the type correct.
  5945. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
  5946. forKeys:&key
  5947. count:1] autorelease];
  5948. }
  5949. + (instancetype)dictionaryWithDoubles:(const double [])values
  5950. forKeys:(const uint64_t [])keys
  5951. count:(NSUInteger)count {
  5952. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  5953. // on to get the type correct.
  5954. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDoubles:values
  5955. forKeys:keys
  5956. count:count] autorelease];
  5957. }
  5958. + (instancetype)dictionaryWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5959. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  5960. // on to get the type correct.
  5961. return [[(GPBUInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  5962. }
  5963. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  5964. return [[[self alloc] initWithCapacity:numItems] autorelease];
  5965. }
  5966. - (instancetype)init {
  5967. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5968. }
  5969. - (instancetype)initWithDoubles:(const double [])values
  5970. forKeys:(const uint64_t [])keys
  5971. count:(NSUInteger)count {
  5972. self = [super init];
  5973. if (self) {
  5974. _dictionary = [[NSMutableDictionary alloc] init];
  5975. if (count && values && keys) {
  5976. for (NSUInteger i = 0; i < count; ++i) {
  5977. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5978. }
  5979. }
  5980. }
  5981. return self;
  5982. }
  5983. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5984. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  5985. if (self) {
  5986. if (dictionary) {
  5987. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5988. }
  5989. }
  5990. return self;
  5991. }
  5992. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5993. #pragma unused(numItems)
  5994. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5995. }
  5996. - (void)dealloc {
  5997. NSAssert(!_autocreator,
  5998. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5999. [self class], _autocreator);
  6000. [_dictionary release];
  6001. [super dealloc];
  6002. }
  6003. - (instancetype)copyWithZone:(NSZone *)zone {
  6004. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6005. }
  6006. - (BOOL)isEqual:(id)other {
  6007. if (self == other) {
  6008. return YES;
  6009. }
  6010. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  6011. return NO;
  6012. }
  6013. GPBUInt64DoubleDictionary *otherDictionary = other;
  6014. return [_dictionary isEqual:otherDictionary->_dictionary];
  6015. }
  6016. - (NSUInteger)hash {
  6017. return _dictionary.count;
  6018. }
  6019. - (NSString *)description {
  6020. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6021. }
  6022. - (NSUInteger)count {
  6023. return _dictionary.count;
  6024. }
  6025. - (void)enumerateKeysAndDoublesUsingBlock:
  6026. (void (^)(uint64_t key, double value, BOOL *stop))block {
  6027. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6028. NSNumber *aValue,
  6029. BOOL *stop) {
  6030. block([aKey unsignedLongLongValue], [aValue doubleValue], stop);
  6031. }];
  6032. }
  6033. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6034. NSUInteger count = _dictionary.count;
  6035. if (count == 0) {
  6036. return 0;
  6037. }
  6038. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6039. GPBDataType keyDataType = field.mapKeyDataType;
  6040. __block size_t result = 0;
  6041. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6042. NSNumber *aValue,
  6043. BOOL *stop) {
  6044. #pragma unused(stop)
  6045. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6046. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6047. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6048. }];
  6049. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6050. result += tagSize * count;
  6051. return result;
  6052. }
  6053. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6054. asField:(GPBFieldDescriptor *)field {
  6055. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6056. GPBDataType keyDataType = field.mapKeyDataType;
  6057. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6058. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6059. NSNumber *aValue,
  6060. BOOL *stop) {
  6061. #pragma unused(stop)
  6062. // Write the tag.
  6063. [outputStream writeInt32NoTag:tag];
  6064. // Write the size of the message.
  6065. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6066. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6067. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6068. // Write the fields.
  6069. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6070. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6071. }];
  6072. }
  6073. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6074. forGPBGenericValueKey:(GPBGenericValue *)key {
  6075. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  6076. }
  6077. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6078. [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  6079. #pragma unused(stop)
  6080. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  6081. }];
  6082. }
  6083. - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
  6084. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6085. if (wrapped && value) {
  6086. *value = [wrapped doubleValue];
  6087. }
  6088. return (wrapped != NULL);
  6089. }
  6090. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  6091. if (otherDictionary) {
  6092. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6093. if (_autocreator) {
  6094. GPBAutocreatedDictionaryModified(_autocreator, self);
  6095. }
  6096. }
  6097. }
  6098. - (void)setDouble:(double)value forKey:(uint64_t)key {
  6099. [_dictionary setObject:@(value) forKey:@(key)];
  6100. if (_autocreator) {
  6101. GPBAutocreatedDictionaryModified(_autocreator, self);
  6102. }
  6103. }
  6104. - (void)removeDoubleForKey:(uint64_t)aKey {
  6105. [_dictionary removeObjectForKey:@(aKey)];
  6106. }
  6107. - (void)removeAll {
  6108. [_dictionary removeAllObjects];
  6109. }
  6110. @end
  6111. #pragma mark - UInt64 -> Enum
  6112. @implementation GPBUInt64EnumDictionary {
  6113. @package
  6114. NSMutableDictionary *_dictionary;
  6115. GPBEnumValidationFunc _validationFunc;
  6116. }
  6117. @synthesize validationFunc = _validationFunc;
  6118. + (instancetype)dictionary {
  6119. return [[[self alloc] initWithValidationFunction:NULL
  6120. rawValues:NULL
  6121. forKeys:NULL
  6122. count:0] autorelease];
  6123. }
  6124. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  6125. return [[[self alloc] initWithValidationFunction:func
  6126. rawValues:NULL
  6127. forKeys:NULL
  6128. count:0] autorelease];
  6129. }
  6130. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6131. rawValue:(int32_t)rawValue
  6132. forKey:(uint64_t)key {
  6133. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6134. // on to get the type correct.
  6135. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6136. rawValues:&rawValue
  6137. forKeys:&key
  6138. count:1] autorelease];
  6139. }
  6140. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6141. rawValues:(const int32_t [])rawValues
  6142. forKeys:(const uint64_t [])keys
  6143. count:(NSUInteger)count {
  6144. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6145. // on to get the type correct.
  6146. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  6147. rawValues:rawValues
  6148. forKeys:keys
  6149. count:count] autorelease];
  6150. }
  6151. + (instancetype)dictionaryWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6152. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  6153. // on to get the type correct.
  6154. return [[(GPBUInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6155. }
  6156. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  6157. capacity:(NSUInteger)numItems {
  6158. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  6159. }
  6160. - (instancetype)init {
  6161. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  6162. }
  6163. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  6164. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6165. }
  6166. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6167. rawValues:(const int32_t [])rawValues
  6168. forKeys:(const uint64_t [])keys
  6169. count:(NSUInteger)count {
  6170. self = [super init];
  6171. if (self) {
  6172. _dictionary = [[NSMutableDictionary alloc] init];
  6173. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  6174. if (count && rawValues && keys) {
  6175. for (NSUInteger i = 0; i < count; ++i) {
  6176. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  6177. }
  6178. }
  6179. }
  6180. return self;
  6181. }
  6182. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  6183. self = [self initWithValidationFunction:dictionary.validationFunc
  6184. rawValues:NULL
  6185. forKeys:NULL
  6186. count:0];
  6187. if (self) {
  6188. if (dictionary) {
  6189. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6190. }
  6191. }
  6192. return self;
  6193. }
  6194. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  6195. capacity:(NSUInteger)numItems {
  6196. #pragma unused(numItems)
  6197. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  6198. }
  6199. - (void)dealloc {
  6200. NSAssert(!_autocreator,
  6201. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6202. [self class], _autocreator);
  6203. [_dictionary release];
  6204. [super dealloc];
  6205. }
  6206. - (instancetype)copyWithZone:(NSZone *)zone {
  6207. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  6208. }
  6209. - (BOOL)isEqual:(id)other {
  6210. if (self == other) {
  6211. return YES;
  6212. }
  6213. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  6214. return NO;
  6215. }
  6216. GPBUInt64EnumDictionary *otherDictionary = other;
  6217. return [_dictionary isEqual:otherDictionary->_dictionary];
  6218. }
  6219. - (NSUInteger)hash {
  6220. return _dictionary.count;
  6221. }
  6222. - (NSString *)description {
  6223. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6224. }
  6225. - (NSUInteger)count {
  6226. return _dictionary.count;
  6227. }
  6228. - (void)enumerateKeysAndRawValuesUsingBlock:
  6229. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6230. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6231. NSNumber *aValue,
  6232. BOOL *stop) {
  6233. block([aKey unsignedLongLongValue], [aValue intValue], stop);
  6234. }];
  6235. }
  6236. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6237. NSUInteger count = _dictionary.count;
  6238. if (count == 0) {
  6239. return 0;
  6240. }
  6241. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6242. GPBDataType keyDataType = field.mapKeyDataType;
  6243. __block size_t result = 0;
  6244. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6245. NSNumber *aValue,
  6246. BOOL *stop) {
  6247. #pragma unused(stop)
  6248. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6249. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6250. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6251. }];
  6252. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6253. result += tagSize * count;
  6254. return result;
  6255. }
  6256. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6257. asField:(GPBFieldDescriptor *)field {
  6258. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6259. GPBDataType keyDataType = field.mapKeyDataType;
  6260. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6261. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6262. NSNumber *aValue,
  6263. BOOL *stop) {
  6264. #pragma unused(stop)
  6265. // Write the tag.
  6266. [outputStream writeInt32NoTag:tag];
  6267. // Write the size of the message.
  6268. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6269. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6270. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6271. // Write the fields.
  6272. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6273. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6274. }];
  6275. }
  6276. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  6277. forKey:(GPBGenericValue *)key
  6278. keyDataType:(GPBDataType)keyDataType {
  6279. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6280. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  6281. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  6282. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  6283. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  6284. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  6285. [outputStream release];
  6286. return data;
  6287. }
  6288. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6289. forGPBGenericValueKey:(GPBGenericValue *)key {
  6290. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  6291. }
  6292. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6293. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  6294. #pragma unused(stop)
  6295. block([NSString stringWithFormat:@"%llu", key], @(value));
  6296. }];
  6297. }
  6298. - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
  6299. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6300. if (wrapped && value) {
  6301. int32_t result = [wrapped intValue];
  6302. if (!_validationFunc(result)) {
  6303. result = kGPBUnrecognizedEnumeratorValue;
  6304. }
  6305. *value = result;
  6306. }
  6307. return (wrapped != NULL);
  6308. }
  6309. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
  6310. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6311. if (wrapped && rawValue) {
  6312. *rawValue = [wrapped intValue];
  6313. }
  6314. return (wrapped != NULL);
  6315. }
  6316. - (void)enumerateKeysAndEnumsUsingBlock:
  6317. (void (^)(uint64_t key, int32_t value, BOOL *stop))block {
  6318. GPBEnumValidationFunc func = _validationFunc;
  6319. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6320. NSNumber *aValue,
  6321. BOOL *stop) {
  6322. int32_t unwrapped = [aValue intValue];
  6323. if (!func(unwrapped)) {
  6324. unwrapped = kGPBUnrecognizedEnumeratorValue;
  6325. }
  6326. block([aKey unsignedLongLongValue], unwrapped, stop);
  6327. }];
  6328. }
  6329. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  6330. if (otherDictionary) {
  6331. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6332. if (_autocreator) {
  6333. GPBAutocreatedDictionaryModified(_autocreator, self);
  6334. }
  6335. }
  6336. }
  6337. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  6338. [_dictionary setObject:@(value) forKey:@(key)];
  6339. if (_autocreator) {
  6340. GPBAutocreatedDictionaryModified(_autocreator, self);
  6341. }
  6342. }
  6343. - (void)removeEnumForKey:(uint64_t)aKey {
  6344. [_dictionary removeObjectForKey:@(aKey)];
  6345. }
  6346. - (void)removeAll {
  6347. [_dictionary removeAllObjects];
  6348. }
  6349. - (void)setEnum:(int32_t)value forKey:(uint64_t)key {
  6350. if (!_validationFunc(value)) {
  6351. [NSException raise:NSInvalidArgumentException
  6352. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  6353. value];
  6354. }
  6355. [_dictionary setObject:@(value) forKey:@(key)];
  6356. if (_autocreator) {
  6357. GPBAutocreatedDictionaryModified(_autocreator, self);
  6358. }
  6359. }
  6360. @end
  6361. #pragma mark - UInt64 -> Object
  6362. @implementation GPBUInt64ObjectDictionary {
  6363. @package
  6364. NSMutableDictionary *_dictionary;
  6365. }
  6366. + (instancetype)dictionary {
  6367. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  6368. }
  6369. + (instancetype)dictionaryWithObject:(id)object
  6370. forKey:(uint64_t)key {
  6371. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6372. // on to get the type correct.
  6373. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  6374. forKeys:&key
  6375. count:1] autorelease];
  6376. }
  6377. + (instancetype)dictionaryWithObjects:(const id [])objects
  6378. forKeys:(const uint64_t [])keys
  6379. count:(NSUInteger)count {
  6380. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  6381. // on to get the type correct.
  6382. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  6383. forKeys:keys
  6384. count:count] autorelease];
  6385. }
  6386. + (instancetype)dictionaryWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6387. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6388. // on to get the type correct.
  6389. return [[(GPBUInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6390. }
  6391. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6392. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6393. }
  6394. - (instancetype)init {
  6395. return [self initWithObjects:NULL forKeys:NULL count:0];
  6396. }
  6397. - (instancetype)initWithObjects:(const id [])objects
  6398. forKeys:(const uint64_t [])keys
  6399. count:(NSUInteger)count {
  6400. self = [super init];
  6401. if (self) {
  6402. _dictionary = [[NSMutableDictionary alloc] init];
  6403. if (count && objects && keys) {
  6404. for (NSUInteger i = 0; i < count; ++i) {
  6405. if (!objects[i]) {
  6406. [NSException raise:NSInvalidArgumentException
  6407. format:@"Attempting to add nil object to a Dictionary"];
  6408. }
  6409. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  6410. }
  6411. }
  6412. }
  6413. return self;
  6414. }
  6415. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  6416. self = [self initWithObjects:NULL forKeys:NULL count:0];
  6417. if (self) {
  6418. if (dictionary) {
  6419. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6420. }
  6421. }
  6422. return self;
  6423. }
  6424. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6425. #pragma unused(numItems)
  6426. return [self initWithObjects:NULL forKeys:NULL count:0];
  6427. }
  6428. - (void)dealloc {
  6429. NSAssert(!_autocreator,
  6430. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6431. [self class], _autocreator);
  6432. [_dictionary release];
  6433. [super dealloc];
  6434. }
  6435. - (instancetype)copyWithZone:(NSZone *)zone {
  6436. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  6437. }
  6438. - (BOOL)isEqual:(id)other {
  6439. if (self == other) {
  6440. return YES;
  6441. }
  6442. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  6443. return NO;
  6444. }
  6445. GPBUInt64ObjectDictionary *otherDictionary = other;
  6446. return [_dictionary isEqual:otherDictionary->_dictionary];
  6447. }
  6448. - (NSUInteger)hash {
  6449. return _dictionary.count;
  6450. }
  6451. - (NSString *)description {
  6452. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6453. }
  6454. - (NSUInteger)count {
  6455. return _dictionary.count;
  6456. }
  6457. - (void)enumerateKeysAndObjectsUsingBlock:
  6458. (void (^)(uint64_t key, id object, BOOL *stop))block {
  6459. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6460. id aObject,
  6461. BOOL *stop) {
  6462. block([aKey unsignedLongLongValue], aObject, stop);
  6463. }];
  6464. }
  6465. - (BOOL)isInitialized {
  6466. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  6467. if (!msg.initialized) {
  6468. return NO;
  6469. }
  6470. }
  6471. return YES;
  6472. }
  6473. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  6474. GPBUInt64ObjectDictionary *newDict =
  6475. [[GPBUInt64ObjectDictionary alloc] init];
  6476. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  6477. GPBMessage *msg,
  6478. BOOL *stop) {
  6479. #pragma unused(stop)
  6480. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  6481. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  6482. [copiedMsg release];
  6483. }];
  6484. return newDict;
  6485. }
  6486. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6487. NSUInteger count = _dictionary.count;
  6488. if (count == 0) {
  6489. return 0;
  6490. }
  6491. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6492. GPBDataType keyDataType = field.mapKeyDataType;
  6493. __block size_t result = 0;
  6494. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6495. id aObject,
  6496. BOOL *stop) {
  6497. #pragma unused(stop)
  6498. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6499. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6500. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6501. }];
  6502. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6503. result += tagSize * count;
  6504. return result;
  6505. }
  6506. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6507. asField:(GPBFieldDescriptor *)field {
  6508. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6509. GPBDataType keyDataType = field.mapKeyDataType;
  6510. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6511. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6512. id aObject,
  6513. BOOL *stop) {
  6514. #pragma unused(stop)
  6515. // Write the tag.
  6516. [outputStream writeInt32NoTag:tag];
  6517. // Write the size of the message.
  6518. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6519. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  6520. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6521. // Write the fields.
  6522. WriteDictUInt64Field(outputStream, [aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  6523. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  6524. }];
  6525. }
  6526. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6527. forGPBGenericValueKey:(GPBGenericValue *)key {
  6528. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  6529. }
  6530. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6531. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  6532. #pragma unused(stop)
  6533. block([NSString stringWithFormat:@"%llu", key], object);
  6534. }];
  6535. }
  6536. - (id)objectForKey:(uint64_t)key {
  6537. id result = [_dictionary objectForKey:@(key)];
  6538. return result;
  6539. }
  6540. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  6541. if (otherDictionary) {
  6542. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6543. if (_autocreator) {
  6544. GPBAutocreatedDictionaryModified(_autocreator, self);
  6545. }
  6546. }
  6547. }
  6548. - (void)setObject:(id)object forKey:(uint64_t)key {
  6549. if (!object) {
  6550. [NSException raise:NSInvalidArgumentException
  6551. format:@"Attempting to add nil object to a Dictionary"];
  6552. }
  6553. [_dictionary setObject:object forKey:@(key)];
  6554. if (_autocreator) {
  6555. GPBAutocreatedDictionaryModified(_autocreator, self);
  6556. }
  6557. }
  6558. - (void)removeObjectForKey:(uint64_t)aKey {
  6559. [_dictionary removeObjectForKey:@(aKey)];
  6560. }
  6561. - (void)removeAll {
  6562. [_dictionary removeAllObjects];
  6563. }
  6564. @end
  6565. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  6566. // This block of code is generated, do not edit it directly.
  6567. #pragma mark - Int64 -> UInt32
  6568. @implementation GPBInt64UInt32Dictionary {
  6569. @package
  6570. NSMutableDictionary *_dictionary;
  6571. }
  6572. + (instancetype)dictionary {
  6573. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  6574. }
  6575. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  6576. forKey:(int64_t)key {
  6577. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  6578. // on to get the type correct.
  6579. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:&value
  6580. forKeys:&key
  6581. count:1] autorelease];
  6582. }
  6583. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  6584. forKeys:(const int64_t [])keys
  6585. count:(NSUInteger)count {
  6586. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  6587. // on to get the type correct.
  6588. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithUInt32s:values
  6589. forKeys:keys
  6590. count:count] autorelease];
  6591. }
  6592. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6593. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6594. // on to get the type correct.
  6595. return [[(GPBInt64UInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6596. }
  6597. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6598. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6599. }
  6600. - (instancetype)init {
  6601. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  6602. }
  6603. - (instancetype)initWithUInt32s:(const uint32_t [])values
  6604. forKeys:(const int64_t [])keys
  6605. count:(NSUInteger)count {
  6606. self = [super init];
  6607. if (self) {
  6608. _dictionary = [[NSMutableDictionary alloc] init];
  6609. if (count && values && keys) {
  6610. for (NSUInteger i = 0; i < count; ++i) {
  6611. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6612. }
  6613. }
  6614. }
  6615. return self;
  6616. }
  6617. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  6618. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  6619. if (self) {
  6620. if (dictionary) {
  6621. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6622. }
  6623. }
  6624. return self;
  6625. }
  6626. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6627. #pragma unused(numItems)
  6628. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  6629. }
  6630. - (void)dealloc {
  6631. NSAssert(!_autocreator,
  6632. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6633. [self class], _autocreator);
  6634. [_dictionary release];
  6635. [super dealloc];
  6636. }
  6637. - (instancetype)copyWithZone:(NSZone *)zone {
  6638. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  6639. }
  6640. - (BOOL)isEqual:(id)other {
  6641. if (self == other) {
  6642. return YES;
  6643. }
  6644. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  6645. return NO;
  6646. }
  6647. GPBInt64UInt32Dictionary *otherDictionary = other;
  6648. return [_dictionary isEqual:otherDictionary->_dictionary];
  6649. }
  6650. - (NSUInteger)hash {
  6651. return _dictionary.count;
  6652. }
  6653. - (NSString *)description {
  6654. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6655. }
  6656. - (NSUInteger)count {
  6657. return _dictionary.count;
  6658. }
  6659. - (void)enumerateKeysAndUInt32sUsingBlock:
  6660. (void (^)(int64_t key, uint32_t value, BOOL *stop))block {
  6661. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6662. NSNumber *aValue,
  6663. BOOL *stop) {
  6664. block([aKey longLongValue], [aValue unsignedIntValue], stop);
  6665. }];
  6666. }
  6667. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6668. NSUInteger count = _dictionary.count;
  6669. if (count == 0) {
  6670. return 0;
  6671. }
  6672. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6673. GPBDataType keyDataType = field.mapKeyDataType;
  6674. __block size_t result = 0;
  6675. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6676. NSNumber *aValue,
  6677. BOOL *stop) {
  6678. #pragma unused(stop)
  6679. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6680. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6681. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6682. }];
  6683. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6684. result += tagSize * count;
  6685. return result;
  6686. }
  6687. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6688. asField:(GPBFieldDescriptor *)field {
  6689. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6690. GPBDataType keyDataType = field.mapKeyDataType;
  6691. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6692. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6693. NSNumber *aValue,
  6694. BOOL *stop) {
  6695. #pragma unused(stop)
  6696. // Write the tag.
  6697. [outputStream writeInt32NoTag:tag];
  6698. // Write the size of the message.
  6699. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6700. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6701. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6702. // Write the fields.
  6703. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6704. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6705. }];
  6706. }
  6707. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6708. forGPBGenericValueKey:(GPBGenericValue *)key {
  6709. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6710. }
  6711. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6712. [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6713. #pragma unused(stop)
  6714. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6715. }];
  6716. }
  6717. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
  6718. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6719. if (wrapped && value) {
  6720. *value = [wrapped unsignedIntValue];
  6721. }
  6722. return (wrapped != NULL);
  6723. }
  6724. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6725. if (otherDictionary) {
  6726. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6727. if (_autocreator) {
  6728. GPBAutocreatedDictionaryModified(_autocreator, self);
  6729. }
  6730. }
  6731. }
  6732. - (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
  6733. [_dictionary setObject:@(value) forKey:@(key)];
  6734. if (_autocreator) {
  6735. GPBAutocreatedDictionaryModified(_autocreator, self);
  6736. }
  6737. }
  6738. - (void)removeUInt32ForKey:(int64_t)aKey {
  6739. [_dictionary removeObjectForKey:@(aKey)];
  6740. }
  6741. - (void)removeAll {
  6742. [_dictionary removeAllObjects];
  6743. }
  6744. @end
  6745. #pragma mark - Int64 -> Int32
  6746. @implementation GPBInt64Int32Dictionary {
  6747. @package
  6748. NSMutableDictionary *_dictionary;
  6749. }
  6750. + (instancetype)dictionary {
  6751. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  6752. }
  6753. + (instancetype)dictionaryWithInt32:(int32_t)value
  6754. forKey:(int64_t)key {
  6755. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  6756. // on to get the type correct.
  6757. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:&value
  6758. forKeys:&key
  6759. count:1] autorelease];
  6760. }
  6761. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  6762. forKeys:(const int64_t [])keys
  6763. count:(NSUInteger)count {
  6764. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  6765. // on to get the type correct.
  6766. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithInt32s:values
  6767. forKeys:keys
  6768. count:count] autorelease];
  6769. }
  6770. + (instancetype)dictionaryWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6771. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6772. // on to get the type correct.
  6773. return [[(GPBInt64Int32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6774. }
  6775. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6776. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6777. }
  6778. - (instancetype)init {
  6779. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6780. }
  6781. - (instancetype)initWithInt32s:(const int32_t [])values
  6782. forKeys:(const int64_t [])keys
  6783. count:(NSUInteger)count {
  6784. self = [super init];
  6785. if (self) {
  6786. _dictionary = [[NSMutableDictionary alloc] init];
  6787. if (count && values && keys) {
  6788. for (NSUInteger i = 0; i < count; ++i) {
  6789. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6790. }
  6791. }
  6792. }
  6793. return self;
  6794. }
  6795. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6796. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  6797. if (self) {
  6798. if (dictionary) {
  6799. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6800. }
  6801. }
  6802. return self;
  6803. }
  6804. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6805. #pragma unused(numItems)
  6806. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6807. }
  6808. - (void)dealloc {
  6809. NSAssert(!_autocreator,
  6810. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6811. [self class], _autocreator);
  6812. [_dictionary release];
  6813. [super dealloc];
  6814. }
  6815. - (instancetype)copyWithZone:(NSZone *)zone {
  6816. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6817. }
  6818. - (BOOL)isEqual:(id)other {
  6819. if (self == other) {
  6820. return YES;
  6821. }
  6822. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6823. return NO;
  6824. }
  6825. GPBInt64Int32Dictionary *otherDictionary = other;
  6826. return [_dictionary isEqual:otherDictionary->_dictionary];
  6827. }
  6828. - (NSUInteger)hash {
  6829. return _dictionary.count;
  6830. }
  6831. - (NSString *)description {
  6832. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6833. }
  6834. - (NSUInteger)count {
  6835. return _dictionary.count;
  6836. }
  6837. - (void)enumerateKeysAndInt32sUsingBlock:
  6838. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  6839. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6840. NSNumber *aValue,
  6841. BOOL *stop) {
  6842. block([aKey longLongValue], [aValue intValue], stop);
  6843. }];
  6844. }
  6845. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6846. NSUInteger count = _dictionary.count;
  6847. if (count == 0) {
  6848. return 0;
  6849. }
  6850. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6851. GPBDataType keyDataType = field.mapKeyDataType;
  6852. __block size_t result = 0;
  6853. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6854. NSNumber *aValue,
  6855. BOOL *stop) {
  6856. #pragma unused(stop)
  6857. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6858. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6859. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6860. }];
  6861. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6862. result += tagSize * count;
  6863. return result;
  6864. }
  6865. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6866. asField:(GPBFieldDescriptor *)field {
  6867. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6868. GPBDataType keyDataType = field.mapKeyDataType;
  6869. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6870. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  6871. NSNumber *aValue,
  6872. BOOL *stop) {
  6873. #pragma unused(stop)
  6874. // Write the tag.
  6875. [outputStream writeInt32NoTag:tag];
  6876. // Write the size of the message.
  6877. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6878. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6879. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6880. // Write the fields.
  6881. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6882. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  6883. }];
  6884. }
  6885. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6886. forGPBGenericValueKey:(GPBGenericValue *)key {
  6887. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6888. }
  6889. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  6890. [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6891. #pragma unused(stop)
  6892. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6893. }];
  6894. }
  6895. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
  6896. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6897. if (wrapped && value) {
  6898. *value = [wrapped intValue];
  6899. }
  6900. return (wrapped != NULL);
  6901. }
  6902. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6903. if (otherDictionary) {
  6904. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6905. if (_autocreator) {
  6906. GPBAutocreatedDictionaryModified(_autocreator, self);
  6907. }
  6908. }
  6909. }
  6910. - (void)setInt32:(int32_t)value forKey:(int64_t)key {
  6911. [_dictionary setObject:@(value) forKey:@(key)];
  6912. if (_autocreator) {
  6913. GPBAutocreatedDictionaryModified(_autocreator, self);
  6914. }
  6915. }
  6916. - (void)removeInt32ForKey:(int64_t)aKey {
  6917. [_dictionary removeObjectForKey:@(aKey)];
  6918. }
  6919. - (void)removeAll {
  6920. [_dictionary removeAllObjects];
  6921. }
  6922. @end
  6923. #pragma mark - Int64 -> UInt64
  6924. @implementation GPBInt64UInt64Dictionary {
  6925. @package
  6926. NSMutableDictionary *_dictionary;
  6927. }
  6928. + (instancetype)dictionary {
  6929. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  6930. }
  6931. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  6932. forKey:(int64_t)key {
  6933. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  6934. // on to get the type correct.
  6935. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:&value
  6936. forKeys:&key
  6937. count:1] autorelease];
  6938. }
  6939. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  6940. forKeys:(const int64_t [])keys
  6941. count:(NSUInteger)count {
  6942. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  6943. // on to get the type correct.
  6944. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithUInt64s:values
  6945. forKeys:keys
  6946. count:count] autorelease];
  6947. }
  6948. + (instancetype)dictionaryWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6949. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  6950. // on to get the type correct.
  6951. return [[(GPBInt64UInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  6952. }
  6953. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  6954. return [[[self alloc] initWithCapacity:numItems] autorelease];
  6955. }
  6956. - (instancetype)init {
  6957. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6958. }
  6959. - (instancetype)initWithUInt64s:(const uint64_t [])values
  6960. forKeys:(const int64_t [])keys
  6961. count:(NSUInteger)count {
  6962. self = [super init];
  6963. if (self) {
  6964. _dictionary = [[NSMutableDictionary alloc] init];
  6965. if (count && values && keys) {
  6966. for (NSUInteger i = 0; i < count; ++i) {
  6967. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6968. }
  6969. }
  6970. }
  6971. return self;
  6972. }
  6973. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6974. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  6975. if (self) {
  6976. if (dictionary) {
  6977. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6978. }
  6979. }
  6980. return self;
  6981. }
  6982. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6983. #pragma unused(numItems)
  6984. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6985. }
  6986. - (void)dealloc {
  6987. NSAssert(!_autocreator,
  6988. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6989. [self class], _autocreator);
  6990. [_dictionary release];
  6991. [super dealloc];
  6992. }
  6993. - (instancetype)copyWithZone:(NSZone *)zone {
  6994. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6995. }
  6996. - (BOOL)isEqual:(id)other {
  6997. if (self == other) {
  6998. return YES;
  6999. }
  7000. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  7001. return NO;
  7002. }
  7003. GPBInt64UInt64Dictionary *otherDictionary = other;
  7004. return [_dictionary isEqual:otherDictionary->_dictionary];
  7005. }
  7006. - (NSUInteger)hash {
  7007. return _dictionary.count;
  7008. }
  7009. - (NSString *)description {
  7010. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7011. }
  7012. - (NSUInteger)count {
  7013. return _dictionary.count;
  7014. }
  7015. - (void)enumerateKeysAndUInt64sUsingBlock:
  7016. (void (^)(int64_t key, uint64_t value, BOOL *stop))block {
  7017. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7018. NSNumber *aValue,
  7019. BOOL *stop) {
  7020. block([aKey longLongValue], [aValue unsignedLongLongValue], stop);
  7021. }];
  7022. }
  7023. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7024. NSUInteger count = _dictionary.count;
  7025. if (count == 0) {
  7026. return 0;
  7027. }
  7028. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7029. GPBDataType keyDataType = field.mapKeyDataType;
  7030. __block size_t result = 0;
  7031. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7032. NSNumber *aValue,
  7033. BOOL *stop) {
  7034. #pragma unused(stop)
  7035. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7036. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7037. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7038. }];
  7039. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7040. result += tagSize * count;
  7041. return result;
  7042. }
  7043. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7044. asField:(GPBFieldDescriptor *)field {
  7045. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7046. GPBDataType keyDataType = field.mapKeyDataType;
  7047. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7048. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7049. NSNumber *aValue,
  7050. BOOL *stop) {
  7051. #pragma unused(stop)
  7052. // Write the tag.
  7053. [outputStream writeInt32NoTag:tag];
  7054. // Write the size of the message.
  7055. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7056. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7057. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7058. // Write the fields.
  7059. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7060. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7061. }];
  7062. }
  7063. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7064. forGPBGenericValueKey:(GPBGenericValue *)key {
  7065. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  7066. }
  7067. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7068. [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  7069. #pragma unused(stop)
  7070. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  7071. }];
  7072. }
  7073. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
  7074. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7075. if (wrapped && value) {
  7076. *value = [wrapped unsignedLongLongValue];
  7077. }
  7078. return (wrapped != NULL);
  7079. }
  7080. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  7081. if (otherDictionary) {
  7082. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7083. if (_autocreator) {
  7084. GPBAutocreatedDictionaryModified(_autocreator, self);
  7085. }
  7086. }
  7087. }
  7088. - (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
  7089. [_dictionary setObject:@(value) forKey:@(key)];
  7090. if (_autocreator) {
  7091. GPBAutocreatedDictionaryModified(_autocreator, self);
  7092. }
  7093. }
  7094. - (void)removeUInt64ForKey:(int64_t)aKey {
  7095. [_dictionary removeObjectForKey:@(aKey)];
  7096. }
  7097. - (void)removeAll {
  7098. [_dictionary removeAllObjects];
  7099. }
  7100. @end
  7101. #pragma mark - Int64 -> Int64
  7102. @implementation GPBInt64Int64Dictionary {
  7103. @package
  7104. NSMutableDictionary *_dictionary;
  7105. }
  7106. + (instancetype)dictionary {
  7107. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  7108. }
  7109. + (instancetype)dictionaryWithInt64:(int64_t)value
  7110. forKey:(int64_t)key {
  7111. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  7112. // on to get the type correct.
  7113. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:&value
  7114. forKeys:&key
  7115. count:1] autorelease];
  7116. }
  7117. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  7118. forKeys:(const int64_t [])keys
  7119. count:(NSUInteger)count {
  7120. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  7121. // on to get the type correct.
  7122. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithInt64s:values
  7123. forKeys:keys
  7124. count:count] autorelease];
  7125. }
  7126. + (instancetype)dictionaryWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7127. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7128. // on to get the type correct.
  7129. return [[(GPBInt64Int64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7130. }
  7131. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7132. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7133. }
  7134. - (instancetype)init {
  7135. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7136. }
  7137. - (instancetype)initWithInt64s:(const int64_t [])values
  7138. forKeys:(const int64_t [])keys
  7139. count:(NSUInteger)count {
  7140. self = [super init];
  7141. if (self) {
  7142. _dictionary = [[NSMutableDictionary alloc] init];
  7143. if (count && values && keys) {
  7144. for (NSUInteger i = 0; i < count; ++i) {
  7145. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7146. }
  7147. }
  7148. }
  7149. return self;
  7150. }
  7151. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  7152. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  7153. if (self) {
  7154. if (dictionary) {
  7155. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7156. }
  7157. }
  7158. return self;
  7159. }
  7160. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7161. #pragma unused(numItems)
  7162. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7163. }
  7164. - (void)dealloc {
  7165. NSAssert(!_autocreator,
  7166. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7167. [self class], _autocreator);
  7168. [_dictionary release];
  7169. [super dealloc];
  7170. }
  7171. - (instancetype)copyWithZone:(NSZone *)zone {
  7172. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  7173. }
  7174. - (BOOL)isEqual:(id)other {
  7175. if (self == other) {
  7176. return YES;
  7177. }
  7178. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  7179. return NO;
  7180. }
  7181. GPBInt64Int64Dictionary *otherDictionary = other;
  7182. return [_dictionary isEqual:otherDictionary->_dictionary];
  7183. }
  7184. - (NSUInteger)hash {
  7185. return _dictionary.count;
  7186. }
  7187. - (NSString *)description {
  7188. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7189. }
  7190. - (NSUInteger)count {
  7191. return _dictionary.count;
  7192. }
  7193. - (void)enumerateKeysAndInt64sUsingBlock:
  7194. (void (^)(int64_t key, int64_t value, BOOL *stop))block {
  7195. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7196. NSNumber *aValue,
  7197. BOOL *stop) {
  7198. block([aKey longLongValue], [aValue longLongValue], stop);
  7199. }];
  7200. }
  7201. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7202. NSUInteger count = _dictionary.count;
  7203. if (count == 0) {
  7204. return 0;
  7205. }
  7206. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7207. GPBDataType keyDataType = field.mapKeyDataType;
  7208. __block size_t result = 0;
  7209. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7210. NSNumber *aValue,
  7211. BOOL *stop) {
  7212. #pragma unused(stop)
  7213. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7214. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7215. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7216. }];
  7217. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7218. result += tagSize * count;
  7219. return result;
  7220. }
  7221. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7222. asField:(GPBFieldDescriptor *)field {
  7223. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7224. GPBDataType keyDataType = field.mapKeyDataType;
  7225. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7226. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7227. NSNumber *aValue,
  7228. BOOL *stop) {
  7229. #pragma unused(stop)
  7230. // Write the tag.
  7231. [outputStream writeInt32NoTag:tag];
  7232. // Write the size of the message.
  7233. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7234. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7235. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7236. // Write the fields.
  7237. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7238. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  7239. }];
  7240. }
  7241. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7242. forGPBGenericValueKey:(GPBGenericValue *)key {
  7243. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  7244. }
  7245. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7246. [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  7247. #pragma unused(stop)
  7248. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  7249. }];
  7250. }
  7251. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
  7252. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7253. if (wrapped && value) {
  7254. *value = [wrapped longLongValue];
  7255. }
  7256. return (wrapped != NULL);
  7257. }
  7258. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  7259. if (otherDictionary) {
  7260. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7261. if (_autocreator) {
  7262. GPBAutocreatedDictionaryModified(_autocreator, self);
  7263. }
  7264. }
  7265. }
  7266. - (void)setInt64:(int64_t)value forKey:(int64_t)key {
  7267. [_dictionary setObject:@(value) forKey:@(key)];
  7268. if (_autocreator) {
  7269. GPBAutocreatedDictionaryModified(_autocreator, self);
  7270. }
  7271. }
  7272. - (void)removeInt64ForKey:(int64_t)aKey {
  7273. [_dictionary removeObjectForKey:@(aKey)];
  7274. }
  7275. - (void)removeAll {
  7276. [_dictionary removeAllObjects];
  7277. }
  7278. @end
  7279. #pragma mark - Int64 -> Bool
  7280. @implementation GPBInt64BoolDictionary {
  7281. @package
  7282. NSMutableDictionary *_dictionary;
  7283. }
  7284. + (instancetype)dictionary {
  7285. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  7286. }
  7287. + (instancetype)dictionaryWithBool:(BOOL)value
  7288. forKey:(int64_t)key {
  7289. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  7290. // on to get the type correct.
  7291. return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:&value
  7292. forKeys:&key
  7293. count:1] autorelease];
  7294. }
  7295. + (instancetype)dictionaryWithBools:(const BOOL [])values
  7296. forKeys:(const int64_t [])keys
  7297. count:(NSUInteger)count {
  7298. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  7299. // on to get the type correct.
  7300. return [[(GPBInt64BoolDictionary*)[self alloc] initWithBools:values
  7301. forKeys:keys
  7302. count:count] autorelease];
  7303. }
  7304. + (instancetype)dictionaryWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7305. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7306. // on to get the type correct.
  7307. return [[(GPBInt64BoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7308. }
  7309. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7310. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7311. }
  7312. - (instancetype)init {
  7313. return [self initWithBools:NULL forKeys:NULL count:0];
  7314. }
  7315. - (instancetype)initWithBools:(const BOOL [])values
  7316. forKeys:(const int64_t [])keys
  7317. count:(NSUInteger)count {
  7318. self = [super init];
  7319. if (self) {
  7320. _dictionary = [[NSMutableDictionary alloc] init];
  7321. if (count && values && keys) {
  7322. for (NSUInteger i = 0; i < count; ++i) {
  7323. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7324. }
  7325. }
  7326. }
  7327. return self;
  7328. }
  7329. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  7330. self = [self initWithBools:NULL forKeys:NULL count:0];
  7331. if (self) {
  7332. if (dictionary) {
  7333. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7334. }
  7335. }
  7336. return self;
  7337. }
  7338. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7339. #pragma unused(numItems)
  7340. return [self initWithBools:NULL forKeys:NULL count:0];
  7341. }
  7342. - (void)dealloc {
  7343. NSAssert(!_autocreator,
  7344. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7345. [self class], _autocreator);
  7346. [_dictionary release];
  7347. [super dealloc];
  7348. }
  7349. - (instancetype)copyWithZone:(NSZone *)zone {
  7350. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  7351. }
  7352. - (BOOL)isEqual:(id)other {
  7353. if (self == other) {
  7354. return YES;
  7355. }
  7356. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  7357. return NO;
  7358. }
  7359. GPBInt64BoolDictionary *otherDictionary = other;
  7360. return [_dictionary isEqual:otherDictionary->_dictionary];
  7361. }
  7362. - (NSUInteger)hash {
  7363. return _dictionary.count;
  7364. }
  7365. - (NSString *)description {
  7366. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7367. }
  7368. - (NSUInteger)count {
  7369. return _dictionary.count;
  7370. }
  7371. - (void)enumerateKeysAndBoolsUsingBlock:
  7372. (void (^)(int64_t key, BOOL value, BOOL *stop))block {
  7373. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7374. NSNumber *aValue,
  7375. BOOL *stop) {
  7376. block([aKey longLongValue], [aValue boolValue], stop);
  7377. }];
  7378. }
  7379. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7380. NSUInteger count = _dictionary.count;
  7381. if (count == 0) {
  7382. return 0;
  7383. }
  7384. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7385. GPBDataType keyDataType = field.mapKeyDataType;
  7386. __block size_t result = 0;
  7387. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7388. NSNumber *aValue,
  7389. BOOL *stop) {
  7390. #pragma unused(stop)
  7391. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7392. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7393. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7394. }];
  7395. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7396. result += tagSize * count;
  7397. return result;
  7398. }
  7399. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7400. asField:(GPBFieldDescriptor *)field {
  7401. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7402. GPBDataType keyDataType = field.mapKeyDataType;
  7403. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7404. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7405. NSNumber *aValue,
  7406. BOOL *stop) {
  7407. #pragma unused(stop)
  7408. // Write the tag.
  7409. [outputStream writeInt32NoTag:tag];
  7410. // Write the size of the message.
  7411. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7412. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  7413. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7414. // Write the fields.
  7415. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7416. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  7417. }];
  7418. }
  7419. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7420. forGPBGenericValueKey:(GPBGenericValue *)key {
  7421. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  7422. }
  7423. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7424. [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  7425. #pragma unused(stop)
  7426. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  7427. }];
  7428. }
  7429. - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
  7430. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7431. if (wrapped && value) {
  7432. *value = [wrapped boolValue];
  7433. }
  7434. return (wrapped != NULL);
  7435. }
  7436. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  7437. if (otherDictionary) {
  7438. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7439. if (_autocreator) {
  7440. GPBAutocreatedDictionaryModified(_autocreator, self);
  7441. }
  7442. }
  7443. }
  7444. - (void)setBool:(BOOL)value forKey:(int64_t)key {
  7445. [_dictionary setObject:@(value) forKey:@(key)];
  7446. if (_autocreator) {
  7447. GPBAutocreatedDictionaryModified(_autocreator, self);
  7448. }
  7449. }
  7450. - (void)removeBoolForKey:(int64_t)aKey {
  7451. [_dictionary removeObjectForKey:@(aKey)];
  7452. }
  7453. - (void)removeAll {
  7454. [_dictionary removeAllObjects];
  7455. }
  7456. @end
  7457. #pragma mark - Int64 -> Float
  7458. @implementation GPBInt64FloatDictionary {
  7459. @package
  7460. NSMutableDictionary *_dictionary;
  7461. }
  7462. + (instancetype)dictionary {
  7463. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  7464. }
  7465. + (instancetype)dictionaryWithFloat:(float)value
  7466. forKey:(int64_t)key {
  7467. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  7468. // on to get the type correct.
  7469. return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:&value
  7470. forKeys:&key
  7471. count:1] autorelease];
  7472. }
  7473. + (instancetype)dictionaryWithFloats:(const float [])values
  7474. forKeys:(const int64_t [])keys
  7475. count:(NSUInteger)count {
  7476. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  7477. // on to get the type correct.
  7478. return [[(GPBInt64FloatDictionary*)[self alloc] initWithFloats:values
  7479. forKeys:keys
  7480. count:count] autorelease];
  7481. }
  7482. + (instancetype)dictionaryWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7483. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7484. // on to get the type correct.
  7485. return [[(GPBInt64FloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7486. }
  7487. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7488. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7489. }
  7490. - (instancetype)init {
  7491. return [self initWithFloats:NULL forKeys:NULL count:0];
  7492. }
  7493. - (instancetype)initWithFloats:(const float [])values
  7494. forKeys:(const int64_t [])keys
  7495. count:(NSUInteger)count {
  7496. self = [super init];
  7497. if (self) {
  7498. _dictionary = [[NSMutableDictionary alloc] init];
  7499. if (count && values && keys) {
  7500. for (NSUInteger i = 0; i < count; ++i) {
  7501. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7502. }
  7503. }
  7504. }
  7505. return self;
  7506. }
  7507. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  7508. self = [self initWithFloats:NULL forKeys:NULL count:0];
  7509. if (self) {
  7510. if (dictionary) {
  7511. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7512. }
  7513. }
  7514. return self;
  7515. }
  7516. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7517. #pragma unused(numItems)
  7518. return [self initWithFloats:NULL forKeys:NULL count:0];
  7519. }
  7520. - (void)dealloc {
  7521. NSAssert(!_autocreator,
  7522. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7523. [self class], _autocreator);
  7524. [_dictionary release];
  7525. [super dealloc];
  7526. }
  7527. - (instancetype)copyWithZone:(NSZone *)zone {
  7528. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  7529. }
  7530. - (BOOL)isEqual:(id)other {
  7531. if (self == other) {
  7532. return YES;
  7533. }
  7534. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  7535. return NO;
  7536. }
  7537. GPBInt64FloatDictionary *otherDictionary = other;
  7538. return [_dictionary isEqual:otherDictionary->_dictionary];
  7539. }
  7540. - (NSUInteger)hash {
  7541. return _dictionary.count;
  7542. }
  7543. - (NSString *)description {
  7544. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7545. }
  7546. - (NSUInteger)count {
  7547. return _dictionary.count;
  7548. }
  7549. - (void)enumerateKeysAndFloatsUsingBlock:
  7550. (void (^)(int64_t key, float value, BOOL *stop))block {
  7551. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7552. NSNumber *aValue,
  7553. BOOL *stop) {
  7554. block([aKey longLongValue], [aValue floatValue], stop);
  7555. }];
  7556. }
  7557. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7558. NSUInteger count = _dictionary.count;
  7559. if (count == 0) {
  7560. return 0;
  7561. }
  7562. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7563. GPBDataType keyDataType = field.mapKeyDataType;
  7564. __block size_t result = 0;
  7565. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7566. NSNumber *aValue,
  7567. BOOL *stop) {
  7568. #pragma unused(stop)
  7569. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7570. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7571. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7572. }];
  7573. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7574. result += tagSize * count;
  7575. return result;
  7576. }
  7577. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7578. asField:(GPBFieldDescriptor *)field {
  7579. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7580. GPBDataType keyDataType = field.mapKeyDataType;
  7581. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7582. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7583. NSNumber *aValue,
  7584. BOOL *stop) {
  7585. #pragma unused(stop)
  7586. // Write the tag.
  7587. [outputStream writeInt32NoTag:tag];
  7588. // Write the size of the message.
  7589. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7590. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  7591. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7592. // Write the fields.
  7593. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7594. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  7595. }];
  7596. }
  7597. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7598. forGPBGenericValueKey:(GPBGenericValue *)key {
  7599. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  7600. }
  7601. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7602. [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) {
  7603. #pragma unused(stop)
  7604. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  7605. }];
  7606. }
  7607. - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
  7608. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7609. if (wrapped && value) {
  7610. *value = [wrapped floatValue];
  7611. }
  7612. return (wrapped != NULL);
  7613. }
  7614. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  7615. if (otherDictionary) {
  7616. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7617. if (_autocreator) {
  7618. GPBAutocreatedDictionaryModified(_autocreator, self);
  7619. }
  7620. }
  7621. }
  7622. - (void)setFloat:(float)value forKey:(int64_t)key {
  7623. [_dictionary setObject:@(value) forKey:@(key)];
  7624. if (_autocreator) {
  7625. GPBAutocreatedDictionaryModified(_autocreator, self);
  7626. }
  7627. }
  7628. - (void)removeFloatForKey:(int64_t)aKey {
  7629. [_dictionary removeObjectForKey:@(aKey)];
  7630. }
  7631. - (void)removeAll {
  7632. [_dictionary removeAllObjects];
  7633. }
  7634. @end
  7635. #pragma mark - Int64 -> Double
  7636. @implementation GPBInt64DoubleDictionary {
  7637. @package
  7638. NSMutableDictionary *_dictionary;
  7639. }
  7640. + (instancetype)dictionary {
  7641. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  7642. }
  7643. + (instancetype)dictionaryWithDouble:(double)value
  7644. forKey:(int64_t)key {
  7645. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  7646. // on to get the type correct.
  7647. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:&value
  7648. forKeys:&key
  7649. count:1] autorelease];
  7650. }
  7651. + (instancetype)dictionaryWithDoubles:(const double [])values
  7652. forKeys:(const int64_t [])keys
  7653. count:(NSUInteger)count {
  7654. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  7655. // on to get the type correct.
  7656. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDoubles:values
  7657. forKeys:keys
  7658. count:count] autorelease];
  7659. }
  7660. + (instancetype)dictionaryWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7661. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  7662. // on to get the type correct.
  7663. return [[(GPBInt64DoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7664. }
  7665. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  7666. return [[[self alloc] initWithCapacity:numItems] autorelease];
  7667. }
  7668. - (instancetype)init {
  7669. return [self initWithDoubles:NULL forKeys:NULL count:0];
  7670. }
  7671. - (instancetype)initWithDoubles:(const double [])values
  7672. forKeys:(const int64_t [])keys
  7673. count:(NSUInteger)count {
  7674. self = [super init];
  7675. if (self) {
  7676. _dictionary = [[NSMutableDictionary alloc] init];
  7677. if (count && values && keys) {
  7678. for (NSUInteger i = 0; i < count; ++i) {
  7679. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  7680. }
  7681. }
  7682. }
  7683. return self;
  7684. }
  7685. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  7686. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  7687. if (self) {
  7688. if (dictionary) {
  7689. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7690. }
  7691. }
  7692. return self;
  7693. }
  7694. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7695. #pragma unused(numItems)
  7696. return [self initWithDoubles:NULL forKeys:NULL count:0];
  7697. }
  7698. - (void)dealloc {
  7699. NSAssert(!_autocreator,
  7700. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7701. [self class], _autocreator);
  7702. [_dictionary release];
  7703. [super dealloc];
  7704. }
  7705. - (instancetype)copyWithZone:(NSZone *)zone {
  7706. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  7707. }
  7708. - (BOOL)isEqual:(id)other {
  7709. if (self == other) {
  7710. return YES;
  7711. }
  7712. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  7713. return NO;
  7714. }
  7715. GPBInt64DoubleDictionary *otherDictionary = other;
  7716. return [_dictionary isEqual:otherDictionary->_dictionary];
  7717. }
  7718. - (NSUInteger)hash {
  7719. return _dictionary.count;
  7720. }
  7721. - (NSString *)description {
  7722. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7723. }
  7724. - (NSUInteger)count {
  7725. return _dictionary.count;
  7726. }
  7727. - (void)enumerateKeysAndDoublesUsingBlock:
  7728. (void (^)(int64_t key, double value, BOOL *stop))block {
  7729. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7730. NSNumber *aValue,
  7731. BOOL *stop) {
  7732. block([aKey longLongValue], [aValue doubleValue], stop);
  7733. }];
  7734. }
  7735. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7736. NSUInteger count = _dictionary.count;
  7737. if (count == 0) {
  7738. return 0;
  7739. }
  7740. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7741. GPBDataType keyDataType = field.mapKeyDataType;
  7742. __block size_t result = 0;
  7743. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7744. NSNumber *aValue,
  7745. BOOL *stop) {
  7746. #pragma unused(stop)
  7747. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7748. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7749. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7750. }];
  7751. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7752. result += tagSize * count;
  7753. return result;
  7754. }
  7755. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7756. asField:(GPBFieldDescriptor *)field {
  7757. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7758. GPBDataType keyDataType = field.mapKeyDataType;
  7759. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7760. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7761. NSNumber *aValue,
  7762. BOOL *stop) {
  7763. #pragma unused(stop)
  7764. // Write the tag.
  7765. [outputStream writeInt32NoTag:tag];
  7766. // Write the size of the message.
  7767. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7768. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7769. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7770. // Write the fields.
  7771. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7772. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  7773. }];
  7774. }
  7775. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7776. forGPBGenericValueKey:(GPBGenericValue *)key {
  7777. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7778. }
  7779. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7780. [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7781. #pragma unused(stop)
  7782. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7783. }];
  7784. }
  7785. - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
  7786. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7787. if (wrapped && value) {
  7788. *value = [wrapped doubleValue];
  7789. }
  7790. return (wrapped != NULL);
  7791. }
  7792. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7793. if (otherDictionary) {
  7794. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7795. if (_autocreator) {
  7796. GPBAutocreatedDictionaryModified(_autocreator, self);
  7797. }
  7798. }
  7799. }
  7800. - (void)setDouble:(double)value forKey:(int64_t)key {
  7801. [_dictionary setObject:@(value) forKey:@(key)];
  7802. if (_autocreator) {
  7803. GPBAutocreatedDictionaryModified(_autocreator, self);
  7804. }
  7805. }
  7806. - (void)removeDoubleForKey:(int64_t)aKey {
  7807. [_dictionary removeObjectForKey:@(aKey)];
  7808. }
  7809. - (void)removeAll {
  7810. [_dictionary removeAllObjects];
  7811. }
  7812. @end
  7813. #pragma mark - Int64 -> Enum
  7814. @implementation GPBInt64EnumDictionary {
  7815. @package
  7816. NSMutableDictionary *_dictionary;
  7817. GPBEnumValidationFunc _validationFunc;
  7818. }
  7819. @synthesize validationFunc = _validationFunc;
  7820. + (instancetype)dictionary {
  7821. return [[[self alloc] initWithValidationFunction:NULL
  7822. rawValues:NULL
  7823. forKeys:NULL
  7824. count:0] autorelease];
  7825. }
  7826. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  7827. return [[[self alloc] initWithValidationFunction:func
  7828. rawValues:NULL
  7829. forKeys:NULL
  7830. count:0] autorelease];
  7831. }
  7832. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7833. rawValue:(int32_t)rawValue
  7834. forKey:(int64_t)key {
  7835. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7836. // on to get the type correct.
  7837. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7838. rawValues:&rawValue
  7839. forKeys:&key
  7840. count:1] autorelease];
  7841. }
  7842. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7843. rawValues:(const int32_t [])rawValues
  7844. forKeys:(const int64_t [])keys
  7845. count:(NSUInteger)count {
  7846. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7847. // on to get the type correct.
  7848. return [[(GPBInt64EnumDictionary*)[self alloc] initWithValidationFunction:func
  7849. rawValues:rawValues
  7850. forKeys:keys
  7851. count:count] autorelease];
  7852. }
  7853. + (instancetype)dictionaryWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7854. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  7855. // on to get the type correct.
  7856. return [[(GPBInt64EnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  7857. }
  7858. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  7859. capacity:(NSUInteger)numItems {
  7860. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  7861. }
  7862. - (instancetype)init {
  7863. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7864. }
  7865. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7866. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7867. }
  7868. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7869. rawValues:(const int32_t [])rawValues
  7870. forKeys:(const int64_t [])keys
  7871. count:(NSUInteger)count {
  7872. self = [super init];
  7873. if (self) {
  7874. _dictionary = [[NSMutableDictionary alloc] init];
  7875. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7876. if (count && rawValues && keys) {
  7877. for (NSUInteger i = 0; i < count; ++i) {
  7878. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7879. }
  7880. }
  7881. }
  7882. return self;
  7883. }
  7884. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7885. self = [self initWithValidationFunction:dictionary.validationFunc
  7886. rawValues:NULL
  7887. forKeys:NULL
  7888. count:0];
  7889. if (self) {
  7890. if (dictionary) {
  7891. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7892. }
  7893. }
  7894. return self;
  7895. }
  7896. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7897. capacity:(NSUInteger)numItems {
  7898. #pragma unused(numItems)
  7899. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7900. }
  7901. - (void)dealloc {
  7902. NSAssert(!_autocreator,
  7903. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7904. [self class], _autocreator);
  7905. [_dictionary release];
  7906. [super dealloc];
  7907. }
  7908. - (instancetype)copyWithZone:(NSZone *)zone {
  7909. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7910. }
  7911. - (BOOL)isEqual:(id)other {
  7912. if (self == other) {
  7913. return YES;
  7914. }
  7915. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7916. return NO;
  7917. }
  7918. GPBInt64EnumDictionary *otherDictionary = other;
  7919. return [_dictionary isEqual:otherDictionary->_dictionary];
  7920. }
  7921. - (NSUInteger)hash {
  7922. return _dictionary.count;
  7923. }
  7924. - (NSString *)description {
  7925. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7926. }
  7927. - (NSUInteger)count {
  7928. return _dictionary.count;
  7929. }
  7930. - (void)enumerateKeysAndRawValuesUsingBlock:
  7931. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  7932. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7933. NSNumber *aValue,
  7934. BOOL *stop) {
  7935. block([aKey longLongValue], [aValue intValue], stop);
  7936. }];
  7937. }
  7938. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7939. NSUInteger count = _dictionary.count;
  7940. if (count == 0) {
  7941. return 0;
  7942. }
  7943. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7944. GPBDataType keyDataType = field.mapKeyDataType;
  7945. __block size_t result = 0;
  7946. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7947. NSNumber *aValue,
  7948. BOOL *stop) {
  7949. #pragma unused(stop)
  7950. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7951. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7952. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7953. }];
  7954. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7955. result += tagSize * count;
  7956. return result;
  7957. }
  7958. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7959. asField:(GPBFieldDescriptor *)field {
  7960. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7961. GPBDataType keyDataType = field.mapKeyDataType;
  7962. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7963. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  7964. NSNumber *aValue,
  7965. BOOL *stop) {
  7966. #pragma unused(stop)
  7967. // Write the tag.
  7968. [outputStream writeInt32NoTag:tag];
  7969. // Write the size of the message.
  7970. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7971. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7972. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7973. // Write the fields.
  7974. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7975. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  7976. }];
  7977. }
  7978. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7979. forKey:(GPBGenericValue *)key
  7980. keyDataType:(GPBDataType)keyDataType {
  7981. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7982. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7983. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7984. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7985. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7986. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7987. [outputStream release];
  7988. return data;
  7989. }
  7990. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7991. forGPBGenericValueKey:(GPBGenericValue *)key {
  7992. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7993. }
  7994. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  7995. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7996. #pragma unused(stop)
  7997. block([NSString stringWithFormat:@"%lld", key], @(value));
  7998. }];
  7999. }
  8000. - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
  8001. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8002. if (wrapped && value) {
  8003. int32_t result = [wrapped intValue];
  8004. if (!_validationFunc(result)) {
  8005. result = kGPBUnrecognizedEnumeratorValue;
  8006. }
  8007. *value = result;
  8008. }
  8009. return (wrapped != NULL);
  8010. }
  8011. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
  8012. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  8013. if (wrapped && rawValue) {
  8014. *rawValue = [wrapped intValue];
  8015. }
  8016. return (wrapped != NULL);
  8017. }
  8018. - (void)enumerateKeysAndEnumsUsingBlock:
  8019. (void (^)(int64_t key, int32_t value, BOOL *stop))block {
  8020. GPBEnumValidationFunc func = _validationFunc;
  8021. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8022. NSNumber *aValue,
  8023. BOOL *stop) {
  8024. int32_t unwrapped = [aValue intValue];
  8025. if (!func(unwrapped)) {
  8026. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8027. }
  8028. block([aKey longLongValue], unwrapped, stop);
  8029. }];
  8030. }
  8031. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  8032. if (otherDictionary) {
  8033. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8034. if (_autocreator) {
  8035. GPBAutocreatedDictionaryModified(_autocreator, self);
  8036. }
  8037. }
  8038. }
  8039. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  8040. [_dictionary setObject:@(value) forKey:@(key)];
  8041. if (_autocreator) {
  8042. GPBAutocreatedDictionaryModified(_autocreator, self);
  8043. }
  8044. }
  8045. - (void)removeEnumForKey:(int64_t)aKey {
  8046. [_dictionary removeObjectForKey:@(aKey)];
  8047. }
  8048. - (void)removeAll {
  8049. [_dictionary removeAllObjects];
  8050. }
  8051. - (void)setEnum:(int32_t)value forKey:(int64_t)key {
  8052. if (!_validationFunc(value)) {
  8053. [NSException raise:NSInvalidArgumentException
  8054. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  8055. value];
  8056. }
  8057. [_dictionary setObject:@(value) forKey:@(key)];
  8058. if (_autocreator) {
  8059. GPBAutocreatedDictionaryModified(_autocreator, self);
  8060. }
  8061. }
  8062. @end
  8063. #pragma mark - Int64 -> Object
  8064. @implementation GPBInt64ObjectDictionary {
  8065. @package
  8066. NSMutableDictionary *_dictionary;
  8067. }
  8068. + (instancetype)dictionary {
  8069. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  8070. }
  8071. + (instancetype)dictionaryWithObject:(id)object
  8072. forKey:(int64_t)key {
  8073. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8074. // on to get the type correct.
  8075. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:&object
  8076. forKeys:&key
  8077. count:1] autorelease];
  8078. }
  8079. + (instancetype)dictionaryWithObjects:(const id [])objects
  8080. forKeys:(const int64_t [])keys
  8081. count:(NSUInteger)count {
  8082. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  8083. // on to get the type correct.
  8084. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithObjects:objects
  8085. forKeys:keys
  8086. count:count] autorelease];
  8087. }
  8088. + (instancetype)dictionaryWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8089. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8090. // on to get the type correct.
  8091. return [[(GPBInt64ObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8092. }
  8093. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8094. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8095. }
  8096. - (instancetype)init {
  8097. return [self initWithObjects:NULL forKeys:NULL count:0];
  8098. }
  8099. - (instancetype)initWithObjects:(const id [])objects
  8100. forKeys:(const int64_t [])keys
  8101. count:(NSUInteger)count {
  8102. self = [super init];
  8103. if (self) {
  8104. _dictionary = [[NSMutableDictionary alloc] init];
  8105. if (count && objects && keys) {
  8106. for (NSUInteger i = 0; i < count; ++i) {
  8107. if (!objects[i]) {
  8108. [NSException raise:NSInvalidArgumentException
  8109. format:@"Attempting to add nil object to a Dictionary"];
  8110. }
  8111. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  8112. }
  8113. }
  8114. }
  8115. return self;
  8116. }
  8117. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  8118. self = [self initWithObjects:NULL forKeys:NULL count:0];
  8119. if (self) {
  8120. if (dictionary) {
  8121. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8122. }
  8123. }
  8124. return self;
  8125. }
  8126. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8127. #pragma unused(numItems)
  8128. return [self initWithObjects:NULL forKeys:NULL count:0];
  8129. }
  8130. - (void)dealloc {
  8131. NSAssert(!_autocreator,
  8132. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8133. [self class], _autocreator);
  8134. [_dictionary release];
  8135. [super dealloc];
  8136. }
  8137. - (instancetype)copyWithZone:(NSZone *)zone {
  8138. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  8139. }
  8140. - (BOOL)isEqual:(id)other {
  8141. if (self == other) {
  8142. return YES;
  8143. }
  8144. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  8145. return NO;
  8146. }
  8147. GPBInt64ObjectDictionary *otherDictionary = other;
  8148. return [_dictionary isEqual:otherDictionary->_dictionary];
  8149. }
  8150. - (NSUInteger)hash {
  8151. return _dictionary.count;
  8152. }
  8153. - (NSString *)description {
  8154. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8155. }
  8156. - (NSUInteger)count {
  8157. return _dictionary.count;
  8158. }
  8159. - (void)enumerateKeysAndObjectsUsingBlock:
  8160. (void (^)(int64_t key, id object, BOOL *stop))block {
  8161. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8162. id aObject,
  8163. BOOL *stop) {
  8164. block([aKey longLongValue], aObject, stop);
  8165. }];
  8166. }
  8167. - (BOOL)isInitialized {
  8168. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  8169. if (!msg.initialized) {
  8170. return NO;
  8171. }
  8172. }
  8173. return YES;
  8174. }
  8175. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  8176. GPBInt64ObjectDictionary *newDict =
  8177. [[GPBInt64ObjectDictionary alloc] init];
  8178. [_dictionary enumerateKeysAndObjectsUsingBlock:^(id aKey,
  8179. GPBMessage *msg,
  8180. BOOL *stop) {
  8181. #pragma unused(stop)
  8182. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  8183. [newDict->_dictionary setObject:copiedMsg forKey:aKey];
  8184. [copiedMsg release];
  8185. }];
  8186. return newDict;
  8187. }
  8188. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8189. NSUInteger count = _dictionary.count;
  8190. if (count == 0) {
  8191. return 0;
  8192. }
  8193. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8194. GPBDataType keyDataType = field.mapKeyDataType;
  8195. __block size_t result = 0;
  8196. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8197. id aObject,
  8198. BOOL *stop) {
  8199. #pragma unused(stop)
  8200. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8201. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8202. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8203. }];
  8204. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8205. result += tagSize * count;
  8206. return result;
  8207. }
  8208. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8209. asField:(GPBFieldDescriptor *)field {
  8210. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8211. GPBDataType keyDataType = field.mapKeyDataType;
  8212. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8213. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSNumber *aKey,
  8214. id aObject,
  8215. BOOL *stop) {
  8216. #pragma unused(stop)
  8217. // Write the tag.
  8218. [outputStream writeInt32NoTag:tag];
  8219. // Write the size of the message.
  8220. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8221. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  8222. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8223. // Write the fields.
  8224. WriteDictInt64Field(outputStream, [aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  8225. WriteDictObjectField(outputStream, aObject, kMapValueFieldNumber, valueDataType);
  8226. }];
  8227. }
  8228. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8229. forGPBGenericValueKey:(GPBGenericValue *)key {
  8230. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  8231. }
  8232. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8233. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  8234. #pragma unused(stop)
  8235. block([NSString stringWithFormat:@"%lld", key], object);
  8236. }];
  8237. }
  8238. - (id)objectForKey:(int64_t)key {
  8239. id result = [_dictionary objectForKey:@(key)];
  8240. return result;
  8241. }
  8242. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  8243. if (otherDictionary) {
  8244. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8245. if (_autocreator) {
  8246. GPBAutocreatedDictionaryModified(_autocreator, self);
  8247. }
  8248. }
  8249. }
  8250. - (void)setObject:(id)object forKey:(int64_t)key {
  8251. if (!object) {
  8252. [NSException raise:NSInvalidArgumentException
  8253. format:@"Attempting to add nil object to a Dictionary"];
  8254. }
  8255. [_dictionary setObject:object forKey:@(key)];
  8256. if (_autocreator) {
  8257. GPBAutocreatedDictionaryModified(_autocreator, self);
  8258. }
  8259. }
  8260. - (void)removeObjectForKey:(int64_t)aKey {
  8261. [_dictionary removeObjectForKey:@(aKey)];
  8262. }
  8263. - (void)removeAll {
  8264. [_dictionary removeAllObjects];
  8265. }
  8266. @end
  8267. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  8268. // This block of code is generated, do not edit it directly.
  8269. #pragma mark - String -> UInt32
  8270. @implementation GPBStringUInt32Dictionary {
  8271. @package
  8272. NSMutableDictionary *_dictionary;
  8273. }
  8274. + (instancetype)dictionary {
  8275. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  8276. }
  8277. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  8278. forKey:(NSString *)key {
  8279. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  8280. // on to get the type correct.
  8281. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:&value
  8282. forKeys:&key
  8283. count:1] autorelease];
  8284. }
  8285. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  8286. forKeys:(const NSString * [])keys
  8287. count:(NSUInteger)count {
  8288. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  8289. // on to get the type correct.
  8290. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithUInt32s:values
  8291. forKeys:keys
  8292. count:count] autorelease];
  8293. }
  8294. + (instancetype)dictionaryWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8295. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8296. // on to get the type correct.
  8297. return [[(GPBStringUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8298. }
  8299. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8300. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8301. }
  8302. - (instancetype)init {
  8303. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8304. }
  8305. - (instancetype)initWithUInt32s:(const uint32_t [])values
  8306. forKeys:(const NSString * [])keys
  8307. count:(NSUInteger)count {
  8308. self = [super init];
  8309. if (self) {
  8310. _dictionary = [[NSMutableDictionary alloc] init];
  8311. if (count && values && keys) {
  8312. for (NSUInteger i = 0; i < count; ++i) {
  8313. if (!keys[i]) {
  8314. [NSException raise:NSInvalidArgumentException
  8315. format:@"Attempting to add nil key to a Dictionary"];
  8316. }
  8317. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8318. }
  8319. }
  8320. }
  8321. return self;
  8322. }
  8323. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  8324. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  8325. if (self) {
  8326. if (dictionary) {
  8327. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8328. }
  8329. }
  8330. return self;
  8331. }
  8332. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8333. #pragma unused(numItems)
  8334. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8335. }
  8336. - (void)dealloc {
  8337. NSAssert(!_autocreator,
  8338. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8339. [self class], _autocreator);
  8340. [_dictionary release];
  8341. [super dealloc];
  8342. }
  8343. - (instancetype)copyWithZone:(NSZone *)zone {
  8344. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8345. }
  8346. - (BOOL)isEqual:(id)other {
  8347. if (self == other) {
  8348. return YES;
  8349. }
  8350. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  8351. return NO;
  8352. }
  8353. GPBStringUInt32Dictionary *otherDictionary = other;
  8354. return [_dictionary isEqual:otherDictionary->_dictionary];
  8355. }
  8356. - (NSUInteger)hash {
  8357. return _dictionary.count;
  8358. }
  8359. - (NSString *)description {
  8360. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8361. }
  8362. - (NSUInteger)count {
  8363. return _dictionary.count;
  8364. }
  8365. - (void)enumerateKeysAndUInt32sUsingBlock:
  8366. (void (^)(NSString *key, uint32_t value, BOOL *stop))block {
  8367. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8368. NSNumber *aValue,
  8369. BOOL *stop) {
  8370. block(aKey, [aValue unsignedIntValue], stop);
  8371. }];
  8372. }
  8373. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8374. NSUInteger count = _dictionary.count;
  8375. if (count == 0) {
  8376. return 0;
  8377. }
  8378. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8379. GPBDataType keyDataType = field.mapKeyDataType;
  8380. __block size_t result = 0;
  8381. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8382. NSNumber *aValue,
  8383. BOOL *stop) {
  8384. #pragma unused(stop)
  8385. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8386. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8387. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8388. }];
  8389. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8390. result += tagSize * count;
  8391. return result;
  8392. }
  8393. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8394. asField:(GPBFieldDescriptor *)field {
  8395. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8396. GPBDataType keyDataType = field.mapKeyDataType;
  8397. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8398. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8399. NSNumber *aValue,
  8400. BOOL *stop) {
  8401. #pragma unused(stop)
  8402. // Write the tag.
  8403. [outputStream writeInt32NoTag:tag];
  8404. // Write the size of the message.
  8405. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8406. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8407. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8408. // Write the fields.
  8409. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8410. WriteDictUInt32Field(outputStream, [aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  8411. }];
  8412. }
  8413. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8414. forGPBGenericValueKey:(GPBGenericValue *)key {
  8415. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  8416. }
  8417. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8418. [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  8419. #pragma unused(stop)
  8420. block(key, [NSString stringWithFormat:@"%u", value]);
  8421. }];
  8422. }
  8423. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
  8424. NSNumber *wrapped = [_dictionary objectForKey:key];
  8425. if (wrapped && value) {
  8426. *value = [wrapped unsignedIntValue];
  8427. }
  8428. return (wrapped != NULL);
  8429. }
  8430. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  8431. if (otherDictionary) {
  8432. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8433. if (_autocreator) {
  8434. GPBAutocreatedDictionaryModified(_autocreator, self);
  8435. }
  8436. }
  8437. }
  8438. - (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
  8439. if (!key) {
  8440. [NSException raise:NSInvalidArgumentException
  8441. format:@"Attempting to add nil key to a Dictionary"];
  8442. }
  8443. [_dictionary setObject:@(value) forKey:key];
  8444. if (_autocreator) {
  8445. GPBAutocreatedDictionaryModified(_autocreator, self);
  8446. }
  8447. }
  8448. - (void)removeUInt32ForKey:(NSString *)aKey {
  8449. [_dictionary removeObjectForKey:aKey];
  8450. }
  8451. - (void)removeAll {
  8452. [_dictionary removeAllObjects];
  8453. }
  8454. @end
  8455. #pragma mark - String -> Int32
  8456. @implementation GPBStringInt32Dictionary {
  8457. @package
  8458. NSMutableDictionary *_dictionary;
  8459. }
  8460. + (instancetype)dictionary {
  8461. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  8462. }
  8463. + (instancetype)dictionaryWithInt32:(int32_t)value
  8464. forKey:(NSString *)key {
  8465. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  8466. // on to get the type correct.
  8467. return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:&value
  8468. forKeys:&key
  8469. count:1] autorelease];
  8470. }
  8471. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  8472. forKeys:(const NSString * [])keys
  8473. count:(NSUInteger)count {
  8474. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  8475. // on to get the type correct.
  8476. return [[(GPBStringInt32Dictionary*)[self alloc] initWithInt32s:values
  8477. forKeys:keys
  8478. count:count] autorelease];
  8479. }
  8480. + (instancetype)dictionaryWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8481. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8482. // on to get the type correct.
  8483. return [[(GPBStringInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8484. }
  8485. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8486. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8487. }
  8488. - (instancetype)init {
  8489. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8490. }
  8491. - (instancetype)initWithInt32s:(const int32_t [])values
  8492. forKeys:(const NSString * [])keys
  8493. count:(NSUInteger)count {
  8494. self = [super init];
  8495. if (self) {
  8496. _dictionary = [[NSMutableDictionary alloc] init];
  8497. if (count && values && keys) {
  8498. for (NSUInteger i = 0; i < count; ++i) {
  8499. if (!keys[i]) {
  8500. [NSException raise:NSInvalidArgumentException
  8501. format:@"Attempting to add nil key to a Dictionary"];
  8502. }
  8503. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8504. }
  8505. }
  8506. }
  8507. return self;
  8508. }
  8509. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  8510. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  8511. if (self) {
  8512. if (dictionary) {
  8513. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8514. }
  8515. }
  8516. return self;
  8517. }
  8518. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8519. #pragma unused(numItems)
  8520. return [self initWithInt32s:NULL forKeys:NULL count:0];
  8521. }
  8522. - (void)dealloc {
  8523. NSAssert(!_autocreator,
  8524. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8525. [self class], _autocreator);
  8526. [_dictionary release];
  8527. [super dealloc];
  8528. }
  8529. - (instancetype)copyWithZone:(NSZone *)zone {
  8530. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8531. }
  8532. - (BOOL)isEqual:(id)other {
  8533. if (self == other) {
  8534. return YES;
  8535. }
  8536. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  8537. return NO;
  8538. }
  8539. GPBStringInt32Dictionary *otherDictionary = other;
  8540. return [_dictionary isEqual:otherDictionary->_dictionary];
  8541. }
  8542. - (NSUInteger)hash {
  8543. return _dictionary.count;
  8544. }
  8545. - (NSString *)description {
  8546. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8547. }
  8548. - (NSUInteger)count {
  8549. return _dictionary.count;
  8550. }
  8551. - (void)enumerateKeysAndInt32sUsingBlock:
  8552. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  8553. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8554. NSNumber *aValue,
  8555. BOOL *stop) {
  8556. block(aKey, [aValue intValue], stop);
  8557. }];
  8558. }
  8559. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8560. NSUInteger count = _dictionary.count;
  8561. if (count == 0) {
  8562. return 0;
  8563. }
  8564. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8565. GPBDataType keyDataType = field.mapKeyDataType;
  8566. __block size_t result = 0;
  8567. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8568. NSNumber *aValue,
  8569. BOOL *stop) {
  8570. #pragma unused(stop)
  8571. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8572. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8573. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8574. }];
  8575. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8576. result += tagSize * count;
  8577. return result;
  8578. }
  8579. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8580. asField:(GPBFieldDescriptor *)field {
  8581. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8582. GPBDataType keyDataType = field.mapKeyDataType;
  8583. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8584. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8585. NSNumber *aValue,
  8586. BOOL *stop) {
  8587. #pragma unused(stop)
  8588. // Write the tag.
  8589. [outputStream writeInt32NoTag:tag];
  8590. // Write the size of the message.
  8591. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8592. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8593. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8594. // Write the fields.
  8595. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8596. WriteDictInt32Field(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  8597. }];
  8598. }
  8599. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8600. forGPBGenericValueKey:(GPBGenericValue *)key {
  8601. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  8602. }
  8603. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8604. [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8605. #pragma unused(stop)
  8606. block(key, [NSString stringWithFormat:@"%d", value]);
  8607. }];
  8608. }
  8609. - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
  8610. NSNumber *wrapped = [_dictionary objectForKey:key];
  8611. if (wrapped && value) {
  8612. *value = [wrapped intValue];
  8613. }
  8614. return (wrapped != NULL);
  8615. }
  8616. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  8617. if (otherDictionary) {
  8618. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8619. if (_autocreator) {
  8620. GPBAutocreatedDictionaryModified(_autocreator, self);
  8621. }
  8622. }
  8623. }
  8624. - (void)setInt32:(int32_t)value forKey:(NSString *)key {
  8625. if (!key) {
  8626. [NSException raise:NSInvalidArgumentException
  8627. format:@"Attempting to add nil key to a Dictionary"];
  8628. }
  8629. [_dictionary setObject:@(value) forKey:key];
  8630. if (_autocreator) {
  8631. GPBAutocreatedDictionaryModified(_autocreator, self);
  8632. }
  8633. }
  8634. - (void)removeInt32ForKey:(NSString *)aKey {
  8635. [_dictionary removeObjectForKey:aKey];
  8636. }
  8637. - (void)removeAll {
  8638. [_dictionary removeAllObjects];
  8639. }
  8640. @end
  8641. #pragma mark - String -> UInt64
  8642. @implementation GPBStringUInt64Dictionary {
  8643. @package
  8644. NSMutableDictionary *_dictionary;
  8645. }
  8646. + (instancetype)dictionary {
  8647. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  8648. }
  8649. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  8650. forKey:(NSString *)key {
  8651. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  8652. // on to get the type correct.
  8653. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:&value
  8654. forKeys:&key
  8655. count:1] autorelease];
  8656. }
  8657. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  8658. forKeys:(const NSString * [])keys
  8659. count:(NSUInteger)count {
  8660. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  8661. // on to get the type correct.
  8662. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithUInt64s:values
  8663. forKeys:keys
  8664. count:count] autorelease];
  8665. }
  8666. + (instancetype)dictionaryWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8667. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8668. // on to get the type correct.
  8669. return [[(GPBStringUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8670. }
  8671. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8672. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8673. }
  8674. - (instancetype)init {
  8675. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  8676. }
  8677. - (instancetype)initWithUInt64s:(const uint64_t [])values
  8678. forKeys:(const NSString * [])keys
  8679. count:(NSUInteger)count {
  8680. self = [super init];
  8681. if (self) {
  8682. _dictionary = [[NSMutableDictionary alloc] init];
  8683. if (count && values && keys) {
  8684. for (NSUInteger i = 0; i < count; ++i) {
  8685. if (!keys[i]) {
  8686. [NSException raise:NSInvalidArgumentException
  8687. format:@"Attempting to add nil key to a Dictionary"];
  8688. }
  8689. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8690. }
  8691. }
  8692. }
  8693. return self;
  8694. }
  8695. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  8696. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  8697. if (self) {
  8698. if (dictionary) {
  8699. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8700. }
  8701. }
  8702. return self;
  8703. }
  8704. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8705. #pragma unused(numItems)
  8706. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  8707. }
  8708. - (void)dealloc {
  8709. NSAssert(!_autocreator,
  8710. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8711. [self class], _autocreator);
  8712. [_dictionary release];
  8713. [super dealloc];
  8714. }
  8715. - (instancetype)copyWithZone:(NSZone *)zone {
  8716. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8717. }
  8718. - (BOOL)isEqual:(id)other {
  8719. if (self == other) {
  8720. return YES;
  8721. }
  8722. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  8723. return NO;
  8724. }
  8725. GPBStringUInt64Dictionary *otherDictionary = other;
  8726. return [_dictionary isEqual:otherDictionary->_dictionary];
  8727. }
  8728. - (NSUInteger)hash {
  8729. return _dictionary.count;
  8730. }
  8731. - (NSString *)description {
  8732. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8733. }
  8734. - (NSUInteger)count {
  8735. return _dictionary.count;
  8736. }
  8737. - (void)enumerateKeysAndUInt64sUsingBlock:
  8738. (void (^)(NSString *key, uint64_t value, BOOL *stop))block {
  8739. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8740. NSNumber *aValue,
  8741. BOOL *stop) {
  8742. block(aKey, [aValue unsignedLongLongValue], stop);
  8743. }];
  8744. }
  8745. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8746. NSUInteger count = _dictionary.count;
  8747. if (count == 0) {
  8748. return 0;
  8749. }
  8750. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8751. GPBDataType keyDataType = field.mapKeyDataType;
  8752. __block size_t result = 0;
  8753. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8754. NSNumber *aValue,
  8755. BOOL *stop) {
  8756. #pragma unused(stop)
  8757. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8758. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8759. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8760. }];
  8761. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8762. result += tagSize * count;
  8763. return result;
  8764. }
  8765. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8766. asField:(GPBFieldDescriptor *)field {
  8767. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8768. GPBDataType keyDataType = field.mapKeyDataType;
  8769. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8770. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8771. NSNumber *aValue,
  8772. BOOL *stop) {
  8773. #pragma unused(stop)
  8774. // Write the tag.
  8775. [outputStream writeInt32NoTag:tag];
  8776. // Write the size of the message.
  8777. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8778. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8779. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8780. // Write the fields.
  8781. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8782. WriteDictUInt64Field(outputStream, [aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  8783. }];
  8784. }
  8785. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8786. forGPBGenericValueKey:(GPBGenericValue *)key {
  8787. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  8788. }
  8789. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8790. [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  8791. #pragma unused(stop)
  8792. block(key, [NSString stringWithFormat:@"%llu", value]);
  8793. }];
  8794. }
  8795. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
  8796. NSNumber *wrapped = [_dictionary objectForKey:key];
  8797. if (wrapped && value) {
  8798. *value = [wrapped unsignedLongLongValue];
  8799. }
  8800. return (wrapped != NULL);
  8801. }
  8802. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  8803. if (otherDictionary) {
  8804. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8805. if (_autocreator) {
  8806. GPBAutocreatedDictionaryModified(_autocreator, self);
  8807. }
  8808. }
  8809. }
  8810. - (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
  8811. if (!key) {
  8812. [NSException raise:NSInvalidArgumentException
  8813. format:@"Attempting to add nil key to a Dictionary"];
  8814. }
  8815. [_dictionary setObject:@(value) forKey:key];
  8816. if (_autocreator) {
  8817. GPBAutocreatedDictionaryModified(_autocreator, self);
  8818. }
  8819. }
  8820. - (void)removeUInt64ForKey:(NSString *)aKey {
  8821. [_dictionary removeObjectForKey:aKey];
  8822. }
  8823. - (void)removeAll {
  8824. [_dictionary removeAllObjects];
  8825. }
  8826. @end
  8827. #pragma mark - String -> Int64
  8828. @implementation GPBStringInt64Dictionary {
  8829. @package
  8830. NSMutableDictionary *_dictionary;
  8831. }
  8832. + (instancetype)dictionary {
  8833. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  8834. }
  8835. + (instancetype)dictionaryWithInt64:(int64_t)value
  8836. forKey:(NSString *)key {
  8837. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  8838. // on to get the type correct.
  8839. return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:&value
  8840. forKeys:&key
  8841. count:1] autorelease];
  8842. }
  8843. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  8844. forKeys:(const NSString * [])keys
  8845. count:(NSUInteger)count {
  8846. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  8847. // on to get the type correct.
  8848. return [[(GPBStringInt64Dictionary*)[self alloc] initWithInt64s:values
  8849. forKeys:keys
  8850. count:count] autorelease];
  8851. }
  8852. + (instancetype)dictionaryWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8853. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  8854. // on to get the type correct.
  8855. return [[(GPBStringInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  8856. }
  8857. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  8858. return [[[self alloc] initWithCapacity:numItems] autorelease];
  8859. }
  8860. - (instancetype)init {
  8861. return [self initWithInt64s:NULL forKeys:NULL count:0];
  8862. }
  8863. - (instancetype)initWithInt64s:(const int64_t [])values
  8864. forKeys:(const NSString * [])keys
  8865. count:(NSUInteger)count {
  8866. self = [super init];
  8867. if (self) {
  8868. _dictionary = [[NSMutableDictionary alloc] init];
  8869. if (count && values && keys) {
  8870. for (NSUInteger i = 0; i < count; ++i) {
  8871. if (!keys[i]) {
  8872. [NSException raise:NSInvalidArgumentException
  8873. format:@"Attempting to add nil key to a Dictionary"];
  8874. }
  8875. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8876. }
  8877. }
  8878. }
  8879. return self;
  8880. }
  8881. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  8882. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  8883. if (self) {
  8884. if (dictionary) {
  8885. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8886. }
  8887. }
  8888. return self;
  8889. }
  8890. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8891. #pragma unused(numItems)
  8892. return [self initWithInt64s:NULL forKeys:NULL count:0];
  8893. }
  8894. - (void)dealloc {
  8895. NSAssert(!_autocreator,
  8896. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8897. [self class], _autocreator);
  8898. [_dictionary release];
  8899. [super dealloc];
  8900. }
  8901. - (instancetype)copyWithZone:(NSZone *)zone {
  8902. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8903. }
  8904. - (BOOL)isEqual:(id)other {
  8905. if (self == other) {
  8906. return YES;
  8907. }
  8908. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8909. return NO;
  8910. }
  8911. GPBStringInt64Dictionary *otherDictionary = other;
  8912. return [_dictionary isEqual:otherDictionary->_dictionary];
  8913. }
  8914. - (NSUInteger)hash {
  8915. return _dictionary.count;
  8916. }
  8917. - (NSString *)description {
  8918. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8919. }
  8920. - (NSUInteger)count {
  8921. return _dictionary.count;
  8922. }
  8923. - (void)enumerateKeysAndInt64sUsingBlock:
  8924. (void (^)(NSString *key, int64_t value, BOOL *stop))block {
  8925. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8926. NSNumber *aValue,
  8927. BOOL *stop) {
  8928. block(aKey, [aValue longLongValue], stop);
  8929. }];
  8930. }
  8931. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8932. NSUInteger count = _dictionary.count;
  8933. if (count == 0) {
  8934. return 0;
  8935. }
  8936. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8937. GPBDataType keyDataType = field.mapKeyDataType;
  8938. __block size_t result = 0;
  8939. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8940. NSNumber *aValue,
  8941. BOOL *stop) {
  8942. #pragma unused(stop)
  8943. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8944. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8945. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8946. }];
  8947. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8948. result += tagSize * count;
  8949. return result;
  8950. }
  8951. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8952. asField:(GPBFieldDescriptor *)field {
  8953. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8954. GPBDataType keyDataType = field.mapKeyDataType;
  8955. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8956. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  8957. NSNumber *aValue,
  8958. BOOL *stop) {
  8959. #pragma unused(stop)
  8960. // Write the tag.
  8961. [outputStream writeInt32NoTag:tag];
  8962. // Write the size of the message.
  8963. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8964. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8965. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8966. // Write the fields.
  8967. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  8968. WriteDictInt64Field(outputStream, [aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8969. }];
  8970. }
  8971. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8972. forGPBGenericValueKey:(GPBGenericValue *)key {
  8973. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8974. }
  8975. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  8976. [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8977. #pragma unused(stop)
  8978. block(key, [NSString stringWithFormat:@"%lld", value]);
  8979. }];
  8980. }
  8981. - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
  8982. NSNumber *wrapped = [_dictionary objectForKey:key];
  8983. if (wrapped && value) {
  8984. *value = [wrapped longLongValue];
  8985. }
  8986. return (wrapped != NULL);
  8987. }
  8988. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8989. if (otherDictionary) {
  8990. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8991. if (_autocreator) {
  8992. GPBAutocreatedDictionaryModified(_autocreator, self);
  8993. }
  8994. }
  8995. }
  8996. - (void)setInt64:(int64_t)value forKey:(NSString *)key {
  8997. if (!key) {
  8998. [NSException raise:NSInvalidArgumentException
  8999. format:@"Attempting to add nil key to a Dictionary"];
  9000. }
  9001. [_dictionary setObject:@(value) forKey:key];
  9002. if (_autocreator) {
  9003. GPBAutocreatedDictionaryModified(_autocreator, self);
  9004. }
  9005. }
  9006. - (void)removeInt64ForKey:(NSString *)aKey {
  9007. [_dictionary removeObjectForKey:aKey];
  9008. }
  9009. - (void)removeAll {
  9010. [_dictionary removeAllObjects];
  9011. }
  9012. @end
  9013. #pragma mark - String -> Bool
  9014. @implementation GPBStringBoolDictionary {
  9015. @package
  9016. NSMutableDictionary *_dictionary;
  9017. }
  9018. + (instancetype)dictionary {
  9019. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  9020. }
  9021. + (instancetype)dictionaryWithBool:(BOOL)value
  9022. forKey:(NSString *)key {
  9023. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  9024. // on to get the type correct.
  9025. return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:&value
  9026. forKeys:&key
  9027. count:1] autorelease];
  9028. }
  9029. + (instancetype)dictionaryWithBools:(const BOOL [])values
  9030. forKeys:(const NSString * [])keys
  9031. count:(NSUInteger)count {
  9032. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  9033. // on to get the type correct.
  9034. return [[(GPBStringBoolDictionary*)[self alloc] initWithBools:values
  9035. forKeys:keys
  9036. count:count] autorelease];
  9037. }
  9038. + (instancetype)dictionaryWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9039. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9040. // on to get the type correct.
  9041. return [[(GPBStringBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9042. }
  9043. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9044. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9045. }
  9046. - (instancetype)init {
  9047. return [self initWithBools:NULL forKeys:NULL count:0];
  9048. }
  9049. - (instancetype)initWithBools:(const BOOL [])values
  9050. forKeys:(const NSString * [])keys
  9051. count:(NSUInteger)count {
  9052. self = [super init];
  9053. if (self) {
  9054. _dictionary = [[NSMutableDictionary alloc] init];
  9055. if (count && values && keys) {
  9056. for (NSUInteger i = 0; i < count; ++i) {
  9057. if (!keys[i]) {
  9058. [NSException raise:NSInvalidArgumentException
  9059. format:@"Attempting to add nil key to a Dictionary"];
  9060. }
  9061. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9062. }
  9063. }
  9064. }
  9065. return self;
  9066. }
  9067. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  9068. self = [self initWithBools:NULL forKeys:NULL count:0];
  9069. if (self) {
  9070. if (dictionary) {
  9071. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9072. }
  9073. }
  9074. return self;
  9075. }
  9076. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9077. #pragma unused(numItems)
  9078. return [self initWithBools:NULL forKeys:NULL count:0];
  9079. }
  9080. - (void)dealloc {
  9081. NSAssert(!_autocreator,
  9082. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9083. [self class], _autocreator);
  9084. [_dictionary release];
  9085. [super dealloc];
  9086. }
  9087. - (instancetype)copyWithZone:(NSZone *)zone {
  9088. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9089. }
  9090. - (BOOL)isEqual:(id)other {
  9091. if (self == other) {
  9092. return YES;
  9093. }
  9094. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  9095. return NO;
  9096. }
  9097. GPBStringBoolDictionary *otherDictionary = other;
  9098. return [_dictionary isEqual:otherDictionary->_dictionary];
  9099. }
  9100. - (NSUInteger)hash {
  9101. return _dictionary.count;
  9102. }
  9103. - (NSString *)description {
  9104. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9105. }
  9106. - (NSUInteger)count {
  9107. return _dictionary.count;
  9108. }
  9109. - (void)enumerateKeysAndBoolsUsingBlock:
  9110. (void (^)(NSString *key, BOOL value, BOOL *stop))block {
  9111. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9112. NSNumber *aValue,
  9113. BOOL *stop) {
  9114. block(aKey, [aValue boolValue], stop);
  9115. }];
  9116. }
  9117. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9118. NSUInteger count = _dictionary.count;
  9119. if (count == 0) {
  9120. return 0;
  9121. }
  9122. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9123. GPBDataType keyDataType = field.mapKeyDataType;
  9124. __block size_t result = 0;
  9125. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9126. NSNumber *aValue,
  9127. BOOL *stop) {
  9128. #pragma unused(stop)
  9129. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9130. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9131. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9132. }];
  9133. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9134. result += tagSize * count;
  9135. return result;
  9136. }
  9137. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9138. asField:(GPBFieldDescriptor *)field {
  9139. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9140. GPBDataType keyDataType = field.mapKeyDataType;
  9141. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9142. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9143. NSNumber *aValue,
  9144. BOOL *stop) {
  9145. #pragma unused(stop)
  9146. // Write the tag.
  9147. [outputStream writeInt32NoTag:tag];
  9148. // Write the size of the message.
  9149. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9150. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  9151. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9152. // Write the fields.
  9153. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9154. WriteDictBoolField(outputStream, [aValue boolValue], kMapValueFieldNumber, valueDataType);
  9155. }];
  9156. }
  9157. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9158. forGPBGenericValueKey:(GPBGenericValue *)key {
  9159. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  9160. }
  9161. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9162. [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  9163. #pragma unused(stop)
  9164. block(key, (value ? @"true" : @"false"));
  9165. }];
  9166. }
  9167. - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
  9168. NSNumber *wrapped = [_dictionary objectForKey:key];
  9169. if (wrapped && value) {
  9170. *value = [wrapped boolValue];
  9171. }
  9172. return (wrapped != NULL);
  9173. }
  9174. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  9175. if (otherDictionary) {
  9176. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9177. if (_autocreator) {
  9178. GPBAutocreatedDictionaryModified(_autocreator, self);
  9179. }
  9180. }
  9181. }
  9182. - (void)setBool:(BOOL)value forKey:(NSString *)key {
  9183. if (!key) {
  9184. [NSException raise:NSInvalidArgumentException
  9185. format:@"Attempting to add nil key to a Dictionary"];
  9186. }
  9187. [_dictionary setObject:@(value) forKey:key];
  9188. if (_autocreator) {
  9189. GPBAutocreatedDictionaryModified(_autocreator, self);
  9190. }
  9191. }
  9192. - (void)removeBoolForKey:(NSString *)aKey {
  9193. [_dictionary removeObjectForKey:aKey];
  9194. }
  9195. - (void)removeAll {
  9196. [_dictionary removeAllObjects];
  9197. }
  9198. @end
  9199. #pragma mark - String -> Float
  9200. @implementation GPBStringFloatDictionary {
  9201. @package
  9202. NSMutableDictionary *_dictionary;
  9203. }
  9204. + (instancetype)dictionary {
  9205. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  9206. }
  9207. + (instancetype)dictionaryWithFloat:(float)value
  9208. forKey:(NSString *)key {
  9209. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  9210. // on to get the type correct.
  9211. return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:&value
  9212. forKeys:&key
  9213. count:1] autorelease];
  9214. }
  9215. + (instancetype)dictionaryWithFloats:(const float [])values
  9216. forKeys:(const NSString * [])keys
  9217. count:(NSUInteger)count {
  9218. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  9219. // on to get the type correct.
  9220. return [[(GPBStringFloatDictionary*)[self alloc] initWithFloats:values
  9221. forKeys:keys
  9222. count:count] autorelease];
  9223. }
  9224. + (instancetype)dictionaryWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9225. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9226. // on to get the type correct.
  9227. return [[(GPBStringFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9228. }
  9229. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9230. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9231. }
  9232. - (instancetype)init {
  9233. return [self initWithFloats:NULL forKeys:NULL count:0];
  9234. }
  9235. - (instancetype)initWithFloats:(const float [])values
  9236. forKeys:(const NSString * [])keys
  9237. count:(NSUInteger)count {
  9238. self = [super init];
  9239. if (self) {
  9240. _dictionary = [[NSMutableDictionary alloc] init];
  9241. if (count && values && keys) {
  9242. for (NSUInteger i = 0; i < count; ++i) {
  9243. if (!keys[i]) {
  9244. [NSException raise:NSInvalidArgumentException
  9245. format:@"Attempting to add nil key to a Dictionary"];
  9246. }
  9247. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9248. }
  9249. }
  9250. }
  9251. return self;
  9252. }
  9253. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  9254. self = [self initWithFloats:NULL forKeys:NULL count:0];
  9255. if (self) {
  9256. if (dictionary) {
  9257. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9258. }
  9259. }
  9260. return self;
  9261. }
  9262. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9263. #pragma unused(numItems)
  9264. return [self initWithFloats:NULL forKeys:NULL count:0];
  9265. }
  9266. - (void)dealloc {
  9267. NSAssert(!_autocreator,
  9268. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9269. [self class], _autocreator);
  9270. [_dictionary release];
  9271. [super dealloc];
  9272. }
  9273. - (instancetype)copyWithZone:(NSZone *)zone {
  9274. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9275. }
  9276. - (BOOL)isEqual:(id)other {
  9277. if (self == other) {
  9278. return YES;
  9279. }
  9280. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  9281. return NO;
  9282. }
  9283. GPBStringFloatDictionary *otherDictionary = other;
  9284. return [_dictionary isEqual:otherDictionary->_dictionary];
  9285. }
  9286. - (NSUInteger)hash {
  9287. return _dictionary.count;
  9288. }
  9289. - (NSString *)description {
  9290. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9291. }
  9292. - (NSUInteger)count {
  9293. return _dictionary.count;
  9294. }
  9295. - (void)enumerateKeysAndFloatsUsingBlock:
  9296. (void (^)(NSString *key, float value, BOOL *stop))block {
  9297. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9298. NSNumber *aValue,
  9299. BOOL *stop) {
  9300. block(aKey, [aValue floatValue], stop);
  9301. }];
  9302. }
  9303. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9304. NSUInteger count = _dictionary.count;
  9305. if (count == 0) {
  9306. return 0;
  9307. }
  9308. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9309. GPBDataType keyDataType = field.mapKeyDataType;
  9310. __block size_t result = 0;
  9311. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9312. NSNumber *aValue,
  9313. BOOL *stop) {
  9314. #pragma unused(stop)
  9315. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9316. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9317. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9318. }];
  9319. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9320. result += tagSize * count;
  9321. return result;
  9322. }
  9323. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9324. asField:(GPBFieldDescriptor *)field {
  9325. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9326. GPBDataType keyDataType = field.mapKeyDataType;
  9327. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9328. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9329. NSNumber *aValue,
  9330. BOOL *stop) {
  9331. #pragma unused(stop)
  9332. // Write the tag.
  9333. [outputStream writeInt32NoTag:tag];
  9334. // Write the size of the message.
  9335. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9336. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  9337. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9338. // Write the fields.
  9339. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9340. WriteDictFloatField(outputStream, [aValue floatValue], kMapValueFieldNumber, valueDataType);
  9341. }];
  9342. }
  9343. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9344. forGPBGenericValueKey:(GPBGenericValue *)key {
  9345. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  9346. }
  9347. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9348. [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) {
  9349. #pragma unused(stop)
  9350. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  9351. }];
  9352. }
  9353. - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
  9354. NSNumber *wrapped = [_dictionary objectForKey:key];
  9355. if (wrapped && value) {
  9356. *value = [wrapped floatValue];
  9357. }
  9358. return (wrapped != NULL);
  9359. }
  9360. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  9361. if (otherDictionary) {
  9362. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9363. if (_autocreator) {
  9364. GPBAutocreatedDictionaryModified(_autocreator, self);
  9365. }
  9366. }
  9367. }
  9368. - (void)setFloat:(float)value forKey:(NSString *)key {
  9369. if (!key) {
  9370. [NSException raise:NSInvalidArgumentException
  9371. format:@"Attempting to add nil key to a Dictionary"];
  9372. }
  9373. [_dictionary setObject:@(value) forKey:key];
  9374. if (_autocreator) {
  9375. GPBAutocreatedDictionaryModified(_autocreator, self);
  9376. }
  9377. }
  9378. - (void)removeFloatForKey:(NSString *)aKey {
  9379. [_dictionary removeObjectForKey:aKey];
  9380. }
  9381. - (void)removeAll {
  9382. [_dictionary removeAllObjects];
  9383. }
  9384. @end
  9385. #pragma mark - String -> Double
  9386. @implementation GPBStringDoubleDictionary {
  9387. @package
  9388. NSMutableDictionary *_dictionary;
  9389. }
  9390. + (instancetype)dictionary {
  9391. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  9392. }
  9393. + (instancetype)dictionaryWithDouble:(double)value
  9394. forKey:(NSString *)key {
  9395. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  9396. // on to get the type correct.
  9397. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:&value
  9398. forKeys:&key
  9399. count:1] autorelease];
  9400. }
  9401. + (instancetype)dictionaryWithDoubles:(const double [])values
  9402. forKeys:(const NSString * [])keys
  9403. count:(NSUInteger)count {
  9404. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  9405. // on to get the type correct.
  9406. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDoubles:values
  9407. forKeys:keys
  9408. count:count] autorelease];
  9409. }
  9410. + (instancetype)dictionaryWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9411. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9412. // on to get the type correct.
  9413. return [[(GPBStringDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9414. }
  9415. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9416. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9417. }
  9418. - (instancetype)init {
  9419. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9420. }
  9421. - (instancetype)initWithDoubles:(const double [])values
  9422. forKeys:(const NSString * [])keys
  9423. count:(NSUInteger)count {
  9424. self = [super init];
  9425. if (self) {
  9426. _dictionary = [[NSMutableDictionary alloc] init];
  9427. if (count && values && keys) {
  9428. for (NSUInteger i = 0; i < count; ++i) {
  9429. if (!keys[i]) {
  9430. [NSException raise:NSInvalidArgumentException
  9431. format:@"Attempting to add nil key to a Dictionary"];
  9432. }
  9433. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  9434. }
  9435. }
  9436. }
  9437. return self;
  9438. }
  9439. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  9440. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  9441. if (self) {
  9442. if (dictionary) {
  9443. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9444. }
  9445. }
  9446. return self;
  9447. }
  9448. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9449. #pragma unused(numItems)
  9450. return [self initWithDoubles:NULL forKeys:NULL count:0];
  9451. }
  9452. - (void)dealloc {
  9453. NSAssert(!_autocreator,
  9454. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9455. [self class], _autocreator);
  9456. [_dictionary release];
  9457. [super dealloc];
  9458. }
  9459. - (instancetype)copyWithZone:(NSZone *)zone {
  9460. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  9461. }
  9462. - (BOOL)isEqual:(id)other {
  9463. if (self == other) {
  9464. return YES;
  9465. }
  9466. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  9467. return NO;
  9468. }
  9469. GPBStringDoubleDictionary *otherDictionary = other;
  9470. return [_dictionary isEqual:otherDictionary->_dictionary];
  9471. }
  9472. - (NSUInteger)hash {
  9473. return _dictionary.count;
  9474. }
  9475. - (NSString *)description {
  9476. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9477. }
  9478. - (NSUInteger)count {
  9479. return _dictionary.count;
  9480. }
  9481. - (void)enumerateKeysAndDoublesUsingBlock:
  9482. (void (^)(NSString *key, double value, BOOL *stop))block {
  9483. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9484. NSNumber *aValue,
  9485. BOOL *stop) {
  9486. block(aKey, [aValue doubleValue], stop);
  9487. }];
  9488. }
  9489. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9490. NSUInteger count = _dictionary.count;
  9491. if (count == 0) {
  9492. return 0;
  9493. }
  9494. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9495. GPBDataType keyDataType = field.mapKeyDataType;
  9496. __block size_t result = 0;
  9497. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9498. NSNumber *aValue,
  9499. BOOL *stop) {
  9500. #pragma unused(stop)
  9501. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9502. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9503. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9504. }];
  9505. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9506. result += tagSize * count;
  9507. return result;
  9508. }
  9509. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9510. asField:(GPBFieldDescriptor *)field {
  9511. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9512. GPBDataType keyDataType = field.mapKeyDataType;
  9513. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9514. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9515. NSNumber *aValue,
  9516. BOOL *stop) {
  9517. #pragma unused(stop)
  9518. // Write the tag.
  9519. [outputStream writeInt32NoTag:tag];
  9520. // Write the size of the message.
  9521. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9522. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9523. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9524. // Write the fields.
  9525. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9526. WriteDictDoubleField(outputStream, [aValue doubleValue], kMapValueFieldNumber, valueDataType);
  9527. }];
  9528. }
  9529. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9530. forGPBGenericValueKey:(GPBGenericValue *)key {
  9531. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  9532. }
  9533. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9534. [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  9535. #pragma unused(stop)
  9536. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  9537. }];
  9538. }
  9539. - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
  9540. NSNumber *wrapped = [_dictionary objectForKey:key];
  9541. if (wrapped && value) {
  9542. *value = [wrapped doubleValue];
  9543. }
  9544. return (wrapped != NULL);
  9545. }
  9546. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  9547. if (otherDictionary) {
  9548. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9549. if (_autocreator) {
  9550. GPBAutocreatedDictionaryModified(_autocreator, self);
  9551. }
  9552. }
  9553. }
  9554. - (void)setDouble:(double)value forKey:(NSString *)key {
  9555. if (!key) {
  9556. [NSException raise:NSInvalidArgumentException
  9557. format:@"Attempting to add nil key to a Dictionary"];
  9558. }
  9559. [_dictionary setObject:@(value) forKey:key];
  9560. if (_autocreator) {
  9561. GPBAutocreatedDictionaryModified(_autocreator, self);
  9562. }
  9563. }
  9564. - (void)removeDoubleForKey:(NSString *)aKey {
  9565. [_dictionary removeObjectForKey:aKey];
  9566. }
  9567. - (void)removeAll {
  9568. [_dictionary removeAllObjects];
  9569. }
  9570. @end
  9571. #pragma mark - String -> Enum
  9572. @implementation GPBStringEnumDictionary {
  9573. @package
  9574. NSMutableDictionary *_dictionary;
  9575. GPBEnumValidationFunc _validationFunc;
  9576. }
  9577. @synthesize validationFunc = _validationFunc;
  9578. + (instancetype)dictionary {
  9579. return [[[self alloc] initWithValidationFunction:NULL
  9580. rawValues:NULL
  9581. forKeys:NULL
  9582. count:0] autorelease];
  9583. }
  9584. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  9585. return [[[self alloc] initWithValidationFunction:func
  9586. rawValues:NULL
  9587. forKeys:NULL
  9588. count:0] autorelease];
  9589. }
  9590. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9591. rawValue:(int32_t)rawValue
  9592. forKey:(NSString *)key {
  9593. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9594. // on to get the type correct.
  9595. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9596. rawValues:&rawValue
  9597. forKeys:&key
  9598. count:1] autorelease];
  9599. }
  9600. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9601. rawValues:(const int32_t [])rawValues
  9602. forKeys:(const NSString * [])keys
  9603. count:(NSUInteger)count {
  9604. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9605. // on to get the type correct.
  9606. return [[(GPBStringEnumDictionary*)[self alloc] initWithValidationFunction:func
  9607. rawValues:rawValues
  9608. forKeys:keys
  9609. count:count] autorelease];
  9610. }
  9611. + (instancetype)dictionaryWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9612. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  9613. // on to get the type correct.
  9614. return [[(GPBStringEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9615. }
  9616. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  9617. capacity:(NSUInteger)numItems {
  9618. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  9619. }
  9620. - (instancetype)init {
  9621. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  9622. }
  9623. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  9624. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9625. }
  9626. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9627. rawValues:(const int32_t [])rawValues
  9628. forKeys:(const NSString * [])keys
  9629. count:(NSUInteger)count {
  9630. self = [super init];
  9631. if (self) {
  9632. _dictionary = [[NSMutableDictionary alloc] init];
  9633. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  9634. if (count && rawValues && keys) {
  9635. for (NSUInteger i = 0; i < count; ++i) {
  9636. if (!keys[i]) {
  9637. [NSException raise:NSInvalidArgumentException
  9638. format:@"Attempting to add nil key to a Dictionary"];
  9639. }
  9640. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  9641. }
  9642. }
  9643. }
  9644. return self;
  9645. }
  9646. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  9647. self = [self initWithValidationFunction:dictionary.validationFunc
  9648. rawValues:NULL
  9649. forKeys:NULL
  9650. count:0];
  9651. if (self) {
  9652. if (dictionary) {
  9653. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  9654. }
  9655. }
  9656. return self;
  9657. }
  9658. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  9659. capacity:(NSUInteger)numItems {
  9660. #pragma unused(numItems)
  9661. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  9662. }
  9663. - (void)dealloc {
  9664. NSAssert(!_autocreator,
  9665. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9666. [self class], _autocreator);
  9667. [_dictionary release];
  9668. [super dealloc];
  9669. }
  9670. - (instancetype)copyWithZone:(NSZone *)zone {
  9671. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  9672. }
  9673. - (BOOL)isEqual:(id)other {
  9674. if (self == other) {
  9675. return YES;
  9676. }
  9677. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  9678. return NO;
  9679. }
  9680. GPBStringEnumDictionary *otherDictionary = other;
  9681. return [_dictionary isEqual:otherDictionary->_dictionary];
  9682. }
  9683. - (NSUInteger)hash {
  9684. return _dictionary.count;
  9685. }
  9686. - (NSString *)description {
  9687. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  9688. }
  9689. - (NSUInteger)count {
  9690. return _dictionary.count;
  9691. }
  9692. - (void)enumerateKeysAndRawValuesUsingBlock:
  9693. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9694. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9695. NSNumber *aValue,
  9696. BOOL *stop) {
  9697. block(aKey, [aValue intValue], stop);
  9698. }];
  9699. }
  9700. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9701. NSUInteger count = _dictionary.count;
  9702. if (count == 0) {
  9703. return 0;
  9704. }
  9705. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9706. GPBDataType keyDataType = field.mapKeyDataType;
  9707. __block size_t result = 0;
  9708. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9709. NSNumber *aValue,
  9710. BOOL *stop) {
  9711. #pragma unused(stop)
  9712. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9713. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9714. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9715. }];
  9716. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9717. result += tagSize * count;
  9718. return result;
  9719. }
  9720. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9721. asField:(GPBFieldDescriptor *)field {
  9722. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9723. GPBDataType keyDataType = field.mapKeyDataType;
  9724. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9725. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9726. NSNumber *aValue,
  9727. BOOL *stop) {
  9728. #pragma unused(stop)
  9729. // Write the tag.
  9730. [outputStream writeInt32NoTag:tag];
  9731. // Write the size of the message.
  9732. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  9733. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  9734. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9735. // Write the fields.
  9736. WriteDictStringField(outputStream, aKey, kMapKeyFieldNumber, keyDataType);
  9737. WriteDictEnumField(outputStream, [aValue intValue], kMapValueFieldNumber, valueDataType);
  9738. }];
  9739. }
  9740. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  9741. forKey:(GPBGenericValue *)key
  9742. keyDataType:(GPBDataType)keyDataType {
  9743. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  9744. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  9745. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  9746. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  9747. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  9748. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  9749. [outputStream release];
  9750. return data;
  9751. }
  9752. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9753. forGPBGenericValueKey:(GPBGenericValue *)key {
  9754. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  9755. }
  9756. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9757. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  9758. #pragma unused(stop)
  9759. block(key, @(value));
  9760. }];
  9761. }
  9762. - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
  9763. NSNumber *wrapped = [_dictionary objectForKey:key];
  9764. if (wrapped && value) {
  9765. int32_t result = [wrapped intValue];
  9766. if (!_validationFunc(result)) {
  9767. result = kGPBUnrecognizedEnumeratorValue;
  9768. }
  9769. *value = result;
  9770. }
  9771. return (wrapped != NULL);
  9772. }
  9773. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
  9774. NSNumber *wrapped = [_dictionary objectForKey:key];
  9775. if (wrapped && rawValue) {
  9776. *rawValue = [wrapped intValue];
  9777. }
  9778. return (wrapped != NULL);
  9779. }
  9780. - (void)enumerateKeysAndEnumsUsingBlock:
  9781. (void (^)(NSString *key, int32_t value, BOOL *stop))block {
  9782. GPBEnumValidationFunc func = _validationFunc;
  9783. [_dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *aKey,
  9784. NSNumber *aValue,
  9785. BOOL *stop) {
  9786. int32_t unwrapped = [aValue intValue];
  9787. if (!func(unwrapped)) {
  9788. unwrapped = kGPBUnrecognizedEnumeratorValue;
  9789. }
  9790. block(aKey, unwrapped, stop);
  9791. }];
  9792. }
  9793. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  9794. if (otherDictionary) {
  9795. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  9796. if (_autocreator) {
  9797. GPBAutocreatedDictionaryModified(_autocreator, self);
  9798. }
  9799. }
  9800. }
  9801. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  9802. if (!key) {
  9803. [NSException raise:NSInvalidArgumentException
  9804. format:@"Attempting to add nil key to a Dictionary"];
  9805. }
  9806. [_dictionary setObject:@(value) forKey:key];
  9807. if (_autocreator) {
  9808. GPBAutocreatedDictionaryModified(_autocreator, self);
  9809. }
  9810. }
  9811. - (void)removeEnumForKey:(NSString *)aKey {
  9812. [_dictionary removeObjectForKey:aKey];
  9813. }
  9814. - (void)removeAll {
  9815. [_dictionary removeAllObjects];
  9816. }
  9817. - (void)setEnum:(int32_t)value forKey:(NSString *)key {
  9818. if (!key) {
  9819. [NSException raise:NSInvalidArgumentException
  9820. format:@"Attempting to add nil key to a Dictionary"];
  9821. }
  9822. if (!_validationFunc(value)) {
  9823. [NSException raise:NSInvalidArgumentException
  9824. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  9825. value];
  9826. }
  9827. [_dictionary setObject:@(value) forKey:key];
  9828. if (_autocreator) {
  9829. GPBAutocreatedDictionaryModified(_autocreator, self);
  9830. }
  9831. }
  9832. @end
  9833. //%PDDM-EXPAND-END (5 expansions)
  9834. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  9835. // This block of code is generated, do not edit it directly.
  9836. #pragma mark - Bool -> UInt32
  9837. @implementation GPBBoolUInt32Dictionary {
  9838. @package
  9839. uint32_t _values[2];
  9840. BOOL _valueSet[2];
  9841. }
  9842. + (instancetype)dictionary {
  9843. return [[[self alloc] initWithUInt32s:NULL forKeys:NULL count:0] autorelease];
  9844. }
  9845. + (instancetype)dictionaryWithUInt32:(uint32_t)value
  9846. forKey:(BOOL)key {
  9847. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  9848. // on to get the type correct.
  9849. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:&value
  9850. forKeys:&key
  9851. count:1] autorelease];
  9852. }
  9853. + (instancetype)dictionaryWithUInt32s:(const uint32_t [])values
  9854. forKeys:(const BOOL [])keys
  9855. count:(NSUInteger)count {
  9856. // Cast is needed so the compiler knows what class we are invoking initWithUInt32s:forKeys:count:
  9857. // on to get the type correct.
  9858. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithUInt32s:values
  9859. forKeys:keys
  9860. count:count] autorelease];
  9861. }
  9862. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9863. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  9864. // on to get the type correct.
  9865. return [[(GPBBoolUInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  9866. }
  9867. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  9868. return [[[self alloc] initWithCapacity:numItems] autorelease];
  9869. }
  9870. - (instancetype)init {
  9871. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  9872. }
  9873. - (instancetype)initWithUInt32s:(const uint32_t [])values
  9874. forKeys:(const BOOL [])keys
  9875. count:(NSUInteger)count {
  9876. self = [super init];
  9877. if (self) {
  9878. for (NSUInteger i = 0; i < count; ++i) {
  9879. int idx = keys[i] ? 1 : 0;
  9880. _values[idx] = values[i];
  9881. _valueSet[idx] = YES;
  9882. }
  9883. }
  9884. return self;
  9885. }
  9886. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  9887. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  9888. if (self) {
  9889. if (dictionary) {
  9890. for (int i = 0; i < 2; ++i) {
  9891. if (dictionary->_valueSet[i]) {
  9892. _values[i] = dictionary->_values[i];
  9893. _valueSet[i] = YES;
  9894. }
  9895. }
  9896. }
  9897. }
  9898. return self;
  9899. }
  9900. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9901. #pragma unused(numItems)
  9902. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  9903. }
  9904. #if !defined(NS_BLOCK_ASSERTIONS)
  9905. - (void)dealloc {
  9906. NSAssert(!_autocreator,
  9907. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9908. [self class], _autocreator);
  9909. [super dealloc];
  9910. }
  9911. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9912. - (instancetype)copyWithZone:(NSZone *)zone {
  9913. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9914. }
  9915. - (BOOL)isEqual:(id)other {
  9916. if (self == other) {
  9917. return YES;
  9918. }
  9919. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  9920. return NO;
  9921. }
  9922. GPBBoolUInt32Dictionary *otherDictionary = other;
  9923. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9924. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9925. return NO;
  9926. }
  9927. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9928. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9929. return NO;
  9930. }
  9931. return YES;
  9932. }
  9933. - (NSUInteger)hash {
  9934. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9935. }
  9936. - (NSString *)description {
  9937. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9938. if (_valueSet[0]) {
  9939. [result appendFormat:@"NO: %u", _values[0]];
  9940. }
  9941. if (_valueSet[1]) {
  9942. [result appendFormat:@"YES: %u", _values[1]];
  9943. }
  9944. [result appendString:@" }"];
  9945. return result;
  9946. }
  9947. - (NSUInteger)count {
  9948. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9949. }
  9950. - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
  9951. int idx = (key ? 1 : 0);
  9952. if (_valueSet[idx]) {
  9953. if (value) {
  9954. *value = _values[idx];
  9955. }
  9956. return YES;
  9957. }
  9958. return NO;
  9959. }
  9960. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9961. forGPBGenericValueKey:(GPBGenericValue *)key {
  9962. int idx = (key->valueBool ? 1 : 0);
  9963. _values[idx] = value->valueUInt32;
  9964. _valueSet[idx] = YES;
  9965. }
  9966. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  9967. if (_valueSet[0]) {
  9968. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  9969. }
  9970. if (_valueSet[1]) {
  9971. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  9972. }
  9973. }
  9974. - (void)enumerateKeysAndUInt32sUsingBlock:
  9975. (void (^)(BOOL key, uint32_t value, BOOL *stop))block {
  9976. BOOL stop = NO;
  9977. if (_valueSet[0]) {
  9978. block(NO, _values[0], &stop);
  9979. }
  9980. if (!stop && _valueSet[1]) {
  9981. block(YES, _values[1], &stop);
  9982. }
  9983. }
  9984. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9985. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9986. NSUInteger count = 0;
  9987. size_t result = 0;
  9988. for (int i = 0; i < 2; ++i) {
  9989. if (_valueSet[i]) {
  9990. ++count;
  9991. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9992. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9993. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9994. }
  9995. }
  9996. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9997. result += tagSize * count;
  9998. return result;
  9999. }
  10000. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10001. asField:(GPBFieldDescriptor *)field {
  10002. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10003. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10004. for (int i = 0; i < 2; ++i) {
  10005. if (_valueSet[i]) {
  10006. // Write the tag.
  10007. [outputStream writeInt32NoTag:tag];
  10008. // Write the size of the message.
  10009. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10010. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10011. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10012. // Write the fields.
  10013. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10014. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10015. }
  10016. }
  10017. }
  10018. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  10019. if (otherDictionary) {
  10020. for (int i = 0; i < 2; ++i) {
  10021. if (otherDictionary->_valueSet[i]) {
  10022. _valueSet[i] = YES;
  10023. _values[i] = otherDictionary->_values[i];
  10024. }
  10025. }
  10026. if (_autocreator) {
  10027. GPBAutocreatedDictionaryModified(_autocreator, self);
  10028. }
  10029. }
  10030. }
  10031. - (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
  10032. int idx = (key ? 1 : 0);
  10033. _values[idx] = value;
  10034. _valueSet[idx] = YES;
  10035. if (_autocreator) {
  10036. GPBAutocreatedDictionaryModified(_autocreator, self);
  10037. }
  10038. }
  10039. - (void)removeUInt32ForKey:(BOOL)aKey {
  10040. _valueSet[aKey ? 1 : 0] = NO;
  10041. }
  10042. - (void)removeAll {
  10043. _valueSet[0] = NO;
  10044. _valueSet[1] = NO;
  10045. }
  10046. @end
  10047. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  10048. // This block of code is generated, do not edit it directly.
  10049. #pragma mark - Bool -> Int32
  10050. @implementation GPBBoolInt32Dictionary {
  10051. @package
  10052. int32_t _values[2];
  10053. BOOL _valueSet[2];
  10054. }
  10055. + (instancetype)dictionary {
  10056. return [[[self alloc] initWithInt32s:NULL forKeys:NULL count:0] autorelease];
  10057. }
  10058. + (instancetype)dictionaryWithInt32:(int32_t)value
  10059. forKey:(BOOL)key {
  10060. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  10061. // on to get the type correct.
  10062. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:&value
  10063. forKeys:&key
  10064. count:1] autorelease];
  10065. }
  10066. + (instancetype)dictionaryWithInt32s:(const int32_t [])values
  10067. forKeys:(const BOOL [])keys
  10068. count:(NSUInteger)count {
  10069. // Cast is needed so the compiler knows what class we are invoking initWithInt32s:forKeys:count:
  10070. // on to get the type correct.
  10071. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithInt32s:values
  10072. forKeys:keys
  10073. count:count] autorelease];
  10074. }
  10075. + (instancetype)dictionaryWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10076. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10077. // on to get the type correct.
  10078. return [[(GPBBoolInt32Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10079. }
  10080. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10081. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10082. }
  10083. - (instancetype)init {
  10084. return [self initWithInt32s:NULL forKeys:NULL count:0];
  10085. }
  10086. - (instancetype)initWithInt32s:(const int32_t [])values
  10087. forKeys:(const BOOL [])keys
  10088. count:(NSUInteger)count {
  10089. self = [super init];
  10090. if (self) {
  10091. for (NSUInteger i = 0; i < count; ++i) {
  10092. int idx = keys[i] ? 1 : 0;
  10093. _values[idx] = values[i];
  10094. _valueSet[idx] = YES;
  10095. }
  10096. }
  10097. return self;
  10098. }
  10099. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  10100. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  10101. if (self) {
  10102. if (dictionary) {
  10103. for (int i = 0; i < 2; ++i) {
  10104. if (dictionary->_valueSet[i]) {
  10105. _values[i] = dictionary->_values[i];
  10106. _valueSet[i] = YES;
  10107. }
  10108. }
  10109. }
  10110. }
  10111. return self;
  10112. }
  10113. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10114. #pragma unused(numItems)
  10115. return [self initWithInt32s:NULL forKeys:NULL count:0];
  10116. }
  10117. #if !defined(NS_BLOCK_ASSERTIONS)
  10118. - (void)dealloc {
  10119. NSAssert(!_autocreator,
  10120. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10121. [self class], _autocreator);
  10122. [super dealloc];
  10123. }
  10124. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10125. - (instancetype)copyWithZone:(NSZone *)zone {
  10126. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  10127. }
  10128. - (BOOL)isEqual:(id)other {
  10129. if (self == other) {
  10130. return YES;
  10131. }
  10132. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  10133. return NO;
  10134. }
  10135. GPBBoolInt32Dictionary *otherDictionary = other;
  10136. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10137. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10138. return NO;
  10139. }
  10140. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10141. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10142. return NO;
  10143. }
  10144. return YES;
  10145. }
  10146. - (NSUInteger)hash {
  10147. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10148. }
  10149. - (NSString *)description {
  10150. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10151. if (_valueSet[0]) {
  10152. [result appendFormat:@"NO: %d", _values[0]];
  10153. }
  10154. if (_valueSet[1]) {
  10155. [result appendFormat:@"YES: %d", _values[1]];
  10156. }
  10157. [result appendString:@" }"];
  10158. return result;
  10159. }
  10160. - (NSUInteger)count {
  10161. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10162. }
  10163. - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
  10164. int idx = (key ? 1 : 0);
  10165. if (_valueSet[idx]) {
  10166. if (value) {
  10167. *value = _values[idx];
  10168. }
  10169. return YES;
  10170. }
  10171. return NO;
  10172. }
  10173. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10174. forGPBGenericValueKey:(GPBGenericValue *)key {
  10175. int idx = (key->valueBool ? 1 : 0);
  10176. _values[idx] = value->valueInt32;
  10177. _valueSet[idx] = YES;
  10178. }
  10179. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10180. if (_valueSet[0]) {
  10181. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  10182. }
  10183. if (_valueSet[1]) {
  10184. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  10185. }
  10186. }
  10187. - (void)enumerateKeysAndInt32sUsingBlock:
  10188. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  10189. BOOL stop = NO;
  10190. if (_valueSet[0]) {
  10191. block(NO, _values[0], &stop);
  10192. }
  10193. if (!stop && _valueSet[1]) {
  10194. block(YES, _values[1], &stop);
  10195. }
  10196. }
  10197. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10198. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10199. NSUInteger count = 0;
  10200. size_t result = 0;
  10201. for (int i = 0; i < 2; ++i) {
  10202. if (_valueSet[i]) {
  10203. ++count;
  10204. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10205. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10206. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10207. }
  10208. }
  10209. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10210. result += tagSize * count;
  10211. return result;
  10212. }
  10213. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10214. asField:(GPBFieldDescriptor *)field {
  10215. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10216. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10217. for (int i = 0; i < 2; ++i) {
  10218. if (_valueSet[i]) {
  10219. // Write the tag.
  10220. [outputStream writeInt32NoTag:tag];
  10221. // Write the size of the message.
  10222. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10223. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10224. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10225. // Write the fields.
  10226. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10227. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10228. }
  10229. }
  10230. }
  10231. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  10232. if (otherDictionary) {
  10233. for (int i = 0; i < 2; ++i) {
  10234. if (otherDictionary->_valueSet[i]) {
  10235. _valueSet[i] = YES;
  10236. _values[i] = otherDictionary->_values[i];
  10237. }
  10238. }
  10239. if (_autocreator) {
  10240. GPBAutocreatedDictionaryModified(_autocreator, self);
  10241. }
  10242. }
  10243. }
  10244. - (void)setInt32:(int32_t)value forKey:(BOOL)key {
  10245. int idx = (key ? 1 : 0);
  10246. _values[idx] = value;
  10247. _valueSet[idx] = YES;
  10248. if (_autocreator) {
  10249. GPBAutocreatedDictionaryModified(_autocreator, self);
  10250. }
  10251. }
  10252. - (void)removeInt32ForKey:(BOOL)aKey {
  10253. _valueSet[aKey ? 1 : 0] = NO;
  10254. }
  10255. - (void)removeAll {
  10256. _valueSet[0] = NO;
  10257. _valueSet[1] = NO;
  10258. }
  10259. @end
  10260. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  10261. // This block of code is generated, do not edit it directly.
  10262. #pragma mark - Bool -> UInt64
  10263. @implementation GPBBoolUInt64Dictionary {
  10264. @package
  10265. uint64_t _values[2];
  10266. BOOL _valueSet[2];
  10267. }
  10268. + (instancetype)dictionary {
  10269. return [[[self alloc] initWithUInt64s:NULL forKeys:NULL count:0] autorelease];
  10270. }
  10271. + (instancetype)dictionaryWithUInt64:(uint64_t)value
  10272. forKey:(BOOL)key {
  10273. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  10274. // on to get the type correct.
  10275. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:&value
  10276. forKeys:&key
  10277. count:1] autorelease];
  10278. }
  10279. + (instancetype)dictionaryWithUInt64s:(const uint64_t [])values
  10280. forKeys:(const BOOL [])keys
  10281. count:(NSUInteger)count {
  10282. // Cast is needed so the compiler knows what class we are invoking initWithUInt64s:forKeys:count:
  10283. // on to get the type correct.
  10284. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithUInt64s:values
  10285. forKeys:keys
  10286. count:count] autorelease];
  10287. }
  10288. + (instancetype)dictionaryWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10289. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10290. // on to get the type correct.
  10291. return [[(GPBBoolUInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10292. }
  10293. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10294. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10295. }
  10296. - (instancetype)init {
  10297. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  10298. }
  10299. - (instancetype)initWithUInt64s:(const uint64_t [])values
  10300. forKeys:(const BOOL [])keys
  10301. count:(NSUInteger)count {
  10302. self = [super init];
  10303. if (self) {
  10304. for (NSUInteger i = 0; i < count; ++i) {
  10305. int idx = keys[i] ? 1 : 0;
  10306. _values[idx] = values[i];
  10307. _valueSet[idx] = YES;
  10308. }
  10309. }
  10310. return self;
  10311. }
  10312. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  10313. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  10314. if (self) {
  10315. if (dictionary) {
  10316. for (int i = 0; i < 2; ++i) {
  10317. if (dictionary->_valueSet[i]) {
  10318. _values[i] = dictionary->_values[i];
  10319. _valueSet[i] = YES;
  10320. }
  10321. }
  10322. }
  10323. }
  10324. return self;
  10325. }
  10326. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10327. #pragma unused(numItems)
  10328. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  10329. }
  10330. #if !defined(NS_BLOCK_ASSERTIONS)
  10331. - (void)dealloc {
  10332. NSAssert(!_autocreator,
  10333. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10334. [self class], _autocreator);
  10335. [super dealloc];
  10336. }
  10337. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10338. - (instancetype)copyWithZone:(NSZone *)zone {
  10339. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10340. }
  10341. - (BOOL)isEqual:(id)other {
  10342. if (self == other) {
  10343. return YES;
  10344. }
  10345. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  10346. return NO;
  10347. }
  10348. GPBBoolUInt64Dictionary *otherDictionary = other;
  10349. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10350. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10351. return NO;
  10352. }
  10353. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10354. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10355. return NO;
  10356. }
  10357. return YES;
  10358. }
  10359. - (NSUInteger)hash {
  10360. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10361. }
  10362. - (NSString *)description {
  10363. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10364. if (_valueSet[0]) {
  10365. [result appendFormat:@"NO: %llu", _values[0]];
  10366. }
  10367. if (_valueSet[1]) {
  10368. [result appendFormat:@"YES: %llu", _values[1]];
  10369. }
  10370. [result appendString:@" }"];
  10371. return result;
  10372. }
  10373. - (NSUInteger)count {
  10374. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10375. }
  10376. - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
  10377. int idx = (key ? 1 : 0);
  10378. if (_valueSet[idx]) {
  10379. if (value) {
  10380. *value = _values[idx];
  10381. }
  10382. return YES;
  10383. }
  10384. return NO;
  10385. }
  10386. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10387. forGPBGenericValueKey:(GPBGenericValue *)key {
  10388. int idx = (key->valueBool ? 1 : 0);
  10389. _values[idx] = value->valueUInt64;
  10390. _valueSet[idx] = YES;
  10391. }
  10392. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10393. if (_valueSet[0]) {
  10394. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  10395. }
  10396. if (_valueSet[1]) {
  10397. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  10398. }
  10399. }
  10400. - (void)enumerateKeysAndUInt64sUsingBlock:
  10401. (void (^)(BOOL key, uint64_t value, BOOL *stop))block {
  10402. BOOL stop = NO;
  10403. if (_valueSet[0]) {
  10404. block(NO, _values[0], &stop);
  10405. }
  10406. if (!stop && _valueSet[1]) {
  10407. block(YES, _values[1], &stop);
  10408. }
  10409. }
  10410. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10411. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10412. NSUInteger count = 0;
  10413. size_t result = 0;
  10414. for (int i = 0; i < 2; ++i) {
  10415. if (_valueSet[i]) {
  10416. ++count;
  10417. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10418. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10419. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10420. }
  10421. }
  10422. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10423. result += tagSize * count;
  10424. return result;
  10425. }
  10426. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10427. asField:(GPBFieldDescriptor *)field {
  10428. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10429. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10430. for (int i = 0; i < 2; ++i) {
  10431. if (_valueSet[i]) {
  10432. // Write the tag.
  10433. [outputStream writeInt32NoTag:tag];
  10434. // Write the size of the message.
  10435. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10436. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10437. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10438. // Write the fields.
  10439. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10440. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10441. }
  10442. }
  10443. }
  10444. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  10445. if (otherDictionary) {
  10446. for (int i = 0; i < 2; ++i) {
  10447. if (otherDictionary->_valueSet[i]) {
  10448. _valueSet[i] = YES;
  10449. _values[i] = otherDictionary->_values[i];
  10450. }
  10451. }
  10452. if (_autocreator) {
  10453. GPBAutocreatedDictionaryModified(_autocreator, self);
  10454. }
  10455. }
  10456. }
  10457. - (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
  10458. int idx = (key ? 1 : 0);
  10459. _values[idx] = value;
  10460. _valueSet[idx] = YES;
  10461. if (_autocreator) {
  10462. GPBAutocreatedDictionaryModified(_autocreator, self);
  10463. }
  10464. }
  10465. - (void)removeUInt64ForKey:(BOOL)aKey {
  10466. _valueSet[aKey ? 1 : 0] = NO;
  10467. }
  10468. - (void)removeAll {
  10469. _valueSet[0] = NO;
  10470. _valueSet[1] = NO;
  10471. }
  10472. @end
  10473. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  10474. // This block of code is generated, do not edit it directly.
  10475. #pragma mark - Bool -> Int64
  10476. @implementation GPBBoolInt64Dictionary {
  10477. @package
  10478. int64_t _values[2];
  10479. BOOL _valueSet[2];
  10480. }
  10481. + (instancetype)dictionary {
  10482. return [[[self alloc] initWithInt64s:NULL forKeys:NULL count:0] autorelease];
  10483. }
  10484. + (instancetype)dictionaryWithInt64:(int64_t)value
  10485. forKey:(BOOL)key {
  10486. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  10487. // on to get the type correct.
  10488. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:&value
  10489. forKeys:&key
  10490. count:1] autorelease];
  10491. }
  10492. + (instancetype)dictionaryWithInt64s:(const int64_t [])values
  10493. forKeys:(const BOOL [])keys
  10494. count:(NSUInteger)count {
  10495. // Cast is needed so the compiler knows what class we are invoking initWithInt64s:forKeys:count:
  10496. // on to get the type correct.
  10497. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithInt64s:values
  10498. forKeys:keys
  10499. count:count] autorelease];
  10500. }
  10501. + (instancetype)dictionaryWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10502. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10503. // on to get the type correct.
  10504. return [[(GPBBoolInt64Dictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10505. }
  10506. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10507. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10508. }
  10509. - (instancetype)init {
  10510. return [self initWithInt64s:NULL forKeys:NULL count:0];
  10511. }
  10512. - (instancetype)initWithInt64s:(const int64_t [])values
  10513. forKeys:(const BOOL [])keys
  10514. count:(NSUInteger)count {
  10515. self = [super init];
  10516. if (self) {
  10517. for (NSUInteger i = 0; i < count; ++i) {
  10518. int idx = keys[i] ? 1 : 0;
  10519. _values[idx] = values[i];
  10520. _valueSet[idx] = YES;
  10521. }
  10522. }
  10523. return self;
  10524. }
  10525. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  10526. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  10527. if (self) {
  10528. if (dictionary) {
  10529. for (int i = 0; i < 2; ++i) {
  10530. if (dictionary->_valueSet[i]) {
  10531. _values[i] = dictionary->_values[i];
  10532. _valueSet[i] = YES;
  10533. }
  10534. }
  10535. }
  10536. }
  10537. return self;
  10538. }
  10539. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10540. #pragma unused(numItems)
  10541. return [self initWithInt64s:NULL forKeys:NULL count:0];
  10542. }
  10543. #if !defined(NS_BLOCK_ASSERTIONS)
  10544. - (void)dealloc {
  10545. NSAssert(!_autocreator,
  10546. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10547. [self class], _autocreator);
  10548. [super dealloc];
  10549. }
  10550. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10551. - (instancetype)copyWithZone:(NSZone *)zone {
  10552. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  10553. }
  10554. - (BOOL)isEqual:(id)other {
  10555. if (self == other) {
  10556. return YES;
  10557. }
  10558. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  10559. return NO;
  10560. }
  10561. GPBBoolInt64Dictionary *otherDictionary = other;
  10562. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10563. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10564. return NO;
  10565. }
  10566. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10567. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10568. return NO;
  10569. }
  10570. return YES;
  10571. }
  10572. - (NSUInteger)hash {
  10573. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10574. }
  10575. - (NSString *)description {
  10576. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10577. if (_valueSet[0]) {
  10578. [result appendFormat:@"NO: %lld", _values[0]];
  10579. }
  10580. if (_valueSet[1]) {
  10581. [result appendFormat:@"YES: %lld", _values[1]];
  10582. }
  10583. [result appendString:@" }"];
  10584. return result;
  10585. }
  10586. - (NSUInteger)count {
  10587. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10588. }
  10589. - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
  10590. int idx = (key ? 1 : 0);
  10591. if (_valueSet[idx]) {
  10592. if (value) {
  10593. *value = _values[idx];
  10594. }
  10595. return YES;
  10596. }
  10597. return NO;
  10598. }
  10599. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10600. forGPBGenericValueKey:(GPBGenericValue *)key {
  10601. int idx = (key->valueBool ? 1 : 0);
  10602. _values[idx] = value->valueInt64;
  10603. _valueSet[idx] = YES;
  10604. }
  10605. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10606. if (_valueSet[0]) {
  10607. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  10608. }
  10609. if (_valueSet[1]) {
  10610. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  10611. }
  10612. }
  10613. - (void)enumerateKeysAndInt64sUsingBlock:
  10614. (void (^)(BOOL key, int64_t value, BOOL *stop))block {
  10615. BOOL stop = NO;
  10616. if (_valueSet[0]) {
  10617. block(NO, _values[0], &stop);
  10618. }
  10619. if (!stop && _valueSet[1]) {
  10620. block(YES, _values[1], &stop);
  10621. }
  10622. }
  10623. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10624. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10625. NSUInteger count = 0;
  10626. size_t result = 0;
  10627. for (int i = 0; i < 2; ++i) {
  10628. if (_valueSet[i]) {
  10629. ++count;
  10630. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10631. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10632. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10633. }
  10634. }
  10635. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10636. result += tagSize * count;
  10637. return result;
  10638. }
  10639. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10640. asField:(GPBFieldDescriptor *)field {
  10641. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10642. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10643. for (int i = 0; i < 2; ++i) {
  10644. if (_valueSet[i]) {
  10645. // Write the tag.
  10646. [outputStream writeInt32NoTag:tag];
  10647. // Write the size of the message.
  10648. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10649. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10650. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10651. // Write the fields.
  10652. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10653. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10654. }
  10655. }
  10656. }
  10657. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  10658. if (otherDictionary) {
  10659. for (int i = 0; i < 2; ++i) {
  10660. if (otherDictionary->_valueSet[i]) {
  10661. _valueSet[i] = YES;
  10662. _values[i] = otherDictionary->_values[i];
  10663. }
  10664. }
  10665. if (_autocreator) {
  10666. GPBAutocreatedDictionaryModified(_autocreator, self);
  10667. }
  10668. }
  10669. }
  10670. - (void)setInt64:(int64_t)value forKey:(BOOL)key {
  10671. int idx = (key ? 1 : 0);
  10672. _values[idx] = value;
  10673. _valueSet[idx] = YES;
  10674. if (_autocreator) {
  10675. GPBAutocreatedDictionaryModified(_autocreator, self);
  10676. }
  10677. }
  10678. - (void)removeInt64ForKey:(BOOL)aKey {
  10679. _valueSet[aKey ? 1 : 0] = NO;
  10680. }
  10681. - (void)removeAll {
  10682. _valueSet[0] = NO;
  10683. _valueSet[1] = NO;
  10684. }
  10685. @end
  10686. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  10687. // This block of code is generated, do not edit it directly.
  10688. #pragma mark - Bool -> Bool
  10689. @implementation GPBBoolBoolDictionary {
  10690. @package
  10691. BOOL _values[2];
  10692. BOOL _valueSet[2];
  10693. }
  10694. + (instancetype)dictionary {
  10695. return [[[self alloc] initWithBools:NULL forKeys:NULL count:0] autorelease];
  10696. }
  10697. + (instancetype)dictionaryWithBool:(BOOL)value
  10698. forKey:(BOOL)key {
  10699. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  10700. // on to get the type correct.
  10701. return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:&value
  10702. forKeys:&key
  10703. count:1] autorelease];
  10704. }
  10705. + (instancetype)dictionaryWithBools:(const BOOL [])values
  10706. forKeys:(const BOOL [])keys
  10707. count:(NSUInteger)count {
  10708. // Cast is needed so the compiler knows what class we are invoking initWithBools:forKeys:count:
  10709. // on to get the type correct.
  10710. return [[(GPBBoolBoolDictionary*)[self alloc] initWithBools:values
  10711. forKeys:keys
  10712. count:count] autorelease];
  10713. }
  10714. + (instancetype)dictionaryWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10715. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10716. // on to get the type correct.
  10717. return [[(GPBBoolBoolDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10718. }
  10719. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10720. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10721. }
  10722. - (instancetype)init {
  10723. return [self initWithBools:NULL forKeys:NULL count:0];
  10724. }
  10725. - (instancetype)initWithBools:(const BOOL [])values
  10726. forKeys:(const BOOL [])keys
  10727. count:(NSUInteger)count {
  10728. self = [super init];
  10729. if (self) {
  10730. for (NSUInteger i = 0; i < count; ++i) {
  10731. int idx = keys[i] ? 1 : 0;
  10732. _values[idx] = values[i];
  10733. _valueSet[idx] = YES;
  10734. }
  10735. }
  10736. return self;
  10737. }
  10738. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  10739. self = [self initWithBools:NULL forKeys:NULL count:0];
  10740. if (self) {
  10741. if (dictionary) {
  10742. for (int i = 0; i < 2; ++i) {
  10743. if (dictionary->_valueSet[i]) {
  10744. _values[i] = dictionary->_values[i];
  10745. _valueSet[i] = YES;
  10746. }
  10747. }
  10748. }
  10749. }
  10750. return self;
  10751. }
  10752. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10753. #pragma unused(numItems)
  10754. return [self initWithBools:NULL forKeys:NULL count:0];
  10755. }
  10756. #if !defined(NS_BLOCK_ASSERTIONS)
  10757. - (void)dealloc {
  10758. NSAssert(!_autocreator,
  10759. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10760. [self class], _autocreator);
  10761. [super dealloc];
  10762. }
  10763. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10764. - (instancetype)copyWithZone:(NSZone *)zone {
  10765. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  10766. }
  10767. - (BOOL)isEqual:(id)other {
  10768. if (self == other) {
  10769. return YES;
  10770. }
  10771. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  10772. return NO;
  10773. }
  10774. GPBBoolBoolDictionary *otherDictionary = other;
  10775. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10776. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10777. return NO;
  10778. }
  10779. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10780. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10781. return NO;
  10782. }
  10783. return YES;
  10784. }
  10785. - (NSUInteger)hash {
  10786. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10787. }
  10788. - (NSString *)description {
  10789. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10790. if (_valueSet[0]) {
  10791. [result appendFormat:@"NO: %d", _values[0]];
  10792. }
  10793. if (_valueSet[1]) {
  10794. [result appendFormat:@"YES: %d", _values[1]];
  10795. }
  10796. [result appendString:@" }"];
  10797. return result;
  10798. }
  10799. - (NSUInteger)count {
  10800. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10801. }
  10802. - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
  10803. int idx = (key ? 1 : 0);
  10804. if (_valueSet[idx]) {
  10805. if (value) {
  10806. *value = _values[idx];
  10807. }
  10808. return YES;
  10809. }
  10810. return NO;
  10811. }
  10812. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10813. forGPBGenericValueKey:(GPBGenericValue *)key {
  10814. int idx = (key->valueBool ? 1 : 0);
  10815. _values[idx] = value->valueBool;
  10816. _valueSet[idx] = YES;
  10817. }
  10818. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  10819. if (_valueSet[0]) {
  10820. block(@"false", (_values[0] ? @"true" : @"false"));
  10821. }
  10822. if (_valueSet[1]) {
  10823. block(@"true", (_values[1] ? @"true" : @"false"));
  10824. }
  10825. }
  10826. - (void)enumerateKeysAndBoolsUsingBlock:
  10827. (void (^)(BOOL key, BOOL value, BOOL *stop))block {
  10828. BOOL stop = NO;
  10829. if (_valueSet[0]) {
  10830. block(NO, _values[0], &stop);
  10831. }
  10832. if (!stop && _valueSet[1]) {
  10833. block(YES, _values[1], &stop);
  10834. }
  10835. }
  10836. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10837. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10838. NSUInteger count = 0;
  10839. size_t result = 0;
  10840. for (int i = 0; i < 2; ++i) {
  10841. if (_valueSet[i]) {
  10842. ++count;
  10843. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10844. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10845. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10846. }
  10847. }
  10848. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10849. result += tagSize * count;
  10850. return result;
  10851. }
  10852. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10853. asField:(GPBFieldDescriptor *)field {
  10854. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10855. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10856. for (int i = 0; i < 2; ++i) {
  10857. if (_valueSet[i]) {
  10858. // Write the tag.
  10859. [outputStream writeInt32NoTag:tag];
  10860. // Write the size of the message.
  10861. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10862. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10863. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10864. // Write the fields.
  10865. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10866. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10867. }
  10868. }
  10869. }
  10870. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  10871. if (otherDictionary) {
  10872. for (int i = 0; i < 2; ++i) {
  10873. if (otherDictionary->_valueSet[i]) {
  10874. _valueSet[i] = YES;
  10875. _values[i] = otherDictionary->_values[i];
  10876. }
  10877. }
  10878. if (_autocreator) {
  10879. GPBAutocreatedDictionaryModified(_autocreator, self);
  10880. }
  10881. }
  10882. }
  10883. - (void)setBool:(BOOL)value forKey:(BOOL)key {
  10884. int idx = (key ? 1 : 0);
  10885. _values[idx] = value;
  10886. _valueSet[idx] = YES;
  10887. if (_autocreator) {
  10888. GPBAutocreatedDictionaryModified(_autocreator, self);
  10889. }
  10890. }
  10891. - (void)removeBoolForKey:(BOOL)aKey {
  10892. _valueSet[aKey ? 1 : 0] = NO;
  10893. }
  10894. - (void)removeAll {
  10895. _valueSet[0] = NO;
  10896. _valueSet[1] = NO;
  10897. }
  10898. @end
  10899. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  10900. // This block of code is generated, do not edit it directly.
  10901. #pragma mark - Bool -> Float
  10902. @implementation GPBBoolFloatDictionary {
  10903. @package
  10904. float _values[2];
  10905. BOOL _valueSet[2];
  10906. }
  10907. + (instancetype)dictionary {
  10908. return [[[self alloc] initWithFloats:NULL forKeys:NULL count:0] autorelease];
  10909. }
  10910. + (instancetype)dictionaryWithFloat:(float)value
  10911. forKey:(BOOL)key {
  10912. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  10913. // on to get the type correct.
  10914. return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:&value
  10915. forKeys:&key
  10916. count:1] autorelease];
  10917. }
  10918. + (instancetype)dictionaryWithFloats:(const float [])values
  10919. forKeys:(const BOOL [])keys
  10920. count:(NSUInteger)count {
  10921. // Cast is needed so the compiler knows what class we are invoking initWithFloats:forKeys:count:
  10922. // on to get the type correct.
  10923. return [[(GPBBoolFloatDictionary*)[self alloc] initWithFloats:values
  10924. forKeys:keys
  10925. count:count] autorelease];
  10926. }
  10927. + (instancetype)dictionaryWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10928. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  10929. // on to get the type correct.
  10930. return [[(GPBBoolFloatDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  10931. }
  10932. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  10933. return [[[self alloc] initWithCapacity:numItems] autorelease];
  10934. }
  10935. - (instancetype)init {
  10936. return [self initWithFloats:NULL forKeys:NULL count:0];
  10937. }
  10938. - (instancetype)initWithFloats:(const float [])values
  10939. forKeys:(const BOOL [])keys
  10940. count:(NSUInteger)count {
  10941. self = [super init];
  10942. if (self) {
  10943. for (NSUInteger i = 0; i < count; ++i) {
  10944. int idx = keys[i] ? 1 : 0;
  10945. _values[idx] = values[i];
  10946. _valueSet[idx] = YES;
  10947. }
  10948. }
  10949. return self;
  10950. }
  10951. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  10952. self = [self initWithFloats:NULL forKeys:NULL count:0];
  10953. if (self) {
  10954. if (dictionary) {
  10955. for (int i = 0; i < 2; ++i) {
  10956. if (dictionary->_valueSet[i]) {
  10957. _values[i] = dictionary->_values[i];
  10958. _valueSet[i] = YES;
  10959. }
  10960. }
  10961. }
  10962. }
  10963. return self;
  10964. }
  10965. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10966. #pragma unused(numItems)
  10967. return [self initWithFloats:NULL forKeys:NULL count:0];
  10968. }
  10969. #if !defined(NS_BLOCK_ASSERTIONS)
  10970. - (void)dealloc {
  10971. NSAssert(!_autocreator,
  10972. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10973. [self class], _autocreator);
  10974. [super dealloc];
  10975. }
  10976. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10977. - (instancetype)copyWithZone:(NSZone *)zone {
  10978. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  10979. }
  10980. - (BOOL)isEqual:(id)other {
  10981. if (self == other) {
  10982. return YES;
  10983. }
  10984. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  10985. return NO;
  10986. }
  10987. GPBBoolFloatDictionary *otherDictionary = other;
  10988. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10989. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10990. return NO;
  10991. }
  10992. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10993. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10994. return NO;
  10995. }
  10996. return YES;
  10997. }
  10998. - (NSUInteger)hash {
  10999. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11000. }
  11001. - (NSString *)description {
  11002. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11003. if (_valueSet[0]) {
  11004. [result appendFormat:@"NO: %f", _values[0]];
  11005. }
  11006. if (_valueSet[1]) {
  11007. [result appendFormat:@"YES: %f", _values[1]];
  11008. }
  11009. [result appendString:@" }"];
  11010. return result;
  11011. }
  11012. - (NSUInteger)count {
  11013. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11014. }
  11015. - (BOOL)getFloat:(float *)value forKey:(BOOL)key {
  11016. int idx = (key ? 1 : 0);
  11017. if (_valueSet[idx]) {
  11018. if (value) {
  11019. *value = _values[idx];
  11020. }
  11021. return YES;
  11022. }
  11023. return NO;
  11024. }
  11025. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11026. forGPBGenericValueKey:(GPBGenericValue *)key {
  11027. int idx = (key->valueBool ? 1 : 0);
  11028. _values[idx] = value->valueFloat;
  11029. _valueSet[idx] = YES;
  11030. }
  11031. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11032. if (_valueSet[0]) {
  11033. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  11034. }
  11035. if (_valueSet[1]) {
  11036. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  11037. }
  11038. }
  11039. - (void)enumerateKeysAndFloatsUsingBlock:
  11040. (void (^)(BOOL key, float value, BOOL *stop))block {
  11041. BOOL stop = NO;
  11042. if (_valueSet[0]) {
  11043. block(NO, _values[0], &stop);
  11044. }
  11045. if (!stop && _valueSet[1]) {
  11046. block(YES, _values[1], &stop);
  11047. }
  11048. }
  11049. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11050. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11051. NSUInteger count = 0;
  11052. size_t result = 0;
  11053. for (int i = 0; i < 2; ++i) {
  11054. if (_valueSet[i]) {
  11055. ++count;
  11056. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11057. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11058. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11059. }
  11060. }
  11061. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11062. result += tagSize * count;
  11063. return result;
  11064. }
  11065. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11066. asField:(GPBFieldDescriptor *)field {
  11067. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11068. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11069. for (int i = 0; i < 2; ++i) {
  11070. if (_valueSet[i]) {
  11071. // Write the tag.
  11072. [outputStream writeInt32NoTag:tag];
  11073. // Write the size of the message.
  11074. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11075. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11076. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11077. // Write the fields.
  11078. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11079. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11080. }
  11081. }
  11082. }
  11083. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  11084. if (otherDictionary) {
  11085. for (int i = 0; i < 2; ++i) {
  11086. if (otherDictionary->_valueSet[i]) {
  11087. _valueSet[i] = YES;
  11088. _values[i] = otherDictionary->_values[i];
  11089. }
  11090. }
  11091. if (_autocreator) {
  11092. GPBAutocreatedDictionaryModified(_autocreator, self);
  11093. }
  11094. }
  11095. }
  11096. - (void)setFloat:(float)value forKey:(BOOL)key {
  11097. int idx = (key ? 1 : 0);
  11098. _values[idx] = value;
  11099. _valueSet[idx] = YES;
  11100. if (_autocreator) {
  11101. GPBAutocreatedDictionaryModified(_autocreator, self);
  11102. }
  11103. }
  11104. - (void)removeFloatForKey:(BOOL)aKey {
  11105. _valueSet[aKey ? 1 : 0] = NO;
  11106. }
  11107. - (void)removeAll {
  11108. _valueSet[0] = NO;
  11109. _valueSet[1] = NO;
  11110. }
  11111. @end
  11112. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  11113. // This block of code is generated, do not edit it directly.
  11114. #pragma mark - Bool -> Double
  11115. @implementation GPBBoolDoubleDictionary {
  11116. @package
  11117. double _values[2];
  11118. BOOL _valueSet[2];
  11119. }
  11120. + (instancetype)dictionary {
  11121. return [[[self alloc] initWithDoubles:NULL forKeys:NULL count:0] autorelease];
  11122. }
  11123. + (instancetype)dictionaryWithDouble:(double)value
  11124. forKey:(BOOL)key {
  11125. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  11126. // on to get the type correct.
  11127. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:&value
  11128. forKeys:&key
  11129. count:1] autorelease];
  11130. }
  11131. + (instancetype)dictionaryWithDoubles:(const double [])values
  11132. forKeys:(const BOOL [])keys
  11133. count:(NSUInteger)count {
  11134. // Cast is needed so the compiler knows what class we are invoking initWithDoubles:forKeys:count:
  11135. // on to get the type correct.
  11136. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDoubles:values
  11137. forKeys:keys
  11138. count:count] autorelease];
  11139. }
  11140. + (instancetype)dictionaryWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11141. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11142. // on to get the type correct.
  11143. return [[(GPBBoolDoubleDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11144. }
  11145. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11146. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11147. }
  11148. - (instancetype)init {
  11149. return [self initWithDoubles:NULL forKeys:NULL count:0];
  11150. }
  11151. - (instancetype)initWithDoubles:(const double [])values
  11152. forKeys:(const BOOL [])keys
  11153. count:(NSUInteger)count {
  11154. self = [super init];
  11155. if (self) {
  11156. for (NSUInteger i = 0; i < count; ++i) {
  11157. int idx = keys[i] ? 1 : 0;
  11158. _values[idx] = values[i];
  11159. _valueSet[idx] = YES;
  11160. }
  11161. }
  11162. return self;
  11163. }
  11164. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  11165. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  11166. if (self) {
  11167. if (dictionary) {
  11168. for (int i = 0; i < 2; ++i) {
  11169. if (dictionary->_valueSet[i]) {
  11170. _values[i] = dictionary->_values[i];
  11171. _valueSet[i] = YES;
  11172. }
  11173. }
  11174. }
  11175. }
  11176. return self;
  11177. }
  11178. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11179. #pragma unused(numItems)
  11180. return [self initWithDoubles:NULL forKeys:NULL count:0];
  11181. }
  11182. #if !defined(NS_BLOCK_ASSERTIONS)
  11183. - (void)dealloc {
  11184. NSAssert(!_autocreator,
  11185. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11186. [self class], _autocreator);
  11187. [super dealloc];
  11188. }
  11189. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11190. - (instancetype)copyWithZone:(NSZone *)zone {
  11191. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  11192. }
  11193. - (BOOL)isEqual:(id)other {
  11194. if (self == other) {
  11195. return YES;
  11196. }
  11197. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  11198. return NO;
  11199. }
  11200. GPBBoolDoubleDictionary *otherDictionary = other;
  11201. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11202. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11203. return NO;
  11204. }
  11205. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11206. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11207. return NO;
  11208. }
  11209. return YES;
  11210. }
  11211. - (NSUInteger)hash {
  11212. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11213. }
  11214. - (NSString *)description {
  11215. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11216. if (_valueSet[0]) {
  11217. [result appendFormat:@"NO: %lf", _values[0]];
  11218. }
  11219. if (_valueSet[1]) {
  11220. [result appendFormat:@"YES: %lf", _values[1]];
  11221. }
  11222. [result appendString:@" }"];
  11223. return result;
  11224. }
  11225. - (NSUInteger)count {
  11226. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11227. }
  11228. - (BOOL)getDouble:(double *)value forKey:(BOOL)key {
  11229. int idx = (key ? 1 : 0);
  11230. if (_valueSet[idx]) {
  11231. if (value) {
  11232. *value = _values[idx];
  11233. }
  11234. return YES;
  11235. }
  11236. return NO;
  11237. }
  11238. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11239. forGPBGenericValueKey:(GPBGenericValue *)key {
  11240. int idx = (key->valueBool ? 1 : 0);
  11241. _values[idx] = value->valueDouble;
  11242. _valueSet[idx] = YES;
  11243. }
  11244. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11245. if (_valueSet[0]) {
  11246. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  11247. }
  11248. if (_valueSet[1]) {
  11249. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  11250. }
  11251. }
  11252. - (void)enumerateKeysAndDoublesUsingBlock:
  11253. (void (^)(BOOL key, double value, BOOL *stop))block {
  11254. BOOL stop = NO;
  11255. if (_valueSet[0]) {
  11256. block(NO, _values[0], &stop);
  11257. }
  11258. if (!stop && _valueSet[1]) {
  11259. block(YES, _values[1], &stop);
  11260. }
  11261. }
  11262. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11263. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11264. NSUInteger count = 0;
  11265. size_t result = 0;
  11266. for (int i = 0; i < 2; ++i) {
  11267. if (_valueSet[i]) {
  11268. ++count;
  11269. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11270. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11271. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11272. }
  11273. }
  11274. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11275. result += tagSize * count;
  11276. return result;
  11277. }
  11278. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11279. asField:(GPBFieldDescriptor *)field {
  11280. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11281. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11282. for (int i = 0; i < 2; ++i) {
  11283. if (_valueSet[i]) {
  11284. // Write the tag.
  11285. [outputStream writeInt32NoTag:tag];
  11286. // Write the size of the message.
  11287. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11288. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11289. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11290. // Write the fields.
  11291. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11292. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11293. }
  11294. }
  11295. }
  11296. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  11297. if (otherDictionary) {
  11298. for (int i = 0; i < 2; ++i) {
  11299. if (otherDictionary->_valueSet[i]) {
  11300. _valueSet[i] = YES;
  11301. _values[i] = otherDictionary->_values[i];
  11302. }
  11303. }
  11304. if (_autocreator) {
  11305. GPBAutocreatedDictionaryModified(_autocreator, self);
  11306. }
  11307. }
  11308. }
  11309. - (void)setDouble:(double)value forKey:(BOOL)key {
  11310. int idx = (key ? 1 : 0);
  11311. _values[idx] = value;
  11312. _valueSet[idx] = YES;
  11313. if (_autocreator) {
  11314. GPBAutocreatedDictionaryModified(_autocreator, self);
  11315. }
  11316. }
  11317. - (void)removeDoubleForKey:(BOOL)aKey {
  11318. _valueSet[aKey ? 1 : 0] = NO;
  11319. }
  11320. - (void)removeAll {
  11321. _valueSet[0] = NO;
  11322. _valueSet[1] = NO;
  11323. }
  11324. @end
  11325. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  11326. // This block of code is generated, do not edit it directly.
  11327. #pragma mark - Bool -> Object
  11328. @implementation GPBBoolObjectDictionary {
  11329. @package
  11330. id _values[2];
  11331. }
  11332. + (instancetype)dictionary {
  11333. return [[[self alloc] initWithObjects:NULL forKeys:NULL count:0] autorelease];
  11334. }
  11335. + (instancetype)dictionaryWithObject:(id)object
  11336. forKey:(BOOL)key {
  11337. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11338. // on to get the type correct.
  11339. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:&object
  11340. forKeys:&key
  11341. count:1] autorelease];
  11342. }
  11343. + (instancetype)dictionaryWithObjects:(const id [])objects
  11344. forKeys:(const BOOL [])keys
  11345. count:(NSUInteger)count {
  11346. // Cast is needed so the compiler knows what class we are invoking initWithObjects:forKeys:count:
  11347. // on to get the type correct.
  11348. return [[(GPBBoolObjectDictionary*)[self alloc] initWithObjects:objects
  11349. forKeys:keys
  11350. count:count] autorelease];
  11351. }
  11352. + (instancetype)dictionaryWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11353. // Cast is needed so the compiler knows what class we are invoking initWithDictionary:
  11354. // on to get the type correct.
  11355. return [[(GPBBoolObjectDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11356. }
  11357. + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems {
  11358. return [[[self alloc] initWithCapacity:numItems] autorelease];
  11359. }
  11360. - (instancetype)init {
  11361. return [self initWithObjects:NULL forKeys:NULL count:0];
  11362. }
  11363. - (instancetype)initWithObjects:(const id [])objects
  11364. forKeys:(const BOOL [])keys
  11365. count:(NSUInteger)count {
  11366. self = [super init];
  11367. if (self) {
  11368. for (NSUInteger i = 0; i < count; ++i) {
  11369. if (!objects[i]) {
  11370. [NSException raise:NSInvalidArgumentException
  11371. format:@"Attempting to add nil object to a Dictionary"];
  11372. }
  11373. int idx = keys[i] ? 1 : 0;
  11374. [_values[idx] release];
  11375. _values[idx] = (id)[objects[i] retain];
  11376. }
  11377. }
  11378. return self;
  11379. }
  11380. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  11381. self = [self initWithObjects:NULL forKeys:NULL count:0];
  11382. if (self) {
  11383. if (dictionary) {
  11384. _values[0] = [dictionary->_values[0] retain];
  11385. _values[1] = [dictionary->_values[1] retain];
  11386. }
  11387. }
  11388. return self;
  11389. }
  11390. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  11391. #pragma unused(numItems)
  11392. return [self initWithObjects:NULL forKeys:NULL count:0];
  11393. }
  11394. - (void)dealloc {
  11395. NSAssert(!_autocreator,
  11396. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11397. [self class], _autocreator);
  11398. [_values[0] release];
  11399. [_values[1] release];
  11400. [super dealloc];
  11401. }
  11402. - (instancetype)copyWithZone:(NSZone *)zone {
  11403. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  11404. }
  11405. - (BOOL)isEqual:(id)other {
  11406. if (self == other) {
  11407. return YES;
  11408. }
  11409. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  11410. return NO;
  11411. }
  11412. GPBBoolObjectDictionary *otherDictionary = other;
  11413. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  11414. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  11415. return NO;
  11416. }
  11417. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  11418. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  11419. return NO;
  11420. }
  11421. return YES;
  11422. }
  11423. - (NSUInteger)hash {
  11424. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11425. }
  11426. - (NSString *)description {
  11427. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11428. if ((_values[0] != nil)) {
  11429. [result appendFormat:@"NO: %@", _values[0]];
  11430. }
  11431. if ((_values[1] != nil)) {
  11432. [result appendFormat:@"YES: %@", _values[1]];
  11433. }
  11434. [result appendString:@" }"];
  11435. return result;
  11436. }
  11437. - (NSUInteger)count {
  11438. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  11439. }
  11440. - (id)objectForKey:(BOOL)key {
  11441. return _values[key ? 1 : 0];
  11442. }
  11443. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11444. forGPBGenericValueKey:(GPBGenericValue *)key {
  11445. int idx = (key->valueBool ? 1 : 0);
  11446. [_values[idx] release];
  11447. _values[idx] = [value->valueString retain];
  11448. }
  11449. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11450. if (_values[0] != nil) {
  11451. block(@"false", _values[0]);
  11452. }
  11453. if ((_values[1] != nil)) {
  11454. block(@"true", _values[1]);
  11455. }
  11456. }
  11457. - (void)enumerateKeysAndObjectsUsingBlock:
  11458. (void (^)(BOOL key, id object, BOOL *stop))block {
  11459. BOOL stop = NO;
  11460. if (_values[0] != nil) {
  11461. block(NO, _values[0], &stop);
  11462. }
  11463. if (!stop && (_values[1] != nil)) {
  11464. block(YES, _values[1], &stop);
  11465. }
  11466. }
  11467. - (BOOL)isInitialized {
  11468. if (_values[0] && ![_values[0] isInitialized]) {
  11469. return NO;
  11470. }
  11471. if (_values[1] && ![_values[1] isInitialized]) {
  11472. return NO;
  11473. }
  11474. return YES;
  11475. }
  11476. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  11477. GPBBoolObjectDictionary *newDict =
  11478. [[GPBBoolObjectDictionary alloc] init];
  11479. for (int i = 0; i < 2; ++i) {
  11480. if (_values[i] != nil) {
  11481. newDict->_values[i] = [_values[i] copyWithZone:zone];
  11482. }
  11483. }
  11484. return newDict;
  11485. }
  11486. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11487. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11488. NSUInteger count = 0;
  11489. size_t result = 0;
  11490. for (int i = 0; i < 2; ++i) {
  11491. if (_values[i] != nil) {
  11492. ++count;
  11493. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11494. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11495. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11496. }
  11497. }
  11498. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11499. result += tagSize * count;
  11500. return result;
  11501. }
  11502. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11503. asField:(GPBFieldDescriptor *)field {
  11504. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11505. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11506. for (int i = 0; i < 2; ++i) {
  11507. if (_values[i] != nil) {
  11508. // Write the tag.
  11509. [outputStream writeInt32NoTag:tag];
  11510. // Write the size of the message.
  11511. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11512. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11513. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11514. // Write the fields.
  11515. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11516. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11517. }
  11518. }
  11519. }
  11520. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  11521. if (otherDictionary) {
  11522. for (int i = 0; i < 2; ++i) {
  11523. if (otherDictionary->_values[i] != nil) {
  11524. [_values[i] release];
  11525. _values[i] = [otherDictionary->_values[i] retain];
  11526. }
  11527. }
  11528. if (_autocreator) {
  11529. GPBAutocreatedDictionaryModified(_autocreator, self);
  11530. }
  11531. }
  11532. }
  11533. - (void)setObject:(id)object forKey:(BOOL)key {
  11534. if (!object) {
  11535. [NSException raise:NSInvalidArgumentException
  11536. format:@"Attempting to add nil object to a Dictionary"];
  11537. }
  11538. int idx = (key ? 1 : 0);
  11539. [_values[idx] release];
  11540. _values[idx] = [object retain];
  11541. if (_autocreator) {
  11542. GPBAutocreatedDictionaryModified(_autocreator, self);
  11543. }
  11544. }
  11545. - (void)removeObjectForKey:(BOOL)aKey {
  11546. int idx = (aKey ? 1 : 0);
  11547. [_values[idx] release];
  11548. _values[idx] = nil;
  11549. }
  11550. - (void)removeAll {
  11551. for (int i = 0; i < 2; ++i) {
  11552. [_values[i] release];
  11553. _values[i] = nil;
  11554. }
  11555. }
  11556. @end
  11557. //%PDDM-EXPAND-END (8 expansions)
  11558. #pragma mark - Bool -> Enum
  11559. @implementation GPBBoolEnumDictionary {
  11560. @package
  11561. GPBEnumValidationFunc _validationFunc;
  11562. int32_t _values[2];
  11563. BOOL _valueSet[2];
  11564. }
  11565. @synthesize validationFunc = _validationFunc;
  11566. + (instancetype)dictionary {
  11567. return [[[self alloc] initWithValidationFunction:NULL
  11568. rawValues:NULL
  11569. forKeys:NULL
  11570. count:0] autorelease];
  11571. }
  11572. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func {
  11573. return [[[self alloc] initWithValidationFunction:func
  11574. rawValues:NULL
  11575. forKeys:NULL
  11576. count:0] autorelease];
  11577. }
  11578. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11579. rawValue:(int32_t)rawValue
  11580. forKey:(BOOL)key {
  11581. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11582. // on to get the type correct.
  11583. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11584. rawValues:&rawValue
  11585. forKeys:&key
  11586. count:1] autorelease];
  11587. }
  11588. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11589. rawValues:(const int32_t [])values
  11590. forKeys:(const BOOL [])keys
  11591. count:(NSUInteger)count {
  11592. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11593. // on to get the type correct.
  11594. return [[(GPBBoolEnumDictionary*)[self alloc] initWithValidationFunction:func
  11595. rawValues:values
  11596. forKeys:keys
  11597. count:count] autorelease];
  11598. }
  11599. + (instancetype)dictionaryWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11600. // Cast is needed so the compiler knows what class we are invoking initWithValues:forKeys:count:
  11601. // on to get the type correct.
  11602. return [[(GPBBoolEnumDictionary*)[self alloc] initWithDictionary:dictionary] autorelease];
  11603. }
  11604. + (instancetype)dictionaryWithValidationFunction:(GPBEnumValidationFunc)func
  11605. capacity:(NSUInteger)numItems {
  11606. return [[[self alloc] initWithValidationFunction:func capacity:numItems] autorelease];
  11607. }
  11608. - (instancetype)init {
  11609. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  11610. }
  11611. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  11612. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11613. }
  11614. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11615. rawValues:(const int32_t [])rawValues
  11616. forKeys:(const BOOL [])keys
  11617. count:(NSUInteger)count {
  11618. self = [super init];
  11619. if (self) {
  11620. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  11621. for (NSUInteger i = 0; i < count; ++i) {
  11622. int idx = keys[i] ? 1 : 0;
  11623. _values[idx] = rawValues[i];
  11624. _valueSet[idx] = YES;
  11625. }
  11626. }
  11627. return self;
  11628. }
  11629. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  11630. self = [self initWithValidationFunction:dictionary.validationFunc
  11631. rawValues:NULL
  11632. forKeys:NULL
  11633. count:0];
  11634. if (self) {
  11635. if (dictionary) {
  11636. for (int i = 0; i < 2; ++i) {
  11637. if (dictionary->_valueSet[i]) {
  11638. _values[i] = dictionary->_values[i];
  11639. _valueSet[i] = YES;
  11640. }
  11641. }
  11642. }
  11643. }
  11644. return self;
  11645. }
  11646. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  11647. capacity:(NSUInteger)numItems {
  11648. #pragma unused(numItems)
  11649. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  11650. }
  11651. #if !defined(NS_BLOCK_ASSERTIONS)
  11652. - (void)dealloc {
  11653. NSAssert(!_autocreator,
  11654. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11655. [self class], _autocreator);
  11656. [super dealloc];
  11657. }
  11658. #endif // !defined(NS_BLOCK_ASSERTIONS)
  11659. - (instancetype)copyWithZone:(NSZone *)zone {
  11660. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  11661. }
  11662. - (BOOL)isEqual:(id)other {
  11663. if (self == other) {
  11664. return YES;
  11665. }
  11666. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  11667. return NO;
  11668. }
  11669. GPBBoolEnumDictionary *otherDictionary = other;
  11670. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  11671. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  11672. return NO;
  11673. }
  11674. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  11675. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  11676. return NO;
  11677. }
  11678. return YES;
  11679. }
  11680. - (NSUInteger)hash {
  11681. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11682. }
  11683. - (NSString *)description {
  11684. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  11685. if (_valueSet[0]) {
  11686. [result appendFormat:@"NO: %d", _values[0]];
  11687. }
  11688. if (_valueSet[1]) {
  11689. [result appendFormat:@"YES: %d", _values[1]];
  11690. }
  11691. [result appendString:@" }"];
  11692. return result;
  11693. }
  11694. - (NSUInteger)count {
  11695. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  11696. }
  11697. - (BOOL)getEnum:(int32_t*)value forKey:(BOOL)key {
  11698. int idx = (key ? 1 : 0);
  11699. if (_valueSet[idx]) {
  11700. if (value) {
  11701. int32_t result = _values[idx];
  11702. if (!_validationFunc(result)) {
  11703. result = kGPBUnrecognizedEnumeratorValue;
  11704. }
  11705. *value = result;
  11706. }
  11707. return YES;
  11708. }
  11709. return NO;
  11710. }
  11711. - (BOOL)getRawValue:(int32_t*)rawValue forKey:(BOOL)key {
  11712. int idx = (key ? 1 : 0);
  11713. if (_valueSet[idx]) {
  11714. if (rawValue) {
  11715. *rawValue = _values[idx];
  11716. }
  11717. return YES;
  11718. }
  11719. return NO;
  11720. }
  11721. - (void)enumerateKeysAndRawValuesUsingBlock:
  11722. (void (^)(BOOL key, int32_t value, BOOL *stop))block {
  11723. BOOL stop = NO;
  11724. if (_valueSet[0]) {
  11725. block(NO, _values[0], &stop);
  11726. }
  11727. if (!stop && _valueSet[1]) {
  11728. block(YES, _values[1], &stop);
  11729. }
  11730. }
  11731. - (void)enumerateKeysAndEnumsUsingBlock:
  11732. (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  11733. BOOL stop = NO;
  11734. GPBEnumValidationFunc func = _validationFunc;
  11735. int32_t validatedValue;
  11736. if (_valueSet[0]) {
  11737. validatedValue = _values[0];
  11738. if (!func(validatedValue)) {
  11739. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11740. }
  11741. block(NO, validatedValue, &stop);
  11742. }
  11743. if (!stop && _valueSet[1]) {
  11744. validatedValue = _values[1];
  11745. if (!func(validatedValue)) {
  11746. validatedValue = kGPBUnrecognizedEnumeratorValue;
  11747. }
  11748. block(YES, validatedValue, &stop);
  11749. }
  11750. }
  11751. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11752. // This block of code is generated, do not edit it directly.
  11753. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  11754. forKey:(GPBGenericValue *)key
  11755. keyDataType:(GPBDataType)keyDataType {
  11756. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  11757. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  11758. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  11759. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  11760. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  11761. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  11762. [outputStream release];
  11763. return data;
  11764. }
  11765. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  11766. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  11767. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11768. NSUInteger count = 0;
  11769. size_t result = 0;
  11770. for (int i = 0; i < 2; ++i) {
  11771. if (_valueSet[i]) {
  11772. ++count;
  11773. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11774. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11775. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  11776. }
  11777. }
  11778. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  11779. result += tagSize * count;
  11780. return result;
  11781. }
  11782. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  11783. asField:(GPBFieldDescriptor *)field {
  11784. GPBDataType valueDataType = GPBGetFieldDataType(field);
  11785. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  11786. for (int i = 0; i < 2; ++i) {
  11787. if (_valueSet[i]) {
  11788. // Write the tag.
  11789. [outputStream writeInt32NoTag:tag];
  11790. // Write the size of the message.
  11791. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11792. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  11793. [outputStream writeInt32NoTag:(int32_t)msgSize];
  11794. // Write the fields.
  11795. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  11796. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  11797. }
  11798. }
  11799. }
  11800. - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block {
  11801. if (_valueSet[0]) {
  11802. block(@"false", @(_values[0]));
  11803. }
  11804. if (_valueSet[1]) {
  11805. block(@"true", @(_values[1]));
  11806. }
  11807. }
  11808. - (void)setGPBGenericValue:(GPBGenericValue *)value
  11809. forGPBGenericValueKey:(GPBGenericValue *)key {
  11810. int idx = (key->valueBool ? 1 : 0);
  11811. _values[idx] = value->valueInt32;
  11812. _valueSet[idx] = YES;
  11813. }
  11814. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  11815. if (otherDictionary) {
  11816. for (int i = 0; i < 2; ++i) {
  11817. if (otherDictionary->_valueSet[i]) {
  11818. _valueSet[i] = YES;
  11819. _values[i] = otherDictionary->_values[i];
  11820. }
  11821. }
  11822. if (_autocreator) {
  11823. GPBAutocreatedDictionaryModified(_autocreator, self);
  11824. }
  11825. }
  11826. }
  11827. - (void)setEnum:(int32_t)value forKey:(BOOL)key {
  11828. if (!_validationFunc(value)) {
  11829. [NSException raise:NSInvalidArgumentException
  11830. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  11831. value];
  11832. }
  11833. int idx = (key ? 1 : 0);
  11834. _values[idx] = value;
  11835. _valueSet[idx] = YES;
  11836. if (_autocreator) {
  11837. GPBAutocreatedDictionaryModified(_autocreator, self);
  11838. }
  11839. }
  11840. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  11841. int idx = (key ? 1 : 0);
  11842. _values[idx] = rawValue;
  11843. _valueSet[idx] = YES;
  11844. if (_autocreator) {
  11845. GPBAutocreatedDictionaryModified(_autocreator, self);
  11846. }
  11847. }
  11848. - (void)removeEnumForKey:(BOOL)aKey {
  11849. _valueSet[aKey ? 1 : 0] = NO;
  11850. }
  11851. - (void)removeAll {
  11852. _valueSet[0] = NO;
  11853. _valueSet[1] = NO;
  11854. }
  11855. @end
  11856. #pragma mark - NSDictionary Subclass
  11857. @implementation GPBAutocreatedDictionary {
  11858. NSMutableDictionary *_dictionary;
  11859. }
  11860. - (void)dealloc {
  11861. NSAssert(!_autocreator,
  11862. @"%@: Autocreator must be cleared before release, autocreator: %@",
  11863. [self class], _autocreator);
  11864. [_dictionary release];
  11865. [super dealloc];
  11866. }
  11867. #pragma mark Required NSDictionary overrides
  11868. - (instancetype)initWithObjects:(const id [])objects
  11869. forKeys:(const id<NSCopying> [])keys
  11870. count:(NSUInteger)count {
  11871. self = [super init];
  11872. if (self) {
  11873. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  11874. forKeys:keys
  11875. count:count];
  11876. }
  11877. return self;
  11878. }
  11879. - (NSUInteger)count {
  11880. return [_dictionary count];
  11881. }
  11882. - (id)objectForKey:(id)aKey {
  11883. return [_dictionary objectForKey:aKey];
  11884. }
  11885. - (NSEnumerator *)keyEnumerator {
  11886. if (_dictionary == nil) {
  11887. _dictionary = [[NSMutableDictionary alloc] init];
  11888. }
  11889. return [_dictionary keyEnumerator];
  11890. }
  11891. #pragma mark Required NSMutableDictionary overrides
  11892. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  11893. // since we only autocreate empty dictionaries.
  11894. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  11895. if (_dictionary == nil) {
  11896. _dictionary = [[NSMutableDictionary alloc] init];
  11897. }
  11898. [_dictionary setObject:anObject forKey:aKey];
  11899. if (_autocreator) {
  11900. GPBAutocreatedDictionaryModified(_autocreator, self);
  11901. }
  11902. }
  11903. - (void)removeObjectForKey:(id)aKey {
  11904. [_dictionary removeObjectForKey:aKey];
  11905. }
  11906. #pragma mark Extra things hooked
  11907. - (id)copyWithZone:(NSZone *)zone {
  11908. if (_dictionary == nil) {
  11909. return [[NSMutableDictionary allocWithZone:zone] init];
  11910. }
  11911. return [_dictionary copyWithZone:zone];
  11912. }
  11913. - (id)mutableCopyWithZone:(NSZone *)zone {
  11914. if (_dictionary == nil) {
  11915. return [[NSMutableDictionary allocWithZone:zone] init];
  11916. }
  11917. return [_dictionary mutableCopyWithZone:zone];
  11918. }
  11919. // Not really needed, but subscripting is likely common enough it doesn't hurt
  11920. // to ensure it goes directly to the real NSMutableDictionary.
  11921. - (id)objectForKeyedSubscript:(id)key {
  11922. return [_dictionary objectForKeyedSubscript:key];
  11923. }
  11924. // Not really needed, but subscripting is likely common enough it doesn't hurt
  11925. // to ensure it goes directly to the real NSMutableDictionary.
  11926. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  11927. if (_dictionary == nil) {
  11928. _dictionary = [[NSMutableDictionary alloc] init];
  11929. }
  11930. [_dictionary setObject:obj forKeyedSubscript:key];
  11931. if (_autocreator) {
  11932. GPBAutocreatedDictionaryModified(_autocreator, self);
  11933. }
  11934. }
  11935. - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key,
  11936. id obj,
  11937. BOOL *stop))block {
  11938. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  11939. }
  11940. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  11941. usingBlock:(void (^)(id key,
  11942. id obj,
  11943. BOOL *stop))block {
  11944. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  11945. }
  11946. @end
  11947. #pragma clang diagnostic pop