Update the API for loading properties to just pass around VariableNode pointers,...
[macgdbp.git] / Source / VariableNode.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2010, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "VariableNode.h"
18
19 #import "AppDelegate.h"
20 #include "NSXMLElementAdditions.h"
21
22 // Private Properties //////////////////////////////////////////////////////////
23
24 @interface VariableNode ()
25
26 @property (copy) NSString* name;
27 @property (copy) NSString* fullName;
28 @property (copy) NSString* className;
29 @property (copy) NSString* type;
30 @property (copy) NSString* value;
31 @property (retain) NSMutableArray* children;
32 @property (copy) NSString* address;
33
34 @end
35
36 ////////////////////////////////////////////////////////////////////////////////
37
38 @implementation VariableNode
39
40 @synthesize name = name_;
41 @synthesize fullName = fullName_;
42 @synthesize className = className_;
43 @synthesize type = type_;
44 @synthesize value = value_;
45 @synthesize children = children_;
46 @synthesize childCount = childCount_;
47 @synthesize address = address_;
48
49 - (id)initWithXMLNode:(NSXMLElement*)node
50 {
51 if (self = [super init]) {
52 self.name = [[node attributeForName:@"name"] stringValue];
53 self.fullName = [[node attributeForName:@"fullname"] stringValue];
54 self.className = [[node attributeForName:@"classname"] stringValue];
55 self.type = [[node attributeForName:@"type"] stringValue];
56 self.value = [node base64DecodedValue];
57 self.children = [NSMutableArray array];
58 if ([node children]) {
59 [self setChildrenFromXMLChildren:[node children]];
60 }
61 childCount_ = [[[node attributeForName:@"numchildren"] stringValue] integerValue];
62 self.address = [[node attributeForName:@"address"] stringValue];
63 }
64 return self;
65 }
66
67 - (void)dealloc
68 {
69 self.name = nil;
70 self.fullName = nil;
71 self.className = nil;
72 self.type = nil;
73 self.value = nil;
74 self.children = nil;
75 self.address = nil;
76 [super dealloc];
77 }
78
79 - (void)setChildrenFromXMLChildren:(NSArray*)children
80 {
81 for (NSXMLNode* child in children) {
82 // Other child nodes may be the string value.
83 if ([child isKindOfClass:[NSXMLElement class]]) {
84 VariableNode* node = [[VariableNode alloc] initWithXMLNode:(NSXMLElement*)child];
85 [children_ addObject:[node autorelease]];
86 }
87 }
88 }
89
90 - (NSArray*)dynamicChildren
91 {
92 NSArray* children = self.children;
93 if (![self isLeaf] && [children count] < 1) {
94 // If this node has children but they haven't been loaded from the backend,
95 // request them asynchronously.
96 [[AppDelegate instance].debugger fetchChildProperties:self];
97 }
98 return children;
99 }
100
101 - (BOOL)isLeaf
102 {
103 return (self.childCount == 0);
104 }
105
106 - (NSString*)displayType
107 {
108 if (self.className != nil) {
109 return [NSString stringWithFormat:@"%@ (%@)", self.className, self.type];
110 }
111 return self.type;
112 }
113
114 @end