|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Constructor/destructor tip |
|
![]() bozho Member since: 4/22/2005 |
||||
|
|
||||
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 Moderator - AngelCode Member since: 3/27/2000 From: Sao Paulo, Brazil |
||||
|
|
||||
| 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 |
||||
|
||||
![]() Dentoid Member since: 1/7/2002 From: Norrkoping, Sweden |
||||
|
|
||||
| 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 Member since: 4/22/2005 |
||||
|
|
||||
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 Member since: 1/7/2002 From: Norrkoping, Sweden |
||||
|
|
||||
| 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 Moderator - AngelCode Member since: 3/27/2000 From: Sao Paulo, Brazil |
||||
|
|
||||
| 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 |
||||
|
||||
![]() Dentoid Member since: 1/7/2002 From: Norrkoping, Sweden |
||||
|
|
||||
| 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 Member since: 1/7/2002 From: Norrkoping, Sweden |
||||
|
|
||||
| Never mind... Google helped me find it. :) |
||||
|
||||
![]() Deyja Member since: 3/5/2002 From: Stafford, VA, United States |
||||
|
|
||||
| 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 Moderator - AngelCode Member since: 3/27/2000 From: Sao Paulo, Brazil |
||||
|
|
||||
| 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 |
||||
|
||||
![]() Deyja Member since: 3/5/2002 From: Stafford, VA, United States |
||||
|
|
||||
| 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 Moderator - AngelCode Member since: 3/27/2000 From: Sao Paulo, Brazil |
||||
|
|
||||
| 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 - Tower - free puzzle game |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|