Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions Sources/OpenAttributeGraph/Attribute/Weak/AnyWeakAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@
// AnyWeakAttribute.swift
// OpenAttributeGraph
//
// Audited for RELEASE_2021
// Audited for 6.5.1
// Status: Complete

public import OpenAttributeGraphCxx

public typealias AnyWeakAttribute = OAGWeakAttribute

extension AnyWeakAttribute {
@_alwaysEmitIntoClient
public init(_ attribute: AnyAttribute?) {
self = __OAGCreateWeakAttribute(attribute ?? .nil)
}

@_alwaysEmitIntoClient
public init<Value>(_ weakAttribute: WeakAttribute<Value>) {
self = weakAttribute.base
}

@_alwaysEmitIntoClient

public func unsafeCast<Value>(to _: Value.Type) -> WeakAttribute<Value> {
WeakAttribute(base: self)
}

@_alwaysEmitIntoClient

public var attribute: AnyAttribute? {
get {
let attribute = __OAGWeakAttributeGetAttribute(self)
Expand All @@ -38,21 +32,14 @@ extension AnyWeakAttribute {
}

extension AnyWeakAttribute: Swift.Hashable {
@_alwaysEmitIntoClient
public static func == (lhs: AnyWeakAttribute, rhs: AnyWeakAttribute) -> Bool {
lhs._details.identifier == rhs._details.identifier && lhs._details.seed == rhs._details.seed
}

@_alwaysEmitIntoClient

public func hash(into hasher: inout Hasher) {
hasher.combine(_details.identifier)
hasher.combine(_details.seed)
}

@_alwaysEmitIntoClient
public var hashValue: Int {
_hashValue(for: self)
}
}

extension AnyWeakAttribute: Swift.CustomStringConvertible {
Expand Down
56 changes: 29 additions & 27 deletions Sources/OpenAttributeGraph/Attribute/Weak/WeakAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// WeakAttribute.swift
// OpenAttributeGraph
//
// Audited for RELEASE_2021
// Audited for 6.5.1
// Status: Complete

public import OpenAttributeGraphCxx
Expand All @@ -11,53 +11,57 @@ public import OpenAttributeGraphCxx
@propertyWrapper
@dynamicMemberLookup
public struct WeakAttribute<Value> {
@usableFromInline
var base: AnyWeakAttribute

@_alwaysEmitIntoClient

public init(base: AnyWeakAttribute) {
self.base = base
}

public init() {
base = AnyWeakAttribute(_details: AnyWeakAttribute.__Unnamed_struct__details(identifier: AnyAttribute(rawValue: 0), seed: 0))
base = AnyWeakAttribute(_details: .init(identifier: .init(rawValue: 0), seed: 0))
}

public init(_ attribute: Attribute<Value>) {
base = AnyWeakAttribute(attribute.identifier)
}

public init(_ attribute: Attribute<Value>?) {
base = AnyWeakAttribute(attribute?.identifier)
}

public var wrappedValue: Value? { value }


public var wrappedValue: Value? {
OAGGraphGetWeakValue(base, type: Value.self)
.value?
.assumingMemoryBound(to: Value.self)
.pointee
}

public var projectedValue: Attribute<Value>?{
get { attribute }
set { attribute = newValue }
_modify { yield &attribute }
}

public subscript<Member>(dynamicMember keyPath: KeyPath<Value, Member>) -> Attribute<Member>? {
attribute?[keyPath: keyPath]
}

public var attribute: Attribute<Value>? {
get { base.attribute?.unsafeCast(to: Value.self) }
set { base.attribute = newValue?.identifier }
}

public var value: Value? {
OAGGraphGetWeakValue(base, type: Value.self)
.value
// TO BE CONFIRMED
.assumingMemoryBound(to: Value?.self)
.pointee
}

public func changedValue(options: OAGValueOptions = []) -> (value: Value, changed: Bool)? {
attribute?.changedValue(options: options)

public var value: Value? { wrappedValue }

public func changedValue(options: OAGValueOptions) -> (value: Value, changed: Bool)? {
let value = OAGGraphGetWeakValue(base, options: options, type: Value.self)
guard let ptr = value.value else {
return nil
}
return (
ptr.assumingMemoryBound(to: Value.self).pointee,
value.flags.contains(.changed)
)
}
}

Expand All @@ -68,6 +72,4 @@ extension WeakAttribute: CustomStringConvertible {
}

@_silgen_name("OAGGraphGetWeakValue")
@inline(__always)
@inlinable
func OAGGraphGetWeakValue<Value>(_ attribute: AnyWeakAttribute, options: OAGValueOptions = [], type: Value.Type = Value.self) -> OAGValue
private func OAGGraphGetWeakValue<Value>(_ attribute: AnyWeakAttribute, options: OAGValueOptions = [], type: Value.Type = Value.self) -> OAGWeakValue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

OAG_ASSUME_NONNULL_BEGIN

typedef struct OAGWeakAttribute {
typedef OAG_SWIFT_STRUCT struct {
struct {
OAGAttribute identifier;
uint32_t seed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ struct AnyAttributeCompatibilityTests {
}
}
}

@Test
func subgraph() {
let identifier = Attribute(value: 0).identifier
#expect(identifier.subgraph2 != nil)
}
#endif
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ struct AnyWeakAttributeCompatibilityTests {
}
#expect(AnyWeakAttribute(nil).description == "nil")
}

@Test
func dict() {
let w1 = AnyWeakAttribute(nil)
let w2 = AnyWeakAttribute(Attribute(value: 0).identifier)
let dict: [AnyWeakAttribute: Int] = [
w1: 1,
w2: 2,
]
#expect(dict[w1] == 1)
#expect(dict[w2] == 2)
}
}
#endif