For importing external C++ libraries, I consider class extensions
to be the most important feature. Especially when working with Strings,
because many libs use its own strings, not only standard C++ strings
as Ascii or WChar.
Would be nice if this very specific and important feature would get some more
attention and find its way into v1.0 of MX2. It would make importing external
C++ libs much better and easier, and users of libs could just use MX2 strings everywhere.
Full support for extension methods will not be in V1.0, but there is actually limited extern-only extension method support in already. The only drawback is the method needs to be written in native-code, but this is sort of the point – it’s for methods that don’t translate easily from native code to mx2.
You can use it like this:
Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'----- monkey2 file -----
#Import"glue.cpp"
Extern
ClassCExtendsVoid
MethodM(x:Int,y:Int)Extension="C_M"
End
Public
FunctionMain()
Localc:=NewC
c.M(10,20)'passes 'c' as first param to C_M
End
'----- glue.cpp -----
//includeactual classwe're wrapping
#include"c.h"
//Note:first param is'this'...
voidC_M(C*c,intx,inty){
//can invokecmethods here ifnecessary...eg:
c->doSomething(x,y);
}
There used to be an example of this in tests/monkey/externs but it appears to have gone…