GPBUtilities.h 17 KB

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