GPBUtilities_PackagePrivate.h 13 KB

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