GPBUtilities.h 16 KB

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