GPBUtilities_PackagePrivate.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <Foundation/Foundation.h>
  31. #import "GPBUtilities.h"
  32. #import "GPBDescriptor_PackagePrivate.h"
  33. // Macros for stringifying library symbols. These are used in the generated
  34. // GPB descriptor classes wherever a library symbol name is represented as a
  35. // string.
  36. #define GPBStringify(S) #S
  37. #define GPBStringifySymbol(S) GPBStringify(S)
  38. #define GPBNSStringify(S) @#S
  39. #define GPBNSStringifySymbol(S) GPBNSStringify(S)
  40. // Macros for generating a Class from a class name. These are used in
  41. // the generated GPB descriptor classes wherever an Objective C class
  42. // reference is needed for a generated class.
  43. #define GPBObjCClassSymbol(name) OBJC_CLASS_$_##name
  44. #define GPBObjCClass(name) \
  45. ((__bridge Class)&(GPBObjCClassSymbol(name)))
  46. #define GPBObjCClassDeclaration(name) \
  47. extern const GPBObjcClass_t GPBObjCClassSymbol(name)
  48. // Constant to internally mark when there is no has bit.
  49. #define GPBNoHasBit INT32_MAX
  50. CF_EXTERN_C_BEGIN
  51. // These two are used to inject a runtime check for version mismatch into the
  52. // generated sources to make sure they are linked with a supporting runtime.
  53. void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion);
  54. GPB_INLINE void GPB_DEBUG_CHECK_RUNTIME_VERSIONS() {
  55. // NOTE: By being inline here, this captures the value from the library's
  56. // headers at the time the generated code was compiled.
  57. #if defined(DEBUG) && DEBUG
  58. GPBCheckRuntimeVersionSupport(GOOGLE_PROTOBUF_OBJC_VERSION);
  59. #endif
  60. }
  61. // Legacy version of the checks, remove when GOOGLE_PROTOBUF_OBJC_GEN_VERSION
  62. // goes away (see more info in GPBBootstrap.h).
  63. void GPBCheckRuntimeVersionInternal(int32_t version);
  64. GPB_INLINE void GPBDebugCheckRuntimeVersion() {
  65. #if defined(DEBUG) && DEBUG
  66. GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
  67. #endif
  68. }
  69. // Conversion functions for de/serializing floating point types.
  70. GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
  71. GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
  72. int64_t result;
  73. memcpy(&result, &v, sizeof(result));
  74. return result;
  75. }
  76. GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
  77. GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
  78. int32_t result;
  79. memcpy(&result, &v, sizeof(result));
  80. return result;
  81. }
  82. GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
  83. GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
  84. double result;
  85. memcpy(&result, &v, sizeof(result));
  86. return result;
  87. }
  88. GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
  89. GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
  90. float result;
  91. memcpy(&result, &v, sizeof(result));
  92. return result;
  93. }
  94. GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
  95. return (int32_t)((uint32_t)(value) >> spaces);
  96. }
  97. GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
  98. return (int64_t)((uint64_t)(value) >> spaces);
  99. }
  100. // Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  101. // into values that can be efficiently encoded with varint. (Otherwise,
  102. // negative values must be sign-extended to 64 bits to be varint encoded,
  103. // thus always taking 10 bytes on the wire.)
  104. GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
  105. return (int32_t)(GPBLogicalRightShift32((int32_t)n, 1) ^ -((int32_t)(n) & 1));
  106. }
  107. // Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  108. // into values that can be efficiently encoded with varint. (Otherwise,
  109. // negative values must be sign-extended to 64 bits to be varint encoded,
  110. // thus always taking 10 bytes on the wire.)
  111. GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
  112. return (int64_t)(GPBLogicalRightShift64((int64_t)n, 1) ^ -((int64_t)(n) & 1));
  113. }
  114. // Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  115. // into values that can be efficiently encoded with varint. (Otherwise,
  116. // negative values must be sign-extended to 64 bits to be varint encoded,
  117. // thus always taking 10 bytes on the wire.)
  118. GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
  119. // Note: the right-shift must be arithmetic
  120. return ((uint32_t)n << 1) ^ (uint32_t)(n >> 31);
  121. }
  122. // Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  123. // into values that can be efficiently encoded with varint. (Otherwise,
  124. // negative values must be sign-extended to 64 bits to be varint encoded,
  125. // thus always taking 10 bytes on the wire.)
  126. GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
  127. // Note: the right-shift must be arithmetic
  128. return ((uint64_t)n << 1) ^ (uint64_t)(n >> 63);
  129. }
  130. #pragma clang diagnostic push
  131. #pragma clang diagnostic ignored "-Wswitch-enum"
  132. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  133. GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
  134. switch (type) {
  135. case GPBDataTypeBytes:
  136. case GPBDataTypeString:
  137. case GPBDataTypeMessage:
  138. case GPBDataTypeGroup:
  139. return YES;
  140. default:
  141. return NO;
  142. }
  143. }
  144. GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
  145. switch (type) {
  146. case GPBDataTypeMessage:
  147. case GPBDataTypeGroup:
  148. return YES;
  149. default:
  150. return NO;
  151. }
  152. }
  153. GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
  154. return GPBDataTypeIsMessage(field->description_->dataType);
  155. }
  156. GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
  157. return GPBDataTypeIsObject(field->description_->dataType);
  158. }
  159. GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
  160. return GPBDataTypeIsMessage(ext->description_->dataType);
  161. }
  162. // The field is an array/map or it has an object value.
  163. GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
  164. GPBMessageFieldDescription *desc = field->description_;
  165. if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
  166. return YES;
  167. }
  168. return GPBDataTypeIsObject(desc->dataType);
  169. }
  170. BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
  171. void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
  172. BOOL value);
  173. uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
  174. GPB_INLINE BOOL
  175. GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
  176. GPBMessageFieldDescription *fieldDesc = field->description_;
  177. return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
  178. }
  179. GPB_INLINE void GPBSetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field,
  180. BOOL value) {
  181. GPBMessageFieldDescription *fieldDesc = field->description_;
  182. GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, value);
  183. }
  184. void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
  185. int32_t oneofHasIndex, uint32_t fieldNumberNotToClear);
  186. #pragma clang diagnostic pop
  187. //%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
  188. //%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self,
  189. //% NAME$S GPBFieldDescriptor *field,
  190. //% NAME$S TYPE value,
  191. //% NAME$S GPBFileSyntax syntax);
  192. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
  193. // This block of code is generated, do not edit it directly.
  194. // clang-format off
  195. void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
  196. GPBFieldDescriptor *field,
  197. BOOL value,
  198. GPBFileSyntax syntax);
  199. // clang-format on
  200. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
  201. // This block of code is generated, do not edit it directly.
  202. // clang-format off
  203. void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
  204. GPBFieldDescriptor *field,
  205. int32_t value,
  206. GPBFileSyntax syntax);
  207. // clang-format on
  208. //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
  209. // This block of code is generated, do not edit it directly.
  210. // clang-format off
  211. void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
  212. GPBFieldDescriptor *field,
  213. uint32_t value,
  214. GPBFileSyntax syntax);
  215. // clang-format on
  216. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
  217. // This block of code is generated, do not edit it directly.
  218. // clang-format off
  219. void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
  220. GPBFieldDescriptor *field,
  221. int64_t value,
  222. GPBFileSyntax syntax);
  223. // clang-format on
  224. //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
  225. // This block of code is generated, do not edit it directly.
  226. // clang-format off
  227. void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
  228. GPBFieldDescriptor *field,
  229. uint64_t value,
  230. GPBFileSyntax syntax);
  231. // clang-format on
  232. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
  233. // This block of code is generated, do not edit it directly.
  234. // clang-format off
  235. void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
  236. GPBFieldDescriptor *field,
  237. float value,
  238. GPBFileSyntax syntax);
  239. // clang-format on
  240. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
  241. // This block of code is generated, do not edit it directly.
  242. // clang-format off
  243. void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
  244. GPBFieldDescriptor *field,
  245. double value,
  246. GPBFileSyntax syntax);
  247. // clang-format on
  248. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Enum, int32_t)
  249. // This block of code is generated, do not edit it directly.
  250. // clang-format off
  251. void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
  252. GPBFieldDescriptor *field,
  253. int32_t value,
  254. GPBFileSyntax syntax);
  255. // clang-format on
  256. //%PDDM-EXPAND-END (8 expansions)
  257. int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
  258. GPBFieldDescriptor *field,
  259. GPBFileSyntax syntax);
  260. id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  261. void GPBSetObjectIvarWithFieldInternal(GPBMessage *self,
  262. GPBFieldDescriptor *field, id value,
  263. GPBFileSyntax syntax);
  264. void GPBSetRetainedObjectIvarWithFieldInternal(GPBMessage *self,
  265. GPBFieldDescriptor *field,
  266. id __attribute__((ns_consumed))
  267. value,
  268. GPBFileSyntax syntax);
  269. // GPBGetObjectIvarWithField will automatically create the field (message) if
  270. // it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
  271. id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
  272. GPBFieldDescriptor *field);
  273. void GPBSetAutocreatedRetainedObjectIvarWithField(
  274. GPBMessage *self, GPBFieldDescriptor *field,
  275. id __attribute__((ns_consumed)) value);
  276. // Clears and releases the autocreated message ivar, if it's autocreated. If
  277. // it's not set as autocreated, this method does nothing.
  278. void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self,
  279. GPBFieldDescriptor *field);
  280. // Returns an Objective C encoding for |selector|. |instanceSel| should be
  281. // YES if it's an instance selector (as opposed to a class selector).
  282. // |selector| must be a selector from MessageSignatureProtocol.
  283. const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
  284. // Helper for text format name encoding.
  285. // decodeData is the data describing the sepecial decodes.
  286. // key and inputString are the input that needs decoding.
  287. NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key,
  288. NSString *inputString);
  289. // A series of selectors that are used solely to get @encoding values
  290. // for them by the dynamic protobuf runtime code. See
  291. // GPBMessageEncodingForSelector for details. GPBRootObject conforms to
  292. // the protocol so that it is encoded in the Objective C runtime.
  293. @protocol GPBMessageSignatureProtocol
  294. @optional
  295. #define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
  296. -(TYPE)get##NAME; \
  297. -(void)set##NAME : (TYPE)value; \
  298. -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
  299. GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
  300. GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
  301. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
  302. GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
  303. GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
  304. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
  305. GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
  306. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
  307. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
  308. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
  309. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
  310. GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
  311. GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
  312. GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
  313. GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
  314. GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
  315. GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
  316. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
  317. #undef GPB_MESSAGE_SIGNATURE_ENTRY
  318. - (id)getArray;
  319. - (NSUInteger)getArrayCount;
  320. - (void)setArray:(NSArray *)array;
  321. + (id)getClassValue;
  322. @end
  323. BOOL GPBClassHasSel(Class aClass, SEL sel);
  324. CF_EXTERN_C_END