From 6e6bd378cba7d3bb70c302c1136c7a32d75cc9fc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 4 Dec 2016 01:08:34 -0500 Subject: [PATCH] Make BreakpointManager a property of DebuggerModel, rather than a singleton. --- Source/BreakpointController.h | 3 +- Source/BreakpointController.m | 5 +- Source/BreakpointManager.h | 7 --- Source/BreakpointManager.m | 96 +++++++++++++++-------------------- Source/DebuggerBackEnd.m | 3 +- Source/DebuggerController.m | 9 ++-- Source/DebuggerModel.h | 4 ++ Source/DebuggerModel.m | 3 ++ 8 files changed, 60 insertions(+), 70 deletions(-) diff --git a/Source/BreakpointController.h b/Source/BreakpointController.h index c19f83e..f4ccc8b 100644 --- a/Source/BreakpointController.h +++ b/Source/BreakpointController.h @@ -23,7 +23,8 @@ @property(nonatomic, assign) IBOutlet NSArrayController* arrayController; -- (instancetype)initWithSourceView:(BSSourceView*)sourceView; +- (instancetype)initWithBreakpointManager:(BreakpointManager*)breakpointManager + sourceView:(BSSourceView*)sourceView; - (IBAction)addBreakpoint:(id)sender; - (IBAction)removeBreakpoint:(id)sender; diff --git a/Source/BreakpointController.m b/Source/BreakpointController.m index 318422d..941ab88 100644 --- a/Source/BreakpointController.m +++ b/Source/BreakpointController.m @@ -29,10 +29,11 @@ /** * Constructor */ -- (id)initWithSourceView:(BSSourceView*)sourceView +- (instancetype)initWithBreakpointManager:(BreakpointManager*)breakpointManager + sourceView:(BSSourceView*)sourceView { if ((self = [super initWithNibName:@"Breakpoints" bundle:nil])) { - _manager = [BreakpointManager sharedManager]; + _manager = breakpointManager; _sourceView = sourceView; } return self; diff --git a/Source/BreakpointManager.h b/Source/BreakpointManager.h index 438d40b..7d41c28 100644 --- a/Source/BreakpointManager.h +++ b/Source/BreakpointManager.h @@ -19,17 +19,10 @@ #import "DebuggerBackEnd.h" @interface BreakpointManager : NSObject -{ - NSMutableArray* breakpoints; - NSMutableArray* savedBreakpoints; - - DebuggerBackEnd* connection; -} @property(readwrite, assign) DebuggerBackEnd* connection; @property(readonly) NSMutableArray* breakpoints; -+ (BreakpointManager*)sharedManager; - (void)addBreakpoint:(Breakpoint*)bp; - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file; - (NSSet*)breakpointsForFile:(NSString*)file; diff --git a/Source/BreakpointManager.m b/Source/BreakpointManager.m index 64ff5f6..453b08e 100644 --- a/Source/BreakpointManager.m +++ b/Source/BreakpointManager.m @@ -1,16 +1,16 @@ /* * MacGDBp * Copyright (c) 2007 - 2011, Blue Static - * - * This program is free software; you can redistribute it and/or modify it under the terms of the GNU - * General Public License as published by the Free Software Foundation; either version 2 of the + * + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without - * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program; if not, + * + * You should have received a copy of the GNU General Public License along with this program; if not, * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ @@ -23,49 +23,35 @@ - (void)updateDisplaysForFile:(NSString*)file; @end -@implementation BreakpointManager +@implementation BreakpointManager { + NSMutableArray* _breakpoints; + NSMutableArray* _savedBreakpoints; -@synthesize breakpoints, connection; + DebuggerBackEnd* _connection; +} -/** - * Initializer - */ - (id)init { if (self = [super init]) { - if (!breakpoints) - { - breakpoints = [[NSMutableArray alloc] init]; - } - - savedBreakpoints = [[[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints] mutableCopy]; - if (savedBreakpoints) - { - for (NSDictionary* d in savedBreakpoints) - { - [breakpoints addObject:[[[Breakpoint alloc] initWithDictionary:d] autorelease]]; + _breakpoints = [[NSMutableArray alloc] init]; + _savedBreakpoints = [[NSMutableArray alloc] init]; + + NSArray* savedBreakpoints = [[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints]; + if (savedBreakpoints) { + [_savedBreakpoints addObjectsFromArray:savedBreakpoints]; + for (NSDictionary* d in savedBreakpoints) { + [_breakpoints addObject:[[[Breakpoint alloc] initWithDictionary:d] autorelease]]; } } - else - { - savedBreakpoints = [NSMutableArray new]; - } } return self; } -/** - * Returns the shared manager (singleton) - */ -+ (BreakpointManager*)sharedManager -{ - static BreakpointManager* manager; - if (!manager) - { - manager = [[BreakpointManager alloc] init]; - } - return manager; +- (void)dealloc { + [_breakpoints release]; + [_savedBreakpoints release]; + [super dealloc]; } /** @@ -73,17 +59,17 @@ */ - (void)addBreakpoint:(Breakpoint*)bp; { - if (![breakpoints containsObject:bp]) + if (![_breakpoints containsObject:bp]) { [self willChangeValueForKey:@"breakpoints"]; - [breakpoints addObject:bp]; + [_breakpoints addObject:bp]; [self didChangeValueForKey:@"breakpoints"]; - [connection addBreakpoint:bp]; - - [savedBreakpoints addObject:[bp dictionary]]; - [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints]; - + [_connection addBreakpoint:bp]; + + [_savedBreakpoints addObject:[bp dictionary]]; + [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints]; + [self updateDisplaysForFile:[bp file]]; } } @@ -93,7 +79,7 @@ */ - (Breakpoint*)removeBreakpointAt:(NSUInteger)line inFile:(NSString*)file { - for (Breakpoint* b in breakpoints) + for (Breakpoint* b in _breakpoints) { if ([b line] == line && [[b file] isEqualToString:file]) { @@ -102,14 +88,14 @@ [[b retain] autorelease]; [self willChangeValueForKey:@"breakpoints"]; - [breakpoints removeObject:b]; + [_breakpoints removeObject:b]; [self didChangeValueForKey:@"breakpoints"]; - [connection removeBreakpoint:b]; - - [savedBreakpoints removeObject:[b dictionary]]; - [[NSUserDefaults standardUserDefaults] setObject:savedBreakpoints forKey:kPrefBreakpoints]; - + [_connection removeBreakpoint:b]; + + [_savedBreakpoints removeObject:[b dictionary]]; + [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints]; + [self updateDisplaysForFile:file]; return b; } @@ -123,7 +109,7 @@ - (NSSet*)breakpointsForFile:(NSString*)file { NSMutableSet* matches = [NSMutableSet set]; - for (Breakpoint* b in breakpoints) { + for (Breakpoint* b in _breakpoints) { if ([b.file isEqualToString:file]) { [matches addObject:@(b.line)]; } @@ -137,7 +123,7 @@ */ - (BOOL)hasBreakpointAt:(NSUInteger)line inFile:(NSString*)file { - return [breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]]; + return [_breakpoints containsObject:[[[Breakpoint alloc] initWithLine:line inFile:file] autorelease]]; } #pragma mark Private diff --git a/Source/DebuggerBackEnd.m b/Source/DebuggerBackEnd.m index 2d9f56e..45a1a0c 100644 --- a/Source/DebuggerBackEnd.m +++ b/Source/DebuggerBackEnd.m @@ -31,7 +31,6 @@ - (instancetype)initWithPort:(NSUInteger)aPort autoAttach:(BOOL)doAttach { if (self = [super init]) { - [[BreakpointManager sharedManager] setConnection:self]; _port = aPort; _client = [[ProtocolClient alloc] initWithDelegate:self]; @@ -279,7 +278,7 @@ } // Register any breakpoints that exist offline. - for (Breakpoint* bp in [[BreakpointManager sharedManager] breakpoints]) + for (Breakpoint* bp in self.model.breakpointManager.breakpoints) [self addBreakpoint:bp]; // TODO: update the status. diff --git a/Source/DebuggerController.m b/Source/DebuggerController.m index b6a3e43..997b767 100644 --- a/Source/DebuggerController.m +++ b/Source/DebuggerController.m @@ -58,6 +58,8 @@ connection = [[DebuggerBackEnd alloc] initWithPort:[defaults integerForKey:kPrefPort] autoAttach:[defaults boolForKey:kPrefDebuggerAttached]]; connection.model = _model; + _model.breakpointManager.connection = connection; + expandedVariables = [[NSMutableSet alloc] init]; [[self window] makeKeyAndOrderFront:nil]; [[self window] setDelegate:self]; @@ -101,7 +103,8 @@ self.connection.autoAttach = [attachedCheckbox_ state] == NSOnState; // Load view controllers into the tab views. - _breakpointsController = [[BreakpointController alloc] initWithSourceView:sourceViewer]; + _breakpointsController = [[BreakpointController alloc] initWithBreakpointManager:_model.breakpointManager + sourceView:sourceViewer]; [[self.tabView tabViewItemAtIndex:1] setView:_breakpointsController.view]; _evalController = [[EvalController alloc] initWithBackEnd:connection]; @@ -351,7 +354,7 @@ { [sourceViewer setString:frame.source asFile:filename]; - NSSet* breakpoints = [[BreakpointManager sharedManager] breakpointsForFile:filename]; + NSSet* breakpoints = [_model.breakpointManager breakpointsForFile:filename]; [sourceViewer setMarkers:breakpoints]; } @@ -406,7 +409,7 @@ */ - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file { - BreakpointManager* mngr = [BreakpointManager sharedManager]; + BreakpointManager* mngr = _model.breakpointManager; if ([mngr hasBreakpointAt:line inFile:file]) { diff --git a/Source/DebuggerModel.h b/Source/DebuggerModel.h index f18a0de..eeded0d 100644 --- a/Source/DebuggerModel.h +++ b/Source/DebuggerModel.h @@ -16,6 +16,7 @@ #import +@class BreakpointManager; @class StackFrame; // This class represents the state of an active debugging session. It is @@ -23,6 +24,9 @@ // All of the properties are KVO-compliant. @interface DebuggerModel : NSObject +// Maintains state about breakpoints. +@property(readonly, nonatomic) BreakpointManager* breakpointManager; + // Whether or not the debugger is currently connected. @property(readonly, nonatomic) BOOL connected; diff --git a/Source/DebuggerModel.m b/Source/DebuggerModel.m index 49738f6..3d2e292 100644 --- a/Source/DebuggerModel.m +++ b/Source/DebuggerModel.m @@ -16,6 +16,7 @@ #import "DebuggerModel.h" +#import "BreakpointManager.h" #import "StackFrame.h" @interface DebuggerModel () @@ -28,12 +29,14 @@ - (instancetype)init { if (self = [super init]) { + _breakpointManager = [[BreakpointManager alloc] init]; _stack = [NSMutableArray new]; } return self; } - (void)dealloc { + [_breakpointManager release]; [_status release]; [_stack release]; [super dealloc]; -- 2.43.5