diff --git a/assets/content/cookbook/Other/hxcpp-pointers.md b/assets/content/cookbook/Other/hxcpp-pointers.md index 89bfb0cc..25c90a62 100644 --- a/assets/content/cookbook/Other/hxcpp-pointers.md +++ b/assets/content/cookbook/Other/hxcpp-pointers.md @@ -2,14 +2,18 @@ # Hxcpp Pointers -This page covers the differences and use cases of the three main pointer types; `cpp.ConstRawPointer` and `cpp.RawPointer`, `cpp.ConstPointer` and `cpp.Pointer`, and `cpp.Star`. +This page covers the differences and use cases of the three main pointer types: + +- `cpp.ConstRawPointer` and `cpp.RawPointer` +- `cpp.ConstPointer` and `cpp.Pointer` +- `cpp.ConstStar` and `cpp.Star`. ## cpp.RawPointer As the name says, just bog standard c pointers. e.g. the following haxe function ```haxe -function foo(cpp.RawPointer bar) {} +function foo(bar:cpp.RawPointer) {} ``` will produce the following C++ function @@ -30,11 +34,11 @@ could be externed with the following haxe class and the `foo` variable could be ```haxe extern class Foo { - var cpp.RawPointer bar; + var bar:cpp.RawPointer; } ``` -The downside with these is that its not ergonomic from a haxe pov to represent and consume pointers to objects. +The downside with these is that its not ergonomic in haxe code to represent and consume pointers to objects. ```haxe extern class Foo { @@ -47,10 +51,10 @@ function myFunc(foo:cpp.RawPointer) { } ``` -They also cannot be used in any situations where Dynamic is expected (explicitely or implicitly). +They also cannot be used in any situations where Dynamic is expected (explicitly or implicitly). ```haxe -Array> // Will generate code which gives C++ compiler errors. +Array> // Will generate code which gives C++ compiler errors. ``` ## cpp.Pointer @@ -58,7 +62,7 @@ Array> // Will generate code which gives C++ compiler errors Similar to the above, but with some key differences. The code they generate does not map directly to pointers, but instead to a special `::cpp::Pointer` struct. ```haxe -function foo(cpp.Pointer p) {} +function foo(p:cpp.Pointer) {} ``` ```cpp @@ -67,7 +71,7 @@ void foo(::cpp::Pointer p) {} This type lives on the stack so does not contribute to GC pressure, it also is compatible with Dynamic, so you can pass `cpp.Pointer` types into dynamic places and use them with generic arguments. This type also contains loads of convenience functions for reinterpreting the pointer, arithmetic, conversions to haxe arrays and vectors, etc, etc. There are also member fields for accessing the `cpp.Pointer` as a `cpp.RawPointer` or `cpp.Star`. -It also retains the array access that `cpp.RawPointer` does. On top of this the `::cpp::Pointer` type has implicit to and from conversions for the underlying pointer, which means you can extern the following function +It also retains the array access that `cpp.RawPointer` supports. On top of this the `::cpp::Pointer` type has implicit to and from conversions for the underlying pointer, which means you can extern the following function ```cpp void foo(int* v) {} @@ -76,10 +80,10 @@ void foo(int* v) {} with this haxe function ```haxe -void foo(v:cpp.Pointer) {} +function foo(v:cpp.Pointer) {} ``` -You don't need to use `cpp.RawPointer` here, you can use `cpp.Pointer` and all the convenience it provides. So why would you ever want to use `cpp.RawPointer` if `cpp.Pointer` does everything it does and is compatible with more of haxe's type system. +You don't need to use `cpp.RawPointer` here, you can use `cpp.Pointer` and all the convenience it provides. So why would you ever want to use `cpp.RawPointer` if `cpp.Pointer` does everything it does and is compatible with more of haxe's type system? Function signatures. @@ -93,20 +97,19 @@ void foo(bar func) {} you could not use the following haxe function to generate a function pointer to pass into it. -``` -function haxe_foo(cpp.Pointer v) {} +```haxe +function haxe_foo(v:cpp.Pointer) {} function main() { cpp.Callable.fromStaticFunction(haxe_foo); } - ``` That `fromStaticFunction` call will generate a function pointer with the signature of `void(*)(::cpp::Pointer)` which is not compatible with the function pointer `bar`. ## cpp.Star -This is the last pointer types and like `cpp.RawPointer` it generates raw C pointers in the output code, the key difference is that this type does not support array access and will auto de-reference when accessing the underlying data. This means it's ideal for representing pointers to objects. E.g. the following C++ +This is the last pointer type and like `cpp.RawPointer` it generates raw C pointers in the output code. The key difference is that this type does not support array access and will auto de-reference when accessing the underlying data. This means it's ideal for representing pointers to objects. E.g. the following C++ ```c++ struct Bar {