GPBUtilities_PackagePrivate.h 13 KB

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