Force a re-serialization of Breakpoints on decode.
[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 for (NSDictionary* d in savedBreakpoints) {
43 Breakpoint* bp = [[Breakpoint alloc] initWithDictionary:d];
44 [_breakpoints addObject:bp];
45 [_savedBreakpoints addObject:[bp dictionary]];
46 }
47 }
48 }
49 return self;
50 }
51
52 /**
53 * Registers a breakpoint at a given line
54 */
55 - (void)addBreakpoint:(Breakpoint*)bp;
56 {
57 if (![_breakpoints containsObject:bp])
58 {
59 [self willChangeValueForKey:@"breakpoints"];
60 [_breakpoints addObject:bp];
61 [self didChangeValueForKey:@"breakpoints"];
62
63 [_connection addBreakpoint:bp];
64
65 [_savedBreakpoints addObject:[bp dictionary]];
66 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
67
68 [self updateDisplaysForFile:[bp file]];
69 }
70 }
71
72 - (Breakpoint*)removeBreakpoint:(Breakpoint*)bp
73 {
74 // Use the -isEqual: test to find the object in |_breakpoints| that also has
75 // the debugger id and secure bookmark data.
76 NSUInteger idx = [_breakpoints indexOfObject:bp];
77 if (idx == NSNotFound)
78 return nil;
79
80 bp = [_breakpoints objectAtIndex:idx];
81
82 [self willChangeValueForKey:@"breakpoints"];
83 [_breakpoints removeObject:bp];
84 [self didChangeValueForKey:@"breakpoints"];
85
86 [_connection removeBreakpoint:bp];
87
88 [_savedBreakpoints removeObject:[bp dictionary]];
89 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
90
91 if (bp.file)
92 [self updateDisplaysForFile:bp.file];
93
94 return bp;
95 }
96
97 /**
98 * Returns all the breakpoints for a given file
99 */
100 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
101 {
102 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
103 for (Breakpoint* b in _breakpoints) {
104 if ([b.file isEqualToString:file]) {
105 [matches addObject:@(b.line)];
106 }
107 }
108
109 return matches;
110 }
111
112
113 - (BOOL)hasBreakpoint:(Breakpoint*)breakpoint
114 {
115 return [_breakpoints containsObject:breakpoint];
116 }
117
118 #pragma mark Private
119
120 /**
121 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
122 * and sets the markers for the BSLineNumberView
123 */
124 - (void)updateDisplaysForFile:(NSString*)file
125 {
126 AppDelegate* appDel = [NSApp delegate];
127 [[[appDel breakpoint] arrayController] rearrangeObjects];
128 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
129 [[[appDel debugger] sourceViewer] setMarkers:[self breakpointsForFile:file]];
130 }
131
132 @end