Advertisement

Registering a C++ object constructor From a global variadic template function

Started by June 26, 2018 05:19 AM
5 comments, last by WitchLord 6 years, 4 months ago

So this probably has been done, but I'm wondering if there was a better way to do this. I wrote a global templated function that allows you to use it as the pointing function for any class's constructor reference. Looking at the stringD code I saw there are 2 ways to go about doing this: Usinging asIScriptGeneric, and Calling it natively using a typed member object pointer. 

Below is some code that does both based on the as_max_portiblity preprocessor define that I created (i assume there is one natively in the engine but I didn't see it):


asERRORCHECK(r) assert(r < 0) //I have a much more complex error message system, so this is just to cover for that

#define as_max_portiblity 0
#if as_max_portiblity
template<class C, typename ...Args> void __constructor__(asIScriptGeneric *gen)
{
	int i = 0, max = sizeof ...(Args);
	new (gen->GetObject()) C(*static_cast<Args *>(gen->GetAddressOfArg(*std::make_unique<int>((max-(++i)))))...);
}
#else
template<class C, typename ...Args> void __constructor__(C *gen, Args... args)
{

	new (gen) C(Args(args)...);
}
#endif

Once you have defined the type of function you want to use then you need to register this inside of a function like this:


void registermyobjects(asIScriptEngine *asEngine)
{
int r;
...
//we have an object registered under the name vec2
...
#if as_max_portiblity
	r = asEngine->RegisterObjectBehaviour("vec2", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(__constructor__<vec2>), asCALL_GENERIC); asERRORCHECK(r);
	r = asEngine->RegisterObjectBehaviour("vec2", asBEHAVE_CONSTRUCT, "void f(float, float)", asFUNCTION((__constructor__<vec2, float, float>)), asCALL_GENERIC); asERRORCHECK(r);
#else
	r = asEngine->RegisterObjectBehaviour("vec2", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONPR((__constructor__<vec2>), (vec2 *), void), asCALL_CDECL_OBJFIRST); asERRORCHECK(r);
	r = asEngine->RegisterObjectBehaviour("vec2", asBEHAVE_CONSTRUCT, "void f(float, float)", asFUNCTIONPR((__constructor__<vec2, float,float>),(vec2 *,float, float), void), asCALL_CDECL_OBJFIRST); asERRORCHECK(r);
#endif
...
}

This should be fairly self explanatory but i have a class vec2 which can take either (void) or (float,float) as its constructor types. So this should in theory be able to take anytype and pass it into the __constructor__() and it will create an object for you. Let me know what you think.

Im curious if anyone has done something like this, or if any one sees a potential problem with this code. In theory I could do the same thing for operator types and maybe even factory functions.

chasester

@edit 1: Made a mistake where the new int(++i) was passing maxargs ... 0 rather than 0 .... maxargs. Fixed by reversing the numbers with a formula (((++i) - maxargs)*-1).

@edit 2: Made max - (++i) so that we dont have to multiply by -1. Also replace *new int() with *std::make_unique<int>() so that we dont run into a memory leak.

I don't see any direct risks with this. It ought to work just fine. 

The autowrapper add-on has some similarities, though it doesn't use the template variadic arguments (perhaps you'd like to help out making improvements to this code? *hint* ;)). 

 

 

 

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

well i found a few improvements when talking to my brother which i will update but i was concerned with:


(*new int(max - (i++)))

creating a  memory leak so im looking into using an auto_pointer to fix this issue. Ill look at the auto wrapper. I haven't looked at much of the add on code, most of it looks very tedious so i was trying to find a more portable solution since i have a lare number of classes I want to add to the library.

Also i've not seen a good geom libray written in the addon (like vec, matrix etc) is there one? I feel like every time I look there are more add-ons which i've never seen for some reason (im probably just blind).

I appreciate your quick response as always

chasester

@edit

I dont really understand what this header does, maybe seeing a short example would help, but it looks very confusing for what I'm presuming is something very simple. A lot of copy and pasted code with very little changes. Or maybe this is just way over my head.

I didn't write the autowrapper.h myself. That was a contribution from SiCrane. I'm honestly not very good at meta programming myself and tend to use it only when absolutely necessary.

The autowrapper.h itself is actually generated using a small program http://svn.code.sf.net/p/angelscript/code/trunk/sdk/add_on/autowrapper/generator/. It was created before C++ had support for variadic template arguments, so that's why you see a lot of similar code to handle different amount of arguments.

 

The code itself automatically builds the generic calling convention wrapper from ordinary functions/class methods. The macros defined in autowrapper.h can be used as replacement for the asFUNCTION, asMETHOD, asFUNCTIONPR, and asMETHODPR macros. The documention on how to use this is available here: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_addon_autowrap.html

 

As for any good geom library. I can't say I've seen any. But why not take a look at some of the open source engines that use AngelScript. Urho3D comes to my mind as a probable good source, but there are probably others too. You can also take a look at my own prototype engine http://www.angelcode.com/projectx/ but it is far from an exemplary source.

I have no plans to include a standard geom library as an add-on in the SDK, but if someone were to create an open source library for it I would be happy to link to it.

 

 

 

 

 

 

 

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

@Geom library.

I have a library already. I was wondering if you want a version of it with it added as a script object in angelscript. 

@Wrapper

I was more asking what exactly would you want in the wrapper. I have macros for globalFunctions globalVaribles, objectMethods, objectProperties, asFuncitonto/C++Template, Operators, etc If you give me some build specs I can make something since im already doing something very simpler.

@Generic

How useful is generic, is it something that people have to use often. I mostly ask for my person project, I want to know if I should always include a generic form for all of the defined functions?

 

@Inline Definitions

Technically there is a way of defining global functions and varibles out side of a function (I assume im using the wrong name for this) using a macro. 


#define asCOMMAND(ret, name, prefab) static bool __DUMMY__##ret##name = \
  addcommand(str( str(#ret) + str(#name) + str(#prefab)), asFUNCTIONPR(ret, name, prefab));

void addcommand(str def, void *(f)(void)); 
//This would basically store the definition and the string name so that after all of the objects
//have been defined by script##name##.h it would run adding all of the underlying globals.

int hello(int a) {cout << "hello: " << a;} asCOMMAND(int, hello, (int));

Basically the goal would be to make it easier to define things in angel script by letting you define them inline where the function definition is and then have them automatically added later.

chasester

Quote

@Geom library.

I have a library already. I was wondering if you want a version of it with it added as a script object in angelscript

If you have a library and are willing to share it with the community I would suggest to create an open source project for it, e.g. like the AngelScript Add-on Template Library maintained by Sami Vuorela. I would be happy to link to your library from my site so others can easily find it.

Quote

@Wrapper

I was more asking what exactly would you want in the wrapper. I have macros for globalFunctions globalVaribles, objectMethods, objectProperties, asFuncitonto/C++Template, Operators, etc If you give me some build specs I can make something since im already doing something very simpler.

What I specifically think the autowrapper add-on could use is a refactor to use the new C++ template variadic feature, to avoid all the repeated code. 

Quote

@Generic

How useful is generic, is it something that people have to use often. I mostly ask for my person project, I want to know if I should always include a generic form for all of the defined functions?

The generic calling convetion is a fallback needed on the platforms for which AngelScript currently doesn't support native calling conventions, currently the most important platforms like this are the 64bit iOS and Android platforms, but also any less popular platforms that I may never have heard about would obviously not be supported with native calling conventions.

If you're not going to target any platform that doesn't have support for native calling conventions, then you do not need to implement the generic calling convention. With the help of the autowrappers it is anyway quite simple to add this support afterwards when needed, so you don't need to spend effort on this at the beginning.

Quote

@Inline Definitions

Technically there is a way of defining global functions and varibles out side of a function (I assume im using the wrong name for this) using a macro. 

I believe it would be possible. I don't remember ever doing something like this myself, but I can imagine you could write the macro to instantiate a class that would as part of the constructor add the command to some global singleton instance. The global singleton instance can later be accessed to get the full list of commands.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement