From c9ad05a693aa83d3939e8275827d73d5ee65f470 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Mon, 16 Mar 2015 16:02:35 +0100 Subject: [PATCH] Cleaner way of invoking callback --- AVRsack.xcodeproj/project.pbxproj | 6 ++++++ AVRsack/ASProjDoc.swift | 7 +------ AVRsack/ASVestigial.h | 20 ++++++++++++++++++++ AVRsack/ASVestigial.mm | 20 ++++++++++++++++++++ AVRsack/AVRsack-Bridging-Header.h | 3 ++- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 AVRsack/ASVestigial.h create mode 100644 AVRsack/ASVestigial.mm diff --git a/AVRsack.xcodeproj/project.pbxproj b/AVRsack.xcodeproj/project.pbxproj index 9b78424..4b327d4 100644 --- a/AVRsack.xcodeproj/project.pbxproj +++ b/AVRsack.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 951CD1741A23C9FC0066C1A1 /* ASBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951CD1731A23C9FC0066C1A1 /* ASBuilder.swift */; }; 951CD1771A2615000066C1A1 /* BuildProject in Resources */ = {isa = PBXBuildFile; fileRef = 951CD1761A2615000066C1A1 /* BuildProject */; }; 95388E5D1AB6882600061435 /* FileRevision in Resources */ = {isa = PBXBuildFile; fileRef = 95388E5C1AB6882600061435 /* FileRevision */; }; + 95388E621AB7235000061435 /* ASVestigial.mm in Sources */ = {isa = PBXBuildFile; fileRef = 95388E611AB7235000061435 /* ASVestigial.mm */; }; 9538E0EC1A8FB215001E02CC /* ACEView.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 95EA325B1A17B8DA00F66EB0 /* ACEView.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 95468DDF1A228BE600668EE2 /* ASHardware.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95468DDE1A228BE600668EE2 /* ASHardware.swift */; }; 95468DE31A228E1300668EE2 /* Defaults.plist in Resources */ = {isa = PBXBuildFile; fileRef = 95468DE21A228E1300668EE2 /* Defaults.plist */; }; @@ -104,6 +105,8 @@ 951CD1731A23C9FC0066C1A1 /* ASBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASBuilder.swift; sourceTree = ""; }; 951CD1761A2615000066C1A1 /* BuildProject */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; fileEncoding = 4; path = BuildProject; sourceTree = ""; }; 95388E5C1AB6882600061435 /* FileRevision */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; fileEncoding = 4; path = FileRevision; sourceTree = ""; }; + 95388E601AB7235000061435 /* ASVestigial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASVestigial.h; sourceTree = ""; }; + 95388E611AB7235000061435 /* ASVestigial.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ASVestigial.mm; sourceTree = ""; }; 95468DDE1A228BE600668EE2 /* ASHardware.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASHardware.swift; sourceTree = ""; }; 95468DE21A228E1300668EE2 /* Defaults.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Defaults.plist; sourceTree = ""; }; 95539B661A3E7EAF00D8595C /* ASSerial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASSerial.h; sourceTree = ""; }; @@ -219,6 +222,8 @@ 95DF20661A45A6090013D1B5 /* ASSketchBook.swift */, 956005F21A4EF79D00327007 /* ASSerialWin.swift */, 95DA9B151A515AF700DE018D /* ACEViewExt.swift */, + 95388E601AB7235000061435 /* ASVestigial.h */, + 95388E611AB7235000061435 /* ASVestigial.mm */, ); name = Source; sourceTree = ""; @@ -409,6 +414,7 @@ files = ( 956005F41A4EF79D00327007 /* ASSerialWin.swift in Sources */, 9501D8041A17025C0034C530 /* ASProjDoc.swift in Sources */, + 95388E621AB7235000061435 /* ASVestigial.mm in Sources */, 95539B681A3E7EAF00D8595C /* ASSerial.mm in Sources */, 95468DDF1A228BE600668EE2 /* ASHardware.swift in Sources */, 95DA9B161A515AF700DE018D /* ACEViewExt.swift in Sources */, diff --git a/AVRsack/ASProjDoc.swift b/AVRsack/ASProjDoc.swift index 5dd205a..64967f3 100644 --- a/AVRsack/ASProjDoc.swift +++ b/AVRsack/ASProjDoc.swift @@ -284,14 +284,9 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa // MARK: Printing override func printDocumentWithSettings(printSettings: [NSObject : AnyObject], showPrintPanel: Bool, delegate: AnyObject?, didPrintSelector: Selector, contextInfo: UnsafeMutablePointer) { - // - // Thanks to Erica Sadun for showing me how to call a selector in Swift - // printingDone = { () -> () in - if let del : AnyObject = delegate { - NSThread.detachNewThreadSelector(didPrintSelector, toTarget: del, withObject: contextInfo as? AnyObject) - } + InvokeCallback(delegate, didPrintSelector, contextInfo); } if let logNode = mainEditor as? ASLogNode { let url = fileURL!.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(logNode.path) diff --git a/AVRsack/ASVestigial.h b/AVRsack/ASVestigial.h new file mode 100644 index 0000000..aef0f91 --- /dev/null +++ b/AVRsack/ASVestigial.h @@ -0,0 +1,20 @@ +// +// ASVestigial.h +// AVRsack +// +// Created by Matthias Neeracher on 16/03/15. +// Copyright © 2015 Aere Perennius. All rights reserved. +// + +#import + +#ifdef __cplusplus +extern "C" { +#endif + +void +InvokeCallback(id target, SEL selector, void * context); + +#ifdef __cplusplus +} +#endif diff --git a/AVRsack/ASVestigial.mm b/AVRsack/ASVestigial.mm new file mode 100644 index 0000000..2a1d564 --- /dev/null +++ b/AVRsack/ASVestigial.mm @@ -0,0 +1,20 @@ +// +// ASVestigial.m +// AVRsack +// +// Created by Matthias Neeracher on 16/03/15. +// Copyright © 2015 Aere Perennius. All rights reserved. +// + +#import "ASVestigial.h" + +void +InvokeCallback(id target, SEL selector, void * context) +{ + if (!target) + return; + NSMethodSignature * sig = [target methodSignatureForSelector:selector]; + NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:sig]; + [invocation setArgument:&context atIndex:2]; + [invocation invokeWithTarget:target]; +} \ No newline at end of file diff --git a/AVRsack/AVRsack-Bridging-Header.h b/AVRsack/AVRsack-Bridging-Header.h index 4305640..b2a5f03 100644 --- a/AVRsack/AVRsack-Bridging-Header.h +++ b/AVRsack/AVRsack-Bridging-Header.h @@ -5,4 +5,5 @@ #import #import -#import "ASSerial.h" \ No newline at end of file +#import "ASSerial.h" +#import "ASVestigial.h"