Notes-9: The prototype of Lua Binder's other wheel

Source: Internet
Author: User

Using the new features of C ++ 0x to implement a Lua binder, this is a super prototype, basically implementing the removal and re-assembly of function parameters.

Press two real parameters from xlb_caller (simulating lua_state can provide us with real parameters)

template<class R, class...A> struct xLB_caller {};template<class R, class...A>struct xLB_caller<R (*)(A...)> { typedef R (*function_t)(A...);function_t __final;xLB_caller(function_t _final) : __final(_final) {__values.push(999);__values.push(333);}std::queue<int> __values;template<typename T>void loadarg(T& __v) {__v = __values.front();__values.pop();}};

Then, the variadic template is used to provide real parameters from xlb_caller one by one. After all the real parameters are removed, the system assembles them one by one and finally calls the function. The effect is similar.

int x(int a, int b) {int r = a-b;std::cout << "x(" << a << "," << b << ") -> " << std::dec << r << std::endl;return r;}int main() {xLB_wf<decltype(&x)> awx(&x);awx();return 0;}

Output result

C:\loonlib\bakefiles\function>f2x(999,333) -> 666result : 666

Complete code

#include <iostream>#include <functional>#include <queue>using namespace std;using namespace std::placeholders;/*============================================================================ * ============================================================================*/template<class R, class...A> struct xLB_caller {};template<class R, class...A>struct xLB_caller<R (*)(A...)> {     using function_t = R (*)(A...);    using return_t = R;    function_t __final;    xLB_caller(function_t _final) : __final(_final) {        __values.push(999);        __values.push(333);        __values.push(111);    }    std::queue<int> __values;    template<class T>void loadarg(T& __v) {        __v = __values.front();        __values.pop();    }};template<int N, class caller_t, class...A> struct xLB_getparam{};template<class caller_t, class...A>struct xLB_getparam<0, caller_t, A...> {     using return_t = typename caller_t::return_t;    caller_t* __caller;    xLB_getparam(caller_t* _caller) : __caller(_caller) {}    void operator()(){ /* N==0 no more param for getting */ };     template<class...VA> return_t part(VA..._args) {        return __caller->__final(_args...);    }};template<int N, class caller_t, class T, class ...A>struct xLB_getparam<N, caller_t, T, A...> {    using next_t = xLB_getparam<N-1, caller_t, A...>;    using return_t = typename caller_t::return_t;    caller_t* __caller;    next_t __next;    T __arg;    xLB_getparam(caller_t* _caller) : __caller(_caller), __next(_caller) {}    void operator()() {        __caller->loadarg(__arg);        __next();    }    template<class...VA> return_t part(VA...__args) {       return __next.part(__args..., __arg);     }    return_t part(){ return __next.part(__arg); }};template<class R> struct xLB_wf{};template<class R, class ...A>struct xLB_wf<R (*)(A...) > {    using function_t = R(*)(A...);    using caller_t = xLB_caller<R (*)(A...)>;    using geter_t = xLB_getparam<sizeof...(A), caller_t, A...>;    caller_t __caller;    geter_t __g;    xLB_wf(function_t _realfunc) : __caller(_realfunc), __g(&__caller)    {}    R operator()() { __g(); return __g.part(); }};template<class R, class S, class ...A>struct xLB_wf<R (S::*)(A...) > {};template<class X>auto xLB_bind(X f) -> xLB_wf<decltype(f)>{    return xLB_wf<decltype(f)>(f);}/*============================================================================ * ============================================================================*/int x(int a, int b, int c) {    int r = a-b-c;    std::cout << "x(" << a << "," << b << "," << c << ") -> " << std::dec << r << std::endl;    return r;}int main() {    //xLB_wf<decltype(&x)> awx(&x);    auto awx = xLB_bind(&x);    std::cout << "awx() -> " << awx() << std::endl;    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.