GPBUnknownFields.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2024 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 "GPBMessage.h"
  9. #import "GPBUnknownField.h"
  10. @class GPBMessage;
  11. @class GPBUnknownField;
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. * A collection of unknown field numbers and their values.
  15. *
  16. * Note: `NSFastEnumeration` is supported to iterate over the `GPBUnknownFields`
  17. * in order.
  18. *
  19. * Reminder: Any field number can occur multiple times. For example, if a .proto
  20. * file were updated to a have a new (unpacked) repeated field, then each value
  21. * would appear independently. Likewise, it is possible that a number appears
  22. * multiple times with different data types, i.e. - unpacked vs. package repeated
  23. * fields from concatenating binary blobs of data.
  24. */
  25. __attribute__((objc_subclassing_restricted))
  26. @interface GPBUnknownFields : NSObject<NSCopying, NSFastEnumeration>
  27. /**
  28. * Initializes a new instance with the data from the unknown fields from the given
  29. * message.
  30. *
  31. * Note: The instance is not linked to the message, any change will not be
  32. * reflected on the message the changes have to be pushed back to the message
  33. * with `-[GPBMessage mergeUnknownFields:extensionRegistry:error:]`.
  34. **/
  35. - (instancetype)initFromMessage:(nonnull GPBMessage *)message;
  36. /**
  37. * Initializes a new empty instance.
  38. **/
  39. - (instancetype)init;
  40. /**
  41. * The number of fields in this set. A single field number can appear in
  42. * multiple `GPBUnknownField` values as it might be a repeated field (it is
  43. * also possible that they have different `type` values (for example package vs
  44. * unpacked repeated fields).
  45. *
  46. * Note: `NSFastEnumeration` is supported to iterate over the fields in order.
  47. **/
  48. @property(nonatomic, readonly, assign) NSUInteger count;
  49. /** If the set is empty or not. */
  50. @property(nonatomic, readonly, assign) BOOL empty;
  51. /**
  52. * Removes all the fields current in the set.
  53. **/
  54. - (void)clear;
  55. /**
  56. * Fetches the subset of all the unknown fields that are for the given field
  57. * number.
  58. *
  59. * @returns An `NSArray` of `GPBUnknownField`s or `nil` if there were none.
  60. */
  61. - (nullable NSArray<GPBUnknownField *> *)fields:(int32_t)fieldNumber;
  62. /**
  63. * Add a new varint unknown field.
  64. *
  65. * @param fieldNumber The field number to use.
  66. * @param value The value to add.
  67. **/
  68. - (void)addFieldNumber:(int32_t)fieldNumber varint:(uint64_t)value;
  69. /**
  70. * Add a new fixed32 unknown field.
  71. *
  72. * @param fieldNumber The field number to use.
  73. * @param value The value to add.
  74. **/
  75. - (void)addFieldNumber:(int32_t)fieldNumber fixed32:(uint32_t)value;
  76. /**
  77. * Add a new fixed64 unknown field.
  78. *
  79. * @param fieldNumber The field number to use.
  80. * @param value The value to add.
  81. **/
  82. - (void)addFieldNumber:(int32_t)fieldNumber fixed64:(uint64_t)value;
  83. /**
  84. * Add a new length delimited (length prefixed) unknown field.
  85. *
  86. * @param fieldNumber The field number to use.
  87. * @param value The value to add.
  88. **/
  89. - (void)addFieldNumber:(int32_t)fieldNumber lengthDelimited:(nonnull NSData *)value;
  90. /**
  91. * Add a group (tag delimited) unknown field.
  92. *
  93. * @param fieldNumber The field number to use.
  94. *
  95. * @return A new `GPBUnknownFields` to set the field of the group too.
  96. **/
  97. - (nonnull GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber;
  98. /**
  99. * Add the copy of the given unknown field.
  100. *
  101. * This can be useful from processing one `GPBUnknownFields` to create another.
  102. *
  103. * NOTE: If the field being copied is an Group, this instance added is new and thus
  104. * the `.group` of that result is also new, so if you intent is to modify the group
  105. * it *must* be fetched out of the result.
  106. *
  107. * It is a programming error to call this when the `type` is a legacy field.
  108. *
  109. * @param field The field to add.
  110. *
  111. * @return The autoreleased field that was added.
  112. **/
  113. - (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field;
  114. /**
  115. * Removes the given field from the set.
  116. *
  117. * It is a programming error to attempt to remove a field that is not in this collection.
  118. *
  119. * Reminder: it is not save to mutate the collection while also using fast enumeration on it.
  120. *
  121. * @param field The field to remove.
  122. **/
  123. - (void)removeField:(nonnull GPBUnknownField *)field;
  124. /**
  125. * Removes all of the fields from the collection that have the given field number.
  126. *
  127. * If there are no fields with the given field number, this is a no-op.
  128. *
  129. * @param fieldNumber The field number to remove.
  130. **/
  131. - (void)clearFieldNumber:(int32_t)fieldNumber;
  132. @end
  133. @interface GPBUnknownFields (AccessHelpers)
  134. /**
  135. * Fetches the first varint for the given field number.
  136. *
  137. * @param fieldNumber The field number to look for.
  138. * @param outValue A pointer to receive the value if found
  139. *
  140. * @returns YES/NO on if there was a matching unknown field.
  141. **/
  142. - (BOOL)getFirst:(int32_t)fieldNumber varint:(nonnull uint64_t *)outValue;
  143. /**
  144. * Fetches the first fixed32 for the given field number.
  145. *
  146. * @param fieldNumber The field number to look for.
  147. * @param outValue A pointer to receive the value if found
  148. *
  149. * @returns YES/NO on if there was a matching unknown field.
  150. **/
  151. - (BOOL)getFirst:(int32_t)fieldNumber fixed32:(nonnull uint32_t *)outValue;
  152. /**
  153. * Fetches the first fixed64 for the given field number.
  154. *
  155. * @param fieldNumber The field number to look for.
  156. * @param outValue A pointer to receive the value if found
  157. *
  158. * @returns YES/NO on if there was a matching unknown field.
  159. **/
  160. - (BOOL)getFirst:(int32_t)fieldNumber fixed64:(nonnull uint64_t *)outValue;
  161. /**
  162. * Fetches the first length delimited (length prefixed) for the given field number.
  163. *
  164. * @param fieldNumber The field number to look for.
  165. *
  166. * @returns The first length delimited value for the given field number.
  167. **/
  168. - (nullable NSData *)firstLengthDelimited:(int32_t)fieldNumber;
  169. /**
  170. * Fetches the first group (tag delimited) field for the given field number.
  171. *
  172. * @param fieldNumber The field number to look for.
  173. *
  174. * @returns The first group for the given field number.
  175. **/
  176. - (nullable GPBUnknownFields *)firstGroup:(int32_t)fieldNumber;
  177. @end
  178. NS_ASSUME_NONNULL_END