Change userdata to usertype names.

We don't need to make the function names
`open_usertype` now, since `new_usertype`
makes sense.
This commit is contained in:
ThePhD 2014-09-19 08:22:21 -04:00
parent 1e3466d173
commit 8970d3cd79
7 changed files with 71 additions and 83 deletions

View File

@ -26,7 +26,7 @@
#include "tuple.hpp"
#include "stack.hpp"
#include "function_types.hpp"
#include "userdata_traits.hpp"
#include "usertype_traits.hpp"
#include "resolve.hpp"
#include <cstdint>
#include <functional>
@ -191,7 +191,7 @@ struct pusher<function_sig_t<Sigs...>> {
template<typename Fx>
static void set_fx(lua_State* L, std::unique_ptr<base_function> luafunc) {
auto&& metakey = userdata_traits<Unqualified<Fx>>::metatable;
auto&& metakey = usertype_traits<Unqualified<Fx>>::metatable;
const char* metatablename = std::addressof(metakey[0]);
base_function* target = luafunc.release();
void* userdata = reinterpret_cast<void*>(target);

View File

@ -248,7 +248,7 @@ struct base_function {
}
template<std::size_t I>
struct userdata {
struct usertype {
static int call(lua_State* L) {
// Zero-based template parameter, but upvalues start at 1
return base_call(L, stack::get<upvalue_t>(L, I + 1));
@ -384,7 +384,7 @@ struct member_function : public base_function {
};
template<typename Function, typename Tp>
struct userdata_function_core : public base_function {
struct usertype_function_core : public base_function {
typedef typename std::remove_pointer<Tp>::type T;
typedef typename std::remove_pointer<typename std::decay<Function>::type>::type function_type;
typedef detail::functor<T, function_type> fx_t;
@ -395,7 +395,7 @@ struct userdata_function_core : public base_function {
fx_t fx;
template<typename... FxArgs>
userdata_function_core(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
usertype_function_core(FxArgs&&... fxargs): fx(std::forward<FxArgs>(fxargs)...) {}
template<typename Return, typename Raw = Unqualified<Return>>
typename std::enable_if<std::is_same<T, Raw>::value, void>::type push(lua_State* L, Return&& r) {
@ -443,15 +443,15 @@ struct userdata_function_core : public base_function {
};
template<typename Function, typename Tp>
struct userdata_function : public userdata_function_core<Function, Tp> {
typedef userdata_function_core<Function, Tp> base_t;
struct usertype_function : public usertype_function_core<Function, Tp> {
typedef usertype_function_core<Function, Tp> base_t;
typedef typename std::remove_pointer<Tp>::type T;
typedef typename base_t::traits_type traits_type;
typedef typename base_t::args_type args_type;
typedef typename base_t::return_type return_type;
template<typename... FxArgs>
userdata_function(FxArgs&&... fxargs): base_t(std::forward<FxArgs>(fxargs)...) {}
usertype_function(FxArgs&&... fxargs): base_t(std::forward<FxArgs>(fxargs)...) {}
template<typename Tx>
int fx_call(lua_State* L) {
@ -472,15 +472,15 @@ struct userdata_function : public userdata_function_core<Function, Tp> {
};
template<typename Function, typename Tp>
struct userdata_variable_function : public userdata_function_core<Function, Tp> {
typedef userdata_function_core<Function, Tp> base_t;
struct usertype_variable_function : public usertype_function_core<Function, Tp> {
typedef usertype_function_core<Function, Tp> base_t;
typedef typename std::remove_pointer<Tp>::type T;
typedef typename base_t::traits_type traits_type;
typedef typename base_t::args_type args_type;
typedef typename base_t::return_type return_type;
template<typename... FxArgs>
userdata_variable_function(FxArgs&&... fxargs): base_t(std::forward<FxArgs>(fxargs)...) {}
usertype_variable_function(FxArgs&&... fxargs): base_t(std::forward<FxArgs>(fxargs)...) {}
template<typename Tx>
int fx_call(lua_State* L) {
@ -511,8 +511,8 @@ struct userdata_variable_function : public userdata_function_core<Function, Tp>
};
template<typename Function, typename Tp>
struct userdata_indexing_function : public userdata_function_core<Function, Tp> {
typedef userdata_function_core<Function, Tp> base_t;
struct usertype_indexing_function : public usertype_function_core<Function, Tp> {
typedef usertype_function_core<Function, Tp> base_t;
typedef typename std::remove_pointer<Tp>::type T;
typedef typename base_t::traits_type traits_type;
typedef typename base_t::args_type args_type;
@ -522,7 +522,7 @@ struct userdata_indexing_function : public userdata_function_core<Function, Tp>
std::unordered_map<std::string, std::pair<std::unique_ptr<base_function>, bool>> functions;
template<typename... FxArgs>
userdata_indexing_function(std::string name, FxArgs&&... fxargs): base_t(std::forward<FxArgs>(fxargs)...), name(std::move(name)) {}
usertype_indexing_function(std::string name, FxArgs&&... fxargs): base_t(std::forward<FxArgs>(fxargs)...), name(std::move(name)) {}
template<typename Tx>
int fx_call(lua_State* L) {
@ -532,10 +532,10 @@ struct userdata_indexing_function : public userdata_function_core<Function, Tp>
if(function->second.second) {
stack::push<upvalue_t>(L, function->second.first.get());
if(std::is_same<T*, Tx>::value) {
stack::push(L, &base_function::userdata<0>::ref_call, 1);
stack::push(L, &base_function::usertype<0>::ref_call, 1);
}
else {
stack::push(L, &base_function::userdata<0>::call, 1);
stack::push(L, &base_function::usertype<0>::call, 1);
}
return 1;
}

View File

@ -26,7 +26,7 @@
#include "reference.hpp"
#include "tuple.hpp"
#include "traits.hpp"
#include "userdata_traits.hpp"
#include "usertype_traits.hpp"
#include <utility>
#include <array>
#include <cstring>
@ -259,19 +259,19 @@ struct pusher {
template<typename U = Unqualified<T>, EnableIf<Not<has_begin_end<U>>, Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
static void push(lua_State* L, T& t) {
detail::push_userdata<U>(L, userdata_traits<T>::metatable, t);
detail::push_userdata<U>(L, usertype_traits<T>::metatable, t);
}
template<typename U = Unqualified<T>, EnableIf<Not<has_begin_end<U>>, Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
static void push(lua_State* L, T&& t) {
detail::push_userdata<U>(L, userdata_traits<T>::metatable, std::move(t));
detail::push_userdata<U>(L, usertype_traits<T>::metatable, std::move(t));
}
};
template<typename T>
struct pusher<T*> {
static void push(lua_State* L, T* obj) {
detail::push_userdata<T*>(L, userdata_traits<T*>::metatable, obj);
detail::push_userdata<T*>(L, usertype_traits<T*>::metatable, obj);
}
};

View File

@ -148,26 +148,26 @@ public:
}
template<typename T>
state& set_userdata(userdata<T>& user) {
return set_userdata(user.name(), user);
state& set_usertype(usertype<T>& user) {
return set_usertype(usertype_traits<T>::name, user);
}
template<typename Key, typename T>
state& set_userdata(Key&& key, userdata<T>& user) {
global.set_userdata(std::forward<Key>(key), user);
state& set_usertype(Key&& key, usertype<T>& user) {
global.set_usertype(std::forward<Key>(key), user);
return *this;
}
template<typename Class, typename... CTor, typename... Args>
state& new_userdata(const std::string& name, Args&&... args) {
state& new_usertype(const std::string& name, Args&&... args) {
constructors<types<CTor...>> ctor{};
return new_userdata<Class>(name, ctor, std::forward<Args>(args)...);
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
state& new_userdata(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
userdata<Class> udata(name, ctor, std::forward<Args>(args)...);
global.set_userdata(udata);
state& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
usertype<Class> utype(ctor, std::forward<Args>(args)...);
set_usertype(name, utype);
return *this;
}

View File

@ -25,7 +25,7 @@
#include "proxy.hpp"
#include "stack.hpp"
#include "function_types.hpp"
#include "userdata.hpp"
#include "usertype.hpp"
namespace sol {
class table : public reference {
@ -84,12 +84,12 @@ public:
}
template<typename T>
table& set_userdata(userdata<T>& user) {
return set_userdata(user.name(), user);
table& set_usertype(usertype<T>& user) {
return set_userdata(usertype_traits<T>::name, user);
}
template<typename Key, typename T>
table& set_userdata(Key&& key, userdata<T>& user) {
table& set_usertype(Key&& key, usertype<T>& user) {
push();
stack::push(state(), std::forward<Key>(key));
stack::push(state(), user);

View File

@ -20,11 +20,11 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SOL_USERDATA_HPP
#define SOL_USERDATA_HPP
#define SOL_USERTYPE_HPP
#include "state.hpp"
#include "function_types.hpp"
#include "userdata_traits.hpp"
#include "usertype_traits.hpp"
#include "default_construct.hpp"
#include <vector>
#include <array>
@ -88,7 +88,7 @@ enum class meta_function {
};
template<typename T>
class userdata {
class usertype {
private:
typedef std::unordered_map<std::string, std::pair<std::unique_ptr<base_function>, bool>> function_map_t;
function_map_t indexmetafunctions, newindexmetafunctions;
@ -97,7 +97,6 @@ private:
std::vector<luaL_Reg> metafunctiontable;
std::vector<luaL_Reg> ptrmetafunctiontable;
lua_CFunction cleanup;
std::string luaname;
template<typename... TTypes>
struct constructor {
@ -121,7 +120,7 @@ private:
}
static int construct(lua_State* L) {
auto&& meta = userdata_traits<T>::metatable;
auto&& meta = usertype_traits<T>::metatable;
call_syntax syntax = stack::get_call_syntax(L, meta);
int argcount = lua_gettop(L);
@ -131,7 +130,7 @@ private:
if(luaL_newmetatable(L, std::addressof(meta[0])) == 1) {
lua_pop(L, 1);
std::string err = "Unable to get userdata metatable for ";
std::string err = "Unable to get usertype metatable for ";
err += meta;
throw error(err);
}
@ -153,7 +152,7 @@ private:
template<std::size_t N>
void build_cleanup() {
cleanup = &base_function::userdata<N>::gc;
cleanup = &base_function::usertype<N>::gc;
}
template<std::size_t N>
@ -161,13 +160,13 @@ private:
int extracount = 0;
if(!indexmetafunctions.empty()) {
if(index == nullptr) {
auto idxptr = detail::make_unique<userdata_indexing_function<void (T::*)(), T>>("__index", nullptr);
auto idxptr = detail::make_unique<usertype_indexing_function<void (T::*)(), T>>("__index", nullptr);
index = &(idxptr->functions);
functionnames.emplace_back("__index");
metafunctions.emplace_back(std::move(idxptr));
std::string& name = functionnames.back();
metafunctiontable.push_back({ name.c_str(), &base_function::userdata<N>::call });
ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata<N>::ref_call });
metafunctiontable.push_back({ name.c_str(), &base_function::usertype<N>::call });
ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype<N>::ref_call });
++extracount;
}
auto& idx = *index;
@ -177,18 +176,18 @@ private:
}
if(!newindexmetafunctions.empty()) {
if(newindex == nullptr) {
auto idxptr = detail::make_unique<userdata_indexing_function<void (T::*)(), T>>("__newindex", nullptr);
auto idxptr = detail::make_unique<usertype_indexing_function<void (T::*)(), T>>("__newindex", nullptr);
newindex = &(idxptr->functions);
functionnames.emplace_back("__newindex");
metafunctions.emplace_back(std::move(idxptr));
std::string& name = functionnames.back();
if(extracount > 0) {
metafunctiontable.push_back({ name.c_str(), &base_function::userdata<N + 1>::call });
ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata<N + 1>::ref_call });
metafunctiontable.push_back({ name.c_str(), &base_function::usertype<N + 1>::call });
ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype<N + 1>::ref_call });
}
else {
metafunctiontable.push_back({ name.c_str(), &base_function::userdata<N>::call });
ptrmetafunctiontable.push_back({ name.c_str(), &base_function::userdata<N>::ref_call });
metafunctiontable.push_back({ name.c_str(), &base_function::usertype<N>::call });
ptrmetafunctiontable.push_back({ name.c_str(), &base_function::usertype<N>::ref_call });
}
++extracount;
}
@ -215,8 +214,8 @@ private:
bool build_function(std::true_type, function_map_t*&, function_map_t*&, std::string funcname, Ret Base::* func) {
static_assert(std::is_base_of<Base, T>::value, "Any registered function must be part of the class");
typedef typename std::decay<decltype(func)>::type function_type;
indexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique<userdata_variable_function<function_type, T>>(func), false));
newindexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique<userdata_variable_function<function_type, T>>(func), false));
indexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique<usertype_variable_function<function_type, T>>(func), false));
newindexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique<usertype_variable_function<function_type, T>>(func), false));
return false;
}
@ -225,21 +224,21 @@ private:
typedef Unqualified<Arg> Argu;
static_assert(std::is_base_of<Argu, T>::value, "Any non-member-function must have a first argument which is covariant with the desired userdata type.");
typedef typename std::decay<decltype(func)>::type function_type;
return detail::make_unique<userdata_function<function_type, T>>(func);
return detail::make_unique<usertype_function<function_type, T>>(func);
}
template<typename Base, typename Ret>
std::unique_ptr<base_function> make_variable_function(std::true_type, const std::string&, Ret Base::* func) {
static_assert(std::is_base_of<Base, T>::value, "Any registered function must be part of the class");
typedef typename std::decay<decltype(func)>::type function_type;
return detail::make_unique<userdata_variable_function<function_type, T>>(func);
return detail::make_unique<usertype_variable_function<function_type, T>>(func);
}
template<typename Base, typename Ret>
std::unique_ptr<base_function> make_variable_function(std::false_type, const std::string&, Ret Base::* func) {
static_assert(std::is_base_of<Base, T>::value, "Any registered function must be part of the class");
typedef typename std::decay<decltype(func)>::type function_type;
return detail::make_unique<userdata_function<function_type, T>>(func);
return detail::make_unique<usertype_function<function_type, T>>(func);
}
template<typename Base, typename Ret>
@ -255,7 +254,7 @@ private:
typedef Unqualified<TArg> TArgu;
static_assert(std::is_base_of<TArgu, T>::value, "Any non-member-function must have a first argument which is covariant with the desired userdata type.");
typedef typename std::decay<decltype(func)>::type function_type;
return detail::make_unique<userdata_function<function_type, T>>(func);
return detail::make_unique<usertype_function<function_type, T>>(func);
}
template<std::size_t N, typename Fx>
@ -268,7 +267,7 @@ private:
auto indexmetamethod = std::find(meta_variable_names.begin(), meta_variable_names.end(), name);
std::unique_ptr<base_function> ptr(nullptr);
if(indexmetamethod != meta_variable_names.end()) {
auto idxptr = detail::make_unique<userdata_indexing_function<function_type, T>>(name, func);
auto idxptr = detail::make_unique<usertype_indexing_function<function_type, T>>(name, func);
switch(std::distance(indexmetamethod, meta_variable_names.end())) {
case 0:
index = &(idxptr->functions);
@ -285,8 +284,8 @@ private:
ptr = make_function(funcname, std::forward<Fx>(func));
}
metafunctions.emplace_back(std::move(ptr));
metafunctiontable.push_back( { name.c_str(), &base_function::userdata<N>::call } );
ptrmetafunctiontable.push_back( { name.c_str(), &base_function::userdata<N>::ref_call } );
metafunctiontable.push_back( { name.c_str(), &base_function::usertype<N>::call } );
ptrmetafunctiontable.push_back( { name.c_str(), &base_function::usertype<N>::ref_call } );
return true;
}
indexmetafunctions.emplace(funcname, std::make_pair(make_function(funcname, std::forward<Fx>(func)), true));
@ -314,13 +313,10 @@ private:
public:
template<typename... Args>
userdata(Args&&... args): userdata(userdata_traits<T>::name, default_constructor, std::forward<Args>(args)...) {}
usertype(Args&&... args): usertype(default_constructor, std::forward<Args>(args)...) {}
template<typename... Args, typename... CArgs>
userdata(constructors<CArgs...> c, Args&&... args): userdata(userdata_traits<T>::name, std::move(c), std::forward<Args>(args)...) {}
template<typename... Args, typename... CArgs>
userdata(std::string name, constructors<CArgs...>, Args&&... args): luaname(std::move(name)) {
usertype(constructors<CArgs...>, Args&&... args) {
functionnames.reserve(sizeof...(args) + 2);
metafunctiontable.reserve(sizeof...(args));
ptrmetafunctiontable.reserve(sizeof...(args));
@ -343,23 +339,15 @@ public:
ptrmetafunctiontable.push_back({ nullptr, nullptr });
}
template<typename... Args, typename... CArgs>
userdata(const char* name, constructors<CArgs...> c, Args&&... args) :
userdata(std::string(name), std::move(c), std::forward<Args>(args)...) {}
const std::string& name() const {
return luaname;
}
void push(lua_State* L) {
// push pointer tables first,
// but leave the regular T table on last
// so it can be linked to a type for usage with `.new(...)` or `:new(...)`
push_metatable(L, userdata_traits<T*>::metatable,
push_metatable(L, usertype_traits<T*>::metatable,
metafunctions, ptrmetafunctiontable);
lua_pop(L, 1);
push_metatable(L, userdata_traits<T>::metatable,
push_metatable(L, usertype_traits<T>::metatable,
metafunctions, metafunctiontable);
set_global_deleter(L);
}
@ -386,7 +374,7 @@ private:
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
// gctable name by default has ♻ part of it
lua_setglobal(L, std::addressof(userdata_traits<T>::gctable[0]));
lua_setglobal(L, std::addressof(usertype_traits<T>::gctable[0]));
}
template<bool release = false, typename TCont>
@ -407,12 +395,12 @@ private:
namespace stack {
template<typename T>
struct pusher<userdata<T>> {
static void push(lua_State* L, userdata<T>& user) {
struct pusher<usertype<T>> {
static void push(lua_State* L, usertype<T>& user) {
user.push(L);
}
};
} // stack
} // sol
#endif // SOL_USERDATA_HPP
#endif // SOL_USERTYPE_HPP

View File

@ -19,29 +19,29 @@
// 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_USERDATA_TRAITS_HPP
#define SOL_USERDATA_TRAITS_HPP
#ifndef SOL_USERTYPE_TRAITS_HPP
#define SOL_USERTYPE_TRAITS_HPP
#include "demangle.hpp"
namespace sol {
template<typename T>
struct userdata_traits {
struct usertype_traits {
static const std::string name;
static const std::string metatable;
static const std::string gctable;
};
template<typename T>
const std::string userdata_traits<T>::name = detail::demangle(typeid(T));
const std::string usertype_traits<T>::name = detail::demangle(typeid(T));
template<typename T>
const std::string userdata_traits<T>::metatable = std::string("sol.").append(detail::demangle(typeid(T)));
const std::string usertype_traits<T>::metatable = std::string("sol.").append(detail::demangle(typeid(T)));
template<typename T>
const std::string userdata_traits<T>::gctable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB");
const std::string usertype_traits<T>::gctable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB");
}
#endif // SOL_USERDATA_TRAITS_HPP
#endif // SOL_USERTYPE_TRAITS_HPP