Home | History | Annotate | Download | only in objectivec
      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 
     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 
     49 CF_EXTERN_C_BEGIN
     50 
     51 // 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.
     53 void GPBCheckRuntimeVersionInternal(int32_t version);
     54 GPB_INLINE void GPBDebugCheckRuntimeVersion() {
     55 #if DEBUG
     56   GPBCheckRuntimeVersionInternal(GOOGLE_PROTOBUF_OBJC_GEN_VERSION);
     57 #endif
     58 }
     59 
     60 // Conversion functions for de/serializing floating point types.
     61 
     62 GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {
     63   union { double f; int64_t i; } u;
     64   u.f = v;
     65   return u.i;
     66 }
     67 
     68 GPB_INLINE int32_t GPBConvertFloatToInt32(float v) {
     69   union { float f; int32_t i; } u;
     70   u.f = v;
     71   return u.i;
     72 }
     73 
     74 GPB_INLINE double GPBConvertInt64ToDouble(int64_t v) {
     75   union { double f; int64_t i; } u;
     76   u.i = v;
     77   return u.f;
     78 }
     79 
     80 GPB_INLINE float GPBConvertInt32ToFloat(int32_t v) {
     81   union { float f; int32_t i; } u;
     82   u.i = v;
     83   return u.f;
     84 }
     85 
     86 GPB_INLINE int32_t GPBLogicalRightShift32(int32_t value, int32_t spaces) {
     87   return (int32_t)((uint32_t)(value) >> spaces);
     88 }
     89 
     90 GPB_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.)
     98 GPB_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.)
    106 GPB_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.)
    114 GPB_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.)
    123 GPB_INLINE uint64_t GPBEncodeZigZag64(int64_t n) {
    124   // Note:  the right-shift must be arithmetic
    125   return (n << 1) ^ (n >> 63);
    126 }
    127 
    128 GPB_INLINE BOOL GPBDataTypeIsObject(GPBDataType type) {
    129   switch (type) {
    130     case GPBDataTypeBytes:
    131     case GPBDataTypeString:
    132     case GPBDataTypeMessage:
    133     case GPBDataTypeGroup:
    134       return YES;
    135     default:
    136       return NO;
    137   }
    138 }
    139 
    140 GPB_INLINE BOOL GPBDataTypeIsMessage(GPBDataType type) {
    141   switch (type) {
    142     case GPBDataTypeMessage:
    143     case GPBDataTypeGroup:
    144       return YES;
    145     default:
    146       return NO;
    147   }
    148 }
    149 
    150 GPB_INLINE BOOL GPBFieldDataTypeIsMessage(GPBFieldDescriptor *field) {
    151   return GPBDataTypeIsMessage(field->description_->dataType);
    152 }
    153 
    154 GPB_INLINE BOOL GPBFieldDataTypeIsObject(GPBFieldDescriptor *field) {
    155   return GPBDataTypeIsObject(field->description_->dataType);
    156 }
    157 
    158 GPB_INLINE BOOL GPBExtensionIsMessage(GPBExtensionDescriptor *ext) {
    159   return GPBDataTypeIsMessage(ext->description_->dataType);
    160 }
    161 
    162 // The field is an array/map or it has an object value.
    163 GPB_INLINE BOOL GPBFieldStoresObject(GPBFieldDescriptor *field) {
    164   GPBMessageFieldDescription *desc = field->description_;
    165   if ((desc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) != 0) {
    166     return YES;
    167   }
    168   return GPBDataTypeIsObject(desc->dataType);
    169 }
    170 
    171 BOOL GPBGetHasIvar(GPBMessage *self, int32_t index, uint32_t fieldNumber);
    172 void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
    173                    BOOL value);
    174 uint32_t GPBGetHasOneof(GPBMessage *self, int32_t index);
    175 
    176 GPB_INLINE BOOL
    177 GPBGetHasIvarField(GPBMessage *self, GPBFieldDescriptor *field) {
    178   GPBMessageFieldDescription *fieldDesc = field->description_;
    179   return GPBGetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number);
    180 }
    181 GPB_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 
    187 void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
    188                         int32_t oneofHasIndex, uint32_t fieldNumberNotToClear);
    189 
    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 
    198 void 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 
    205 void 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 
    212 void 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 
    219 void 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 
    226 void 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 
    233 void 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 
    240 void 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 
    247 void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
    248                                      GPBFieldDescriptor *field,
    249                                      int32_t value,
    250                                      GPBFileSyntax syntax);
    251 //%PDDM-EXPAND-END (8 expansions)
    252 
    253 int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
    254                                         GPBFieldDescriptor *field,
    255                                         GPBFileSyntax syntax);
    256 
    257 id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
    258 
    259 void GPBSetObjectIvarWithFieldInternal(GPBMessage *self,
    260                                        GPBFieldDescriptor *field, id value,
    261                                        GPBFileSyntax syntax);
    262 void 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.
    270 id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
    271                                          GPBFieldDescriptor *field);
    272 
    273 void 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.
    279 void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self,
    280                                              GPBFieldDescriptor *field);
    281 
    282 // 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.
    285 const 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.
    290 NSString *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 
    304 GPB_MESSAGE_SIGNATURE_ENTRY(BOOL, Bool)
    305 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, Fixed32)
    306 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SFixed32)
    307 GPB_MESSAGE_SIGNATURE_ENTRY(float, Float)
    308 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, Fixed64)
    309 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SFixed64)
    310 GPB_MESSAGE_SIGNATURE_ENTRY(double, Double)
    311 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Int32)
    312 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, Int64)
    313 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, SInt32)
    314 GPB_MESSAGE_SIGNATURE_ENTRY(int64_t, SInt64)
    315 GPB_MESSAGE_SIGNATURE_ENTRY(uint32_t, UInt32)
    316 GPB_MESSAGE_SIGNATURE_ENTRY(uint64_t, UInt64)
    317 GPB_MESSAGE_SIGNATURE_ENTRY(NSData *, Bytes)
    318 GPB_MESSAGE_SIGNATURE_ENTRY(NSString *, String)
    319 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Message)
    320 GPB_MESSAGE_SIGNATURE_ENTRY(GPBMessage *, Group)
    321 GPB_MESSAGE_SIGNATURE_ENTRY(int32_t, Enum)
    322 
    323 #undef GPB_MESSAGE_SIGNATURE_ENTRY
    324 
    325 - (id)getArray;
    326 - (NSUInteger)getArrayCount;
    327 - (void)setArray:(NSArray *)array;
    328 + (id)getClassValue;
    329 @end
    330 
    331 CF_EXTERN_C_END
    332