Skip to main content
GameDev.net gamedev.net
🔒 Locked

Constructor/destructor tip

Started by bozho Apr 27, 2005 at 5:26 AM 10 replies 4.7k views
Original Post
bozho
bozho
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
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel
Dentoid
Dentoid
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.)
bozho
bozho
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...
Dentoid
Dentoid
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.
WitchLord
WitchLord
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 ∈)", asMETHODPR(asCScriptString, operator+=, (const asCScriptString&), asCScriptString&), asCALL_THISCALL); assert( r >= 0 );r = engine->RegisterGlobalBehaviour(asBEHAVE_EQUAL,       "bool f(const string ∈, const string ∈)",    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 - <a href="http://www.angelcode.com/tower" rel
Dentoid
Dentoid
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. :)
Dentoid
Dentoid
Never mind... Google helped me find it. :)
Deyja
Deyja
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.
WitchLord
WitchLord
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 - <a href="http://www.angelcode.com/tower" rel
Deyja
Deyja
I imagine it does not work at all with 2.x. Has the equivlant functionality been supplied somewhere else? I don't actually use it myself. :D
WitchLord
WitchLord
Actually, it still works. It is part of my regression testing, it's just not an official add on anymore. :)

I'd like an official add on for overriding the AS array to have the same functionality as the built-in arrays, which means support object handles and have the methods length() and resize(). I may write this add on myself someday, and if I do I will of course base it on your std_vector code.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.