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

Custom SmartPointers and Angelscript ?

Started by Wracky Jul 7, 2009 at 3:11 PM 6 replies 4.9k views
Original Post
Wracky
Wracky
Hi there, and thanks for reading. I'm working on a little 3D engine of my own right now and I am looking for a scripting library for it. Well my first choice would be angelscript right now. At work we use LUA, but I think the syntax is hideous, and I like the features Angelscript offers in working with objects etc. and it being statically typed. I have one problem though, that I know how to address in lua, but not with angelscript: I manage my objects and resources with my own implementation of ref-counted smartpointers. ofcourse, I would like to use these smartpointers in scripts for obvious reasons. If I was to go for LUA, I would look into SWIG. Swig should be able to generate extra functions in the gluecode for lua, so that I don't need to change my C++ code, and the scripter doesn't notice the difference between a smartpointer and the actual object. (Read how it's done at http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_nn34 ) I know Angelscript won't let me bind the -> operator (is this even possible? Other languages seem to have the same restriction) but are there perhaps other ways so that I could make some bindings for my pointers in angelscript and roughly keep the same functionality? Or could I perhaps change angelscript so that they do work? Thanks in advance, Wracky
http://www.piko3d.net
WitchLord
WitchLord
Assuming your smart pointers work like those in the SWIG documentation, then you'll have to create and register wrapper functions the same way that SWIG does. You might even be able to use SWIG to generate the wrapper functions for you.

However, if your objects are already reference counted, e.g. like COM+ objects, then you don't need smart pointers at all, as AngelScript will take care of the reference counting for you.

Can you show us how you have implemented your smart pointers? It would be easier to give you suggestions if I know what to work with.
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
Wracky
Wracky
Thanks for the reply!

I have smartpointers like in the swig documentations. Much like the boost shared_ptr and weak_ptr.

My shared and weak pointers are castable, and you can construct shared pointers from weak pointers and vice versa...

I don't think pasting the complete implementation here (it's almost 200 lines) would help anything, so here's te interface for my SharedPtr. It inherrits from WeakPtr ....
I left out some extra functions required for the confused template parser in visual studio, but this is how it works in GCC.

template <class T>class WeakPtr : public SharedPtrParent {public:	WeakPtr();	/** Copy Constructor. Doubles as cast operator ;-) **/	WeakPtr( const WeakPtr< T >& ptr );	template <class C>	WeakPtr( const WeakPtr< C >& ptr );	~WeakPtr() {}	/** Operators **/	//@{	T& operator *() const;	T* operator ->() const;	T* getPtr() const;	//Releases this pointer.	void release();		template <class C>	WeakPtr<T>& operator = (const WeakPtr<C>& ptr );protected:	T* mPtr;};template <class T>class SharedPtr : public WeakPtr<T>{public:	/** Constructors & Destructor**/	//@{	/** Default **/	SharedPtr() {}	/** Create a Smart pointer for Pointer p **/	SharedPtr(T* p);	/** default copy constructor */	SharedPtr(const SharedPtr< T >& ptr );		/** Copy Constructor. Doubles as cast operator ;-) **/	template <class C>	SharedPtr(const WeakPtr< C >& ptr );	/** Destructor **/	~SharedPtr();	//@}		void release();		template <class C>	SharedPtr<T>& operator = (const WeakPtr<C>& ptr );	//@}protected:};


All refcounting is done with the SharedPtr.. not in the objects itself.
Now I could generate wrapper functions for this, but yeah I would like to have this done automatically in some way... because I would have a hell of an interface to forward. Let alone update everytime I change or updaten anything :-)

Thanks again for the help!
http://www.piko3d.net
WitchLord
WitchLord
Unfortunately you will indeed have to use wrappers to hide the fact that your classes are in smart pointers.

I suggest looking into a tool for automatically parsing your C++ code to generate the wrapper functions. SWIG might work for this, or perhaps the AngelScript C++ interface generator that was quickly implemented by the author of Rigs of Rods.

I have however added an item on the to-do list to investigate what might be done within AngelScript to allow the registration of the smart pointers directly. However I'm afraid it is not as simple as just registering the -> operator and then having AS call it as appropriate to give access to the true object's members. There is still the whole question about what to do when passing these objects to functions in the application or receiving them as return values.

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
Wracky
Wracky
Thank you very much! :)

Ok I edited my post. forget what I said before ;-)
I get the problem.... The problem is probably not registering a smartpointer as such, and using the -> operator on it...

The problem you describe is keeping users from knowing they are working with smartpointers... and having it look like they are working with objects.

In C++ this means calling the -> operator all the time. which can be done because users expect that from a pointer... but in script you will probably want foo.x() to be possible.... instead of foo->x()

Hmm.... The * operator helps, in the sense that it can return a T& where you need the actual objects to be passed... but how to hide this from the user without saying "Hey dude, this is a smartpointer, and you have to call -> and * all the time ;-)

Well just my two cents...
I had a big reply first, but I kinda missed the actual problem you were talking about :D

Thanks again!

[Edited by - Wracky on July 9, 2009 3:38:43 AM]
http://www.piko3d.net
Wracky
Wracky
I read the part on your website, that you wrote about smartpointers.
There's one line I don't really get.

You say: "Usually objects held in smart pointers are passed as normal refernces to application functions, but sometimes, it really is a reference to the smart pointer itself"

By this do you mean something like this?

void foo(Foo& f) {  f.x()}shared_ptr<Foo> p(new Foo());foo(p); 


'cause that doesn't compile.
If you want to use the smart pointer like this, you'd have to explicitly call it's operator *() first... Like this.

foo(*p);


If Angelscript would be aware of smart pointers, and can forward calls to ->() when needed, it should also be able to call operator *() when passing it as a T& parameter when needed....

But perhaps this is what you were saying ;-)

I'm just still running around with this idea in my head, since I would really like to work with angelscript ;-)

At work, we have a bindings creator for LUA, and the bossman told me it shouldn't be too much work to have that work for Angelscript, and have it generate wrapper functions for my smart pointers, so perhaps I'll look into that!

Thanks for putting this on your site!
Let me know if I can help out in any way.
http://www.piko3d.net
WitchLord
WitchLord
I meant something like this:

void foo1(Foo &f) {  f.x();}void foo2(shared_ptr<Foo> &f) {  f.x();}


If you want to bind these two functions, then you'll need to register both the Foo type and the shared_prt type, or AngelScript needs to know how to transform the shared_ptr to a Foo and the application needs to tell AngelScript when that should be done.

I'm just trying to think of the complications with supporting smart pointers before I start adding that support, so that I don't have to stop halfways due to unforeseen obstacles.



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
Wracky
Wracky
OK, great.
I think it would be only natural to have to register both the pointer and the type. This would be something the application programmer does, and the scripter doesn't notice... so I think that could work...

But hey, I think it's great that you are actually thinking about SUPPORTING smart pointers (I do understand that this is special case to handle) so thanks so much for that ;-)

I think I'll just go with angelscript, and make forward functions for now.
Direct calls, static types, and script objects, are just to good walk away from :D

Thanks again.
http://www.piko3d.net

Topic Locked

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

Sign in to reply to this topic.