Home » Community » Forums » AngelCode » Constructor/destructor tip
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Constructor/destructor tip
Post New Topic  Post Reply 
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

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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


 User Rating: 1702   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.)

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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...

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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


 User Rating: 1702   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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. :)

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Never mind... Google helped me find it. :)

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1547   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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


 User Rating: 1702   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1547   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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


 User Rating: 1702   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: