-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGraph.swift
More file actions
145 lines (123 loc) · 3.67 KB
/
Graph.swift
File metadata and controls
145 lines (123 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Graph.swift
// OpenAttributeGraph
//
// Audited for RELEASE_2021
// Status: WIP
public import OpenAttributeGraphCxx
extension Graph {
@_silgen_name("OAGGraphInternAttributeType")
public static func typeIndex(
ctx: UnownedGraphContext,
body: Metadata,
makeAttributeType: () -> UnsafePointer<_AttributeType>
) -> Int
}
@_silgen_name("OAGGraphSetInvalidationCallback")
private func OAGGraphSetInvalidationCallback(
graph: Graph,
_ callback: @escaping (AnyAttribute) -> Void
)
@_silgen_name("OAGGraphSetUpdateCallback")
private func OAGGraphSetUpdateCallback(
graph: Graph,
_ callback: @escaping () -> Void
)
extension Graph {
public static func withoutUpdate<V>(_ body: () -> V) -> V {
let update = clearUpdate()
defer { setUpdate(update) }
return body()
}
public func withoutSubgraphInvalidation<V>(_ body: () -> V) -> V {
preconditionFailure("TODO")
}
public func withDeadline<V>(
_: UInt64,
_: () -> V
) -> V {
preconditionFailure("TODO")
}
public func onInvalidation(_ callback: @escaping (AnyAttribute) -> Void) {
OAGGraphSetInvalidationCallback(graph: self, callback)
}
public func onUpdate(_ callback: @escaping () -> Void) {
OAGGraphSetUpdateCallback(graph: self, callback)
}
public func withMainThreadHandler(_: (() -> Void) -> Void, do: () -> Void) {
// TODO: OAGGraphWithMainThreadHandler
preconditionFailure("TODO")
}
}
extension Graph {
@_transparent
public func startProfiling() {
__OAGGraphStartProfiling(self)
}
@_transparent
public func stopProfiling() {
__OAGGraphStopProfiling(self)
}
@_transparent
public func resetProfile() {
__OAGGraphResetProfile(self)
}
public static func startProfiling() {
__OAGGraphStartProfiling(nil)
}
public static func stopProfiling() {
__OAGGraphStopProfiling(nil)
}
public static func resetProfile() {
__OAGGraphResetProfile(nil)
}
}
extension Graph {
@_transparent
public var mainUpdates: Int { numericCast(counter(for: .mainThreadUpdates)) }
}
extension Graph {
// NOTE: Currently Swift does not support generic computed variable
@_silgen_name("OAGGraphGetOutputValue")
@inline(__always)
@inlinable
public static func outputValue<Value>() -> UnsafePointer<Value>?
@_silgen_name("OAGGraphSetOutputValue")
@inline(__always)
@inlinable
public static func setOutputValue<Value>(_ value: UnsafePointer<Value>)
}
extension Graph {
@_transparent
public static func anyInputsChanged(excluding excludedInputs: [AnyAttribute]) -> Bool {
return __OAGGraphAnyInputsChanged(excludedInputs, excludedInputs.count)
}
}
#if canImport(Darwin)
import Foundation
#endif
extension Graph {
public func archiveJSON(name: String?) {
#if canImport(Darwin)
let options: NSDictionary = [
DescriptionOption.format: "graph/dict",
DescriptionOption.includeValues: true,
]
guard let description = Graph.description(self, options: options) as? [String: Any] else {
return
}
var name = name ?? "graph"
name.append(".ag-json")
let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(name)
guard let data = try? JSONSerialization.data(withJSONObject: description, options: []) else {
return
}
let url = URL(fileURLWithPath: path)
do {
try data.write(to: url, options: [])
print("Wrote graph data to \"\(path)\".")
} catch {
}
#endif
}
}