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