-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMNLColorPalette.m
205 lines (163 loc) · 5.74 KB
/
MNLColorPalette.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#import "MNLColorInternal.h"
@interface MNLColorPalette ()
@property (retain, setter=_setDictionary:) NSDictionary *_dictionary;
@end
@implementation MNLColorPalette
{
NSDictionary *_dictionary;
uint64_t _revision;
}
@synthesize _dictionary;
+ (BOOL)supportsSecureCoding {
return YES;
}
- (NSArray *)allKeys {
return [[self _dictionary] allKeys];
}
- (NSArray *)allColors {
return [[self _dictionary] allValues];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
NSArray *allKeys = [coder decodeObjectForKey:@"__allKeys"];
NSMutableDictionary *dictionary = [NSMutableDictionary new];
for (NSString *key in allKeys)
[dictionary setObject:[coder decodeObjectOfClass:[MNLColor class] forKey:key] forKey:key];
_dictionary = [[NSDictionary alloc] initWithDictionary:dictionary];
[dictionary release];
}
return self;
}
- (instancetype)initWithColorPalette:(MNLColorPalette *)colorPalette {
if (self = [super init])
_dictionary = [[NSDictionary alloc] initWithDictionary:[colorPalette _dictionary]];
return self;
}
- (instancetype)initWithDictionary:(NSDictionary *)colorTable {
if (self = [super init])
_dictionary = [[NSDictionary alloc] initWithDictionary:colorTable];
return self;
}
- (id)copyWithZone:(NSZone *)zone {
return [[[self class] allocWithZone:zone] initWithColorPalette:self];
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return [[MNLMutableColorPalette allocWithZone:zone] initWithColorPalette:self];
}
- (void)encodeWithCoder:(NSCoder *)coder {
NSDictionary *dictionary = [[self _dictionary] retain];
NSArray *allKeys = [dictionary allKeys];
[coder encodeObject:allKeys forKey:@"__allKeys"];
for (NSString *key in allKeys)
[coder encodeObject:[dictionary objectForKey:key] forKey:key];
[dictionary release];
}
- (MNLColor *)colorForKey:(NSString *)key {
return [[self _dictionary] objectForKey:key];
}
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *restrict)state objects:(id *)buffer count:(NSUInteger)len {
const CFDictionaryRef restrict dictionary = [self _dictionary];
CFIndex count = CFDictionaryGetCount(dictionary);
if (state->state >= count) {
memset(state, 0, sizeof(NSFastEnumerationState));
memset(buffer, 0, sizeof(id) * len);
return 0;
}
CFStringRef *keys = malloc(count * sizeof(CFStringRef));
CFDictionaryGetKeysAndValues(dictionary, keys, NULL);
CFIndex s = state->state;
for (CFIndex i = 0; s < count && i < len; i++, s++)
buffer[i] = keys[s];
free(keys);
state->state = s;
state->itemsPtr = buffer;
state->mutationsPtr = &_revision;
return count;
}
- (void)dealloc {
[_dictionary release];
[super dealloc];
}
@end
#pragma mark -
@interface MNLMutableColorPalette ()
@property (retain, setter=_setMutableDictionary:) NSMutableDictionary *_mutableDictionary;
@end
@implementation MNLMutableColorPalette
{
uint64_t _revision;
}
- (NSMutableDictionary *)_mutableDictionary {
return (NSMutableDictionary *)[self _dictionary];
}
- (void)_setMutableDictionary:(NSMutableDictionary *)_mutableDictionary {
return [self _setDictionary:_mutableDictionary];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
NSArray *allKeys = [coder decodeObjectForKey:@"__allKeys"];
NSMutableDictionary *dictionary = [NSMutableDictionary new];
for (NSString *key in allKeys)
[dictionary setObject:[coder decodeObjectOfClass:[MNLColor class] forKey:key] forKey: key];
[self _setMutableDictionary:dictionary];
[dictionary release];
_revision = 0;
}
return self;
}
- (instancetype)initWithColorPalette:(MNLColorPalette *)colorPalette {
if (self = [super init]) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:[colorPalette _dictionary]];
[self _setMutableDictionary:dictionary];
[dictionary release];
_revision = 0;
}
return self;
}
- (instancetype)initWithDictionary:(NSDictionary *)colorTable {
if (self = [super init]) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:colorTable];
[self _setMutableDictionary:dictionary];
[dictionary release];
_revision = 0;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
return [[MNLColorPalette allocWithZone:zone] initWithColorPalette:self];
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return [[[self class] allocWithZone:zone] initWithColorPalette:self];
}
- (void)setColor:(MNLColor *)color forKey:(NSString *)key {
_revision++;
[[self _mutableDictionary] setObject:color forKey:key];
}
- (void)removeColorForKey:(NSString *)key {
_revision++;
[[self _mutableDictionary] removeObjectForKey:key];
}
- (void)removeAllColors {
_revision++;
[[self _mutableDictionary] removeAllObjects];
}
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *restrict)state objects:(id __unsafe_unretained *)buffer count:(NSUInteger)len {
const CFDictionaryRef restrict dictionary = [self _mutableDictionary];
CFIndex count = CFDictionaryGetCount(dictionary);
if (state->state >= count) {
memset(state, 0, sizeof(NSFastEnumerationState));
memset(buffer, 0, sizeof(id) * len);
return 0;
}
CFStringRef *keys = malloc(count * sizeof(CFStringRef));
CFDictionaryGetKeysAndValues(dictionary, keys, NULL);
CFIndex s = state->state;
for (CFIndex i = 0; s < count && i < len; i++, s++)
buffer[i] = keys[s];
free(keys);
state->state = s;
state->itemsPtr = buffer;
state->mutationsPtr = &_revision;
return count;
}
@end