From 522839143f7c85e4cfbbf54b94cef246d2f20914 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 27 Aug 2017 16:36:21 -0400 Subject: [PATCH] Revokes -[CSMachine init] and the slightly troubling create-on-demand semantics it places upon subclasses via .machine. Therefore each machine must announce its own implementation of -init. --- .../Machine/CSMachine+Subclassing.h | 2 -- .../Mac/Clock Signal/Machine/CSMachine.h | 9 ++++++ .../Mac/Clock Signal/Machine/CSMachine.mm | 31 ++++++++++--------- .../Machine/Wrappers/CSAmstradCPC.h | 2 ++ .../Machine/Wrappers/CSAmstradCPC.mm | 14 +++------ .../Machine/Wrappers/CSAtari2600.h | 2 ++ .../Machine/Wrappers/CSAtari2600.mm | 10 +++--- .../Machine/Wrappers/CSElectron.h | 2 ++ .../Machine/Wrappers/CSElectron.mm | 13 +++----- .../Clock Signal/Machine/Wrappers/CSOric.h | 2 ++ .../Clock Signal/Machine/Wrappers/CSOric.mm | 13 +++----- .../Clock Signal/Machine/Wrappers/CSVic20.h | 2 ++ .../Clock Signal/Machine/Wrappers/CSVic20.mm | 12 +++---- .../Clock Signal/Machine/Wrappers/CSZX8081.h | 2 ++ .../Clock Signal/Machine/Wrappers/CSZX8081.mm | 12 +++---- 15 files changed, 68 insertions(+), 60 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/Machine/CSMachine+Subclassing.h b/OSBindings/Mac/Clock Signal/Machine/CSMachine+Subclassing.h index 20776b832..fd027fc93 100644 --- a/OSBindings/Mac/Clock Signal/Machine/CSMachine+Subclassing.h +++ b/OSBindings/Mac/Clock Signal/Machine/CSMachine+Subclassing.h @@ -11,7 +11,5 @@ @interface CSMachine (Subclassing) -- (CRTMachine::Machine * const)machine; - (void)setupOutputWithAspectRatio:(float)aspectRatio; - @end diff --git a/OSBindings/Mac/Clock Signal/Machine/CSMachine.h b/OSBindings/Mac/Clock Signal/Machine/CSMachine.h index 8b601279c..8eaf2113e 100644 --- a/OSBindings/Mac/Clock Signal/Machine/CSMachine.h +++ b/OSBindings/Mac/Clock Signal/Machine/CSMachine.h @@ -18,6 +18,15 @@ @interface CSMachine : NSObject +- (instancetype)init NS_UNAVAILABLE; +/*! + Initialises an instance of CSMachine. + + @param machine The pointer to an instance of @c CRTMachine::Machine* . C++ type is omitted because + this header is visible to Swift, and the designated initialiser cannot be placed into a category. +*/ +- (instancetype)initWithMachine:(void *)machine NS_DESIGNATED_INITIALIZER; + - (void)runForNumberOfCycles:(int)numberOfCycles; - (float)idealSamplingRateFromRange:(NSRange)range; diff --git a/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm b/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm index 58401636b..1695cbcac 100644 --- a/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm +++ b/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm @@ -39,14 +39,17 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { @implementation CSMachine { SpeakerDelegate _speakerDelegate; MachineDelegate _machineDelegate; + CRTMachine::Machine *_machine; } -- (instancetype)init { +- (instancetype)initWithMachine:(void *)machine { self = [super init]; if(self) { + _machine = (CRTMachine::Machine *)machine; _machineDelegate.machine = self; - self.machine->set_delegate(&_machineDelegate); _speakerDelegate.machine = self; + + _machine->set_delegate(&_machineDelegate); } return self; } @@ -66,14 +69,14 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { - (void)dealloc { [_view performWithGLContext:^{ @synchronized(self) { - self.machine->close_output(); + _machine->close_output(); } }]; } - (float)idealSamplingRateFromRange:(NSRange)range { @synchronized(self) { - std::shared_ptr speaker = self.machine->get_speaker(); + std::shared_ptr speaker = _machine->get_speaker(); if(speaker) { return speaker->get_ideal_clock_rate_in_range((float)range.location, (float)(range.location + range.length)); @@ -90,7 +93,7 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { - (BOOL)setSpeakerDelegate:(Outputs::Speaker::Delegate *)delegate sampleRate:(float)sampleRate bufferSize:(NSUInteger)bufferSize { @synchronized(self) { - std::shared_ptr speaker = self.machine->get_speaker(); + std::shared_ptr speaker = _machine->get_speaker(); if(speaker) { speaker->set_output_rate(sampleRate, (int)bufferSize); @@ -103,7 +106,7 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { - (void)runForNumberOfCycles:(int)numberOfCycles { @synchronized(self) { - self.machine->run_for(Cycles(numberOfCycles)); + _machine->run_for(Cycles(numberOfCycles)); } } @@ -115,26 +118,26 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { } - (void)setupOutputWithAspectRatio:(float)aspectRatio { - self.machine->setup_output(aspectRatio); + _machine->setup_output(aspectRatio); // Since OS X v10.6, Macs have had a gamma of 2.2. - self.machine->get_crt()->set_output_gamma(2.2f); + _machine->get_crt()->set_output_gamma(2.2f); } - (void)drawViewForPixelSize:(CGSize)pixelSize onlyIfDirty:(BOOL)onlyIfDirty { - self.machine->get_crt()->draw_frame((unsigned int)pixelSize.width, (unsigned int)pixelSize.height, onlyIfDirty ? true : false); + _machine->get_crt()->draw_frame((unsigned int)pixelSize.width, (unsigned int)pixelSize.height, onlyIfDirty ? true : false); } - (double)clockRate { - return self.machine->get_clock_rate(); + return _machine->get_clock_rate(); } - (BOOL)clockIsUnlimited { - return self.machine->get_clock_is_unlimited() ? YES : NO; + return _machine->get_clock_is_unlimited() ? YES : NO; } - (void)paste:(NSString *)paste { - Utility::TypeRecipient *typeRecipient = dynamic_cast(self.machine); + Utility::TypeRecipient *typeRecipient = dynamic_cast(_machine); if(typeRecipient) typeRecipient->set_typer_for_string([paste UTF8String]); } @@ -142,7 +145,7 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { - (void)applyTarget:(const StaticAnalyser::Target &)target { @synchronized(self) { ConfigurationTarget::Machine *const configurationTarget = - dynamic_cast(self.machine); + dynamic_cast(_machine); if(configurationTarget) configurationTarget->configure_as_target(target); } } @@ -150,7 +153,7 @@ struct MachineDelegate: CRTMachine::Machine::Delegate { - (void)applyMedia:(const StaticAnalyser::Media &)media { @synchronized(self) { ConfigurationTarget::Machine *const configurationTarget = - dynamic_cast(self.machine); + dynamic_cast(_machine); if(configurationTarget) configurationTarget->insert_media(media); } } diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.h index 1286a6046..a10e1de80 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.h @@ -11,4 +11,6 @@ @interface CSAmstradCPC : CSMachine +- (instancetype)init; + @end diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.mm index ae54f4d47..d3aa3eb47 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAmstradCPC.mm @@ -18,17 +18,13 @@ std::unique_ptr _amstradCPC; } -- (CRTMachine::Machine * const)machine { - if(!_amstradCPC) { - _amstradCPC.reset(AmstradCPC::Machine::AmstradCPC()); - } - return _amstradCPC.get(); -} - - (instancetype)init { - self = [super init]; + AmstradCPC::Machine *machine = AmstradCPC::Machine::AmstradCPC(); + + self = [super initWithMachine:machine]; if(self) { - [self machine]; + _amstradCPC.reset(machine); + NSDictionary *roms = @{ @(AmstradCPC::ROMType::OS464) : @"os464", @(AmstradCPC::ROMType::OS664) : @"os664", diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h index 7026f02d7..8661ff646 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h @@ -12,6 +12,8 @@ @interface CSAtari2600 : CSMachine +- (instancetype)init; + - (void)setResetLineEnabled:(BOOL)enabled; @property (nonatomic, assign) BOOL colourButton; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm index 07ef20dc4..327233c08 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm @@ -15,11 +15,13 @@ std::unique_ptr _atari2600; } -- (CRTMachine::Machine * const)machine { - if(!_atari2600) { - _atari2600.reset(Atari2600::Machine::Atari2600()); +- (instancetype)init { + Atari2600::Machine *machine = Atari2600::Machine::Atari2600(); + self = [super initWithMachine:machine]; + if(self) { + _atari2600.reset(machine); } - return _atari2600.get(); + return self; } - (void)setDirection:(CSJoystickDirection)direction onPad:(NSUInteger)pad isPressed:(BOOL)isPressed { diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.h index 622ab749b..be83f2fdb 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.h @@ -12,6 +12,8 @@ @interface CSElectron : CSMachine +- (instancetype)init; + @property (nonatomic, assign) BOOL useFastLoadingHack; @property (nonatomic, assign) BOOL useTelevisionOutput; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.mm index df3a2eb07..496f87fef 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSElectron.mm @@ -19,16 +19,13 @@ std::unique_ptr _electron; } -- (CRTMachine::Machine * const)machine { - if(!_electron) { - _electron.reset(Electron::Machine::Electron()); - } - return _electron.get(); -} - - (instancetype)init { - self = [super init]; + Electron::Machine *machine = Electron::Machine::Electron(); + + self = [super initWithMachine:machine]; if(self) { + _electron.reset(machine); + [self setOSROM:[self rom:@"os"]]; [self setBASICROM:[self rom:@"basic"]]; [self setDFSROM:[self rom:@"DFS-1770-2.20"]]; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.h index c284944c1..f6a6d683e 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.h @@ -12,6 +12,8 @@ @interface CSOric : CSMachine +- (instancetype)init; + @property (nonatomic, assign) BOOL useFastLoadingHack; @property (nonatomic, assign) BOOL useCompositeOutput; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.mm index b231c2ba1..672be644d 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSOric.mm @@ -19,16 +19,13 @@ std::unique_ptr _oric; } -- (CRTMachine::Machine * const)machine { - if(!_oric) { - _oric.reset(Oric::Machine::Oric()); - } - return _oric.get(); -} - - (instancetype)init { - self = [super init]; + Oric::Machine *machine = Oric::Machine::Oric(); + + self = [super initWithMachine:machine]; if(self) { + _oric.reset(machine); + NSData *basic10 = [self rom:@"basic10"]; NSData *basic11 = [self rom:@"basic11"]; NSData *colour = [self rom:@"colour"]; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h index 5e97f7209..b2b2022fc 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h @@ -28,6 +28,8 @@ typedef NS_ENUM(NSInteger, CSVic20MemorySize) @interface CSVic20 : CSMachine +- (instancetype)init; + @property (nonatomic, assign) BOOL useFastLoadingHack; @property (nonatomic, assign) CSVic20Country country; @property (nonatomic, assign) CSVic20MemorySize memorySize; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm index a95f3afa2..4740c3e64 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm @@ -13,6 +13,7 @@ #include "G64.hpp" #include "D64.hpp" +#import "CSmachine+Subclassing.h" #import "NSBundle+DataResource.h" using namespace Commodore::Vic20; @@ -22,17 +23,14 @@ using namespace Commodore::Vic20; BOOL _joystickMode; } -- (CRTMachine::Machine * const)machine { - if(!_vic20) { - _vic20.reset(Commodore::Vic20::Machine::Vic20()); - } - return _vic20.get(); -} - (NSString *)userDefaultsPrefix { return @"vic20"; } - (instancetype)init { - self = [super init]; + Machine *machine = Commodore::Vic20::Machine::Vic20(); + + self = [super initWithMachine:machine]; if(self) { + _vic20.reset(machine); [self setDriveROM:[[NSBundle mainBundle] dataForResource:@"1540" withExtension:@"bin" subdirectory:@"ROMImages/Commodore1540"]]; [self setBASICROM:[self rom:@"basic"]]; [self setCountry:CSVic20CountryEuropean]; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.h index 3478e8c18..9b0bc7758 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.h @@ -12,6 +12,8 @@ @interface CSZX8081 : CSMachine +- (instancetype)init; + @property (nonatomic, assign) BOOL useFastLoadingHack; @property (nonatomic, assign) BOOL useAutomaticTapeMotorControl; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.mm index 1af610c48..4f897cbce 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSZX8081.mm @@ -18,16 +18,12 @@ std::unique_ptr _zx8081; } -- (CRTMachine::Machine * const)machine { - if(!_zx8081) { - _zx8081.reset(ZX8081::Machine::ZX8081()); - } - return _zx8081.get(); -} - - (instancetype)init { - self = [super init]; + ZX8081::Machine *machine = ZX8081::Machine::ZX8081(); + + self = [super initWithMachine:machine]; if(self) { + _zx8081.reset(machine); _zx8081->set_rom(ZX8081::ROMType::ZX80, [self rom:@"zx80"].stdVector8); _zx8081->set_rom(ZX8081::ROMType::ZX81, [self rom:@"zx81"].stdVector8); }