GPBUtilities.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 "GPBArray.h"
  9. #import "GPBDescriptor.h"
  10. #import "GPBMessage.h"
  11. #import "GPBRuntimeTypes.h"
  12. CF_EXTERN_C_BEGIN
  13. NS_ASSUME_NONNULL_BEGIN
  14. /**
  15. * Generates a string that should be a valid "TextFormat" for the C++ version
  16. * of Protocol Buffers.
  17. *
  18. * @param message The message to generate from.
  19. * @param lineIndent A string to use as the prefix for all lines generated. Can
  20. * be nil if no extra indent is needed.
  21. *
  22. * @return An NSString with the TextFormat of the message.
  23. **/
  24. NSString *GPBTextFormatForMessage(GPBMessage *message, NSString *__nullable lineIndent);
  25. /**
  26. * Generates a string that should be a valid "TextFormat" for the C++ version
  27. * of Protocol Buffers.
  28. *
  29. * @param unknownSet The unknown field set to generate from.
  30. * @param lineIndent A string to use as the prefix for all lines generated. Can
  31. * be nil if no extra indent is needed.
  32. *
  33. * @return An NSString with the TextFormat of the unknown field set.
  34. **/
  35. NSString *GPBTextFormatForUnknownFieldSet(GPBUnknownFieldSet *__nullable unknownSet,
  36. NSString *__nullable lineIndent)
  37. __attribute__((deprecated("Use GPBTextFormatForMessage will include the unknown fields, and "
  38. "GPBUnknownFieldSet it going away.")));
  39. /**
  40. * Checks if the given field number is set on a message.
  41. *
  42. * @param self The message to check.
  43. * @param fieldNumber The field number to check.
  44. *
  45. * @return YES if the field number is set on the given message.
  46. **/
  47. BOOL GPBMessageHasFieldNumberSet(GPBMessage *self, uint32_t fieldNumber);
  48. /**
  49. * Checks if the given field is set on a message.
  50. *
  51. * @param self The message to check.
  52. * @param field The field to check.
  53. *
  54. * @return YES if the field is set on the given message.
  55. **/
  56. BOOL GPBMessageHasFieldSet(GPBMessage *self, GPBFieldDescriptor *field);
  57. /**
  58. * Clears the given field for the given message.
  59. *
  60. * @param self The message for which to clear the field.
  61. * @param field The field to clear.
  62. **/
  63. void GPBClearMessageField(GPBMessage *self, GPBFieldDescriptor *field);
  64. /**
  65. * Clears the given oneof field for the given message.
  66. *
  67. * @param self The message for which to clear the field.
  68. * @param oneof The oneof to clear.
  69. **/
  70. void GPBClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof);
  71. // Disable clang-format for the macros.
  72. // clang-format off
  73. //%PDDM-EXPAND GPB_ACCESSORS()
  74. // This block of code is generated, do not edit it directly.
  75. //
  76. // Get/Set a given field from/to a message.
  77. //
  78. // Single Fields
  79. /**
  80. * Gets the value of a bytes field.
  81. *
  82. * @param self The message from which to get the field.
  83. * @param field The field to get.
  84. **/
  85. NSData *GPBGetMessageBytesField(GPBMessage *self, GPBFieldDescriptor *field);
  86. /**
  87. * Sets the value of a bytes field.
  88. *
  89. * @param self The message into which to set the field.
  90. * @param field The field to set.
  91. * @param value The to set in the field.
  92. **/
  93. void GPBSetMessageBytesField(GPBMessage *self, GPBFieldDescriptor *field, NSData *value);
  94. /**
  95. * Gets the value of a string field.
  96. *
  97. * @param self The message from which to get the field.
  98. * @param field The field to get.
  99. **/
  100. NSString *GPBGetMessageStringField(GPBMessage *self, GPBFieldDescriptor *field);
  101. /**
  102. * Sets the value of a string field.
  103. *
  104. * @param self The message into which to set the field.
  105. * @param field The field to set.
  106. * @param value The to set in the field.
  107. **/
  108. void GPBSetMessageStringField(GPBMessage *self, GPBFieldDescriptor *field, NSString *value);
  109. /**
  110. * Gets the value of a message field.
  111. *
  112. * @param self The message from which to get the field.
  113. * @param field The field to get.
  114. **/
  115. GPBMessage *GPBGetMessageMessageField(GPBMessage *self, GPBFieldDescriptor *field);
  116. /**
  117. * Sets the value of a message field.
  118. *
  119. * @param self The message into which to set the field.
  120. * @param field The field to set.
  121. * @param value The to set in the field.
  122. **/
  123. void GPBSetMessageMessageField(GPBMessage *self, GPBFieldDescriptor *field, GPBMessage *value);
  124. /**
  125. * Gets the value of a group field.
  126. *
  127. * @param self The message from which to get the field.
  128. * @param field The field to get.
  129. **/
  130. GPBMessage *GPBGetMessageGroupField(GPBMessage *self, GPBFieldDescriptor *field);
  131. /**
  132. * Sets the value of a group field.
  133. *
  134. * @param self The message into which to set the field.
  135. * @param field The field to set.
  136. * @param value The to set in the field.
  137. **/
  138. void GPBSetMessageGroupField(GPBMessage *self, GPBFieldDescriptor *field, GPBMessage *value);
  139. /**
  140. * Gets the value of a bool field.
  141. *
  142. * @param self The message from which to get the field.
  143. * @param field The field to get.
  144. **/
  145. BOOL GPBGetMessageBoolField(GPBMessage *self, GPBFieldDescriptor *field);
  146. /**
  147. * Sets the value of a bool field.
  148. *
  149. * @param self The message into which to set the field.
  150. * @param field The field to set.
  151. * @param value The to set in the field.
  152. **/
  153. void GPBSetMessageBoolField(GPBMessage *self, GPBFieldDescriptor *field, BOOL value);
  154. /**
  155. * Gets the value of an int32 field.
  156. *
  157. * @param self The message from which to get the field.
  158. * @param field The field to get.
  159. **/
  160. int32_t GPBGetMessageInt32Field(GPBMessage *self, GPBFieldDescriptor *field);
  161. /**
  162. * Sets the value of an int32 field.
  163. *
  164. * @param self The message into which to set the field.
  165. * @param field The field to set.
  166. * @param value The to set in the field.
  167. **/
  168. void GPBSetMessageInt32Field(GPBMessage *self, GPBFieldDescriptor *field, int32_t value);
  169. /**
  170. * Gets the value of an uint32 field.
  171. *
  172. * @param self The message from which to get the field.
  173. * @param field The field to get.
  174. **/
  175. uint32_t GPBGetMessageUInt32Field(GPBMessage *self, GPBFieldDescriptor *field);
  176. /**
  177. * Sets the value of an uint32 field.
  178. *
  179. * @param self The message into which to set the field.
  180. * @param field The field to set.
  181. * @param value The to set in the field.
  182. **/
  183. void GPBSetMessageUInt32Field(GPBMessage *self, GPBFieldDescriptor *field, uint32_t value);
  184. /**
  185. * Gets the value of an int64 field.
  186. *
  187. * @param self The message from which to get the field.
  188. * @param field The field to get.
  189. **/
  190. int64_t GPBGetMessageInt64Field(GPBMessage *self, GPBFieldDescriptor *field);
  191. /**
  192. * Sets the value of an int64 field.
  193. *
  194. * @param self The message into which to set the field.
  195. * @param field The field to set.
  196. * @param value The to set in the field.
  197. **/
  198. void GPBSetMessageInt64Field(GPBMessage *self, GPBFieldDescriptor *field, int64_t value);
  199. /**
  200. * Gets the value of an uint64 field.
  201. *
  202. * @param self The message from which to get the field.
  203. * @param field The field to get.
  204. **/
  205. uint64_t GPBGetMessageUInt64Field(GPBMessage *self, GPBFieldDescriptor *field);
  206. /**
  207. * Sets the value of an uint64 field.
  208. *
  209. * @param self The message into which to set the field.
  210. * @param field The field to set.
  211. * @param value The to set in the field.
  212. **/
  213. void GPBSetMessageUInt64Field(GPBMessage *self, GPBFieldDescriptor *field, uint64_t value);
  214. /**
  215. * Gets the value of a float field.
  216. *
  217. * @param self The message from which to get the field.
  218. * @param field The field to get.
  219. **/
  220. float GPBGetMessageFloatField(GPBMessage *self, GPBFieldDescriptor *field);
  221. /**
  222. * Sets the value of a float field.
  223. *
  224. * @param self The message into which to set the field.
  225. * @param field The field to set.
  226. * @param value The to set in the field.
  227. **/
  228. void GPBSetMessageFloatField(GPBMessage *self, GPBFieldDescriptor *field, float value);
  229. /**
  230. * Gets the value of a double field.
  231. *
  232. * @param self The message from which to get the field.
  233. * @param field The field to get.
  234. **/
  235. double GPBGetMessageDoubleField(GPBMessage *self, GPBFieldDescriptor *field);
  236. /**
  237. * Sets the value of a double field.
  238. *
  239. * @param self The message into which to set the field.
  240. * @param field The field to set.
  241. * @param value The to set in the field.
  242. **/
  243. void GPBSetMessageDoubleField(GPBMessage *self, GPBFieldDescriptor *field, double value);
  244. /**
  245. * Gets the given enum field of a message. For proto3, if the value isn't a
  246. * member of the enum, @c kGPBUnrecognizedEnumeratorValue will be returned.
  247. * GPBGetMessageRawEnumField will bypass the check and return whatever value
  248. * was set.
  249. *
  250. * @param self The message from which to get the field.
  251. * @param field The field to get.
  252. *
  253. * @return The enum value for the given field.
  254. **/
  255. int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field);
  256. /**
  257. * Set the given enum field of a message. You can only set values that are
  258. * members of the enum.
  259. *
  260. * @param self The message into which to set the field.
  261. * @param field The field to set.
  262. * @param value The enum value to set in the field.
  263. **/
  264. void GPBSetMessageEnumField(GPBMessage *self,
  265. GPBFieldDescriptor *field,
  266. int32_t value);
  267. /**
  268. * Get the given enum field of a message. No check is done to ensure the value
  269. * was defined in the enum.
  270. *
  271. * @param self The message from which to get the field.
  272. * @param field The field to get.
  273. *
  274. * @return The raw enum value for the given field.
  275. **/
  276. int32_t GPBGetMessageRawEnumField(GPBMessage *self, GPBFieldDescriptor *field);
  277. /**
  278. * Set the given enum field of a message. You can set the value to anything,
  279. * even a value that is not a member of the enum.
  280. *
  281. * @param self The message into which to set the field.
  282. * @param field The field to set.
  283. * @param value The raw enum value to set in the field.
  284. **/
  285. void GPBSetMessageRawEnumField(GPBMessage *self,
  286. GPBFieldDescriptor *field,
  287. int32_t value);
  288. // Repeated Fields
  289. /**
  290. * Gets the value of a repeated field.
  291. *
  292. * @param self The message from which to get the field.
  293. * @param field The repeated field to get.
  294. *
  295. * @return A GPB*Array or an NSMutableArray based on the field's type.
  296. **/
  297. id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field);
  298. /**
  299. * Sets the value of a repeated field.
  300. *
  301. * @param self The message into which to set the field.
  302. * @param field The field to set.
  303. * @param array A GPB*Array or NSMutableArray based on the field's type.
  304. **/
  305. void GPBSetMessageRepeatedField(GPBMessage *self,
  306. GPBFieldDescriptor *field,
  307. id array);
  308. // Map Fields
  309. /**
  310. * Gets the value of a map<> field.
  311. *
  312. * @param self The message from which to get the field.
  313. * @param field The repeated field to get.
  314. *
  315. * @return A GPB*Dictionary or NSMutableDictionary based on the field's type.
  316. **/
  317. id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field);
  318. /**
  319. * Sets the value of a map<> field.
  320. *
  321. * @param self The message into which to set the field.
  322. * @param field The field to set.
  323. * @param dictionary A GPB*Dictionary or NSMutableDictionary based on the
  324. * field's type.
  325. **/
  326. void GPBSetMessageMapField(GPBMessage *self,
  327. GPBFieldDescriptor *field,
  328. id dictionary);
  329. //%PDDM-EXPAND-END GPB_ACCESSORS()
  330. // clang-format on
  331. /**
  332. * Returns an empty NSData to assign to byte fields when you wish to assign them
  333. * to empty. Prevents allocating a lot of little [NSData data] objects.
  334. **/
  335. NSData *GPBEmptyNSData(void) __attribute__((pure));
  336. /**
  337. * Drops the `unknownFields` from the given message and from all sub message.
  338. **/
  339. void GPBMessageDropUnknownFieldsRecursively(GPBMessage *message);
  340. NS_ASSUME_NONNULL_END
  341. CF_EXTERN_C_END
  342. // Disable clang-format for the macros.
  343. // clang-format off
  344. //%PDDM-DEFINE GPB_ACCESSORS()
  345. //%
  346. //%//
  347. //%// Get/Set a given field from/to a message.
  348. //%//
  349. //%
  350. //%// Single Fields
  351. //%
  352. //%GPB_ACCESSOR_SINGLE_FULL(Bytes, NSData, , *)
  353. //%GPB_ACCESSOR_SINGLE_FULL(String, NSString, , *)
  354. //%GPB_ACCESSOR_SINGLE_FULL(Message, GPBMessage, , *)
  355. //%GPB_ACCESSOR_SINGLE_FULL(Group, GPBMessage, , *)
  356. //%GPB_ACCESSOR_SINGLE(Bool, BOOL, )
  357. //%GPB_ACCESSOR_SINGLE(Int32, int32_t, n)
  358. //%GPB_ACCESSOR_SINGLE(UInt32, uint32_t, n)
  359. //%GPB_ACCESSOR_SINGLE(Int64, int64_t, n)
  360. //%GPB_ACCESSOR_SINGLE(UInt64, uint64_t, n)
  361. //%GPB_ACCESSOR_SINGLE(Float, float, )
  362. //%GPB_ACCESSOR_SINGLE(Double, double, )
  363. //%/**
  364. //% * Gets the given enum field of a message. For proto3, if the value isn't a
  365. //% * member of the enum, @c kGPBUnrecognizedEnumeratorValue will be returned.
  366. //% * GPBGetMessageRawEnumField will bypass the check and return whatever value
  367. //% * was set.
  368. //% *
  369. //% * @param self The message from which to get the field.
  370. //% * @param field The field to get.
  371. //% *
  372. //% * @return The enum value for the given field.
  373. //% **/
  374. //%int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field);
  375. //%
  376. //%/**
  377. //% * Set the given enum field of a message. You can only set values that are
  378. //% * members of the enum.
  379. //% *
  380. //% * @param self The message into which to set the field.
  381. //% * @param field The field to set.
  382. //% * @param value The enum value to set in the field.
  383. //% **/
  384. //%void GPBSetMessageEnumField(GPBMessage *self,
  385. //% GPBFieldDescriptor *field,
  386. //% int32_t value);
  387. //%
  388. //%/**
  389. //% * Get the given enum field of a message. No check is done to ensure the value
  390. //% * was defined in the enum.
  391. //% *
  392. //% * @param self The message from which to get the field.
  393. //% * @param field The field to get.
  394. //% *
  395. //% * @return The raw enum value for the given field.
  396. //% **/
  397. //%int32_t GPBGetMessageRawEnumField(GPBMessage *self, GPBFieldDescriptor *field);
  398. //%
  399. //%/**
  400. //% * Set the given enum field of a message. You can set the value to anything,
  401. //% * even a value that is not a member of the enum.
  402. //% *
  403. //% * @param self The message into which to set the field.
  404. //% * @param field The field to set.
  405. //% * @param value The raw enum value to set in the field.
  406. //% **/
  407. //%void GPBSetMessageRawEnumField(GPBMessage *self,
  408. //% GPBFieldDescriptor *field,
  409. //% int32_t value);
  410. //%
  411. //%// Repeated Fields
  412. //%
  413. //%/**
  414. //% * Gets the value of a repeated field.
  415. //% *
  416. //% * @param self The message from which to get the field.
  417. //% * @param field The repeated field to get.
  418. //% *
  419. //% * @return A GPB*Array or an NSMutableArray based on the field's type.
  420. //% **/
  421. //%id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field);
  422. //%
  423. //%/**
  424. //% * Sets the value of a repeated field.
  425. //% *
  426. //% * @param self The message into which to set the field.
  427. //% * @param field The field to set.
  428. //% * @param array A GPB*Array or NSMutableArray based on the field's type.
  429. //% **/
  430. //%void GPBSetMessageRepeatedField(GPBMessage *self,
  431. //% GPBFieldDescriptor *field,
  432. //% id array);
  433. //%
  434. //%// Map Fields
  435. //%
  436. //%/**
  437. //% * Gets the value of a map<> field.
  438. //% *
  439. //% * @param self The message from which to get the field.
  440. //% * @param field The repeated field to get.
  441. //% *
  442. //% * @return A GPB*Dictionary or NSMutableDictionary based on the field's type.
  443. //% **/
  444. //%id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field);
  445. //%
  446. //%/**
  447. //% * Sets the value of a map<> field.
  448. //% *
  449. //% * @param self The message into which to set the field.
  450. //% * @param field The field to set.
  451. //% * @param dictionary A GPB*Dictionary or NSMutableDictionary based on the
  452. //% * field's type.
  453. //% **/
  454. //%void GPBSetMessageMapField(GPBMessage *self,
  455. //% GPBFieldDescriptor *field,
  456. //% id dictionary);
  457. //%
  458. //%PDDM-DEFINE GPB_ACCESSOR_SINGLE(NAME, TYPE, AN)
  459. //%GPB_ACCESSOR_SINGLE_FULL(NAME, TYPE, AN, )
  460. //%PDDM-DEFINE GPB_ACCESSOR_SINGLE_FULL(NAME, TYPE, AN, TisP)
  461. //%/**
  462. //% * Gets the value of a##AN NAME$L field.
  463. //% *
  464. //% * @param self The message from which to get the field.
  465. //% * @param field The field to get.
  466. //% **/
  467. //%TYPE TisP##GPBGetMessage##NAME##Field(GPBMessage *self, GPBFieldDescriptor *field);
  468. //%
  469. //%/**
  470. //% * Sets the value of a##AN NAME$L field.
  471. //% *
  472. //% * @param self The message into which to set the field.
  473. //% * @param field The field to set.
  474. //% * @param value The to set in the field.
  475. //% **/
  476. //%void GPBSetMessage##NAME##Field(GPBMessage *self, GPBFieldDescriptor *field, TYPE TisP##value);
  477. //%
  478. // clang-format on