From 9a5eedd11cc043fd09c96d57eb901fb85fc1655d Mon Sep 17 00:00:00 2001 From: pixel Date: Thu, 11 Dec 2003 16:53:27 +0000 Subject: Linux ports --- include/BLua.h | 628 ++++++++++++++++++++++++++-------------------------- include/LuaHandle.h | 88 ++++---- include/Makefile.am | 2 +- 3 files changed, 359 insertions(+), 359 deletions(-) (limited to 'include') diff --git a/include/BLua.h b/include/BLua.h index a0d33c1..53eb1bb 100644 --- a/include/BLua.h +++ b/include/BLua.h @@ -17,317 +17,317 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: BLua.h,v 1.8 2003-12-08 15:12:56 pixel Exp $ */ - -#ifndef __BLUA_H__ -#define __BLUA_H__ - -#include -#include -#include -#include - -class Lua : public Base { - public: - Lua(); - Lua(const Lua &) throw (GeneralException); - virtual ~Lua(); - void open_base(); - void open_table(); - void open_io(); - void open_string(); - void open_math(); - void open_debug(); - void declarefunc(const String &, lua_CFunction, int = LUA_GLOBALSINDEX); - void call(const String &, int = LUA_GLOBALSINDEX, int = 0, int = 0); - void call(int = 0, int = 0); - void push(); - void push(lua_Number); - void push(const String &); - void push(bool); - void push(char *, int size = -1); - void push(void *); - void push(lua_CFunction, int = 0); - void pop(int = 1); - void newtable(); - void settable(int = -3, bool raw = false); - void gettable(int = -2, bool raw = false); - void setvar(); - int gettop(); - void error(const String &); - int type(int = -1); - bool isnil(int = -1); - bool isboolean(int = -1); - bool isnumber(int = -1); - bool isstring(int = -1); - bool istable(int = -1); - bool isfunction(int = -1); - bool iscfunction(int = -1); - bool isuserdata(int = -1); - bool islightuserdata(int = -1); - bool toboolean(int = -1); - lua_Number tonumber(int = -1); - String tostring(int = -1); - lua_CFunction tocfunction(int = -1); - void * touserdata(int = -1); - Lua * tothread(int = -1); - void load(Handle *, bool docall = true) throw (GeneralException); - void dump(Handle *, bool strip = true); - Lua * thread(); - static Lua * find(lua_State *) throw (GeneralException); - void showerror(); - int getmetatable(int = -1); - int setmetatable(int = -2); - private: - Lua(lua_State *); - lua_State * L; - static std::map lualist; -}; - -class LuaObject : public Base { - public: - LuaObject() : wantdestruct(false), pushed(false) {} - virtual void push(Lua *) throw (GeneralException); - static void * getme(Lua *, int = 1); - void pushdestruct(Lua *) throw (GeneralException); - protected: - virtual void pushmembers(Lua *) = 0; - void pushme(Lua *, void *); - static void pushit(Lua *, const String &, lua_CFunction); - static void pushmeta(Lua *, const String &, lua_CFunction); - bool wantdestruct, pushed; -}; - -enum Lua_types_t { - LUA_ANY = 0, - LUA_OBJECT, - LUA_TABLE, - LUA_BOOLEAN, - LUA_NUMBER, - LUA_STRING, - LUA_FUNCTION, -}; - -#define MAXARGS 32 - -struct lua_functypes_t { - int number; - char * name; - int minargs, maxargs; - int argtypes[MAXARGS]; -}; - -#define DECLARE_METHOD(classname, enumvar) static int method_##enumvar(lua_State * L) { \ - return LuaHelpers::method_multiplex( \ - enumvar, \ - L, \ - sLua_##classname::##classname##_proceed, \ - 0, \ - classname##_methods, \ - true); \ - } - -#define DECLARE_FUNCTION(classname, enumvar) static int function_##enumvar(lua_State * L) { \ - return LuaHelpers::method_multiplex( \ - enumvar, \ - L, \ - 0, \ - sLua_##classname::##classname##_proceed_statics, \ - classname##_functions, \ - false); \ - } - -#define PUSH_METHOD(classname, enumvar) pushit( \ - L, \ - classname##_methods[enumvar].name, \ - sLua_##classname##::method_##enumvar) - -#define PUSH_METAMETHOD(classname, enumvar) pushmeta( \ - L, \ - String("__") + classname##_methods[enumvar].name, \ - sLua_##classname##::method_##enumvar) - -#define PUSH_FUNCTION(classname, enumvar) L->declarefunc( \ - classname##_functions[enumvar].name, \ - sLua_##classname##::function_##enumvar) - - -#define CHECK_METHODS(classname) { \ - int i = 0; \ - while (classname##_methods[i].number != -1) { \ - if (i != classname##_methods[i].number) { \ - throw GeneralException("Datas of " #classname "_methods inconsistants!"); \ - } \ - i++; \ - } \ -} - -#define CHECK_FUNCTIONS(classname) { \ - int i = 0; \ - while (classname##_functions[i].number != -1) { \ - if (i != classname##_functions[i].number) { \ - throw GeneralException("Datas of " #classname "_functions inconsistants!"); \ - } \ - i++; \ - } \ -} - -template -class LuaHelpers : public Base { - public: - static int method_multiplex(int caller, lua_State * _L, int (*proceed)(Lua * L, int n, T * obj, int caller), int (*proceed_static)(Lua * L, int n, int caller), lua_functypes_t * tab, bool method) { - Lua * L = Lua::find(_L); - int add = method ? 1 : 0; - int n = L->gettop() - add; - T * obj = (T *) LuaObject::getme(L); - int i; - bool invalid = false; - - if ((n < tab[caller].minargs) || (n > tab[caller].maxargs)) { - invalid = true; - } else { - for (i = 0; i < tab[caller].maxargs && !invalid; i++) { - if (n >= (i + 1)) { - switch(tab[caller].argtypes[i]) { - case LUA_ANY: - break; - case LUA_OBJECT: - if (L->istable(i + 1 + add)) { - L->push("__obj"); - L->gettable(i + 1 + add); - invalid = !L->islightuserdata(); - L->pop(); - } else { - invalid = !L->isnil(i + 1 + add); - } - break; - case LUA_TABLE: - invalid = !L->istable(i + 1 + add); - break; - case LUA_BOOLEAN: - invalid = !L->isboolean(i + 1 + add); - break; - case LUA_NUMBER: - invalid = !L->isnumber(i + 1 + add); - break; - case LUA_STRING: - invalid = !L->isstring(i + 1 + add); - break; - case LUA_FUNCTION: - invalid = !L->isfunction(i + 1 + add); - break; - } - } - } - } - - if (invalid) { - if (method) { - L->error(String("Invalid arguments to method `") + typeid(T).name() + "::" + tab[caller].name + "'"); - } else { - L->error(String("Invalid arguments to function `") + typeid(T).name() + "::" + tab[caller].name + "'"); - } - } - - if (method) { - return proceed(L, n, obj, caller); - } else { - return proceed_static(L, n, caller); - } - } -}; - - - /*******************************\ -|** Let's have a sample of use **| - \*******************************/ - -#ifdef THIS_IS_A_SAMPLE_WHICH_DOES_NOT_COMPILE -Luacdfile::Luacdfile(cdfile * h) : LuaHandle(h) { } - -enum cdfile_methods_t { - CDFILE_XXX = 0, - CDFILE_YYY -}; - -enum cdfile_functions_t { - CDFILE_NEWCDFILE = 0, -}; - -struct lua_functypes_t cdutils_methods[] = { - { CDFILE_XXX, "xxx", 1, 1, {LUA_OBJECT} }, - { CDFILE_YYY, "yyy", 0, 2, {LUA_NUMBER, LUA_NUMBER} }, - { -1, 0, 0, 0, 0 } -}; - -struct lua_functypes_t cdfile_functions[] = { - { CDFILE_NEWCDFILE, "cdfile", 1, 4, {LUA_OBJECT, LUA_ANY, LUA_NUMBER, LUA_NUMBER} }, - { -1, 0, 0, 0, 0 } -}; - -class sLua_cdfile : public Base { - public: - static int newcdfile(lua_State * L); - DECLARE_METHOD(cdfile, CDFILE_XXX); - DECLARE_METHOD(cdfile, CDFILE_YYY); - DECLARE_FUNCTION(cdfile, CDFILE_NEWCDFILE); - private: - static int cdfile_proceed(Lua * L, int n, cdfile * obj, int caller); - static int cdfile_proceed_statics(Lua * L, int n, int caller); -}; - -void Luacdfile::pushmembers(Lua * L) { - { - LuaHandle::pushmembers(L); - or - pushme(L, SomeObject); - } - PUSH_METHOD(cdfile, CDFILE_XXX); - PUSH_METHOD(cdfile, CDFILE_YYY); -} - -void Luacdfile::pushstatics(Lua * L) { - CHECK_METHODS(cdfile); - CHECK_FUNCTIONS(cdfile); - - PUSH_FUNCTION(cdfile, CDFILE_NEWCDFILE); -} - -int sLua_cdfile::cdfile_proceed(Lua * L, int n, cdfile * cdfile, int caller) { - int r = 0; - SomeObj * obj; - int arg1 = DEFAULT1, arg2 = DEFAULT2; - - switch(caller) { - case CDFILE_XXX: - obj = (SomeObj *) LuaObject::getme(L, 2); - cdfile->xxx(obj); - break; - case CDFILE_YYY: - if (n >= 1) - arg1 = L->tonumber(2); - if (n >= 2) - arg2 = L->tonumber(3); - L->push((lua_Number) cdfile->yyy(arg1, arg2)); - r = 1; - break; - } - - return r; -} - -int sLua_cdfile::cdfile_proceed_statics(Lua * L, int n, int caller) { - int r = 0; - - switch(caller) { - case CDFILE_NEWCDFILE: - /****TODO****/ - break; - } - - return r; -} - -#endif - -#endif +/* $Id: BLua.h,v 1.9 2003-12-11 16:53:27 pixel Exp $ */ + +#ifndef __BLUA_H__ +#define __BLUA_H__ + +#include +#include +#include +#include + +class Lua : public Base { + public: + Lua(); + Lua(const Lua &) throw (GeneralException); + virtual ~Lua(); + void open_base(); + void open_table(); + void open_io(); + void open_string(); + void open_math(); + void open_debug(); + void declarefunc(const String &, lua_CFunction, int = LUA_GLOBALSINDEX); + void call(const String &, int = LUA_GLOBALSINDEX, int = 0, int = 0); + void call(int = 0, int = 0); + void push(); + void push(lua_Number); + void push(const String &); + void push(bool); + void push(char *, int size = -1); + void push(void *); + void push(lua_CFunction, int = 0); + void pop(int = 1); + void newtable(); + void settable(int = -3, bool raw = false); + void gettable(int = -2, bool raw = false); + void setvar(); + int gettop(); + void error(const String &); + int type(int = -1); + bool isnil(int = -1); + bool isboolean(int = -1); + bool isnumber(int = -1); + bool isstring(int = -1); + bool istable(int = -1); + bool isfunction(int = -1); + bool iscfunction(int = -1); + bool isuserdata(int = -1); + bool islightuserdata(int = -1); + bool toboolean(int = -1); + lua_Number tonumber(int = -1); + String tostring(int = -1); + lua_CFunction tocfunction(int = -1); + void * touserdata(int = -1); + Lua * tothread(int = -1); + void load(Handle *, bool docall = true) throw (GeneralException); + void dump(Handle *, bool strip = true); + Lua * thread(); + static Lua * find(lua_State *) throw (GeneralException); + void showerror(); + int getmetatable(int = -1); + int setmetatable(int = -2); + private: + Lua(lua_State *); + lua_State * L; + static std::map lualist; +}; + +class LuaObject : public Base { + public: + LuaObject() : wantdestruct(false), pushed(false) {} + virtual void push(Lua *) throw (GeneralException); + static void * getme(Lua *, int = 1); + void pushdestruct(Lua *) throw (GeneralException); + protected: + virtual void pushmembers(Lua *) = 0; + void pushme(Lua *, void *); + static void pushit(Lua *, const String &, lua_CFunction); + static void pushmeta(Lua *, const String &, lua_CFunction); + bool wantdestruct, pushed; +}; + +enum Lua_types_t { + LUA_ANY = 0, + LUA_OBJECT, + LUA_TABLE, + LUA_BOOLEAN, + LUA_NUMBER, + LUA_STRING, + LUA_FUNCTION, +}; + +#define MAXARGS 32 + +struct lua_functypes_t { + int number; + char * name; + int minargs, maxargs; + int argtypes[MAXARGS]; +}; + +#define DECLARE_METHOD(classname, enumvar) static int method_##enumvar(lua_State * L) { \ + return LuaHelpers::method_multiplex( \ + enumvar, \ + L, \ + sLua_##classname::classname##_proceed, \ + 0, \ + classname##_methods, \ + true); \ + } + +#define DECLARE_FUNCTION(classname, enumvar) static int function_##enumvar(lua_State * L) { \ + return LuaHelpers::method_multiplex( \ + enumvar, \ + L, \ + 0, \ + sLua_##classname::classname##_proceed_statics, \ + classname##_functions, \ + false); \ + } + +#define PUSH_METHOD(classname, enumvar) pushit( \ + L, \ + classname##_methods[enumvar].name, \ + sLua_##classname::method_##enumvar) + +#define PUSH_METAMETHOD(classname, enumvar) pushmeta( \ + L, \ + String("__") + classname##_methods[enumvar].name, \ + sLua_##classname::method_##enumvar) + +#define PUSH_FUNCTION(classname, enumvar) L->declarefunc( \ + classname##_functions[enumvar].name, \ + sLua_##classname::function_##enumvar) + + +#define CHECK_METHODS(classname) { \ + int i = 0; \ + while (classname##_methods[i].number != -1) { \ + if (i != classname##_methods[i].number) { \ + throw GeneralException("Datas of " #classname "_methods inconsistants!"); \ + } \ + i++; \ + } \ +} + +#define CHECK_FUNCTIONS(classname) { \ + int i = 0; \ + while (classname##_functions[i].number != -1) { \ + if (i != classname##_functions[i].number) { \ + throw GeneralException("Datas of " #classname "_functions inconsistants!"); \ + } \ + i++; \ + } \ +} + +template +class LuaHelpers : public Base { + public: + static int method_multiplex(int caller, lua_State * _L, int (*proceed)(Lua * L, int n, T * obj, int caller), int (*proceed_static)(Lua * L, int n, int caller), lua_functypes_t * tab, bool method) { + Lua * L = Lua::find(_L); + int add = method ? 1 : 0; + int n = L->gettop() - add; + T * obj = (T *) LuaObject::getme(L); + int i; + bool invalid = false; + + if ((n < tab[caller].minargs) || (n > tab[caller].maxargs)) { + invalid = true; + } else { + for (i = 0; i < tab[caller].maxargs && !invalid; i++) { + if (n >= (i + 1)) { + switch(tab[caller].argtypes[i]) { + case LUA_ANY: + break; + case LUA_OBJECT: + if (L->istable(i + 1 + add)) { + L->push("__obj"); + L->gettable(i + 1 + add); + invalid = !L->islightuserdata(); + L->pop(); + } else { + invalid = !L->isnil(i + 1 + add); + } + break; + case LUA_TABLE: + invalid = !L->istable(i + 1 + add); + break; + case LUA_BOOLEAN: + invalid = !L->isboolean(i + 1 + add); + break; + case LUA_NUMBER: + invalid = !L->isnumber(i + 1 + add); + break; + case LUA_STRING: + invalid = !L->isstring(i + 1 + add); + break; + case LUA_FUNCTION: + invalid = !L->isfunction(i + 1 + add); + break; + } + } + } + } + + if (invalid) { + if (method) { + L->error(String("Invalid arguments to method `") + typeid(T).name() + "::" + tab[caller].name + "'"); + } else { + L->error(String("Invalid arguments to function `") + typeid(T).name() + "::" + tab[caller].name + "'"); + } + } + + if (method) { + return proceed(L, n, obj, caller); + } else { + return proceed_static(L, n, caller); + } + } +}; + + + /*******************************\ +|** Let's have a sample of use **| + \*******************************/ + +#ifdef THIS_IS_A_SAMPLE_WHICH_DOES_NOT_COMPILE +Luacdfile::Luacdfile(cdfile * h) : LuaHandle(h) { } + +enum cdfile_methods_t { + CDFILE_XXX = 0, + CDFILE_YYY +}; + +enum cdfile_functions_t { + CDFILE_NEWCDFILE = 0, +}; + +struct lua_functypes_t cdutils_methods[] = { + { CDFILE_XXX, "xxx", 1, 1, {LUA_OBJECT} }, + { CDFILE_YYY, "yyy", 0, 2, {LUA_NUMBER, LUA_NUMBER} }, + { -1, 0, 0, 0, 0 } +}; + +struct lua_functypes_t cdfile_functions[] = { + { CDFILE_NEWCDFILE, "cdfile", 1, 4, {LUA_OBJECT, LUA_ANY, LUA_NUMBER, LUA_NUMBER} }, + { -1, 0, 0, 0, 0 } +}; + +class sLua_cdfile : public Base { + public: + static int newcdfile(lua_State * L); + DECLARE_METHOD(cdfile, CDFILE_XXX); + DECLARE_METHOD(cdfile, CDFILE_YYY); + DECLARE_FUNCTION(cdfile, CDFILE_NEWCDFILE); + private: + static int cdfile_proceed(Lua * L, int n, cdfile * obj, int caller); + static int cdfile_proceed_statics(Lua * L, int n, int caller); +}; + +void Luacdfile::pushmembers(Lua * L) { + { + LuaHandle::pushmembers(L); + or + pushme(L, SomeObject); + } + PUSH_METHOD(cdfile, CDFILE_XXX); + PUSH_METHOD(cdfile, CDFILE_YYY); +} + +void Luacdfile::pushstatics(Lua * L) { + CHECK_METHODS(cdfile); + CHECK_FUNCTIONS(cdfile); + + PUSH_FUNCTION(cdfile, CDFILE_NEWCDFILE); +} + +int sLua_cdfile::cdfile_proceed(Lua * L, int n, cdfile * cdfile, int caller) { + int r = 0; + SomeObj * obj; + int arg1 = DEFAULT1, arg2 = DEFAULT2; + + switch(caller) { + case CDFILE_XXX: + obj = (SomeObj *) LuaObject::getme(L, 2); + cdfile->xxx(obj); + break; + case CDFILE_YYY: + if (n >= 1) + arg1 = L->tonumber(2); + if (n >= 2) + arg2 = L->tonumber(3); + L->push((lua_Number) cdfile->yyy(arg1, arg2)); + r = 1; + break; + } + + return r; +} + +int sLua_cdfile::cdfile_proceed_statics(Lua * L, int n, int caller) { + int r = 0; + + switch(caller) { + case CDFILE_NEWCDFILE: + /****TODO****/ + break; + } + + return r; +} + +#endif + +#endif diff --git a/include/LuaHandle.h b/include/LuaHandle.h index e9f844e..cd5ac63 100644 --- a/include/LuaHandle.h +++ b/include/LuaHandle.h @@ -17,47 +17,47 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: LuaHandle.h,v 1.4 2003-12-06 04:26:16 pixel Exp $ */ - -#ifndef __LUAHANDLE_H__ -#define __LUAHANDLE_H__ - -#include -#include -#include -#include -#include - -class LuaHandle : public LuaObject { - public: - LuaHandle(Handle *); - protected: - virtual void pushmembers(Lua *); - Handle * h; -}; - -class LuaInput : public LuaHandle { - public: - static void pushconstruct(Lua *); - LuaInput(Input *); - protected: - virtual void pushmembers(Lua *); -}; - -class LuaOutput : public LuaHandle { - public: - static void pushconstruct(Lua *); - LuaOutput(Output *); - protected: - virtual void pushmembers(Lua *); -}; - -class LuaBuffer : public LuaHandle { - public: - static void pushconstruct(Lua *); - LuaBuffer(Buffer *); - protected: - virtual void pushmembers(Lua *); -}; - -#endif +/* $Id: LuaHandle.h,v 1.5 2003-12-11 16:53:27 pixel Exp $ */ + +#ifndef __LUAHANDLE_H__ +#define __LUAHANDLE_H__ + +#include +#include +#include +#include +#include + +class LuaHandle : public LuaObject { + public: + LuaHandle(Handle *); + protected: + virtual void pushmembers(Lua *); + Handle * h; +}; + +class LuaInput : public LuaHandle { + public: + static void pushconstruct(Lua *); + LuaInput(Input *); + protected: + virtual void pushmembers(Lua *); +}; + +class LuaOutput : public LuaHandle { + public: + static void pushconstruct(Lua *); + LuaOutput(Output *); + protected: + virtual void pushmembers(Lua *); +}; + +class LuaBuffer : public LuaHandle { + public: + static void pushconstruct(Lua *); + LuaBuffer(Buffer *); + protected: + virtual void pushmembers(Lua *); +}; + +#endif diff --git a/include/Makefile.am b/include/Makefile.am index 3fc5f29..00f9ca0 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -2,6 +2,6 @@ pkginclude_HEADERS = \ Exceptions.h Handle.h BString.h Output.h Socket.h HttpServ.h Variables.h Menu.h \ Action.h Message.h Form.h Confirm.h Table.h IRC.h Task.h Buffer.h generic.h \ CopyJob.h ReadJob.h Regex.h TaskMan.h InPipe.h OutPipe.h Input.h Image.h \ -Main.h Color.h GMPString.h SQL.h ConfigFile.h +Main.h Color.h GMPString.h SQL.h ConfigFile.h BLua.h LuaHandle.h noinst_HEADERS = gettext.h -- cgit v1.2.3