Convert the entire project to ARC.
[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 "AppDelegate.h"
20 #import "PreferenceNames.h"
21
22 @interface BreakpointManager (Private)
23 - (void)updateDisplaysForFile:(NSString*)file;
24 @end
25
26 @implementation BreakpointManager {
27 NSMutableArray* _breakpoints;
28 NSMutableArray* _savedBreakpoints;
29
30 DebuggerBackEnd* __weak _connection;
31 }
32
33 - (id)init
34 {
35 if (self = [super init])
36 {
37 _breakpoints = [[NSMutableArray alloc] init];
38 _savedBreakpoints = [[NSMutableArray alloc] init];
39
40 NSArray* savedBreakpoints = [[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints];
41 if (savedBreakpoints) {
42 [_savedBreakpoints addObjectsFromArray:savedBreakpoints];
43 for (NSDictionary* d in savedBreakpoints) {
44 [_breakpoints addObject:[[Breakpoint alloc] initWithDictionary:d]];
45 }
46 }
47 }
48 return self;
49 }
50
51 /**
52 * Registers a breakpoint at a given line
53 */
54 - (void)addBreakpoint:(Breakpoint*)bp;
55 {
56 if (![_breakpoints containsObject:bp])
57 {
58 [self willChangeValueForKey:@"breakpoints"];
59 [_breakpoints addObject:bp];
60 [self didChangeValueForKey:@"breakpoints"];
61
62 [_connection addBreakpoint:bp];
63
64 [_savedBreakpoints addObject:[bp dictionary]];
65 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
66
67 [self updateDisplaysForFile:[bp file]];
68 }
69 }
70
71 - (Breakpoint*)removeBreakpoint:(Breakpoint*)bp
72 {
73 if ([_breakpoints containsObject:bp]) {
74 [self willChangeValueForKey:@"breakpoints"];
75 [_breakpoints removeObject:bp];
76 [self didChangeValueForKey:@"breakpoints"];
77
78 [_connection removeBreakpoint:bp];
79
80 [_savedBreakpoints removeObject:[bp dictionary]];
81 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
82
83 if (bp.file)
84 [self updateDisplaysForFile:bp.file];
85
86 return bp;
87 }
88 return nil;
89 }
90
91 /**
92 * Returns all the breakpoints for a given file
93 */
94 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
95 {
96 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
97 for (Breakpoint* b in _breakpoints) {
98 if ([b.file isEqualToString:file]) {
99 [matches addObject:@(b.line)];
100 }
101 }
102
103 return matches;
104 }
105
106
107 - (BOOL)hasBreakpoint:(Breakpoint*)breakpoint
108 {
109 return [_breakpoints containsObject:breakpoint];
110 }
111
112 /**
113 * Checks to see if a given file has a breakpoint on a given line
114 */
115 - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file
116 {
117 return [self hasBreakpoint:[Breakpoint breakpointAtLine:line inFile:file]];
118 }
119
120 #pragma mark Private
121
122 /**
123 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
124 * and sets the markers for the BSLineNumberView
125 */
126 - (void)updateDisplaysForFile:(NSString*)file
127 {
128 AppDelegate* appDel = [NSApp delegate];
129 [[[appDel breakpoint] arrayController] rearrangeObjects];
130 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
131 [[[appDel debugger] sourceViewer] setMarkers:[self breakpointsForFile:file]];
132 }
133
134 @end