-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CtrlCHandler helper class (Swift) #3469
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a66342d
add files
bernardnormier 6d83a16
Fix implementation of CtrlCHandler in Swift
bernardnormier 2e9cb97
Small comment fix
bernardnormier 2f139fc
Clarify signals
bernardnormier 801aa4d
Better comment
bernardnormier a29a0de
Fix another typo
bernardnormier 796b364
Cleanup CtrlCHandler.cpp
bernardnormier b6ff47d
Rename function to catchSignal
bernardnormier 8dac59d
Make CtrlCHandler macOS-only
bernardnormier 20ec52d
Rework Swift/ObjC impl
bernardnormier fca168e
Fix review comments
bernardnormier bece94b
Fix iOS build failure
bernardnormier 7969c17
Fix ios fix
bernardnormier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) ZeroC, Inc. | ||
|
||
import IceImpl | ||
|
||
#if os(macOS) | ||
/// Helps applications handle Ctrl+C (SIGINT) and similar signals (SIGHUP and SIGTERM). Only available on macOS. | ||
public final class CtrlCHandler { | ||
private let handle = ICECtrlCHandler() | ||
|
||
/// Creates a CtrlCHandler. Only one instance of this class can exist in a program at any point in time. | ||
/// This instance must be created before starting any thread. | ||
public init() {} | ||
|
||
/// Waits until this handler catches a Ctrl+C or similar signal. | ||
/// - Returns: The signal number. | ||
public func catchSignal() async -> Int32 { | ||
return await withCheckedContinuation { continuation in | ||
self.handle.catchSignal { signal in | ||
continuation.resume(returning: signal) | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) ZeroC, Inc. | ||
|
||
#import "include/CtrlCHandler.h" | ||
|
||
#if TARGET_OS_OSX != 0 | ||
@implementation ICECtrlCHandler | ||
|
||
- (void)catchSignal:(void (^)(int))callback | ||
{ | ||
[[maybe_unused]] Ice::CtrlCHandlerCallback previousCallback = self->cppObject.setCallback( | ||
[callback, self](int signal) | ||
{ | ||
// This callback executes in the C++ CtrlCHandler thread. | ||
|
||
// We need an autorelease pool in case the callback creates autorelease objects. | ||
@autoreleasepool | ||
{ | ||
callback(signal); | ||
} | ||
|
||
// Then remove callback | ||
self->cppObject.setCallback(nullptr); | ||
}); | ||
|
||
assert(previousCallback == nullptr); | ||
} | ||
@end | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) ZeroC, Inc. | ||
|
||
#import "Config.h" | ||
|
||
#if TARGET_OS_OSX != 0 | ||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
// The implementation of Ice.CtrlCHandler, which wraps the C++ class Ice::CtrlCHandler. | ||
ICEIMPL_API @interface ICECtrlCHandler : NSObject | ||
- (void)catchSignal:(void (^)(int))callback; | ||
@end | ||
|
||
# ifdef __cplusplus | ||
|
||
@interface ICECtrlCHandler () | ||
{ | ||
Ice::CtrlCHandler cppObject; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's essential to use a field and not a property here, since a property wants to return a copy of the C++ object (which is not copyable). |
||
} | ||
@end | ||
|
||
# endif | ||
|
||
NS_ASSUME_NONNULL_END | ||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not having these made the class rather bug-prone.