PBArray.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Protocol Buffers for Objective C
  2. //
  3. // Copyright 2010 Booyah Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. // Author: Jon Parise <jon@booyah.com>
  18. #import <Foundation/Foundation.h>
  19. extern NSString * const PBArrayTypeMismatchException;
  20. extern NSString * const PBArrayNumberExpectedException;
  21. extern NSString * const PBArrayAllocationFailureException;
  22. typedef enum _PBArrayValueType
  23. {
  24. PBArrayValueTypeBool,
  25. PBArrayValueTypeInt32,
  26. PBArrayValueTypeUInt32,
  27. PBArrayValueTypeInt64,
  28. PBArrayValueTypeUInt64,
  29. PBArrayValueTypeFloat,
  30. PBArrayValueTypeDouble,
  31. } PBArrayValueType;
  32. // PBArray is an immutable array class that's optimized for storing primitive
  33. // values. All values stored in an PBArray instance must have the same type
  34. // (PBArrayValueType). Object values (PBArrayValueTypeObject) are retained.
  35. @interface PBArray : NSObject <NSCopying>
  36. {
  37. @protected
  38. PBArrayValueType _valueType;
  39. NSUInteger _capacity;
  40. NSUInteger _count;
  41. void * _data;
  42. }
  43. //Count
  44. - (NSUInteger)count;
  45. //bool At Index
  46. - (BOOL)boolAtIndex:(NSUInteger)index;
  47. //int32 At Index
  48. - (SInt32)int32AtIndex:(NSUInteger)index;
  49. //enum At Index
  50. - (SInt32)enumAtIndex:(NSUInteger)index;
  51. //uint32 At Index
  52. - (UInt32)uint32AtIndex:(NSUInteger)index;
  53. //int64 At Index
  54. - (SInt64)int64AtIndex:(NSUInteger)index;
  55. //uint64 At Index
  56. - (UInt64)uint64AtIndex:(NSUInteger)index;
  57. //float At Index
  58. - (Float32)floatAtIndex:(NSUInteger)index;
  59. //double At Index
  60. - (Float64)doubleAtIndex:(NSUInteger)index;
  61. //Equal To Array
  62. - (BOOL)isEqualToArray:(PBArray *)array;
  63. //enumerate
  64. - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block;
  65. //index Of Object
  66. - (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate;
  67. //This Methods automaticaly pack/unpack in NSNumber primitive values
  68. - (id)firstObject;
  69. //last Object
  70. - (id)lastObject;
  71. //object At Indexed
  72. - (id)objectAtIndexedSubscript:(NSUInteger)idx;
  73. @property (nonatomic,assign,readonly) PBArrayValueType valueType;
  74. @property (nonatomic,assign,readonly) const void * data;
  75. @property (nonatomic,assign,readonly,getter=count) NSUInteger count;
  76. @end
  77. @interface PBArray (PBArrayExtended)
  78. //- (instancetype)arrayByAppendingArray:(PBArray *)array;
  79. - (PBArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate;
  80. @end
  81. @interface PBArray (PBArrayCreation)
  82. //init With PBArrayValueType
  83. + (instancetype)arrayWithValueType:(PBArrayValueType)valueType;
  84. //+ (instancetype)arrayWithValues:(const void *)values count:(NSUInteger)count valueType:(PBArrayValueType)valueType;
  85. + (instancetype)arrayWithArray:(NSArray *)array valueType:(PBArrayValueType)valueType;
  86. //init With PBArrayValueType
  87. - (instancetype)initWithValueType:(PBArrayValueType)valueType;
  88. //init With (values,count,valueType)
  89. - (instancetype)initWithValues:(const void *)values count:(NSUInteger)count valueType:(PBArrayValueType)valueType;
  90. //init With Array
  91. - (instancetype)initWithArray:(NSArray *)array valueType:(PBArrayValueType)valueType;
  92. @end
  93. // PBAppendableArray extends PBArray with the ability to append new values to
  94. // the end of the array.
  95. @interface PBAppendableArray : PBArray
  96. //add BOOL
  97. - (void)addBool:(BOOL)value;
  98. //add Int32
  99. - (void)addInt32:(SInt32)value;
  100. //add Uint32
  101. - (void)addUint32:(UInt32)value;
  102. //add Int64
  103. - (void)addInt64:(SInt64)value;
  104. //add Uint64
  105. - (void)addUint64:(UInt64)value;
  106. //add Float
  107. - (void)addFloat:(Float32)value;
  108. //add Double
  109. - (void)addDouble:(Float64)value;
  110. //add Enum
  111. - (void)addEnum:(SInt32)value;
  112. //append Array
  113. - (void)appendArray:(PBArray *)array;
  114. //append Values
  115. - (void)appendValues:(const void *)values count:(UInt32)count;
  116. @end