sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

[v4] Container detection through a smart pointer

Open s13n opened this issue 4 years ago • 1 comments
trafficstars

Container detection for usertypes seems to work only when passing the container directly to sol, rather than a smart pointer to it. Wouldn't it be nice if sol could look through the smart pointer, see the container, and treat it as a container in much the same way as when passed directly?

My problem: I have a custom container class that gets lifetime-managed with std::shared_ptr, so when passing an instance of the container to Lua, I pass a shared_ptr instead. However, this defeats automatic detection of containers.

Should it? Am I doing something wrong? I figured it would be convenient if this would still happen automatically, but maybe there's a snag I'm unaware of that makes this a silly wish.

Anyway, I attempted to do my own struct usertype_container<std::shared_ptr<my_type>> specialization, but it still doesn't work. As described in the documentation for container overriding, I also need to "provide the appropriate iterator and value_type definitions on the type."

But how is this done for a type I can't modify, like std::shared_ptr? I didn't find any obvious trait template I could specialize - it looks to me as if sol expects member typedefs for this.

Of course, it would be particularly cool if I could write a partial specialization of struct usertype_container that covers all containers that are pointed to by std::shared_ptr, so that this needn't be repeated for each individual usertype. But as long as I still can't get the non-templated version to work right, I see no chance...

s13n avatar Sep 26 '21 16:09 s13n

This might be nice to do, albeit exceeedingly complicated and probably open up Yet Another Can Of Worms where I attempt to mix a thing that has had a notorious number of bugs in it (the Automatic Container Special Handling) with the other bit that's been notoriously difficult to provide ease-of-use for (Unique Usertypes).

For now, you'll have to do Other Shenanigans™ - like write a function that explicitly converts the thing into a std::reference_wrapper<T> and returns it - to get some of the benefits while I try to straddle this beautiful and daunting asset-liability of an idea. :D

ThePhD avatar Oct 22 '21 20:10 ThePhD