aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-05-19 11:46:33 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:52:45 (GMT)
commitb4117e5dce073c02bf66b336b8eed1ce413cdc92 (patch)
treec1862ba3301c01b80f0ce00aeab469d680f5059d /src/FbTk
parent696039d730244f3543b2e0bed8cd384a9958d5ea (diff)
downloadfluxbox_pavel-b4117e5dce073c02bf66b336b8eed1ce413cdc92.zip
fluxbox_pavel-b4117e5dce073c02bf66b336b8eed1ce413cdc92.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.
Diffstat (limited to 'src/FbTk')
-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 }