Cpp externs and & syntax

149 views
Skip to first unread message

Vincent Blanchet

unread,
Apr 13, 2016, 5:47:11 AM4/13/16
to Haxe
Hello,

I'm trying to make some cpp externs for SmartFox Server client api. I'm not really familiar with cpp
and I have some troubles with types from haxe to cpp.

For instance : What is the matching haxe extern declaration in this function ? 

void ReadBytes(long int offset, long int count, vector<unsigned char>&);

I don't know what to do with the "&" param without var name.
Moreover if you have some documentation regarding cpp extern, it could be a great help.
I'm currently using these:




Thank you for your help
-- 
Vincent BLANCHET,
__________________________________
Développeur,
Skype : vincent.blanchet.info

Hugh

unread,
Apr 14, 2016, 12:40:05 AM4/14/16
to Haxe
It depends on how you are storing your vector<unsigned char> in hxcpp.
Generally, you do not need to tell haxe that it is a reference - just as long as haxe does not decide to make a copy of the variable you pass in at a bad time.

I'm guessing you would actually store your std::vector in a cpp.Reference, which is actually a pointer.  In this case, you would type it as the same as your cpp.Reference.
It can be instructive to look at the generated code, and you should see that it is passing the cpp.Reference type directly, and in turn this reference should "autocast" to the actual std::vector&.
Good thing about cpp.Reference is that copying is ok, since it is really a pointer.

The key thing to remember is that the exerns are really to get the haxe compiler to type-check your code, not to literally represent the proto-types, although it is easier to be consistent, the closer you get to actual types.

Hugh

Vincent Blanchet

unread,
Apr 14, 2016, 9:05:30 AM4/14/16
to Haxe
Thank you Hugh!
I will try to figure out how it works.
Maybe I will ask other questions in the next days :)

--
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.

Vincent Blanchet

unread,
Apr 18, 2016, 5:37:13 AM4/18/16
to Haxe
Hello,

I have a new issue with the smartfox library. It heavily uses boost::shared_ptr as pointer, no simple Pointer, like in these functions from ByteArray.h file:

ByteArray(boost::shared_ptr<vector<unsigned char> > buf);
boost::shared_ptr<vector<unsigned char> > Bytes()

How can I handle this boost::shared_ptr type?


Garanti sans virus. www.avast.com

Vincent Blanchet

unread,
Apr 18, 2016, 10:23:00 AM4/18/16
to Haxe
In fact I wonder how can I generate code like :

boost::shared_ptr<vector<unsigned char>> mySharedPtr (vector<unsigned char> tab(4,'a'));

I tried this:

Shared_ptr.hx :

@:include('linc_sfs2x.h')
@:build(linc.Linc.touch())
@:build(linc.Linc.xml('sfs2x', '..'))
@:native('boost::shared_ptr')
extern class Shared_ptr<T>
{
@:native('shared_ptr') static function create<T>(a:T):Shared_ptr<T>;
}



Main.hx :

@:headerCode("#undef RegisterClass")
@:headerCode("#include <windows.h>")
class Main 
{

static function main() 
{
var l = 4;
var v:Vector<cpp.UInt8> = new Vector<cpp.UInt8>(l);
for (i in 0...l)
v.set(i, Std.random(255));
var sp:Shared_ptr<Vector<cpp.UInt8>> = Shared_ptr.create(v);
}

}


but the genrated code look like this :

Array< ::cpp::UInt8 > tmp1 = v;
boost::shared_ptr tmp2 = shared_ptr(tmp1);
boost::shared_ptr sp = tmp2;

and I have some compiler errors :

 error C2955: 'boost::shared_ptr'  use of class template requires template argument list

you can check boost/shared_ptr.hpp and boost/smart_ptr/shared_ptr.hpp if you need the cpp code

Cheers

Hugh

unread,
Apr 19, 2016, 12:30:23 AM4/19/16
to Haxe
There is currently no good way I can think of to get the type-param from haxe to the variable type name.
The problem is that the @:native('boost::shared_ptr') meta completely defines the name - there is no room for a parameter.  The cpp Array and Pointer classes have special code to handle this.
The only solution I can think of is to have one of these per shared-pointer type with the parameter name wired into the @:native meta.  You may be able to use a macro to generate these for you.

You might also be courting trouble mixing shared-pointers with hxcpp Gc.  It should be ok with local variables, but if you store these in member variables, you will need to explicitly null them out at some stage.

Hugh

Vincent Blanchet

unread,
Apr 19, 2016, 11:35:42 AM4/19/16
to Haxe
Thank you again Hugh

I'm trying to use macros but I am such a newbie. I tried to use the class reification exemple to make it works but
it's a fail I miss a lot of knowledge in macros, code I wrote :
 
package macros;
class TypedSharedPtr
{
public static function generate(typeParam:String){
var r = ~/[<.>]/g;
var stringName:String = "SharedPtr"+r.replace(typeParam, "_");
var c = macro class $stringName {

};
c.isExtern = true;
c.pack = ["boost"];
c.meta = [{name:":native", pos:haxe.macro.PositionTools.here(), params:[macro $i{"boost::shared_ptr"+typeParam}]}];

haxe.macro.Context.defineType(c);
return macro c;
}
}

Main.hx
class Main 
{

static function main() 
{
var c = macro macros.TypedSharedPtr.generate("<vector<cpp.UInt8>>");
var test:boost.Shared_ptr_vector_cpp_UInt8__ = new c();
}

}

it doesn't compile i tried other things but always a fail ( use it in @:build has break with "no static function main in Main class")

Does someone has an exemple of fully macro generated class and use case?
Reply all
Reply to author
Forward
0 new messages