/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. This 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.
  5. */
  6. #import "JMRTree.h"
  7. @implementation JMRTree
  8. -(id)init {
  9. return [self initWithObject:nil];
  10. }
  11. -(id)initWithTreeRef:(CFTreeRef)treeRef {
  12. [super init];
  13. if (treeRef) {
  14. CFRetain(treeRef);
  15. _tree = treeRef;
  16. return self;
  17. }
  18. return nil;
  19. }
  20. -(id)initWithObject:(NSObject *)object {
  21. CFTreeContext ctx;
  22. ctx.version = 0;
  23. ctx.info = object;
  24. ctx.retain = NULL;
  25. ctx.release = NULL;
  26. ctx.copyDescription = NULL;
  27. return [self initWithTreeRef:CFTreeCreate(kCFAllocatorDefault, &ctx)];
  28. }
  29. +(JMRTree *)treeWithTreeRef:(CFTreeRef)treeRef {
  30. return [[[JMRTree alloc] initWithTreeRef:treeRef] autorelease];
  31. }
  32. +(JMRTree *)treeWithObject:(NSObject *)object {
  33. return [[[JMRTree alloc] initWithObject:object] autorelease];
  34. }
  35. +(JMRTree *)createTree {
  36. return [JMRTree alloc];
  37. }
  38. -(JMRTree *)createTree {
  39. return [JMRTree alloc];
  40. }
  41. -(JMRTree *)createTreeWithTreeRef:(CFTreeRef)treeRef {
  42. return treeRef ? [[[self createTree] initWithTreeRef:treeRef] autorelease] : nil;
  43. }
  44. -(CFTreeRef)getTree {
  45. return _tree;
  46. }
  47. -(int)childCount {
  48. return CFTreeGetChildCount(_tree);
  49. }
  50. -(JMRTree *)childAtIndex:(int)index {
  51. return [self createTreeWithTreeRef:CFTreeGetChildAtIndex(_tree, index)];
  52. }
  53. -(JMRTree *)firstChild {
  54. return [self childAtIndex:0];
  55. }
  56. -(JMRTree *)nextSibling {
  57. return [self createTreeWithTreeRef:CFTreeGetNextSibling(_tree)];
  58. }
  59. -(JMRTree *)parent {
  60. return [self createTreeWithTreeRef:CFTreeGetParent(_tree)];
  61. }
  62. -(void)addChild:(JMRTree *)child {
  63. if (child)
  64. CFTreeAppendChild([self getTree], [child getTree]);
  65. }
  66. -(void)addChild:(JMRTree *)child afterChild:(JMRTree *)sibling {
  67. if (child && sibling)
  68. [sibling insertSibling:child];
  69. }
  70. -(void)prependChild:(JMRTree *)child {
  71. if (child)
  72. CFTreePrependChild([self getTree], [child getTree]);
  73. }
  74. -(void)insertSibling:(JMRTree *)sibling {
  75. if (sibling)
  76. CFTreeInsertSibling([self getTree], [sibling getTree]);
  77. }
  78. -(void)removeChild:(JMRTree *)child {
  79. if (child && [child parent]==self)
  80. [child detachFromParent];
  81. }
  82. -(void)detachFromParent {
  83. CFTreeRemove([self getTree]);
  84. }
  85. -(void)removeAllChildren {
  86. CFTreeRemoveAllChildren([self getTree]);
  87. }
  88. -(CFTreeContext)context {
  89. CFTreeContext ctx;
  90. CFTreeGetContext([self getTree], &ctx);
  91. return ctx;
  92. }
  93. -(NSObject *)object {
  94. return (NSObject *)[self context].info;
  95. }
  96. -(void)setObject:(NSObject *)value {
  97. CFTreeContext ctx = [self context];
  98. ctx.info = value;
  99. CFTreeSetContext([self getTree], &ctx);
  100. }
  101. -(void)dealloc {
  102. CFRelease(_tree);
  103. [super dealloc];
  104. }
  105. @end