Lua note-implementation of a class that calls the Lua function in C ++

Source: Internet
Author: User

This class is basically obtained from luaplus. Why does it not support luaplus? Because I really don't know how to compile the old version that can be compiled, in addition, the previous versions have bugs, and there are shadows in use. If you don't want to use them, you can write it by yourself.

It seems that luaplus's luafunction is a little difficult to use, so we have made some changes. I think this encapsulated by myself is still quite useful.

 

Namespace lua_wrapper {struct luanil {}; inline void push (lua_state * l, bool value) {lua_pushboolean (L, value);} inline void push (lua_state * l, char value) {lua_pushnumber (L, value);} inline void push (lua_state * l, unsigned char value) {lua_pushnumber (L, value);} inline void push (lua_state * l, short value) {lua_pushnumber (L, value);} inline void push (lua_state * l, unsigned short value) {lua_pushnumber (L, value);} inline void push (lua_state * l, int value) {lua_pushnumber (L, value);} inline void push (lua_state * l, unsigned int value) {lua_pushnumber (L, value);} inline void push (lua_state * l, long value) {lua_pushnumber (L, value);} inline void push (lua_state * l, unsigned long value) {lua_pushnumber (L, value);} inline void push (lua_state * l, double value) {lua_pushnumber (L, (lua_number) value);} in Line void push (lua_state * l, float value) {lua_pushnumber (L, (lua_number) value);} inline void push (lua_state * l, const char * value) {lua_pushstring (L, value);} inline void push (lua_state * l, const luanil &) {lua_pushnil (l);} inline void push (lua_state * l, lua_cfunction value) {lua_pushcclosure (L, value, 0);} inline void push (lua_state * l, const void * value) {lua_pushlightuserdata (L, (void *) value );} Struct get {lua_state * l; int idx; get (lua_state * l, int Index =-1): L (L), idx (INDEX) {} operator bool () {assert (lua_isboolean (L, idx); Return lua_toboolean (L, idx )! = 0;} operator char () {assert (lua_isnumber (L, idx); Return static_cast <char> (lua_tonumber (L, idx);} operator unsigned char () {assert (lua_isnumber (L, idx); Return static_cast <unsigned char> (lua_tonumber (L, idx);} operator short () {assert (lua_isnumber (L, idx); Return static_cast <short> (lua_tonumber (L, idx);} operator unsigned short () {assert (lua_isnumber (L, idx )); return static_cast <unsigned short> (lua_tonumbe R (L, idx);} operator int () {assert (lua_isnumber (L, idx); Return static_cast <int> (lua_tonumber (L, idx ));} operator unsigned int () {assert (lua_isnumber (L, idx); Return static_cast <unsigned int> (lua_tonumber (L, idx);} operator long () {assert (lua_isnumber (L, idx); Return static_cast <long> (lua_tonumber (L, idx);} operator unsigned long () {assert (lua_isnumber (L, idx); Return static_cast <unsigned long> (lua_tonumbe R (L, idx);} operator double () {assert (lua_isnumber (L, idx); Return static_cast <double> (lua_tonumber (L, idx ));} operator float () {assert (lua_isnumber (L, idx); Return static_cast <float> (lua_tonumber (L, idx);} operator const char *() {assert (lua_isstring (L, idx); Return static_cast <const char *> (lua_tostring (L, idx);} operator lua_cfunction () {assert (lua_iscfunction (L, idx); Return static_cast <lua_cfunction> (Lu A_tocfunction (L, idx);} operator void * () {assert (lua_islightuserdata (L, idx); Return static_cast <void *> (lua_touserdata (L, idx) ;}};} class luaautoblock {public: luaautoblock (lua_state * l): L (L), m_stacktop (lua_gettop (L )){}~ Luaautoblock () {lua_settop (L, m_stacktop);} PRIVATE: luaautoblock (const luaautoblock & SRC); // not implementedconst luaautoblock & operator = (const luaautoblock & SRC ); // not implementedlua_state * l; int m_stacktop;}; struct callluafunction {lua_state * l; luaautoblock autoblock; callluafunction (lua_state * l, const char * fnname): L (L ), autoblock (l) {lua_getglobal (L, fnname); If (lua_pcall (L, 0, 0) {logluaerror (l );}} template <class T1> callluafunction (lua_state * l, const char * fnname, T1 T1): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper :: push (L, T1); If (lua_pcall (L, 1, 1, 0) {logluaerror (l) ;}template <class T1, class T2> callluafunction (lua_state * l, const char * fnname, T1 T1, T2 T2): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1 ); lua_wrapper: Push (L, T2); If (lua_pcall (l, 2, 1, 0) {logluaerror (l) ;}} template <class T1, class T2, class T3> callluafunction (lua_state * l, const char * fnname, T1 T1, T2 T2, T3 T3): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1); lua_wrapper: Push (L, T2); lua_wrapper: Push (L, T3); If (lua_pcall (L, 3, 1, 0) {logluaerror (l) ;}template <class T1, class T2, class T3, class T4> callluafunction (lua_state * l, const char * fnname, t1 T1, T2 T2, T3 T3, T4 T4): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1 ); lua_wrapper: Push (L, T2); lua_wrapper: Push (L, T3); lua_wrapper: Push (L, T4); If (lua_pcall (L, 4,1, 0) {logluaerror (l) ;}template <class T1, class T2, class T3, class T4, class T5> callluafunction (lua_state * l, const char * fnname, t1 T1, T2 T2, T3 T3, T4 T4, T5 t5): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, t1); lua_wrapper: Push (L, T2); lua_wrapper: Push (L, T3); lua_wrapper: Push (L, T4); lua_wrapper: Push (L, T4, t5); If (lua_pcall (L, 5, 1, 0) {logluaerror (l) ;}} template <class T1, class T2, class T3, class T4, class T5, class T6> callluafunction (lua_state * l, const char * fnname, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6): L (L ), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1); lua_wrapper: Push (L, T2); lua_wrapper: Push (L, T2, t3); lua_wrapper: Push (L, T4); lua_wrapper: Push (L, T5); lua_wrapper: Push (L, T6); If (lua_pcall (L, 6, 1, 0) {logluaerror (l) ;}template <class T1, class T2, class T3, class T4, class T5, class T6, class T7> callluafunction (lua_state * l, const char * fnname, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6, T7 T7 ): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1); lua_wrapper: Push (L, T2); lua_wrapper :: push (L, T3); lua_wrapper: Push (L, T4); lua_wrapper: Push (L, T5); lua_wrapper: Push (L, T6); lua_wrapper :: push (L, T7); If (lua_pcall (L, 7,1, 0) {logluaerror (l) ;}} template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8> callluafunction (lua_state * l, const char * fnname, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, t6 T6, T7 T7, T8 T8): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1); lua_wrapper :: push (L, T2); lua_wrapper: Push (L, T3); lua_wrapper: Push (L, T4); lua_wrapper: Push (L, T5); lua_wrapper :: push (L, T6); lua_wrapper: Push (L, T7); lua_wrapper: Push (L, T8); If (lua_pcall (L, 8, 1, 0 )) {logluaerror (l) ;}} template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9> callluafunction (lua_state * l, const char * fnname, T1 T1, T2 T2, T3 T3, T4 T4, T5 T5, T6 T6, T7 T7, T8 T8, t9 T9): L (L), autoblock (l) {lua_getglobal (L, fnname); lua_wrapper: Push (L, T1); lua_wrapper: Push (L, t2); lua_wrapper: Push (L, T3); lua_wrapper: Push (L, T4); lua_wrapper: Push (L, T5); lua_wrapper: Push (L, T5, t6); lua_wrapper: Push (L, T7); lua_wrapper: Push (L, T8); lua_wrapper: Push (L, T9); If (lua_pcall (L, 9,1, 0) {logluaerror (l) ;}} template <class RT> operator RT () {return lua_wrapper: Get (l );}};

Usage:

 

 

 
Lual_dostring (L, "function test (...) print (...) return 'OK' end "); const char * ret = callluafunction (L," test "," A "," B ", 3, & A, 5," 666 ", 7, "888", 998); // call the Lua function printf ("RET: % s \ n", RET );

It looks like a function, but the problem is that too many calls may lead to Lua full stack. I suspect that the previous luaplus bug is true. If it is just a local variable, there is no problem, it will be cleared automatically. This problem is difficult to handle. Currently, we can think of a void version like luaplus.

 

 

Related Article

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.