GPBUtilities_PackagePrivate.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // Conversion functions for de/serializing floating point types.
  44. GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
  45. union { double f; int64_t i; } u;
  46. u.f = v;
  47. return u.i;
  48. }
  49. GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
  50. union { float f; int32_t i; } u;
  51. u.f = v;
  52. return u.i;
  53. }
  54. GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
  55. union { double f; int64_t i; } u;
  56. u.i = v;
  57. return u.f;
  58. }
  59. GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
  60. union { float f; int32_t i; } u;
  61. u.i = v;
  62. return u.f;
  63. }
  64. GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
  65. return (int32_t)((uint32_t)(value) >> spaces);
  66. }
  67. GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
  68. return (int64_t)((uint64_t)(value) >> spaces);
  69. }
  70. // Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  71. // into values that can be efficiently encoded with varint. (Otherwise,
  72. // negative values must be sign-extended to 64 bits to be varint encoded,
  73. // thus always taking 10 bytes on the wire.)
  74. GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
  75. return GPBLogicalRightShift32(n, 1) ^ -(n & 1);
  76. }
  77. // Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
  78. // into values that can be efficiently encoded with varint. (Otherwise,
  79. // negative values must be sign-extended to 64 bits to be varint encoded,
  80. // thus always taking 10 bytes on the wire.)
  81. GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
  82. return GPBLogicalRightShift64(n, 1) ^ -(n & 1);
  83. }
  84. // Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
  85. // into values that can be efficiently encoded with varint. (Otherwise,
  86. // negative values must be sign-extended to 64 bits to be varint encoded,
  87. // thus always taking 10 bytes on the wire.)
  88. GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
  89. // Note: the right-shift must be arithmetic
  90. return (n << 1) ^ (n >> 31);
  91. }
  92. // Encode 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 uint64_t GPBEncodeZigZag64(int64_t n) {
  97. // Note: the right-shift must be arithmetic
  98. return (n << 1) ^ (n >> 63);
  99. }
  100. GPB_INLINE BOOL GPBTypeIsObject(GPBType type) {
  101. switch (type) {
  102. case GPBTypeData:
  103. case GPBTypeString:
  104. case GPBTypeMessage:
  105. case GPBTypeGroup:
  106. return YES;
  107. default:
  108. return NO;
  109. }
  110. }
  111. GPB_INLINE BOOL GPBTypeIsMessage(GPBType type) {
  112. switch (type) {
  113. case GPBTypeMessage:
  114. case GPBTypeGroup:
  115. return YES;
  116. default:
  117. return NO;
  118. }
  119. }
  120. GPB_INLINE BOOL GPBTypeIsEnum(GPBType type) { return type == GPBTypeEnum; }
  121. GPB_INLINE BOOL GPBFieldTypeIsMessage(GPBFieldDescriptor *field) {
  122. return GPBTypeIsMessage(field->description_->type);
  123. }
  124. GPB_INLINE BOOL GPBFieldTypeIsObject(GPBFieldDescriptor *field) {
  125. return GPBTypeIsObject(field->description_->type);
  126. }
  127. GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
  128. return GPBTypeIsMessage(ext->description_->type);
  129. }
  130. // The field is an array/map or it has an object value.
  131. GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
  132. GPBMessageFieldDescription *desc = field->description_;
  133. if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
  134. return YES;
  135. }
  136. return GPBTypeIsObject(desc->type);
  137. }
  138. BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
  139. void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
  140. BOOL value);
  141. uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
  142. GPB_INLINE BOOL
  143. GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
  144. GPBMessageFieldDescription *fieldDesc = field->description_;
  145. return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
  146. }
  147. GPB_INLINE void GPBSetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field,
  148. BOOL value) {
  149. GPBMessageFieldDescription *fieldDesc = field->description_;
  150. GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, value);
  151. }
  152. void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
  153. uint32_t fieldNumberNotToClear);
  154. //%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
  155. //%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self,
  156. //% NAME$S GPBFieldDescriptor *field,
  157. //% NAME$S TYPE value,
  158. //% NAME$S GPBFileSyntax syntax);
  159. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
  160. // This block of code is generated, do not edit it directly.
  161. void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
  162. GPBFieldDescriptor *field,
  163. BOOL value,
  164. GPBFileSyntax syntax);
  165. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
  166. // This block of code is generated, do not edit it directly.
  167. void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
  168. GPBFieldDescriptor *field,
  169. int32_t value,
  170. GPBFileSyntax syntax);
  171. //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
  172. // This block of code is generated, do not edit it directly.
  173. void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
  174. GPBFieldDescriptor *field,
  175. uint32_t value,
  176. GPBFileSyntax syntax);
  177. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
  178. // This block of code is generated, do not edit it directly.
  179. void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
  180. GPBFieldDescriptor *field,
  181. int64_t value,
  182. GPBFileSyntax syntax);
  183. //%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
  184. // This block of code is generated, do not edit it directly.
  185. void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
  186. GPBFieldDescriptor *field,
  187. uint64_t value,
  188. GPBFileSyntax syntax);
  189. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
  190. // This block of code is generated, do not edit it directly.
  191. void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
  192. GPBFieldDescriptor *field,
  193. float value,
  194. GPBFileSyntax syntax);
  195. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
  196. // This block of code is generated, do not edit it directly.
  197. void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
  198. GPBFieldDescriptor *field,
  199. double value,
  200. GPBFileSyntax syntax);
  201. //%PDDM-EXPAND GPB_IVAR_SET_DECL(Enum, int32_t)
  202. // This block of code is generated, do not edit it directly.
  203. void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
  204. GPBFieldDescriptor *field,
  205. int32_t value,
  206. GPBFileSyntax syntax);
  207. //%PDDM-EXPAND-END (8 expansions)
  208. int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
  209. GPBFieldDescriptor *field,
  210. GPBFileSyntax syntax);
  211. id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  212. void GPBSetObjectIvarWithFieldInternal(GPBMessage *self,
  213. GPBFieldDescriptor *field, id value,
  214. GPBFileSyntax syntax);
  215. void GPBSetRetainedObjectIvarWithFieldInternal(GPBMessage *self,
  216. GPBFieldDescriptor *field,
  217. id __attribute__((ns_consumed))
  218. value,
  219. GPBFileSyntax syntax);
  220. // GPBGetObjectIvarWithField will automatically create the field (message) if
  221. // it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
  222. id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
  223. GPBFieldDescriptor *field);
  224. void GPBSetAutocreatedRetainedObjectIvarWithField(
  225. GPBMessage *self, GPBFieldDescriptor *field,
  226. id __attribute__((ns_consumed)) value);
  227. // Clears and releases the autocreated message ivar, if it's autocreated. If
  228. // it's not set as autocreated, this method does nothing.
  229. void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self,
  230. GPBFieldDescriptor *field);
  231. // Utilities for applying various functions based on Objective C types.
  232. // A basic functor that is passed a field and a context. Returns YES
  233. // if the calling function should continue processing, and NO if the calling
  234. // function should stop processing.
  235. typedef BOOL (*GPBApplyFunction)(GPBFieldDescriptor *field, void *context);
  236. // Functions called for various types. See ApplyFunctionsToMessageFields.
  237. typedef enum {
  238. GPBApplyFunctionObject,
  239. GPBApplyFunctionBool,
  240. GPBApplyFunctionInt32,
  241. GPBApplyFunctionUInt32,
  242. GPBApplyFunctionInt64,
  243. GPBApplyFunctionUInt64,
  244. GPBApplyFunctionFloat,
  245. GPBApplyFunctionDouble,
  246. } GPBApplyFunctionOrder;
  247. enum {
  248. // A count of the number of types in GPBApplyFunctionOrder. Separated out
  249. // from the GPBApplyFunctionOrder enum to avoid warnings regarding not
  250. // handling GPBApplyFunctionCount in switch statements.
  251. GPBApplyFunctionCount = GPBApplyFunctionDouble + 1
  252. };
  253. typedef GPBApplyFunction GPBApplyFunctions[GPBApplyFunctionCount];
  254. // Functions called for various types.
  255. // See ApplyStrictFunctionsToMessageFields.
  256. // They are in the same order as the GPBTypes enum.
  257. typedef GPBApplyFunction GPBApplyStrictFunctions[GPBTypeCount];
  258. // A macro for easily initializing a GPBApplyFunctions struct
  259. // GPBApplyFunctions foo = GPBAPPLY_FUNCTIONS_INIT(Foo);
  260. #define GPBAPPLY_FUNCTIONS_INIT(PREFIX) \
  261. { \
  262. PREFIX##Object, \
  263. PREFIX##Bool, \
  264. PREFIX##Int32, \
  265. PREFIX##UInt32, \
  266. PREFIX##Int64, \
  267. PREFIX##UInt64, \
  268. PREFIX##Float, \
  269. PREFIX##Double, \
  270. }
  271. // A macro for easily initializing a GPBApplyStrictFunctions struct
  272. // GPBApplyStrictFunctions foo = GPBAPPLY_STRICT_FUNCTIONS_INIT(Foo);
  273. // These need to stay in the same order as
  274. // the GPBType enum.
  275. #define GPBAPPLY_STRICT_FUNCTIONS_INIT(PREFIX) \
  276. { \
  277. PREFIX##Bool, \
  278. PREFIX##Fixed32, \
  279. PREFIX##SFixed32, \
  280. PREFIX##Float, \
  281. PREFIX##Fixed64, \
  282. PREFIX##SFixed64, \
  283. PREFIX##Double, \
  284. PREFIX##Int32, \
  285. PREFIX##Int64, \
  286. PREFIX##SInt32, \
  287. PREFIX##SInt64, \
  288. PREFIX##UInt32, \
  289. PREFIX##UInt64, \
  290. PREFIX##Data, \
  291. PREFIX##String, \
  292. PREFIX##Message, \
  293. PREFIX##Group, \
  294. PREFIX##Enum, \
  295. }
  296. // Iterates over the fields of a proto |msg| and applies the functions in
  297. // |functions| to them with |context|. If one of the functions in |functions|
  298. // returns NO, it will return immediately and not process the rest of the
  299. // ivars. The types in the fields are mapped so:
  300. // Int32, Enum, SInt32 and SFixed32 will be mapped to the int32Function,
  301. // UInt32 and Fixed32 will be mapped to the uint32Function,
  302. // Bytes, String, Message and Group will be mapped to the objectFunction,
  303. // etc..
  304. // If you require more specific mappings look at
  305. // GPBApplyStrictFunctionsToMessageFields.
  306. void GPBApplyFunctionsToMessageFields(GPBApplyFunctions *functions,
  307. GPBMessage *msg, void *context);
  308. // Iterates over the fields of a proto |msg| and applies the functions in
  309. // |functions| to them with |context|. If one of the functions in |functions|
  310. // returns NO, it will return immediately and not process the rest of the
  311. // ivars. The types in the fields are mapped directly:
  312. // Int32 -> functions[GPBTypeInt32],
  313. // SFixed32 -> functions[GPBTypeSFixed32],
  314. // etc...
  315. // If you can use looser mappings look at GPBApplyFunctionsToMessageFields.
  316. void GPBApplyStrictFunctionsToMessageFields(GPBApplyStrictFunctions *functions,
  317. GPBMessage *msg, void *context);
  318. // Applies the appropriate function in |functions| based on |field|.
  319. // Returns the value from function(name, context).
  320. // Throws an exception if the type is unrecognized.
  321. BOOL GPBApplyFunctionsBasedOnField(GPBFieldDescriptor *field,
  322. GPBApplyFunctions *functions, void *context);
  323. // Returns an Objective C encoding for |selector|. |instanceSel| should be
  324. // YES if it's an instance selector (as opposed to a class selector).
  325. // |selector| must be a selector from MessageSignatureProtocol.
  326. const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
  327. // Helper for text format name encoding.
  328. // decodeData is the data describing the sepecial decodes.
  329. // key and inputString are the input that needs decoding.
  330. NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key,
  331. NSString *inputString);
  332. // A series of selectors that are used solely to get @encoding values
  333. // for them by the dynamic protobuf runtime code. See
  334. // GPBMessageEncodingForSelector for details.
  335. @protocol GPBMessageSignatureProtocol
  336. @optional
  337. #define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
  338. -(TYPE)get##NAME; \
  339. -(void)set##NAME : (TYPE)value; \
  340. -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
  341. GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
  342. GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
  343. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
  344. GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
  345. GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
  346. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
  347. GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
  348. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
  349. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
  350. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
  351. GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
  352. GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
  353. GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
  354. GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Data)
  355. GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
  356. GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
  357. GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
  358. GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
  359. #undef GPB_MESSAGE_SIGNATURE_ENTRY
  360. - (id)getArray;
  361. - (void)setArray:(NSArray *)array;
  362. + (id)getClassValue;
  363. @end
  364. CF_EXTERN_C_END