PageRenderTime 46ms CodeModel.GetById 14ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/JMRTree.m

http://rtm2cocoa.googlecode.com/
Objective C | 140 lines | 100 code | 33 blank | 7 comment | 8 complexity | cf2839d5a7abd0aa2bd740863ec59d69 MD5 | raw file
  1//
  2//  JMRTree.m
  3
  4/*
  5
  6This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7
  8*/
  9
 10#import "JMRTree.h"
 11
 12@implementation JMRTree
 13
 14-(id)init {
 15    return [self initWithObject:nil];
 16}
 17
 18-(id)initWithTreeRef:(CFTreeRef)treeRef {
 19    [super init];
 20    if (treeRef) {
 21        CFRetain(treeRef);
 22	_tree = treeRef;
 23	return self;
 24    }
 25
 26    return nil;
 27}
 28
 29-(id)initWithObject:(NSObject *)object {
 30    CFTreeContext ctx;
 31
 32    ctx.version = 0;
 33    ctx.info = object;
 34    ctx.retain = NULL;
 35    ctx.release = NULL;
 36    ctx.copyDescription = NULL;
 37    
 38    return [self initWithTreeRef:CFTreeCreate(kCFAllocatorDefault, &ctx)];
 39}
 40
 41+(JMRTree *)treeWithTreeRef:(CFTreeRef)treeRef {
 42    return [[[JMRTree alloc] initWithTreeRef:treeRef] autorelease];
 43}
 44
 45+(JMRTree *)treeWithObject:(NSObject *)object {
 46    return [[[JMRTree alloc] initWithObject:object] autorelease];
 47}
 48
 49+(JMRTree *)createTree {
 50    return [JMRTree alloc];
 51}
 52
 53-(JMRTree *)createTree {
 54    return [JMRTree alloc];
 55}
 56
 57-(JMRTree *)createTreeWithTreeRef:(CFTreeRef)treeRef {
 58    return treeRef ? [[[self createTree] initWithTreeRef:treeRef] autorelease] : nil;
 59}
 60
 61-(CFTreeRef)getTree {
 62    return _tree;
 63}
 64
 65-(int)childCount {
 66    return CFTreeGetChildCount(_tree);
 67}
 68
 69-(JMRTree *)childAtIndex:(int)index {
 70    return [self createTreeWithTreeRef:CFTreeGetChildAtIndex(_tree, index)];
 71}
 72
 73-(JMRTree *)firstChild {
 74    return [self childAtIndex:0];
 75}
 76
 77-(JMRTree *)nextSibling {
 78    return [self createTreeWithTreeRef:CFTreeGetNextSibling(_tree)];
 79}
 80
 81-(JMRTree *)parent {
 82    return [self createTreeWithTreeRef:CFTreeGetParent(_tree)];
 83}
 84
 85-(void)addChild:(JMRTree *)child {
 86    if (child)
 87        CFTreeAppendChild([self getTree], [child getTree]);
 88}
 89
 90-(void)addChild:(JMRTree *)child afterChild:(JMRTree *)sibling {
 91    if (child && sibling)
 92        [sibling insertSibling:child];
 93}
 94
 95-(void)prependChild:(JMRTree *)child {
 96    if (child)
 97        CFTreePrependChild([self getTree], [child getTree]);
 98}
 99
100-(void)insertSibling:(JMRTree *)sibling {
101    if (sibling)
102        CFTreeInsertSibling([self getTree], [sibling getTree]);
103}
104
105-(void)removeChild:(JMRTree *)child {
106    if (child && [child parent]==self)
107        [child detachFromParent];
108}
109
110-(void)detachFromParent {
111    CFTreeRemove([self getTree]);
112}
113
114-(void)removeAllChildren {
115    CFTreeRemoveAllChildren([self getTree]);
116}
117
118-(CFTreeContext)context {
119    CFTreeContext ctx;
120
121    CFTreeGetContext([self getTree], &ctx);
122    return ctx;
123}
124
125-(NSObject *)object {
126    return (NSObject *)[self context].info;
127}
128-(void)setObject:(NSObject *)value {
129    CFTreeContext ctx = [self context];
130
131    ctx.info = value;
132    CFTreeSetContext([self getTree], &ctx);
133}
134
135-(void)dealloc {
136    CFRelease(_tree);
137    [super dealloc];
138}
139
140@end