Skip to content
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

FeatureRequest: Add handlers for selectors #87

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ let package = Package(
],
dependencies: [],
targets: [
.target(name: "CombineCocoa", dependencies: ["Runtime"]),
.target(name: "Runtime", dependencies: [])
.target(
name: "CombineCocoa",
dependencies: [
"CombineCocoaInterception",
"CombineCocoaRuntime"
]),
.target(
name: "CombineCocoaInterception",
dependencies: [
.target(name: "CombineCocoaRuntime")
]
),
.target(name: "CombineCocoaRuntime")
]
)
13 changes: 0 additions & 13 deletions Sources/CombineCocoa/CombineCocoa.h

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/CombineCocoa/DelegateProxy/DelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import Foundation
import Combine

#if canImport(Runtime)
import Runtime
#if canImport(CombineCocoaRuntime)
import CombineCocoaRuntime
#endif

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
Expand Down
8 changes: 8 additions & 0 deletions Sources/CombineCocoa/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Exports.swift
//
//
// Created by Benjamin Deckys on 2023/07/27.
//

@_exported import CombineCocoaInterception
167 changes: 167 additions & 0 deletions Sources/CombineCocoaInterception/NSObject+Association.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//
// NSObject+Association.swift
// CombineCocoa
//
// Created by Maxim Krouk on 22.06.21.
// Copyright © 2020 Combine Community. All rights reserved.
//

import Foundation

#if canImport(CombineCocoaRuntime)
import CombineCocoaRuntime
#endif

internal struct AssociationKey<Value> {
fileprivate let address: UnsafeRawPointer
fileprivate let `default`: Value!

/// Create an ObjC association key.
///
/// - warning: The key must be uniqued.
///
/// - parameters:
/// - default: The default value, or `nil` to trap on undefined value. It is
/// ignored if `Value` is an optional.
init(default: Value? = nil) {
self.address = UnsafeRawPointer(
UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
)
self.default = `default`
}

/// Create an ObjC association key from a `StaticString`.
///
/// - precondition: `key` has a pointer representation.
///
/// - parameters:
/// - default: The default value, or `nil` to trap on undefined value. It is
/// ignored if `Value` is an optional.
init(_ key: StaticString, default: Value? = nil) {
assert(key.hasPointerRepresentation)
self.address = UnsafeRawPointer(key.utf8Start)
self.default = `default`
}

/// Create an ObjC association key from a `Selector`.
///
/// - parameters:
/// - default: The default value, or `nil` to trap on undefined value. It is
/// ignored if `Value` is an optional.
init(_ key: Selector, default: Value? = nil) {
self.address = UnsafeRawPointer(key.utf8Start)
self.default = `default`
}
}

internal struct Associations<Base: AnyObject> {
fileprivate let base: Base

init(_ base: Base) {
self.base = base
}
}

extension NSObjectProtocol {
/// Retrieve the associated value for the specified key. If the value does not
/// exist, `initial` would be called and the returned value would be
/// associated subsequently.
///
/// - parameters:
/// - key: An optional key to differentiate different values.
/// - initial: The action that supples an initial value.
///
/// - returns: The associated value for the specified key.
internal func associatedValue<T>(
forKey key: StaticString = #function,
initial: (Self) -> T
) -> T {
let key = AssociationKey<T?>(key)

if let value = associations.value(forKey: key) {
return value
}

let value = initial(self)
associations.setValue(value, forKey: key)

return value
}
}

extension NSObjectProtocol {
@nonobjc internal var associations: Associations<Self> {
return Associations(self)
}
}

extension Associations {
/// Retrieve the associated value for the specified key.
///
/// - parameters:
/// - key: The key.
///
/// - returns: The associated value, or the default value if no value has been
/// associated with the key.
internal func value<Value>(
forKey key: AssociationKey<Value>
) -> Value {
return (objc_getAssociatedObject(base, key.address) as! Value?) ?? key.default
}

/// Retrieve the associated value for the specified key.
///
/// - parameters:
/// - key: The key.
///
/// - returns: The associated value, or `nil` if no value is associated with
/// the key.
internal func value<Value>(
forKey key: AssociationKey<Value?>
) -> Value? {
return objc_getAssociatedObject(base, key.address) as! Value?
}

/// Set the associated value for the specified key.
///
/// - parameters:
/// - value: The value to be associated.
/// - key: The key.
internal func setValue<Value>(
_ value: Value,
forKey key: AssociationKey<Value>
) {
objc_setAssociatedObject(base, key.address, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}

/// Set the associated value for the specified key.
///
/// - parameters:
/// - value: The value to be associated.
/// - key: The key.
internal func setValue<Value>(
_ value: Value?,
forKey key: AssociationKey<Value?>
) {
objc_setAssociatedObject(base, key.address, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

/// Set the associated value for the specified key.
///
/// - parameters:
/// - value: The value to be associated.
/// - key: The key.
/// - address: The address of the object.
internal func unsafeSetAssociatedValue<Value>(
_ value: Value?,
forKey key: AssociationKey<Value>,
forObjectAt address: UnsafeRawPointer
) {
_combinecocoa_objc_setAssociatedObject(
address,
key.address,
value,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
Loading