Track the number of objects

With this patch, we track the number of objects that are alive for any class.
This information can be accessed via class.instances()

For example:

  print("wiboxes", wibox.instances())
  print("widgets", widget.instances())

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-09-17 17:10:53 +02:00
parent c368b84817
commit 8701295e6c
11 changed files with 54 additions and 1 deletions

View file

@ -174,6 +174,7 @@ luaA_class_gc(lua_State *L)
signal_array_wipe(&item->signals);
/* Get the object class */
lua_class_t *class = luaA_class_get(L, 1);
class->instances--;
/* Call the collector function of the class, and all its parent classes */
for(; class; class = class->parent)
if(class->collector)
@ -243,6 +244,7 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
class->newindex_miss_property = newindex_miss_property;
class->checker = checker;
class->parent = parent;
class->instances = 0;
signal_add(&class->signals, "new");

View file

@ -67,6 +67,8 @@ struct lua_class_t
lua_class_propfunc_t newindex_miss_property;
/** Function to call to check if an object is valid */
lua_class_checker_t checker;
/** Number of instances of this class in lua */
unsigned int instances;
};
const char * luaA_typename(lua_State *, int);
@ -133,13 +135,21 @@ luaA_checkudataornil(lua_State *L, int udx, lua_class_t *class)
luaA_class_emit_signal(L, &(lua_class), luaL_checkstring(L, 1), \
lua_gettop(L) - 1); \
return 0; \
} \
\
static inline int \
luaA_##prefix##_class_instances(lua_State *L) \
{ \
lua_pushnumber(L, (lua_class).instances); \
return 1; \
}
#define LUA_CLASS_METHODS(class) \
{ "add_signal", luaA_##class##_class_add_signal }, \
{ "connect_signal", luaA_##class##_class_connect_signal }, \
{ "disconnect_signal", luaA_##class##_class_disconnect_signal }, \
{ "emit_signal", luaA_##class##_class_emit_signal },
{ "emit_signal", luaA_##class##_class_emit_signal }, \
{ "instances", luaA_##class##_class_instances }, \
#define LUA_CLASS_META \
{ "__index", luaA_class_index }, \

View file

@ -167,6 +167,7 @@ int luaA_object_emit_signal_simple(lua_State *);
{ \
type *p = lua_newuserdata(L, sizeof(type)); \
p_clear(p, 1); \
(lua_class).instances++; \
luaA_settype(L, &(lua_class)); \
lua_newtable(L); \
lua_newtable(L); \

View file

@ -27,3 +27,8 @@ module("button")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of button objects alive.
-- @name instances
-- @class function

View file

@ -128,3 +128,8 @@ module("client")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of client objects alive.
-- @name instances
-- @class function

View file

@ -128,3 +128,8 @@ module("image")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of image objects alive.
-- @name instances
-- @class function

View file

@ -30,3 +30,8 @@ module("key")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of key objects alive.
-- @name instances
-- @class function

View file

@ -33,3 +33,8 @@ module("tag")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of tag objects alive.
-- @name instances
-- @class function

View file

@ -38,3 +38,8 @@ module("timer")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of timer objects alive.
-- @name instances
-- @class function

View file

@ -58,3 +58,8 @@ module("wibox")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of wibox objects alive.
-- @name instances
-- @class function

View file

@ -66,3 +66,8 @@ module("widget")
-- @param ... Various arguments, optional.
-- @name emit_signal
-- @class function
--- Get the number of instances.
-- @return The number of widget objects alive.
-- @name instances
-- @class function