QGVAPSafeMutableDictionary.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // QGVAPSafeMutableDictionary.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 "QGVAPSafeMutableDictionary.h"
  16. #define VAP_INIT(...) self = super.init; \
  17. if (!self) return nil; \
  18. __VA_ARGS__; \
  19. if (!_dic) return nil; \
  20. _lock = [[NSRecursiveLock alloc] init]; \
  21. return self;
  22. #define VAP_LOCK(...) [_lock lock]; \
  23. __VA_ARGS__; \
  24. [_lock unlock];
  25. @interface QGVAPSafeMutableDictionary(){
  26. NSMutableDictionary *_dic;
  27. NSRecursiveLock *_lock;
  28. }
  29. @end
  30. @implementation QGVAPSafeMutableDictionary
  31. #pragma mark - init
  32. - (instancetype)init {
  33. VAP_INIT(_dic = [[NSMutableDictionary alloc] init]);
  34. }
  35. - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
  36. VAP_INIT(_dic = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys]);
  37. }
  38. - (instancetype)initWithCapacity:(NSUInteger)capacity {
  39. VAP_INIT(_dic = [[NSMutableDictionary alloc] initWithCapacity:capacity]);
  40. }
  41. - (instancetype)initWithObjects:(const id[])objects forKeys:(const id <NSCopying>[])keys count:(NSUInteger)cnt {
  42. VAP_INIT(_dic = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys count:cnt]);
  43. }
  44. - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary {
  45. VAP_INIT(_dic = [[NSMutableDictionary alloc] initWithDictionary:otherDictionary]);
  46. }
  47. - (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag {
  48. VAP_INIT(_dic = [[NSMutableDictionary alloc] initWithDictionary:otherDictionary copyItems:flag]);
  49. }
  50. #pragma mark - method
  51. - (NSUInteger)count {
  52. VAP_LOCK(NSUInteger c = _dic.count); return c;
  53. }
  54. - (id)objectForKey:(id)aKey {
  55. VAP_LOCK(id o = [_dic objectForKey:aKey]); return o;
  56. }
  57. - (NSEnumerator *)keyEnumerator {
  58. VAP_LOCK(NSEnumerator * e = [_dic keyEnumerator]); return e;
  59. }
  60. - (NSArray *)allKeys {
  61. VAP_LOCK(NSArray * a = [_dic allKeys]); return a;
  62. }
  63. - (NSArray *)allKeysForObject:(id)anObject {
  64. VAP_LOCK(NSArray * a = [_dic allKeysForObject:anObject]); return a;
  65. }
  66. - (NSArray *)allValues {
  67. VAP_LOCK(NSArray * a = [_dic allValues]); return a;
  68. }
  69. - (NSString *)description {
  70. VAP_LOCK(NSString * d = [_dic description]); return d;
  71. }
  72. - (NSString *)descriptionInStringsFileFormat {
  73. VAP_LOCK(NSString * d = [_dic descriptionInStringsFileFormat]); return d;
  74. }
  75. - (NSString *)descriptionWithLocale:(id)locale {
  76. VAP_LOCK(NSString * d = [_dic descriptionWithLocale:locale]); return d;
  77. }
  78. - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
  79. VAP_LOCK(NSString * d = [_dic descriptionWithLocale:locale indent:level]); return d;
  80. }
  81. - (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary {
  82. if (otherDictionary == self) return YES;
  83. if ([otherDictionary isKindOfClass:QGVAPSafeMutableDictionary.class]) {
  84. QGVAPSafeMutableDictionary *other = (id)otherDictionary;
  85. BOOL isEqual;
  86. [_lock lock];
  87. [other->_lock lock];
  88. isEqual = [_dic isEqual:other->_dic];
  89. [other->_lock unlock];
  90. [_lock unlock];
  91. return isEqual;
  92. }
  93. return NO;
  94. }
  95. - (NSEnumerator *)objectEnumerator {
  96. VAP_LOCK(NSEnumerator * e = [_dic objectEnumerator]); return e;
  97. }
  98. - (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker {
  99. VAP_LOCK(NSArray * a = [_dic objectsForKeys:keys notFoundMarker:marker]); return a;
  100. }
  101. - (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator {
  102. VAP_LOCK(NSArray * a = [_dic keysSortedByValueUsingSelector:comparator]); return a;
  103. }
  104. - (void)getObjects:(id __unsafe_unretained[])objects andKeys:(id __unsafe_unretained[])keys {
  105. VAP_LOCK([_dic getObjects:objects andKeys:keys]);
  106. }
  107. - (id)objectForKeyedSubscript:(id)key {
  108. VAP_LOCK(id o = [_dic objectForKeyedSubscript:key]); return o;
  109. }
  110. - (void)enumerateKeysAndObjectsUsingBlock:(__attribute__((noescape)) void (^)(id key, id obj, BOOL *stop))block {
  111. VAP_LOCK([_dic enumerateKeysAndObjectsUsingBlock:block]);
  112. }
  113. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(__attribute__((noescape)) void (^)(id key, id obj, BOOL *stop))block {
  114. VAP_LOCK([_dic enumerateKeysAndObjectsWithOptions:opts usingBlock:block]);
  115. }
  116. - (NSArray *)keysSortedByValueUsingComparator:(__attribute__((noescape)) NSComparator)cmptr {
  117. VAP_LOCK(NSArray * a = [_dic keysSortedByValueUsingComparator:cmptr]); return a;
  118. }
  119. - (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(__attribute__((noescape)) NSComparator)cmptr {
  120. VAP_LOCK(NSArray * a = [_dic keysSortedByValueWithOptions:opts usingComparator:cmptr]); return a;
  121. }
  122. - (NSSet *)keysOfEntriesPassingTest:(__attribute__((noescape)) BOOL (^)(id key, id obj, BOOL *stop))predicate {
  123. VAP_LOCK(NSSet * a = [_dic keysOfEntriesPassingTest:predicate]); return a;
  124. }
  125. - (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(__attribute__((noescape)) BOOL (^)(id key, id obj, BOOL *stop))predicate {
  126. VAP_LOCK(NSSet * a = [_dic keysOfEntriesWithOptions:opts passingTest:predicate]); return a;
  127. }
  128. #pragma mark - mutable
  129. - (void)removeObjectForKey:(id)aKey {
  130. VAP_LOCK([_dic removeObjectForKey:aKey]);
  131. }
  132. - (void)setObject:(id)anObject forKey:(id <NSCopying> )aKey {
  133. VAP_LOCK([_dic setObject:anObject forKey:aKey]);
  134. }
  135. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary {
  136. VAP_LOCK([_dic addEntriesFromDictionary:otherDictionary]);
  137. }
  138. - (void)removeAllObjects {
  139. VAP_LOCK([_dic removeAllObjects]);
  140. }
  141. - (void)removeObjectsForKeys:(NSArray *)keyArray {
  142. VAP_LOCK([_dic removeObjectsForKeys:keyArray]);
  143. }
  144. - (void)setDictionary:(NSDictionary *)otherDictionary {
  145. VAP_LOCK([_dic setDictionary:otherDictionary]);
  146. }
  147. - (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying> )key {
  148. VAP_LOCK([_dic setObject:obj forKeyedSubscript:key]);
  149. }
  150. #pragma mark - protocol
  151. - (id)copyWithZone:(NSZone *)zone {
  152. return [self mutableCopyWithZone:zone];
  153. }
  154. - (id)mutableCopyWithZone:(NSZone *)zone {
  155. VAP_LOCK(id copiedDictionary = [[self.class allocWithZone:zone] initWithDictionary:_dic]);
  156. return copiedDictionary;
  157. }
  158. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  159. objects:(id __unsafe_unretained[])stackbuf
  160. count:(NSUInteger)len {
  161. VAP_LOCK(NSUInteger count = [_dic countByEnumeratingWithState:state objects:stackbuf count:len]);
  162. return count;
  163. }
  164. - (BOOL)isEqual:(id)object {
  165. if (object == self) return YES;
  166. if ([object isKindOfClass:QGVAPSafeMutableDictionary.class]) {
  167. QGVAPSafeMutableDictionary *other = object;
  168. BOOL isEqual;
  169. [_lock lock];
  170. [other->_lock lock];
  171. isEqual = [_dic isEqual:other->_dic];
  172. [other->_lock unlock];
  173. [_lock unlock];
  174. return isEqual;
  175. }
  176. return NO;
  177. }
  178. - (NSUInteger)hash {
  179. VAP_LOCK(NSUInteger hash = [_dic hash]);
  180. return hash;
  181. }
  182. @end