GPBMessage.m 181 KB

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