| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- // sol2
- // The MIT License (MIT)
- // Copyright (c) 2013-2022 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_WRAPPER_HPP
- #define SOL_WRAPPER_HPP
- #include <sol/types.hpp>
- namespace sol {
- namespace detail {
- template <typename T>
- using array_return_type = meta::conditional_t<std::is_array<T>::value, std::add_lvalue_reference_t<T>, T>;
- }
- template <typename F, typename = void>
- struct wrapper {
- typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
- typedef typename traits_type::args_list args_list;
- typedef typename traits_type::args_list free_args_list;
- typedef typename traits_type::returns_list returns_list;
- template <typename... Args>
- static decltype(auto) call(F& f, Args&&... args) {
- return f(std::forward<Args>(args)...);
- }
- struct caller {
- template <typename... Args>
- decltype(auto) operator()(F& fx, Args&&... args) const {
- return call(fx, std::forward<Args>(args)...);
- }
- };
- };
- template <typename F>
- struct wrapper<F, std::enable_if_t<std::is_function<std::remove_pointer_t<meta::unqualified_t<F>>>::value>> {
- typedef lua_bind_traits<std::remove_pointer_t<meta::unqualified_t<F>>> traits_type;
- typedef typename traits_type::args_list args_list;
- typedef typename traits_type::args_list free_args_list;
- typedef typename traits_type::returns_list returns_list;
- template <F fx, typename... Args>
- static decltype(auto) invoke(Args&&... args) {
- return fx(std::forward<Args>(args)...);
- }
- template <typename... Args>
- static decltype(auto) call(F& fx, Args&&... args) {
- return fx(std::forward<Args>(args)...);
- }
- struct caller {
- template <typename... Args>
- decltype(auto) operator()(F& fx, Args&&... args) const {
- return call(fx, std::forward<Args>(args)...);
- }
- };
- template <F fx>
- struct invoker {
- template <typename... Args>
- decltype(auto) operator()(Args&&... args) const {
- return invoke<fx>(std::forward<Args>(args)...);
- }
- };
- };
- template <typename F>
- struct wrapper<F, std::enable_if_t<std::is_member_object_pointer<meta::unqualified_t<F>>::value>> {
- typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
- typedef typename traits_type::object_type object_type;
- typedef typename traits_type::return_type return_type;
- typedef typename traits_type::args_list args_list;
- typedef types<object_type&, return_type> free_args_list;
- typedef typename traits_type::returns_list returns_list;
- template <F fx>
- static auto call(object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
- return mem.*fx;
- }
- template <F fx, typename Arg, typename... Args>
- static decltype(auto) invoke(object_type& mem, Arg&& arg, Args&&...) {
- return mem.*fx = std::forward<Arg>(arg);
- }
- template <typename Fx>
- static auto call(Fx&& fx, object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
- return mem.*fx;
- }
- template <typename Fx, typename Arg, typename... Args>
- static void call(Fx&& fx, object_type& mem, Arg&& arg, Args&&...) {
- using actual_type = meta::unqualified_t<detail::array_return_type<decltype(mem.*fx)>>;
- if constexpr (std::is_array_v<actual_type>) {
- using std::cbegin;
- using std::cend;
- auto first = cbegin(arg);
- auto last = cend(arg);
- for (std::size_t i = 0; first != last; ++i, ++first) {
- (mem.*fx)[i] = *first;
- }
- }
- else {
- (mem.*fx) = std::forward<Arg>(arg);
- }
- }
- struct caller {
- template <typename Fx, typename... Args>
- decltype(auto) operator()(Fx&& fx, object_type& mem, Args&&... args) const {
- return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
- }
- };
- template <F fx>
- struct invoker {
- template <typename... Args>
- decltype(auto) operator()(Args&&... args) const {
- return invoke<fx>(std::forward<Args>(args)...);
- }
- };
- };
- template <typename F, typename R, typename O, typename... FArgs>
- struct member_function_wrapper {
- typedef O object_type;
- typedef lua_bind_traits<F> traits_type;
- typedef typename traits_type::args_list args_list;
- typedef types<object_type&, FArgs...> free_args_list;
- typedef meta::tuple_types<R> returns_list;
- template <F fx, typename... Args>
- static R invoke(O& mem, Args&&... args) {
- return (mem.*fx)(std::forward<Args>(args)...);
- }
- template <typename Fx, typename... Args>
- static R call(Fx&& fx, O& mem, Args&&... args) {
- return (mem.*fx)(std::forward<Args>(args)...);
- }
- struct caller {
- template <typename Fx, typename... Args>
- decltype(auto) operator()(Fx&& fx, O& mem, Args&&... args) const {
- return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
- }
- };
- template <F fx>
- struct invoker {
- template <typename... Args>
- decltype(auto) operator()(O& mem, Args&&... args) const {
- return invoke<fx>(mem, std::forward<Args>(args)...);
- }
- };
- };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...)> : public member_function_wrapper<R (O::*)(Args...), R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const> : public member_function_wrapper<R (O::*)(Args...) const, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const volatile> : public member_function_wrapper<R (O::*)(Args...) const volatile, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...)&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const volatile&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...)&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const volatile&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) &&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const&&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const volatile&&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) &&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const&&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const volatile&&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> { };
- #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
- // noexcept has become a part of a function's type
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) noexcept> : public member_function_wrapper<R (O::*)(Args...) noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const noexcept> : public member_function_wrapper<R (O::*)(Args...) const noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const volatile noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...)& noexcept> : public member_function_wrapper<R (O::*)(Args...)& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const volatile& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...)& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...)& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const volatile& noexcept>
- : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...)&& noexcept> : public member_function_wrapper<R (O::*)(Args...)& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args...) const volatile&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> {
- };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...)&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...)& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> { };
- template <typename R, typename O, typename... Args>
- struct wrapper<R (O::*)(Args..., ...) const volatile&& noexcept>
- : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> { };
- #endif // noexcept is part of a function's type
- } // namespace sol
- #endif // SOL_WRAPPER_HPP
|