You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: assets/content/cookbook/Other/hxcpp-pointers.md
+17-14Lines changed: 17 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,18 @@
2
2
3
3
# Hxcpp Pointers
4
4
5
-
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`.
5
+
This page covers the differences and use cases of the three main pointer types:
6
+
7
+
-`cpp.ConstRawPointer` and `cpp.RawPointer`
8
+
-`cpp.ConstPointer` and `cpp.Pointer`
9
+
-`cpp.ConstStar` and `cpp.Star`.
6
10
7
11
## cpp.RawPointer
8
12
9
13
As the name says, just bog standard c pointers. e.g. the following haxe function
10
14
11
15
```haxe
12
-
function foo(cpp.RawPointer<int> bar) {}
16
+
function foo(bar:cpp.RawPointer<Int>) {}
13
17
```
14
18
15
19
will produce the following C++ function
@@ -30,11 +34,11 @@ could be externed with the following haxe class and the `foo` variable could be
30
34
31
35
```haxe
32
36
extern class Foo {
33
-
var cpp.RawPointer<cpp.UInt8> bar;
37
+
var bar:cpp.RawPointer<cpp.UInt8>;
34
38
}
35
39
```
36
40
37
-
The downside with these is that its not ergonomic from a haxe pov to represent and consume pointers to objects.
41
+
The downside with these is that its not ergonomic in haxe code to represent and consume pointers to objects.
38
42
39
43
```haxe
40
44
extern class Foo {
@@ -47,18 +51,18 @@ function myFunc(foo:cpp.RawPointer<Foo>) {
47
51
}
48
52
```
49
53
50
-
They also cannot be used in any situations where Dynamic is expected (explicitely or implicitly).
54
+
They also cannot be used in any situations where Dynamic is expected (explicitly or implicitly).
51
55
52
56
```haxe
53
-
Array<cpp.RawPointer<int>> // Will generate code which gives C++ compiler errors.
57
+
Array<cpp.RawPointer<Int>> // Will generate code which gives C++ compiler errors.
54
58
```
55
59
56
60
## cpp.Pointer
57
61
58
62
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.
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`.
69
73
70
-
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
74
+
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
71
75
72
76
```cpp
73
77
void foo(int* v) {}
@@ -76,10 +80,10 @@ void foo(int* v) {}
76
80
with this haxe function
77
81
78
82
```haxe
79
-
void foo(v:cpp.Pointer<Int>) {}
83
+
function foo(v:cpp.Pointer<Int>) {}
80
84
```
81
85
82
-
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.
86
+
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?
83
87
84
88
Function signatures.
85
89
@@ -93,20 +97,19 @@ void foo(bar func) {}
93
97
94
98
you could not use the following haxe function to generate a function pointer to pass into it.
95
99
96
-
```
97
-
function haxe_foo(cpp.Pointer<int> v) {}
100
+
```haxe
101
+
function haxe_foo(v:cpp.Pointer<Int>) {}
98
102
99
103
function main() {
100
104
cpp.Callable.fromStaticFunction(haxe_foo);
101
105
}
102
-
103
106
```
104
107
105
108
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`.
106
109
107
110
## cpp.Star
108
111
109
-
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++
112
+
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++
0 commit comments