Skip to content

Commit 858721e

Browse files
Hendrik HübnerHendrik Hübner
authored andcommitted
Refactor and fix gcc errors
1 parent 5263f33 commit 858721e

5 files changed

Lines changed: 79 additions & 12 deletions

File tree

Source/NSKVOSupport.m

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ - (void) dealloc
207207
[super dealloc];
208208
}
209209

210+
// While exploring the observer graph, for dependencies, we use this to
211+
// detect cycles to prevent infinite recursion.
210212
- (void) beginDependencyExpansionScope
211213
{
212214
GS_MUTEX_LOCK(_lock);
@@ -219,6 +221,8 @@ - (void) beginDependencyExpansionScope
219221
GS_MUTEX_UNLOCK(_lock);
220222
}
221223

224+
/* Nodes (observable values) in the observer graph are uniquely identified
225+
* using a combination of the object pointer and the name of the value */
222226
- (id) dependencyKeyForObject: (id)object key: (NSString *)key
223227
{
224228
return [NSArray arrayWithObjects:
@@ -227,7 +231,7 @@ - (id) dependencyKeyForObject: (id)object key: (NSString *)key
227231
nil];
228232
}
229233

230-
- (void) pushAncestorForNode: (_NSKVOKeyObserver *)keyObserver
234+
- (void) pushObserverToCurrentAncestorStack: (_NSKVOKeyObserver *)keyObserver
231235
{
232236
id ancestorKey = [self dependencyKeyForObject: keyObserver.object
233237
key: keyObserver.key];
@@ -236,7 +240,7 @@ - (void) pushAncestorForNode: (_NSKVOKeyObserver *)keyObserver
236240
GS_MUTEX_UNLOCK(_lock);
237241
}
238242

239-
- (void) popAncestorForNode: (_NSKVOKeyObserver *)keyObserver
243+
- (void) popObserverFromCurrentAncestorStack: (_NSKVOKeyObserver *)keyObserver
240244
{
241245
id ancestorKey = [self dependencyKeyForObject: keyObserver.object
242246
key: keyObserver.key];
@@ -246,7 +250,7 @@ - (void) popAncestorForNode: (_NSKVOKeyObserver *)keyObserver
246250
}
247251

248252
/// Mark keypath as visited in the current dependency-expansion scope.
249-
- (BOOL) markDependentKeypathVisited: (NSString *)keypath
253+
- (BOOL) checkDependencyForCycle: (NSString *)keypath
250254
forNode: (_NSKVOKeyObserver *)keyObserver
251255
{
252256
NSString *dependentKey;
@@ -261,7 +265,7 @@ - (BOOL) markDependentKeypathVisited: (NSString *)keypath
261265
{
262266
BOOL isCycle = [_dependencyAncestorKeys containsObject: visitToken];
263267
GS_MUTEX_UNLOCK(_lock);
264-
// If it's on the active ancestor stack, treat as true cycle and dedup.
268+
// If it's on the current ancestor stack, treat as cycle and dedup.
265269
// If it's already visited but not on stack, allow expansion to continue.
266270
return !isCycle;
267271
}
@@ -407,16 +411,17 @@ - (bool) isEmpty
407411
}
408412

409413
[observationInfo beginDependencyExpansionScope];
410-
[observationInfo pushAncestorForNode: keyObserver];
414+
[observationInfo pushObserverToCurrentAncestorStack: keyObserver];
411415
/* Don't allow our own key to be recreated. */
412-
[observationInfo markDependentKeypathVisited:keyObserver.key
416+
[observationInfo checkDependencyForCycle:keyObserver.key
413417
forNode:keyObserver];
414418

419+
/* The observers, which affect us */
415420
dependentObservers =
416421
[NSMutableArray arrayWithCapacity:[valueInfluencingKeys count]];
417422
for (NSString *dependentKeypath in valueInfluencingKeys)
418423
{
419-
if ([observationInfo markDependentKeypathVisited:dependentKeypath
424+
if ([observationInfo checkDependencyForCycle:dependentKeypath
420425
forNode:keyObserver])
421426
{
422427
_NSKVOKeyObserver *dependentObserver
@@ -428,13 +433,10 @@ - (bool) isEmpty
428433
[dependentObservers addObject:dependentObserver];
429434
}
430435
}
431-
else
432-
{
433-
}
434436
}
435437
keyObserver.dependentObservers = dependentObservers;
436438

437-
[observationInfo popAncestorForNode: keyObserver];
439+
[observationInfo popObserverFromCurrentAncestorStack: keyObserver];
438440
[observationInfo endDependencyExpansionScope];
439441
}
440442
}

Tests/base/NSKVOSupport/ComplexNilAndCycleTest.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
@interface CNode : NSObject
77
{
88
NSInteger _f;
9+
NSObject *_d;
10+
BNode *_b;
911
}
1012
@property (nonatomic, retain) NSObject *d;
1113
@property (nonatomic, retain) BNode *b;
1214
@property (nonatomic, assign) NSInteger f;
1315
@end
1416
@implementation CNode
17+
@synthesize d = _d;
18+
@synthesize b = _b;
1519
- (NSInteger) f
1620
{
1721
return _f;
@@ -34,24 +38,40 @@ - (void) setF: (NSInteger)f
3438
@end
3539

3640
@interface BNode : NSObject
41+
{
42+
CNode *_c;
43+
NSInteger _e;
44+
}
3745
@property (nonatomic, retain) CNode *c;
3846
@property (nonatomic, assign) NSInteger e;
3947
@end
4048
@implementation BNode
49+
@synthesize c = _c;
50+
@synthesize e = _e;
4151
@end
4252

4353
@interface ANode : NSObject
54+
{
55+
BNode *_b;
56+
CNode *_c;
57+
}
4458
@property (nonatomic, retain) BNode *b;
4559
@property (nonatomic, retain) CNode *c;
4660
@end
4761
@implementation ANode
62+
@synthesize b = _b;
63+
@synthesize c = _c;
4864
@end
4965

5066
@interface RootNode : NSObject
67+
{
68+
ANode *_a;
69+
}
5170
@property (nonatomic, retain) ANode *a;
5271
@property (nonatomic, readonly) BOOL x;
5372
@end
5473
@implementation RootNode
74+
@synthesize a = _a;
5575
+ (NSSet *) keyPathsForValuesAffectingX
5676
{
5777
return [NSSet setWithObjects: @"a.b.c.d", @"a.c.b.e", nil];

Tests/base/NSKVOSupport/branchVisitedScope.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22
#import "ObjectTesting.h"
33

44
@interface BVSLeaf : NSObject
5+
{
6+
NSInteger _value;
7+
BOOL _flag;
8+
}
59
@property (nonatomic, assign) NSInteger value;
610
@property (nonatomic, assign) BOOL flag;
711
@end
812

913
@implementation BVSLeaf
14+
@synthesize value = _value;
15+
@synthesize flag = _flag;
1016
@end
1117

1218
@interface BVSNode : NSObject
19+
{
20+
BVSLeaf *_leaf;
21+
}
1322
@property (nonatomic, retain) BVSLeaf *leaf;
1423
@property (nonatomic, readonly) NSInteger score;
1524
@end
1625

1726
@implementation BVSNode
27+
@synthesize leaf = _leaf;
1828
+ (NSSet *) keyPathsForValuesAffectingScore
1929
{
2030
return [NSSet setWithObject: @"leaf.value"];
@@ -26,18 +36,26 @@ - (NSInteger) score
2636
@end
2737

2838
@interface BVSHolder : NSObject
39+
{
40+
BVSNode *_node;
41+
}
2942
@property (nonatomic, retain) BVSNode *node;
3043
@end
3144
@implementation BVSHolder
45+
@synthesize node = _node;
3246
@end
3347

3448
@interface BVSRoot : NSObject
49+
{
50+
BVSHolder *_holder;
51+
}
3552
@property (nonatomic, retain) BVSHolder *holder;
3653
@property (nonatomic, readonly) BVSNode *selected;
3754
@property (nonatomic, readonly) BOOL derived;
3855
@end
3956

4057
@implementation BVSRoot
58+
@synthesize holder = _holder;
4159
+ (NSSet *) keyPathsForValuesAffectingSelected
4260
{
4361
return [NSSet setWithObject: @"holder.node"];
@@ -97,7 +115,8 @@ int main(void)
97115
context: NULL];
98116

99117
// Repeatedly rematerialize the shared dependency path.
100-
for (NSUInteger i = 0; i < 25; i++)
118+
NSUInteger i;
119+
for (i = 0; i < 25; i++)
101120
{
102121
root.holder.node = nil;
103122
root.holder.node = node;

Tests/base/NSKVOSupport/cyclicalDependencies.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ - (void) observeValueForKeyPath: (NSString *)keyPath
2323
@end
2424

2525
@interface CDODirectCycleRoot : NSObject
26+
{
27+
NSInteger _leaf;
28+
}
2629
@property (nonatomic, assign) NSInteger leaf;
2730
@property (nonatomic, readonly) NSInteger a;
2831
@property (nonatomic, readonly) NSInteger b;
2932
@property (nonatomic, readonly) NSInteger derived;
3033
@end
3134

3235
@implementation CDODirectCycleRoot
36+
@synthesize leaf = _leaf;
3337
+ (NSSet *) keyPathsForValuesAffectingA
3438
{
3539
return [NSSet setWithObject: @"b"];
@@ -57,6 +61,9 @@ - (NSInteger) derived
5761
@end
5862

5963
@interface CDOThreeCycleRoot : NSObject
64+
{
65+
NSInteger _leaf;
66+
}
6067
@property (nonatomic, assign) NSInteger leaf;
6168
@property (nonatomic, readonly) NSInteger x;
6269
@property (nonatomic, readonly) NSInteger y;
@@ -65,6 +72,7 @@ @interface CDOThreeCycleRoot : NSObject
6572
@end
6673

6774
@implementation CDOThreeCycleRoot
75+
@synthesize leaf = _leaf;
6876
+ (NSSet *) keyPathsForValuesAffectingX
6977
{
7078
return [NSSet setWithObject: @"y"];

Tests/base/NSKVOSupport/visitedAliasCollision.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22
#import "ObjectTesting.h"
33

44
@interface VACLeaf : NSObject
5+
{
6+
NSInteger _value;
7+
BOOL _flag;
8+
}
59
@property (nonatomic, assign) NSInteger value;
610
@property (nonatomic, assign) BOOL flag;
711
@end
812
@implementation VACLeaf
13+
@synthesize value = _value;
14+
@synthesize flag = _flag;
915
@end
1016

1117
@interface VACNode : NSObject
18+
{
19+
VACLeaf *_leaf;
20+
}
1221
@property (nonatomic, retain) VACLeaf *leaf;
1322
@property (nonatomic, readonly) NSInteger score;
1423
@end
1524
@implementation VACNode
25+
@synthesize leaf = _leaf;
1626
+ (NSSet *) keyPathsForValuesAffectingScore
1727
{
1828
return [NSSet setWithObject: @"leaf.value"];
@@ -24,19 +34,27 @@ - (NSInteger) score
2434
@end
2535

2636
@interface VACHolder : NSObject
37+
{
38+
VACNode *_node;
39+
}
2740
@property (nonatomic, retain) VACNode *node;
2841
@end
2942
@implementation VACHolder
43+
@synthesize node = _node;
3044
@end
3145

3246
@interface VACRoot : NSObject
47+
{
48+
VACHolder *_holder;
49+
}
3350
@property (nonatomic, retain) VACHolder *holder;
3451
@property (nonatomic, readonly) VACNode *selectedA;
3552
@property (nonatomic, readonly) VACNode *selectedB;
3653
@property (nonatomic, readonly) BOOL derived;
3754
@end
3855

3956
@implementation VACRoot
57+
@synthesize holder = _holder;
4058
+ (NSSet *) keyPathsForValuesAffectingSelectedA
4159
{
4260
return [NSSet setWithObject: @"holder.node"];

0 commit comments

Comments
 (0)