GPBMessage.h 7.6 KB

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