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

add: NSObject Publisher #88

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme LastUpgradeVersion = "9999" version = "1.3">
<BuildAction parallelizeBuildables = "YES" buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry buildForTesting = "YES" buildForRunning = "YES" buildForProfiling = "YES" buildForArchiving = "YES" buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BuildableName = "'lib$(TARGET_NAME)'"
BlueprintName = "Runtime"
ReferencedContainer = "container:CombineCocoa.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry buildForTesting = "YES" buildForRunning = "YES" buildForProfiling = "YES" buildForArchiving = "YES" buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BuildableName = "'lib$(TARGET_NAME)'"
BlueprintName = "CombineCocoa"
ReferencedContainer = "container:CombineCocoa.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
</Testables>
</TestAction>
<Scheme
LastUpgradeVersion = "9999"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CombineCocoa::Runtime"
BuildableName = "Runtime.framework"
BlueprintName = "Runtime"
ReferencedContainer = "container:CombineCocoa.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CombineCocoa::CombineCocoa"
BuildableName = "CombineCocoa.framework"
BlueprintName = "CombineCocoa"
ReferencedContainer = "container:CombineCocoa.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
167 changes: 167 additions & 0 deletions Sources/CombineCocoa/Interception/NSObject+Association.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This folder (Sources/CombineCocoa/Interception) is actually a port of ReactiveCocoa code, so maybe it is worth mentioning them in Readme/Credits/Acknowledgements

// NSObject+Association.swift
// CombineCocoa
//
// Created by Maxim Krouk on 22.06.21.
// Copyright © 2020 Combine Community. All rights reserved.
//

import Foundation

#if canImport(Runtime)
import Runtime
#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