-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Re-design struct API to facilitate reference semantics #35840
Comments
To align the struct and non struct api's and to support struct by value (#36730), we have settled on the following: @struct Coordinate extends NativeType {
@Int32 int x;
@Int32 int y;
} And uses will look like the following Pointer<Coordinate> p;
Coordinate c = p.load(); // Which creates a 'reference', it does not actually load the value.
int x = c.x; // This loads the value. We're still debating whether in some cases we do want to load the whole struct. In any case, we have settled on the api and syntax, so I'm closing this issue. |
In order to load the address of a struct, we need at least: class Struct<T> {
Pointer<T> get addressOf;
}
class SomeStruct extends Struct<SomeStruct> {
// ...
} |
But this is not yet fully implemented, I presume? |
That remark is old, the new API (which is implemented) is outlined in #37229 |
Structs are a bit different from the primitives supported in the FFI. Since structs are value types in C++, and Dart does not have value types, structs cannot be used with
load
andstore
. Instead, we provide a way to load from and store into the fields of the structs.We can represent structs in different ways:
Coordinate extends Pointer<Void>
Coordinate extends NativeType
and use it asPointer<Coordinate>
Coordinate extends NativeType
and wrapsPointer<Void>
Coordinate extends NativeType
and type wrapsPointer<Void>
Coordinate extends Pointer<Void>
We define
Coordinate
as:which we can then use as objects:
or as C++ pointer:
Pros:
Cons:
Coordinate
and the Dart pointer to a Coordinate in a single type, which is confusing.offsetBy
andelementAt
returnPointer<Void>
and thus have to be wrapped -> this could be solved by having aStructMixin<Coordinate>
that makes them return the right type -> but this doesn't work for static methods and factoriesCoordinate extends NativeType
We define
Coordinate
as:Pros:
Coordinate
represents C++ struct,Pointer<Coordinate>
is a Dart wrapperoffsetBy
works properly out of the boxCons:
Coordinate extends NativeType and wraps Pointer<Void>
Pros:
Coordinate
represents the Dart wrapper with access to the fields, while the fieldPointer<Void>
provides the C++ view with pointer arithmeticCons:
Coordinate as type wrapper of Pointer<Void>
Suggested by @lrhn in #35782 (comment)
Pros & cons: same as
Coordinate
wrapsPointer<Void>
but without the overhead of an extra object.Extra con: Dart has no facilities to provide type wrappers, so we would have to program this by hand.
Looping in @lrhn, @mit-mit, and @sjindel-google
The text was updated successfully, but these errors were encountered: