hxcpp, externs, array access

112 views
Skip to first unread message

Dmitry Hryppa

unread,
Sep 11, 2017, 7:58:52 AM9/11/17
to Haxe
Hey ;)
Can someone point me to the right solution, please?
I'm writing externs for c++ library and can't find a proper way for access to the c++ class fields as to array. 
For example, in c++ code I have fields like this:

C_STRUCT aiVector3D* mVertices;
//or
unsigned int* mSomeValues;
//or
C_STRUCT aiBone** mBones
//or even
C_STRUCT aiVector3D* mTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];

In c++ I can use them as this:
mVertices[i], mSomeValue[i], mBones[i]
//or
mTextureCoords[0][i];

So, the question is how to write externs for fields like that?

Firstly, I tried haxe Array and Vector. Both don't give me any result, because generated c++ in that case will look like:
//c++ side:
C_STRUCT aiVector3D* mVertices;

//haxe side:
@:native("mVertices") public var vertices:Array<Pointer<AiVector3D>>;//or Vector

//generated c++:
aiMesh->ptr->mVertices->__get(i)->ptr->x;  //And this won't compile, because it should be aiMesh->ptr->mVertices[i].x; and mVertices does not have __get() function;


So... My current solution is pretty weird:
//c++ side:
C_STRUCT aiVector3D* mVertices;

//haxe side:
@:native("mVertices") public var vertices:AiMeshVertices;

abstract AiMeshVertices(Pointer<AiVector3D>)
{
    @:arrayAccess public function get(index:Int):AiVector3D
    {
        return untyped __cpp__("this1[index]");
    }
}

@:native('aiVector3D')
@:structAccess
extern class AiVector3D
{
    public var x:Float;
    public var y:Float;
    public var z:Float;
}

And it works, but I think it is not a proper way. Any suggestions? 
Thank you :)


Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Valentin

unread,
Sep 12, 2017, 12:16:29 AM9/12/17
to haxe...@googlegroups.com

Hey,

you should use cpp.RawPointer instead, it represents a real cpp pointer, while Pointer is a hxcpp class that wrap one.

With it you can do array access that'll be correctly generated.

`@:native("mVertices") public var vertices:RawPointer<AiVector3D>;`

`vertices[0]` => `mVertices[0]`.

--
Valentin

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.


Dmitry Hryppa

unread,
Sep 12, 2017, 7:35:23 AM9/12/17
to Haxe
Ah, that was so simple :)
Thank you! 

вторник, 12 сентября 2017 г., 7:16:29 UTC+3 пользователь Valentin Lemière написал:
Reply all
Reply to author
Forward
0 new messages