2016-03-14 21:53:24 +08:00
|
|
|
// The MIT License (MIT)
|
2014-04-26 07:41:03 +08:00
|
|
|
|
2016-02-27 15:43:53 +08:00
|
|
|
// Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
|
2014-04-26 07:41:03 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2014-09-19 22:45:13 +08:00
|
|
|
#ifndef SOL_USERTYPE_HPP
|
2014-09-19 20:22:21 +08:00
|
|
|
#define SOL_USERTYPE_HPP
|
2014-04-26 07:41:03 +08:00
|
|
|
|
2016-06-04 09:40:23 +08:00
|
|
|
#include "usertype_metatable.hpp"
|
|
|
|
#include "stack.hpp"
|
2016-06-07 03:46:53 +08:00
|
|
|
#include <memory>
|
2014-04-26 07:41:03 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-02-22 08:26:58 +08:00
|
|
|
namespace usertype_detail {
|
|
|
|
struct add_destructor_tag {};
|
|
|
|
struct check_destructor_tag {};
|
|
|
|
struct verified_tag {} const verified {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_constructor : std::false_type {};
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
struct is_constructor<constructors<Args...>> : std::true_type {};
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
struct is_constructor<constructor_wrapper<Args...>> : std::true_type {};
|
|
|
|
|
2016-05-04 02:19:30 +08:00
|
|
|
template <>
|
|
|
|
struct is_constructor<no_construction> : std::true_type {};
|
|
|
|
|
2016-02-22 08:26:58 +08:00
|
|
|
template <typename... Args>
|
2016-05-19 14:15:42 +08:00
|
|
|
using has_constructor = meta::any<is_constructor<meta::unqualified_t<Args>>...>;
|
2016-02-22 08:26:58 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_destructor : std::false_type {};
|
|
|
|
|
|
|
|
template <typename Fx>
|
|
|
|
struct is_destructor<destructor_wrapper<Fx>> : std::true_type {};
|
|
|
|
|
|
|
|
template <typename... Args>
|
2016-05-19 14:15:42 +08:00
|
|
|
using has_destructor = meta::any<is_destructor<meta::unqualified_t<Args>>...>;
|
2016-03-13 20:30:14 +08:00
|
|
|
} // usertype_detail
|
2016-02-22 08:26:58 +08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class usertype {
|
|
|
|
private:
|
2016-06-07 03:46:53 +08:00
|
|
|
std::unique_ptr<usertype_detail::registrar> metatableregister;
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename... Args>
|
|
|
|
usertype(usertype_detail::verified_tag, Args&&... args) : metatableregister( std::make_unique<usertype_metatable<T, std::tuple<std::decay_t<Args>...>>>(std::make_tuple(std::forward<Args>(args)...)) ) {}
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
usertype(usertype_detail::add_destructor_tag, Args&&... args) : usertype(usertype_detail::verified, std::forward<Args>(args)..., "__gc", default_destructor) {}
|
2016-04-11 13:02:43 +08:00
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename... Args>
|
|
|
|
usertype(usertype_detail::check_destructor_tag, Args&&... args) : usertype(meta::condition<meta::all<std::is_destructible<T>, meta::neg<usertype_detail::has_destructor<Args...>>>, usertype_detail::add_destructor_tag, usertype_detail::verified_tag>(), std::forward<Args>(args)...) {}
|
2016-02-22 08:26:58 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename... Args>
|
|
|
|
usertype(Args&&... args) : usertype(meta::condition<meta::all<std::is_default_constructible<T>, meta::neg<usertype_detail::has_constructor<Args...>>>, decltype(default_constructor), usertype_detail::check_destructor_tag>(), std::forward<Args>(args)...) {}
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename... Args, typename... CArgs>
|
2016-06-20 05:59:40 +08:00
|
|
|
usertype(constructors<CArgs...> constructorlist, Args&&... args) : usertype(usertype_detail::check_destructor_tag(), std::forward<Args>(args)..., "new", constructorlist) {}
|
2016-02-22 08:26:58 +08:00
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename... Args, typename... Fxs>
|
2016-06-20 05:59:40 +08:00
|
|
|
usertype(constructor_wrapper<Fxs...> constructorlist, Args&&... args) : usertype(usertype_detail::check_destructor_tag(), std::forward<Args>(args)..., "new", constructorlist) {}
|
2016-06-07 03:46:53 +08:00
|
|
|
|
|
|
|
int push(lua_State* L) {
|
|
|
|
return metatableregister->push_um(L);
|
|
|
|
}
|
2016-02-22 08:26:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace stack {
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<usertype<T>> {
|
|
|
|
static int push(lua_State* L, usertype<T>& user) {
|
|
|
|
return user.push(L);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // stack
|
2014-04-27 13:29:37 +08:00
|
|
|
} // sol
|
2014-04-26 07:41:03 +08:00
|
|
|
|
2014-09-19 20:22:21 +08:00
|
|
|
#endif // SOL_USERTYPE_HPP
|