std::function to asSFuncPtr

Started by
1 comment, last by B_old 11 years, 6 months ago
I'm wondering if there is a practicable way of converting a std::function to an asSFuncPtr. I think it would be nice to have an interface/wrapper to the script engine that can be used without including angelscript.h. Can this or something similar be done?
Advertisement
It's not possible to directly assign a std::function to an asSFuncPtr; however, it's possible to get that functionality with a little work. First create a class that contains a map of function IDs to std::function<void (asIScriptGeneric *)> and set a pointer to that class as the script engine's user data. Then every time you want to register a std::function, instead register a generic function that looks up the function ID in the map and calls the appropriate std::function. The registration function returns the function ID for the function you just registered so then stick a std::function<void (asIScriptGeneric *)> in the map with the appropriate ID. For the basic idea of how to convert an arbitrary function object to one using the script generic calling convention look at the autowrapper add-on.
Ah, that is a good idea. I would prefer to get rid of the indirection at compile time, but if this is not feasible without including angescript.h I can always come back to your suggestion.
I could also register functions that come from inside the framework with the AS macros and use another interface for stuff that might be registered by the application.

This topic is closed to new replies.

Advertisement