GPBUtilities_PackagePrivate.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import <Foundation/Foundation.h>
  8. #import "GPBUtilities.h"
  9. #import "GPBDescriptor_PackagePrivate.h"
  10. // Macros for stringifying library symbols. These are used in the generated
  11. // GPB descriptor classes wherever a library symbol name is represented as a
  12. // string.
  13. #define GPBStringify(S) #S
  14. #define GPBStringifySymbol(S) GPBStringify(S)
  15. #define GPBNSStringify(S) @ #S
  16. #define GPBNSStringifySymbol(S) GPBNSStringify(S)
  17. // Macros for generating a Class from a class name. These are used in
  18. // the generated GPB descriptor classes wherever an Objective C class
  19. // reference is needed for a generated class.
  20. #define GPBObjCClassSymbol(name) OBJC_CLASS_$_##name
  21. #define GPBObjCClass(name) ((__bridge Class) & (GPBObjCClassSymbol(name)))
  22. #define GPBObjCClassDeclaration(name) extern const GPBObjcClass_t GPBObjCClassSymbol(name)
  23. // Constant to internally mark when there is no has bit.
  24. #define GPBNoHasBit INT32_MAX
  25. CF_EXTERN_C_BEGIN
  26. // These two are used to inject a runtime check for version mismatch into the
  27. // generated sources to make sure they are linked with a supporting runtime.
  28. void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion);
  29. GPB_INLINE void GPB_DEBUG_CHECK_RUNTIME_VERSIONS(void) {
  30. // NOTE: By being inline here, this captures the value from the library's
  31. // headers at the time the generated code was compiled.
  32. #if defined(DEBUG) && DEBUG
  33. GPBCheckRuntimeVersionSupport(GOOGLE_PROTOBUF_OBJC_VERSION);
  34. #endif
  35. }
  36. // Helper called within the library when the runtime detects something that
  37. // indicates a older runtime is being used with newer generated code. Normally
  38. // GPB_DEBUG_CHECK_RUNTIME_VERSIONS() gates this with a better message; this
  39. // is just a final safety net to prevent otherwise hard to diagnose errors.
  40. void GPBRuntimeMatchFailure(void);
  41. // Legacy version of the checks, remove when GOOGLE_PROTOBUF_OBJC_GEN_VERSION
  42. // goes away (see more info in GPBBootstrap.h).
  43. void GPBCheckRuntimeVersionInternal(int32_t version);
  44. GPB_INLINE void GPBDebugCheckRuntimeVersion(void) {
  45. #if defined(DEBUG) && DEBUG
  46. GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
  47. #endif
  48. }
  49. // Conversion functions for de/serializing floating point types.
  50. GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
  51. GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
  52. int64_t result;
  53. memcpy(&result, &v, sizeof(result));
  54. return result;
  55. }
  56. GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
  57. GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
  58. int32_t result;
  59. memcpy(&result, &v, sizeof(result));
  60. return result;
  61. }
  62. GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
  63. GPBInternalCompileAssert(sizeof(double) == sizeof(int64_t), double_not_64_bits);
  64. double result;
  65. memcpy(&result, &v, sizeof(result));
  66. return result;
  67. }
  68. GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
  69. GPBInternalCompileAssert(sizeof(float) == sizeof(int32_t), float_not_32_bits);
  70. float result;
  71. memcpy(&result, &v, sizeof(result));
  72. return result;
  73. }
  74. GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
  75. return (int32_t)((uint32_t)(value) >> spaces);
  76. }
  77. GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
  78. return (int64_t)((uint64_t)(value) >> spaces);
  79. }
  80. // Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  81. // into values that can be efficiently encoded with varint. (Otherwise,
  82. // negative values must be sign-extended to 64 bits to be varint encoded,
  83. // thus always taking 10 bytes on the wire.)
  84. GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
  85. return (int32_t)(GPBLogicalRightShift32((int32_t)n, 1) ^ -((int32_t)(n) & 1));
  86. }
  87. // Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  88. // into values that can be efficiently encoded with varint. (Otherwise,
  89. // negative values must be sign-extended to 64 bits to be varint encoded,
  90. // thus always taking 10 bytes on the wire.)
  91. GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
  92. return (int64_t)(GPBLogicalRightShift64((int64_t)n, 1) ^ -((int64_t)(n) & 1));
  93. }
  94. // Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  95. // into values that can be efficiently encoded with varint. (Otherwise,
  96. // negative values must be sign-extended to 64 bits to be varint encoded,
  97. // thus always taking 10 bytes on the wire.)
  98. GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
  99. // Note: the right-shift must be arithmetic
  100. return ((uint32_t)n << 1) ^ (uint32_t)(n >> 31);
  101. }
  102. // Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  103. // into values that can be efficiently encoded with varint. (Otherwise,
  104. // negative values must be sign-extended to 64 bits to be varint encoded,
  105. // thus always taking 10 bytes on the wire.)
  106. GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
  107. // Note: the right-shift must be arithmetic
  108. return ((uint64_t)n << 1) ^ (uint64_t)(n >> 63);
  109. }
  110. #pragma clang diagnostic push
  111. #pragma clang diagnostic ignored "-Wswitch-enum"
  112. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  113. GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
  114. switch (type) {
  115. case GPBDataTypeBytes:
  116. case GPBDataTypeString:
  117. case GPBDataTypeMessage:
  118. case GPBDataTypeGroup:
  119. return YES;
  120. default:
  121. return NO;
  122. }
  123. }
  124. GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
  125. switch (type) {
  126. case GPBDataTypeMessage:
  127. case GPBDataTypeGroup:
  128. return YES;
  129. default:
  130. return NO;
  131. }
  132. }
  133. GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
  134. return GPBDataTypeIsMessage(field->description_->dataType);
  135. }
  136. GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
  137. return GPBDataTypeIsObject(field->description_->dataType);
  138. }
  139. GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
  140. return GPBDataTypeIsMessage(ext->description_->dataType);
  141. }
  142. // The field is an array/map or it has an object value.
  143. GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
  144. GPBMessageFieldDescription *desc = field->description_;
  145. if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
  146. return YES;
  147. }
  148. return GPBDataTypeIsObject(desc->dataType);
  149. }
  150. BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
  151. void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber, BOOL value);
  152. uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
  153. GPB_INLINE BOOL GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
  154. GPBMessageFieldDescription *fieldDesc = field->description_;
  155. return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
  156. }
  157. #pragma clang diagnostic pop
  158. // Disable clang-format for the macros.
  159. // clang-format off
  160. //%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
  161. //%void GPBSet##NAME##IvarWithFieldPrivate(GPBMessage *self,
  162. //% NAME$S GPBFieldDescriptor *field,
  163. //% NAME$S TYPE value);
  164. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
  165. // This block of code is generated, do not edit it directly.
  166. void GPBSetBoolIvarWithFieldPrivate(GPBMessage *self,
  167. GPBFieldDescriptor *field,
  168. BOOL value);
  169. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
  170. // This block of code is generated, do not edit it directly.
  171. void GPBSetInt32IvarWithFieldPrivate(GPBMessage *self,
  172. GPBFieldDescriptor *field,
  173. int32_t value);
  174. //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
  175. // This block of code is generated, do not edit it directly.
  176. void GPBSetUInt32IvarWithFieldPrivate(GPBMessage *self,
  177. GPBFieldDescriptor *field,
  178. uint32_t value);
  179. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
  180. // This block of code is generated, do not edit it directly.
  181. void GPBSetInt64IvarWithFieldPrivate(GPBMessage *self,
  182. GPBFieldDescriptor *field,
  183. int64_t value);
  184. //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
  185. // This block of code is generated, do not edit it directly.
  186. void GPBSetUInt64IvarWithFieldPrivate(GPBMessage *self,
  187. GPBFieldDescriptor *field,
  188. uint64_t value);
  189. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
  190. // This block of code is generated, do not edit it directly.
  191. void GPBSetFloatIvarWithFieldPrivate(GPBMessage *self,
  192. GPBFieldDescriptor *field,
  193. float value);
  194. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
  195. // This block of code is generated, do not edit it directly.
  196. void GPBSetDoubleIvarWithFieldPrivate(GPBMessage *self,
  197. GPBFieldDescriptor *field,
  198. double value);
  199. //%PDDM-EXPAND-END (7 expansions)
  200. // clang-format on
  201. void GPBSetEnumIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field, int32_t value);
  202. id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  203. void GPBSetObjectIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field, id value);
  204. void GPBSetRetainedObjectIvarWithFieldPrivate(GPBMessage *self, GPBFieldDescriptor *field,
  205. id __attribute__((ns_consumed)) value);
  206. // GPBGetObjectIvarWithField will automatically create the field (message) if
  207. // it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
  208. id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self, GPBFieldDescriptor *field);
  209. // Clears and releases the autocreated message ivar, if it's autocreated. If
  210. // it's not set as autocreated, this method does nothing.
  211. void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  212. // Returns an Objective C encoding for |selector|. |instanceSel| should be
  213. // YES if it's an instance selector (as opposed to a class selector).
  214. // |selector| must be a selector from MessageSignatureProtocol.
  215. const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
  216. // Helper for text format name encoding.
  217. // decodeData is the data describing the special decodes.
  218. // key and inputString are the input that needs decoding.
  219. NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key, NSString *inputString);
  220. // Shims from the older generated code into the runtime.
  221. void GPBSetInt32IvarWithFieldInternal(GPBMessage *self, GPBFieldDescriptor *field, int32_t value,
  222. GPBFileSyntax syntax);
  223. void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof, int32_t oneofHasIndex,
  224. uint32_t fieldNumberNotToClear);
  225. // A series of selectors that are used solely to get @encoding values
  226. // for them by the dynamic protobuf runtime code. See
  227. // GPBMessageEncodingForSelector for details. GPBRootObject conforms to
  228. // the protocol so that it is encoded in the Objective C runtime.
  229. @protocol GPBMessageSignatureProtocol
  230. @optional
  231. #define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
  232. -(TYPE)get##NAME; \
  233. -(void)set##NAME : (TYPE)value; \
  234. -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
  235. GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
  236. GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
  237. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
  238. GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
  239. GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
  240. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
  241. GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
  242. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
  243. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
  244. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
  245. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
  246. GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
  247. GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
  248. GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
  249. GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
  250. GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
  251. GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
  252. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
  253. #undef GPB_MESSAGE_SIGNATURE_ENTRY
  254. - (id)getArray;
  255. - (NSUInteger)getArrayCount;
  256. - (void)setArray:(NSArray *)array;
  257. + (id)getClassValue;
  258. @end
  259. BOOL GPBClassHasSel(Class aClass, SEL sel);
  260. CF_EXTERN_C_END