-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKarapuz.m
313 lines (229 loc) · 8.56 KB
/
Karapuz.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//==============================================================================
//
//
// Karapuz.m
// Karapuz
//
// Created by Vladimir Obrizan on 19.12.12.
// Copyright (c) 2012 Design and Test Lab. All rights reserved.
//
//
//==============================================================================
#import "Karapuz.h"
#import <objc/message.h>
//==============================================================================
@implementation Karapuz
//==============================================================================
static Karapuz *gInstance = NULL;
//==============================================================================
+(Karapuz *)instance
{
@synchronized(self)
{
if (gInstance == NULL)
{
gInstance = [self new];
}
}
return gInstance;
}
//==============================================================================
-(id)init
{
self = [super init];
if (self)
{
self.bindings = [NSMutableArray array];
}
return self;
}
//==============================================================================
+(void)dst:(id)dst pty:(NSString *)pty src:(id)src pty:(NSString *)pty2;
{
WeakStore *weakDst = [[WeakStore alloc] initWithObj:dst];
WeakStore *weakSrc = [[WeakStore alloc] initWithObj:src];
NSDictionary *d = @{@"dst": weakDst,
@"pty": pty,
@"src": weakSrc,
@"pty2": pty2,
@"dstAddr": [NSString stringWithFormat:@"%p",dst],
@"srcAddr": [NSString stringWithFormat:@"%p",src]};
[Karapuz.instance.bindings addObject:d];
[src addObserver:Karapuz.instance forKeyPath:pty2 options:0 context:nil];
}
//==============================================================================
+(void)dst:(id)dst block:(KarapuzBlock)block src:(id)src pty:(NSString *)pty2
{
WeakStore *weakDst = [[WeakStore alloc] initWithObj:dst];
WeakStore *weakSrc = [[WeakStore alloc] initWithObj:src];
WeakStore *weakBlc = [[WeakStore alloc] initWithBlo:block];
NSDictionary *d = @{@"dst": weakDst,
@"block": weakBlc,
@"src": weakSrc,
@"pty2": pty2,
@"dstAddr": [NSString stringWithFormat:@"%p",dst],
@"srcAddr": [NSString stringWithFormat:@"%p",src]};
[Karapuz.instance.bindings addObject:d];
[src addObserver:Karapuz.instance forKeyPath:pty2 options:0 context:nil];
}
//==============================================================================
+(void)dst:(id)dst selector:(NSString *)selector withParams:(NSDictionary *)params src:(id)src pty:(NSString *)pty2
{
WeakStore *weakDst = [[WeakStore alloc] initWithObj:dst];
WeakStore *weakSrc = [[WeakStore alloc] initWithObj:src];
if (!params)
params = [NSDictionary dictionaryWithObject:@"" forKey:@"testParam"];
NSDictionary *d = @{@"dst": weakDst,
@"selector": selector,
@"params" : params,
@"src": weakSrc,
@"pty2": pty2,
@"dstAddr": [NSString stringWithFormat:@"%p",dst],
@"srcAddr": [NSString stringWithFormat:@"%p",src]};
[Karapuz.instance.bindings addObject:d];
[src addObserver:Karapuz.instance forKeyPath:pty2 options:0 context:nil];
}
//==============================================================================
+(void)dst:(id)dst selector:(SEL)selector src:(id)src pty:(NSString *)pty2
{
WeakStore *weakDst = [[WeakStore alloc] initWithObj:dst];
WeakStore *weakSrc = [[WeakStore alloc] initWithObj:src];
NSDictionary *d = @{@"dst": weakDst,
@"sel": [NSValue valueWithPointer:selector],
@"src": weakSrc,
@"pty2": pty2,
@"dstAddr": [NSString stringWithFormat:@"%p",dst],
@"srcAddr": [NSString stringWithFormat:@"%p",src]};
[Karapuz.instance.bindings addObject:d];
[src addObserver:Karapuz.instance forKeyPath:pty2 options:0 context:nil];
}
//==============================================================================
#pragma mark - Observers
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSArray *tmpArray = [NSArray arrayWithArray:self.bindings];
for (NSDictionary *binding in tmpArray)
{
if ([binding[@"pty2"] isEqualToString:keyPath] && ([binding[@"srcAddr"] isEqualToString:[NSString stringWithFormat:@"%p",object]]))
{
WeakStore *weakDst = (WeakStore *)binding[@"dst"];
if ([Karapuz exists:weakDst])
{
id dst = weakDst.store;
NSString *pty = binding[@"pty"];
if (pty)
{
id value = objc_msgSend(object, NSSelectorFromString(keyPath));
NSString *selectorName = [NSString stringWithFormat:@"set%@:", [pty stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[pty substringToIndex:1] capitalizedString]]];
objc_msgSend(dst, NSSelectorFromString(selectorName), value);
}
WeakStore *weakBlc = (WeakStore *)binding[@"block"];
if (weakBlc)
if ([Karapuz exists:weakBlc])
{
KarapuzBlock block = weakBlc.storeBlock;
if (block)
block(object, keyPath);
}
NSString *selector = binding[@"selector"];
if (selector)
objc_msgSend(dst, NSSelectorFromString(selector), binding[@"params"]);
if (binding[@"sel"])
{
SEL sel = [binding[@"sel"] pointerValue];
objc_msgSend(dst, sel);
}
}
}
}
[self check];
}
//==============================================================================
+(void)remove:(id)dst
{
NSMutableArray *objectsToBeRemoved = [NSMutableArray array];
for (NSDictionary *d in Karapuz.instance.bindings)
{
if ([d[@"dstAddr"] isEqualToString:[NSString stringWithFormat:@"%p",dst]])
{
WeakStore *weakSrc = (WeakStore *)d[@"src"];
if ([Karapuz exists:weakSrc])
{
id obj = weakSrc.store;
[obj removeObserver:Karapuz.instance forKeyPath:d[@"pty2"]];
}
[objectsToBeRemoved addObject:d];
}
}
for (NSDictionary *d in objectsToBeRemoved)
[Karapuz.instance.bindings removeObject:d];
[Karapuz.instance check];
}
//==============================================================================
+(void)removeAll
{
NSArray *objectsToBeRemoved = [Karapuz.instance.bindings copy];
for (NSDictionary *d in objectsToBeRemoved)
{
WeakStore *weakDst = (WeakStore *)d[@"dst"];
if ([Karapuz exists:weakDst])
{
id dst = weakDst.store;
[Karapuz remove:dst];
}
}
}
//==============================================================================
-(void)check
{
NSMutableArray *objectsToBeRemoved = [NSMutableArray array];
for (NSDictionary *d in Karapuz.instance.bindings)
{
WeakStore *weakDst = (WeakStore *)d[@"dst"];
WeakStore *weakSrc = (WeakStore *)d[@"src"];
if (![Karapuz exists:weakSrc] || ![Karapuz exists:weakDst])
{
if ([Karapuz exists:weakSrc])
{
id obj = weakSrc.store;
[obj removeObserver:Karapuz.instance forKeyPath:d[@"pty2"]];
}
[objectsToBeRemoved addObject:d];
}
}
for (NSDictionary *d in objectsToBeRemoved)
[Karapuz.instance.bindings removeObject:d];
}
//==============================================================================
+(BOOL)exists:(WeakStore *)weakStore
{
BOOL isExist = [weakStore store] ? YES : NO;
if (isExist)
return isExist;
else
isExist = [weakStore storeBlock] ? YES : NO;
return isExist;
}
@end
//==============================================================================
@implementation WeakStore
-(id)initWithObj:(id)obj
{
self = [super init];
if (self)
{
self.store = obj;
}
return self;
}
-(id)initWithBlo:(KarapuzBlock)blo
{
self = [super init];
if (self)
{
self.storeBlock = blo;
}
return self;
}
//==============================================================================
@end