1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-16 10:05:22 +00:00

added state::createuserdata template function

This commit is contained in:
Pavel Labath 2010-02-12 17:41:24 +01:00
parent 0ae2ba5210
commit 00d8215e00
2 changed files with 16 additions and 4 deletions

View File

@ -94,10 +94,9 @@ namespace lua {
catch(...) {
// C++ exceptions (pointers to them, actually) are stored as lua userdata and
// then thrown
void *ptr = L->newuserdata(sizeof(std::exception_ptr));
L->createuserdata<std::exception_ptr>(std::current_exception());
L->rawgetfield(REGISTRYINDEX, cpp_exception_metatable);
L->setmetatable(-2);
new(ptr) std::exception_ptr(std::current_exception());
}
// lua_error does longjmp(), so destructors for objects in this function will not be
@ -386,10 +385,9 @@ namespace lua {
{
checkstack(2);
void *ptr = newuserdata(sizeof(cpp_function));
createuserdata<cpp_function>(fn);
rawgetfield(REGISTRYINDEX, cpp_function_metatable);
setmetatable(-2);
new(ptr) cpp_function(fn);
insert(-n-1);
lua_pushcclosure(cobj.get(), &closure_trampoline, n+1);

View File

@ -263,6 +263,10 @@ namespace lua {
// lua_tostring uses NULL to indicate conversion error, since there is no such thing as a
// NULL std::string, we throw an exception. Returned value may contain '\0'
std::string tostring(int index) throw(lua::not_string_error);
// allocate a new lua userdata of appropriate size, and create a object in it
// pushes the userdata on stack and returns the pointer
template<typename T, typename... Args>
T* createuserdata(Args&&... args);
};
/*
@ -291,6 +295,16 @@ namespace lua {
void operator+=(int n_) throw() { n+=n_; }
void operator-=(int n_) throw() { n-=n_; }
};
template<typename T, typename... Args>
T* state::createuserdata(Args&&... args)
{
stack_sentry s(*this);
void *t = newuserdata(sizeof(T)); ++s;
new(t) T(std::forward<Args>(args)...); --s;
return static_cast<T *>(t);
}
}
#endif /* LUAMM_HH */