sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Enhanced container operations

Open deadlocklogic opened this issue 1 year ago • 0 comments

Sometimes a container type may have missing semantics in order to be customized as in containers. For example:

  • a sequence container type missing max_size() will fail to compile. https://github.com/ThePhD/sol2/blob/2b0d2fe8ba0074e16b499940c4f3126b9c7d3471/include/sol/stack_get_unqualified.hpp#L399-L402
  • a sequence container type missing value_type will fail to compile. https://github.com/ThePhD/sol2/blob/2b0d2fe8ba0074e16b499940c4f3126b9c7d3471/include/sol/stack_get_unqualified.hpp#L585-L587

Here is a concrete example (actually I encountered a similar case in a library: the containers in the skia library SkSpan, TArray, STArray):

template <typename T>
class Vector
{
private:
    std::vector<T> data; // changed to map

public:
    // Intentionally omitted
    // using value_type = typename std::vector<T>::value_type;
    using iterator = typename std::vector<T>::iterator;
    using size_type = typename std::vector<T>::size_type;
    using reference = typename std::vector<T>::reference;

    iterator begin()
    {
        return iterator(data.begin());
    }
    iterator end()
    {
        return iterator(data.end());
    }
    reference operator[](size_type n)
    {
        return data[n];
    }
};

I suggest deferring such missing traits in the container type to the customization traits sol::usertype_container. Thanks.

deadlocklogic avatar Aug 12 '24 12:08 deadlocklogic