luaclass: import class system

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-06-08 17:28:20 +02:00
parent dea2b6303a
commit 4003ef726f
3 changed files with 129 additions and 0 deletions

View File

@ -74,6 +74,7 @@ set(AWE_SRCS
${SOURCE_DIR}/common/xcursor.c ${SOURCE_DIR}/common/xcursor.c
${SOURCE_DIR}/common/backtrace.c ${SOURCE_DIR}/common/backtrace.c
${SOURCE_DIR}/common/luaobject.c ${SOURCE_DIR}/common/luaobject.c
${SOURCE_DIR}/common/luaclass.c
${SOURCE_DIR}/widgets/graph.c ${SOURCE_DIR}/widgets/graph.c
${SOURCE_DIR}/widgets/progressbar.c ${SOURCE_DIR}/widgets/progressbar.c
${SOURCE_DIR}/widgets/textbox.c ${SOURCE_DIR}/widgets/textbox.c

61
common/luaclass.c Normal file
View File

@ -0,0 +1,61 @@
/*
* luaclass.c - useful functions for handling Lua classes
*
* Copyright © 2009 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "common/luaclass.h"
#include "common/luaobject.h"
void
luaA_class_add_signal(lua_State *L, lua_class_t *lua_class,
const char *name, int ud)
{
luaA_checkfunction(L, ud);
signal_add(&lua_class->signals, name, luaA_object_ref(L, ud));
}
void
luaA_class_remove_signal(lua_State *L, lua_class_t *lua_class,
const char *name, int ud)
{
luaA_checkfunction(L, ud);
void *ref = (void *) lua_topointer(L, ud);
signal_remove(&lua_class->signals, name, ref);
luaA_object_unref(L, (void *) ref);
lua_remove(L, ud);
}
void
luaA_class_emit_signal(lua_State *L, lua_class_t *lua_class,
const char *name, int nargs)
{
signal_t *sigfound = signal_array_getbyid(&lua_class->signals,
a_strhash((const unsigned char *) name));
if(sigfound)
foreach(ref, sigfound->sigfuncs)
{
for(int i = 0; i < nargs; i++)
lua_pushvalue(L, - nargs);
luaA_object_push(L, (void *) *ref);
luaA_dofunction(L, nargs, 0);
}
lua_pop(L, nargs);
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

67
common/luaclass.h Normal file
View File

@ -0,0 +1,67 @@
/*
* luaclass.h - useful functions for handling Lua classes
*
* Copyright © 2009 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef AWESOME_COMMON_LUACLASS
#define AWESOME_COMMON_LUACLASS
#include "common/signal.h"
typedef struct
{
signal_array_t signals;
} lua_class_t;
void luaA_class_add_signal(lua_State *, lua_class_t *, const char *, int);
void luaA_class_remove_signal(lua_State *, lua_class_t *, const char *, int);
void luaA_class_emit_signal(lua_State *, lua_class_t *, const char *, int);
#define LUA_CLASS_FUNCS(prefix, lua_class) \
static inline int \
luaA_##prefix##_class_add_signal(lua_State *L) \
{ \
luaA_class_add_signal(L, &(lua_class), luaL_checkstring(L, 1), 2); \
return 0; \
} \
\
static inline int \
luaA_##prefix##_class_remove_signal(lua_State *L) \
{ \
luaA_class_remove_signal(L, &(lua_class), \
luaL_checkstring(L, 1), 2); \
return 0; \
} \
\
static inline int \
luaA_##prefix##_class_emit_signal(lua_State *L) \
{ \
luaA_class_emit_signal(L, &(lua_class), luaL_checkstring(L, 1), \
lua_gettop(L) - 1); \
return 0; \
}
#define LUA_CLASS_METHODS(class) \
{ "add_signal", luaA_##class##_class_add_signal }, \
{ "remove_signal", luaA_##class##_class_remove_signal }, \
{ "emit_signal", luaA_##class##_class_emit_signal },
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80