blob: a6b6c84d988064c5e8f1d90ab7240a75c1df63df [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// 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
31#import <Foundation/Foundation.h>
32
33#import "GPBUtilities.h"
34
35#import "GPBDescriptor_PackagePrivate.h"
36
37// Macros for stringifying library symbols. These are used in the generated
38// PB descriptor classes wherever a library symbol name is represented as a
39// string. See README.google for more information.
40#define GPBStringify(S) #S
41#define GPBStringifySymbol(S) GPBStringify(S)
42
43#define GPBNSStringify(S) @#S
44#define GPBNSStringifySymbol(S) GPBNSStringify(S)
45
46// Constant to internally mark when there is no has bit.
47#define GPBNoHasBit INT32_MAX
48
49CF_EXTERN_C_BEGIN
50
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040051// These two are used to inject a runtime check for version mismatch into the
52// generated sources to make sure they are linked with a supporting runtime.
53void GPBCheckRuntimeVersionInternal(int32_t version);
54GPB_INLINE void GPBDebugCheckRuntimeVersion() {
55#if DEBUG
56 GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
57#endif
58}
59
Thomas Van Lenten30650d82015-05-01 08:57:16 -040060// Conversion functions for de/serializing floating point types.
61
62GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
63 union { double f; int64_t i; } u;
64 u.f = v;
65 return u.i;
66}
67
68GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
69 union { float f; int32_t i; } u;
70 u.f = v;
71 return u.i;
72}
73
74GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
75 union { double f; int64_t i; } u;
76 u.i = v;
77 return u.f;
78}
79
80GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
81 union { float f; int32_t i; } u;
82 u.i = v;
83 return u.f;
84}
85
86GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
87 return (int32_t)((uint32_t)(value) >> spaces);
88}
89
90GPB_INLINE int64_t GPBLogicalRightShift64(int64_t value, int32_t spaces) {
91 return (int64_t)((uint64_t)(value) >> spaces);
92}
93
94// Decode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
95// into values that can be efficiently encoded with varint. (Otherwise,
96// negative values must be sign-extended to 64 bits to be varint encoded,
97// thus always taking 10 bytes on the wire.)
98GPB_INLINE int32_t GPBDecodeZigZag32(uint32_t n) {
99 return GPBLogicalRightShift32(n, 1) ^ -(n & 1);
100}
101
102// Decode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
103// into values that can be efficiently encoded with varint. (Otherwise,
104// negative values must be sign-extended to 64 bits to be varint encoded,
105// thus always taking 10 bytes on the wire.)
106GPB_INLINE int64_t GPBDecodeZigZag64(uint64_t n) {
107 return GPBLogicalRightShift64(n, 1) ^ -(n & 1);
108}
109
110// Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers
111// into values that can be efficiently encoded with varint. (Otherwise,
112// negative values must be sign-extended to 64 bits to be varint encoded,
113// thus always taking 10 bytes on the wire.)
114GPB_INLINE uint32_t GPBEncodeZigZag32(int32_t n) {
115 // Note: the right-shift must be arithmetic
116 return (n << 1) ^ (n >> 31);
117}
118
119// Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers
120// into values that can be efficiently encoded with varint. (Otherwise,
121// negative values must be sign-extended to 64 bits to be varint encoded,
122// thus always taking 10 bytes on the wire.)
123GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
124 // Note: the right-shift must be arithmetic
125 return (n << 1) ^ (n >> 63);
126}
127
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400128GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400129 switch (type) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400130 case GPBDataTypeBytes:
131 case GPBDataTypeString:
132 case GPBDataTypeMessage:
133 case GPBDataTypeGroup:
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400134 return YES;
135 default:
136 return NO;
137 }
138}
139
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400140GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400141 switch (type) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400142 case GPBDataTypeMessage:
143 case GPBDataTypeGroup:
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400144 return YES;
145 default:
146 return NO;
147 }
148}
149
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400150GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
151 return GPBDataTypeIsMessage(field->description_->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400152}
153
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400154GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
155 return GPBDataTypeIsObject(field->description_->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400156}
157
158GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400159 return GPBDataTypeIsMessage(ext->description_->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400160}
161
162// The field is an array/map or it has an object value.
163GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
164 GPBMessageFieldDescription *desc = field->description_;
165 if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
166 return YES;
167 }
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400168 return GPBDataTypeIsObject(desc->dataType);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400169}
170
171BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
172void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
173 BOOL value);
174uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
175
176GPB_INLINE BOOL
177GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
178 GPBMessageFieldDescription *fieldDesc = field->description_;
179 return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
180}
181GPB_INLINE void GPBSetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field,
182 BOOL value) {
183 GPBMessageFieldDescription *fieldDesc = field->description_;
184 GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, value);
185}
186
187void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
Thomas Van Lenten79a23c42016-03-17 10:04:21 -0400188 int32_t oneofHasIndex, uint32_t fieldNumberNotToClear);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400189
190//%PDDM-DEFINE GPB_IVAR_SET_DECL(NAME, TYPE)
191//%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self,
192//% NAME$S GPBFieldDescriptor *field,
193//% NAME$S TYPE value,
194//% NAME$S GPBFileSyntax syntax);
195//%PDDM-EXPAND GPB_IVAR_SET_DECL(Bool, BOOL)
196// This block of code is generated, do not edit it directly.
197
198void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
199 GPBFieldDescriptor *field,
200 BOOL value,
201 GPBFileSyntax syntax);
202//%PDDM-EXPAND GPB_IVAR_SET_DECL(Int32, int32_t)
203// This block of code is generated, do not edit it directly.
204
205void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
206 GPBFieldDescriptor *field,
207 int32_t value,
208 GPBFileSyntax syntax);
209//%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt32, uint32_t)
210// This block of code is generated, do not edit it directly.
211
212void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
213 GPBFieldDescriptor *field,
214 uint32_t value,
215 GPBFileSyntax syntax);
216//%PDDM-EXPAND GPB_IVAR_SET_DECL(Int64, int64_t)
217// This block of code is generated, do not edit it directly.
218
219void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
220 GPBFieldDescriptor *field,
221 int64_t value,
222 GPBFileSyntax syntax);
223//%PDDM-EXPAND GPB_IVAR_SET_DECL(UInt64, uint64_t)
224// This block of code is generated, do not edit it directly.
225
226void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
227 GPBFieldDescriptor *field,
228 uint64_t value,
229 GPBFileSyntax syntax);
230//%PDDM-EXPAND GPB_IVAR_SET_DECL(Float, float)
231// This block of code is generated, do not edit it directly.
232
233void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
234 GPBFieldDescriptor *field,
235 float value,
236 GPBFileSyntax syntax);
237//%PDDM-EXPAND GPB_IVAR_SET_DECL(Double, double)
238// This block of code is generated, do not edit it directly.
239
240void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
241 GPBFieldDescriptor *field,
242 double value,
243 GPBFileSyntax syntax);
244//%PDDM-EXPAND GPB_IVAR_SET_DECL(Enum, int32_t)
245// This block of code is generated, do not edit it directly.
246
247void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
248 GPBFieldDescriptor *field,
249 int32_t value,
250 GPBFileSyntax syntax);
251//%PDDM-EXPAND-END (8 expansions)
252
253int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
254 GPBFieldDescriptor *field,
255 GPBFileSyntax syntax);
256
257id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
258
259void GPBSetObjectIvarWithFieldInternal(GPBMessage *self,
260 GPBFieldDescriptor *field, id value,
261 GPBFileSyntax syntax);
262void GPBSetRetainedObjectIvarWithFieldInternal(GPBMessage *self,
263 GPBFieldDescriptor *field,
264 id __attribute__((ns_consumed))
265 value,
266 GPBFileSyntax syntax);
267
268// GPBGetObjectIvarWithField will automatically create the field (message) if
269// it doesn't exist. GPBGetObjectIvarWithFieldNoAutocreate will return nil.
270id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
271 GPBFieldDescriptor *field);
272
273void GPBSetAutocreatedRetainedObjectIvarWithField(
274 GPBMessage *self, GPBFieldDescriptor *field,
275 id __attribute__((ns_consumed)) value);
276
277// Clears and releases the autocreated message ivar, if it's autocreated. If
278// it's not set as autocreated, this method does nothing.
279void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self,
280 GPBFieldDescriptor *field);
281
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400282// Returns an Objective C encoding for |selector|. |instanceSel| should be
283// YES if it's an instance selector (as opposed to a class selector).
284// |selector| must be a selector from MessageSignatureProtocol.
285const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel);
286
287// Helper for text format name encoding.
288// decodeData is the data describing the sepecial decodes.
289// key and inputString are the input that needs decoding.
290NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key,
291 NSString *inputString);
292
293// A series of selectors that are used solely to get @encoding values
294// for them by the dynamic protobuf runtime code. See
295// GPBMessageEncodingForSelector for details.
296@protocol GPBMessageSignatureProtocol
297@optional
298
299#define GPB_MESSAGE_SIGNATURE_ENTRY(TYPE, NAME) \
300 -(TYPE)get##NAME; \
301 -(void)set##NAME : (TYPE)value; \
302 -(TYPE)get##NAME##AtIndex : (NSUInteger)index;
303
304GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
305GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
306GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
307GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
308GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
309GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
310GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
311GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
312GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
313GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
314GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
315GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
316GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400317GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400318GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
319GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
320GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
321GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
322
323#undef GPB_MESSAGE_SIGNATURE_ENTRY
324
325- (id)getArray;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400326- (NSUInteger)getArrayCount;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400327- (void)setArray:(NSArray *)array;
328+ (id)getClassValue;
329@end
330
331CF_EXTERN_C_END