Introduce DebuggerModel, which will be updated by the BackEnd.
[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 #import "AppDelegate.h"
19
20 @interface BreakpointManager (Private)
21 - (void)updateDisplaysForFile:(NSString*)file;
22 @end
23
24 @implementation BreakpointManager
25
26 @synthesize breakpoints, connection;
27
28 /**
29 * Initializer
30 */
31 - (id)init
32 {
33 if (self = [super init])
34 {
35 if (!breakpoints)
36 {
37 breakpoints = [[NSMutableArray alloc] init];
38 }
39
40 savedBreakpoints = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Breakpoints"] mutableCopy];
41 if (savedBreakpoints)
42 {
43 for (NSDictionary* d in savedBreakpoints)
44 {
45 [breakpoints addObject:[[[Breakpoint alloc] initWithDictionary:d] autorelease]];
46 }
47 }
48 else
49 {
50 savedBreakpoints = [NSMutableArray new];
51 }
52 }
53 return self;
54 }
55
56 /**
57 * Returns the shared manager (singleton)
58 */
59 + (BreakpointManager*)sharedManager
60 {
61 static BreakpointManager* manager;
62 if (!manager)
63 {
64 manager = [[BreakpointManager alloc] init];
65 }
66 return manager;
67 }
68
69 /**
70 * Registers a breakpoint at a given line
71 */
72 - (void)addBreakpoint:(Breakpoint*)bp;
73 {
74 if (![breakpoints containsObject:bp])
75 {
76 [breakpoints addObject:bp];
77 [connection addBreakpoint:bp];
78
79 [savedBreakpoints addObject:[bp dictionary]];
80 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:@"Breakpoints"];
81
82 [self updateDisplaysForFile:[bp file]];
83 }
84 }
85
86 /**
87 * Removes a breakpoint at a given line/file combination, or nil if nothing was removed
88 */
89 - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file
90 {
91 for (Breakpoint* b in breakpoints)
92 {
93 if ([b line] == line && [[b file] isEqualToString:file])
94 {
95 [breakpoints removeObject:b];
96 [connection removeBreakpoint:b];
97
98 [savedBreakpoints removeObject:[b dictionary]];
99 [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:@"Breakpoints"];
100
101 [self updateDisplaysForFile:file];
102 return b;
103 }
104 }
105 return nil;
106 }
107
108 /**
109 * Returns all the breakpoints for a given file
110 */
111 - (NSArray*)breakpointsForFile:(NSString*)file
112 {
113 NSMutableArray* matches = [NSMutableArray array];
114 for (Breakpoint* b in breakpoints)
115 {
116 if ([[b file] isEqualToString:file])
117 {
118 [matches addObject:b];
119 }
120 }
121
122 return matches;
123 }
124
125 /**
126 * Checks to see if a given file has a breakpoint on a given line
127 */
128 - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file
129 {
130 return [breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]];
131 }
132
133 #pragma mark Private
134
135 /**
136 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
137 * and sets the markers for the BSLineNumberView
138 */
139 - (void)updateDisplaysForFile:(NSString*)file
140 {
141 AppDelegate* appDel = [NSApp delegate];
142 [[[appDel breakpoint] arrayController] rearrangeObjects];
143 [[[appDel breakpoint] sourceView] setNeedsDisplay:YES];
144 [[[appDel breakpoint] sourceView] setMarkers:[NSSet setWithArray:[self breakpointsForFile:file]]];
145 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
146 [[[appDel debugger] sourceViewer] setMarkers:[NSSet setWithArray:[self breakpointsForFile:file]]];
147 }
148
149 @end