Skip to content

Commit

Permalink
Removing the CodedInputStream routines from PBArray.
Browse files Browse the repository at this point in the history
Previously, we were only supporting a subset of the CodedInputStream reading
routines.  In order to support types like Fixed32, we'd need to further expand
our set of array value types simply to recognize these additional stream
reading functions.

Instead, we revert to the earlier approach of hanging all stream operations
externally.  There may be a small performance and code-size penalty in doing
so, but those can be revisited some time in the future.
  • Loading branch information
Jon Parise committed Nov 16, 2010
1 parent a4c9075 commit 491ce76
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 71 deletions.
4 changes: 3 additions & 1 deletion src/compiler/objc_primitive_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,9 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
"if (result.$list_name$ == nil) {\n"
" result.$list_name$ = [PBAppendableArray arrayWithValueType:$array_value_type$];\n"
"}\n"
"[result.$list_name$ appendInputStream:input];\n"
"while (input.bytesUntilLimit > 0) {\n"
" [result.$list_name$ add$array_value_type_name_cap$:[input read$capitalized_type$]];\n"
"}\n"
"[input popLimit:limit];\n");
} else {
printer->Print(variables_,
Expand Down
3 changes: 0 additions & 3 deletions src/runtime/Classes/PBArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#import <Foundation/Foundation.h>

@class PBCodedInputStream;

extern NSString * const PBArrayTypeMismatchException;
extern NSString * const PBArrayNumberExpectedException;
extern NSString * const PBArrayAllocationFailureException;
Expand Down Expand Up @@ -96,6 +94,5 @@ typedef enum _PBArrayValueType

- (void)appendArray:(PBArray *)array;
- (void)appendValues:(const void *)values count:(NSUInteger)count;
- (void)appendInputStream:(PBCodedInputStream *)input;

@end
75 changes: 8 additions & 67 deletions src/runtime/Classes/PBArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// Author: Jon Parise <[email protected]>

#import "PBArray.h"
#import "CodedInputStream.h"

NSString * const PBArrayTypeMismatchException = @"PBArrayTypeMismatchException";
NSString * const PBArrayNumberExpectedException = @"PBArrayNumberExpectedException";
Expand Down Expand Up @@ -62,69 +61,28 @@ static void PBArraySetDoubleValue(NSNumber *number, void *value)
*((Float64 *)value) = [number doubleValue];
}

#pragma mark PBCodedInputStream Getters

typedef void (*PBInputStreamGetter)(PBCodedInputStream *input, void *value);

static void PBStreamGetBoolValue(PBCodedInputStream *input, void *value)
{
*(BOOL *)(value) = [input readBool];
}

static void PBStreamGetInt32Value(PBCodedInputStream *input, void *value)
{
*(int32_t *)(value) = [input readInt32];
}

static void PBStreamGetUInt32Value(PBCodedInputStream *input, void *value)
{
*(uint32_t *)(value) = [input readUInt32];
}

static void PBStreamGetInt64Value(PBCodedInputStream *input, void *value)
{
*(int64_t *)(value) = [input readInt64];
}

static void PBStreamGetUInt64Value(PBCodedInputStream *input, void *value)
{
*(uint64_t *)(value) = [input readUInt64];
}

static void PBStreamGetFloatValue(PBCodedInputStream *input, void *value)
{
*(Float32 *)(value) = [input readFloat];
}

static void PBStreamGetDoubleValue(PBCodedInputStream *input, void *value)
{
*(Float64 *)(value) = [input readDouble];
}

#pragma mark Array Value Types

typedef struct _PBArrayValueTypeInfo
{
const size_t size;
const PBArrayValueSetter setter;
const PBInputStreamGetter getter;
} PBArrayValueTypeInfo;

static PBArrayValueTypeInfo PBValueTypes[] =
{
{ sizeof(id), NULL, NULL },
{ sizeof(BOOL), PBArraySetBoolValue, PBStreamGetBoolValue },
{ sizeof(int32_t), PBArraySetInt32Value, PBStreamGetInt32Value },
{ sizeof(uint32_t), PBArraySetUInt32Value, PBStreamGetUInt32Value },
{ sizeof(int64_t), PBArraySetInt64Value, PBStreamGetInt64Value },
{ sizeof(uint64_t), PBArraySetUInt64Value, PBStreamGetUInt64Value },
{ sizeof(Float32), PBArraySetFloatValue, PBStreamGetFloatValue },
{ sizeof(Float64), PBArraySetDoubleValue, PBStreamGetDoubleValue },
{ sizeof(id), NULL },
{ sizeof(BOOL), PBArraySetBoolValue },
{ sizeof(int32_t), PBArraySetInt32Value },
{ sizeof(uint32_t), PBArraySetUInt32Value },
{ sizeof(int64_t), PBArraySetInt64Value },
{ sizeof(uint64_t), PBArraySetUInt64Value },
{ sizeof(Float32), PBArraySetFloatValue },
{ sizeof(Float64), PBArraySetDoubleValue },
};

#define PBArrayValueTypeSize(type) PBValueTypes[type].size
#define PBArrayValueTypeSetter(type) PBValueTypes[type].setter
#define PBArrayValueTypeGetter(type) PBValueTypes[type].getter

#pragma mark Helper Macros

Expand Down Expand Up @@ -505,21 +463,4 @@ - (void)appendValues:(const void *)values count:(NSUInteger)count
PBArrayForEachObject(values, count, retain);
}

- (void)appendInputStream:(PBCodedInputStream *)input
{
const size_t elementSize = PBArrayValueTypeSize(_valueType);
const size_t originalSize = _count * elementSize;

PBInputStreamGetter getter = PBArrayValueTypeGetter(_valueType);

size_t offset = originalSize;
while (input.bytesUntilLimit > 0)
{
[self ensureAdditionalCapacity:1];
getter(input, _data + offset);
offset += elementSize;
_count++;
}
}

@end

0 comments on commit 491ce76

Please sign in to comment.