MessageBuilder.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Protocol Buffers for Objective C
  2. //
  3. // Copyright 2010 Booyah Inc.
  4. // Copyright 2008 Cyrus Najmabadi
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #import "Message.h"
  18. @class PBCodedInputStream;
  19. @class PBExtensionRegistry;
  20. /**
  21. * Abstract interface implemented by Protocol Message builders.
  22. */
  23. @protocol PBMessageBuilder<NSObject>
  24. /** Resets all fields to their default values. */
  25. - (id<PBMessageBuilder>) clear;
  26. /**
  27. * Construct the final message. Once this is called, the Builder is no
  28. * longer valid, and calling any other method may throw a
  29. * NullPointerException. If you need to continue working with the builder
  30. * after calling {@code build()}, {@code clone()} it first.
  31. * @throws UninitializedMessageException The message is missing one or more
  32. * required fields (i.e. {@link #isInitialized()} returns false).
  33. * Use {@link #buildPartial()} to bypass this check.
  34. */
  35. - (id<PBMessage>) build;
  36. /**
  37. * Like {@link #build()}, but does not throw an exception if the message
  38. * is missing required fields. Instead, a partial message is returned.
  39. */
  40. - (id<PBMessage>) buildPartial;
  41. /**
  42. * 子类实现该方法
  43. */
  44. - (id<PBMessageBuilder>) clone;
  45. /**
  46. * Returns true if all required fields in the message and all embedded
  47. * messages are set, false otherwise.
  48. */
  49. - (BOOL) isInitialized;
  50. /**
  51. * Get the message's type's default instance.
  52. * See {@link Message#getDefaultInstanceForType()}.
  53. */
  54. - (id<PBMessage>) defaultInstance;
  55. /**
  56. * 未知字段(新加字段)
  57. */
  58. - (PBUnknownFieldSet*) unknownFields;
  59. /**
  60. * 设置未知字段(新加字段)
  61. */
  62. - (id<PBMessageBuilder>) setUnknownFields:(PBUnknownFieldSet*) unknownFields;
  63. /**
  64. * Merge some unknown fields into the {@link UnknownFieldSet} for this
  65. * message.
  66. */
  67. - (id<PBMessageBuilder>) mergeUnknownFields:(PBUnknownFieldSet*) unknownFields;
  68. /**
  69. * Parses a message of this type from the input and merges it with this
  70. * message, as if using {@link Builder#mergeFrom(Message)}.
  71. *
  72. * <p>Warning: This does not verify that all required fields are present in
  73. * the input message. If you call {@link #build()} without setting all
  74. * required fields, it will throw an {@link UninitializedMessageException},
  75. * which is a {@code RuntimeException} and thus might not be caught. There
  76. * are a few good ways to deal with this:
  77. * <ul>
  78. * <li>Call {@link #isInitialized()} to verify that all required fields
  79. * are set before building.
  80. * <li>Parse the message separately using one of the static
  81. * {@code parseFrom} methods, then use {@link #mergeFrom(Message)}
  82. * to merge it with this one. {@code parseFrom} will throw an
  83. * {@link InvalidProtocolBufferException} (an {@code IOException})
  84. * if some required fields are missing.
  85. * <li>Use {@code buildPartial()} to build, which ignores missing
  86. * required fields.
  87. * </ul>
  88. *
  89. * <p>Note: The caller should call
  90. * {@link CodedInputStream#checkLastTagWas(int)} after calling this to
  91. * verify that the last tag seen was the appropriate end-group tag,
  92. * or zero for EOF.
  93. */
  94. - (id<PBMessageBuilder>) mergeFromCodedInputStream:(PBCodedInputStream*) input;
  95. /**
  96. * Like {@link Builder#mergeFrom(CodedInputStream)}, but also
  97. * parses extensions. The extensions that you want to be able to parse
  98. * must be registered in {@code extensionRegistry}. Extensions not in
  99. * the registry will be treated as unknown fields.
  100. */
  101. - (id<PBMessageBuilder>) mergeFromCodedInputStream:(PBCodedInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
  102. /**
  103. * Parse {@code data} as a message of this type and merge it with the
  104. * message being built. This is just a small wrapper around
  105. * {@link #mergeFrom(CodedInputStream)}.
  106. */
  107. - (id<PBMessageBuilder>) mergeFromData:(NSData*) data;
  108. /**
  109. * Parse {@code data} as a message of this type and merge it with the
  110. * message being built. This is just a small wrapper around
  111. * {@link #mergeFrom(CodedInputStream,ExtensionRegistry)}.
  112. */
  113. - (id<PBMessageBuilder>) mergeFromData:(NSData*) data extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
  114. /**
  115. * Parse a message of this type from {@code input} and merge it with the
  116. * message being built. This is just a small wrapper around
  117. * {@link #mergeFrom(CodedInputStream)}. Note that this method always
  118. * reads the <i>entire</i> input (unless it throws an exception). If you
  119. * want it to stop earlier, you will need to wrap your input in some
  120. * wrapper stream that limits reading. Despite usually reading the entire
  121. * input, this does not close the stream.
  122. */
  123. - (id<PBMessageBuilder>) mergeFromInputStream:(NSInputStream*) input;
  124. /**
  125. * Parse a message of this type from {@code input} and merge it with the
  126. * message being built. This is just a small wrapper around
  127. * {@link #mergeFrom(CodedInputStream,ExtensionRegistry)}.
  128. */
  129. - (id<PBMessageBuilder>) mergeFromInputStream:(NSInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
  130. @end