GPBMessage.m 188 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940
  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 "GPBMessage_PackagePrivate.h"
  31. #import <objc/runtime.h>
  32. #import <objc/message.h>
  33. #import "GPBArray_PackagePrivate.h"
  34. #import "GPBCodedInputStream_PackagePrivate.h"
  35. #import "GPBCodedOutputStream.h"
  36. #import "GPBDescriptor_PackagePrivate.h"
  37. #import "GPBDictionary_PackagePrivate.h"
  38. #import "GPBExtensionField_PackagePrivate.h"
  39. #import "GPBExtensionRegistry.h"
  40. #import "GPBRootObject_PackagePrivate.h"
  41. #import "GPBUnknownFieldSet_PackagePrivate.h"
  42. #import "GPBUtilities_PackagePrivate.h"
  43. NSString *const GPBMessageErrorDomain =
  44. GPBNSStringifySymbol(GPBMessageErrorDomain);
  45. #ifdef DEBUG
  46. NSString *const GPBExceptionMessageKey =
  47. GPBNSStringifySymbol(GPBExceptionMessage);
  48. #endif // DEBUG
  49. static NSString *const kGPBDataCoderKey = @"GPBData";
  50. //
  51. // PLEASE REMEMBER:
  52. //
  53. // This is the base class for *all* messages generated, so any selector defined,
  54. // *public* or *private* could end up colliding with a proto message field. So
  55. // avoid using selectors that could match a property, use C functions to hide
  56. // them, etc.
  57. //
  58. @interface GPBMessage () {
  59. @package
  60. GPBUnknownFieldSet *unknownFields_;
  61. NSMutableDictionary *extensionMap_;
  62. NSMutableDictionary *autocreatedExtensionMap_;
  63. // If the object was autocreated, we remember the creator so that if we get
  64. // mutated, we can inform the creator to make our field visible.
  65. GPBMessage *autocreator_;
  66. GPBFieldDescriptor *autocreatorField_;
  67. GPBExtensionField *autocreatorExtension_;
  68. }
  69. @end
  70. static id CreateArrayForField(GPBFieldDescriptor *field,
  71. GPBMessage *autocreator)
  72. __attribute__((ns_returns_retained));
  73. static id GetOrCreateArrayIvarWithField(GPBMessage *self,
  74. GPBFieldDescriptor *field,
  75. GPBFileSyntax syntax);
  76. static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  77. static id CreateMapForField(GPBFieldDescriptor *field,
  78. GPBMessage *autocreator)
  79. __attribute__((ns_returns_retained));
  80. static id GetOrCreateMapIvarWithField(GPBMessage *self,
  81. GPBFieldDescriptor *field,
  82. GPBFileSyntax syntax);
  83. static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  84. static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
  85. NSZone *zone)
  86. __attribute__((ns_returns_retained));
  87. static NSError *MessageError(NSInteger code, NSDictionary *userInfo) {
  88. return [NSError errorWithDomain:GPBMessageErrorDomain
  89. code:code
  90. userInfo:userInfo];
  91. }
  92. static NSError *MessageErrorWithReason(NSInteger code, NSString *reason) {
  93. NSDictionary *userInfo = nil;
  94. if ([reason length]) {
  95. userInfo = @{ @"Reason" : reason };
  96. }
  97. return MessageError(code, userInfo);
  98. }
  99. static void CheckExtension(GPBMessage *self, GPBExtensionField *extension) {
  100. if ([[self class] descriptor] != [extension containingType]) {
  101. [NSException
  102. raise:NSInvalidArgumentException
  103. format:@"Extension %@ used on wrong class (%@ instead of %@)",
  104. extension.descriptor.singletonName,
  105. [[self class] descriptor].name, [extension containingType].name];
  106. }
  107. }
  108. static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
  109. NSZone *zone) {
  110. if (extensionMap.count == 0) {
  111. return nil;
  112. }
  113. NSMutableDictionary *result = [[NSMutableDictionary allocWithZone:zone]
  114. initWithCapacity:extensionMap.count];
  115. for (GPBExtensionField *field in extensionMap) {
  116. id value = [extensionMap objectForKey:field];
  117. GPBExtensionDescriptor *fieldDescriptor = field.descriptor;
  118. BOOL isMessageExtension = GPBExtensionIsMessage(fieldDescriptor);
  119. if ([field isRepeated]) {
  120. if (isMessageExtension) {
  121. NSMutableArray *list =
  122. [[NSMutableArray alloc] initWithCapacity:[value count]];
  123. for (GPBMessage *listValue in value) {
  124. GPBMessage *copiedValue = [listValue copyWithZone:zone];
  125. [list addObject:copiedValue];
  126. [copiedValue release];
  127. }
  128. [result setObject:list forKey:field];
  129. [list release];
  130. } else {
  131. NSMutableArray *copiedValue = [value mutableCopyWithZone:zone];
  132. [result setObject:copiedValue forKey:field];
  133. [copiedValue release];
  134. }
  135. } else {
  136. if (isMessageExtension) {
  137. GPBMessage *copiedValue = [value copyWithZone:zone];
  138. [result setObject:copiedValue forKey:field];
  139. [copiedValue release];
  140. } else {
  141. [result setObject:value forKey:field];
  142. }
  143. }
  144. }
  145. return result;
  146. }
  147. static id CreateArrayForField(GPBFieldDescriptor *field,
  148. GPBMessage *autocreator) {
  149. id result;
  150. GPBType fieldDataType = GPBGetFieldType(field);
  151. switch (fieldDataType) {
  152. case GPBTypeBool:
  153. result = [[GPBBoolArray alloc] init];
  154. break;
  155. case GPBTypeFixed32:
  156. case GPBTypeUInt32:
  157. result = [[GPBUInt32Array alloc] init];
  158. break;
  159. case GPBTypeInt32:
  160. case GPBTypeSFixed32:
  161. case GPBTypeSInt32:
  162. result = [[GPBInt32Array alloc] init];
  163. break;
  164. case GPBTypeFixed64:
  165. case GPBTypeUInt64:
  166. result = [[GPBUInt64Array alloc] init];
  167. break;
  168. case GPBTypeInt64:
  169. case GPBTypeSFixed64:
  170. case GPBTypeSInt64:
  171. result = [[GPBInt64Array alloc] init];
  172. break;
  173. case GPBTypeFloat:
  174. result = [[GPBFloatArray alloc] init];
  175. break;
  176. case GPBTypeDouble:
  177. result = [[GPBDoubleArray alloc] init];
  178. break;
  179. case GPBTypeEnum:
  180. result = [[GPBEnumArray alloc]
  181. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  182. break;
  183. case GPBTypeData:
  184. case GPBTypeGroup:
  185. case GPBTypeMessage:
  186. case GPBTypeString:
  187. if (autocreator) {
  188. result = [[GPBAutocreatedArray alloc] init];
  189. } else {
  190. result = [[NSMutableArray alloc] init];
  191. }
  192. break;
  193. }
  194. if (autocreator) {
  195. if (GPBTypeIsObject(fieldDataType)) {
  196. GPBAutocreatedArray *autoArray = result;
  197. autoArray->_autocreator = autocreator;
  198. } else {
  199. GPBInt32Array *gpbArray = result;
  200. gpbArray->_autocreator = autocreator;
  201. }
  202. }
  203. return result;
  204. }
  205. static id CreateMapForField(GPBFieldDescriptor *field,
  206. GPBMessage *autocreator) {
  207. id result;
  208. GPBType keyType = field.mapKeyType;
  209. GPBType valueType = GPBGetFieldType(field);
  210. switch (keyType) {
  211. case GPBTypeBool:
  212. switch (valueType) {
  213. case GPBTypeBool:
  214. result = [[GPBBoolBoolDictionary alloc] init];
  215. break;
  216. case GPBTypeFixed32:
  217. case GPBTypeUInt32:
  218. result = [[GPBBoolUInt32Dictionary alloc] init];
  219. break;
  220. case GPBTypeInt32:
  221. case GPBTypeSFixed32:
  222. case GPBTypeSInt32:
  223. result = [[GPBBoolInt32Dictionary alloc] init];
  224. break;
  225. case GPBTypeFixed64:
  226. case GPBTypeUInt64:
  227. result = [[GPBBoolUInt64Dictionary alloc] init];
  228. break;
  229. case GPBTypeInt64:
  230. case GPBTypeSFixed64:
  231. case GPBTypeSInt64:
  232. result = [[GPBBoolInt64Dictionary alloc] init];
  233. break;
  234. case GPBTypeFloat:
  235. result = [[GPBBoolFloatDictionary alloc] init];
  236. break;
  237. case GPBTypeDouble:
  238. result = [[GPBBoolDoubleDictionary alloc] init];
  239. break;
  240. case GPBTypeEnum:
  241. result = [[GPBBoolEnumDictionary alloc]
  242. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  243. break;
  244. case GPBTypeData:
  245. case GPBTypeMessage:
  246. case GPBTypeString:
  247. result = [[GPBBoolObjectDictionary alloc] init];
  248. break;
  249. case GPBTypeGroup:
  250. NSCAssert(NO, @"shouldn't happen");
  251. return nil;
  252. }
  253. break;
  254. case GPBTypeFixed32:
  255. case GPBTypeUInt32:
  256. switch (valueType) {
  257. case GPBTypeBool:
  258. result = [[GPBUInt32BoolDictionary alloc] init];
  259. break;
  260. case GPBTypeFixed32:
  261. case GPBTypeUInt32:
  262. result = [[GPBUInt32UInt32Dictionary alloc] init];
  263. break;
  264. case GPBTypeInt32:
  265. case GPBTypeSFixed32:
  266. case GPBTypeSInt32:
  267. result = [[GPBUInt32Int32Dictionary alloc] init];
  268. break;
  269. case GPBTypeFixed64:
  270. case GPBTypeUInt64:
  271. result = [[GPBUInt32UInt64Dictionary alloc] init];
  272. break;
  273. case GPBTypeInt64:
  274. case GPBTypeSFixed64:
  275. case GPBTypeSInt64:
  276. result = [[GPBUInt32Int64Dictionary alloc] init];
  277. break;
  278. case GPBTypeFloat:
  279. result = [[GPBUInt32FloatDictionary alloc] init];
  280. break;
  281. case GPBTypeDouble:
  282. result = [[GPBUInt32DoubleDictionary alloc] init];
  283. break;
  284. case GPBTypeEnum:
  285. result = [[GPBUInt32EnumDictionary alloc]
  286. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  287. break;
  288. case GPBTypeData:
  289. case GPBTypeMessage:
  290. case GPBTypeString:
  291. result = [[GPBUInt32ObjectDictionary alloc] init];
  292. break;
  293. case GPBTypeGroup:
  294. NSCAssert(NO, @"shouldn't happen");
  295. return nil;
  296. }
  297. break;
  298. case GPBTypeInt32:
  299. case GPBTypeSFixed32:
  300. case GPBTypeSInt32:
  301. switch (valueType) {
  302. case GPBTypeBool:
  303. result = [[GPBInt32BoolDictionary alloc] init];
  304. break;
  305. case GPBTypeFixed32:
  306. case GPBTypeUInt32:
  307. result = [[GPBInt32UInt32Dictionary alloc] init];
  308. break;
  309. case GPBTypeInt32:
  310. case GPBTypeSFixed32:
  311. case GPBTypeSInt32:
  312. result = [[GPBInt32Int32Dictionary alloc] init];
  313. break;
  314. case GPBTypeFixed64:
  315. case GPBTypeUInt64:
  316. result = [[GPBInt32UInt64Dictionary alloc] init];
  317. break;
  318. case GPBTypeInt64:
  319. case GPBTypeSFixed64:
  320. case GPBTypeSInt64:
  321. result = [[GPBInt32Int64Dictionary alloc] init];
  322. break;
  323. case GPBTypeFloat:
  324. result = [[GPBInt32FloatDictionary alloc] init];
  325. break;
  326. case GPBTypeDouble:
  327. result = [[GPBInt32DoubleDictionary alloc] init];
  328. break;
  329. case GPBTypeEnum:
  330. result = [[GPBInt32EnumDictionary alloc]
  331. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  332. break;
  333. case GPBTypeData:
  334. case GPBTypeMessage:
  335. case GPBTypeString:
  336. result = [[GPBInt32ObjectDictionary alloc] init];
  337. break;
  338. case GPBTypeGroup:
  339. NSCAssert(NO, @"shouldn't happen");
  340. return nil;
  341. }
  342. break;
  343. case GPBTypeFixed64:
  344. case GPBTypeUInt64:
  345. switch (valueType) {
  346. case GPBTypeBool:
  347. result = [[GPBUInt64BoolDictionary alloc] init];
  348. break;
  349. case GPBTypeFixed32:
  350. case GPBTypeUInt32:
  351. result = [[GPBUInt64UInt32Dictionary alloc] init];
  352. break;
  353. case GPBTypeInt32:
  354. case GPBTypeSFixed32:
  355. case GPBTypeSInt32:
  356. result = [[GPBUInt64Int32Dictionary alloc] init];
  357. break;
  358. case GPBTypeFixed64:
  359. case GPBTypeUInt64:
  360. result = [[GPBUInt64UInt64Dictionary alloc] init];
  361. break;
  362. case GPBTypeInt64:
  363. case GPBTypeSFixed64:
  364. case GPBTypeSInt64:
  365. result = [[GPBUInt64Int64Dictionary alloc] init];
  366. break;
  367. case GPBTypeFloat:
  368. result = [[GPBUInt64FloatDictionary alloc] init];
  369. break;
  370. case GPBTypeDouble:
  371. result = [[GPBUInt64DoubleDictionary alloc] init];
  372. break;
  373. case GPBTypeEnum:
  374. result = [[GPBUInt64EnumDictionary alloc]
  375. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  376. break;
  377. case GPBTypeData:
  378. case GPBTypeMessage:
  379. case GPBTypeString:
  380. result = [[GPBUInt64ObjectDictionary alloc] init];
  381. break;
  382. case GPBTypeGroup:
  383. NSCAssert(NO, @"shouldn't happen");
  384. return nil;
  385. }
  386. break;
  387. case GPBTypeInt64:
  388. case GPBTypeSFixed64:
  389. case GPBTypeSInt64:
  390. switch (valueType) {
  391. case GPBTypeBool:
  392. result = [[GPBInt64BoolDictionary alloc] init];
  393. break;
  394. case GPBTypeFixed32:
  395. case GPBTypeUInt32:
  396. result = [[GPBInt64UInt32Dictionary alloc] init];
  397. break;
  398. case GPBTypeInt32:
  399. case GPBTypeSFixed32:
  400. case GPBTypeSInt32:
  401. result = [[GPBInt64Int32Dictionary alloc] init];
  402. break;
  403. case GPBTypeFixed64:
  404. case GPBTypeUInt64:
  405. result = [[GPBInt64UInt64Dictionary alloc] init];
  406. break;
  407. case GPBTypeInt64:
  408. case GPBTypeSFixed64:
  409. case GPBTypeSInt64:
  410. result = [[GPBInt64Int64Dictionary alloc] init];
  411. break;
  412. case GPBTypeFloat:
  413. result = [[GPBInt64FloatDictionary alloc] init];
  414. break;
  415. case GPBTypeDouble:
  416. result = [[GPBInt64DoubleDictionary alloc] init];
  417. break;
  418. case GPBTypeEnum:
  419. result = [[GPBInt64EnumDictionary alloc]
  420. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  421. break;
  422. case GPBTypeData:
  423. case GPBTypeMessage:
  424. case GPBTypeString:
  425. result = [[GPBInt64ObjectDictionary alloc] init];
  426. break;
  427. case GPBTypeGroup:
  428. NSCAssert(NO, @"shouldn't happen");
  429. return nil;
  430. }
  431. break;
  432. case GPBTypeString:
  433. switch (valueType) {
  434. case GPBTypeBool:
  435. result = [[GPBStringBoolDictionary alloc] init];
  436. break;
  437. case GPBTypeFixed32:
  438. case GPBTypeUInt32:
  439. result = [[GPBStringUInt32Dictionary alloc] init];
  440. break;
  441. case GPBTypeInt32:
  442. case GPBTypeSFixed32:
  443. case GPBTypeSInt32:
  444. result = [[GPBStringInt32Dictionary alloc] init];
  445. break;
  446. case GPBTypeFixed64:
  447. case GPBTypeUInt64:
  448. result = [[GPBStringUInt64Dictionary alloc] init];
  449. break;
  450. case GPBTypeInt64:
  451. case GPBTypeSFixed64:
  452. case GPBTypeSInt64:
  453. result = [[GPBStringInt64Dictionary alloc] init];
  454. break;
  455. case GPBTypeFloat:
  456. result = [[GPBStringFloatDictionary alloc] init];
  457. break;
  458. case GPBTypeDouble:
  459. result = [[GPBStringDoubleDictionary alloc] init];
  460. break;
  461. case GPBTypeEnum:
  462. result = [[GPBStringEnumDictionary alloc]
  463. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  464. break;
  465. case GPBTypeData:
  466. case GPBTypeMessage:
  467. case GPBTypeString:
  468. if (autocreator) {
  469. result = [[GPBAutocreatedDictionary alloc] init];
  470. } else {
  471. result = [[NSMutableDictionary alloc] init];
  472. }
  473. break;
  474. case GPBTypeGroup:
  475. NSCAssert(NO, @"shouldn't happen");
  476. return nil;
  477. }
  478. break;
  479. case GPBTypeFloat:
  480. case GPBTypeDouble:
  481. case GPBTypeEnum:
  482. case GPBTypeData:
  483. case GPBTypeGroup:
  484. case GPBTypeMessage:
  485. NSCAssert(NO, @"shouldn't happen");
  486. return nil;
  487. }
  488. if (autocreator) {
  489. if ((keyType == GPBTypeString) && GPBTypeIsObject(valueType)) {
  490. GPBAutocreatedDictionary *autoDict = result;
  491. autoDict->_autocreator = autocreator;
  492. } else {
  493. GPBInt32Int32Dictionary *gpbDict = result;
  494. gpbDict->_autocreator = autocreator;
  495. }
  496. }
  497. return result;
  498. }
  499. #if !defined(__clang_analyzer__)
  500. // These functions are blocked from the analyzer because the analyzer sees the
  501. // GPBSetRetainedObjectIvarWithFieldInternal() call as consuming the array/map,
  502. // so use of the array/map after the call returns is flagged as a use after
  503. // free.
  504. // But GPBSetRetainedObjectIvarWithFieldInternal() is "consuming" the retain
  505. // count be holding onto the object (it is transfering it), the object is
  506. // still valid after returning from the call. The other way to avoid this
  507. // would be to add a -retain/-autorelease, but that would force every
  508. // repeated/map field parsed into the autorelease pool which is both a memory
  509. // and performance hit.
  510. static id GetOrCreateArrayIvarWithField(GPBMessage *self,
  511. GPBFieldDescriptor *field,
  512. GPBFileSyntax syntax) {
  513. id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  514. if (!array) {
  515. // No lock needed, this is called from places expecting to mutate
  516. // so no threading protection is needed.
  517. array = CreateArrayForField(field, nil);
  518. GPBSetRetainedObjectIvarWithFieldInternal(self, field, array, syntax);
  519. }
  520. return array;
  521. }
  522. // This is like GPBGetObjectIvarWithField(), but for arrays, it should
  523. // only be used to wire the method into the class.
  524. static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
  525. id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  526. if (!array) {
  527. // Check again after getting the lock.
  528. OSSpinLockLock(&self->readOnlyMutex_);
  529. array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  530. if (!array) {
  531. array = CreateArrayForField(field, self);
  532. GPBSetAutocreatedRetainedObjectIvarWithField(self, field, array);
  533. }
  534. OSSpinLockUnlock(&self->readOnlyMutex_);
  535. }
  536. return array;
  537. }
  538. static id GetOrCreateMapIvarWithField(GPBMessage *self,
  539. GPBFieldDescriptor *field,
  540. GPBFileSyntax syntax) {
  541. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  542. if (!dict) {
  543. // No lock needed, this is called from places expecting to mutate
  544. // so no threading protection is needed.
  545. dict = CreateMapForField(field, nil);
  546. GPBSetRetainedObjectIvarWithFieldInternal(self, field, dict, syntax);
  547. }
  548. return dict;
  549. }
  550. // This is like GPBGetObjectIvarWithField(), but for maps, it should
  551. // only be used to wire the method into the class.
  552. static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
  553. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  554. if (!dict) {
  555. // Check again after getting the lock.
  556. OSSpinLockLock(&self->readOnlyMutex_);
  557. dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  558. if (!dict) {
  559. dict = CreateMapForField(field, self);
  560. GPBSetAutocreatedRetainedObjectIvarWithField(self, field, dict);
  561. }
  562. OSSpinLockUnlock(&self->readOnlyMutex_);
  563. }
  564. return dict;
  565. }
  566. #endif // !defined(__clang_analyzer__)
  567. GPBMessage *GPBCreateMessageWithAutocreator(Class msgClass,
  568. GPBMessage *autocreator,
  569. GPBFieldDescriptor *field) {
  570. GPBMessage *message = [[msgClass alloc] init];
  571. message->autocreator_ = autocreator;
  572. message->autocreatorField_ = [field retain];
  573. return message;
  574. }
  575. static GPBMessage *CreateMessageWithAutocreatorForExtension(
  576. Class msgClass, GPBMessage *autocreator, GPBExtensionField *extension)
  577. __attribute__((ns_returns_retained));
  578. static GPBMessage *CreateMessageWithAutocreatorForExtension(
  579. Class msgClass, GPBMessage *autocreator, GPBExtensionField *extension) {
  580. GPBMessage *message = [[msgClass alloc] init];
  581. message->autocreator_ = autocreator;
  582. message->autocreatorExtension_ = [extension retain];
  583. return message;
  584. }
  585. BOOL GPBWasMessageAutocreatedBy(GPBMessage *message, GPBMessage *parent) {
  586. return (message->autocreator_ == parent);
  587. }
  588. void GPBBecomeVisibleToAutocreator(GPBMessage *self) {
  589. // Message objects that are implicitly created by accessing a message field
  590. // are initially not visible via the hasX selector. This method makes them
  591. // visible.
  592. if (self->autocreator_) {
  593. // This will recursively make all parent messages visible until it reaches a
  594. // super-creator that's visible.
  595. if (self->autocreatorField_) {
  596. GPBFileSyntax syntax = [self->autocreator_ descriptor].file.syntax;
  597. GPBSetObjectIvarWithFieldInternal(self->autocreator_,
  598. self->autocreatorField_, self, syntax);
  599. } else {
  600. [self->autocreator_ setExtension:self->autocreatorExtension_ value:self];
  601. }
  602. }
  603. }
  604. void GPBAutocreatedArrayModified(GPBMessage *self, id array) {
  605. // When one of our autocreated arrays adds elements, make it visible.
  606. GPBDescriptor *descriptor = [[self class] descriptor];
  607. for (GPBFieldDescriptor *field in descriptor->fields_) {
  608. if (field.fieldType == GPBFieldTypeRepeated) {
  609. id curArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  610. if (curArray == array) {
  611. if (GPBFieldTypeIsObject(field)) {
  612. GPBAutocreatedArray *autoArray = array;
  613. autoArray->_autocreator = nil;
  614. } else {
  615. GPBInt32Array *gpbArray = array;
  616. gpbArray->_autocreator = nil;
  617. }
  618. GPBBecomeVisibleToAutocreator(self);
  619. return;
  620. }
  621. }
  622. }
  623. NSCAssert(NO, @"Unknown autocreated %@ for %@.", [array class], self);
  624. }
  625. void GPBAutocreatedDictionaryModified(GPBMessage *self, id dictionary) {
  626. // When one of our autocreated dicts adds elements, make it visible.
  627. GPBDescriptor *descriptor = [[self class] descriptor];
  628. for (GPBFieldDescriptor *field in descriptor->fields_) {
  629. if (field.fieldType == GPBFieldTypeMap) {
  630. id curDict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  631. if (curDict == dictionary) {
  632. if ((field.mapKeyType == GPBTypeString) &&
  633. GPBFieldTypeIsObject(field)) {
  634. GPBAutocreatedDictionary *autoDict = dictionary;
  635. autoDict->_autocreator = nil;
  636. } else {
  637. GPBInt32Int32Dictionary *gpbDict = dictionary;
  638. gpbDict->_autocreator = nil;
  639. }
  640. GPBBecomeVisibleToAutocreator(self);
  641. return;
  642. }
  643. }
  644. }
  645. NSCAssert(NO, @"Unknown autocreated %@ for %@.", [dictionary class], self);
  646. }
  647. void GPBClearMessageAutocreator(GPBMessage *self) {
  648. if ((self == nil) || !self->autocreator_) {
  649. return;
  650. }
  651. #if DEBUG && !defined(NS_BLOCK_ASSERTIONS)
  652. // Either the autocreator must have its "has" flag set to YES, or it must be
  653. // NO and not equal to ourselves.
  654. BOOL autocreatorHas =
  655. (self->autocreatorField_
  656. ? GPBGetHasIvarField(self->autocreator_, self->autocreatorField_)
  657. : [self->autocreator_ hasExtension:self->autocreatorExtension_]);
  658. GPBMessage *autocreatorFieldValue =
  659. (self->autocreatorField_
  660. ? GPBGetObjectIvarWithFieldNoAutocreate(self->autocreator_,
  661. self->autocreatorField_)
  662. : [self->autocreator_->autocreatedExtensionMap_
  663. objectForKey:self->autocreatorExtension_]);
  664. NSCAssert(autocreatorHas || autocreatorFieldValue != self,
  665. @"Cannot clear autocreator because it still refers to self, self: %@.",
  666. self);
  667. #endif // DEBUG && !defined(NS_BLOCK_ASSERTIONS)
  668. self->autocreator_ = nil;
  669. [self->autocreatorField_ release];
  670. self->autocreatorField_ = nil;
  671. [self->autocreatorExtension_ release];
  672. self->autocreatorExtension_ = nil;
  673. }
  674. static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
  675. if (!self->unknownFields_) {
  676. self->unknownFields_ = [[GPBUnknownFieldSet alloc] init];
  677. GPBBecomeVisibleToAutocreator(self);
  678. }
  679. return self->unknownFields_;
  680. }
  681. @implementation GPBMessage
  682. + (void)initialize {
  683. Class pbMessageClass = [GPBMessage class];
  684. if ([self class] == pbMessageClass) {
  685. // This is here to start up the "base" class descriptor.
  686. [self descriptor];
  687. // Message shares extension method resolving with GPBRootObject so insure
  688. // it is started up at the same time.
  689. (void)[GPBRootObject class];
  690. } else if ([self superclass] == pbMessageClass) {
  691. // This is here to start up all the "message" subclasses. Just needs to be
  692. // done for the messages, not any of the subclasses.
  693. // This must be done in initialize to enforce thread safety of start up of
  694. // the protocol buffer library.
  695. // Note: The generated code for -descriptor calls
  696. // +[GPBDescriptor allocDescriptorForClass:...], passing the GPBRootObject
  697. // subclass for the file. That call chain is what ensures that *Root class
  698. // is started up to support extension resolution off the message class
  699. // (+resolveClassMethod: below) in a thread safe manner.
  700. [self descriptor];
  701. }
  702. }
  703. + (instancetype)allocWithZone:(NSZone *)zone {
  704. // Override alloc to allocate our classes with the additional storage
  705. // required for the instance variables.
  706. GPBDescriptor *descriptor = [self descriptor];
  707. return NSAllocateObject(self, descriptor->storageSize_, zone);
  708. }
  709. + (instancetype)alloc {
  710. return [self allocWithZone:nil];
  711. }
  712. + (GPBDescriptor *)descriptor {
  713. // This is thread safe because it is called from +initialize.
  714. static GPBDescriptor *descriptor = NULL;
  715. static GPBFileDescriptor *fileDescriptor = NULL;
  716. if (!descriptor) {
  717. // Use a dummy file that marks it as proto2 syntax so when used generically
  718. // it supports unknowns/etc.
  719. fileDescriptor =
  720. [[GPBFileDescriptor alloc] initWithPackage:@"internal"
  721. syntax:GPBFileSyntaxProto2];
  722. descriptor = [GPBDescriptor allocDescriptorForClass:[GPBMessage class]
  723. rootClass:Nil
  724. file:fileDescriptor
  725. fields:NULL
  726. fieldCount:0
  727. oneofs:NULL
  728. oneofCount:0
  729. enums:NULL
  730. enumCount:0
  731. ranges:NULL
  732. rangeCount:0
  733. storageSize:0
  734. wireFormat:NO];
  735. }
  736. return descriptor;
  737. }
  738. + (instancetype)message {
  739. return [[[self alloc] init] autorelease];
  740. }
  741. - (instancetype)init {
  742. if ((self = [super init])) {
  743. messageStorage_ = (GPBMessage_StoragePtr)(
  744. ((uint8_t *)self) + class_getInstanceSize([self class]));
  745. readOnlyMutex_ = OS_SPINLOCK_INIT;
  746. }
  747. return self;
  748. }
  749. - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr {
  750. return [self initWithData:data extensionRegistry:nil error:errorPtr];
  751. }
  752. - (instancetype)initWithData:(NSData *)data
  753. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  754. error:(NSError **)errorPtr {
  755. if ((self = [self init])) {
  756. @try {
  757. [self mergeFromData:data extensionRegistry:extensionRegistry];
  758. }
  759. @catch (NSException *exception) {
  760. [self release];
  761. self = nil;
  762. if (errorPtr) {
  763. *errorPtr = MessageErrorWithReason(GPBMessageErrorCodeMalformedData,
  764. exception.reason);
  765. }
  766. }
  767. #ifdef DEBUG
  768. if (self && !self.initialized) {
  769. [self release];
  770. self = nil;
  771. if (errorPtr) {
  772. *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
  773. }
  774. }
  775. #endif
  776. }
  777. return self;
  778. }
  779. - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
  780. extensionRegistry:
  781. (GPBExtensionRegistry *)extensionRegistry
  782. error:(NSError **)errorPtr {
  783. if ((self = [self init])) {
  784. @try {
  785. [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
  786. }
  787. @catch (NSException *exception) {
  788. [self release];
  789. self = nil;
  790. if (errorPtr) {
  791. *errorPtr = MessageErrorWithReason(GPBMessageErrorCodeMalformedData,
  792. exception.reason);
  793. }
  794. }
  795. #ifdef DEBUG
  796. if (self && !self.initialized) {
  797. [self release];
  798. self = nil;
  799. if (errorPtr) {
  800. *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
  801. }
  802. }
  803. #endif
  804. }
  805. return self;
  806. }
  807. - (void)dealloc {
  808. [self internalClear:NO];
  809. NSCAssert(!autocreator_, @"Autocreator was not cleared before dealloc.");
  810. [super dealloc];
  811. }
  812. - (void)copyFieldsInto:(GPBMessage *)message
  813. zone:(NSZone *)zone
  814. descriptor:(GPBDescriptor *)descriptor {
  815. // Copy all the storage...
  816. memcpy(message->messageStorage_, messageStorage_, descriptor->storageSize_);
  817. GPBFileSyntax syntax = descriptor.file.syntax;
  818. // Loop over the fields doing fixup...
  819. for (GPBFieldDescriptor *field in descriptor->fields_) {
  820. if (GPBFieldIsMapOrArray(field)) {
  821. id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  822. if (value) {
  823. // We need to copy the array/map, but the catch is for message fields,
  824. // we also need to ensure all the messages as those need copying also.
  825. id newValue;
  826. if (GPBFieldTypeIsMessage(field)) {
  827. if (field.fieldType == GPBFieldTypeRepeated) {
  828. NSArray *existingArray = (NSArray *)value;
  829. NSMutableArray *newArray =
  830. [[NSMutableArray alloc] initWithCapacity:existingArray.count];
  831. newValue = newArray;
  832. for (GPBMessage *msg in existingArray) {
  833. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  834. [newArray addObject:copiedMsg];
  835. [copiedMsg release];
  836. }
  837. } else {
  838. if (field.mapKeyType == GPBTypeString) {
  839. // Map is an NSDictionary.
  840. NSDictionary *existingDict = value;
  841. NSMutableDictionary *newDict = [[NSMutableDictionary alloc]
  842. initWithCapacity:existingDict.count];
  843. newValue = newDict;
  844. [existingDict enumerateKeysAndObjectsUsingBlock:^(NSString *key,
  845. GPBMessage *msg,
  846. BOOL *stop) {
  847. #pragma unused(stop)
  848. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  849. [newDict setObject:copiedMsg forKey:key];
  850. [copiedMsg release];
  851. }];
  852. } else {
  853. // Is one of the GPB*ObjectDictionary classes. Type doesn't
  854. // matter, just need one to invoke the selector.
  855. GPBInt32ObjectDictionary *existingDict = value;
  856. newValue = [existingDict deepCopyWithZone:zone];
  857. }
  858. }
  859. } else {
  860. // Not messages (but is a map/array)...
  861. if (field.fieldType == GPBFieldTypeRepeated) {
  862. if (GPBFieldTypeIsObject(field)) {
  863. // NSArray
  864. newValue = [value mutableCopyWithZone:zone];
  865. } else {
  866. // GPB*Array
  867. newValue = [value copyWithZone:zone];
  868. }
  869. } else {
  870. if (field.mapKeyType == GPBTypeString) {
  871. // NSDictionary
  872. newValue = [value mutableCopyWithZone:zone];
  873. } else {
  874. // Is one of the GPB*Dictionary classes. Type doesn't matter,
  875. // just need one to invoke the selector.
  876. GPBInt32Int32Dictionary *existingDict = value;
  877. newValue = [existingDict copyWithZone:zone];
  878. }
  879. }
  880. }
  881. // We retain here because the memcpy picked up the pointer value and
  882. // the next call to SetRetainedObject... will release the current value.
  883. [value retain];
  884. GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
  885. syntax);
  886. }
  887. } else if (GPBFieldTypeIsMessage(field)) {
  888. // For object types, if we have a value, copy it. If we don't,
  889. // zero it to remove the pointer to something that was autocreated
  890. // (and the ptr just got memcpyed).
  891. if (GPBGetHasIvarField(self, field)) {
  892. GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  893. GPBMessage *newValue = [value copyWithZone:zone];
  894. // We retain here because the memcpy picked up the pointer value and
  895. // the next call to SetRetainedObject... will release the current value.
  896. [value retain];
  897. GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
  898. syntax);
  899. } else {
  900. uint8_t *storage = (uint8_t *)message->messageStorage_;
  901. id *typePtr = (id *)&storage[field->description_->offset];
  902. *typePtr = NULL;
  903. }
  904. } else if (GPBFieldTypeIsObject(field) && GPBGetHasIvarField(self, field)) {
  905. // A set string/data value (message picked off above), copy it.
  906. id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  907. id newValue = [value copyWithZone:zone];
  908. // We retain here because the memcpy picked up the pointer value and
  909. // the next call to SetRetainedObject... will release the current value.
  910. [value retain];
  911. GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
  912. syntax);
  913. } else {
  914. // memcpy took care of the rest of the primative fields if they were set.
  915. }
  916. } // for (field in descriptor->fields_)
  917. }
  918. - (id)copyWithZone:(NSZone *)zone {
  919. GPBDescriptor *descriptor = [self descriptor];
  920. GPBMessage *result = [[descriptor.messageClass allocWithZone:zone] init];
  921. [self copyFieldsInto:result zone:zone descriptor:descriptor];
  922. // Make immutable copies of the extra bits.
  923. result->unknownFields_ = [unknownFields_ copyWithZone:zone];
  924. result->extensionMap_ = CloneExtensionMap(extensionMap_, zone);
  925. return result;
  926. }
  927. - (void)clear {
  928. [self internalClear:YES];
  929. }
  930. - (void)internalClear:(BOOL)zeroStorage {
  931. GPBDescriptor *descriptor = [self descriptor];
  932. for (GPBFieldDescriptor *field in descriptor->fields_) {
  933. if (GPBFieldIsMapOrArray(field)) {
  934. id arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  935. if (arrayOrMap) {
  936. if (field.fieldType == GPBFieldTypeRepeated) {
  937. if (GPBFieldTypeIsObject(field)) {
  938. GPBAutocreatedArray *autoArray = arrayOrMap;
  939. if (autoArray->_autocreator == self) {
  940. autoArray->_autocreator = nil;
  941. }
  942. } else {
  943. // Type doesn't matter, it is a GPB*Array.
  944. GPBInt32Array *gpbArray = arrayOrMap;
  945. if (gpbArray->_autocreator == self) {
  946. gpbArray->_autocreator = nil;
  947. }
  948. }
  949. } else {
  950. if ((field.mapKeyType == GPBTypeString) &&
  951. GPBFieldTypeIsObject(field)) {
  952. GPBAutocreatedDictionary *autoDict = arrayOrMap;
  953. if (autoDict->_autocreator == self) {
  954. autoDict->_autocreator = nil;
  955. }
  956. } else {
  957. // Type doesn't matter, it is a GPB*Dictionary.
  958. GPBInt32Int32Dictionary *gpbDict = arrayOrMap;
  959. if (gpbDict->_autocreator == self) {
  960. gpbDict->_autocreator = nil;
  961. }
  962. }
  963. }
  964. [arrayOrMap release];
  965. }
  966. } else if (GPBFieldTypeIsMessage(field)) {
  967. GPBClearAutocreatedMessageIvarWithField(self, field);
  968. GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  969. [value release];
  970. } else if (GPBFieldTypeIsObject(field) && GPBGetHasIvarField(self, field)) {
  971. id value = GPBGetObjectIvarWithField(self, field);
  972. [value release];
  973. }
  974. }
  975. // GPBClearMessageAutocreator() expects that its caller has already been
  976. // removed from autocreatedExtensionMap_ so we set to nil first.
  977. NSArray *autocreatedValues = [autocreatedExtensionMap_ allValues];
  978. [autocreatedExtensionMap_ release];
  979. autocreatedExtensionMap_ = nil;
  980. // Since we're clearing all of our extensions, make sure that we clear the
  981. // autocreator on any that we've created so they no longer refer to us.
  982. for (GPBMessage *value in autocreatedValues) {
  983. NSCAssert(GPBWasMessageAutocreatedBy(value, self),
  984. @"Autocreated extension does not refer back to self.");
  985. GPBClearMessageAutocreator(value);
  986. }
  987. [extensionMap_ release];
  988. extensionMap_ = nil;
  989. [unknownFields_ release];
  990. unknownFields_ = nil;
  991. // Note that clearing does not affect autocreator_. If we are being cleared
  992. // because of a dealloc, then autocreator_ should be nil anyway. If we are
  993. // being cleared because someone explicitly clears us, we don't want to
  994. // sever our relationship with our autocreator.
  995. if (zeroStorage) {
  996. memset(messageStorage_, 0, descriptor->storageSize_);
  997. }
  998. }
  999. - (BOOL)isInitialized {
  1000. GPBDescriptor *descriptor = [self descriptor];
  1001. for (GPBFieldDescriptor *field in descriptor->fields_) {
  1002. if (field.isRequired) {
  1003. if (!GPBGetHasIvarField(self, field)) {
  1004. return NO;
  1005. }
  1006. }
  1007. if (GPBFieldTypeIsMessage(field)) {
  1008. GPBFieldType fieldType = field.fieldType;
  1009. if (fieldType == GPBFieldTypeSingle) {
  1010. if (field.isRequired) {
  1011. GPBMessage *message = GPBGetMessageIvarWithField(self, field);
  1012. if (!message.initialized) {
  1013. return NO;
  1014. }
  1015. } else {
  1016. NSAssert(field.isOptional,
  1017. @"%@: Single message field %@ not required or optional?",
  1018. [self class], field.name);
  1019. if (GPBGetHasIvarField(self, field)) {
  1020. GPBMessage *message = GPBGetMessageIvarWithField(self, field);
  1021. if (!message.initialized) {
  1022. return NO;
  1023. }
  1024. }
  1025. }
  1026. } else if (fieldType == GPBFieldTypeRepeated) {
  1027. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1028. for (GPBMessage *message in array) {
  1029. if (!message.initialized) {
  1030. return NO;
  1031. }
  1032. }
  1033. } else { // fieldType == GPBFieldTypeMap
  1034. if (field.mapKeyType == GPBTypeString) {
  1035. NSDictionary *map =
  1036. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1037. if (map && !GPBDictionaryIsInitializedInternalHelper(map, field)) {
  1038. return NO;
  1039. }
  1040. } else {
  1041. // Real type is GPB*ObjectDictionary, exact type doesn't matter.
  1042. GPBInt32ObjectDictionary *map =
  1043. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1044. if (map && ![map isInitialized]) {
  1045. return NO;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. }
  1051. __block BOOL result = YES;
  1052. [extensionMap_
  1053. enumerateKeysAndObjectsUsingBlock:^(GPBExtensionField *extension, id obj,
  1054. BOOL *stop) {
  1055. GPBExtensionDescriptor *extDesc = extension.descriptor;
  1056. if (GPBExtensionIsMessage(extDesc)) {
  1057. if (extDesc.isRepeated) {
  1058. for (GPBMessage *msg in obj) {
  1059. if (!msg.initialized) {
  1060. result = NO;
  1061. *stop = YES;
  1062. break;
  1063. }
  1064. }
  1065. } else {
  1066. GPBMessage *asMsg = obj;
  1067. if (!asMsg.initialized) {
  1068. result = NO;
  1069. *stop = YES;
  1070. }
  1071. }
  1072. }
  1073. }];
  1074. return result;
  1075. }
  1076. - (GPBDescriptor *)descriptor {
  1077. return [[self class] descriptor];
  1078. }
  1079. - (NSData *)data {
  1080. #ifdef DEBUG
  1081. if (!self.initialized) {
  1082. return nil;
  1083. }
  1084. #endif
  1085. NSMutableData *data = [NSMutableData dataWithLength:[self serializedSize]];
  1086. GPBCodedOutputStream *stream =
  1087. [[GPBCodedOutputStream alloc] initWithData:data];
  1088. @try {
  1089. [self writeToCodedOutputStream:stream];
  1090. }
  1091. @catch (NSException *exception) {
  1092. // This really shouldn't happen. The only way writeToCodedOutputStream:
  1093. // could throw is if something in the library has a bug and the
  1094. // serializedSize was wrong.
  1095. #ifdef DEBUG
  1096. NSLog(@"%@: Internal exception while building message data: %@",
  1097. [self class], exception);
  1098. #endif
  1099. data = nil;
  1100. }
  1101. [stream release];
  1102. return data;
  1103. }
  1104. - (NSData *)delimitedData {
  1105. size_t serializedSize = [self serializedSize];
  1106. size_t varintSize = GPBComputeRawVarint32SizeForInteger(serializedSize);
  1107. NSMutableData *data =
  1108. [NSMutableData dataWithLength:(serializedSize + varintSize)];
  1109. GPBCodedOutputStream *stream =
  1110. [[GPBCodedOutputStream alloc] initWithData:data];
  1111. @try {
  1112. [self writeDelimitedToCodedOutputStream:stream];
  1113. }
  1114. @catch (NSException *exception) {
  1115. // This really shouldn't happen. The only way writeToCodedOutputStream:
  1116. // could throw is if something in the library has a bug and the
  1117. // serializedSize was wrong.
  1118. #ifdef DEBUG
  1119. NSLog(@"%@: Internal exception while building message delimitedData: %@",
  1120. [self class], exception);
  1121. #endif
  1122. data = nil;
  1123. }
  1124. [stream release];
  1125. return data;
  1126. }
  1127. - (void)writeToOutputStream:(NSOutputStream *)output {
  1128. GPBCodedOutputStream *stream =
  1129. [[GPBCodedOutputStream alloc] initWithOutputStream:output];
  1130. [self writeToCodedOutputStream:stream];
  1131. [stream release];
  1132. }
  1133. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
  1134. GPBDescriptor *descriptor = [self descriptor];
  1135. NSArray *fieldsArray = descriptor->fields_;
  1136. NSUInteger fieldCount = fieldsArray.count;
  1137. const GPBExtensionRange *extensionRanges = descriptor.extensionRanges;
  1138. NSUInteger extensionRangesCount = descriptor.extensionRangesCount;
  1139. for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) {
  1140. if (i == fieldCount) {
  1141. [self writeExtensionsToCodedOutputStream:output
  1142. range:extensionRanges[j++]];
  1143. } else if (j == extensionRangesCount ||
  1144. GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) {
  1145. [self writeField:fieldsArray[i++] toCodedOutputStream:output];
  1146. } else {
  1147. [self writeExtensionsToCodedOutputStream:output
  1148. range:extensionRanges[j++]];
  1149. }
  1150. }
  1151. if (descriptor.isWireFormat) {
  1152. [unknownFields_ writeAsMessageSetTo:output];
  1153. } else {
  1154. [unknownFields_ writeToCodedOutputStream:output];
  1155. }
  1156. }
  1157. - (void)writeDelimitedToOutputStream:(NSOutputStream *)output {
  1158. GPBCodedOutputStream *codedOutput =
  1159. [[GPBCodedOutputStream alloc] initWithOutputStream:output];
  1160. [self writeDelimitedToCodedOutputStream:codedOutput];
  1161. [codedOutput release];
  1162. }
  1163. - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output {
  1164. [output writeRawVarintSizeTAs32:[self serializedSize]];
  1165. [self writeToCodedOutputStream:output];
  1166. }
  1167. - (void)writeField:(GPBFieldDescriptor *)field
  1168. toCodedOutputStream:(GPBCodedOutputStream *)output {
  1169. GPBFieldType fieldType = field.fieldType;
  1170. if (fieldType == GPBFieldTypeSingle) {
  1171. BOOL has = GPBGetHasIvarField(self, field);
  1172. if (!has) {
  1173. return;
  1174. }
  1175. }
  1176. uint32_t fieldNumber = GPBFieldNumber(field);
  1177. //%PDDM-DEFINE FIELD_CASE(TYPE, REAL_TYPE)
  1178. //%FIELD_CASE_FULL(TYPE, REAL_TYPE, REAL_TYPE)
  1179. //%PDDM-DEFINE FIELD_CASE_FULL(TYPE, REAL_TYPE, ARRAY_TYPE)
  1180. //% case GPBType##TYPE:
  1181. //% if (fieldType == GPBFieldTypeRepeated) {
  1182. //% uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1183. //% GPB##ARRAY_TYPE##Array *array =
  1184. //% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1185. //% [output write##TYPE##s:fieldNumber values:array tag:tag];
  1186. //% } else if (fieldType == GPBFieldTypeSingle) {
  1187. //% [output write##TYPE:fieldNumber
  1188. //% TYPE$S value:GPBGet##REAL_TYPE##IvarWithField(self, field)];
  1189. //% } else { // fieldType == GPBFieldTypeMap
  1190. //% // Exact type here doesn't matter.
  1191. //% GPBInt32##ARRAY_TYPE##Dictionary *dict =
  1192. //% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1193. //% [dict writeToCodedOutputStream:output asField:field];
  1194. //% }
  1195. //% break;
  1196. //%
  1197. //%PDDM-DEFINE FIELD_CASE2(TYPE)
  1198. //% case GPBType##TYPE:
  1199. //% if (fieldType == GPBFieldTypeRepeated) {
  1200. //% NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1201. //% [output write##TYPE##s:fieldNumber values:array];
  1202. //% } else if (fieldType == GPBFieldTypeSingle) {
  1203. //% // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1204. //% // again.
  1205. //% [output write##TYPE:fieldNumber
  1206. //% TYPE$S value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1207. //% } else { // fieldType == GPBFieldTypeMap
  1208. //% // Exact type here doesn't matter.
  1209. //% id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1210. //% GPBType mapKeyType = field.mapKeyType;
  1211. //% if (mapKeyType == GPBTypeString) {
  1212. //% GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1213. //% } else {
  1214. //% [dict writeToCodedOutputStream:output asField:field];
  1215. //% }
  1216. //% }
  1217. //% break;
  1218. //%
  1219. switch (GPBGetFieldType(field)) {
  1220. //%PDDM-EXPAND FIELD_CASE(Bool, Bool)
  1221. // This block of code is generated, do not edit it directly.
  1222. case GPBTypeBool:
  1223. if (fieldType == GPBFieldTypeRepeated) {
  1224. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1225. GPBBoolArray *array =
  1226. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1227. [output writeBools:fieldNumber values:array tag:tag];
  1228. } else if (fieldType == GPBFieldTypeSingle) {
  1229. [output writeBool:fieldNumber
  1230. value:GPBGetBoolIvarWithField(self, field)];
  1231. } else { // fieldType == GPBFieldTypeMap
  1232. // Exact type here doesn't matter.
  1233. GPBInt32BoolDictionary *dict =
  1234. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1235. [dict writeToCodedOutputStream:output asField:field];
  1236. }
  1237. break;
  1238. //%PDDM-EXPAND FIELD_CASE(Fixed32, UInt32)
  1239. // This block of code is generated, do not edit it directly.
  1240. case GPBTypeFixed32:
  1241. if (fieldType == GPBFieldTypeRepeated) {
  1242. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1243. GPBUInt32Array *array =
  1244. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1245. [output writeFixed32s:fieldNumber values:array tag:tag];
  1246. } else if (fieldType == GPBFieldTypeSingle) {
  1247. [output writeFixed32:fieldNumber
  1248. value:GPBGetUInt32IvarWithField(self, field)];
  1249. } else { // fieldType == GPBFieldTypeMap
  1250. // Exact type here doesn't matter.
  1251. GPBInt32UInt32Dictionary *dict =
  1252. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1253. [dict writeToCodedOutputStream:output asField:field];
  1254. }
  1255. break;
  1256. //%PDDM-EXPAND FIELD_CASE(SFixed32, Int32)
  1257. // This block of code is generated, do not edit it directly.
  1258. case GPBTypeSFixed32:
  1259. if (fieldType == GPBFieldTypeRepeated) {
  1260. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1261. GPBInt32Array *array =
  1262. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1263. [output writeSFixed32s:fieldNumber values:array tag:tag];
  1264. } else if (fieldType == GPBFieldTypeSingle) {
  1265. [output writeSFixed32:fieldNumber
  1266. value:GPBGetInt32IvarWithField(self, field)];
  1267. } else { // fieldType == GPBFieldTypeMap
  1268. // Exact type here doesn't matter.
  1269. GPBInt32Int32Dictionary *dict =
  1270. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1271. [dict writeToCodedOutputStream:output asField:field];
  1272. }
  1273. break;
  1274. //%PDDM-EXPAND FIELD_CASE(Float, Float)
  1275. // This block of code is generated, do not edit it directly.
  1276. case GPBTypeFloat:
  1277. if (fieldType == GPBFieldTypeRepeated) {
  1278. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1279. GPBFloatArray *array =
  1280. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1281. [output writeFloats:fieldNumber values:array tag:tag];
  1282. } else if (fieldType == GPBFieldTypeSingle) {
  1283. [output writeFloat:fieldNumber
  1284. value:GPBGetFloatIvarWithField(self, field)];
  1285. } else { // fieldType == GPBFieldTypeMap
  1286. // Exact type here doesn't matter.
  1287. GPBInt32FloatDictionary *dict =
  1288. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1289. [dict writeToCodedOutputStream:output asField:field];
  1290. }
  1291. break;
  1292. //%PDDM-EXPAND FIELD_CASE(Fixed64, UInt64)
  1293. // This block of code is generated, do not edit it directly.
  1294. case GPBTypeFixed64:
  1295. if (fieldType == GPBFieldTypeRepeated) {
  1296. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1297. GPBUInt64Array *array =
  1298. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1299. [output writeFixed64s:fieldNumber values:array tag:tag];
  1300. } else if (fieldType == GPBFieldTypeSingle) {
  1301. [output writeFixed64:fieldNumber
  1302. value:GPBGetUInt64IvarWithField(self, field)];
  1303. } else { // fieldType == GPBFieldTypeMap
  1304. // Exact type here doesn't matter.
  1305. GPBInt32UInt64Dictionary *dict =
  1306. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1307. [dict writeToCodedOutputStream:output asField:field];
  1308. }
  1309. break;
  1310. //%PDDM-EXPAND FIELD_CASE(SFixed64, Int64)
  1311. // This block of code is generated, do not edit it directly.
  1312. case GPBTypeSFixed64:
  1313. if (fieldType == GPBFieldTypeRepeated) {
  1314. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1315. GPBInt64Array *array =
  1316. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1317. [output writeSFixed64s:fieldNumber values:array tag:tag];
  1318. } else if (fieldType == GPBFieldTypeSingle) {
  1319. [output writeSFixed64:fieldNumber
  1320. value:GPBGetInt64IvarWithField(self, field)];
  1321. } else { // fieldType == GPBFieldTypeMap
  1322. // Exact type here doesn't matter.
  1323. GPBInt32Int64Dictionary *dict =
  1324. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1325. [dict writeToCodedOutputStream:output asField:field];
  1326. }
  1327. break;
  1328. //%PDDM-EXPAND FIELD_CASE(Double, Double)
  1329. // This block of code is generated, do not edit it directly.
  1330. case GPBTypeDouble:
  1331. if (fieldType == GPBFieldTypeRepeated) {
  1332. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1333. GPBDoubleArray *array =
  1334. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1335. [output writeDoubles:fieldNumber values:array tag:tag];
  1336. } else if (fieldType == GPBFieldTypeSingle) {
  1337. [output writeDouble:fieldNumber
  1338. value:GPBGetDoubleIvarWithField(self, field)];
  1339. } else { // fieldType == GPBFieldTypeMap
  1340. // Exact type here doesn't matter.
  1341. GPBInt32DoubleDictionary *dict =
  1342. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1343. [dict writeToCodedOutputStream:output asField:field];
  1344. }
  1345. break;
  1346. //%PDDM-EXPAND FIELD_CASE(Int32, Int32)
  1347. // This block of code is generated, do not edit it directly.
  1348. case GPBTypeInt32:
  1349. if (fieldType == GPBFieldTypeRepeated) {
  1350. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1351. GPBInt32Array *array =
  1352. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1353. [output writeInt32s:fieldNumber values:array tag:tag];
  1354. } else if (fieldType == GPBFieldTypeSingle) {
  1355. [output writeInt32:fieldNumber
  1356. value:GPBGetInt32IvarWithField(self, field)];
  1357. } else { // fieldType == GPBFieldTypeMap
  1358. // Exact type here doesn't matter.
  1359. GPBInt32Int32Dictionary *dict =
  1360. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1361. [dict writeToCodedOutputStream:output asField:field];
  1362. }
  1363. break;
  1364. //%PDDM-EXPAND FIELD_CASE(Int64, Int64)
  1365. // This block of code is generated, do not edit it directly.
  1366. case GPBTypeInt64:
  1367. if (fieldType == GPBFieldTypeRepeated) {
  1368. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1369. GPBInt64Array *array =
  1370. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1371. [output writeInt64s:fieldNumber values:array tag:tag];
  1372. } else if (fieldType == GPBFieldTypeSingle) {
  1373. [output writeInt64:fieldNumber
  1374. value:GPBGetInt64IvarWithField(self, field)];
  1375. } else { // fieldType == GPBFieldTypeMap
  1376. // Exact type here doesn't matter.
  1377. GPBInt32Int64Dictionary *dict =
  1378. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1379. [dict writeToCodedOutputStream:output asField:field];
  1380. }
  1381. break;
  1382. //%PDDM-EXPAND FIELD_CASE(SInt32, Int32)
  1383. // This block of code is generated, do not edit it directly.
  1384. case GPBTypeSInt32:
  1385. if (fieldType == GPBFieldTypeRepeated) {
  1386. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1387. GPBInt32Array *array =
  1388. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1389. [output writeSInt32s:fieldNumber values:array tag:tag];
  1390. } else if (fieldType == GPBFieldTypeSingle) {
  1391. [output writeSInt32:fieldNumber
  1392. value:GPBGetInt32IvarWithField(self, field)];
  1393. } else { // fieldType == GPBFieldTypeMap
  1394. // Exact type here doesn't matter.
  1395. GPBInt32Int32Dictionary *dict =
  1396. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1397. [dict writeToCodedOutputStream:output asField:field];
  1398. }
  1399. break;
  1400. //%PDDM-EXPAND FIELD_CASE(SInt64, Int64)
  1401. // This block of code is generated, do not edit it directly.
  1402. case GPBTypeSInt64:
  1403. if (fieldType == GPBFieldTypeRepeated) {
  1404. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1405. GPBInt64Array *array =
  1406. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1407. [output writeSInt64s:fieldNumber values:array tag:tag];
  1408. } else if (fieldType == GPBFieldTypeSingle) {
  1409. [output writeSInt64:fieldNumber
  1410. value:GPBGetInt64IvarWithField(self, field)];
  1411. } else { // fieldType == GPBFieldTypeMap
  1412. // Exact type here doesn't matter.
  1413. GPBInt32Int64Dictionary *dict =
  1414. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1415. [dict writeToCodedOutputStream:output asField:field];
  1416. }
  1417. break;
  1418. //%PDDM-EXPAND FIELD_CASE(UInt32, UInt32)
  1419. // This block of code is generated, do not edit it directly.
  1420. case GPBTypeUInt32:
  1421. if (fieldType == GPBFieldTypeRepeated) {
  1422. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1423. GPBUInt32Array *array =
  1424. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1425. [output writeUInt32s:fieldNumber values:array tag:tag];
  1426. } else if (fieldType == GPBFieldTypeSingle) {
  1427. [output writeUInt32:fieldNumber
  1428. value:GPBGetUInt32IvarWithField(self, field)];
  1429. } else { // fieldType == GPBFieldTypeMap
  1430. // Exact type here doesn't matter.
  1431. GPBInt32UInt32Dictionary *dict =
  1432. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1433. [dict writeToCodedOutputStream:output asField:field];
  1434. }
  1435. break;
  1436. //%PDDM-EXPAND FIELD_CASE(UInt64, UInt64)
  1437. // This block of code is generated, do not edit it directly.
  1438. case GPBTypeUInt64:
  1439. if (fieldType == GPBFieldTypeRepeated) {
  1440. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1441. GPBUInt64Array *array =
  1442. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1443. [output writeUInt64s:fieldNumber values:array tag:tag];
  1444. } else if (fieldType == GPBFieldTypeSingle) {
  1445. [output writeUInt64:fieldNumber
  1446. value:GPBGetUInt64IvarWithField(self, field)];
  1447. } else { // fieldType == GPBFieldTypeMap
  1448. // Exact type here doesn't matter.
  1449. GPBInt32UInt64Dictionary *dict =
  1450. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1451. [dict writeToCodedOutputStream:output asField:field];
  1452. }
  1453. break;
  1454. //%PDDM-EXPAND FIELD_CASE_FULL(Enum, Int32, Enum)
  1455. // This block of code is generated, do not edit it directly.
  1456. case GPBTypeEnum:
  1457. if (fieldType == GPBFieldTypeRepeated) {
  1458. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1459. GPBEnumArray *array =
  1460. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1461. [output writeEnums:fieldNumber values:array tag:tag];
  1462. } else if (fieldType == GPBFieldTypeSingle) {
  1463. [output writeEnum:fieldNumber
  1464. value:GPBGetInt32IvarWithField(self, field)];
  1465. } else { // fieldType == GPBFieldTypeMap
  1466. // Exact type here doesn't matter.
  1467. GPBInt32EnumDictionary *dict =
  1468. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1469. [dict writeToCodedOutputStream:output asField:field];
  1470. }
  1471. break;
  1472. //%PDDM-EXPAND FIELD_CASE2(Data)
  1473. // This block of code is generated, do not edit it directly.
  1474. case GPBTypeData:
  1475. if (fieldType == GPBFieldTypeRepeated) {
  1476. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1477. [output writeDatas:fieldNumber values:array];
  1478. } else if (fieldType == GPBFieldTypeSingle) {
  1479. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1480. // again.
  1481. [output writeData:fieldNumber
  1482. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1483. } else { // fieldType == GPBFieldTypeMap
  1484. // Exact type here doesn't matter.
  1485. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1486. GPBType mapKeyType = field.mapKeyType;
  1487. if (mapKeyType == GPBTypeString) {
  1488. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1489. } else {
  1490. [dict writeToCodedOutputStream:output asField:field];
  1491. }
  1492. }
  1493. break;
  1494. //%PDDM-EXPAND FIELD_CASE2(String)
  1495. // This block of code is generated, do not edit it directly.
  1496. case GPBTypeString:
  1497. if (fieldType == GPBFieldTypeRepeated) {
  1498. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1499. [output writeStrings:fieldNumber values:array];
  1500. } else if (fieldType == GPBFieldTypeSingle) {
  1501. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1502. // again.
  1503. [output writeString:fieldNumber
  1504. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1505. } else { // fieldType == GPBFieldTypeMap
  1506. // Exact type here doesn't matter.
  1507. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1508. GPBType mapKeyType = field.mapKeyType;
  1509. if (mapKeyType == GPBTypeString) {
  1510. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1511. } else {
  1512. [dict writeToCodedOutputStream:output asField:field];
  1513. }
  1514. }
  1515. break;
  1516. //%PDDM-EXPAND FIELD_CASE2(Message)
  1517. // This block of code is generated, do not edit it directly.
  1518. case GPBTypeMessage:
  1519. if (fieldType == GPBFieldTypeRepeated) {
  1520. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1521. [output writeMessages:fieldNumber values:array];
  1522. } else if (fieldType == GPBFieldTypeSingle) {
  1523. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1524. // again.
  1525. [output writeMessage:fieldNumber
  1526. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1527. } else { // fieldType == GPBFieldTypeMap
  1528. // Exact type here doesn't matter.
  1529. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1530. GPBType mapKeyType = field.mapKeyType;
  1531. if (mapKeyType == GPBTypeString) {
  1532. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1533. } else {
  1534. [dict writeToCodedOutputStream:output asField:field];
  1535. }
  1536. }
  1537. break;
  1538. //%PDDM-EXPAND FIELD_CASE2(Group)
  1539. // This block of code is generated, do not edit it directly.
  1540. case GPBTypeGroup:
  1541. if (fieldType == GPBFieldTypeRepeated) {
  1542. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1543. [output writeGroups:fieldNumber values:array];
  1544. } else if (fieldType == GPBFieldTypeSingle) {
  1545. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1546. // again.
  1547. [output writeGroup:fieldNumber
  1548. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1549. } else { // fieldType == GPBFieldTypeMap
  1550. // Exact type here doesn't matter.
  1551. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1552. GPBType mapKeyType = field.mapKeyType;
  1553. if (mapKeyType == GPBTypeString) {
  1554. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1555. } else {
  1556. [dict writeToCodedOutputStream:output asField:field];
  1557. }
  1558. }
  1559. break;
  1560. //%PDDM-EXPAND-END (18 expansions)
  1561. }
  1562. }
  1563. #pragma mark - Extensions
  1564. - (id)getExtension:(GPBExtensionField *)extension {
  1565. CheckExtension(self, extension);
  1566. id value = [extensionMap_ objectForKey:extension];
  1567. if (value != nil) {
  1568. return value;
  1569. }
  1570. GPBExtensionDescriptor *extDesc = extension.descriptor;
  1571. // No default for repeated.
  1572. if (extDesc.isRepeated) {
  1573. return nil;
  1574. }
  1575. // Non messages get their default.
  1576. if (!GPBExtensionIsMessage(extDesc)) {
  1577. return [extension defaultValue];
  1578. }
  1579. // Check for an autocreated value.
  1580. OSSpinLockLock(&readOnlyMutex_);
  1581. value = [autocreatedExtensionMap_ objectForKey:extension];
  1582. if (!value) {
  1583. // Auto create the message extensions to match normal fields.
  1584. value = CreateMessageWithAutocreatorForExtension(extDesc.msgClass, self,
  1585. extension);
  1586. if (autocreatedExtensionMap_ == nil) {
  1587. autocreatedExtensionMap_ = [[NSMutableDictionary alloc] init];
  1588. }
  1589. // We can't simply call setExtension here because that would clear the new
  1590. // value's autocreator.
  1591. [autocreatedExtensionMap_ setObject:value forKey:extension];
  1592. [value release];
  1593. }
  1594. OSSpinLockUnlock(&readOnlyMutex_);
  1595. return value;
  1596. }
  1597. - (id)getExistingExtension:(GPBExtensionField *)extension {
  1598. // This is an internal method so we don't need to call CheckExtension().
  1599. return [extensionMap_ objectForKey:extension];
  1600. }
  1601. - (BOOL)hasExtension:(GPBExtensionField *)extension {
  1602. #if DEBUG
  1603. CheckExtension(self, extension);
  1604. #endif // DEBUG
  1605. return nil != [extensionMap_ objectForKey:extension];
  1606. }
  1607. - (NSArray *)extensionsCurrentlySet {
  1608. return [extensionMap_ allKeys];
  1609. }
  1610. - (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output
  1611. range:(GPBExtensionRange)range {
  1612. NSArray *sortedExtensions = [[extensionMap_ allKeys]
  1613. sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
  1614. uint32_t start = range.start;
  1615. uint32_t end = range.end;
  1616. for (GPBExtensionField *extension in sortedExtensions) {
  1617. uint32_t fieldNumber = [extension fieldNumber];
  1618. if (fieldNumber >= start && fieldNumber < end) {
  1619. id value = [extensionMap_ objectForKey:extension];
  1620. [extension writeValue:value includingTagToCodedOutputStream:output];
  1621. }
  1622. }
  1623. }
  1624. - (NSArray *)sortedExtensionsInUse {
  1625. return [[extensionMap_ allKeys]
  1626. sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
  1627. }
  1628. - (void)setExtension:(GPBExtensionField *)extension value:(id)value {
  1629. if (!value) {
  1630. [self clearExtension:extension];
  1631. return;
  1632. }
  1633. CheckExtension(self, extension);
  1634. if ([extension isRepeated]) {
  1635. [NSException raise:NSInvalidArgumentException
  1636. format:@"Must call addExtension() for repeated types."];
  1637. }
  1638. if (extensionMap_ == nil) {
  1639. extensionMap_ = [[NSMutableDictionary alloc] init];
  1640. }
  1641. [extensionMap_ setObject:value forKey:extension];
  1642. GPBExtensionDescriptor *descriptor = extension.descriptor;
  1643. if (GPBExtensionIsMessage(descriptor) && !descriptor.isRepeated) {
  1644. GPBMessage *autocreatedValue =
  1645. [[autocreatedExtensionMap_ objectForKey:extension] retain];
  1646. // Must remove from the map before calling GPBClearMessageAutocreator() so
  1647. // that GPBClearMessageAutocreator() knows its safe to clear.
  1648. [autocreatedExtensionMap_ removeObjectForKey:extension];
  1649. GPBClearMessageAutocreator(autocreatedValue);
  1650. [autocreatedValue release];
  1651. }
  1652. GPBBecomeVisibleToAutocreator(self);
  1653. }
  1654. - (void)addExtension:(GPBExtensionField *)extension value:(id)value {
  1655. CheckExtension(self, extension);
  1656. if (![extension isRepeated]) {
  1657. [NSException raise:NSInvalidArgumentException
  1658. format:@"Must call setExtension() for singular types."];
  1659. }
  1660. if (extensionMap_ == nil) {
  1661. extensionMap_ = [[NSMutableDictionary alloc] init];
  1662. }
  1663. NSMutableArray *list = [extensionMap_ objectForKey:extension];
  1664. if (list == nil) {
  1665. list = [NSMutableArray array];
  1666. [extensionMap_ setObject:list forKey:extension];
  1667. }
  1668. [list addObject:value];
  1669. GPBBecomeVisibleToAutocreator(self);
  1670. }
  1671. - (void)setExtension:(GPBExtensionField *)extension
  1672. index:(NSUInteger)idx
  1673. value:(id)value {
  1674. CheckExtension(self, extension);
  1675. if (![extension isRepeated]) {
  1676. [NSException raise:NSInvalidArgumentException
  1677. format:@"Must call setExtension() for singular types."];
  1678. }
  1679. if (extensionMap_ == nil) {
  1680. extensionMap_ = [[NSMutableDictionary alloc] init];
  1681. }
  1682. NSMutableArray *list = [extensionMap_ objectForKey:extension];
  1683. [list replaceObjectAtIndex:idx withObject:value];
  1684. GPBBecomeVisibleToAutocreator(self);
  1685. }
  1686. - (void)clearExtension:(GPBExtensionField *)extension {
  1687. CheckExtension(self, extension);
  1688. // Only become visible if there was actually a value to clear.
  1689. if ([extensionMap_ objectForKey:extension]) {
  1690. [extensionMap_ removeObjectForKey:extension];
  1691. GPBBecomeVisibleToAutocreator(self);
  1692. }
  1693. }
  1694. #pragma mark - mergeFrom
  1695. - (void)mergeFromData:(NSData *)data
  1696. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  1697. GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
  1698. [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
  1699. [input checkLastTagWas:0];
  1700. [input release];
  1701. }
  1702. #pragma mark - mergeDelimitedFrom
  1703. - (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  1704. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  1705. GPBCodedInputStreamState *state = &input->state_;
  1706. if (GPBCodedInputStreamIsAtEnd(state)) {
  1707. return;
  1708. }
  1709. NSData *data = GPBCodedInputStreamReadRetainedDataNoCopy(state);
  1710. if (data == nil) {
  1711. return;
  1712. }
  1713. [self mergeFromData:data extensionRegistry:extensionRegistry];
  1714. [data release];
  1715. }
  1716. #pragma mark - Parse From Data Support
  1717. + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr {
  1718. return [self parseFromData:data extensionRegistry:nil error:errorPtr];
  1719. }
  1720. + (instancetype)parseFromData:(NSData *)data
  1721. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  1722. error:(NSError **)errorPtr {
  1723. return [[[self alloc] initWithData:data
  1724. extensionRegistry:extensionRegistry
  1725. error:errorPtr] autorelease];
  1726. }
  1727. + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
  1728. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  1729. error:(NSError **)errorPtr {
  1730. return
  1731. [[[self alloc] initWithCodedInputStream:input
  1732. extensionRegistry:extensionRegistry
  1733. error:errorPtr] autorelease];
  1734. }
  1735. #pragma mark - Parse Delimited From Data Support
  1736. + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  1737. extensionRegistry:
  1738. (GPBExtensionRegistry *)extensionRegistry
  1739. error:(NSError **)errorPtr {
  1740. GPBMessage *message = [[[self alloc] init] autorelease];
  1741. @try {
  1742. [message mergeDelimitedFromCodedInputStream:input
  1743. extensionRegistry:extensionRegistry];
  1744. }
  1745. @catch (NSException *exception) {
  1746. [message release];
  1747. message = nil;
  1748. if (errorPtr) {
  1749. *errorPtr = MessageErrorWithReason(GPBMessageErrorCodeMalformedData,
  1750. exception.reason);
  1751. }
  1752. }
  1753. #ifdef DEBUG
  1754. if (message && !message.initialized) {
  1755. [message release];
  1756. message = nil;
  1757. if (errorPtr) {
  1758. *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
  1759. }
  1760. }
  1761. #endif
  1762. return message;
  1763. }
  1764. #pragma mark - Unknown Field Support
  1765. - (GPBUnknownFieldSet *)unknownFields {
  1766. return unknownFields_;
  1767. }
  1768. - (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields {
  1769. if (unknownFields != unknownFields_) {
  1770. [unknownFields_ release];
  1771. unknownFields_ = [unknownFields copy];
  1772. GPBBecomeVisibleToAutocreator(self);
  1773. }
  1774. }
  1775. - (void)parseMessageSet:(GPBCodedInputStream *)input
  1776. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  1777. uint32_t typeId = 0;
  1778. NSData *rawBytes = nil;
  1779. GPBExtensionField *extension = nil;
  1780. GPBCodedInputStreamState *state = &input->state_;
  1781. while (true) {
  1782. uint32_t tag = GPBCodedInputStreamReadTag(state);
  1783. if (tag == 0) {
  1784. break;
  1785. }
  1786. if (tag == GPBWireFormatMessageSetTypeIdTag) {
  1787. typeId = GPBCodedInputStreamReadUInt32(state);
  1788. if (typeId != 0) {
  1789. extension = [extensionRegistry getExtension:[self descriptor]
  1790. fieldNumber:typeId];
  1791. }
  1792. } else if (tag == GPBWireFormatMessageSetMessageTag) {
  1793. rawBytes = [GPBCodedInputStreamReadRetainedDataNoCopy(state) autorelease];
  1794. } else {
  1795. if (![input skipField:tag]) {
  1796. break;
  1797. }
  1798. }
  1799. }
  1800. [input checkLastTagWas:GPBWireFormatMessageSetItemEndTag];
  1801. if (rawBytes != nil && typeId != 0) {
  1802. if (extension != nil) {
  1803. GPBCodedInputStream *newInput =
  1804. [[GPBCodedInputStream alloc] initWithData:rawBytes];
  1805. [extension mergeFromCodedInputStream:newInput
  1806. extensionRegistry:extensionRegistry
  1807. message:self];
  1808. [newInput release];
  1809. } else {
  1810. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1811. [unknownFields mergeMessageSetMessage:typeId data:rawBytes];
  1812. }
  1813. }
  1814. }
  1815. - (BOOL)parseUnknownField:(GPBCodedInputStream *)input
  1816. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  1817. tag:(uint32_t)tag {
  1818. GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
  1819. int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
  1820. GPBDescriptor *descriptor = [self descriptor];
  1821. GPBExtensionField *extension =
  1822. [extensionRegistry getExtension:descriptor fieldNumber:fieldNumber];
  1823. if (extension == nil) {
  1824. if (descriptor.wireFormat && GPBWireFormatMessageSetItemTag == tag) {
  1825. [self parseMessageSet:input extensionRegistry:extensionRegistry];
  1826. return YES;
  1827. }
  1828. } else {
  1829. if ([extension wireType] == wireType) {
  1830. [extension mergeFromCodedInputStream:input
  1831. extensionRegistry:extensionRegistry
  1832. message:self];
  1833. return YES;
  1834. }
  1835. }
  1836. if ([GPBUnknownFieldSet isFieldTag:tag]) {
  1837. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1838. return [unknownFields mergeFieldFrom:tag input:input];
  1839. } else {
  1840. return NO;
  1841. }
  1842. }
  1843. - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data {
  1844. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1845. [unknownFields addUnknownMapEntry:fieldNum value:data];
  1846. }
  1847. #pragma mark - MergeFromCodedInputStream Support
  1848. typedef struct MergeFromCodedInputStreamContext {
  1849. GPBMessage *self;
  1850. GPBCodedInputStream *stream;
  1851. GPBMessage *result;
  1852. GPBExtensionRegistry *registry;
  1853. uint32_t tag;
  1854. GPBFileSyntax syntax;
  1855. } MergeFromCodedInputStreamContext;
  1856. //%PDDM-DEFINE MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(NAME, TYPE, ARRAY_TYPE)
  1857. //%static BOOL DynamicMergeFromCodedInputStream##NAME(GPBFieldDescriptor *field,
  1858. //% NAME$S void *voidContext) {
  1859. //% MergeFromCodedInputStreamContext *context =
  1860. //% (MergeFromCodedInputStreamContext *)voidContext;
  1861. //% GPBCodedInputStreamState *state = &context->stream->state_;
  1862. //% GPBFieldType fieldType = field.fieldType;
  1863. //% if (fieldType == GPBFieldTypeRepeated) {
  1864. //% GPB##ARRAY_TYPE##Array *array =
  1865. //% GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  1866. //% if (field.isPackable) {
  1867. //% int32_t length = GPBCodedInputStreamReadInt32(state);
  1868. //% size_t limit = GPBCodedInputStreamPushLimit(state, length);
  1869. //% while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  1870. //% TYPE val = GPBCodedInputStreamRead##NAME(state);
  1871. //% [array addValue:val];
  1872. //% }
  1873. //% GPBCodedInputStreamPopLimit(state, limit);
  1874. //% } else {
  1875. //% TYPE val = GPBCodedInputStreamRead##NAME(state);
  1876. //% [array addValue:val];
  1877. //% }
  1878. //% } else if (fieldType == GPBFieldTypeSingle) {
  1879. //% TYPE val = GPBCodedInputStreamRead##NAME(state);
  1880. //% GPBSet##ARRAY_TYPE##IvarWithFieldInternal(context->result, field, val,
  1881. //% ARRAY_TYPE$S context->syntax);
  1882. //% } else { // fieldType == GPBFieldTypeMap
  1883. //% // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  1884. //% GPBInt32Int32Dictionary *map =
  1885. //% GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  1886. //% [context->stream readMapEntry:map
  1887. //% extensionRegistry:nil
  1888. //% field:field
  1889. //% parentMessage:context->result];
  1890. //% }
  1891. //% return NO;
  1892. //%}
  1893. //%
  1894. //%PDDM-DEFINE MERGE_FROM_CODED_INPUT_STREAM_OBJ_FUNC(NAME)
  1895. //%static BOOL DynamicMergeFromCodedInputStream##NAME(GPBFieldDescriptor *field,
  1896. //% NAME$S void *voidContext) {
  1897. //% MergeFromCodedInputStreamContext *context = voidContext;
  1898. //% GPBCodedInputStreamState *state = &context->stream->state_;
  1899. //% GPBFieldType fieldType = field.fieldType;
  1900. //% if (fieldType == GPBFieldTypeMap) {
  1901. //% // GPB*Dictionary or NSDictionary, exact type doesn't matter at this point.
  1902. //% id map =
  1903. //% GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  1904. //% [context->stream readMapEntry:map
  1905. //% extensionRegistry:nil
  1906. //% field:field
  1907. //% parentMessage:context->result];
  1908. //% } else {
  1909. //% id val = GPBCodedInputStreamReadRetained##NAME(state);
  1910. //% if (fieldType == GPBFieldTypeRepeated) {
  1911. //% NSMutableArray *array =
  1912. //% GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  1913. //% [array addObject:val];
  1914. //% [val release];
  1915. //% } else { // fieldType == GPBFieldTypeSingle
  1916. //% GPBSetRetainedObjectIvarWithFieldInternal(context->result, field, val,
  1917. //% context->syntax);
  1918. //% }
  1919. //% }
  1920. //% return NO;
  1921. //%}
  1922. //%
  1923. static BOOL DynamicMergeFromCodedInputStreamGroup(GPBFieldDescriptor *field,
  1924. void *voidContext) {
  1925. MergeFromCodedInputStreamContext *context =
  1926. (MergeFromCodedInputStreamContext *)voidContext;
  1927. int number = GPBFieldNumber(field);
  1928. GPBFieldType fieldType = field.fieldType;
  1929. if (fieldType == GPBFieldTypeRepeated) {
  1930. GPBMessage *message = [[field.msgClass alloc] init];
  1931. NSMutableArray *array =
  1932. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  1933. [context->stream readGroup:number
  1934. message:message
  1935. extensionRegistry:context->registry];
  1936. [array addObject:message];
  1937. [message release];
  1938. } else if (fieldType == GPBFieldTypeSingle) {
  1939. BOOL has = GPBGetHasIvarField(context->result, field);
  1940. if (has) {
  1941. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1942. // again.
  1943. GPBMessage *message =
  1944. GPBGetObjectIvarWithFieldNoAutocreate(context->result, field);
  1945. [context->stream readGroup:number
  1946. message:message
  1947. extensionRegistry:context->registry];
  1948. } else {
  1949. GPBMessage *message = [[field.msgClass alloc] init];
  1950. [context->stream readGroup:number
  1951. message:message
  1952. extensionRegistry:context->registry];
  1953. GPBSetRetainedObjectIvarWithFieldInternal(context->result, field, message,
  1954. context->syntax);
  1955. }
  1956. } else { // fieldType == GPBFieldTypeMap
  1957. NSCAssert(NO, @"Shouldn't happen");
  1958. return YES;
  1959. }
  1960. return NO;
  1961. }
  1962. static BOOL DynamicMergeFromCodedInputStreamMessage(GPBFieldDescriptor *field,
  1963. void *voidContext) {
  1964. MergeFromCodedInputStreamContext *context =
  1965. (MergeFromCodedInputStreamContext *)voidContext;
  1966. GPBFieldType fieldType = field.fieldType;
  1967. if (fieldType == GPBFieldTypeRepeated) {
  1968. GPBMessage *message = [[field.msgClass alloc] init];
  1969. NSMutableArray *array =
  1970. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  1971. [context->stream readMessage:message extensionRegistry:context->registry];
  1972. [array addObject:message];
  1973. [message release];
  1974. } else if (fieldType == GPBFieldTypeSingle) {
  1975. BOOL has = GPBGetHasIvarField(context->result, field);
  1976. if (has) {
  1977. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1978. // again.
  1979. GPBMessage *message =
  1980. GPBGetObjectIvarWithFieldNoAutocreate(context->result, field);
  1981. [context->stream readMessage:message extensionRegistry:context->registry];
  1982. } else {
  1983. GPBMessage *message = [[field.msgClass alloc] init];
  1984. [context->stream readMessage:message extensionRegistry:context->registry];
  1985. GPBSetRetainedObjectIvarWithFieldInternal(context->result, field, message,
  1986. context->syntax);
  1987. }
  1988. } else { // fieldType == GPBFieldTypeMap
  1989. // GPB*Dictionary or NSDictionary, exact type doesn't matter.
  1990. id map =
  1991. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  1992. [context->stream readMapEntry:map
  1993. extensionRegistry:context->registry
  1994. field:field
  1995. parentMessage:context->result];
  1996. }
  1997. return NO;
  1998. }
  1999. static BOOL DynamicMergeFromCodedInputStreamEnum(GPBFieldDescriptor *field,
  2000. void *voidContext) {
  2001. MergeFromCodedInputStreamContext *context =
  2002. (MergeFromCodedInputStreamContext *)voidContext;
  2003. int number = GPBFieldNumber(field);
  2004. BOOL hasPreservingUnknownEnumSemantics =
  2005. GPBHasPreservingUnknownEnumSemantics(context->syntax);
  2006. GPBCodedInputStreamState *state = &context->stream->state_;
  2007. GPBFieldType fieldType = field.fieldType;
  2008. if (fieldType == GPBFieldTypeRepeated) {
  2009. GPBEnumArray *array =
  2010. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2011. if (field.isPackable) {
  2012. int32_t length = GPBCodedInputStreamReadInt32(state);
  2013. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2014. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2015. int32_t val = GPBCodedInputStreamReadEnum(state);
  2016. if (hasPreservingUnknownEnumSemantics || [field isValidEnumValue:val]) {
  2017. [array addRawValue:val];
  2018. } else {
  2019. GPBUnknownFieldSet *unknownFields =
  2020. GetOrMakeUnknownFields(context->self);
  2021. [unknownFields mergeVarintField:number value:val];
  2022. }
  2023. }
  2024. GPBCodedInputStreamPopLimit(state, limit);
  2025. } else {
  2026. int32_t val = GPBCodedInputStreamReadEnum(state);
  2027. if (hasPreservingUnknownEnumSemantics || [field isValidEnumValue:val]) {
  2028. [array addRawValue:val];
  2029. } else {
  2030. GPBUnknownFieldSet *unknownFields =
  2031. GetOrMakeUnknownFields(context->self);
  2032. [unknownFields mergeVarintField:number value:val];
  2033. }
  2034. }
  2035. } else if (fieldType == GPBFieldTypeSingle) {
  2036. int32_t val = GPBCodedInputStreamReadEnum(state);
  2037. if (hasPreservingUnknownEnumSemantics || [field isValidEnumValue:val]) {
  2038. GPBSetInt32IvarWithFieldInternal(context->result, field, val,
  2039. context->syntax);
  2040. } else {
  2041. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(context->self);
  2042. [unknownFields mergeVarintField:number value:val];
  2043. }
  2044. } else { // fieldType == GPBFieldTypeMap
  2045. // The exact type doesn't matter, just need to know it is a
  2046. // GPB*EnumDictionary.
  2047. GPBInt32EnumDictionary *map =
  2048. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2049. [context->stream readMapEntry:map
  2050. extensionRegistry:context->registry
  2051. field:field
  2052. parentMessage:context->result];
  2053. }
  2054. return NO;
  2055. }
  2056. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Bool, BOOL, Bool)
  2057. // This block of code is generated, do not edit it directly.
  2058. static BOOL DynamicMergeFromCodedInputStreamBool(GPBFieldDescriptor *field,
  2059. void *voidContext) {
  2060. MergeFromCodedInputStreamContext *context =
  2061. (MergeFromCodedInputStreamContext *)voidContext;
  2062. GPBCodedInputStreamState *state = &context->stream->state_;
  2063. GPBFieldType fieldType = field.fieldType;
  2064. if (fieldType == GPBFieldTypeRepeated) {
  2065. GPBBoolArray *array =
  2066. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2067. if (field.isPackable) {
  2068. int32_t length = GPBCodedInputStreamReadInt32(state);
  2069. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2070. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2071. BOOL val = GPBCodedInputStreamReadBool(state);
  2072. [array addValue:val];
  2073. }
  2074. GPBCodedInputStreamPopLimit(state, limit);
  2075. } else {
  2076. BOOL val = GPBCodedInputStreamReadBool(state);
  2077. [array addValue:val];
  2078. }
  2079. } else if (fieldType == GPBFieldTypeSingle) {
  2080. BOOL val = GPBCodedInputStreamReadBool(state);
  2081. GPBSetBoolIvarWithFieldInternal(context->result, field, val,
  2082. context->syntax);
  2083. } else { // fieldType == GPBFieldTypeMap
  2084. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2085. GPBInt32Int32Dictionary *map =
  2086. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2087. [context->stream readMapEntry:map
  2088. extensionRegistry:nil
  2089. field:field
  2090. parentMessage:context->result];
  2091. }
  2092. return NO;
  2093. }
  2094. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Int32, int32_t, Int32)
  2095. // This block of code is generated, do not edit it directly.
  2096. static BOOL DynamicMergeFromCodedInputStreamInt32(GPBFieldDescriptor *field,
  2097. void *voidContext) {
  2098. MergeFromCodedInputStreamContext *context =
  2099. (MergeFromCodedInputStreamContext *)voidContext;
  2100. GPBCodedInputStreamState *state = &context->stream->state_;
  2101. GPBFieldType fieldType = field.fieldType;
  2102. if (fieldType == GPBFieldTypeRepeated) {
  2103. GPBInt32Array *array =
  2104. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2105. if (field.isPackable) {
  2106. int32_t length = GPBCodedInputStreamReadInt32(state);
  2107. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2108. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2109. int32_t val = GPBCodedInputStreamReadInt32(state);
  2110. [array addValue:val];
  2111. }
  2112. GPBCodedInputStreamPopLimit(state, limit);
  2113. } else {
  2114. int32_t val = GPBCodedInputStreamReadInt32(state);
  2115. [array addValue:val];
  2116. }
  2117. } else if (fieldType == GPBFieldTypeSingle) {
  2118. int32_t val = GPBCodedInputStreamReadInt32(state);
  2119. GPBSetInt32IvarWithFieldInternal(context->result, field, val,
  2120. context->syntax);
  2121. } else { // fieldType == GPBFieldTypeMap
  2122. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2123. GPBInt32Int32Dictionary *map =
  2124. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2125. [context->stream readMapEntry:map
  2126. extensionRegistry:nil
  2127. field:field
  2128. parentMessage:context->result];
  2129. }
  2130. return NO;
  2131. }
  2132. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(SInt32, int32_t, Int32)
  2133. // This block of code is generated, do not edit it directly.
  2134. static BOOL DynamicMergeFromCodedInputStreamSInt32(GPBFieldDescriptor *field,
  2135. void *voidContext) {
  2136. MergeFromCodedInputStreamContext *context =
  2137. (MergeFromCodedInputStreamContext *)voidContext;
  2138. GPBCodedInputStreamState *state = &context->stream->state_;
  2139. GPBFieldType fieldType = field.fieldType;
  2140. if (fieldType == GPBFieldTypeRepeated) {
  2141. GPBInt32Array *array =
  2142. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2143. if (field.isPackable) {
  2144. int32_t length = GPBCodedInputStreamReadInt32(state);
  2145. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2146. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2147. int32_t val = GPBCodedInputStreamReadSInt32(state);
  2148. [array addValue:val];
  2149. }
  2150. GPBCodedInputStreamPopLimit(state, limit);
  2151. } else {
  2152. int32_t val = GPBCodedInputStreamReadSInt32(state);
  2153. [array addValue:val];
  2154. }
  2155. } else if (fieldType == GPBFieldTypeSingle) {
  2156. int32_t val = GPBCodedInputStreamReadSInt32(state);
  2157. GPBSetInt32IvarWithFieldInternal(context->result, field, val,
  2158. context->syntax);
  2159. } else { // fieldType == GPBFieldTypeMap
  2160. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2161. GPBInt32Int32Dictionary *map =
  2162. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2163. [context->stream readMapEntry:map
  2164. extensionRegistry:nil
  2165. field:field
  2166. parentMessage:context->result];
  2167. }
  2168. return NO;
  2169. }
  2170. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(SFixed32, int32_t, Int32)
  2171. // This block of code is generated, do not edit it directly.
  2172. static BOOL DynamicMergeFromCodedInputStreamSFixed32(GPBFieldDescriptor *field,
  2173. void *voidContext) {
  2174. MergeFromCodedInputStreamContext *context =
  2175. (MergeFromCodedInputStreamContext *)voidContext;
  2176. GPBCodedInputStreamState *state = &context->stream->state_;
  2177. GPBFieldType fieldType = field.fieldType;
  2178. if (fieldType == GPBFieldTypeRepeated) {
  2179. GPBInt32Array *array =
  2180. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2181. if (field.isPackable) {
  2182. int32_t length = GPBCodedInputStreamReadInt32(state);
  2183. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2184. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2185. int32_t val = GPBCodedInputStreamReadSFixed32(state);
  2186. [array addValue:val];
  2187. }
  2188. GPBCodedInputStreamPopLimit(state, limit);
  2189. } else {
  2190. int32_t val = GPBCodedInputStreamReadSFixed32(state);
  2191. [array addValue:val];
  2192. }
  2193. } else if (fieldType == GPBFieldTypeSingle) {
  2194. int32_t val = GPBCodedInputStreamReadSFixed32(state);
  2195. GPBSetInt32IvarWithFieldInternal(context->result, field, val,
  2196. context->syntax);
  2197. } else { // fieldType == GPBFieldTypeMap
  2198. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2199. GPBInt32Int32Dictionary *map =
  2200. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2201. [context->stream readMapEntry:map
  2202. extensionRegistry:nil
  2203. field:field
  2204. parentMessage:context->result];
  2205. }
  2206. return NO;
  2207. }
  2208. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(UInt32, uint32_t, UInt32)
  2209. // This block of code is generated, do not edit it directly.
  2210. static BOOL DynamicMergeFromCodedInputStreamUInt32(GPBFieldDescriptor *field,
  2211. void *voidContext) {
  2212. MergeFromCodedInputStreamContext *context =
  2213. (MergeFromCodedInputStreamContext *)voidContext;
  2214. GPBCodedInputStreamState *state = &context->stream->state_;
  2215. GPBFieldType fieldType = field.fieldType;
  2216. if (fieldType == GPBFieldTypeRepeated) {
  2217. GPBUInt32Array *array =
  2218. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2219. if (field.isPackable) {
  2220. int32_t length = GPBCodedInputStreamReadInt32(state);
  2221. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2222. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2223. uint32_t val = GPBCodedInputStreamReadUInt32(state);
  2224. [array addValue:val];
  2225. }
  2226. GPBCodedInputStreamPopLimit(state, limit);
  2227. } else {
  2228. uint32_t val = GPBCodedInputStreamReadUInt32(state);
  2229. [array addValue:val];
  2230. }
  2231. } else if (fieldType == GPBFieldTypeSingle) {
  2232. uint32_t val = GPBCodedInputStreamReadUInt32(state);
  2233. GPBSetUInt32IvarWithFieldInternal(context->result, field, val,
  2234. context->syntax);
  2235. } else { // fieldType == GPBFieldTypeMap
  2236. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2237. GPBInt32Int32Dictionary *map =
  2238. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2239. [context->stream readMapEntry:map
  2240. extensionRegistry:nil
  2241. field:field
  2242. parentMessage:context->result];
  2243. }
  2244. return NO;
  2245. }
  2246. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Fixed32, uint32_t, UInt32)
  2247. // This block of code is generated, do not edit it directly.
  2248. static BOOL DynamicMergeFromCodedInputStreamFixed32(GPBFieldDescriptor *field,
  2249. void *voidContext) {
  2250. MergeFromCodedInputStreamContext *context =
  2251. (MergeFromCodedInputStreamContext *)voidContext;
  2252. GPBCodedInputStreamState *state = &context->stream->state_;
  2253. GPBFieldType fieldType = field.fieldType;
  2254. if (fieldType == GPBFieldTypeRepeated) {
  2255. GPBUInt32Array *array =
  2256. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2257. if (field.isPackable) {
  2258. int32_t length = GPBCodedInputStreamReadInt32(state);
  2259. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2260. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2261. uint32_t val = GPBCodedInputStreamReadFixed32(state);
  2262. [array addValue:val];
  2263. }
  2264. GPBCodedInputStreamPopLimit(state, limit);
  2265. } else {
  2266. uint32_t val = GPBCodedInputStreamReadFixed32(state);
  2267. [array addValue:val];
  2268. }
  2269. } else if (fieldType == GPBFieldTypeSingle) {
  2270. uint32_t val = GPBCodedInputStreamReadFixed32(state);
  2271. GPBSetUInt32IvarWithFieldInternal(context->result, field, val,
  2272. context->syntax);
  2273. } else { // fieldType == GPBFieldTypeMap
  2274. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2275. GPBInt32Int32Dictionary *map =
  2276. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2277. [context->stream readMapEntry:map
  2278. extensionRegistry:nil
  2279. field:field
  2280. parentMessage:context->result];
  2281. }
  2282. return NO;
  2283. }
  2284. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Int64, int64_t, Int64)
  2285. // This block of code is generated, do not edit it directly.
  2286. static BOOL DynamicMergeFromCodedInputStreamInt64(GPBFieldDescriptor *field,
  2287. void *voidContext) {
  2288. MergeFromCodedInputStreamContext *context =
  2289. (MergeFromCodedInputStreamContext *)voidContext;
  2290. GPBCodedInputStreamState *state = &context->stream->state_;
  2291. GPBFieldType fieldType = field.fieldType;
  2292. if (fieldType == GPBFieldTypeRepeated) {
  2293. GPBInt64Array *array =
  2294. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2295. if (field.isPackable) {
  2296. int32_t length = GPBCodedInputStreamReadInt32(state);
  2297. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2298. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2299. int64_t val = GPBCodedInputStreamReadInt64(state);
  2300. [array addValue:val];
  2301. }
  2302. GPBCodedInputStreamPopLimit(state, limit);
  2303. } else {
  2304. int64_t val = GPBCodedInputStreamReadInt64(state);
  2305. [array addValue:val];
  2306. }
  2307. } else if (fieldType == GPBFieldTypeSingle) {
  2308. int64_t val = GPBCodedInputStreamReadInt64(state);
  2309. GPBSetInt64IvarWithFieldInternal(context->result, field, val,
  2310. context->syntax);
  2311. } else { // fieldType == GPBFieldTypeMap
  2312. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2313. GPBInt32Int32Dictionary *map =
  2314. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2315. [context->stream readMapEntry:map
  2316. extensionRegistry:nil
  2317. field:field
  2318. parentMessage:context->result];
  2319. }
  2320. return NO;
  2321. }
  2322. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(SFixed64, int64_t, Int64)
  2323. // This block of code is generated, do not edit it directly.
  2324. static BOOL DynamicMergeFromCodedInputStreamSFixed64(GPBFieldDescriptor *field,
  2325. void *voidContext) {
  2326. MergeFromCodedInputStreamContext *context =
  2327. (MergeFromCodedInputStreamContext *)voidContext;
  2328. GPBCodedInputStreamState *state = &context->stream->state_;
  2329. GPBFieldType fieldType = field.fieldType;
  2330. if (fieldType == GPBFieldTypeRepeated) {
  2331. GPBInt64Array *array =
  2332. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2333. if (field.isPackable) {
  2334. int32_t length = GPBCodedInputStreamReadInt32(state);
  2335. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2336. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2337. int64_t val = GPBCodedInputStreamReadSFixed64(state);
  2338. [array addValue:val];
  2339. }
  2340. GPBCodedInputStreamPopLimit(state, limit);
  2341. } else {
  2342. int64_t val = GPBCodedInputStreamReadSFixed64(state);
  2343. [array addValue:val];
  2344. }
  2345. } else if (fieldType == GPBFieldTypeSingle) {
  2346. int64_t val = GPBCodedInputStreamReadSFixed64(state);
  2347. GPBSetInt64IvarWithFieldInternal(context->result, field, val,
  2348. context->syntax);
  2349. } else { // fieldType == GPBFieldTypeMap
  2350. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2351. GPBInt32Int32Dictionary *map =
  2352. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2353. [context->stream readMapEntry:map
  2354. extensionRegistry:nil
  2355. field:field
  2356. parentMessage:context->result];
  2357. }
  2358. return NO;
  2359. }
  2360. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(SInt64, int64_t, Int64)
  2361. // This block of code is generated, do not edit it directly.
  2362. static BOOL DynamicMergeFromCodedInputStreamSInt64(GPBFieldDescriptor *field,
  2363. void *voidContext) {
  2364. MergeFromCodedInputStreamContext *context =
  2365. (MergeFromCodedInputStreamContext *)voidContext;
  2366. GPBCodedInputStreamState *state = &context->stream->state_;
  2367. GPBFieldType fieldType = field.fieldType;
  2368. if (fieldType == GPBFieldTypeRepeated) {
  2369. GPBInt64Array *array =
  2370. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2371. if (field.isPackable) {
  2372. int32_t length = GPBCodedInputStreamReadInt32(state);
  2373. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2374. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2375. int64_t val = GPBCodedInputStreamReadSInt64(state);
  2376. [array addValue:val];
  2377. }
  2378. GPBCodedInputStreamPopLimit(state, limit);
  2379. } else {
  2380. int64_t val = GPBCodedInputStreamReadSInt64(state);
  2381. [array addValue:val];
  2382. }
  2383. } else if (fieldType == GPBFieldTypeSingle) {
  2384. int64_t val = GPBCodedInputStreamReadSInt64(state);
  2385. GPBSetInt64IvarWithFieldInternal(context->result, field, val,
  2386. context->syntax);
  2387. } else { // fieldType == GPBFieldTypeMap
  2388. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2389. GPBInt32Int32Dictionary *map =
  2390. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2391. [context->stream readMapEntry:map
  2392. extensionRegistry:nil
  2393. field:field
  2394. parentMessage:context->result];
  2395. }
  2396. return NO;
  2397. }
  2398. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(UInt64, uint64_t, UInt64)
  2399. // This block of code is generated, do not edit it directly.
  2400. static BOOL DynamicMergeFromCodedInputStreamUInt64(GPBFieldDescriptor *field,
  2401. void *voidContext) {
  2402. MergeFromCodedInputStreamContext *context =
  2403. (MergeFromCodedInputStreamContext *)voidContext;
  2404. GPBCodedInputStreamState *state = &context->stream->state_;
  2405. GPBFieldType fieldType = field.fieldType;
  2406. if (fieldType == GPBFieldTypeRepeated) {
  2407. GPBUInt64Array *array =
  2408. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2409. if (field.isPackable) {
  2410. int32_t length = GPBCodedInputStreamReadInt32(state);
  2411. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2412. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2413. uint64_t val = GPBCodedInputStreamReadUInt64(state);
  2414. [array addValue:val];
  2415. }
  2416. GPBCodedInputStreamPopLimit(state, limit);
  2417. } else {
  2418. uint64_t val = GPBCodedInputStreamReadUInt64(state);
  2419. [array addValue:val];
  2420. }
  2421. } else if (fieldType == GPBFieldTypeSingle) {
  2422. uint64_t val = GPBCodedInputStreamReadUInt64(state);
  2423. GPBSetUInt64IvarWithFieldInternal(context->result, field, val,
  2424. context->syntax);
  2425. } else { // fieldType == GPBFieldTypeMap
  2426. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2427. GPBInt32Int32Dictionary *map =
  2428. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2429. [context->stream readMapEntry:map
  2430. extensionRegistry:nil
  2431. field:field
  2432. parentMessage:context->result];
  2433. }
  2434. return NO;
  2435. }
  2436. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Fixed64, uint64_t, UInt64)
  2437. // This block of code is generated, do not edit it directly.
  2438. static BOOL DynamicMergeFromCodedInputStreamFixed64(GPBFieldDescriptor *field,
  2439. void *voidContext) {
  2440. MergeFromCodedInputStreamContext *context =
  2441. (MergeFromCodedInputStreamContext *)voidContext;
  2442. GPBCodedInputStreamState *state = &context->stream->state_;
  2443. GPBFieldType fieldType = field.fieldType;
  2444. if (fieldType == GPBFieldTypeRepeated) {
  2445. GPBUInt64Array *array =
  2446. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2447. if (field.isPackable) {
  2448. int32_t length = GPBCodedInputStreamReadInt32(state);
  2449. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2450. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2451. uint64_t val = GPBCodedInputStreamReadFixed64(state);
  2452. [array addValue:val];
  2453. }
  2454. GPBCodedInputStreamPopLimit(state, limit);
  2455. } else {
  2456. uint64_t val = GPBCodedInputStreamReadFixed64(state);
  2457. [array addValue:val];
  2458. }
  2459. } else if (fieldType == GPBFieldTypeSingle) {
  2460. uint64_t val = GPBCodedInputStreamReadFixed64(state);
  2461. GPBSetUInt64IvarWithFieldInternal(context->result, field, val,
  2462. context->syntax);
  2463. } else { // fieldType == GPBFieldTypeMap
  2464. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2465. GPBInt32Int32Dictionary *map =
  2466. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2467. [context->stream readMapEntry:map
  2468. extensionRegistry:nil
  2469. field:field
  2470. parentMessage:context->result];
  2471. }
  2472. return NO;
  2473. }
  2474. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Float, float, Float)
  2475. // This block of code is generated, do not edit it directly.
  2476. static BOOL DynamicMergeFromCodedInputStreamFloat(GPBFieldDescriptor *field,
  2477. void *voidContext) {
  2478. MergeFromCodedInputStreamContext *context =
  2479. (MergeFromCodedInputStreamContext *)voidContext;
  2480. GPBCodedInputStreamState *state = &context->stream->state_;
  2481. GPBFieldType fieldType = field.fieldType;
  2482. if (fieldType == GPBFieldTypeRepeated) {
  2483. GPBFloatArray *array =
  2484. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2485. if (field.isPackable) {
  2486. int32_t length = GPBCodedInputStreamReadInt32(state);
  2487. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2488. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2489. float val = GPBCodedInputStreamReadFloat(state);
  2490. [array addValue:val];
  2491. }
  2492. GPBCodedInputStreamPopLimit(state, limit);
  2493. } else {
  2494. float val = GPBCodedInputStreamReadFloat(state);
  2495. [array addValue:val];
  2496. }
  2497. } else if (fieldType == GPBFieldTypeSingle) {
  2498. float val = GPBCodedInputStreamReadFloat(state);
  2499. GPBSetFloatIvarWithFieldInternal(context->result, field, val,
  2500. context->syntax);
  2501. } else { // fieldType == GPBFieldTypeMap
  2502. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2503. GPBInt32Int32Dictionary *map =
  2504. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2505. [context->stream readMapEntry:map
  2506. extensionRegistry:nil
  2507. field:field
  2508. parentMessage:context->result];
  2509. }
  2510. return NO;
  2511. }
  2512. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_POD_FUNC(Double, double, Double)
  2513. // This block of code is generated, do not edit it directly.
  2514. static BOOL DynamicMergeFromCodedInputStreamDouble(GPBFieldDescriptor *field,
  2515. void *voidContext) {
  2516. MergeFromCodedInputStreamContext *context =
  2517. (MergeFromCodedInputStreamContext *)voidContext;
  2518. GPBCodedInputStreamState *state = &context->stream->state_;
  2519. GPBFieldType fieldType = field.fieldType;
  2520. if (fieldType == GPBFieldTypeRepeated) {
  2521. GPBDoubleArray *array =
  2522. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2523. if (field.isPackable) {
  2524. int32_t length = GPBCodedInputStreamReadInt32(state);
  2525. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  2526. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  2527. double val = GPBCodedInputStreamReadDouble(state);
  2528. [array addValue:val];
  2529. }
  2530. GPBCodedInputStreamPopLimit(state, limit);
  2531. } else {
  2532. double val = GPBCodedInputStreamReadDouble(state);
  2533. [array addValue:val];
  2534. }
  2535. } else if (fieldType == GPBFieldTypeSingle) {
  2536. double val = GPBCodedInputStreamReadDouble(state);
  2537. GPBSetDoubleIvarWithFieldInternal(context->result, field, val,
  2538. context->syntax);
  2539. } else { // fieldType == GPBFieldTypeMap
  2540. // The exact type doesn't matter, just need to know it is a GPB*Dictionary.
  2541. GPBInt32Int32Dictionary *map =
  2542. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2543. [context->stream readMapEntry:map
  2544. extensionRegistry:nil
  2545. field:field
  2546. parentMessage:context->result];
  2547. }
  2548. return NO;
  2549. }
  2550. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_OBJ_FUNC(String)
  2551. // This block of code is generated, do not edit it directly.
  2552. static BOOL DynamicMergeFromCodedInputStreamString(GPBFieldDescriptor *field,
  2553. void *voidContext) {
  2554. MergeFromCodedInputStreamContext *context = voidContext;
  2555. GPBCodedInputStreamState *state = &context->stream->state_;
  2556. GPBFieldType fieldType = field.fieldType;
  2557. if (fieldType == GPBFieldTypeMap) {
  2558. // GPB*Dictionary or NSDictionary, exact type doesn't matter at this point.
  2559. id map =
  2560. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2561. [context->stream readMapEntry:map
  2562. extensionRegistry:nil
  2563. field:field
  2564. parentMessage:context->result];
  2565. } else {
  2566. id val = GPBCodedInputStreamReadRetainedString(state);
  2567. if (fieldType == GPBFieldTypeRepeated) {
  2568. NSMutableArray *array =
  2569. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2570. [array addObject:val];
  2571. [val release];
  2572. } else { // fieldType == GPBFieldTypeSingle
  2573. GPBSetRetainedObjectIvarWithFieldInternal(context->result, field, val,
  2574. context->syntax);
  2575. }
  2576. }
  2577. return NO;
  2578. }
  2579. //%PDDM-EXPAND MERGE_FROM_CODED_INPUT_STREAM_OBJ_FUNC(Data)
  2580. // This block of code is generated, do not edit it directly.
  2581. static BOOL DynamicMergeFromCodedInputStreamData(GPBFieldDescriptor *field,
  2582. void *voidContext) {
  2583. MergeFromCodedInputStreamContext *context = voidContext;
  2584. GPBCodedInputStreamState *state = &context->stream->state_;
  2585. GPBFieldType fieldType = field.fieldType;
  2586. if (fieldType == GPBFieldTypeMap) {
  2587. // GPB*Dictionary or NSDictionary, exact type doesn't matter at this point.
  2588. id map =
  2589. GetOrCreateMapIvarWithField(context->result, field, context->syntax);
  2590. [context->stream readMapEntry:map
  2591. extensionRegistry:nil
  2592. field:field
  2593. parentMessage:context->result];
  2594. } else {
  2595. id val = GPBCodedInputStreamReadRetainedData(state);
  2596. if (fieldType == GPBFieldTypeRepeated) {
  2597. NSMutableArray *array =
  2598. GetOrCreateArrayIvarWithField(context->result, field, context->syntax);
  2599. [array addObject:val];
  2600. [val release];
  2601. } else { // fieldType == GPBFieldTypeSingle
  2602. GPBSetRetainedObjectIvarWithFieldInternal(context->result, field, val,
  2603. context->syntax);
  2604. }
  2605. }
  2606. return NO;
  2607. }
  2608. //%PDDM-EXPAND-END (15 expansions)
  2609. - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
  2610. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  2611. GPBDescriptor *descriptor = [self descriptor];
  2612. GPBFileSyntax syntax = descriptor.file.syntax;
  2613. MergeFromCodedInputStreamContext context = {
  2614. self, input, self, extensionRegistry, 0, syntax
  2615. };
  2616. GPBApplyStrictFunctions funcs =
  2617. GPBAPPLY_STRICT_FUNCTIONS_INIT(DynamicMergeFromCodedInputStream);
  2618. NSUInteger startingIndex = 0;
  2619. NSArray *fields = descriptor->fields_;
  2620. NSUInteger count = fields.count;
  2621. while (YES) {
  2622. BOOL merged = NO;
  2623. context.tag = GPBCodedInputStreamReadTag(&input->state_);
  2624. for (NSUInteger i = 0; i < count; ++i) {
  2625. if (startingIndex >= count) startingIndex = 0;
  2626. GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
  2627. if (GPBFieldTag(fieldDescriptor) == context.tag) {
  2628. GPBApplyFunction function = funcs[GPBGetFieldType(fieldDescriptor)];
  2629. function(fieldDescriptor, &context);
  2630. merged = YES;
  2631. break;
  2632. } else {
  2633. startingIndex += 1;
  2634. }
  2635. }
  2636. if (!merged) {
  2637. if (context.tag == 0) {
  2638. // zero signals EOF / limit reached
  2639. return;
  2640. } else {
  2641. if (GPBPreserveUnknownFields(syntax)) {
  2642. if (![self parseUnknownField:input
  2643. extensionRegistry:extensionRegistry
  2644. tag:context.tag]) {
  2645. // it's an endgroup tag
  2646. return;
  2647. }
  2648. } else {
  2649. if (![input skipField:context.tag]) {
  2650. return;
  2651. }
  2652. }
  2653. }
  2654. }
  2655. }
  2656. }
  2657. #pragma mark - MergeFrom Support
  2658. typedef struct MergeFromContext {
  2659. GPBMessage *other;
  2660. GPBMessage *result;
  2661. GPBFileSyntax syntax;
  2662. } MergeFromContext;
  2663. //%PDDM-DEFINE GPB_MERGE_FROM_FUNC(NAME)
  2664. //%static BOOL MergeFrom##NAME(GPBFieldDescriptor *field, void *voidContext) {
  2665. //% MergeFromContext *context = (MergeFromContext *)voidContext;
  2666. //% BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2667. //% if (otherHas) {
  2668. //% GPBSet##NAME##IvarWithFieldInternal(
  2669. //% context->result, field, GPBGet##NAME##IvarWithField(context->other, field),
  2670. //% context->syntax);
  2671. //% }
  2672. //% return YES;
  2673. //%}
  2674. //%
  2675. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(Bool)
  2676. // This block of code is generated, do not edit it directly.
  2677. static BOOL MergeFromBool(GPBFieldDescriptor *field, void *voidContext) {
  2678. MergeFromContext *context = (MergeFromContext *)voidContext;
  2679. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2680. if (otherHas) {
  2681. GPBSetBoolIvarWithFieldInternal(
  2682. context->result, field, GPBGetBoolIvarWithField(context->other, field),
  2683. context->syntax);
  2684. }
  2685. return YES;
  2686. }
  2687. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(Int32)
  2688. // This block of code is generated, do not edit it directly.
  2689. static BOOL MergeFromInt32(GPBFieldDescriptor *field, void *voidContext) {
  2690. MergeFromContext *context = (MergeFromContext *)voidContext;
  2691. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2692. if (otherHas) {
  2693. GPBSetInt32IvarWithFieldInternal(
  2694. context->result, field, GPBGetInt32IvarWithField(context->other, field),
  2695. context->syntax);
  2696. }
  2697. return YES;
  2698. }
  2699. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(UInt32)
  2700. // This block of code is generated, do not edit it directly.
  2701. static BOOL MergeFromUInt32(GPBFieldDescriptor *field, void *voidContext) {
  2702. MergeFromContext *context = (MergeFromContext *)voidContext;
  2703. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2704. if (otherHas) {
  2705. GPBSetUInt32IvarWithFieldInternal(
  2706. context->result, field, GPBGetUInt32IvarWithField(context->other, field),
  2707. context->syntax);
  2708. }
  2709. return YES;
  2710. }
  2711. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(Int64)
  2712. // This block of code is generated, do not edit it directly.
  2713. static BOOL MergeFromInt64(GPBFieldDescriptor *field, void *voidContext) {
  2714. MergeFromContext *context = (MergeFromContext *)voidContext;
  2715. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2716. if (otherHas) {
  2717. GPBSetInt64IvarWithFieldInternal(
  2718. context->result, field, GPBGetInt64IvarWithField(context->other, field),
  2719. context->syntax);
  2720. }
  2721. return YES;
  2722. }
  2723. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(UInt64)
  2724. // This block of code is generated, do not edit it directly.
  2725. static BOOL MergeFromUInt64(GPBFieldDescriptor *field, void *voidContext) {
  2726. MergeFromContext *context = (MergeFromContext *)voidContext;
  2727. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2728. if (otherHas) {
  2729. GPBSetUInt64IvarWithFieldInternal(
  2730. context->result, field, GPBGetUInt64IvarWithField(context->other, field),
  2731. context->syntax);
  2732. }
  2733. return YES;
  2734. }
  2735. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(Float)
  2736. // This block of code is generated, do not edit it directly.
  2737. static BOOL MergeFromFloat(GPBFieldDescriptor *field, void *voidContext) {
  2738. MergeFromContext *context = (MergeFromContext *)voidContext;
  2739. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2740. if (otherHas) {
  2741. GPBSetFloatIvarWithFieldInternal(
  2742. context->result, field, GPBGetFloatIvarWithField(context->other, field),
  2743. context->syntax);
  2744. }
  2745. return YES;
  2746. }
  2747. //%PDDM-EXPAND GPB_MERGE_FROM_FUNC(Double)
  2748. // This block of code is generated, do not edit it directly.
  2749. static BOOL MergeFromDouble(GPBFieldDescriptor *field, void *voidContext) {
  2750. MergeFromContext *context = (MergeFromContext *)voidContext;
  2751. BOOL otherHas = GPBGetHasIvarField(context->other, field);
  2752. if (otherHas) {
  2753. GPBSetDoubleIvarWithFieldInternal(
  2754. context->result, field, GPBGetDoubleIvarWithField(context->other, field),
  2755. context->syntax);
  2756. }
  2757. return YES;
  2758. }
  2759. //%PDDM-EXPAND-END (7 expansions)
  2760. static BOOL MergeFromObject(GPBFieldDescriptor *field, void *voidContext) {
  2761. MergeFromContext *context = (MergeFromContext *)voidContext;
  2762. GPBFieldType fieldType = field.fieldType;
  2763. if (fieldType == GPBFieldTypeRepeated) {
  2764. // In the case of a list, they need to be appended, and there is no
  2765. // _hasIvar to worry about setting.
  2766. id otherArray =
  2767. GPBGetObjectIvarWithFieldNoAutocreate(context->other, field);
  2768. if (otherArray) {
  2769. GPBType fieldDataType = field->description_->type;
  2770. if (GPBTypeIsObject(fieldDataType)) {
  2771. NSMutableArray *resultArray = GetOrCreateArrayIvarWithField(
  2772. context->result, field, context->syntax);
  2773. [resultArray addObjectsFromArray:otherArray];
  2774. } else if (fieldDataType == GPBTypeEnum) {
  2775. GPBEnumArray *resultArray = GetOrCreateArrayIvarWithField(
  2776. context->result, field, context->syntax);
  2777. [resultArray addRawValuesFromArray:otherArray];
  2778. } else {
  2779. // The array type doesn't matter, that all implment
  2780. // -addValuesFromArray:.
  2781. GPBInt32Array *resultArray = GetOrCreateArrayIvarWithField(
  2782. context->result, field, context->syntax);
  2783. [resultArray addValuesFromArray:otherArray];
  2784. }
  2785. }
  2786. return YES;
  2787. }
  2788. if (fieldType == GPBFieldTypeMap) {
  2789. // In the case of a map, they need to be merged, and there is no
  2790. // _hasIvar to worry about setting.
  2791. id otherDict = GPBGetObjectIvarWithFieldNoAutocreate(context->other, field);
  2792. if (otherDict) {
  2793. GPBType keyType = field.mapKeyType;
  2794. GPBType valueType = field->description_->type;
  2795. if (GPBTypeIsObject(keyType) && GPBTypeIsObject(valueType)) {
  2796. NSMutableDictionary *resultDict = GetOrCreateMapIvarWithField(
  2797. context->result, field, context->syntax);
  2798. [resultDict addEntriesFromDictionary:otherDict];
  2799. } else if (valueType == GPBTypeEnum) {
  2800. // The exact type doesn't matter, just need to know it is a
  2801. // GPB*EnumDictionary.
  2802. GPBInt32EnumDictionary *resultDict = GetOrCreateMapIvarWithField(
  2803. context->result, field, context->syntax);
  2804. [resultDict addRawEntriesFromDictionary:otherDict];
  2805. } else {
  2806. // The exact type doesn't matter, they all implement
  2807. // -addEntriesFromDictionary:.
  2808. GPBInt32Int32Dictionary *resultDict = GetOrCreateMapIvarWithField(
  2809. context->result, field, context->syntax);
  2810. [resultDict addEntriesFromDictionary:otherDict];
  2811. }
  2812. }
  2813. return YES;
  2814. }
  2815. int32_t hasIndex = GPBFieldHasIndex(field);
  2816. uint32_t fieldNumber = GPBFieldNumber(field);
  2817. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNumber);
  2818. if (!otherHas) {
  2819. return YES;
  2820. }
  2821. // GPBGetObjectIvarWithFieldNoAutocreate skips the has check, faster.
  2822. id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(context->other, field);
  2823. if (GPBFieldTypeIsMessage(field)) {
  2824. if (GPBGetHasIvar(context->result, hasIndex, fieldNumber)) {
  2825. GPBMessage *message =
  2826. GPBGetObjectIvarWithFieldNoAutocreate(context->result, field);
  2827. [message mergeFrom:otherVal];
  2828. } else {
  2829. GPBMessage *message = [otherVal copy];
  2830. GPBSetRetainedObjectIvarWithFieldInternal(context->result, field, message,
  2831. context->syntax);
  2832. }
  2833. } else {
  2834. GPBSetObjectIvarWithFieldInternal(context->result, field, otherVal,
  2835. context->syntax);
  2836. }
  2837. return YES;
  2838. }
  2839. - (void)mergeFrom:(GPBMessage *)other {
  2840. Class selfClass = [self class];
  2841. Class otherClass = [other class];
  2842. if (!([selfClass isSubclassOfClass:otherClass] ||
  2843. [otherClass isSubclassOfClass:selfClass])) {
  2844. [NSException raise:NSInvalidArgumentException
  2845. format:@"Classes must match %@ != %@", selfClass, otherClass];
  2846. }
  2847. GPBApplyFunctions funcs = GPBAPPLY_FUNCTIONS_INIT(MergeFrom);
  2848. GPBFileSyntax syntax = [self descriptor].file.syntax;
  2849. MergeFromContext context = {other, self, syntax};
  2850. GPBApplyFunctionsToMessageFields(&funcs, self, &context);
  2851. // We assume someting got done, and become visible.
  2852. GPBBecomeVisibleToAutocreator(self);
  2853. // Unknown fields.
  2854. if (!unknownFields_) {
  2855. [self setUnknownFields:context.other.unknownFields];
  2856. } else {
  2857. [unknownFields_ mergeUnknownFields:context.other.unknownFields];
  2858. }
  2859. if (other->extensionMap_.count == 0) {
  2860. return;
  2861. }
  2862. if (extensionMap_ == nil) {
  2863. extensionMap_ =
  2864. CloneExtensionMap(other->extensionMap_, NSZoneFromPointer(self));
  2865. } else {
  2866. for (GPBExtensionField *thisField in other->extensionMap_) {
  2867. id otherValue = [other->extensionMap_ objectForKey:thisField];
  2868. id value = [extensionMap_ objectForKey:thisField];
  2869. GPBExtensionDescriptor *thisFieldDescriptor = thisField.descriptor;
  2870. BOOL isMessageExtension = GPBExtensionIsMessage(thisFieldDescriptor);
  2871. if ([thisField isRepeated]) {
  2872. NSMutableArray *list = value;
  2873. if (list == nil) {
  2874. list = [[NSMutableArray alloc] init];
  2875. [extensionMap_ setObject:list forKey:thisField];
  2876. [list release];
  2877. }
  2878. if (isMessageExtension) {
  2879. for (GPBMessage *otherListValue in otherValue) {
  2880. GPBMessage *copiedValue = [otherListValue copy];
  2881. [list addObject:copiedValue];
  2882. [copiedValue release];
  2883. }
  2884. } else {
  2885. [list addObjectsFromArray:otherValue];
  2886. }
  2887. } else {
  2888. if (isMessageExtension) {
  2889. if (value) {
  2890. [(GPBMessage *)value mergeFrom:(GPBMessage *)otherValue];
  2891. } else {
  2892. GPBMessage *copiedValue = [otherValue copy];
  2893. [extensionMap_ setObject:copiedValue forKey:thisField];
  2894. [copiedValue release];
  2895. }
  2896. } else {
  2897. [extensionMap_ setObject:otherValue forKey:thisField];
  2898. }
  2899. }
  2900. if (isMessageExtension && !thisFieldDescriptor.isRepeated) {
  2901. GPBMessage *autocreatedValue =
  2902. [[autocreatedExtensionMap_ objectForKey:thisField] retain];
  2903. // Must remove from the map before calling GPBClearMessageAutocreator()
  2904. // so that GPBClearMessageAutocreator() knows its safe to clear.
  2905. [autocreatedExtensionMap_ removeObjectForKey:thisField];
  2906. GPBClearMessageAutocreator(autocreatedValue);
  2907. [autocreatedValue release];
  2908. }
  2909. }
  2910. }
  2911. }
  2912. #pragma mark - IsEqual Support
  2913. typedef struct IsEqualContext {
  2914. GPBMessage *other;
  2915. GPBMessage *self;
  2916. BOOL outIsEqual;
  2917. } IsEqualContext;
  2918. // If both self and other "has" a value then compare.
  2919. //%PDDM-DEFINE IS_EQUAL_FUNC(NAME, TYPE)
  2920. //%static BOOL IsEqual##NAME(GPBFieldDescriptor *field, void *voidContext) {
  2921. //% IsEqualContext *context = (IsEqualContext *)voidContext;
  2922. //% int32_t hasIndex = GPBFieldHasIndex(field);
  2923. //% uint32_t fieldNum = GPBFieldNumber(field);
  2924. //% BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  2925. //% BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  2926. //% if (selfHas != otherHas) {
  2927. //% context->outIsEqual = NO;
  2928. //% return NO;
  2929. //% }
  2930. //% if (!selfHas) {
  2931. //% return YES;
  2932. //% }
  2933. //% TYPE selfVal = GPBGet##NAME##IvarWithField(context->self, field);
  2934. //% TYPE otherVal = GPBGet##NAME##IvarWithField(context->other, field);
  2935. //% if (selfVal != otherVal) {
  2936. //% context->outIsEqual = NO;
  2937. //% return NO;
  2938. //% }
  2939. //% return YES;
  2940. //%}
  2941. //%
  2942. //%PDDM-EXPAND IS_EQUAL_FUNC(Bool, BOOL)
  2943. // This block of code is generated, do not edit it directly.
  2944. static BOOL IsEqualBool(GPBFieldDescriptor *field, void *voidContext) {
  2945. IsEqualContext *context = (IsEqualContext *)voidContext;
  2946. int32_t hasIndex = GPBFieldHasIndex(field);
  2947. uint32_t fieldNum = GPBFieldNumber(field);
  2948. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  2949. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  2950. if (selfHas != otherHas) {
  2951. context->outIsEqual = NO;
  2952. return NO;
  2953. }
  2954. if (!selfHas) {
  2955. return YES;
  2956. }
  2957. BOOL selfVal = GPBGetBoolIvarWithField(context->self, field);
  2958. BOOL otherVal = GPBGetBoolIvarWithField(context->other, field);
  2959. if (selfVal != otherVal) {
  2960. context->outIsEqual = NO;
  2961. return NO;
  2962. }
  2963. return YES;
  2964. }
  2965. //%PDDM-EXPAND IS_EQUAL_FUNC(Int32, int32_t)
  2966. // This block of code is generated, do not edit it directly.
  2967. static BOOL IsEqualInt32(GPBFieldDescriptor *field, void *voidContext) {
  2968. IsEqualContext *context = (IsEqualContext *)voidContext;
  2969. int32_t hasIndex = GPBFieldHasIndex(field);
  2970. uint32_t fieldNum = GPBFieldNumber(field);
  2971. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  2972. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  2973. if (selfHas != otherHas) {
  2974. context->outIsEqual = NO;
  2975. return NO;
  2976. }
  2977. if (!selfHas) {
  2978. return YES;
  2979. }
  2980. int32_t selfVal = GPBGetInt32IvarWithField(context->self, field);
  2981. int32_t otherVal = GPBGetInt32IvarWithField(context->other, field);
  2982. if (selfVal != otherVal) {
  2983. context->outIsEqual = NO;
  2984. return NO;
  2985. }
  2986. return YES;
  2987. }
  2988. //%PDDM-EXPAND IS_EQUAL_FUNC(UInt32, uint32_t)
  2989. // This block of code is generated, do not edit it directly.
  2990. static BOOL IsEqualUInt32(GPBFieldDescriptor *field, void *voidContext) {
  2991. IsEqualContext *context = (IsEqualContext *)voidContext;
  2992. int32_t hasIndex = GPBFieldHasIndex(field);
  2993. uint32_t fieldNum = GPBFieldNumber(field);
  2994. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  2995. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  2996. if (selfHas != otherHas) {
  2997. context->outIsEqual = NO;
  2998. return NO;
  2999. }
  3000. if (!selfHas) {
  3001. return YES;
  3002. }
  3003. uint32_t selfVal = GPBGetUInt32IvarWithField(context->self, field);
  3004. uint32_t otherVal = GPBGetUInt32IvarWithField(context->other, field);
  3005. if (selfVal != otherVal) {
  3006. context->outIsEqual = NO;
  3007. return NO;
  3008. }
  3009. return YES;
  3010. }
  3011. //%PDDM-EXPAND IS_EQUAL_FUNC(Int64, int64_t)
  3012. // This block of code is generated, do not edit it directly.
  3013. static BOOL IsEqualInt64(GPBFieldDescriptor *field, void *voidContext) {
  3014. IsEqualContext *context = (IsEqualContext *)voidContext;
  3015. int32_t hasIndex = GPBFieldHasIndex(field);
  3016. uint32_t fieldNum = GPBFieldNumber(field);
  3017. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  3018. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  3019. if (selfHas != otherHas) {
  3020. context->outIsEqual = NO;
  3021. return NO;
  3022. }
  3023. if (!selfHas) {
  3024. return YES;
  3025. }
  3026. int64_t selfVal = GPBGetInt64IvarWithField(context->self, field);
  3027. int64_t otherVal = GPBGetInt64IvarWithField(context->other, field);
  3028. if (selfVal != otherVal) {
  3029. context->outIsEqual = NO;
  3030. return NO;
  3031. }
  3032. return YES;
  3033. }
  3034. //%PDDM-EXPAND IS_EQUAL_FUNC(UInt64, uint64_t)
  3035. // This block of code is generated, do not edit it directly.
  3036. static BOOL IsEqualUInt64(GPBFieldDescriptor *field, void *voidContext) {
  3037. IsEqualContext *context = (IsEqualContext *)voidContext;
  3038. int32_t hasIndex = GPBFieldHasIndex(field);
  3039. uint32_t fieldNum = GPBFieldNumber(field);
  3040. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  3041. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  3042. if (selfHas != otherHas) {
  3043. context->outIsEqual = NO;
  3044. return NO;
  3045. }
  3046. if (!selfHas) {
  3047. return YES;
  3048. }
  3049. uint64_t selfVal = GPBGetUInt64IvarWithField(context->self, field);
  3050. uint64_t otherVal = GPBGetUInt64IvarWithField(context->other, field);
  3051. if (selfVal != otherVal) {
  3052. context->outIsEqual = NO;
  3053. return NO;
  3054. }
  3055. return YES;
  3056. }
  3057. //%PDDM-EXPAND IS_EQUAL_FUNC(Float, float)
  3058. // This block of code is generated, do not edit it directly.
  3059. static BOOL IsEqualFloat(GPBFieldDescriptor *field, void *voidContext) {
  3060. IsEqualContext *context = (IsEqualContext *)voidContext;
  3061. int32_t hasIndex = GPBFieldHasIndex(field);
  3062. uint32_t fieldNum = GPBFieldNumber(field);
  3063. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  3064. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  3065. if (selfHas != otherHas) {
  3066. context->outIsEqual = NO;
  3067. return NO;
  3068. }
  3069. if (!selfHas) {
  3070. return YES;
  3071. }
  3072. float selfVal = GPBGetFloatIvarWithField(context->self, field);
  3073. float otherVal = GPBGetFloatIvarWithField(context->other, field);
  3074. if (selfVal != otherVal) {
  3075. context->outIsEqual = NO;
  3076. return NO;
  3077. }
  3078. return YES;
  3079. }
  3080. //%PDDM-EXPAND IS_EQUAL_FUNC(Double, double)
  3081. // This block of code is generated, do not edit it directly.
  3082. static BOOL IsEqualDouble(GPBFieldDescriptor *field, void *voidContext) {
  3083. IsEqualContext *context = (IsEqualContext *)voidContext;
  3084. int32_t hasIndex = GPBFieldHasIndex(field);
  3085. uint32_t fieldNum = GPBFieldNumber(field);
  3086. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  3087. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  3088. if (selfHas != otherHas) {
  3089. context->outIsEqual = NO;
  3090. return NO;
  3091. }
  3092. if (!selfHas) {
  3093. return YES;
  3094. }
  3095. double selfVal = GPBGetDoubleIvarWithField(context->self, field);
  3096. double otherVal = GPBGetDoubleIvarWithField(context->other, field);
  3097. if (selfVal != otherVal) {
  3098. context->outIsEqual = NO;
  3099. return NO;
  3100. }
  3101. return YES;
  3102. }
  3103. //%PDDM-EXPAND-END (7 expansions)
  3104. static BOOL IsEqualObject(GPBFieldDescriptor *field, void *voidContext) {
  3105. IsEqualContext *context = (IsEqualContext *)voidContext;
  3106. if (GPBFieldIsMapOrArray(field)) {
  3107. // In the case of a list/map, there is no _hasIvar to worry about checking.
  3108. // NOTE: These are NSArray/GPB*Array/NSDictionary/GPB*Dictionary, but the
  3109. // type doesn't really matter as the object just has to support
  3110. // -count/-isEqual:.
  3111. NSArray *resultMapOrArray =
  3112. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3113. NSArray *otherMapOrArray =
  3114. GPBGetObjectIvarWithFieldNoAutocreate(context->other, field);
  3115. context->outIsEqual =
  3116. (resultMapOrArray.count == 0 && otherMapOrArray.count == 0) ||
  3117. [resultMapOrArray isEqual:otherMapOrArray];
  3118. return context->outIsEqual;
  3119. }
  3120. int32_t hasIndex = GPBFieldHasIndex(field);
  3121. uint32_t fieldNum = GPBFieldNumber(field);
  3122. BOOL selfHas = GPBGetHasIvar(context->self, hasIndex, fieldNum);
  3123. BOOL otherHas = GPBGetHasIvar(context->other, hasIndex, fieldNum);
  3124. if (selfHas != otherHas) {
  3125. context->outIsEqual = NO;
  3126. return NO;
  3127. }
  3128. if (!selfHas) {
  3129. return YES;
  3130. }
  3131. // GPBGetObjectIvarWithFieldNoAutocreate skips the has check, faster.
  3132. NSObject *selfVal =
  3133. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3134. NSObject *otherVal =
  3135. GPBGetObjectIvarWithFieldNoAutocreate(context->other, field);
  3136. // This covers case where selfVal is set to nil, as well as shortcuts the call
  3137. // to isEqual: in common cases.
  3138. if (selfVal == otherVal) {
  3139. return YES;
  3140. }
  3141. if (![selfVal isEqual:otherVal]) {
  3142. context->outIsEqual = NO;
  3143. return NO;
  3144. }
  3145. return YES;
  3146. }
  3147. - (BOOL)isEqual:(GPBMessage *)other {
  3148. if (other == self) {
  3149. return YES;
  3150. }
  3151. if (![other isKindOfClass:[self class]] &&
  3152. ![self isKindOfClass:[other class]]) {
  3153. return NO;
  3154. }
  3155. GPBApplyFunctions funcs = GPBAPPLY_FUNCTIONS_INIT(IsEqual);
  3156. IsEqualContext context = {other, self, YES};
  3157. GPBApplyFunctionsToMessageFields(&funcs, self, &context);
  3158. if (!context.outIsEqual) {
  3159. return NO;
  3160. }
  3161. // nil and empty are equal
  3162. if (extensionMap_.count != 0 || other->extensionMap_.count != 0) {
  3163. if (![extensionMap_ isEqual:other->extensionMap_]) {
  3164. return NO;
  3165. }
  3166. }
  3167. // nil and empty are equal
  3168. GPBUnknownFieldSet *otherUnknowns = other.unknownFields;
  3169. if ([unknownFields_ countOfFields] != 0 ||
  3170. [otherUnknowns countOfFields] != 0) {
  3171. if (![unknownFields_ isEqual:otherUnknowns]) {
  3172. return NO;
  3173. }
  3174. }
  3175. return YES;
  3176. }
  3177. // It is very difficult to implement a generic hash for ProtoBuf messages that
  3178. // will perform well. If you need hashing on your ProtoBufs (eg you are using
  3179. // them as dictionary keys) you will probably want to implement a ProtoBuf
  3180. // message specific hash as a category on your protobuf class. Do not make it a
  3181. // category on GPBMessage as you will conflict with this hash, and will possibly
  3182. // override hash for all generated protobufs. A good implementation of hash will
  3183. // be really fast, so we would recommend only hashing protobufs that have an
  3184. // identifier field of some kind that you can easily hash. If you implement
  3185. // hash, we would strongly recommend overriding isEqual: in your category as
  3186. // well, as the default implementation of isEqual: is extremely slow, and may
  3187. // drastically affect performance in large sets.
  3188. - (NSUInteger)hash {
  3189. GPBDescriptor *descriptor = [[self class] descriptor];
  3190. const NSUInteger prime = 19;
  3191. // Start with the descriptor and then mix it with the field numbers that
  3192. // are set. Hopefully that will give a spread based on classes and what
  3193. // fields are set.
  3194. NSUInteger result = (NSUInteger)descriptor;
  3195. for (GPBFieldDescriptor *field in descriptor->fields_) {
  3196. if (GPBFieldIsMapOrArray(field)) {
  3197. // Exact type doesn't matter, just check if there are any elements.
  3198. NSArray *mapOrArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  3199. if (mapOrArray.count) {
  3200. result = prime * result + GPBFieldNumber(field);
  3201. }
  3202. } else if (GPBGetHasIvarField(self, field)) {
  3203. result = prime * result + GPBFieldNumber(field);
  3204. }
  3205. }
  3206. return result;
  3207. }
  3208. #pragma mark - Description Support
  3209. - (NSString *)description {
  3210. NSString *textFormat = GPBTextFormatForMessage(self, @" ");
  3211. NSString *description = [NSString
  3212. stringWithFormat:@"<%@ %p>: {\n%@}", [self class], self, textFormat];
  3213. return description;
  3214. }
  3215. #if DEBUG
  3216. // Xcode 5.1 added support for custom quick look info.
  3217. // https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/CustomClassDisplay_in_QuickLook/CH01-quick_look_for_custom_objects/CH01-quick_look_for_custom_objects.html#//apple_ref/doc/uid/TP40014001-CH2-SW1
  3218. - (id)debugQuickLookObject {
  3219. return GPBTextFormatForMessage(self, nil);
  3220. }
  3221. #endif // DEBUG
  3222. #pragma mark - SerializedSize Support
  3223. // Serialized size is only calculated once, and then is stored into
  3224. // memoizedSerializedSize.
  3225. typedef struct SerializedSizeContext {
  3226. GPBMessage *self;
  3227. size_t outSize;
  3228. } SerializedSizeContext;
  3229. //%PDDM-DEFINE SERIALIZED_SIZE_POD_FUNC(NAME, TYPE, REAL_TYPE)
  3230. //%SERIALIZED_SIZE_POD_FUNC_FULL(NAME, TYPE, REAL_TYPE, REAL_TYPE, )
  3231. //%PDDM-DEFINE SERIALIZED_SIZE_POD_FUNC_FULL(NAME, TYPE, REAL_TYPE, ARRAY_TYPE, ARRAY_ACCESSOR_NAME)
  3232. //%static BOOL DynamicSerializedSize##NAME(GPBFieldDescriptor *field,
  3233. //% NAME$S void *voidContext) {
  3234. //% SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3235. //% GPBFieldType fieldType = field.fieldType;
  3236. //% if (fieldType == GPBFieldTypeRepeated) {
  3237. //% GPB##ARRAY_TYPE##Array *array =
  3238. //% GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3239. //% NSUInteger count = array.count;
  3240. //% if (count == 0) return YES;
  3241. //% __block size_t dataSize = 0;
  3242. //% [array enumerate##ARRAY_ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
  3243. //% #pragma unused(idx, stop)
  3244. //% dataSize += GPBCompute##NAME##SizeNoTag(value);
  3245. //% }];
  3246. //% context->outSize += dataSize;
  3247. //% size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3248. //% if (field.isPackable) {
  3249. //% context->outSize += tagSize;
  3250. //% context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3251. //% } else {
  3252. //% context->outSize += count * tagSize;
  3253. //% }
  3254. //% } else if (fieldType == GPBFieldTypeSingle) {
  3255. //% BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3256. //% if (selfHas) {
  3257. //% TYPE selfVal = GPBGet##REAL_TYPE##IvarWithField(context->self, field);
  3258. //% context->outSize += GPBCompute##NAME##Size(GPBFieldNumber(field), selfVal);
  3259. //% }
  3260. //% } else { // fieldType == GPBFieldTypeMap
  3261. //% // Type will be GPB*##REAL_TYPE##Dictionary, exact type doesn't matter.
  3262. //% GPBInt32##REAL_TYPE##Dictionary *map =
  3263. //% GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3264. //% context->outSize += [map computeSerializedSizeAsField:field];
  3265. //% }
  3266. //% return YES;
  3267. //%}
  3268. //%
  3269. //%PDDM-DEFINE SERIALIZED_SIZE_OBJECT_FUNC(NAME)
  3270. //%static BOOL DynamicSerializedSize##NAME(GPBFieldDescriptor *field,
  3271. //% NAME$S void *voidContext) {
  3272. //% SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3273. //% GPBFieldType fieldType = field.fieldType;
  3274. //% if (fieldType == GPBFieldTypeRepeated) {
  3275. //% NSArray *array =
  3276. //% GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3277. //% for (id value in array) {
  3278. //% context->outSize += GPBCompute##NAME##SizeNoTag(value);
  3279. //% }
  3280. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field),
  3281. //% GPBGetFieldType(field));
  3282. //% context->outSize += array.count * tagSize;
  3283. //% } else if (fieldType == GPBFieldTypeSingle) {
  3284. //% BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3285. //% if (selfHas) {
  3286. //% // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  3287. //% // again.
  3288. //% id selfVal = GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3289. //% context->outSize += GPBCompute##NAME##Size(GPBFieldNumber(field), selfVal);
  3290. //% }
  3291. //% } else { // fieldType == GPBFieldTypeMap
  3292. //% GPBType mapKeyType = field.mapKeyType;
  3293. //% if (mapKeyType == GPBTypeString) {
  3294. //% // If key type was string, then the map is an NSDictionary.
  3295. //% NSDictionary *map =
  3296. //% GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3297. //% context->outSize += GPBDictionaryComputeSizeInternalHelper(map, field);
  3298. //% } else {
  3299. //% // Type will be GPB*##NAME##Dictionary, exact type doesn't matter.
  3300. //% GPBInt32ObjectDictionary *map =
  3301. //% GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3302. //% context->outSize += [map computeSerializedSizeAsField:field];
  3303. //% }
  3304. //% }
  3305. //% return YES;
  3306. //%}
  3307. //%
  3308. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Bool, BOOL, Bool)
  3309. // This block of code is generated, do not edit it directly.
  3310. static BOOL DynamicSerializedSizeBool(GPBFieldDescriptor *field,
  3311. void *voidContext) {
  3312. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3313. GPBFieldType fieldType = field.fieldType;
  3314. if (fieldType == GPBFieldTypeRepeated) {
  3315. GPBBoolArray *array =
  3316. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3317. NSUInteger count = array.count;
  3318. if (count == 0) return YES;
  3319. __block size_t dataSize = 0;
  3320. [array enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
  3321. #pragma unused(idx, stop)
  3322. dataSize += GPBComputeBoolSizeNoTag(value);
  3323. }];
  3324. context->outSize += dataSize;
  3325. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3326. if (field.isPackable) {
  3327. context->outSize += tagSize;
  3328. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3329. } else {
  3330. context->outSize += count * tagSize;
  3331. }
  3332. } else if (fieldType == GPBFieldTypeSingle) {
  3333. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3334. if (selfHas) {
  3335. BOOL selfVal = GPBGetBoolIvarWithField(context->self, field);
  3336. context->outSize += GPBComputeBoolSize(GPBFieldNumber(field), selfVal);
  3337. }
  3338. } else { // fieldType == GPBFieldTypeMap
  3339. // Type will be GPB*BoolDictionary, exact type doesn't matter.
  3340. GPBInt32BoolDictionary *map =
  3341. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3342. context->outSize += [map computeSerializedSizeAsField:field];
  3343. }
  3344. return YES;
  3345. }
  3346. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Int32, int32_t, Int32)
  3347. // This block of code is generated, do not edit it directly.
  3348. static BOOL DynamicSerializedSizeInt32(GPBFieldDescriptor *field,
  3349. void *voidContext) {
  3350. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3351. GPBFieldType fieldType = field.fieldType;
  3352. if (fieldType == GPBFieldTypeRepeated) {
  3353. GPBInt32Array *array =
  3354. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3355. NSUInteger count = array.count;
  3356. if (count == 0) return YES;
  3357. __block size_t dataSize = 0;
  3358. [array enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  3359. #pragma unused(idx, stop)
  3360. dataSize += GPBComputeInt32SizeNoTag(value);
  3361. }];
  3362. context->outSize += dataSize;
  3363. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3364. if (field.isPackable) {
  3365. context->outSize += tagSize;
  3366. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3367. } else {
  3368. context->outSize += count * tagSize;
  3369. }
  3370. } else if (fieldType == GPBFieldTypeSingle) {
  3371. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3372. if (selfHas) {
  3373. int32_t selfVal = GPBGetInt32IvarWithField(context->self, field);
  3374. context->outSize += GPBComputeInt32Size(GPBFieldNumber(field), selfVal);
  3375. }
  3376. } else { // fieldType == GPBFieldTypeMap
  3377. // Type will be GPB*Int32Dictionary, exact type doesn't matter.
  3378. GPBInt32Int32Dictionary *map =
  3379. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3380. context->outSize += [map computeSerializedSizeAsField:field];
  3381. }
  3382. return YES;
  3383. }
  3384. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(SInt32, int32_t, Int32)
  3385. // This block of code is generated, do not edit it directly.
  3386. static BOOL DynamicSerializedSizeSInt32(GPBFieldDescriptor *field,
  3387. void *voidContext) {
  3388. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3389. GPBFieldType fieldType = field.fieldType;
  3390. if (fieldType == GPBFieldTypeRepeated) {
  3391. GPBInt32Array *array =
  3392. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3393. NSUInteger count = array.count;
  3394. if (count == 0) return YES;
  3395. __block size_t dataSize = 0;
  3396. [array enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  3397. #pragma unused(idx, stop)
  3398. dataSize += GPBComputeSInt32SizeNoTag(value);
  3399. }];
  3400. context->outSize += dataSize;
  3401. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3402. if (field.isPackable) {
  3403. context->outSize += tagSize;
  3404. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3405. } else {
  3406. context->outSize += count * tagSize;
  3407. }
  3408. } else if (fieldType == GPBFieldTypeSingle) {
  3409. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3410. if (selfHas) {
  3411. int32_t selfVal = GPBGetInt32IvarWithField(context->self, field);
  3412. context->outSize += GPBComputeSInt32Size(GPBFieldNumber(field), selfVal);
  3413. }
  3414. } else { // fieldType == GPBFieldTypeMap
  3415. // Type will be GPB*Int32Dictionary, exact type doesn't matter.
  3416. GPBInt32Int32Dictionary *map =
  3417. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3418. context->outSize += [map computeSerializedSizeAsField:field];
  3419. }
  3420. return YES;
  3421. }
  3422. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(SFixed32, int32_t, Int32)
  3423. // This block of code is generated, do not edit it directly.
  3424. static BOOL DynamicSerializedSizeSFixed32(GPBFieldDescriptor *field,
  3425. void *voidContext) {
  3426. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3427. GPBFieldType fieldType = field.fieldType;
  3428. if (fieldType == GPBFieldTypeRepeated) {
  3429. GPBInt32Array *array =
  3430. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3431. NSUInteger count = array.count;
  3432. if (count == 0) return YES;
  3433. __block size_t dataSize = 0;
  3434. [array enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  3435. #pragma unused(idx, stop)
  3436. dataSize += GPBComputeSFixed32SizeNoTag(value);
  3437. }];
  3438. context->outSize += dataSize;
  3439. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3440. if (field.isPackable) {
  3441. context->outSize += tagSize;
  3442. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3443. } else {
  3444. context->outSize += count * tagSize;
  3445. }
  3446. } else if (fieldType == GPBFieldTypeSingle) {
  3447. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3448. if (selfHas) {
  3449. int32_t selfVal = GPBGetInt32IvarWithField(context->self, field);
  3450. context->outSize += GPBComputeSFixed32Size(GPBFieldNumber(field), selfVal);
  3451. }
  3452. } else { // fieldType == GPBFieldTypeMap
  3453. // Type will be GPB*Int32Dictionary, exact type doesn't matter.
  3454. GPBInt32Int32Dictionary *map =
  3455. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3456. context->outSize += [map computeSerializedSizeAsField:field];
  3457. }
  3458. return YES;
  3459. }
  3460. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC_FULL(Enum, int32_t, Int32, Enum, Raw)
  3461. // This block of code is generated, do not edit it directly.
  3462. static BOOL DynamicSerializedSizeEnum(GPBFieldDescriptor *field,
  3463. void *voidContext) {
  3464. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3465. GPBFieldType fieldType = field.fieldType;
  3466. if (fieldType == GPBFieldTypeRepeated) {
  3467. GPBEnumArray *array =
  3468. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3469. NSUInteger count = array.count;
  3470. if (count == 0) return YES;
  3471. __block size_t dataSize = 0;
  3472. [array enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  3473. #pragma unused(idx, stop)
  3474. dataSize += GPBComputeEnumSizeNoTag(value);
  3475. }];
  3476. context->outSize += dataSize;
  3477. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3478. if (field.isPackable) {
  3479. context->outSize += tagSize;
  3480. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3481. } else {
  3482. context->outSize += count * tagSize;
  3483. }
  3484. } else if (fieldType == GPBFieldTypeSingle) {
  3485. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3486. if (selfHas) {
  3487. int32_t selfVal = GPBGetInt32IvarWithField(context->self, field);
  3488. context->outSize += GPBComputeEnumSize(GPBFieldNumber(field), selfVal);
  3489. }
  3490. } else { // fieldType == GPBFieldTypeMap
  3491. // Type will be GPB*Int32Dictionary, exact type doesn't matter.
  3492. GPBInt32Int32Dictionary *map =
  3493. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3494. context->outSize += [map computeSerializedSizeAsField:field];
  3495. }
  3496. return YES;
  3497. }
  3498. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(UInt32, uint32_t, UInt32)
  3499. // This block of code is generated, do not edit it directly.
  3500. static BOOL DynamicSerializedSizeUInt32(GPBFieldDescriptor *field,
  3501. void *voidContext) {
  3502. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3503. GPBFieldType fieldType = field.fieldType;
  3504. if (fieldType == GPBFieldTypeRepeated) {
  3505. GPBUInt32Array *array =
  3506. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3507. NSUInteger count = array.count;
  3508. if (count == 0) return YES;
  3509. __block size_t dataSize = 0;
  3510. [array enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  3511. #pragma unused(idx, stop)
  3512. dataSize += GPBComputeUInt32SizeNoTag(value);
  3513. }];
  3514. context->outSize += dataSize;
  3515. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3516. if (field.isPackable) {
  3517. context->outSize += tagSize;
  3518. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3519. } else {
  3520. context->outSize += count * tagSize;
  3521. }
  3522. } else if (fieldType == GPBFieldTypeSingle) {
  3523. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3524. if (selfHas) {
  3525. uint32_t selfVal = GPBGetUInt32IvarWithField(context->self, field);
  3526. context->outSize += GPBComputeUInt32Size(GPBFieldNumber(field), selfVal);
  3527. }
  3528. } else { // fieldType == GPBFieldTypeMap
  3529. // Type will be GPB*UInt32Dictionary, exact type doesn't matter.
  3530. GPBInt32UInt32Dictionary *map =
  3531. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3532. context->outSize += [map computeSerializedSizeAsField:field];
  3533. }
  3534. return YES;
  3535. }
  3536. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Fixed32, uint32_t, UInt32)
  3537. // This block of code is generated, do not edit it directly.
  3538. static BOOL DynamicSerializedSizeFixed32(GPBFieldDescriptor *field,
  3539. void *voidContext) {
  3540. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3541. GPBFieldType fieldType = field.fieldType;
  3542. if (fieldType == GPBFieldTypeRepeated) {
  3543. GPBUInt32Array *array =
  3544. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3545. NSUInteger count = array.count;
  3546. if (count == 0) return YES;
  3547. __block size_t dataSize = 0;
  3548. [array enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  3549. #pragma unused(idx, stop)
  3550. dataSize += GPBComputeFixed32SizeNoTag(value);
  3551. }];
  3552. context->outSize += dataSize;
  3553. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3554. if (field.isPackable) {
  3555. context->outSize += tagSize;
  3556. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3557. } else {
  3558. context->outSize += count * tagSize;
  3559. }
  3560. } else if (fieldType == GPBFieldTypeSingle) {
  3561. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3562. if (selfHas) {
  3563. uint32_t selfVal = GPBGetUInt32IvarWithField(context->self, field);
  3564. context->outSize += GPBComputeFixed32Size(GPBFieldNumber(field), selfVal);
  3565. }
  3566. } else { // fieldType == GPBFieldTypeMap
  3567. // Type will be GPB*UInt32Dictionary, exact type doesn't matter.
  3568. GPBInt32UInt32Dictionary *map =
  3569. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3570. context->outSize += [map computeSerializedSizeAsField:field];
  3571. }
  3572. return YES;
  3573. }
  3574. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Int64, int64_t, Int64)
  3575. // This block of code is generated, do not edit it directly.
  3576. static BOOL DynamicSerializedSizeInt64(GPBFieldDescriptor *field,
  3577. void *voidContext) {
  3578. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3579. GPBFieldType fieldType = field.fieldType;
  3580. if (fieldType == GPBFieldTypeRepeated) {
  3581. GPBInt64Array *array =
  3582. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3583. NSUInteger count = array.count;
  3584. if (count == 0) return YES;
  3585. __block size_t dataSize = 0;
  3586. [array enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  3587. #pragma unused(idx, stop)
  3588. dataSize += GPBComputeInt64SizeNoTag(value);
  3589. }];
  3590. context->outSize += dataSize;
  3591. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3592. if (field.isPackable) {
  3593. context->outSize += tagSize;
  3594. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3595. } else {
  3596. context->outSize += count * tagSize;
  3597. }
  3598. } else if (fieldType == GPBFieldTypeSingle) {
  3599. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3600. if (selfHas) {
  3601. int64_t selfVal = GPBGetInt64IvarWithField(context->self, field);
  3602. context->outSize += GPBComputeInt64Size(GPBFieldNumber(field), selfVal);
  3603. }
  3604. } else { // fieldType == GPBFieldTypeMap
  3605. // Type will be GPB*Int64Dictionary, exact type doesn't matter.
  3606. GPBInt32Int64Dictionary *map =
  3607. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3608. context->outSize += [map computeSerializedSizeAsField:field];
  3609. }
  3610. return YES;
  3611. }
  3612. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(SFixed64, int64_t, Int64)
  3613. // This block of code is generated, do not edit it directly.
  3614. static BOOL DynamicSerializedSizeSFixed64(GPBFieldDescriptor *field,
  3615. void *voidContext) {
  3616. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3617. GPBFieldType fieldType = field.fieldType;
  3618. if (fieldType == GPBFieldTypeRepeated) {
  3619. GPBInt64Array *array =
  3620. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3621. NSUInteger count = array.count;
  3622. if (count == 0) return YES;
  3623. __block size_t dataSize = 0;
  3624. [array enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  3625. #pragma unused(idx, stop)
  3626. dataSize += GPBComputeSFixed64SizeNoTag(value);
  3627. }];
  3628. context->outSize += dataSize;
  3629. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3630. if (field.isPackable) {
  3631. context->outSize += tagSize;
  3632. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3633. } else {
  3634. context->outSize += count * tagSize;
  3635. }
  3636. } else if (fieldType == GPBFieldTypeSingle) {
  3637. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3638. if (selfHas) {
  3639. int64_t selfVal = GPBGetInt64IvarWithField(context->self, field);
  3640. context->outSize += GPBComputeSFixed64Size(GPBFieldNumber(field), selfVal);
  3641. }
  3642. } else { // fieldType == GPBFieldTypeMap
  3643. // Type will be GPB*Int64Dictionary, exact type doesn't matter.
  3644. GPBInt32Int64Dictionary *map =
  3645. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3646. context->outSize += [map computeSerializedSizeAsField:field];
  3647. }
  3648. return YES;
  3649. }
  3650. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(SInt64, int64_t, Int64)
  3651. // This block of code is generated, do not edit it directly.
  3652. static BOOL DynamicSerializedSizeSInt64(GPBFieldDescriptor *field,
  3653. void *voidContext) {
  3654. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3655. GPBFieldType fieldType = field.fieldType;
  3656. if (fieldType == GPBFieldTypeRepeated) {
  3657. GPBInt64Array *array =
  3658. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3659. NSUInteger count = array.count;
  3660. if (count == 0) return YES;
  3661. __block size_t dataSize = 0;
  3662. [array enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  3663. #pragma unused(idx, stop)
  3664. dataSize += GPBComputeSInt64SizeNoTag(value);
  3665. }];
  3666. context->outSize += dataSize;
  3667. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3668. if (field.isPackable) {
  3669. context->outSize += tagSize;
  3670. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3671. } else {
  3672. context->outSize += count * tagSize;
  3673. }
  3674. } else if (fieldType == GPBFieldTypeSingle) {
  3675. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3676. if (selfHas) {
  3677. int64_t selfVal = GPBGetInt64IvarWithField(context->self, field);
  3678. context->outSize += GPBComputeSInt64Size(GPBFieldNumber(field), selfVal);
  3679. }
  3680. } else { // fieldType == GPBFieldTypeMap
  3681. // Type will be GPB*Int64Dictionary, exact type doesn't matter.
  3682. GPBInt32Int64Dictionary *map =
  3683. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3684. context->outSize += [map computeSerializedSizeAsField:field];
  3685. }
  3686. return YES;
  3687. }
  3688. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(UInt64, uint64_t, UInt64)
  3689. // This block of code is generated, do not edit it directly.
  3690. static BOOL DynamicSerializedSizeUInt64(GPBFieldDescriptor *field,
  3691. void *voidContext) {
  3692. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3693. GPBFieldType fieldType = field.fieldType;
  3694. if (fieldType == GPBFieldTypeRepeated) {
  3695. GPBUInt64Array *array =
  3696. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3697. NSUInteger count = array.count;
  3698. if (count == 0) return YES;
  3699. __block size_t dataSize = 0;
  3700. [array enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  3701. #pragma unused(idx, stop)
  3702. dataSize += GPBComputeUInt64SizeNoTag(value);
  3703. }];
  3704. context->outSize += dataSize;
  3705. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3706. if (field.isPackable) {
  3707. context->outSize += tagSize;
  3708. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3709. } else {
  3710. context->outSize += count * tagSize;
  3711. }
  3712. } else if (fieldType == GPBFieldTypeSingle) {
  3713. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3714. if (selfHas) {
  3715. uint64_t selfVal = GPBGetUInt64IvarWithField(context->self, field);
  3716. context->outSize += GPBComputeUInt64Size(GPBFieldNumber(field), selfVal);
  3717. }
  3718. } else { // fieldType == GPBFieldTypeMap
  3719. // Type will be GPB*UInt64Dictionary, exact type doesn't matter.
  3720. GPBInt32UInt64Dictionary *map =
  3721. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3722. context->outSize += [map computeSerializedSizeAsField:field];
  3723. }
  3724. return YES;
  3725. }
  3726. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Fixed64, uint64_t, UInt64)
  3727. // This block of code is generated, do not edit it directly.
  3728. static BOOL DynamicSerializedSizeFixed64(GPBFieldDescriptor *field,
  3729. void *voidContext) {
  3730. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3731. GPBFieldType fieldType = field.fieldType;
  3732. if (fieldType == GPBFieldTypeRepeated) {
  3733. GPBUInt64Array *array =
  3734. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3735. NSUInteger count = array.count;
  3736. if (count == 0) return YES;
  3737. __block size_t dataSize = 0;
  3738. [array enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  3739. #pragma unused(idx, stop)
  3740. dataSize += GPBComputeFixed64SizeNoTag(value);
  3741. }];
  3742. context->outSize += dataSize;
  3743. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3744. if (field.isPackable) {
  3745. context->outSize += tagSize;
  3746. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3747. } else {
  3748. context->outSize += count * tagSize;
  3749. }
  3750. } else if (fieldType == GPBFieldTypeSingle) {
  3751. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3752. if (selfHas) {
  3753. uint64_t selfVal = GPBGetUInt64IvarWithField(context->self, field);
  3754. context->outSize += GPBComputeFixed64Size(GPBFieldNumber(field), selfVal);
  3755. }
  3756. } else { // fieldType == GPBFieldTypeMap
  3757. // Type will be GPB*UInt64Dictionary, exact type doesn't matter.
  3758. GPBInt32UInt64Dictionary *map =
  3759. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3760. context->outSize += [map computeSerializedSizeAsField:field];
  3761. }
  3762. return YES;
  3763. }
  3764. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Float, float, Float)
  3765. // This block of code is generated, do not edit it directly.
  3766. static BOOL DynamicSerializedSizeFloat(GPBFieldDescriptor *field,
  3767. void *voidContext) {
  3768. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3769. GPBFieldType fieldType = field.fieldType;
  3770. if (fieldType == GPBFieldTypeRepeated) {
  3771. GPBFloatArray *array =
  3772. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3773. NSUInteger count = array.count;
  3774. if (count == 0) return YES;
  3775. __block size_t dataSize = 0;
  3776. [array enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
  3777. #pragma unused(idx, stop)
  3778. dataSize += GPBComputeFloatSizeNoTag(value);
  3779. }];
  3780. context->outSize += dataSize;
  3781. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3782. if (field.isPackable) {
  3783. context->outSize += tagSize;
  3784. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3785. } else {
  3786. context->outSize += count * tagSize;
  3787. }
  3788. } else if (fieldType == GPBFieldTypeSingle) {
  3789. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3790. if (selfHas) {
  3791. float selfVal = GPBGetFloatIvarWithField(context->self, field);
  3792. context->outSize += GPBComputeFloatSize(GPBFieldNumber(field), selfVal);
  3793. }
  3794. } else { // fieldType == GPBFieldTypeMap
  3795. // Type will be GPB*FloatDictionary, exact type doesn't matter.
  3796. GPBInt32FloatDictionary *map =
  3797. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3798. context->outSize += [map computeSerializedSizeAsField:field];
  3799. }
  3800. return YES;
  3801. }
  3802. //%PDDM-EXPAND SERIALIZED_SIZE_POD_FUNC(Double, double, Double)
  3803. // This block of code is generated, do not edit it directly.
  3804. static BOOL DynamicSerializedSizeDouble(GPBFieldDescriptor *field,
  3805. void *voidContext) {
  3806. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3807. GPBFieldType fieldType = field.fieldType;
  3808. if (fieldType == GPBFieldTypeRepeated) {
  3809. GPBDoubleArray *array =
  3810. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3811. NSUInteger count = array.count;
  3812. if (count == 0) return YES;
  3813. __block size_t dataSize = 0;
  3814. [array enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
  3815. #pragma unused(idx, stop)
  3816. dataSize += GPBComputeDoubleSizeNoTag(value);
  3817. }];
  3818. context->outSize += dataSize;
  3819. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(field));
  3820. if (field.isPackable) {
  3821. context->outSize += tagSize;
  3822. context->outSize += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  3823. } else {
  3824. context->outSize += count * tagSize;
  3825. }
  3826. } else if (fieldType == GPBFieldTypeSingle) {
  3827. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3828. if (selfHas) {
  3829. double selfVal = GPBGetDoubleIvarWithField(context->self, field);
  3830. context->outSize += GPBComputeDoubleSize(GPBFieldNumber(field), selfVal);
  3831. }
  3832. } else { // fieldType == GPBFieldTypeMap
  3833. // Type will be GPB*DoubleDictionary, exact type doesn't matter.
  3834. GPBInt32DoubleDictionary *map =
  3835. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3836. context->outSize += [map computeSerializedSizeAsField:field];
  3837. }
  3838. return YES;
  3839. }
  3840. //%PDDM-EXPAND SERIALIZED_SIZE_OBJECT_FUNC(String)
  3841. // This block of code is generated, do not edit it directly.
  3842. static BOOL DynamicSerializedSizeString(GPBFieldDescriptor *field,
  3843. void *voidContext) {
  3844. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3845. GPBFieldType fieldType = field.fieldType;
  3846. if (fieldType == GPBFieldTypeRepeated) {
  3847. NSArray *array =
  3848. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3849. for (id value in array) {
  3850. context->outSize += GPBComputeStringSizeNoTag(value);
  3851. }
  3852. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field),
  3853. GPBGetFieldType(field));
  3854. context->outSize += array.count * tagSize;
  3855. } else if (fieldType == GPBFieldTypeSingle) {
  3856. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3857. if (selfHas) {
  3858. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  3859. // again.
  3860. id selfVal = GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3861. context->outSize += GPBComputeStringSize(GPBFieldNumber(field), selfVal);
  3862. }
  3863. } else { // fieldType == GPBFieldTypeMap
  3864. GPBType mapKeyType = field.mapKeyType;
  3865. if (mapKeyType == GPBTypeString) {
  3866. // If key type was string, then the map is an NSDictionary.
  3867. NSDictionary *map =
  3868. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3869. context->outSize += GPBDictionaryComputeSizeInternalHelper(map, field);
  3870. } else {
  3871. // Type will be GPB*StringDictionary, exact type doesn't matter.
  3872. GPBInt32ObjectDictionary *map =
  3873. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3874. context->outSize += [map computeSerializedSizeAsField:field];
  3875. }
  3876. }
  3877. return YES;
  3878. }
  3879. //%PDDM-EXPAND SERIALIZED_SIZE_OBJECT_FUNC(Data)
  3880. // This block of code is generated, do not edit it directly.
  3881. static BOOL DynamicSerializedSizeData(GPBFieldDescriptor *field,
  3882. void *voidContext) {
  3883. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3884. GPBFieldType fieldType = field.fieldType;
  3885. if (fieldType == GPBFieldTypeRepeated) {
  3886. NSArray *array =
  3887. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3888. for (id value in array) {
  3889. context->outSize += GPBComputeDataSizeNoTag(value);
  3890. }
  3891. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field),
  3892. GPBGetFieldType(field));
  3893. context->outSize += array.count * tagSize;
  3894. } else if (fieldType == GPBFieldTypeSingle) {
  3895. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3896. if (selfHas) {
  3897. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  3898. // again.
  3899. id selfVal = GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3900. context->outSize += GPBComputeDataSize(GPBFieldNumber(field), selfVal);
  3901. }
  3902. } else { // fieldType == GPBFieldTypeMap
  3903. GPBType mapKeyType = field.mapKeyType;
  3904. if (mapKeyType == GPBTypeString) {
  3905. // If key type was string, then the map is an NSDictionary.
  3906. NSDictionary *map =
  3907. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3908. context->outSize += GPBDictionaryComputeSizeInternalHelper(map, field);
  3909. } else {
  3910. // Type will be GPB*DataDictionary, exact type doesn't matter.
  3911. GPBInt32ObjectDictionary *map =
  3912. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3913. context->outSize += [map computeSerializedSizeAsField:field];
  3914. }
  3915. }
  3916. return YES;
  3917. }
  3918. //%PDDM-EXPAND SERIALIZED_SIZE_OBJECT_FUNC(Message)
  3919. // This block of code is generated, do not edit it directly.
  3920. static BOOL DynamicSerializedSizeMessage(GPBFieldDescriptor *field,
  3921. void *voidContext) {
  3922. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3923. GPBFieldType fieldType = field.fieldType;
  3924. if (fieldType == GPBFieldTypeRepeated) {
  3925. NSArray *array =
  3926. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3927. for (id value in array) {
  3928. context->outSize += GPBComputeMessageSizeNoTag(value);
  3929. }
  3930. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field),
  3931. GPBGetFieldType(field));
  3932. context->outSize += array.count * tagSize;
  3933. } else if (fieldType == GPBFieldTypeSingle) {
  3934. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3935. if (selfHas) {
  3936. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  3937. // again.
  3938. id selfVal = GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3939. context->outSize += GPBComputeMessageSize(GPBFieldNumber(field), selfVal);
  3940. }
  3941. } else { // fieldType == GPBFieldTypeMap
  3942. GPBType mapKeyType = field.mapKeyType;
  3943. if (mapKeyType == GPBTypeString) {
  3944. // If key type was string, then the map is an NSDictionary.
  3945. NSDictionary *map =
  3946. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3947. context->outSize += GPBDictionaryComputeSizeInternalHelper(map, field);
  3948. } else {
  3949. // Type will be GPB*MessageDictionary, exact type doesn't matter.
  3950. GPBInt32ObjectDictionary *map =
  3951. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3952. context->outSize += [map computeSerializedSizeAsField:field];
  3953. }
  3954. }
  3955. return YES;
  3956. }
  3957. //%PDDM-EXPAND SERIALIZED_SIZE_OBJECT_FUNC(Group)
  3958. // This block of code is generated, do not edit it directly.
  3959. static BOOL DynamicSerializedSizeGroup(GPBFieldDescriptor *field,
  3960. void *voidContext) {
  3961. SerializedSizeContext *context = (SerializedSizeContext *)voidContext;
  3962. GPBFieldType fieldType = field.fieldType;
  3963. if (fieldType == GPBFieldTypeRepeated) {
  3964. NSArray *array =
  3965. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3966. for (id value in array) {
  3967. context->outSize += GPBComputeGroupSizeNoTag(value);
  3968. }
  3969. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field),
  3970. GPBGetFieldType(field));
  3971. context->outSize += array.count * tagSize;
  3972. } else if (fieldType == GPBFieldTypeSingle) {
  3973. BOOL selfHas = GPBGetHasIvarField(context->self, field);
  3974. if (selfHas) {
  3975. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  3976. // again.
  3977. id selfVal = GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3978. context->outSize += GPBComputeGroupSize(GPBFieldNumber(field), selfVal);
  3979. }
  3980. } else { // fieldType == GPBFieldTypeMap
  3981. GPBType mapKeyType = field.mapKeyType;
  3982. if (mapKeyType == GPBTypeString) {
  3983. // If key type was string, then the map is an NSDictionary.
  3984. NSDictionary *map =
  3985. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3986. context->outSize += GPBDictionaryComputeSizeInternalHelper(map, field);
  3987. } else {
  3988. // Type will be GPB*GroupDictionary, exact type doesn't matter.
  3989. GPBInt32ObjectDictionary *map =
  3990. GPBGetObjectIvarWithFieldNoAutocreate(context->self, field);
  3991. context->outSize += [map computeSerializedSizeAsField:field];
  3992. }
  3993. }
  3994. return YES;
  3995. }
  3996. //%PDDM-EXPAND-END (18 expansions)
  3997. - (size_t)serializedSize {
  3998. // Fields.
  3999. SerializedSizeContext context = {self, 0};
  4000. GPBApplyStrictFunctions funcs =
  4001. GPBAPPLY_STRICT_FUNCTIONS_INIT(DynamicSerializedSize);
  4002. GPBApplyStrictFunctionsToMessageFields(&funcs, self, &context);
  4003. // Add any unknown fields.
  4004. const GPBDescriptor *descriptor = [self descriptor];
  4005. if (descriptor.wireFormat) {
  4006. context.outSize += [unknownFields_ serializedSizeAsMessageSet];
  4007. } else {
  4008. context.outSize += [unknownFields_ serializedSize];
  4009. }
  4010. // Add any extensions.
  4011. for (GPBExtensionField *extension in extensionMap_) {
  4012. id value = [extensionMap_ objectForKey:extension];
  4013. context.outSize += [extension computeSerializedSizeIncludingTag:value];
  4014. }
  4015. return context.outSize;
  4016. }
  4017. #pragma mark - Resolve Methods Support
  4018. typedef struct IvarAccessorMethodContext {
  4019. GPBFileSyntax syntax;
  4020. IMP impToAdd;
  4021. SEL encodingSelector;
  4022. } IvarAccessorMethodContext;
  4023. //%PDDM-DEFINE IVAR_ACCESSOR_FUNC_COMMON(NAME, TYPE, TRUE_NAME)
  4024. //%static BOOL IvarGet##NAME(GPBFieldDescriptor *field, void *voidContext) {
  4025. //% IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4026. //% context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4027. //% return GPBGet##TRUE_NAME##IvarWithField(obj, field);
  4028. //% });
  4029. //% context->encodingSelector = @selector(get##NAME);
  4030. //% return NO;
  4031. //%}
  4032. //%
  4033. //%PDDM-DEFINE IVAR_ACCESSOR_FUNC_OBJECT(NAME, TYPE)
  4034. //%IVAR_ACCESSOR_FUNC_COMMON(NAME, TYPE, Object)
  4035. //%static BOOL IvarSet##NAME(GPBFieldDescriptor *field, void *voidContext) {
  4036. //% IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4037. //% // Local for syntax so the block doesn't capture context and use random
  4038. //% // memory in the future.
  4039. //% const GPBFileSyntax syntax = context->syntax;
  4040. //% context->impToAdd = imp_implementationWithBlock(^(id obj, TYPE value) {
  4041. //% return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  4042. //% });
  4043. //% context->encodingSelector = @selector(set##NAME:);
  4044. //% return NO;
  4045. //%}
  4046. //%
  4047. //%PDDM-DEFINE IVAR_ACCESSOR_FUNC_PER_VERSION(NAME, TYPE)
  4048. //%IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(NAME, TYPE, NAME)
  4049. //%PDDM-DEFINE IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(NAME, TYPE, TRUE_NAME)
  4050. //%IVAR_ACCESSOR_FUNC_COMMON(NAME, TYPE, TRUE_NAME)
  4051. //%static BOOL IvarSet##NAME(GPBFieldDescriptor *field, void *voidContext) {
  4052. //% IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4053. //% // Local for syntax so the block doesn't capture context and use random
  4054. //% // memory in the future.
  4055. //% const GPBFileSyntax syntax = context->syntax;
  4056. //% context->impToAdd = imp_implementationWithBlock(^(id obj, TYPE value) {
  4057. //% return GPBSet##TRUE_NAME##IvarWithFieldInternal(obj, field, value, syntax);
  4058. //% });
  4059. //% context->encodingSelector = @selector(set##NAME:);
  4060. //% return NO;
  4061. //%}
  4062. //%
  4063. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(Bool, BOOL)
  4064. // This block of code is generated, do not edit it directly.
  4065. static BOOL IvarGetBool(GPBFieldDescriptor *field, void *voidContext) {
  4066. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4067. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4068. return GPBGetBoolIvarWithField(obj, field);
  4069. });
  4070. context->encodingSelector = @selector(getBool);
  4071. return NO;
  4072. }
  4073. static BOOL IvarSetBool(GPBFieldDescriptor *field, void *voidContext) {
  4074. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4075. // Local for syntax so the block doesn't capture context and use random
  4076. // memory in the future.
  4077. const GPBFileSyntax syntax = context->syntax;
  4078. context->impToAdd = imp_implementationWithBlock(^(id obj, BOOL value) {
  4079. return GPBSetBoolIvarWithFieldInternal(obj, field, value, syntax);
  4080. });
  4081. context->encodingSelector = @selector(setBool:);
  4082. return NO;
  4083. }
  4084. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(Int32, int32_t)
  4085. // This block of code is generated, do not edit it directly.
  4086. static BOOL IvarGetInt32(GPBFieldDescriptor *field, void *voidContext) {
  4087. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4088. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4089. return GPBGetInt32IvarWithField(obj, field);
  4090. });
  4091. context->encodingSelector = @selector(getInt32);
  4092. return NO;
  4093. }
  4094. static BOOL IvarSetInt32(GPBFieldDescriptor *field, void *voidContext) {
  4095. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4096. // Local for syntax so the block doesn't capture context and use random
  4097. // memory in the future.
  4098. const GPBFileSyntax syntax = context->syntax;
  4099. context->impToAdd = imp_implementationWithBlock(^(id obj, int32_t value) {
  4100. return GPBSetInt32IvarWithFieldInternal(obj, field, value, syntax);
  4101. });
  4102. context->encodingSelector = @selector(setInt32:);
  4103. return NO;
  4104. }
  4105. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(SInt32, int32_t, Int32)
  4106. // This block of code is generated, do not edit it directly.
  4107. static BOOL IvarGetSInt32(GPBFieldDescriptor *field, void *voidContext) {
  4108. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4109. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4110. return GPBGetInt32IvarWithField(obj, field);
  4111. });
  4112. context->encodingSelector = @selector(getSInt32);
  4113. return NO;
  4114. }
  4115. static BOOL IvarSetSInt32(GPBFieldDescriptor *field, void *voidContext) {
  4116. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4117. // Local for syntax so the block doesn't capture context and use random
  4118. // memory in the future.
  4119. const GPBFileSyntax syntax = context->syntax;
  4120. context->impToAdd = imp_implementationWithBlock(^(id obj, int32_t value) {
  4121. return GPBSetInt32IvarWithFieldInternal(obj, field, value, syntax);
  4122. });
  4123. context->encodingSelector = @selector(setSInt32:);
  4124. return NO;
  4125. }
  4126. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(SFixed32, int32_t, Int32)
  4127. // This block of code is generated, do not edit it directly.
  4128. static BOOL IvarGetSFixed32(GPBFieldDescriptor *field, void *voidContext) {
  4129. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4130. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4131. return GPBGetInt32IvarWithField(obj, field);
  4132. });
  4133. context->encodingSelector = @selector(getSFixed32);
  4134. return NO;
  4135. }
  4136. static BOOL IvarSetSFixed32(GPBFieldDescriptor *field, void *voidContext) {
  4137. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4138. // Local for syntax so the block doesn't capture context and use random
  4139. // memory in the future.
  4140. const GPBFileSyntax syntax = context->syntax;
  4141. context->impToAdd = imp_implementationWithBlock(^(id obj, int32_t value) {
  4142. return GPBSetInt32IvarWithFieldInternal(obj, field, value, syntax);
  4143. });
  4144. context->encodingSelector = @selector(setSFixed32:);
  4145. return NO;
  4146. }
  4147. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(UInt32, uint32_t)
  4148. // This block of code is generated, do not edit it directly.
  4149. static BOOL IvarGetUInt32(GPBFieldDescriptor *field, void *voidContext) {
  4150. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4151. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4152. return GPBGetUInt32IvarWithField(obj, field);
  4153. });
  4154. context->encodingSelector = @selector(getUInt32);
  4155. return NO;
  4156. }
  4157. static BOOL IvarSetUInt32(GPBFieldDescriptor *field, void *voidContext) {
  4158. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4159. // Local for syntax so the block doesn't capture context and use random
  4160. // memory in the future.
  4161. const GPBFileSyntax syntax = context->syntax;
  4162. context->impToAdd = imp_implementationWithBlock(^(id obj, uint32_t value) {
  4163. return GPBSetUInt32IvarWithFieldInternal(obj, field, value, syntax);
  4164. });
  4165. context->encodingSelector = @selector(setUInt32:);
  4166. return NO;
  4167. }
  4168. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(Fixed32, uint32_t, UInt32)
  4169. // This block of code is generated, do not edit it directly.
  4170. static BOOL IvarGetFixed32(GPBFieldDescriptor *field, void *voidContext) {
  4171. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4172. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4173. return GPBGetUInt32IvarWithField(obj, field);
  4174. });
  4175. context->encodingSelector = @selector(getFixed32);
  4176. return NO;
  4177. }
  4178. static BOOL IvarSetFixed32(GPBFieldDescriptor *field, void *voidContext) {
  4179. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4180. // Local for syntax so the block doesn't capture context and use random
  4181. // memory in the future.
  4182. const GPBFileSyntax syntax = context->syntax;
  4183. context->impToAdd = imp_implementationWithBlock(^(id obj, uint32_t value) {
  4184. return GPBSetUInt32IvarWithFieldInternal(obj, field, value, syntax);
  4185. });
  4186. context->encodingSelector = @selector(setFixed32:);
  4187. return NO;
  4188. }
  4189. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(Int64, int64_t)
  4190. // This block of code is generated, do not edit it directly.
  4191. static BOOL IvarGetInt64(GPBFieldDescriptor *field, void *voidContext) {
  4192. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4193. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4194. return GPBGetInt64IvarWithField(obj, field);
  4195. });
  4196. context->encodingSelector = @selector(getInt64);
  4197. return NO;
  4198. }
  4199. static BOOL IvarSetInt64(GPBFieldDescriptor *field, void *voidContext) {
  4200. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4201. // Local for syntax so the block doesn't capture context and use random
  4202. // memory in the future.
  4203. const GPBFileSyntax syntax = context->syntax;
  4204. context->impToAdd = imp_implementationWithBlock(^(id obj, int64_t value) {
  4205. return GPBSetInt64IvarWithFieldInternal(obj, field, value, syntax);
  4206. });
  4207. context->encodingSelector = @selector(setInt64:);
  4208. return NO;
  4209. }
  4210. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(SFixed64, int64_t, Int64)
  4211. // This block of code is generated, do not edit it directly.
  4212. static BOOL IvarGetSFixed64(GPBFieldDescriptor *field, void *voidContext) {
  4213. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4214. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4215. return GPBGetInt64IvarWithField(obj, field);
  4216. });
  4217. context->encodingSelector = @selector(getSFixed64);
  4218. return NO;
  4219. }
  4220. static BOOL IvarSetSFixed64(GPBFieldDescriptor *field, void *voidContext) {
  4221. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4222. // Local for syntax so the block doesn't capture context and use random
  4223. // memory in the future.
  4224. const GPBFileSyntax syntax = context->syntax;
  4225. context->impToAdd = imp_implementationWithBlock(^(id obj, int64_t value) {
  4226. return GPBSetInt64IvarWithFieldInternal(obj, field, value, syntax);
  4227. });
  4228. context->encodingSelector = @selector(setSFixed64:);
  4229. return NO;
  4230. }
  4231. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(SInt64, int64_t, Int64)
  4232. // This block of code is generated, do not edit it directly.
  4233. static BOOL IvarGetSInt64(GPBFieldDescriptor *field, void *voidContext) {
  4234. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4235. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4236. return GPBGetInt64IvarWithField(obj, field);
  4237. });
  4238. context->encodingSelector = @selector(getSInt64);
  4239. return NO;
  4240. }
  4241. static BOOL IvarSetSInt64(GPBFieldDescriptor *field, void *voidContext) {
  4242. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4243. // Local for syntax so the block doesn't capture context and use random
  4244. // memory in the future.
  4245. const GPBFileSyntax syntax = context->syntax;
  4246. context->impToAdd = imp_implementationWithBlock(^(id obj, int64_t value) {
  4247. return GPBSetInt64IvarWithFieldInternal(obj, field, value, syntax);
  4248. });
  4249. context->encodingSelector = @selector(setSInt64:);
  4250. return NO;
  4251. }
  4252. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(UInt64, uint64_t)
  4253. // This block of code is generated, do not edit it directly.
  4254. static BOOL IvarGetUInt64(GPBFieldDescriptor *field, void *voidContext) {
  4255. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4256. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4257. return GPBGetUInt64IvarWithField(obj, field);
  4258. });
  4259. context->encodingSelector = @selector(getUInt64);
  4260. return NO;
  4261. }
  4262. static BOOL IvarSetUInt64(GPBFieldDescriptor *field, void *voidContext) {
  4263. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4264. // Local for syntax so the block doesn't capture context and use random
  4265. // memory in the future.
  4266. const GPBFileSyntax syntax = context->syntax;
  4267. context->impToAdd = imp_implementationWithBlock(^(id obj, uint64_t value) {
  4268. return GPBSetUInt64IvarWithFieldInternal(obj, field, value, syntax);
  4269. });
  4270. context->encodingSelector = @selector(setUInt64:);
  4271. return NO;
  4272. }
  4273. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION_ALIASED(Fixed64, uint64_t, UInt64)
  4274. // This block of code is generated, do not edit it directly.
  4275. static BOOL IvarGetFixed64(GPBFieldDescriptor *field, void *voidContext) {
  4276. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4277. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4278. return GPBGetUInt64IvarWithField(obj, field);
  4279. });
  4280. context->encodingSelector = @selector(getFixed64);
  4281. return NO;
  4282. }
  4283. static BOOL IvarSetFixed64(GPBFieldDescriptor *field, void *voidContext) {
  4284. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4285. // Local for syntax so the block doesn't capture context and use random
  4286. // memory in the future.
  4287. const GPBFileSyntax syntax = context->syntax;
  4288. context->impToAdd = imp_implementationWithBlock(^(id obj, uint64_t value) {
  4289. return GPBSetUInt64IvarWithFieldInternal(obj, field, value, syntax);
  4290. });
  4291. context->encodingSelector = @selector(setFixed64:);
  4292. return NO;
  4293. }
  4294. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(Float, float)
  4295. // This block of code is generated, do not edit it directly.
  4296. static BOOL IvarGetFloat(GPBFieldDescriptor *field, void *voidContext) {
  4297. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4298. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4299. return GPBGetFloatIvarWithField(obj, field);
  4300. });
  4301. context->encodingSelector = @selector(getFloat);
  4302. return NO;
  4303. }
  4304. static BOOL IvarSetFloat(GPBFieldDescriptor *field, void *voidContext) {
  4305. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4306. // Local for syntax so the block doesn't capture context and use random
  4307. // memory in the future.
  4308. const GPBFileSyntax syntax = context->syntax;
  4309. context->impToAdd = imp_implementationWithBlock(^(id obj, float value) {
  4310. return GPBSetFloatIvarWithFieldInternal(obj, field, value, syntax);
  4311. });
  4312. context->encodingSelector = @selector(setFloat:);
  4313. return NO;
  4314. }
  4315. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_PER_VERSION(Double, double)
  4316. // This block of code is generated, do not edit it directly.
  4317. static BOOL IvarGetDouble(GPBFieldDescriptor *field, void *voidContext) {
  4318. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4319. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4320. return GPBGetDoubleIvarWithField(obj, field);
  4321. });
  4322. context->encodingSelector = @selector(getDouble);
  4323. return NO;
  4324. }
  4325. static BOOL IvarSetDouble(GPBFieldDescriptor *field, void *voidContext) {
  4326. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4327. // Local for syntax so the block doesn't capture context and use random
  4328. // memory in the future.
  4329. const GPBFileSyntax syntax = context->syntax;
  4330. context->impToAdd = imp_implementationWithBlock(^(id obj, double value) {
  4331. return GPBSetDoubleIvarWithFieldInternal(obj, field, value, syntax);
  4332. });
  4333. context->encodingSelector = @selector(setDouble:);
  4334. return NO;
  4335. }
  4336. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_OBJECT(String, id)
  4337. // This block of code is generated, do not edit it directly.
  4338. static BOOL IvarGetString(GPBFieldDescriptor *field, void *voidContext) {
  4339. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4340. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4341. return GPBGetObjectIvarWithField(obj, field);
  4342. });
  4343. context->encodingSelector = @selector(getString);
  4344. return NO;
  4345. }
  4346. static BOOL IvarSetString(GPBFieldDescriptor *field, void *voidContext) {
  4347. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4348. // Local for syntax so the block doesn't capture context and use random
  4349. // memory in the future.
  4350. const GPBFileSyntax syntax = context->syntax;
  4351. context->impToAdd = imp_implementationWithBlock(^(id obj, id value) {
  4352. return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  4353. });
  4354. context->encodingSelector = @selector(setString:);
  4355. return NO;
  4356. }
  4357. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_OBJECT(Data, id)
  4358. // This block of code is generated, do not edit it directly.
  4359. static BOOL IvarGetData(GPBFieldDescriptor *field, void *voidContext) {
  4360. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4361. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4362. return GPBGetObjectIvarWithField(obj, field);
  4363. });
  4364. context->encodingSelector = @selector(getData);
  4365. return NO;
  4366. }
  4367. static BOOL IvarSetData(GPBFieldDescriptor *field, void *voidContext) {
  4368. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4369. // Local for syntax so the block doesn't capture context and use random
  4370. // memory in the future.
  4371. const GPBFileSyntax syntax = context->syntax;
  4372. context->impToAdd = imp_implementationWithBlock(^(id obj, id value) {
  4373. return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  4374. });
  4375. context->encodingSelector = @selector(setData:);
  4376. return NO;
  4377. }
  4378. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_OBJECT(Message, id)
  4379. // This block of code is generated, do not edit it directly.
  4380. static BOOL IvarGetMessage(GPBFieldDescriptor *field, void *voidContext) {
  4381. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4382. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4383. return GPBGetObjectIvarWithField(obj, field);
  4384. });
  4385. context->encodingSelector = @selector(getMessage);
  4386. return NO;
  4387. }
  4388. static BOOL IvarSetMessage(GPBFieldDescriptor *field, void *voidContext) {
  4389. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4390. // Local for syntax so the block doesn't capture context and use random
  4391. // memory in the future.
  4392. const GPBFileSyntax syntax = context->syntax;
  4393. context->impToAdd = imp_implementationWithBlock(^(id obj, id value) {
  4394. return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  4395. });
  4396. context->encodingSelector = @selector(setMessage:);
  4397. return NO;
  4398. }
  4399. //%PDDM-EXPAND IVAR_ACCESSOR_FUNC_OBJECT(Group, id)
  4400. // This block of code is generated, do not edit it directly.
  4401. static BOOL IvarGetGroup(GPBFieldDescriptor *field, void *voidContext) {
  4402. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4403. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4404. return GPBGetObjectIvarWithField(obj, field);
  4405. });
  4406. context->encodingSelector = @selector(getGroup);
  4407. return NO;
  4408. }
  4409. static BOOL IvarSetGroup(GPBFieldDescriptor *field, void *voidContext) {
  4410. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4411. // Local for syntax so the block doesn't capture context and use random
  4412. // memory in the future.
  4413. const GPBFileSyntax syntax = context->syntax;
  4414. context->impToAdd = imp_implementationWithBlock(^(id obj, id value) {
  4415. return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  4416. });
  4417. context->encodingSelector = @selector(setGroup:);
  4418. return NO;
  4419. }
  4420. //%PDDM-EXPAND-END (17 expansions)
  4421. // Enum gets custom hooks because get needs the syntax to Get.
  4422. static BOOL IvarGetEnum(GPBFieldDescriptor *field, void *voidContext) {
  4423. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4424. // Local for syntax so the block doesn't capture context and use random
  4425. // memory in the future.
  4426. const GPBFileSyntax syntax = context->syntax;
  4427. context->impToAdd = imp_implementationWithBlock(^(id obj) {
  4428. return GPBGetEnumIvarWithFieldInternal(obj, field, syntax);
  4429. });
  4430. context->encodingSelector = @selector(getEnum);
  4431. return NO;
  4432. }
  4433. static BOOL IvarSetEnum(GPBFieldDescriptor *field, void *voidContext) {
  4434. IvarAccessorMethodContext *context = (IvarAccessorMethodContext *)voidContext;
  4435. // Local for syntax so the block doesn't capture context and use random
  4436. // memory in the future.
  4437. const GPBFileSyntax syntax = context->syntax;
  4438. context->impToAdd = imp_implementationWithBlock(^(id obj, int32_t value) {
  4439. return GPBSetEnumIvarWithFieldInternal(obj, field, value, syntax);
  4440. });
  4441. context->encodingSelector = @selector(setEnum:);
  4442. return NO;
  4443. }
  4444. + (BOOL)resolveInstanceMethod:(SEL)sel {
  4445. const GPBDescriptor *descriptor = [self descriptor];
  4446. if (!descriptor) {
  4447. return NO;
  4448. }
  4449. // NOTE: hasSel_/setHasSel_ will be NULL if the field for the given message
  4450. // should not have has support (done in GPBDescriptor.m), so there is no need
  4451. // for checks here to see if has*/setHas* are allowed.
  4452. IvarAccessorMethodContext context = {descriptor.file.syntax, NULL, NULL};
  4453. for (GPBFieldDescriptor *field in descriptor->fields_) {
  4454. BOOL isMapOrArray = GPBFieldIsMapOrArray(field);
  4455. if (!isMapOrArray) {
  4456. if (sel == field->getSel_) {
  4457. static const GPBApplyStrictFunctions funcs =
  4458. GPBAPPLY_STRICT_FUNCTIONS_INIT(IvarGet);
  4459. funcs[GPBGetFieldType(field)](field, &context);
  4460. break;
  4461. } else if (sel == field->hasSel_) {
  4462. int32_t index = GPBFieldHasIndex(field);
  4463. uint32_t fieldNum = GPBFieldNumber(field);
  4464. context.impToAdd = imp_implementationWithBlock(^(id obj) {
  4465. return GPBGetHasIvar(obj, index, fieldNum);
  4466. });
  4467. context.encodingSelector = @selector(getBool);
  4468. break;
  4469. } else if (sel == field->setHasSel_) {
  4470. context.impToAdd = imp_implementationWithBlock(^(id obj, BOOL value) {
  4471. if (value) {
  4472. [NSException raise:NSInvalidArgumentException
  4473. format:@"%@: %@ can only be set to NO (to clear field).",
  4474. [obj class],
  4475. NSStringFromSelector(field->setHasSel_)];
  4476. }
  4477. GPBClearMessageField(obj, field);
  4478. });
  4479. context.encodingSelector = @selector(setBool:);
  4480. break;
  4481. } else if (sel == field->setSel_) {
  4482. GPBApplyStrictFunctions funcs = GPBAPPLY_STRICT_FUNCTIONS_INIT(IvarSet);
  4483. funcs[GPBGetFieldType(field)](field, &context);
  4484. break;
  4485. } else {
  4486. GPBOneofDescriptor *oneof = field->containingOneof_;
  4487. if (oneof && (sel == oneof->caseSel_)) {
  4488. int32_t index = oneof->oneofDescription_->index;
  4489. context.impToAdd = imp_implementationWithBlock(^(id obj) {
  4490. return GPBGetHasOneof(obj, index);
  4491. });
  4492. context.encodingSelector = @selector(getEnum);
  4493. break;
  4494. }
  4495. }
  4496. } else {
  4497. if (sel == field->getSel_) {
  4498. if (field.fieldType == GPBFieldTypeRepeated) {
  4499. context.impToAdd = imp_implementationWithBlock(^(id obj) {
  4500. return GetArrayIvarWithField(obj, field);
  4501. });
  4502. } else {
  4503. context.impToAdd = imp_implementationWithBlock(^(id obj) {
  4504. return GetMapIvarWithField(obj, field);
  4505. });
  4506. }
  4507. context.encodingSelector = @selector(getArray);
  4508. break;
  4509. } else if (sel == field->setSel_) {
  4510. // Local for syntax so the block doesn't capture context and use random
  4511. // memory in the future.
  4512. const GPBFileSyntax syntax = context.syntax;
  4513. context.impToAdd = imp_implementationWithBlock(^(id obj, id value) {
  4514. return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  4515. });
  4516. context.encodingSelector = @selector(setArray:);
  4517. break;
  4518. }
  4519. }
  4520. }
  4521. if (context.impToAdd) {
  4522. const char *encoding =
  4523. GPBMessageEncodingForSelector(context.encodingSelector, YES);
  4524. BOOL methodAdded = class_addMethod(descriptor.messageClass, sel,
  4525. context.impToAdd, encoding);
  4526. return methodAdded;
  4527. }
  4528. return [super resolveInstanceMethod:sel];
  4529. }
  4530. + (BOOL)resolveClassMethod:(SEL)sel {
  4531. // Extensions scoped to a Message and looked up via class methods.
  4532. if (GPBResolveExtensionClassMethod(self, sel)) {
  4533. return YES;
  4534. }
  4535. return [super resolveClassMethod:sel];
  4536. }
  4537. #pragma mark - NSCoding Support
  4538. + (BOOL)supportsSecureCoding {
  4539. return YES;
  4540. }
  4541. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  4542. self = [self init];
  4543. if (self) {
  4544. NSData *data =
  4545. [aDecoder decodeObjectOfClass:[NSData class] forKey:kGPBDataCoderKey];
  4546. if (data.length) {
  4547. [self mergeFromData:data extensionRegistry:nil];
  4548. }
  4549. }
  4550. return self;
  4551. }
  4552. - (void)encodeWithCoder:(NSCoder *)aCoder {
  4553. NSData *data = [self data];
  4554. if (data.length) {
  4555. [aCoder encodeObject:data forKey:kGPBDataCoderKey];
  4556. }
  4557. }
  4558. #pragma mark - KVC Support
  4559. + (BOOL)accessInstanceVariablesDirectly {
  4560. // Make sure KVC doesn't use instance variables.
  4561. return NO;
  4562. }
  4563. @end