Skip to content
Merged
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
31 changes: 17 additions & 14 deletions assets/content/cookbook/Other/hxcpp-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> bar) {}
function foo(bar:cpp.RawPointer<Int>) {}
```

will produce the following C++ function
Expand All @@ -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<cpp.UInt8> bar;
var bar:cpp.RawPointer<cpp.UInt8>;
}
```

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 {
Expand All @@ -47,18 +51,18 @@ function myFunc(foo:cpp.RawPointer<Foo>) {
}
```

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<cpp.RawPointer<int>> // Will generate code which gives C++ compiler errors.
Array<cpp.RawPointer<Int>> // Will generate code which gives C++ compiler errors.
```

## cpp.Pointer

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<T>` struct.

```haxe
function foo(cpp.Pointer<int> p) {}
function foo(p:cpp.Pointer<Int>) {}
```

```cpp
Expand All @@ -67,7 +71,7 @@ void foo(::cpp::Pointer<int> 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) {}
Expand All @@ -76,10 +80,10 @@ void foo(int* v) {}
with this haxe function

```haxe
void foo(v:cpp.Pointer<Int>) {}
function foo(v:cpp.Pointer<Int>) {}
```

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.

Expand All @@ -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<int> v) {}
```haxe
function haxe_foo(v:cpp.Pointer<Int>) {}

function main() {
cpp.Callable.fromStaticFunction(haxe_foo);
}

```

That `fromStaticFunction` call will generate a function pointer with the signature of `void(*)(::cpp::Pointer<int>)` 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 {
Expand Down