Convert the remaining DebuggerController IBOutlets to properties.
[macgdbp.git] / Source / DebuggerModel.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2015, Blue Static <https://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 "DebuggerModel.h"
18
19 #import "BreakpointManager.h"
20 #import "StackFrame.h"
21
22 @interface DebuggerModel ()
23 @property(assign, nonatomic) BOOL connected;
24 @end
25
26 @implementation DebuggerModel {
27 NSMutableArray* _stack;
28 }
29
30 - (instancetype)init {
31 if (self = [super init]) {
32 _breakpointManager = [[BreakpointManager alloc] init];
33 _stack = [NSMutableArray new];
34
35 [self onDisconnect];
36 }
37 return self;
38 }
39
40 - (void)dealloc {
41 [_breakpointManager release];
42 [_status release];
43 [_lastError release];
44 [_stack release];
45 [super dealloc];
46 }
47
48 - (NSUInteger)stackDepth {
49 return self.stack.count;
50 }
51
52 - (void)onNewConnection {
53 self.status = nil;
54 self.connected = YES;
55 [_stack removeAllObjects];
56 }
57
58 - (void)onDisconnect {
59 self.connected = NO;
60 self.status = @"Disconnected";
61 }
62
63 - (void)updateStack:(NSArray<StackFrame*>*)newStack {
64 // Iterate, in reverse order from the bottom to the top, both stacks to find
65 // the point of divergence.
66 NSEnumerator* itNewStack = [newStack reverseObjectEnumerator];
67 NSEnumerator* itOldStack = [self.stack reverseObjectEnumerator];
68
69 StackFrame* frameNew;
70 StackFrame* frameOld = [itOldStack nextObject];
71 NSUInteger oldStackOffset = self.stack.count;
72 while (frameNew = [itNewStack nextObject]) {
73 if ([frameNew isEqual:frameOld]) {
74 --oldStackOffset;
75 frameOld = [itOldStack nextObject];
76 } else {
77 break;
78 }
79 }
80
81 [self willChangeValueForKey:@"stack"];
82
83 // Remove any frames from the top of the stack that are not shared with the
84 // new stack.
85 [_stack removeObjectsInRange:NSMakeRange(0, oldStackOffset)];
86
87 // Continue inserting objects to update the stack with the new frames.
88 while (frameNew) {
89 [_stack insertObject:frameNew atIndex:0];
90 frameNew = [itNewStack nextObject];
91 }
92
93 // Renumber the stack.
94 for (NSUInteger i = 0; i < self.stack.count; ++i)
95 self.stack[i].index = i;
96
97 [self didChangeValueForKey:@"stack"];
98 }
99
100 @end