Constructor/destructor tip

Started by
10 comments, last by WitchLord 18 years, 11 months ago
Here's a nice way for declaring ctor/dtor functions if you need them for lots of classes (tested on VS.NET 2003 and VS6):

template<typename T>
void Constructor(T* o) {
   new(o) T();
}

template<typename T>
void Destructor(T& o) {
   o.~T();
}
Then, register them as your class' ctor/dtor:

pEngine->RegisterObjectBehaviour(
           "MyClass", 
           asBEHAVE_CONSTRUCT, 
           "void Constructor()", 
           asFUNCTIONPR(Constructor, (MyClass*), void), 
           asCALL_CDECL_OBJLAST);

pEngine->RegisterObjectBehaviour(
           "MyClass", 
           asBEHAVE_DESTRUCT, 
           "void Destructor()", 
           asFUNCTIONPR(Destructor, (MyClass&), void), 
           asCALL_CDECL_OBJLAST);
Have fun
Advertisement
Excellent tip! Thanks for sharing it with us.

I think I'll even include it in the documentation.

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

Yay, that looks very useful.

I tried to do something similar for overloaded operators, but I couldn't get the casts etc. right. Any ideas on how to do that? (Maybe it's possible to use the operator methods directly, but I tried making a template similar to your constructor templates, without success.)
If I understood you correctly... You'd like to use template operator functions:
class MyClass {....};template<typename T>MyClass myclass_add(const MyClass& left, T& right);


I'm not sure how useful that would be, since you'd have to do template specialization for different types, and essentially that would be no different from writing overloaded operators...
Well, from the C++ side it would be useless. I was just curious though if it could help out registering operators to angelscript. I haven't managed to register an operator method directly (if this is possible, I'd be glad to do that), so I was thinking maybe it would be possible to wrap it using a template. Currently I manually wrap the operators with loads of functions. (Got some macro magic to keep the manual labour down a bit. :)

Don't know if you understand what I mean. It's hard to explain.
An operator can only be registered if it has been implemented manually by you. Then you would register just like any other function/method.

Here are some examples from scriptstring.cpp (available in the add_on folder)

r = engine->RegisterObjectBehaviour("string", asBEHAVE_ADD_ASSIGN, "string &f(const string &in)", asMETHODPR(asCScriptString, operator+=, (const asCScriptString&), asCScriptString&), asCALL_THISCALL); assert( r >= 0 );r = engine->RegisterGlobalBehaviour(asBEHAVE_EQUAL,       "bool f(const string &in, const string &in)",    asFUNCTIONPR(operator==, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 );


AngelScript currently doesn't accept dual operators registered as object behaviours, except for the assignment operators. All other dual operators must be registered as global behaviours, which means that they must be implemented as global functions (or at least static member functions).

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

Ah, that was exactly what I was looking for. I'll try that out whenever I've got time.

Btw, I don't recognize the asFUNCTIONPR macro you're using. I might have missed that in the docs though. What does it do?

EDIT: What does it do different from asFUNCTION, that is. :)
Never mind... Google helped me find it. :)
Basic operators could be auto-wrapped using a simple template. I did something similiar in the std_vector addon, though for methods not operators. The code was available unchanged with the 1.10.1d release. I don't know if it's still in 2.x.
Your std_vector is still available in the test application, though I have removed it as official add on.

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