LuaBridge icon indicating copy to clipboard operation
LuaBridge copied to clipboard

Additional trait to allow use of shared_ptr

Open progschj opened this issue 12 years ago • 5 comments

So, the way I understand it the issue with using shared_ptr as container is that there is a priory no way to cleanly create a shared_ptr from a raw ptr (since there could already be another shared_ptr we don't know about). Now in a class that has been accordingly "instrumented" with a method to obtain a shared_ptr (shared_from_this) this could be circumvented though if we could provide a method that tells LuaBridge how to obtain that shared_ptr. Essentially in addition to a trait that tells LuaBridge how to get a raw pointer form a container there could also be a trait for the inverse operation.

The concept is sketched in this commit: https://github.com/progschj/LuaBridge/commit/b0c51a91740011266b7281163fc92d26d3b04269 (I couldn't really figure out what the trait should be called so I just added a 2)

And here is an example how the usage could look like: https://gist.github.com/4570574

progschj avatar Jan 19 '13 03:01 progschj

Phew...I'm not really a fan of the requirement of having the friend declaration. The syntax for the assignments also looks quite strained. But there is certainly utility in supporting standard C++ smart pointer containers. I have to think about this.

vinniefalco avatar Jan 19 '13 03:01 vinniefalco

It doesn't have to be a friend declaration though. You could instead add a "get_shared" member function to the class that just forwards to shared_from_this and then call that in the trait.

I kinda like the approach because the second trait doesn't really cost anything if it is not used (doesn't break backwards compatibility or anything), while providing a hook to allow for this kind of workaround of the shared_ptr issue.

progschj avatar Jan 19 '13 03:01 progschj

Hmm...I guess I'm a little confused. What changes need to be made in LuaBridge?

OOPS...Okay I see it. StackHelper.

vinniefalco avatar Jan 19 '13 03:01 vinniefalco

This would need to be documented. And I would also want to clean up the names of the relevant class and members.

vinniefalco avatar Jan 19 '13 03:01 vinniefalco

Updated version with Manual entry: https://github.com/progschj/LuaBridge/commit/8d640ae7a20a444083b63bcb3912e80f9f09d75a

I'm still not 100% sure about the name, so if anyone has a better idea please share.

progschj avatar Jan 20 '13 12:01 progschj