// The MIT License (MIT) // Copyright (c) 2013-2017 Rapptz, ThePhD and contributors // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef SOL_STACK_CORE_HPP #define SOL_STACK_CORE_HPP #include "types.hpp" #include "reference.hpp" #include "stack_reference.hpp" #include "tuple.hpp" #include "traits.hpp" #include "tie.hpp" #include "stack_guard.hpp" #include #include namespace sol { namespace detail { struct as_reference_tag {}; template struct as_pointer_tag {}; template struct as_value_tag {}; using special_destruct_func = void(*)(void*); template inline void special_destruct(void* memory) { T** pointerpointer = static_cast(memory); special_destruct_func* dx = static_cast(static_cast(pointerpointer + 1)); Real* target = static_cast(static_cast(dx + 1)); target->~Real(); } template inline int unique_destruct(lua_State* L) { void* memory = lua_touserdata(L, 1); T** pointerpointer = static_cast(memory); special_destruct_func& dx = *static_cast(static_cast(pointerpointer + 1)); (dx)(memory); return 0; } template inline int user_alloc_destroy(lua_State* L) { void* rawdata = lua_touserdata(L, 1); T* data = static_cast(rawdata); std::allocator alloc; alloc.destroy(data); return 0; } template inline int usertype_alloc_destroy(lua_State* L) { void* rawdata = lua_touserdata(L, 1); T** pdata = static_cast(rawdata); T* data = *pdata; std::allocator alloc{}; alloc.destroy(data); return 0; } template void reserve(T&, std::size_t) {} template void reserve(std::vector& arr, std::size_t hint) { arr.reserve(hint); } template void reserve(std::basic_string& arr, std::size_t hint) { arr.reserve(hint); } } // detail namespace stack { template struct field_getter; template struct probe_field_getter; template struct field_setter; template struct getter; template struct popper; template struct pusher; template::value, typename = void> struct checker; template struct check_getter; struct probe { bool success; int levels; probe(bool s, int l) : success(s), levels(l) {} operator bool() const { return success; }; }; struct record { int last; int used; record() : last(), used() {} void use(int count) { last = count; used += count; } }; namespace stack_detail { template struct strip { typedef T type; }; template struct strip> { typedef T& type; }; template struct strip> { typedef T& type; }; template struct strip> { typedef T type; }; template using strip_t = typename strip::type; const bool default_check_arguments = #ifdef SOL_CHECK_ARGUMENTS true; #else false; #endif template inline decltype(auto) unchecked_get(lua_State* L, int index, record& tracking) { return getter>{}.get(L, index, tracking); } template inline int push_reference(lua_State* L, Arg&& arg, Args&&... args) { typedef meta::all< std::is_lvalue_reference, meta::neg>, meta::neg>>, meta::neg>> > use_reference_tag; return pusher>>{}.push(L, std::forward(arg), std::forward(args)...); } } // stack_detail inline bool maybe_indexable(lua_State* L, int index = -1) { type t = type_of(L, index); return t == type::userdata || t == type::table; } template inline int push(lua_State* L, T&& t, Args&&... args) { return pusher>{}.push(L, std::forward(t), std::forward(args)...); } // overload allows to use a pusher of a specific type, but pass in any kind of args template::value>> inline int push(lua_State* L, Arg&& arg, Args&&... args) { return pusher>{}.push(L, std::forward(arg), std::forward(args)...); } template inline int push_reference(lua_State* L, T&& t, Args&&... args) { return stack_detail::push_reference(L, std::forward(t), std::forward(args)...); } template inline int push_reference(lua_State* L, Arg&& arg, Args&&... args) { return stack_detail::push_reference(L, std::forward(arg), std::forward(args)...); } inline int multi_push(lua_State*) { // do nothing return 0; } template inline int multi_push(lua_State* L, T&& t, Args&&... args) { int pushcount = push(L, std::forward(t)); void(sol::detail::swallow{ (pushcount += sol::stack::push(L, std::forward(args)), 0)... }); return pushcount; } inline int multi_push_reference(lua_State*) { // do nothing return 0; } template inline int multi_push_reference(lua_State* L, T&& t, Args&&... args) { int pushcount = push_reference(L, std::forward(t)); void(sol::detail::swallow{ (pushcount += sol::stack::push_reference(L, std::forward(args)), 0)... }); return pushcount; } template bool check(lua_State* L, int index, Handler&& handler, record& tracking) { typedef meta::unqualified_t Tu; checker c; // VC++ has a bad warning here: shut it up (void)c; return c.check(L, index, std::forward(handler), tracking); } template bool check(lua_State* L, int index, Handler&& handler) { record tracking{}; return check(L, index, std::forward(handler), tracking); } template bool check(lua_State* L, int index = -lua_size>::value) { auto handler = no_panic; return check(L, index, handler); } template inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler, record& tracking) { return check_getter>{}.get(L, index, std::forward(handler), tracking); } template inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler) { record tracking{}; return check_get(L, index, handler, tracking); } template inline decltype(auto) check_get(lua_State* L, int index = -lua_size>::value) { auto handler = no_panic; return check_get(L, index, handler); } namespace stack_detail { #ifdef SOL_CHECK_ARGUMENTS template inline auto tagged_get(types, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get(L, index, tracking)) { auto op = check_get(L, index, type_panic, tracking); return *std::move(op); } #else template inline decltype(auto) tagged_get(types, lua_State* L, int index, record& tracking) { return stack_detail::unchecked_get(L, index, tracking); } #endif template inline decltype(auto) tagged_get(types>, lua_State* L, int index, record& tracking) { return stack_detail::unchecked_get>(L, index, tracking); } template struct check_types { template static bool check(types, lua_State* L, int firstargument, Handler&& handler, record& tracking) { if (!stack::check(L, firstargument + tracking.used, handler, tracking)) return false; return check(types(), L, firstargument, std::forward(handler), tracking); } template static bool check(types<>, lua_State*, int, Handler&&, record&) { return true; } }; template <> struct check_types { template static bool check(types, lua_State*, int, Handler&&, record&) { return true; } }; } // stack_detail template bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) { return stack_detail::check_types{}.check(types...>(), L, index, std::forward(handler), tracking); } template bool multi_check(lua_State* L, int index, Handler&& handler) { record tracking{}; return multi_check(L, index, std::forward(handler), tracking); } template bool multi_check(lua_State* L, int index) { auto handler = no_panic; return multi_check(L, index, handler); } template bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) { return multi_check(L, index, std::forward(handler), tracking); } template bool multi_check(lua_State* L, int index, Handler&& handler) { return multi_check(L, index, std::forward(handler)); } template bool multi_check(lua_State* L, int index) { return multi_check(L, index); } template inline decltype(auto) get(lua_State* L, int index, record& tracking) { return stack_detail::tagged_get(types(), L, index, tracking); } template inline decltype(auto) get(lua_State* L, int index = -lua_size>::value) { record tracking{}; return get(L, index, tracking); } template inline decltype(auto) pop(lua_State* L) { return popper>{}.pop(L); } template void get_field(lua_State* L, Key&& key) { field_getter, global, raw>{}.get(L, std::forward(key)); } template void get_field(lua_State* L, Key&& key, int tableindex) { field_getter, global, raw>{}.get(L, std::forward(key), tableindex); } template void raw_get_field(lua_State* L, Key&& key) { get_field(L, std::forward(key)); } template void raw_get_field(lua_State* L, Key&& key, int tableindex) { get_field(L, std::forward(key), tableindex); } template probe probe_get_field(lua_State* L, Key&& key) { return probe_field_getter, global, raw>{}.get(L, std::forward(key)); } template probe probe_get_field(lua_State* L, Key&& key, int tableindex) { return probe_field_getter, global, raw>{}.get(L, std::forward(key), tableindex); } template probe probe_raw_get_field(lua_State* L, Key&& key) { return probe_get_field(L, std::forward(key)); } template probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) { return probe_get_field(L, std::forward(key), tableindex); } template void set_field(lua_State* L, Key&& key, Value&& value) { field_setter, global, raw>{}.set(L, std::forward(key), std::forward(value)); } template void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) { field_setter, global, raw>{}.set(L, std::forward(key), std::forward(value), tableindex); } template void raw_set_field(lua_State* L, Key&& key, Value&& value) { set_field(L, std::forward(key), std::forward(value)); } template void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) { set_field(L, std::forward(key), std::forward(value), tableindex); } } // stack } // sol #endif // SOL_STACK_CORE_HPP