Bump project version to 212.1.
[macgdbp.git] / Source / BreakpointManager.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, 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 "BreakpointManager.h"
18
19 #import "PreferenceNames.h"
20
21 @implementation BreakpointManager {
22 NSMutableArray* _breakpoints;
23 NSMutableArray* _savedBreakpoints;
24
25 DebuggerBackEnd* __weak _connection;
26 }
27
28 - (id)init
29 {
30 if (self = [super init])
31 {
32 _breakpoints = [[NSMutableArray alloc] init];
33 _savedBreakpoints = [[NSMutableArray alloc] init];
34
35 NSArray* savedBreakpoints = [[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints];
36 if (savedBreakpoints) {
37 for (NSDictionary* d in savedBreakpoints) {
38 Breakpoint* bp = [[Breakpoint alloc] initWithDictionary:d];
39 [_breakpoints addObject:bp];
40 [_savedBreakpoints addObject:[bp dictionary]];
41 }
42 }
43 }
44 return self;
45 }
46
47 /**
48 * Registers a breakpoint at a given line
49 */
50 - (void)addBreakpoint:(Breakpoint*)bp;
51 {
52 if ([_breakpoints containsObject:bp])
53 return;
54
55 [self willChangeValueForKey:@"breakpoints"];
56 [_breakpoints addObject:bp];
57 [self didChangeValueForKey:@"breakpoints"];
58
59 [_connection addBreakpoint:bp];
60
61 [_savedBreakpoints addObject:[bp dictionary]];
62 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
63 }
64
65 - (Breakpoint*)removeBreakpoint:(Breakpoint*)bp
66 {
67 // Use the -isEqual: test to find the object in |_breakpoints| that also has
68 // the debugger id and secure bookmark data.
69 NSUInteger idx = [_breakpoints indexOfObject:bp];
70 if (idx == NSNotFound)
71 return nil;
72
73 bp = [_breakpoints objectAtIndex:idx];
74
75 [self willChangeValueForKey:@"breakpoints"];
76 [_breakpoints removeObject:bp];
77 [self didChangeValueForKey:@"breakpoints"];
78
79 [_connection removeBreakpoint:bp];
80
81 [_savedBreakpoints removeObject:[bp dictionary]];
82 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
83
84 return bp;
85 }
86
87 /**
88 * Returns all the breakpoints for a given file
89 */
90 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
91 {
92 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
93 for (Breakpoint* b in _breakpoints) {
94 if ([b.file isEqualToString:file]) {
95 [matches addObject:@(b.line)];
96 }
97 }
98
99 return matches;
100 }
101
102
103 - (BOOL)hasBreakpoint:(Breakpoint*)breakpoint
104 {
105 return [_breakpoints containsObject:breakpoint];
106 }
107
108 @end