Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CustomPSPDFThreadSafeMutableDictionary.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // CustomPSPDFThreadSafeMutableDictionary.m
  3. //
  4. // renamed from PSPDFThreadSafeMutableDictionary.m
  5. //
  6. // Copyright (c) 2019-present Christopher J. Brody (aka Chris Brody)
  7. //
  8. // Copyright (c) 2013 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. #import <Foundation/Foundation.h>
  28. // Dictionary-Subclasss whose primitive operations are thread safe.
  29. @interface CustomPSPDFThreadSafeMutableDictionary : NSMutableDictionary
  30. @end
  31. // ----------------------------------------------------------------
  32. //
  33. // CustomPSPDFThreadSafeMutableDictionary.m
  34. //
  35. // renamed from PSPDFThreadSafeMutableDictionary.m
  36. //
  37. // PSPDFKit
  38. //
  39. // Copyright (c) 2013 PSPDFKit GmbH. All rights reserved.
  40. //
  41. // #import "PSPDFThreadSafeMutableDictionary.h"
  42. #import <libkern/OSAtomic.h>
  43. #define LOCKED(...) OSSpinLockLock(&_lock); \
  44. __VA_ARGS__; \
  45. OSSpinLockUnlock(&_lock);
  46. @implementation CustomPSPDFThreadSafeMutableDictionary {
  47. OSSpinLock _lock;
  48. NSMutableDictionary *_dictionary; // Class Cluster!
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////////////////
  51. #pragma mark - NSObject
  52. - (id)init {
  53. return [self initWithCapacity:0];
  54. }
  55. - (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
  56. if ((self = [self initWithCapacity:objects.count])) {
  57. [objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  58. _dictionary[keys[idx]] = obj;
  59. }];
  60. }
  61. return self;
  62. }
  63. - (id)initWithCapacity:(NSUInteger)capacity {
  64. if ((self = [super init])) {
  65. _dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
  66. _lock = OS_SPINLOCK_INIT;
  67. }
  68. return self;
  69. }
  70. ///////////////////////////////////////////////////////////////////////////////////////////
  71. #pragma mark - NSMutableDictionary
  72. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  73. LOCKED(_dictionary[aKey] = anObject)
  74. }
  75. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary {
  76. LOCKED([_dictionary addEntriesFromDictionary:otherDictionary]);
  77. }
  78. - (void)setDictionary:(NSDictionary *)otherDictionary {
  79. LOCKED([_dictionary setDictionary:otherDictionary]);
  80. }
  81. - (void)removeObjectForKey:(id)aKey {
  82. LOCKED([_dictionary removeObjectForKey:aKey])
  83. }
  84. - (void)removeAllObjects {
  85. LOCKED([_dictionary removeAllObjects]);
  86. }
  87. - (NSUInteger)count {
  88. LOCKED(NSUInteger count = _dictionary.count)
  89. return count;
  90. }
  91. - (NSArray *)allKeys {
  92. LOCKED(NSArray *allKeys = _dictionary.allKeys)
  93. return allKeys;
  94. }
  95. - (NSArray *)allValues {
  96. LOCKED(NSArray *allValues = _dictionary.allValues)
  97. return allValues;
  98. }
  99. - (id)objectForKey:(id)aKey {
  100. LOCKED(id obj = _dictionary[aKey])
  101. return obj;
  102. }
  103. - (NSEnumerator *)keyEnumerator {
  104. LOCKED(NSEnumerator *keyEnumerator = [_dictionary keyEnumerator])
  105. return keyEnumerator;
  106. }
  107. - (id)copyWithZone:(NSZone *)zone {
  108. return [self mutableCopyWithZone:zone];
  109. }
  110. - (id)mutableCopyWithZone:(NSZone *)zone {
  111. LOCKED(id copiedDictionary = [[self.class allocWithZone:zone] initWithDictionary:_dictionary])
  112. return copiedDictionary;
  113. }
  114. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  115. objects:(id __unsafe_unretained [])stackbuf
  116. count:(NSUInteger)len {
  117. LOCKED(NSUInteger count = [[_dictionary copy] countByEnumeratingWithState:state objects:stackbuf count:len]);
  118. return count;
  119. }
  120. - (void)performLockedWithDictionary:(void (^)(NSDictionary *dictionary))block {
  121. if (block) LOCKED(block(_dictionary));
  122. }
  123. - (BOOL)isEqual:(id)object {
  124. if (object == self) return YES;
  125. if ([object isKindOfClass:CustomPSPDFThreadSafeMutableDictionary.class]) {
  126. CustomPSPDFThreadSafeMutableDictionary *other = object;
  127. __block BOOL isEqual = NO;
  128. [other performLockedWithDictionary:^(NSDictionary *dictionary) {
  129. [self performLockedWithDictionary:^(NSDictionary *otherDictionary) {
  130. isEqual = [dictionary isEqual:otherDictionary];
  131. }];
  132. }];
  133. return isEqual;
  134. }
  135. return NO;
  136. }
  137. @end