mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Please read the docs. Pretty please.
This commit is contained in:
parent
ce32549bc6
commit
2ca27eede3
|
@ -90,6 +90,45 @@ The various operations provided by ``usertype_container<T>`` are expected to be
|
|||
|
||||
Exception handling **WILL** be provided around these particular raw C functions, so you do not need to worry about exceptions or errors bubbling through and handling that part. It is specifically handled for you in this specific instance, and **ONLY** in this specific instance. The raw note still applies to every other raw C function you make manually.
|
||||
|
||||
.. _container-classifications:
|
||||
|
||||
container classifications
|
||||
-------------------------
|
||||
|
||||
When you push a container into sol3, the default container handler deals with the containers by inspecting various properties, functions, and type definitions on them. Here are the broad implications of containers sol3's defaults will recognize, and which already-known containers fall into their categories:
|
||||
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| container type | requirements | known containers | notes/caveats |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| sequence | ``erase(iterator)`` | std::vector | - ``find`` operation is linear in size of list (searches all elements) |
|
||||
| | ``push_back``/``insert(value_type)`` | std::deque | - std::forward_list has forward-only iterators: set/find is a linear operation |
|
||||
| | | std::list | - std::forward_list uses "insert_after" idiom, requires special handling internally |
|
||||
| | | std::forward_list | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| fixed | lacking ``push_back``/``insert`` | std::array<T, n> | - regular c-style arrays must be set with ``std::ref( arr )`` or ``&arr`` to be usable |
|
||||
| | lacking ``erase`` | T[n] (fixed arrays) | - default implementation sets values using operator[] |
|
||||
| | | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| ordered | ``key_type`` typedef | std::set | - ``container[key] = stuff`` operation erases when ``stuff`` is nil, inserts/sets when not |
|
||||
| | ``erase(key)`` | std::multi_set | - ``container.get(key)`` returns the key itself |
|
||||
| | ``find(key)`` | | |
|
||||
| | ``insert(key)`` | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| associative, ordered | ``key_type``, ``mapped_type`` typedefs | std::map | |
|
||||
| | ``erase(key)`` | std::multi_map | |
|
||||
| | ``find(key)`` | | |
|
||||
| | ``insert({ key, value })`` | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| unordered | same as ordered | std::unordered_set | - ``container[key] = stuff`` operation erases when ``stuff`` is nil, inserts/sets when not |
|
||||
| | | std::unordered_multiset | - ``container.get(key)`` returns the key itself |
|
||||
| | | | - iteration not guaranteed to be in order of insertion, just like the C++ container |
|
||||
| | | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| unordered, associative | same as ordered, associative | std::unordered_map | - iteration not guaranteed to be in order of insertion, just like the C++ container |
|
||||
| | | std::unordered_multimap | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
|
||||
|
||||
.. _container-operations:
|
||||
|
||||
container operations
|
||||
|
@ -126,7 +165,7 @@ Below are the many container operations and their override points for ``usertype
|
|||
| erase | ``c:erase(target)`` | ``static int erase(lua_State*);`` | 1 self | - for sequence containers, ``target`` is an index to erase |
|
||||
| | | | 2 target | - for lookup containers, ``target`` is the key type |
|
||||
| | | | | - uses linear incrementation to spot for sequence containers that do not have random access iterators (``std::list``, ``std::forward_list``, and similar) |
|
||||
| | | | | - can invalidates iteration |
|
||||
| | | | | - can invalidate iteration |
|
||||
+-----------+-------------------------------------------+--------------------------------------------------+----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||||
| insert | ``c:insert(target, value)`` | | 1 self | - for sequence containers, ``target`` is an index, otherwise it is the key type |
|
||||
| | | | 2 target | - inserts into a container if possible at the specified location |
|
||||
|
@ -174,45 +213,6 @@ Below are the many container operations and their override points for ``usertype
|
|||
Overriding the detection traits and operation traits listed above and then trying to use ``sol::as_table`` or similar can result in compilation failures if you do not have a proper ``begin()`` or ``end()`` function on the type. If you want things to behave with special usertype considerations, please do not wrap the container in one of the special table-converting/forcing abstractions.
|
||||
|
||||
|
||||
.. _container-classifications:
|
||||
|
||||
container classifications
|
||||
-------------------------
|
||||
|
||||
When you push a container into sol3, the default container handler deals with the containers by inspecting various properties, functions, and typedefs on them. Here are the broad implications of containers sol3's defaults will recognize, and which already-known containers fall into their categories:
|
||||
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| container type | requirements | known containers | notes/caveats |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| sequence | ``erase(iterator)`` | std::vector | - ``find`` operation is linear in size of list (searches all elements) |
|
||||
| | ``push_back``/``insert(value_type)`` | std::deque | - std::forward_list has forward-only iterators: set/find is a linear operation |
|
||||
| | | std::list | - std::forward_list uses "insert_after" idiom, requires special handling internally |
|
||||
| | | std::forward_list | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| fixed | lacking ``push_back``/``insert`` | std::array<T, n> | - regular c-style arrays must be set with ``std::ref( arr )`` or ``&arr`` to be usable |
|
||||
| | lacking ``erase`` | T[n] (fixed arrays) | - default implementation sets values using operator[] |
|
||||
| | | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| ordered | ``key_type`` typedef | std::set | - ``container[key] = stuff`` operation erases when ``stuff`` is nil, inserts/sets when not |
|
||||
| | ``erase(key)`` | std::multi_set | - ``container.get(key)`` returns the key itself |
|
||||
| | ``find(key)`` | | |
|
||||
| | ``insert(key)`` | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| associative, ordered | ``key_type``, ``mapped_type`` typedefs | std::map | |
|
||||
| | ``erase(key)`` | std::multi_map | |
|
||||
| | ``find(key)`` | | |
|
||||
| | ``insert({ key, value })`` | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| unordered | same as ordered | std::unordered_set | - ``container[key] = stuff`` operation erases when ``stuff`` is nil, inserts/sets when not |
|
||||
| | | std::unordered_multiset | - ``container.get(key)`` returns the key itself |
|
||||
| | | | - iteration not guaranteed to be in order of insertion, just like the C++ container |
|
||||
| | | | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
| unordered, associative | same as ordered, associative | std::unordered_map | - iteration not guaranteed to be in order of insertion, just like the C++ container |
|
||||
| | | std::unordered_multimap | |
|
||||
+------------------------+----------------------------------------+-------------------------+-----------------------------------------------------------------------------------------------+
|
||||
|
||||
|
||||
a complete example
|
||||
------------------
|
||||
|
||||
|
|
|
@ -109,6 +109,18 @@ namespace sol {
|
|||
static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct has_erase_key_test {
|
||||
private:
|
||||
template <typename C>
|
||||
static meta::sfinae_yes_t test(decltype(std::declval<C>().erase(std::declval<typename C::key_type>()))*);
|
||||
template <typename C>
|
||||
static meta::sfinae_no_t test(...);
|
||||
|
||||
public:
|
||||
static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct has_traits_find_test {
|
||||
private:
|
||||
|
@ -289,6 +301,9 @@ namespace sol {
|
|||
template <typename T>
|
||||
using has_erase = meta::boolean<has_erase_test<T>::value>;
|
||||
|
||||
template <typename T>
|
||||
using has_erase_key = meta::boolean<has_erase_key_test<T>::value>;
|
||||
|
||||
template <typename T>
|
||||
using has_erase_after = meta::boolean<has_erase_after_test<T>::value>;
|
||||
|
||||
|
@ -1059,12 +1074,20 @@ namespace sol {
|
|||
return detail::error_result("sol: cannot call erase on '%s'", detail::demangle<T>().c_str());
|
||||
}
|
||||
|
||||
static detail::error_result erase_key_has(std::true_type, lua_State* L, T& self, K& key) {
|
||||
return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
|
||||
}
|
||||
|
||||
static detail::error_result erase_key_has(std::false_type, lua_State* L, T& self, K& key) {
|
||||
return erase_after_has(has_erase_after<T>(), L, self, key);
|
||||
}
|
||||
|
||||
static detail::error_result erase_has(std::true_type, lua_State* L, T& self, K& key) {
|
||||
return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
|
||||
}
|
||||
|
||||
static detail::error_result erase_has(std::false_type, lua_State* L, T& self, K& key) {
|
||||
return erase_after_has(has_erase_after<T>(), L, self, key);
|
||||
return erase_key_has(has_erase_key<T>(), L, self, key);
|
||||
}
|
||||
|
||||
static auto size_has(std::false_type, lua_State* L, T& self) {
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2019-11-09 04:52:35.824332 UTC
|
||||
// This header was generated with sol v3.0.3 (revision 88355fc)
|
||||
// Generated 2019-11-12 17:52:53.774510 UTC
|
||||
// This header was generated with sol v3.0.3 (revision ce32549)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2019-11-09 04:52:35.317324 UTC
|
||||
// This header was generated with sol v3.0.3 (revision 88355fc)
|
||||
// Generated 2019-11-12 17:52:53.422846 UTC
|
||||
// This header was generated with sol v3.0.3 (revision ce32549)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||
|
@ -19153,6 +19153,18 @@ namespace sol {
|
|||
static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct has_erase_key_test {
|
||||
private:
|
||||
template <typename C>
|
||||
static meta::sfinae_yes_t test(decltype(std::declval<C>().erase(std::declval<typename C::key_type>()))*);
|
||||
template <typename C>
|
||||
static meta::sfinae_no_t test(...);
|
||||
|
||||
public:
|
||||
static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct has_traits_find_test {
|
||||
private:
|
||||
|
@ -19333,6 +19345,9 @@ namespace sol {
|
|||
template <typename T>
|
||||
using has_erase = meta::boolean<has_erase_test<T>::value>;
|
||||
|
||||
template <typename T>
|
||||
using has_erase_key = meta::boolean<has_erase_key_test<T>::value>;
|
||||
|
||||
template <typename T>
|
||||
using has_erase_after = meta::boolean<has_erase_after_test<T>::value>;
|
||||
|
||||
|
@ -20103,12 +20118,20 @@ namespace sol {
|
|||
return detail::error_result("sol: cannot call erase on '%s'", detail::demangle<T>().c_str());
|
||||
}
|
||||
|
||||
static detail::error_result erase_key_has(std::true_type, lua_State* L, T& self, K& key) {
|
||||
return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
|
||||
}
|
||||
|
||||
static detail::error_result erase_key_has(std::false_type, lua_State* L, T& self, K& key) {
|
||||
return erase_after_has(has_erase_after<T>(), L, self, key);
|
||||
}
|
||||
|
||||
static detail::error_result erase_has(std::true_type, lua_State* L, T& self, K& key) {
|
||||
return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
|
||||
}
|
||||
|
||||
static detail::error_result erase_has(std::false_type, lua_State* L, T& self, K& key) {
|
||||
return erase_after_has(has_erase_after<T>(), L, self, key);
|
||||
return erase_key_has(has_erase_key<T>(), L, self, key);
|
||||
}
|
||||
|
||||
static auto size_has(std::false_type, lua_State* L, T& self) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user