QGVAPSafeMutableArray.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // QGVAPSafeMutableArray.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGVAPSafeMutableArray.h"
  16. #define VAP_INIT(...) self = super.init; \
  17. if (!self) return nil; \
  18. __VA_ARGS__; \
  19. if (!_arr) return nil; \
  20. _lock = [[NSRecursiveLock alloc] init]; \
  21. return self;
  22. #define VAP_LOCK(...) [_lock lock]; \
  23. __VA_ARGS__; \
  24. [_lock unlock];
  25. @interface QGVAPSafeMutableArray (){
  26. NSMutableArray *_arr;
  27. NSRecursiveLock *_lock;
  28. }
  29. @end
  30. @implementation QGVAPSafeMutableArray
  31. #pragma mark - init
  32. -(instancetype)init{
  33. VAP_INIT(_arr = [NSMutableArray new]);
  34. }
  35. -(instancetype)initWithCapacity:(NSUInteger)numItems{
  36. VAP_INIT(_arr = [[NSMutableArray alloc] initWithCapacity:numItems]);
  37. }
  38. -(instancetype)initWithArray:(NSArray *)array{
  39. VAP_INIT(_arr = [[NSMutableArray alloc] initWithArray:array]);
  40. }
  41. -(instancetype)initWithObjects:(id _Nonnull const [])objects count:(NSUInteger)cnt{
  42. VAP_INIT(_arr = [[NSMutableArray alloc] initWithObjects:objects count:cnt]);
  43. }
  44. - (instancetype)initWithContentsOfFile:(NSString *)path {
  45. VAP_INIT(_arr = [[NSMutableArray alloc] initWithContentsOfFile:path]);
  46. }
  47. - (instancetype)initWithContentsOfURL:(NSURL *)url {
  48. VAP_INIT(_arr = [[NSMutableArray alloc] initWithContentsOfURL:url]);
  49. }
  50. #pragma mark - methods
  51. - (NSUInteger)count {
  52. VAP_LOCK(NSUInteger count = _arr.count); return count;
  53. }
  54. - (id)objectAtIndex:(NSUInteger)index {
  55. VAP_LOCK(id obj = [_arr objectAtIndex:index]); return obj;
  56. }
  57. - (NSArray *)arrayByAddingObject:(id)anObject {
  58. VAP_LOCK(NSArray * arr = [_arr arrayByAddingObject:anObject]); return arr;
  59. }
  60. - (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray {
  61. VAP_LOCK(NSArray * arr = [_arr arrayByAddingObjectsFromArray:otherArray]); return arr;
  62. }
  63. - (NSString *)componentsJoinedByString:(NSString *)separator {
  64. VAP_LOCK(NSString * str = [_arr componentsJoinedByString:separator]); return str;
  65. }
  66. - (BOOL)containsObject:(id)anObject {
  67. VAP_LOCK(BOOL c = [_arr containsObject:anObject]); return c;
  68. }
  69. - (NSString *)description {
  70. VAP_LOCK(NSString * d = _arr.description); return d;
  71. }
  72. - (NSString *)descriptionWithLocale:(id)locale {
  73. VAP_LOCK(NSString * d = [_arr descriptionWithLocale:locale]); return d;
  74. }
  75. - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
  76. VAP_LOCK(NSString * d = [_arr descriptionWithLocale:locale indent:level]); return d;
  77. }
  78. - (id)firstObjectCommonWithArray:(NSArray *)otherArray {
  79. VAP_LOCK(id o = [_arr firstObjectCommonWithArray:otherArray]); return o;
  80. }
  81. - (void)getObjects:(id __unsafe_unretained[])objects range:(NSRange)range {
  82. VAP_LOCK([_arr getObjects:objects range:range]);
  83. }
  84. - (NSUInteger)indexOfObject:(id)anObject {
  85. VAP_LOCK(NSUInteger i = [_arr indexOfObject:anObject]); return i;
  86. }
  87. - (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range {
  88. VAP_LOCK(NSUInteger i = [_arr indexOfObject:anObject inRange:range]); return i;
  89. }
  90. - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject {
  91. VAP_LOCK(NSUInteger i = [_arr indexOfObjectIdenticalTo:anObject]); return i;
  92. }
  93. - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range {
  94. VAP_LOCK(NSUInteger i = [_arr indexOfObjectIdenticalTo:anObject inRange:range]); return i;
  95. }
  96. - (id)firstObject {
  97. VAP_LOCK(id o = _arr.firstObject); return o;
  98. }
  99. - (id)lastObject {
  100. VAP_LOCK(id o = _arr.lastObject); return o;
  101. }
  102. - (NSEnumerator *)objectEnumerator {
  103. VAP_LOCK(NSEnumerator * e = [_arr objectEnumerator]); return e;
  104. }
  105. - (NSEnumerator *)reverseObjectEnumerator {
  106. VAP_LOCK(NSEnumerator * e = [_arr reverseObjectEnumerator]); return e;
  107. }
  108. - (NSData *)sortedArrayHint {
  109. VAP_LOCK(NSData * d = [_arr sortedArrayHint]); return d;
  110. }
  111. - (NSArray *)sortedArrayUsingFunction:(__attribute__((noescape)) NSInteger (*)(id, id, void *))comparator context:(void *)context {
  112. VAP_LOCK(NSArray * arr = [_arr sortedArrayUsingFunction:comparator context:context]) return arr;
  113. }
  114. - (NSArray *)sortedArrayUsingFunction:(__attribute__((noescape)) NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint {
  115. VAP_LOCK(NSArray * arr = [_arr sortedArrayUsingFunction:comparator context:context hint:hint]); return arr;
  116. }
  117. - (NSArray *)sortedArrayUsingSelector:(SEL)comparator {
  118. VAP_LOCK(NSArray * arr = [_arr sortedArrayUsingSelector:comparator]); return arr;
  119. }
  120. - (NSArray *)subarrayWithRange:(NSRange)range {
  121. VAP_LOCK(NSArray * arr = [_arr subarrayWithRange:range]) return arr;
  122. }
  123. - (void)makeObjectsPerformSelector:(SEL)aSelector {
  124. VAP_LOCK([_arr makeObjectsPerformSelector:aSelector]);
  125. }
  126. - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument {
  127. VAP_LOCK([_arr makeObjectsPerformSelector:aSelector withObject:argument]);
  128. }
  129. - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes {
  130. VAP_LOCK(NSArray * arr = [_arr objectsAtIndexes:indexes]); return arr;
  131. }
  132. - (id)objectAtIndexedSubscript:(NSUInteger)idx {
  133. VAP_LOCK(id o = [_arr objectAtIndexedSubscript:idx]); return o;
  134. }
  135. - (void)enumerateObjectsUsingBlock:(__attribute__((noescape)) void (^)(id obj, NSUInteger idx, BOOL *stop))block {
  136. VAP_LOCK([_arr enumerateObjectsUsingBlock:block]);
  137. }
  138. - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(__attribute__((noescape)) void (^)(id obj, NSUInteger idx, BOOL *stop))block {
  139. VAP_LOCK([_arr enumerateObjectsWithOptions:opts usingBlock:block]);
  140. }
  141. - (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(__attribute__((noescape)) void (^)(id obj, NSUInteger idx, BOOL *stop))block {
  142. VAP_LOCK([_arr enumerateObjectsAtIndexes:s options:opts usingBlock:block]);
  143. }
  144. - (NSUInteger)indexOfObjectPassingTest:(__attribute__((noescape)) BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate {
  145. VAP_LOCK(NSUInteger i = [_arr indexOfObjectPassingTest:predicate]); return i;
  146. }
  147. - (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(__attribute__((noescape)) BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate {
  148. VAP_LOCK(NSUInteger i = [_arr indexOfObjectWithOptions:opts passingTest:predicate]); return i;
  149. }
  150. - (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(__attribute__((noescape)) BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate {
  151. VAP_LOCK(NSUInteger i = [_arr indexOfObjectAtIndexes:s options:opts passingTest:predicate]); return i;
  152. }
  153. - (NSIndexSet *)indexesOfObjectsPassingTest:(__attribute__((noescape)) BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate {
  154. VAP_LOCK(NSIndexSet * i = [_arr indexesOfObjectsPassingTest:predicate]); return i;
  155. }
  156. - (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(__attribute__((noescape)) BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate {
  157. VAP_LOCK(NSIndexSet * i = [_arr indexesOfObjectsWithOptions:opts passingTest:predicate]); return i;
  158. }
  159. - (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(__attribute__((noescape)) BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate {
  160. VAP_LOCK(NSIndexSet * i = [_arr indexesOfObjectsAtIndexes:s options:opts passingTest:predicate]); return i;
  161. }
  162. - (NSArray *)sortedArrayUsingComparator:(__attribute__((noescape)) NSComparator)cmptr {
  163. VAP_LOCK(NSArray * a = [_arr sortedArrayUsingComparator:cmptr]); return a;
  164. }
  165. - (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(__attribute__((noescape)) NSComparator)cmptr {
  166. VAP_LOCK(NSArray * a = [_arr sortedArrayWithOptions:opts usingComparator:cmptr]); return a;
  167. }
  168. - (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(__attribute__((noescape)) NSComparator)cmp {
  169. VAP_LOCK(NSUInteger i = [_arr indexOfObject:obj inSortedRange:r options:opts usingComparator:cmp]); return i;
  170. }
  171. #pragma mark - mutable
  172. - (void)addObject:(id)anObject {
  173. VAP_LOCK([_arr addObject:anObject]);
  174. }
  175. - (void)insertObject:(id)anObject atIndex:(NSUInteger)index {
  176. VAP_LOCK([_arr insertObject:anObject atIndex:index]);
  177. }
  178. - (void)removeLastObject {
  179. VAP_LOCK([_arr removeLastObject]);
  180. }
  181. - (void)removeObjectAtIndex:(NSUInteger)index {
  182. VAP_LOCK([_arr removeObjectAtIndex:index]);
  183. }
  184. - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
  185. VAP_LOCK([_arr replaceObjectAtIndex:index withObject:anObject]);
  186. }
  187. - (void)addObjectsFromArray:(NSArray *)otherArray {
  188. VAP_LOCK([_arr addObjectsFromArray:otherArray]);
  189. }
  190. - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2 {
  191. VAP_LOCK([_arr exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2]);
  192. }
  193. - (void)removeAllObjects {
  194. VAP_LOCK([_arr removeAllObjects]);
  195. }
  196. - (void)removeObject:(id)anObject inRange:(NSRange)range {
  197. VAP_LOCK([_arr removeObject:anObject inRange:range]);
  198. }
  199. - (void)removeObject:(id)anObject {
  200. VAP_LOCK([_arr removeObject:anObject]);
  201. }
  202. - (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range {
  203. VAP_LOCK([_arr removeObjectIdenticalTo:anObject inRange:range]);
  204. }
  205. - (void)removeObjectIdenticalTo:(id)anObject {
  206. VAP_LOCK([_arr removeObjectIdenticalTo:anObject]);
  207. }
  208. - (void)removeObjectsInArray:(NSArray *)otherArray {
  209. VAP_LOCK([_arr removeObjectsInArray:otherArray]);
  210. }
  211. - (void)removeObjectsInRange:(NSRange)range {
  212. VAP_LOCK([_arr removeObjectsInRange:range]);
  213. }
  214. - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange {
  215. VAP_LOCK([_arr replaceObjectsInRange:range withObjectsFromArray:otherArray range:otherRange]);
  216. }
  217. - (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray {
  218. VAP_LOCK([_arr replaceObjectsInRange:range withObjectsFromArray:otherArray]);
  219. }
  220. - (void)setArray:(NSArray *)otherArray {
  221. VAP_LOCK([_arr setArray:otherArray]);
  222. }
  223. - (void)sortUsingFunction:(__attribute__((noescape)) NSInteger (*)(id, id, void *))compare context:(void *)context {
  224. VAP_LOCK([_arr sortUsingFunction:compare context:context]);
  225. }
  226. - (void)sortUsingSelector:(SEL)comparator {
  227. VAP_LOCK([_arr sortUsingSelector:comparator]);
  228. }
  229. - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes {
  230. VAP_LOCK([_arr insertObjects:objects atIndexes:indexes]);
  231. }
  232. - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes {
  233. VAP_LOCK([_arr removeObjectsAtIndexes:indexes]);
  234. }
  235. - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects {
  236. VAP_LOCK([_arr replaceObjectsAtIndexes:indexes withObjects:objects]);
  237. }
  238. - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
  239. VAP_LOCK([_arr setObject:obj atIndexedSubscript:idx]);
  240. }
  241. - (void)sortUsingComparator:(__attribute__((noescape)) NSComparator)cmptr {
  242. VAP_LOCK([_arr sortUsingComparator:cmptr]);
  243. }
  244. - (void)sortWithOptions:(NSSortOptions)opts usingComparator:(__attribute__((noescape)) NSComparator)cmptr {
  245. VAP_LOCK([_arr sortWithOptions:opts usingComparator:cmptr]);
  246. }
  247. - (BOOL)isEqualToArray:(NSArray *)otherArray {
  248. if (otherArray == self) return YES;
  249. if ([otherArray isKindOfClass:QGVAPSafeMutableArray.class]) {
  250. QGVAPSafeMutableArray *other = (id)otherArray;
  251. BOOL isEqual;
  252. [_lock lock];
  253. [other->_lock lock];
  254. isEqual = [_arr isEqualToArray:other->_arr];
  255. [other->_lock unlock];
  256. [_lock unlock];
  257. return isEqual;
  258. }
  259. return NO;
  260. }
  261. #pragma mark - protocol
  262. - (id)copyWithZone:(NSZone *)zone {
  263. return [self mutableCopyWithZone:zone];
  264. }
  265. - (id)mutableCopyWithZone:(NSZone *)zone {
  266. VAP_LOCK(id copiedArr = [[self.class allocWithZone:zone] initWithArray:_arr]);
  267. return copiedArr;
  268. }
  269. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  270. objects:(id __unsafe_unretained[])stackbuf
  271. count:(NSUInteger)len {
  272. VAP_LOCK(NSUInteger count = [_arr countByEnumeratingWithState:state objects:stackbuf count:len]);
  273. return count;
  274. }
  275. - (BOOL)isEqual:(id)object {
  276. if (object == self) return YES;
  277. if ([object isKindOfClass:[QGVAPSafeMutableArray class]]) {
  278. QGVAPSafeMutableArray *other = object;
  279. BOOL isEqual;
  280. [_lock lock];
  281. [other->_lock lock];
  282. isEqual = [_arr isEqual:other->_arr];
  283. [other->_lock unlock];
  284. [_lock unlock];
  285. return isEqual;
  286. }
  287. return NO;
  288. }
  289. - (NSUInteger)hash {
  290. VAP_LOCK(NSUInteger hash = [_arr hash]);
  291. return hash;
  292. }
  293. @end