Merge pull request #225 from OrfeasZ/ut-helpers

Added helper functions for defining simple_usertype members manually.
This commit is contained in:
The Phantom Derpstorm 2016-09-17 12:02:15 -04:00 committed by GitHub
commit 68a9877a5e
3 changed files with 53 additions and 4 deletions

View File

@ -355,13 +355,28 @@ namespace sol {
global.new_simple_usertype<Class, CTor0, CTor...>(name, std::forward<Args>(args)...); global.new_simple_usertype<Class, CTor0, CTor...>(name, std::forward<Args>(args)...);
return *this; return *this;
} }
template<typename Class, typename... CArgs, typename... Args> template<typename Class, typename... CArgs, typename... Args>
state_view& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) { state_view& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
global.new_simple_usertype<Class>(name, ctor, std::forward<Args>(args)...); global.new_simple_usertype<Class>(name, ctor, std::forward<Args>(args)...);
return *this; return *this;
} }
template<typename Class, typename... Args>
simple_usertype<Class> create_simple_usertype(Args&&... args) {
return global.create_simple_usertype<Class>(std::forward<Args>(args)...);
}
template<typename Class, typename CTor0, typename... CTor, typename... Args>
simple_usertype<Class> create_simple_usertype(Args&&... args) {
return global.create_simple_usertype<Class, CTor0, CTor...>(std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
simple_usertype<Class> create_simple_usertype(constructors<CArgs...> ctor, Args&&... args) {
return global.create_simple_usertype<Class>(ctor, std::forward<Args>(args)...);
}
template<bool read_only = true, typename... Args> template<bool read_only = true, typename... Args>
state_view& new_enum(const std::string& name, Args&&... args) { state_view& new_enum(const std::string& name, Args&&... args) {
global.new_enum<read_only>(name, std::forward<Args>(args)...); global.new_enum<read_only>(name, std::forward<Args>(args)...);

View File

@ -274,7 +274,7 @@ namespace sol {
template<typename Class, typename... Args> template<typename Class, typename... Args>
basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) { basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) {
usertype<Class> utype(simple, base_t::lua_state(), std::forward<Args>(args)...); simple_usertype<Class> utype(base_t::lua_state(), std::forward<Args>(args)...);
set_usertype(name, utype); set_usertype(name, utype);
return *this; return *this;
} }
@ -287,11 +287,29 @@ namespace sol {
template<typename Class, typename... CArgs, typename... Args> template<typename Class, typename... CArgs, typename... Args>
basic_table_core& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) { basic_table_core& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
usertype<Class> utype(simple, base_t::lua_state(), ctor, std::forward<Args>(args)...); simple_usertype<Class> utype(base_t::lua_state(), ctor, std::forward<Args>(args)...);
set_usertype(name, utype); set_usertype(name, utype);
return *this; return *this;
} }
template<typename Class, typename... Args>
simple_usertype<Class> create_simple_usertype(Args&&... args) {
simple_usertype<Class> utype(base_t::lua_state(), std::forward<Args>(args)...);
return utype;
}
template<typename Class, typename CTor0, typename... CTor, typename... Args>
simple_usertype<Class> create_simple_usertype(Args&&... args) {
constructors<types<CTor0, CTor...>> ctor{};
return create_simple_usertype<Class>(ctor, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
simple_usertype<Class> create_simple_usertype(constructors<CArgs...> ctor, Args&&... args) {
simple_usertype<Class> utype(base_t::lua_state(), ctor, std::forward<Args>(args)...);
return utype;
}
template<bool read_only = true, typename... Args> template<bool read_only = true, typename... Args>
basic_table_core& new_enum(const std::string& name, Args&&... args) { basic_table_core& new_enum(const std::string& name, Args&&... args) {
if (read_only) { if (read_only) {

View File

@ -32,7 +32,7 @@ namespace sol {
template<typename T> template<typename T>
class usertype { class usertype {
private: protected:
std::unique_ptr<usertype_detail::registrar, detail::deleter> metatableregister; std::unique_ptr<usertype_detail::registrar, detail::deleter> metatableregister;
template<typename... Args> template<typename... Args>
@ -63,6 +63,22 @@ namespace sol {
} }
}; };
template<typename T>
class simple_usertype : public usertype<T> {
protected:
lua_State* state;
public:
template<typename... Args>
simple_usertype(lua_State* L, Args&&... args) : state(L), usertype(simple, L, std::forward<Args>(args)...) {}
template <typename N, typename F>
void add(N&& n, F&& f) {
auto meta = (simple_usertype_metatable<T>*) metatableregister.get();
meta->add(state, n, f);
}
};
namespace stack { namespace stack {
template<typename T> template<typename T>
struct pusher<usertype<T>> { struct pusher<usertype<T>> {