Skip to content
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
16 changes: 16 additions & 0 deletions Sources/OpenAttributeGraph/Attribute/Attribute/Attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,27 @@ extension Attribute {
}
}

public init<R: Rule>(_ rule: R, initialValue: Value) where R.Value == Value {
self = withUnsafePointer(to: rule) { pointer in
withUnsafePointer(to: initialValue) { initialValuePointer in
Attribute(body: pointer, value: initialValuePointer) { R._update }
}
}
}

public init<R: StatefulRule>(_ rule: R) where R.Value == Value {
self = withUnsafePointer(to: rule) { pointer in
Attribute(body: pointer, value: nil) { R._update }
}
}

public init<R: StatefulRule>(_ rule: R, initialValue: Value) where R.Value == Value {
self = withUnsafePointer(to: rule) { pointer in
withUnsafePointer(to: initialValue) { initialValuePointer in
Attribute(body: pointer, value: initialValuePointer) { R._update }
}
}
}
}

@_silgen_name("OAGGraphCreateAttribute")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
// Status: WIP

public protocol AttributeBodyVisitor {
func visit<Body: _AttributeBody>(body: UnsafePointer<Body>)
mutating func visit<Body: _AttributeBody>(body: UnsafePointer<Body>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ public struct IndirectAttribute<Value> {
identifier = source.identifier.createIndirect(size: UInt64(MemoryLayout<Value>.size))
}

public var attribute: Attribute<Value> {
Attribute(identifier: identifier)
}

public var source: Attribute<Value> {
get { Attribute(identifier: identifier.source) }
nonmutating set { identifier.source = newValue.identifier }
}

public func resetSource() {
__OAGGraphResetIndirectAttribute(identifier, false)
}

public var dependency: AnyAttribute? {
get {
let dependency = identifier._indirectDependency
Expand Down
4 changes: 4 additions & 0 deletions Sources/OpenAttributeGraphCxx/Attribute/OAGAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ void OAGGraphSetIndirectAttribute(OAGAttribute attribute1, OAGAttribute attribut
// TODO
}

void OAGGraphResetIndirectAttribute(OAGAttribute attribute, bool non_nil) {
// TODO
}

OAGAttribute OAGGraphCreateAttribute(long index, const void *body, const void * value) {
// TODO
return OAGAttributeNil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ OAG_EXPORT
OAG_REFINED_FOR_SWIFT
void OAGGraphSetIndirectAttribute(OAGAttribute attribute1, OAGAttribute attribute2) OAG_SWIFT_NAME(setter:OAGAttribute.source(self:_:));

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
void OAGGraphResetIndirectAttribute(OAGAttribute attribute, bool non_nil);

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
OAGAttribute OAGGraphCreateAttribute(long index, const void *body, const void * _Nullable value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ struct AttributeCompatibilityTests {
#expect(intAttribute.value == 0)
}

@Test
func initWithRule() {
struct TestRule: Rule {
var value: Int {
return 0
}
}
let attribute1 = Attribute(TestRule())
#expect(attribute1.value == 0)

let attribute2 = Attribute(TestRule(), initialValue: 1)
#expect(attribute2.value == 1)
}

@Test
func initWithStatefulRule() {
struct TestStatefuleRule: StatefulRule {
typealias Value = Int
func updateValue() {
withUnsafePointer(to: 0) { ptr in
Graph.setOutputValue(ptr)
}
}
}
let attribute1 = Attribute(TestStatefuleRule())
#expect(attribute1.value == 0)

let attribute2 = Attribute(TestStatefuleRule(), initialValue: 1)
#expect(attribute2.value == 1)
}

@Test
func hashableAndEquatable() {
let a = Attribute<Int>(identifier: .nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ struct IndirectAttributeCompatibilityTests {
func basic() {
let source = Attribute(value: 0)
let indirect = IndirectAttribute(source: source)
#expect(indirect.attribute.identifier == indirect.identifier)
#expect(indirect.identifier != source.identifier)
#expect(indirect.source.identifier == source.identifier)
#expect(indirect.dependency == .init(rawValue: 0))
}

@Test
func resetSource() {
let source1 = Attribute(value: 0)
let indirect = IndirectAttribute(source: source1)
#expect(indirect.source.identifier == source1.identifier)

let source2 = Attribute(value: 0)
indirect.source = source2
#expect(indirect.source.identifier == source2.identifier)

indirect.resetSource()
#expect(indirect.source.identifier == source1.identifier)
}
}
#endif
Loading