sol2/docs/source/api/as_container.rst
ThePhD eb1560d12a add as_container documentation
improve exception documentation
improve state_view default handlers
add SAFE_PROPAGATION defines for compiling C++ as Lua
add examples for automatic operator registrations and as_container
fix tutorial code
change tests to not throw unless absolutely necessary
provide synchronization for file writing in tests
provide thread safety around thread tests for REQUIRE
add ostream automatic support
change 5.1 compat to only kick in luaL_loadbufferx and luaL_loadfilex when LuaJIT is version 2.0.1 and lower
2017-08-11 03:24:17 -04:00

24 lines
1.4 KiB
ReStructuredText

as_container
============
*force a type to be viewed as a container-type when serialized to Lua*
.. code-block:: cpp
template <typename T>
struct as_returns_t { ... };
template <typename T>
as_returns_t<T> as_returns( T&& );
Sometimes, you have a type whose metatable you claim with a usertype metatable via :doc:`usertype semantics<usertype>`. But, it still has parts of it that make it behave like a container in C++: A ``value_type`` typedef, an ``iterator`` typedef, a ``begin``, an ``end``, and other things that satisfy the `Container requirements`_ or the `Sequence Container requirements`_ or behaves like a `forward_list`_.
Whatever the case is, you need it to be returned to Lua and have many of the traits and functionality described in the :doc:`containers documentation<../containers>`. Wrap a return type or a setter in ``sol::as_container( value );`` to allow for a type to be treated like a container, regardless of whether ``sol::is_container`` triggers or not.
See `this container example`_ to see how it works.
.. _this container example: https://github.com/ThePhD/sol2/blob/develop/examples/container_as_container.cpp
.. _Container requirements: http://en.cppreference.com/w/cpp/concept/Container
.. _Sequence Container requirements: http://en.cppreference.com/w/cpp/concept/SequenceContainer
.. _forward_list: http://en.cppreference.com/w/cpp/container/forward_list