Skip to content

Update mutateBody implementation #61

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

Merged
merged 1 commit into from
Mar 31, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/);
void AGGraphMutateAttribute(AGAttribute attribute,
const AGTypeID type,
bool invalidating,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/);
void AGGraphMutateAttribute(AGAttribute attribute,
const AGTypeID type,
bool invalidating,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/);
void AGGraphMutateAttribute(AGAttribute attribute,
const AGTypeID type,
bool invalidating,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
Expand Down
6 changes: 5 additions & 1 deletion AG/Sources/Headers/AGAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const AGAttributeInfo AGGraphGetAttributeInfo(AGAttribute attribute) AG_SWIFT_NA

AG_EXPORT
AG_REFINED_FOR_SWIFT
void AGGraphMutateAttribute(AGAttribute attribute, const AGTypeID type, bool invalidating/*, closure*/);
void AGGraphMutateAttribute(AGAttribute attribute,
const AGTypeID type,
bool invalidating,
const void (*function)(const void * _Nullable context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift),
const void * _Nullable context);

AG_EXPORT
AG_REFINED_FOR_SWIFT
Expand Down
18 changes: 15 additions & 3 deletions Sources/OpenGraph/Attribute/Attribute/OGAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ extension OGAttribute {
bodyType._visitBody(&visitor, info.body)
}

public func mutateBody<Value>(as type: Value.Type, invalidating: Bool, _: (inout Value) -> Void) {
// FIXME: pass body
__OGGraphMutateAttribute(self, OGTypeID(type), invalidating)
public func mutateBody<Value>(as type: Value.Type, invalidating: Bool, _ body: (inout Value) -> Void) {
OGAttribute.mutateAttribute(self, type: OGTypeID(type), invalidating: invalidating) { value in
body(&value.assumingMemoryBound(to: Value.self).pointee)
}
}

public func breadthFirstSearch(options _: OGSearchOptions = [], _: (OGAttribute) -> Bool) -> Bool {
Expand Down Expand Up @@ -83,3 +84,14 @@ extension OGAttribute: CustomStringConvertible {
}

public typealias AttributeUpdateBlock = () -> (UnsafeMutableRawPointer, OGAttribute) -> Void

// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension OGAttribute {
@_silgen_name("OGGraphMutateAttribute")
private static func mutateAttribute(
_ attribute: OGAttribute,
type: OGTypeID,
invalidating: Bool,
body: (UnsafeMutableRawPointer) -> Void
)
}
10 changes: 9 additions & 1 deletion Sources/_OpenGraph/Attribute/AttributeID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "OGBase.h"
#include "OGAttribute.h"
#include "../Util/assert.hpp"

namespace OG {
class AttributeID final {
Expand All @@ -32,7 +33,7 @@ class AttributeID final {
}
public:
OG_INLINE OG_CONSTEXPR
AttributeID(OGAttribute attribute) OG_NOEXCEPT:
AttributeID(OGAttribute& attribute) OG_NOEXCEPT:
_rawValue(attribute) {}

OG_INLINE OG_CONSTEXPR
Expand Down Expand Up @@ -61,6 +62,13 @@ class AttributeID final {
const bool isNil() const OG_NOEXCEPT {
return getKind() == Kind::Nil;
}

OG_INLINE OG_CONSTEXPR
const void checkIsDirect() const OG_NOEXCEPT {
if (!isDirect()) {
OG::precondition_failure("non-direct attribute id: %u", _rawValue);
}
}
};
static_assert(sizeof(AttributeID) == sizeof(uint32_t));
}
Expand Down
26 changes: 12 additions & 14 deletions Sources/_OpenGraph/Attribute/OGAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,38 @@ OGAttribute OGGraphCreateIndirectAttribute2(OGAttribute attribute, uint64_t size

OGAttributeFlags OGGraphGetFlags(OGAttribute attribute) {
const OG::AttributeID id = OG::AttributeID(attribute);
if (!id.isDirect()) {
OG::precondition_failure("non-direct attribute id: %u", id);
}
id.checkIsDirect();
// TODO: data/table
return OGAttributeFlagsDefault;
}

void OGGraphSetFlags(OGAttribute attribute, OGAttributeFlags flags) {
const OG::AttributeID id = OG::AttributeID(attribute);
if (!id.isDirect()) {
OG::precondition_failure("non-direct attribute id: %u", id);
}
id.checkIsDirect();
// TODO: data/table
}

void OGGraphAddInput(OGAttribute attribute1, OGAttribute attribute2, OGInputOptions options, long token) {
const OG::AttributeID id = OG::AttributeID(attribute1);
if (!id.isDirect()) {
OG::precondition_failure("non-direct attribute id: %u", id);
}
id.checkIsDirect();
// TODO: data/table
}

const OGAttributeInfo OGGraphGetAttributeInfo(OGAttribute attribute) {
const OG::AttributeID id = OG::AttributeID(attribute);
if (!id.isDirect()) {
OG::precondition_failure("non-direct attribute id: %u", id);
}
id.checkIsDirect();
// TODO
return { nullptr, nullptr };
}

void OGGraphMutateAttribute(OGAttribute attribute, const OGTypeID type, bool invalidating) {
// TODO
void OGGraphMutateAttribute(OGAttribute attribute,
const OGTypeID type,
bool invalidating,
const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT, void *body) OG_SWIFT_CC(swift),
const void * _Nullable context) {
const OG::AttributeID id = OG::AttributeID(attribute);
id.checkIsDirect();
// attribute_modify(AG::data::ptr<AG::Node>, AGSwiftMetadata const*, AG::ClosureFunction<void, void*>, bool
}

OGAttribute OGGraphGetIndirectDependency(OGAttribute attribute) {
Expand Down
6 changes: 5 additions & 1 deletion Sources/_OpenGraph/Attribute/OGAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const OGAttributeInfo OGGraphGetAttributeInfo(OGAttribute attribute) OG_SWIFT_NA

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphMutateAttribute(OGAttribute attribute, const OGTypeID type, bool invalidating/*, closure*/);
void OGGraphMutateAttribute(OGAttribute attribute,
const OGTypeID type,
bool invalidating,
const void (*function)(const void * _Nullable context OG_SWIFT_CONTEXT, void *body) OG_SWIFT_CC(swift),
const void * _Nullable context);

OG_EXPORT
OG_REFINED_FOR_SWIFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,13 @@ final class AttributeTests: AttributeTestBase {
attribute.prefetchValue()
attribute.invalidateValue()
}

@Test
func mutateBodyAPI() {
let attribute = Attribute(value: 5)
attribute.mutateBody(as: External<Int>.self, invalidating: true) { _ in

}
}
}
#endif