aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-05-19 11:46:33 (GMT)
committerPaul Tagliamonte <paultag@fluxbox.org>2012-04-07 02:10:13 (GMT)
commit804743183e0de7fd46b7e04a0751ed1d6f61aad4 (patch)
treeee33832a368f2fc930f6fe22d1fe8185ce42bdf9
parentab0cc1975cd0834dfbff07724f7c182429a62616 (diff)
downloadfluxbox_paul-804743183e0de7fd46b7e04a0751ed1d6f61aad4.zip
fluxbox_paul-804743183e0de7fd46b7e04a0751ed1d6f61aad4.tar.bz2
c++ lua binding: get rid of variadic templates
See how variadic templates are good. They enabled me to write those four functions as one.
-rw-r--r--src/FbTk/Luamm.hh49
1 files 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 {
267 std::string tostring(int index) throw(lua::not_string_error); 267 std::string tostring(int index) throw(lua::not_string_error);
268 // allocate a new lua userdata of appropriate size, and create a object in it 268 // allocate a new lua userdata of appropriate size, and create a object in it
269 // pushes the userdata on stack and returns the pointer 269 // pushes the userdata on stack and returns the pointer
270 template<typename T, typename... Args> 270 template<typename T>
271 T* createuserdata(Args&&... args); 271 T* createuserdata();
272 template<typename T, typename Arg1>
273 T* createuserdata(const Arg1 &arg1);
274 template<typename T, typename Arg1, typename Arg2>
275 T* createuserdata(const Arg1 &arg1, const Arg2 &arg2);
276 template<typename T, typename Arg1, typename Arg2, typename Arg3>
277 T* createuserdata(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3);
272 }; 278 };
273 279
274 /* 280 /*
@@ -303,13 +309,46 @@ namespace lua {
303 void operator-=(int n_) throw() { n-=n_; assert(n >= 0); } 309 void operator-=(int n_) throw() { n-=n_; assert(n >= 0); }
304 }; 310 };
305 311
306 template<typename T, typename... Args> 312 template<typename T>
307 T* state::createuserdata(Args&&... args) 313 T* state::createuserdata()
314 {
315 stack_sentry s(*this);
316
317 void *t = newuserdata(sizeof(T));
318 new(t) T;
319 ++s;
320 return static_cast<T *>(t);
321 }
322
323 template<typename T, typename Arg1>
324 T* state::createuserdata(const Arg1 &arg1)
325 {
326 stack_sentry s(*this);
327
328 void *t = newuserdata(sizeof(T));
329 new(t) T(arg1);
330 ++s;
331 return static_cast<T *>(t);
332 }
333
334 template<typename T, typename Arg1, typename Arg2>
335 T* state::createuserdata(const Arg1 &arg1, const Arg2 &arg2)
336 {
337 stack_sentry s(*this);
338
339 void *t = newuserdata(sizeof(T));
340 new(t) T(arg1, arg2);
341 ++s;
342 return static_cast<T *>(t);
343 }
344
345 template<typename T, typename Arg1, typename Arg2, typename Arg3>
346 T* state::createuserdata(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3)
308 { 347 {
309 stack_sentry s(*this); 348 stack_sentry s(*this);
310 349
311 void *t = newuserdata(sizeof(T)); 350 void *t = newuserdata(sizeof(T));
312 new(t) T(std::forward<Args>(args)...); 351 new(t) T(arg1, arg2, arg3);
313 ++s; 352 ++s;
314 return static_cast<T *>(t); 353 return static_cast<T *>(t);
315 } 354 }