C + + calls LUA function, variable parameter template implementation

Source: Internet
Author: User
Tags lua

Code:

FUNCTION template:luax_vcalltemplate<typename..._args> inlinevoid luax_vcall (const char* func, _Args...args ); Template<typename _result, typename..._args> inline_result luax_vxcall (const char* func, _Args...args);/// TEMPLATE luax_vxcall aliastemplate<typename..._args> inlineint luax_vicall (const char* func, _args...args); template<typename..._args> inlinefloat luax_vfcall (const char* func, _args...args); template<typename..._ args> inlinedouble luax_vdcall (const char* func, _args...args);template<typename..._args> inlinestd::string Luax_vvcall (const char* func, _args...args);///ARG push helperinlinevoid Luax_vpusharg (lua_state* L, int& Carg, int& Amp Narg) {}template<typename _ty> inlinevoid luax_vpusharg (lua_state* L, int& carg, int& narg, _Ty Arg) {stat Ic_assert (False, "error, luax_unpack:unsupported type!");} Template<> inlinevoid luax_vpusharg<int> (lua_state* L, int& carg, int& narg, int arg) {+ +Carg; if (Lua_checkstack (l, 1)) Lua_pushinteger (L, Arg), ++narg;}    Template<> inlinevoid luax_vpusharg<float> (lua_state* L, int& carg, int& narg, float arg) {++carg; if (Lua_checkstack (l, 1)) Lua_pushnumber (L, Arg), ++narg;} Template<> inlinevoid luax_vpusharg<double> (lua_state* L, int& carg, int& narg, double arg) {++carg    ; if (Lua_checkstack (l, 1)) Lua_pushnumber (L, Arg), ++narg;} Template<> inlinevoid luax_vpusharg<const char*> (lua_state* L, int& carg, int& narg, const char* ARG)    {++carg; if (Lua_checkstack (l, 1)) lua_pushstring (L, Arg), ++narg;} Template<> inlinevoid luax_vpusharg<const std::string&> (lua_state* L, int& carg, int& Narg,    Const std::string& ARG) {++carg; if (Lua_checkstack (l, 1)) lua_pushlstring (L, Arg.c_str (), Arg.length ()), ++narg;} Template<typename _ty, typename..._args> inlinevoid luax_vpusharg (lua_state* L, int& Carg, int& Narg, _ty arg1, _args...args) {Luax_vpusharg (L, Carg, Narg, arg1); Luax_vpusharg (L, Carg, Narg, args ...);} Template<typename _ty> inline_ty luax_getretval (lua_state* L);template<> inlineint luax_getretval<int    > (lua_state* L) {if (Lua_isnumber (L,-1)) {return Lua_tointeger (L,-1); } return 0;} Template<> inlinefloat luax_getretval<float> (lua_state* l) {if (Lua_isnumber (L,-1)) {return Lua_ton    Umber (L,-1); } return 0;} Template<> inlinedouble luax_getretval<double> (lua_state* l) {if (Lua_isnumber (L,-1)) {return lua_t    Onumber (L,-1); } return 0;} Template<> inlinestd::string luax_getretval<std::string> (lua_state* l) {if (Lua_isstring (L,-1)) {RE    Turn lua_tostring (L,-1); } return 0;}    template<typename..._args> inlinevoid luax_vcall (const char* func, _args...args) {Auto L = luax_get_l (); Auto top = lua_gettop (L); Store Stack Lua_getglobal (s_intriluas, func);    if (!lua_isfunction (L,-1)) {goto err_exit;    } int carg = 0, narg = 0;    Luax_vpusharg (L, Carg, Narg, args ...);    if (carg! = Narg) {goto err_exit;    } if (Lua_pcall (L, narg, 0, 0)! = 0) {goto err_exit; } lua_settop (L, top); Resume Stackerr_exit:lua_settop (L, top); Resume Stack}template<typename _result, typename..._args> inline_result luax_vxcall (const char* func, _Args ...    args) {Auto L = luax_get_l (); Auto top = lua_gettop (L);    Store Stack _result Result;    Lua_getglobal (S_intriluas, func);    if (!lua_isfunction (L,-1)) {goto err_exit;    } int carg = 0, narg = 0;    Luax_vpusharg (L, Carg, Narg, args ...);    if (carg! = Narg) {goto err_exit;    } if (Lua_pcall (L, Narg, 1, 0)! = 0) {goto err_exit;    } result = Luax_getretval<_result> (L); Lua_settop (L, top); Resume stack return std::move (result); Err_exit:lua_settop (L, top); ResUme Stack return _result ();} TEMPLATE luax_vxcall aliastemplate<typename..._args> inlineint luax_vicall (const char* func, _args...args) {R Eturn luax_vxcall<int> (func, args ...);} template<typename..._args> inlinefloat luax_vfcall (const char* func, _args...args) {return luax_vxcall<float > (func, args ...);} template<typename..._args> inlinedouble luax_vdcall (const char* func, _args...args) {return Luax_vxcall<doub Le> (func, args ...);} template<typename..._args> inlinestd::string luax_vvcall (const char* func, _args...args) {return luax_vxcall&lt ;std::string> (func, args ...);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + calls LUA function, variable parameter template implementation

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.