AEArray.h
Go to the documentation of this file.
1 //
2 // AEArray.h
3 // TheAmazingAudioEngine
4 //
5 // Created by Michael Tyson on 30/03/2016.
6 // Copyright © 2016 A Tasty Pixel. All rights reserved.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source distribution.
25 //
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #import <Foundation/Foundation.h>
32 
33 typedef const void * AEArrayToken;
34 
50 typedef void * _Nullable (^AEArrayCustomMappingBlock)(id _Nonnull item);
51 
62 typedef void * _Nullable (^AEArrayIndexedCustomMappingBlock)(id _Nonnull item, int index);
63 
74 typedef void (^AEArrayReleaseBlock)(id _Nonnull item, void * _Nonnull bytes);
75 
76 // Some indirection macros required for AEArrayEnumerate
77 #define __AEArrayVar2(x, y) x ## y
78 #define __AEArrayVar(x, y) __AEArrayVar2(__ ## x ## _line_, y)
79 
101 @interface AEArray : NSObject <NSFastEnumeration>
102 
109 - (instancetype _Nonnull)init;
110 
123 - (instancetype _Nullable)initWithCustomMapping:(AEArrayCustomMappingBlock _Nullable)block;
124 
139 - (void)updateWithContentsOfArray:(NSArray * _Nonnull)array;
140 
158 - (void)updateWithContentsOfArray:(NSArray * _Nonnull)array customMapping:(AEArrayIndexedCustomMappingBlock _Nullable)block;
159 
174 - (void * _Nullable)pointerValueAtIndex:(int)index;
175 
190 - (void * _Nullable)pointerValueForObject:(id _Nonnull)object;
191 
205 - (void)updatePointerValue:(void * _Nullable)value forObject:(id _Nonnull)object;
206 
210 - (id _Nullable)objectAtIndexedSubscript:(NSUInteger)idx;
211 
226 AEArrayToken _Nonnull AEArrayGetToken(__unsafe_unretained AEArray * _Nonnull array);
227 
234 int AEArrayGetCount(AEArrayToken _Nonnull token);
235 
243 void * _Nullable AEArrayGetItem(AEArrayToken _Nonnull token, int index);
244 
266 #define AEArrayEnumerateObjects(array, type, varname) \
267  AEArrayToken __AEArrayVar(token, __LINE__) = AEArrayGetToken(array); \
268  int __AEArrayVar(count, __LINE__) = AEArrayGetCount(__AEArrayVar(token, __LINE__)); \
269  int __AEArrayVar(i, __LINE__) = 0; \
270  for ( __unsafe_unretained type varname = __AEArrayVar(count, __LINE__) > 0 ? (__bridge type)AEArrayGetItem(__AEArrayVar(token, __LINE__), 0) : NULL; \
271  __AEArrayVar(i, __LINE__) < __AEArrayVar(count, __LINE__); \
272  __AEArrayVar(i, __LINE__)++, varname = __AEArrayVar(i, __LINE__) < __AEArrayVar(count, __LINE__) ? \
273  (__bridge type)AEArrayGetItem(__AEArrayVar(token, __LINE__), __AEArrayVar(i, __LINE__)) : NULL )
274 
297 #define AEArrayEnumeratePointers(array, type, varname) \
298  AEArrayToken __AEArrayVar(token, __LINE__) = AEArrayGetToken(array); \
299  int __AEArrayVar(count, __LINE__) = AEArrayGetCount(__AEArrayVar(token, __LINE__)); \
300  int __AEArrayVar(i, __LINE__) = 0; \
301  for ( type varname = __AEArrayVar(count, __LINE__) > 0 ? (type)AEArrayGetItem(__AEArrayVar(token, __LINE__), 0) : NULL; \
302  __AEArrayVar(i, __LINE__) < __AEArrayVar(count, __LINE__); \
303  __AEArrayVar(i, __LINE__)++, varname = __AEArrayVar(i, __LINE__) < __AEArrayVar(count, __LINE__) ? \
304  (type)AEArrayGetItem(__AEArrayVar(token, __LINE__), __AEArrayVar(i, __LINE__)) : NULL )
305 
306 
308 @property (nonatomic, readonly) int count;
309 
311 @property (nonatomic, strong, readonly) NSArray * _Nonnull allValues;
312 
315 @property (nonatomic, copy) AEArrayReleaseBlock _Nullable releaseBlock;
316 
317 @end
318 
319 #ifdef __cplusplus
320 }
321 #endif