|
|
发表于 2007-1-12 01:28:00
|
显示全部楼层
Re: 模板产生式编程,奇技淫巧也是临门一脚
黑猫白猫 能抓住老鼠就是好猫
楼主 赞一个。
之前小弟我也写了一个 从C++ 调用lua的封装。
挺漂亮,可惜不是很实用。所以最后就没用上。
使用方法是
lua_State * L = luaL_newstate();;
LuaFunction lua(L);
lua::exe(“function1”,result, parameter0, parameter1) ;//六个以内参数,常用数据类型支持。
里面都是使用内联和模板,效率不差,展开代码也都和手写一样,只是好看一些。
- #ifndef LUAFUNCTION_H
- #define LUAFUNCTION_H
- extern "C" {
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- }
- class VOID{};
- class NUL{};
- template<class Q>
- class Push{};
- template<>
- class Push<int>
- {
- public:
- inline static void push(lua_State * L, int q)
- {
- lua_pushnumber(L, q);
- }
- };
- template<>
- class Push<float>
- {
- public:
- inline static void push(lua_State * L, int q)
- {
- lua_pushnumber(L, q);
- }
- };
- template<>
- class Push<bool>
- {
- public:
- inline static void push(lua_State * L, bool q)
- {
- lua_pushboolean(L, q);
- }
- };
- template<>
- class Push<const char *>
- {
- public:
- inline static void push(lua_State * L, const char * q)
- {
- lua_pushstring(L, q);
- }
- };
- template<>
- class Push<char *>
- {
- public:
- inline static void push(lua_State * L, const char * q)
- {
- Push<const char *>::push(L, q);
- //lua_pushstring(L, q);
- }
- };
- template<>
- class Push<std::string>
- {
- public:
- inline static void push(lua_State * L, std::string & q)
- {
- Push<const char *>::push(L, q.c_str());
- //lua_pushstring(L, q.c_str());
- }
- };
- template<>
- class Push<const NUL>
- {
- public:
- inline static void push(lua_State * L, NUL)
- {
- lua_pushnil(L);
- }
- };
- template<class R>
- class Pop{};
- template<>
- class Pop<int>
- {
- public:
- inline static void pop(lua_State * L, int &r,int n)
- {
- lua_pcall(L, n, 1, 0);
- r = (int)lua_tonumber(L, -1);
- }
- };
- template<>
- class Pop<float>
- {
- public:
- inline static void pop(lua_State * L, float &r,int n)
- {
- lua_pcall(L, n, 1, 0);
- r = (float)lua_tonumber(L, -1);
- }
- };
- template<>
- class Pop<bool>
- {
- public:
- inline static void pop(lua_State * L, bool &r,int n)
- {
- lua_pcall(L, n, 1, 0);
- r = lua_toboolean(L, -1);
- }
- };
- template<>
- class Pop<std::string>
- {
- public:
- inline static void pop(lua_State * L, std::string &r,int n)
- {
- lua_pcall(L, n, 1, 0);
- r = lua_tostring(L, -1);
- }
- };
- template<>
- class Pop<char *>
- {
- public:
- inline static void pop(lua_State * L, char *r,int n)
- {
- lua_pcall(L, n, 1, 0);
- strcpy(r, lua_tostring(L, -1));
-
- }
- };
- template<>
- class Pop<const VOID>
- {
- public:
- inline static void pop(lua_State * L, const VOID &r,int n)
- {
- lua_pcall(L, n, 0, 0);
- }
- };
- class LuaFunction
- {
- private:
- lua_State * _L;
- public :
- const static VOID VOID;
- const static NUL NUL;
- inline LuaFunction(lua_State * L):_L(L){}
- template<class R>
- inline void exe(const char * fun, R &r)
- {
- lua_getglobal(_L, fun);
- Pop<R>::pop(_L, r, 0);
- }
- template<class R, class P0>
- inline void exe(const char * fun, R &r, P0 p0)
- {
- lua_getglobal(_L, fun);
- Push<P0>::push(_L, p0);
- Pop<R>::pop(_L, r,1);
- }
- template<class R, class P0,class P1>
- inline void exe(const char * fun, R &r, P0 p0, P1 p1)
- {
- lua_getglobal(_L, fun);
- Push<P0>::push(_L, p0);
- Push<P1>::push(_L, p1);
- Pop<R>::pop(_L, r, 2);
- }
- template<class R, class P0,class P1,class P2>
- inline void exe(const char * fun, R &r, P0 p0, P1 p1,P2 p2)
- {
- lua_getglobal(_L, fun);
- Push<P0>::push(_L, p0);
- Push<P1>::push(_L, p1);
- Push<P2>::push(_L, p2);
- Pop<R>::pop(_L, r, 3);
- }
- template<class R, class P0,class P1,class P2, class P3>
- inline void exe(const char * fun, R &r, P0 p0, P1 p1, P2 p2, P3 p3)
- {
- lua_getglobal(_L, fun);
- Push<P0>::push(_L, p0);
- Push<P1>::push(_L, p1);
- Push<P2>::push(_L, p2);
- Push<P3>::push(_L, p3);
- Pop<R>::pop(_L, r, 4);
- }
- template<class R, class P0,class P1,class P2, class P3, class P4>
- inline void exe(const char * fun, R &r, P0 p0, P1 p1, P2 p2, P3 p3, P4 p4)
- {
- lua_getglobal(_L, fun);
- Push<P0>::push(_L, p0);
- Push<P1>::push(_L, p1);
- Push<P2>::push(_L, p2);
- Push<P3>::push(_L, p3);
- Push<P4>::push(_L, p4);
- Pop<R>::pop(_L, r, 5);
- }
- template<class R, class P0,class P1,class P2, class P3, class P4, class P5>
- inline void exe(const char * fun, R &r, P0 p0, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
- {
- lua_getglobal(_L, fun);
- Push<P0>::push(_L, p0);
- Push<P1>::push(_L, p1);
- Push<P2>::push(_L, p2);
- Push<P3>::push(_L, p3);
- Push<P4>::push(_L, p4);
- Push<P5>::push(_L, p5);
- Pop<R>::pop(_L, r, 6);
- }
- };
- const VOID LuaFunction::VOID;
- const NUL LuaFunction::NUL;
- #endif
复制代码
虽然没有用上,但在这里供大家批评吧。Loki里面的函数对象比较好,任意参数类型任意数量。有时间能适配一下Lua应该不错。 |
|