Jump to content
  • Advertisement
Sign in to follow this  

smart pointer for reference counted objects

This topic is 3086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wanted to implement some small smart pointer for angelscript, especially when passing handles from angelscript to c/c++ (so I can spare myself all the AddRef/Release calls). When I register a function that receives a handle, then it is passed by pointer:

void some_function(Foo* foo);

Adding a smart pointer means adding another line like this:

void some_function(Foo* foo)
smart_ptr<Foo> f(foo);
// only use 'f' from now on

Though it would be easier if I could write my function like this:

void some_function(smart_ptr<Foo> foo);

I have little knowledge of assembler, but I know that pointer arguments are passed on the stack (under msvc and cdecl at least). Is the same true for small objects, like a smart_ptr would be?
I imagine an implementation like:

template <typename T>
class as_pointer

as_pointer(T* foo) : foo(foo) {}
as_pointer(as_pointer<T> that) : foo(that.foo) { if(foo) foo->AddRef(); }
~as_pointer() { if(foo) foo->Release(); }


T* foo;

It would have the same size as a pointer (given a padding of 4 bytes).
Do you think this can work under most compilers that angelscript already supports with native calling conventions?

Share this post

Link to post
Share on other sites
Objects that have a constructor, destructor, and/or assignment operator are commonly passed by reference, regardless of the size of the objects.

Your suggestion will likely work for some compilers, but not for others. Only testing will let you determine which it work on, though I suspect it will work on MSVC but not on GNUC.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!