GPBMessage.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "GPBRootObject.h"
  31. @class GPBDescriptor;
  32. @class GPBCodedInputStream;
  33. @class GPBCodedOutputStream;
  34. @class GPBExtensionField;
  35. @class GPBFieldDescriptor;
  36. @class GPBUnknownFieldSet;
  37. // In DEBUG ONLY, an NSException is thrown when a parsed message doesn't
  38. // contain required fields. This key allows you to retrieve the parsed message
  39. // from the exception's |userInfo| dictionary.
  40. #ifdef DEBUG
  41. extern NSString *const GPBExceptionMessageKey;
  42. #endif // DEBUG
  43. // NOTE:
  44. // If you add a instance method/property to this class that may conflict with
  45. // methods declared in protos, you need to update objective_helpers.cc.
  46. // The main cases are methods that take no arguments, or setFoo:/hasFoo: type
  47. // methods.
  48. @interface GPBMessage : GPBRootObject<NSCoding, NSCopying>
  49. @property(nonatomic, readonly) GPBUnknownFieldSet *unknownFields;
  50. // Are all required fields in the message and all embedded messages set.
  51. @property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
  52. // Returns an autoreleased instance.
  53. + (instancetype)message;
  54. // Create a message based on a variety of inputs.
  55. // In DEBUG ONLY
  56. // @throws NSInternalInconsistencyException The message is missing one or more
  57. // required fields (i.e. -[isInitialized] returns false). Use
  58. // GGPBExceptionMessageKey to retrieve the message from |userInfo|.
  59. + (instancetype)parseFromData:(NSData *)data;
  60. + (instancetype)parseFromData:(NSData *)data
  61. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry;
  62. + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
  63. extensionRegistry:
  64. (GPBExtensionRegistry *)extensionRegistry;
  65. // Create a message based on delimited input.
  66. + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  67. extensionRegistry:
  68. (GPBExtensionRegistry *)extensionRegistry;
  69. - (instancetype)initWithData:(NSData *)data;
  70. - (instancetype)initWithData:(NSData *)data
  71. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry;
  72. - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
  73. extensionRegistry:
  74. (GPBExtensionRegistry *)extensionRegistry;
  75. // Serializes the message and writes it to output.
  76. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
  77. - (void)writeToOutputStream:(NSOutputStream *)output;
  78. // Serializes the message and writes it to output, but writes the size of the
  79. // message as a variant before writing the message.
  80. - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
  81. - (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
  82. // Serializes the message to an NSData. Note that this value is not cached, so
  83. // if you are using it repeatedly, cache it yourself.
  84. // In DEBUG ONLY:
  85. // @throws NSInternalInconsistencyException The message is missing one or more
  86. // required fields (i.e. -[isInitialized] returns false). Use
  87. // GPBExceptionMessageKey to retrieve the message from |userInfo|.
  88. - (NSData *)data;
  89. // Same as -[data], except a delimiter is added to the start of the data
  90. // indicating the size of the message data that follows.
  91. - (NSData *)delimitedData;
  92. // Returns the size of the object if it were serialized.
  93. // This is not a cached value. If you are following a pattern like this:
  94. // size_t size = [aMsg serializedSize];
  95. // NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
  96. // [foo writeSize:size];
  97. // [foo appendData:[aMsg data]];
  98. // you would be better doing:
  99. // NSData *data = [aMsg data];
  100. // NSUInteger size = [aMsg length];
  101. // NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
  102. // [foo writeSize:size];
  103. // [foo appendData:data];
  104. - (size_t)serializedSize;
  105. // Return the descriptor for the message
  106. + (GPBDescriptor *)descriptor;
  107. - (GPBDescriptor *)descriptor;
  108. // Extensions use boxed values (NSNumbers) for PODs, NSMutableArrays for
  109. // repeated. If the extension is a Message, just like fields, one will be
  110. // auto created for you and returned.
  111. - (BOOL)hasExtension:(GPBExtensionField *)extension;
  112. - (id)getExtension:(GPBExtensionField *)extension;
  113. - (void)setExtension:(GPBExtensionField *)extension value:(id)value;
  114. - (void)addExtension:(GPBExtensionField *)extension value:(id)value;
  115. - (void)setExtension:(GPBExtensionField *)extension
  116. index:(NSUInteger)index
  117. value:(id)value;
  118. - (void)clearExtension:(GPBExtensionField *)extension;
  119. - (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields;
  120. // Resets all fields to their default values.
  121. - (void)clear;
  122. // Parses a message of this type from the input and merges it with this
  123. // message.
  124. - (void)mergeFromData:(NSData *)data
  125. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry;
  126. // Merges the fields from another message (of the same type) into this
  127. // message.
  128. - (void)mergeFrom:(GPBMessage *)other;
  129. @end