Github user jeremy-w commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/442#discussion_r28600626
  
    --- Diff: lib/cocoa/src/protocol/TCompactProtocol.m ---
    @@ -0,0 +1,706 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements. See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership. The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License. You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied. See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +#import "TCompactProtocol.h"
    +#import "TObjective-C.h"
    +#import "TProtocolException.h"
    +
    +static const uint8_t COMPACT_PROTOCOL_ID = 0x82;
    +static const uint8_t COMPACT_VERSION = 1;
    +static const uint8_t COMPACT_VERSION_MASK = 0x1F; // 0001 1111
    +static const uint8_t COMPACT_TYPE_MASK = 0xE0; // 1110 0000
    +static const uint8_t COMPACT_TYPE_BITS = 0x07; // 0000 0111
    +static const int COMPACT_TYPE_SHIFT_AMOUNT = 5;
    +
    +static uint8_t ttypeToCompactType[16] = {0};
    +
    +enum
    +{
    +    TCType_STOP = 0x00,
    +    TCType_BOOLEAN_TRUE = 0x01,
    +    TCType_BOOLEAN_FALSE = 0x02,
    +    TCType_BYTE = 0x03,
    +    TCType_I16 = 0x04,
    +    TCType_I32 = 0x05,
    +    TCType_I64 = 0x06,
    +    TCType_DOUBLE = 0x07,
    +    TCType_BINARY = 0x08,
    +    TCType_LIST = 0x09,
    +    TCType_SET = 0x0A,
    +    TCType_MAP = 0x0B,
    +    TCType_STRUCT = 0x0C,
    +};
    +
    +@implementation TCompactProtocolFactory
    +
    ++(TCompactProtocolFactory*)sharedFactory
    +{
    +    static TCompactProtocolFactory* gSharedFactory = nil;
    +    if (gSharedFactory == nil)
    +    {
    +        gSharedFactory = [[TCompactProtocolFactory alloc] init];
    +    }
    +    
    +    return gSharedFactory;
    +}
    +
    +-(TCompactProtocol*)newProtocolOnTransport:(id <TTransport>)transport
    +{
    +    return [[TCompactProtocol alloc] initWithTransport:transport];
    +}
    +
    +@end
    +
    +@implementation TCompactProtocol
    +{
    +    NSMutableArray* lastField;
    +    short lastFieldId;
    +    id <TTransport> mTransport;
    +    
    +    NSString* boolFieldName;
    +    NSNumber* boolFieldType;
    +    NSNumber* boolFieldId;
    +    NSNumber* booleanValue;
    +}
    +
    +-(id)init
    +{
    +    self = [super init];
    +    
    +    if (self != nil)
    +    {
    +        lastField = [[NSMutableArray alloc] init];
    +    }
    +    
    +    return self;
    +}
    +
    +-(id)initWithTransport:(id <TTransport>)transport
    +{
    +    self = [self init];
    +    
    +    if (self != nil)
    +    {
    +        mTransport = [transport retain_stub];
    +        
    +        ttypeToCompactType[TType_STOP] = TCType_STOP;
    +        ttypeToCompactType[TType_BOOL] = TCType_BOOLEAN_FALSE;
    +        ttypeToCompactType[TType_BYTE] = TCType_BYTE;
    +        ttypeToCompactType[TType_DOUBLE] = TCType_DOUBLE;
    +        ttypeToCompactType[TType_I16] = TCType_I16;
    +        ttypeToCompactType[TType_I32] = TCType_I32;
    +        ttypeToCompactType[TType_I64] = TCType_I64;
    +        ttypeToCompactType[TType_STRING] = TCType_BINARY;
    +        ttypeToCompactType[TType_STRUCT] = TCType_STRUCT;
    +        ttypeToCompactType[TType_MAP] = TCType_MAP;
    +        ttypeToCompactType[TType_SET] = TCType_SET;
    +        ttypeToCompactType[TType_LIST] = TCType_LIST;
    +    }
    +    
    +    return self;
    +}
    +
    +-(void)dealloc
    +{
    +    [lastField release_stub];
    +    [mTransport release_stub];
    +    [boolFieldName release_stub];
    +    [boolFieldType release_stub];
    +    [boolFieldId release_stub];
    +    [booleanValue release_stub];
    +    [super dealloc_stub];
    +}
    +
    +-(id<TTransport>)transport
    +{
    +    return mTransport;
    +}
    +
    +-(void)writeByteDirect:(int8_t)n
    +{
    +    [mTransport write:(uint8_t*)&n offset:0 length:1];
    +}
    +
    +-(void)writeVarint32:(uint32_t)n
    +{
    +    uint8_t i32buf[5] = {0};
    +    uint32_t idx = 0;
    +    while (true)
    +    {
    +        if ((n & ~0x7F) == 0)
    +        {
    +            i32buf[idx++] = (uint8_t)n;
    +            break;
    +        }
    +        else
    +        {
    +            i32buf[idx++] = (uint8_t)((n & 0x7F) | 0x80);
    +            n >>= 7;
    +        }
    +    }
    +    [mTransport write:i32buf offset:0 length:idx];
    +}
    +
    +-(void)writeMessageBeginWithName:(NSString *)name type:(int)messageType 
sequenceID:(int)sequenceID
    +{
    +    [self writeByteDirect:COMPACT_PROTOCOL_ID];
    +    [self writeByteDirect:(uint8_t)((COMPACT_VERSION & 
COMPACT_VERSION_MASK) | ((((uint32_t)messageType) << COMPACT_TYPE_SHIFT_AMOUNT) 
& COMPACT_TYPE_MASK))];
    +    [self writeVarint32:(uint32_t)sequenceID];
    +    [self writeString:name];
    +}
    +
    +-(void)writeStructBeginWithName:(NSString*)name
    +{
    +    [lastField addObject:[NSNumber numberWithShort:lastFieldId]];
    +    lastFieldId = 0;
    +}
    +
    +-(void)writeStructEnd
    +{
    +    lastFieldId = [[lastField lastObject] shortValue];
    +    [lastField removeLastObject];
    +}
    +
    +-(void)writeFieldBeginWithName:(NSString*)name type:(int)fieldType 
fieldID:(int)fieldID
    +{
    +    if (fieldType == TType_BOOL)
    +    {
    +        boolFieldName = [name copy];
    +        boolFieldType = [[NSNumber numberWithInt:fieldType] retain_stub];
    +        boolFieldId = [[NSNumber numberWithInt:fieldID] retain_stub];
    +    }
    +    else
    +    {
    +        [self writeFieldBeginInternalWithName:name type:fieldType 
fieldID:fieldID typeOverride:0xFF];
    +    }
    +}
    +
    +-(void)writeFieldBeginInternalWithName:(NSString *)name 
type:(int)fieldType fieldID:(int)fieldID typeOverride:(uint8_t)typeOverride
    +{
    +    uint8_t typeToWrite = typeOverride == 0xFF ? [self 
getCompactTypeForTType:fieldType] : typeOverride;
    +    
    +    // check if we can use delta encoding for the field id
    +    if (fieldID > lastFieldId && fieldID - lastFieldId <= 15)
    +    {
    +        // Write them together
    +        [self writeByteDirect:(fieldID - lastFieldId) << 4 | typeToWrite];
    +    }
    +    else
    +    {
    +        // Write them separate
    +        [self writeByteDirect:typeToWrite];
    +        [self writeI16:fieldID];
    +    }
    +    
    +    lastFieldId = fieldID;
    +}
    +
    +-(void)writeFieldStop
    +{
    +    [self writeByteDirect:TCType_STOP];
    +}
    +
    +-(void)writeMapBeginWithKeyType:(int)keyType valueType:(int)valueType 
size:(int)size
    +{
    +    if (size == 0)
    +    {
    +        [self writeByteDirect:0];
    +    }
    +    else
    +    {
    +        [self writeVarint32:(uint32_t)size];
    +        [self writeByteDirect:[self getCompactTypeForTType:keyType] << 4 | 
[self getCompactTypeForTType:valueType]];
    +    }
    +}
    +
    +-(void)writeListBeginWithElementType:(int)elementType size:(int)size
    +{
    +    [self writeCollectionBeginWithElementType:elementType size:size];
    +}
    +
    +-(void)writeSetBeginWithElementType:(int)elementType size:(int)size
    +{
    +    [self writeCollectionBeginWithElementType:elementType size:size];
    +}
    +
    +-(void)writeBool:(BOOL)b
    +{
    +    if (boolFieldId != nil && boolFieldName != nil && boolFieldType != nil)
    +    {
    +        // we haven't written the field header yet
    +        [self writeFieldBeginInternalWithName:boolFieldName 
type:[boolFieldType intValue] fieldID:[boolFieldId intValue] typeOverride:b ? 
TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE];
    +        
    +        [boolFieldId release_stub];
    +        [boolFieldName release_stub];
    +        [boolFieldType release_stub];
    +        
    +        boolFieldId = nil;
    +        boolFieldName = nil;
    +        boolFieldType = nil;
    +    }
    +    else
    +    {
    +        // we're not part of a field, so just Write the value.
    +        [self writeByteDirect:b ? TCType_BOOLEAN_TRUE : 
TCType_BOOLEAN_FALSE];
    +    }
    +}
    +
    +-(void)writeByte:(uint8_t)value
    +{
    +    [self writeByteDirect:value];
    +}
    +
    +-(void)writeI16:(short)value
    +{
    +    [self writeVarint32:[self i32ToZigZag:value]];
    +}
    +
    +-(void)writeI32:(int32_t)value
    +{
    +    [self writeVarint32:[self i32ToZigZag:value]];
    +}
    +
    +-(void)writeI64:(int64_t)value
    +{
    +    [self writeVarint64:[self i64ToZigZag:value]];
    +}
    +
    +-(void)writeDouble:(double)value
    +{
    +    //Safe bit-casting double->uint64
    +    uint8_t data[8];
    +    uint64_t bits = 0;
    +    memcpy(data, &value, 8);
    +    memcpy(&bits, data, 8);
    +    
    +    bits = OSSwapHostToLittleInt64(bits);
    +    [mTransport write:(uint8_t*)&bits offset:0 length:8];
    +}
    +
    +-(void)writeString:(NSString*)value
    +{
    +    [self writeBinary:[value dataUsingEncoding:NSUTF8StringEncoding]];
    +}
    +
    +-(void)writeBinary:(NSData*)data
    +{
    +    [self writeVarint32:(uint32_t)data.length];
    +    [mTransport write:data.bytes offset:0 length:data.length];
    +}
    +
    +-(void)writeMessageEnd {}
    +-(void)writeMapEnd {}
    +-(void)writeListEnd {}
    +-(void)writeSetEnd {}
    +-(void)writeFieldEnd {}
    +
    +-(void)writeCollectionBeginWithElementType:(int)elementType size:(int)size
    +{
    +    if (size <= 14)
    +    {
    +        [self writeByteDirect:size << 4 | [self 
getCompactTypeForTType:elementType]];
    +    }
    +    else
    +    {
    +        [self writeByteDirect:0xf0 | [self 
getCompactTypeForTType:elementType]];
    +        [self writeVarint32:(uint32_t)size];
    +    }
    +}
    +
    +-(void)writeVarint64:(UInt64)n
    --- End diff --
    
    We should consistently use either the Carbon-style `UInt64` or the C-style 
`uint_64_t` throughout here. Currently it's a mix - the method body uses 
`uint8_t` 2 lines below here, for example.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to