diff options
author | Andy <drumsetmonkey@gmail.com> | 2019-08-29 13:07:45 -0400 |
---|---|---|
committer | Andy <drumsetmonkey@gmail.com> | 2019-08-29 13:07:45 -0400 |
commit | 4ac4b280abf2ffa28caa5a532353115a3033444f (patch) | |
tree | 2a13d658bb454360b2faf401244bb0321d3460d4 /lib/sol2/docs/source/api/as_table.rst | |
parent | e9758416b18b27a65337c28d9641afc0ee89b34b (diff) | |
parent | 7a46fa2dd3dad3f038bf8e7339bc67abca428ae6 (diff) |
Started creating scripting library/namespace and added sol2 for interfacing
Diffstat (limited to 'lib/sol2/docs/source/api/as_table.rst')
-rw-r--r-- | lib/sol2/docs/source/api/as_table.rst | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/sol2/docs/source/api/as_table.rst b/lib/sol2/docs/source/api/as_table.rst new file mode 100644 index 0000000..28c0636 --- /dev/null +++ b/lib/sol2/docs/source/api/as_table.rst @@ -0,0 +1,33 @@ +as_table +=========== +*make sure an object is pushed as a table* + + +.. code-block:: cpp + + template <typename T> + as_table_t { + T& value() &; + const T& value() & const; + T&& value() &&; + }; + + template <typename T> + as_table_t<T> as_function ( T&& container ); + +This function serves the purpose of ensuring that an object is pushed -- if possible -- like a table into Lua. The container passed here can be a pointer, a reference, a ``std::reference_wrapper`` around a container, or just a plain container value. It must have a begin/end function, and if it has a ``std::pair<Key, Value>`` as its ``value_type``, it will be pushed as a dictionary. Otherwise, it's pushed as a sequence. + +.. literalinclude:: ../../../examples/source/docs/as_table_ipairs.cpp + :linenos: + +Note that any caveats with Lua tables apply the moment it is serialized, and the data cannot be gotten out back out in C++ as a C++ type. You can deserialize the Lua table into something explicitly using the ``sol::as_table_t`` marker for your get and conversion operations using sol. At that point, the returned type is deserialized **from** a table, meaning you cannot reference any kind of C++ data directly as you do with regular userdata/usertypes. *All C++ type information is lost upon serialization into Lua.* + +If you need this functionality with a member variable, use a :doc:`property on a getter function<property>` that returns the result of ``sol::as_table``. + +This marker does NOT apply to :doc:`usertypes<usertype>`. + +You can also use this to nest types and retrieve tables within tables as shown by this example. + +.. literalinclude:: ../../../examples/source/containers_as_table.cpp + :linenos: + :lines: 1-8,31-60,62-68,70- |