From b4117e5dce073c02bf66b336b8eed1ce413cdc92 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 19 May 2011 13:46:33 +0200 Subject: c++ lua binding: get rid of variadic templates See how variadic templates are good. They enabled me to write those four functions as one. --- src/FbTk/Luamm.hh | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 6b26a17..f39f3de 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh @@ -267,8 +267,14 @@ namespace lua { 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 - T* createuserdata(Args&&... args); + template + T* createuserdata(); + template + T* createuserdata(const Arg1 &arg1); + template + T* createuserdata(const Arg1 &arg1, const Arg2 &arg2); + template + T* createuserdata(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3); }; /* @@ -303,13 +309,46 @@ namespace lua { void operator-=(int n_) throw() { n-=n_; assert(n >= 0); } }; - template - T* state::createuserdata(Args&&... args) + template + T* state::createuserdata() + { + stack_sentry s(*this); + + void *t = newuserdata(sizeof(T)); + new(t) T; + ++s; + return static_cast(t); + } + + template + T* state::createuserdata(const Arg1 &arg1) + { + stack_sentry s(*this); + + void *t = newuserdata(sizeof(T)); + new(t) T(arg1); + ++s; + return static_cast(t); + } + + template + T* state::createuserdata(const Arg1 &arg1, const Arg2 &arg2) + { + stack_sentry s(*this); + + void *t = newuserdata(sizeof(T)); + new(t) T(arg1, arg2); + ++s; + return static_cast(t); + } + + template + T* state::createuserdata(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) { stack_sentry s(*this); void *t = newuserdata(sizeof(T)); - new(t) T(std::forward(args)...); + new(t) T(arg1, arg2, arg3); ++s; return static_cast(t); } -- cgit v0.11.2