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
4 changes: 2 additions & 2 deletions Package.resolved

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

4 changes: 2 additions & 2 deletions Sources/OpenAttributeGraph/Graph/Subgraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension Subgraph {
}

extension Subgraph {
public func addObserver(_ observer: () -> Void) -> Int {
public func addObserver(_ observer: @escaping () -> Void) -> Int {
Subgraph.addObserver(self, observer: observer)
}

Expand Down Expand Up @@ -65,5 +65,5 @@ extension Subgraph {
private static func apply(_ graph: Subgraph, flags: Flags, callback: (AnyAttribute) -> Void)

@_silgen_name("OAGSubgraphAddObserver")
private static func addObserver(_ graph: Subgraph, observer: () -> Void) -> Int
private static func addObserver(_ graph: Subgraph, observer: @escaping () -> Void) -> Int
}
15 changes: 15 additions & 0 deletions Sources/OpenAttributeGraphCxx/Attribute/OAGAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ OAGGraphRef OAGGraphGetAttributeGraph(OAGAttribute attribute) {
}

OAGSubgraphRef OAGGraphGetAttributeSubgraph(OAGAttribute attribute) {
OAGSubgraphRef subgraph = OAGGraphGetAttributeSubgraph2(attribute);
if (subgraph == nullptr) {
OAG::precondition_failure("no subgraph");
}
return subgraph;
}

_Nullable OAGSubgraphRef OAGGraphGetAttributeSubgraph2(OAGAttribute attribute) {
auto attribute_id = OAG::AttributeID(attribute);
// attribute_id.validate_data_offset();
// auto subgraph = attribute_id.subgraph();
// if (subgraph == nullptr) {
// OAG::precondition_failure("internal error");
// }
// return subgraph->to_cf();
// TODO
return nullptr;
}
Expand Down
7 changes: 3 additions & 4 deletions Sources/OpenAttributeGraphCxx/Data/OAGUniqueID.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// OAGUniqueID.c
// OpenAttributeGraphCxx
//
// Audited for iOS 18.0
// Audited for 6.5.1
// Status: Complete

#include <OpenAttributeGraph/OAGUniqueID.h>
#include <stdatomic.h>

OAGUniqueID OAGMakeUniqueID(void) {
// Initial value is 1
static atomic_long counter = 1;
return counter++;
static atomic_ulong counter = 1;
return atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);
}
10 changes: 9 additions & 1 deletion Sources/OpenAttributeGraphCxx/Graph/OAGSubgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool OAGSubgraphIsDirty(OAGSubgraphRef cf_subgraph, OAGAttributeFlags flags) {
return false;
}

OAGUniqueID OAGSubgraphAddObserver(OAGSubgraphRef cf_subgraph,
OAGObserverID OAGSubgraphAddObserver(OAGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift),
const void * _Nullable context) {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
Expand All @@ -173,6 +173,14 @@ OAGUniqueID OAGSubgraphAddObserver(OAGSubgraphRef cf_subgraph,
return subgraph->add_observer(OAG::ClosureFunction<void>(function, context));
}

void OAGSubgraphRemoveObserver(OAGSubgraphRef cf_subgraph, OAGObserverID observerID) {
OAG::Subgraph *subgraph = cf_subgraph->subgraph;
if (subgraph == nullptr) {
return;
}
subgraph->remove_observer(observerID);
}

#if !OAG_TARGET_OS_WASI
static bool should_record_tree;
static dispatch_once_t should_record_tree_once;
Expand Down
4 changes: 4 additions & 0 deletions Sources/OpenAttributeGraphCxx/Graph/Subgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ OAGUniqueID OAG::Subgraph::add_observer(OAG::ClosureFunction<void> observer) con
return OAGMakeUniqueID();
}

void OAG::Subgraph::remove_observer(OAGUniqueID observerID) const OAG_NOEXCEPT {
// TODO
}

void OAG::Subgraph::begin_tree(OAG::AttributeID id, OAG::swift::metadata const* type, unsigned int flags) const OAG_NOEXCEPT {
// TODO
}
Expand Down
109 changes: 0 additions & 109 deletions Sources/OpenAttributeGraphCxx/Graph/Subgraph.hpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ OAG_EXPORT
OAG_REFINED_FOR_SWIFT
OAGSubgraphRef OAGGraphGetAttributeSubgraph(OAGAttribute attribute) OAG_SWIFT_NAME(getter:OAGAttribute.subgraph(self:));

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
_Nullable OAGSubgraphRef OAGGraphGetAttributeSubgraph2(OAGAttribute attribute) OAG_SWIFT_NAME(getter:OAGAttribute.subgraph2(self:));

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
const void * OAGGraphReadCachedAttribute(long hashValue, OAGTypeID bodyType, const void *bodyPointer, OAGTypeID valueType, OAGCachedValueOptions options, OAGAttribute attribute, bool unknown/*, ...*/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ OAG_EXPORT
OAG_REFINED_FOR_SWIFT
bool OAGSubgraphIsDirty(OAGSubgraphRef cf_subgraph, OAGAttributeFlags flags) OAG_SWIFT_NAME(OAGSubgraphRef.isDirty(self:flags:));

typedef long OAGObserverID OAG_SWIFT_NAME(ObserverID);

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
OAGUniqueID OAGSubgraphAddObserver(OAGSubgraphRef cf_subgraph,
OAGObserverID OAGSubgraphAddObserver(OAGSubgraphRef cf_subgraph,
const void (*function)(const void * _Nullable context OAG_SWIFT_CONTEXT) OAG_SWIFT_CC(swift),
const void * _Nullable context);

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
void OAGSubgraphRemoveObserver(OAGSubgraphRef cf_subgraph, OAGObserverID observerID) OAG_SWIFT_NAME(OAGSubgraphRef.removeObserver(self:_:));

OAG_EXPORT
OAG_REFINED_FOR_SWIFT
bool OAGSubgraphShouldRecordTree(void) OAG_SWIFT_NAME(getter:OAGSubgraphRef.shouldRecordTree());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// OAGUniqueID.h
// OpenAttributeGraphCxx
//
// Audited for iOS 18.0
// Audited for 6.5.1
// Status: Complete

#ifndef OAGUniqueID_h
#define OAGUniqueID_h

#include <OpenAttributeGraph/OAGBase.h>
typedef long OAGUniqueID;
typedef u_long OAGUniqueID OAG_SWIFT_NAME(UniqueID);

OAG_EXTERN_C_BEGIN
OAG_EXPORT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class Subgraph final {
void apply(OAGAttributeFlags flags, OAG::ClosureFunction<void, OAGAttribute> body) const OAG_NOEXCEPT;

OAGUniqueID add_observer(OAG::ClosureFunction<void> observer) const OAG_NOEXCEPT;

void remove_observer(OAGUniqueID observerID) const OAG_NOEXCEPT;

void begin_tree(OAG::AttributeID id, OAG::swift::metadata const *type, uint32_t flags) const OAG_NOEXCEPT;
void add_tree_value(OAG::AttributeID id, OAG::swift::metadata const *type, const char* key, uint32_t flags) const OAG_NOEXCEPT;
void end_tree(OAG::AttributeID id) const OAG_NOEXCEPT;
Expand Down
1 change: 0 additions & 1 deletion Sources/OpenAttributeGraphShims/GraphShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public typealias OAGAttributeInfo = AGAttributeInfo
public typealias OAGCachedValueOptions = AGCachedValueOptions
public typealias OAGChangedValueFlags = AGChangedValueFlags
public typealias OAGInputOptions = AGInputOptions
public typealias OAGUniqueID = AGUniqueID
public typealias OAGValue = AGValue
public typealias OAGValueOptions = AGValueOptions
public let attributeGraphEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,52 @@ struct SubgraphCompatibilityTests {
Subgraph.endTreeElement(value: value)
}
}

@Suite
struct ObserverTests {
@Test
func observerNotifiedOnSubgraphDestroyed() {
var notifiedCount = 0
do {
let graph = Graph()
do {
let subgraph = Subgraph(graph: graph)
let _ = subgraph.addObserver {
notifiedCount += 1
}
}
#expect(notifiedCount == 1)
}
// Observers aren't notified more than once
#expect(notifiedCount == 1)
}

@Test
func observerNotifiedOnGraphDestroyed() {
var notifiedCount = 0
do {
let graph = Graph()
let subgraph = Subgraph(graph: graph)
let _ = subgraph.addObserver {
notifiedCount += 1
}
#expect(notifiedCount == 0)
}
#expect(notifiedCount == 1)
}

@Test
func removedObserverNotNotified() {
var notifiedCount = 0
do {
let graph = Graph()
let subgraph = Subgraph(graph: graph)
let observerID = subgraph.addObserver {
notifiedCount += 1
}
subgraph.removeObserver(observerID)
}
#expect(notifiedCount == 0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public typealias OAGAttributeInfo = AGAttributeInfo
public typealias OAGCachedValueOptions = AGCachedValueOptions
public typealias OAGChangedValueFlags = AGChangedValueFlags
public typealias OAGInputOptions = AGInputOptions
public typealias OAGUniqueID = AGUniqueID
public typealias OAGValue = AGValue
public typealias OAGValueOptions = AGValueOptions
public let compatibilityTestEnabled = true
Expand Down