Hi, I'm mapping a C library interface to angelscript, and have mostly managed to make it work nicely.
Problem:
One problem I have is how to handle non-refcounted pointer parameters. I haven't find a good non-kludgy solution to it so far. Consider the following function declaration:
void foo(Bar* fp);
I could make a wrapper function like so:
void fooWrapper(Bar& f)
{
foo(&f);
}
...but doing that for a lot of functions is not very nice. One typical case of this is C-style objects with large-ish function parameters:
void setFoo(FooObject* fo, BarValue const* bv);
BarValue const* bv getFoo(FooObject* fo);
The setter here copies values inside bv, and getter returns a pointer to the actual member.
Main question:
Is there a pattern to map cases like this into angelscript?
I can't make BarValue a value type because then I can't call pointer-type functions, and I can't make it a reference type because there's no refcounting included. I can't make it a NOCOUNT reference either because BarValues need to be created inside the script to pass them as parameters.
I've tried to make quite a few generic template-based solutions to this, transparently generating wrapper functions as above, but AS's function system doesn't seem to like them. I've tried creating separate refcounters but they end up being as verbose solutions as the wrapper functions.
Proposed solution:
What would create a viable solution would be std::function support:
- Lambda expressions would make the wrapper function solution a lot cleaner (BarValue could be mapped as a value type)
- Possibility of using a simple generic templated external reference counting mechanism (BarValue could be mapped as a reference type)
For example:
engine->RegisterObjectMethod("FooObject", "void set_bar(const BarValue &in)", asSTDFUNCTION([](FooObject* o, BarValue const& v){ setFoo(o, &v); }), asCALL_CDECL_OBJFIRST);
engine->RegisterObjectMethod("FooObject", "BarValue get_bar()", asSTDFUNCTION([](FooObject* o){ return getFoo(o); }), asCALL_CDECL_OBJFIRST);
Would this be possible to implement? Am I completely off the mark here? Is mapping interfaces like this possible without wrapper functions and/or types?