2009-06-08 17:28:20 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2009-06-16 11:44:28 +02:00
|
|
|
|
2014-03-30 20:07:48 +02:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
2009-06-16 11:44:28 +02:00
|
|
|
typedef struct lua_class_property lua_class_property_t;
|
|
|
|
|
|
|
|
ARRAY_TYPE(lua_class_property_t, lua_class_property)
|
2009-06-08 17:28:20 +02:00
|
|
|
|
2009-06-15 17:24:36 +02:00
|
|
|
#define LUA_OBJECT_HEADER \
|
|
|
|
signal_array_t signals;
|
|
|
|
|
|
|
|
/** Generic type for all objects.
|
|
|
|
* All Lua objects can be casted to this type.
|
|
|
|
*/
|
2009-06-08 17:28:20 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2009-06-15 17:24:36 +02:00
|
|
|
LUA_OBJECT_HEADER
|
|
|
|
} lua_object_t;
|
|
|
|
|
|
|
|
typedef lua_object_t *(*lua_class_allocator_t)(lua_State *);
|
2009-10-02 15:48:32 +02:00
|
|
|
typedef void (*lua_class_collector_t)(lua_object_t *);
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2009-08-20 17:37:46 +02:00
|
|
|
typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
|
|
|
|
|
2009-09-30 11:55:43 +02:00
|
|
|
typedef bool (*lua_class_checker_t)(lua_object_t *);
|
|
|
|
|
2009-09-30 14:21:25 +02:00
|
|
|
typedef struct lua_class_t lua_class_t;
|
|
|
|
struct lua_class_t
|
2009-06-15 17:24:36 +02:00
|
|
|
{
|
|
|
|
/** Class name */
|
|
|
|
const char *name;
|
2009-08-20 17:37:46 +02:00
|
|
|
/** Class signals */
|
2009-06-08 17:28:20 +02:00
|
|
|
signal_array_t signals;
|
2009-09-30 14:21:25 +02:00
|
|
|
/** Parent class */
|
|
|
|
lua_class_t *parent;
|
2009-06-15 17:24:36 +02:00
|
|
|
/** Allocator for creating new objects of that class */
|
|
|
|
lua_class_allocator_t allocator;
|
2009-10-02 15:48:32 +02:00
|
|
|
/** Garbage collection function */
|
|
|
|
lua_class_collector_t collector;
|
2009-06-16 11:44:28 +02:00
|
|
|
/** Class properties */
|
|
|
|
lua_class_property_array_t properties;
|
2009-08-20 17:37:46 +02:00
|
|
|
/** Function to call when a indexing an unknown property */
|
|
|
|
lua_class_propfunc_t index_miss_property;
|
|
|
|
/** Function to call when a indexing an unknown property */
|
|
|
|
lua_class_propfunc_t newindex_miss_property;
|
2009-09-30 11:55:43 +02:00
|
|
|
/** Function to call to check if an object is valid */
|
|
|
|
lua_class_checker_t checker;
|
2010-09-17 17:10:53 +02:00
|
|
|
/** Number of instances of this class in lua */
|
|
|
|
unsigned int instances;
|
2014-12-06 18:16:05 +01:00
|
|
|
/** Class tostring method */
|
|
|
|
lua_class_propfunc_t tostring;
|
2015-03-14 10:06:53 +01:00
|
|
|
/** Function to call on index misses */
|
|
|
|
int index_miss_handler;
|
|
|
|
/** Function to call on newindex misses */
|
|
|
|
int newindex_miss_handler;
|
2009-09-30 14:21:25 +02:00
|
|
|
};
|
2009-06-08 17:28:20 +02:00
|
|
|
|
2009-06-15 17:57:04 +02:00
|
|
|
const char * luaA_typename(lua_State *, int);
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_class_t * luaA_class_get(lua_State *, int);
|
2009-06-15 17:57:04 +02:00
|
|
|
|
2009-10-09 20:39:55 +02:00
|
|
|
void luaA_class_connect_signal(lua_State *, lua_class_t *, const char *, lua_CFunction);
|
|
|
|
void luaA_class_connect_signal_from_stack(lua_State *, lua_class_t *, const char *, int);
|
|
|
|
void luaA_class_disconnect_signal_from_stack(lua_State *, lua_class_t *, const char *, int);
|
2009-06-08 17:28:20 +02:00
|
|
|
void luaA_class_emit_signal(lua_State *, lua_class_t *, const char *, int);
|
|
|
|
|
2012-06-06 23:07:07 +02:00
|
|
|
void luaA_openlib(lua_State *, const char *, const struct luaL_Reg[], const struct luaL_Reg[]);
|
2009-09-30 14:21:25 +02:00
|
|
|
void luaA_class_setup(lua_State *, lua_class_t *, const char *, lua_class_t *,
|
2009-10-02 15:48:32 +02:00
|
|
|
lua_class_allocator_t, lua_class_collector_t,
|
|
|
|
lua_class_checker_t,
|
2009-08-20 17:37:46 +02:00
|
|
|
lua_class_propfunc_t, lua_class_propfunc_t,
|
2012-06-06 23:07:07 +02:00
|
|
|
const struct luaL_Reg[], const struct luaL_Reg[]);
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2010-09-01 15:41:41 +02:00
|
|
|
void luaA_class_add_property(lua_class_t *, const char *,
|
2009-06-16 11:44:28 +02:00
|
|
|
lua_class_propfunc_t, lua_class_propfunc_t, lua_class_propfunc_t);
|
|
|
|
|
2009-06-16 15:42:32 +02:00
|
|
|
int luaA_usemetatable(lua_State *, int, int);
|
|
|
|
int luaA_class_index(lua_State *);
|
|
|
|
int luaA_class_newindex(lua_State *);
|
2009-06-25 11:21:51 +02:00
|
|
|
int luaA_class_new(lua_State *, lua_class_t *);
|
2009-06-16 15:42:32 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
void * luaA_checkudata(lua_State *, int, lua_class_t *);
|
|
|
|
void * luaA_toudata(lua_State *L, int ud, lua_class_t *);
|
|
|
|
|
2014-12-06 18:16:05 +01:00
|
|
|
static inline void luaA_class_set_tostring(lua_class_t *class, lua_class_propfunc_t callback)
|
|
|
|
{
|
|
|
|
class->tostring = callback;
|
|
|
|
}
|
|
|
|
|
2009-09-04 16:24:14 +02:00
|
|
|
static inline void *
|
|
|
|
luaA_checkudataornil(lua_State *L, int udx, lua_class_t *class)
|
|
|
|
{
|
|
|
|
if(lua_isnil(L, udx))
|
|
|
|
return NULL;
|
|
|
|
return luaA_checkudata(L, udx, class);
|
|
|
|
}
|
|
|
|
|
2009-06-08 17:28:20 +02:00
|
|
|
#define LUA_CLASS_FUNCS(prefix, lua_class) \
|
|
|
|
static inline int \
|
2010-08-25 23:00:36 +02:00
|
|
|
luaA_##prefix##_class_add_signal(lua_State *L) \
|
|
|
|
{ \
|
|
|
|
signal_add(&(lua_class).signals, luaL_checkstring(L, 1)); \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static inline int \
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_##prefix##_class_connect_signal(lua_State *L) \
|
2009-06-08 17:28:20 +02:00
|
|
|
{ \
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_class_connect_signal_from_stack(L, &(lua_class), \
|
|
|
|
luaL_checkstring(L, 1), 2); \
|
2009-06-08 17:28:20 +02:00
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static inline int \
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_##prefix##_class_disconnect_signal(lua_State *L) \
|
2009-06-08 17:28:20 +02:00
|
|
|
{ \
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_class_disconnect_signal_from_stack(L, &(lua_class), \
|
|
|
|
luaL_checkstring(L, 1), 2); \
|
2009-06-08 17:28:20 +02:00
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static inline int \
|
|
|
|
luaA_##prefix##_class_emit_signal(lua_State *L) \
|
|
|
|
{ \
|
|
|
|
luaA_class_emit_signal(L, &(lua_class), luaL_checkstring(L, 1), \
|
2009-10-09 20:39:55 +02:00
|
|
|
lua_gettop(L) - 1); \
|
2009-06-08 17:28:20 +02:00
|
|
|
return 0; \
|
2010-09-17 17:10:53 +02:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
static inline int \
|
|
|
|
luaA_##prefix##_class_instances(lua_State *L) \
|
|
|
|
{ \
|
2016-02-06 13:46:46 +01:00
|
|
|
lua_pushinteger(L, (lua_class).instances); \
|
2010-09-17 17:10:53 +02:00
|
|
|
return 1; \
|
2015-03-14 10:06:53 +01:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
static inline int \
|
|
|
|
luaA_##prefix##_set_index_miss_handler(lua_State *L) \
|
|
|
|
{ \
|
|
|
|
return luaA_registerfct(L, 1, &(lua_class).index_miss_handler); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static inline int \
|
|
|
|
luaA_##prefix##_set_newindex_miss_handler(lua_State *L) \
|
|
|
|
{ \
|
|
|
|
return luaA_registerfct(L, 1, &(lua_class).newindex_miss_handler); \
|
2009-06-08 17:28:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LUA_CLASS_METHODS(class) \
|
2010-08-25 23:00:36 +02:00
|
|
|
{ "add_signal", luaA_##class##_class_add_signal }, \
|
2009-10-09 20:39:55 +02:00
|
|
|
{ "connect_signal", luaA_##class##_class_connect_signal }, \
|
|
|
|
{ "disconnect_signal", luaA_##class##_class_disconnect_signal }, \
|
2010-09-17 17:10:53 +02:00
|
|
|
{ "emit_signal", luaA_##class##_class_emit_signal }, \
|
|
|
|
{ "instances", luaA_##class##_class_instances }, \
|
2015-03-14 10:06:53 +01:00
|
|
|
{ "set_index_miss_handler", luaA_##class##_set_index_miss_handler }, \
|
|
|
|
{ "set_newindex_miss_handler", luaA_##class##_set_newindex_miss_handler }, \
|
2009-06-08 17:28:20 +02:00
|
|
|
|
2009-06-16 15:42:32 +02:00
|
|
|
#define LUA_CLASS_META \
|
|
|
|
{ "__index", luaA_class_index }, \
|
|
|
|
{ "__newindex", luaA_class_newindex },
|
|
|
|
|
2009-06-08 17:28:20 +02:00
|
|
|
#endif
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|