2
0
Fork 0

Remove action_list struct

This commit is contained in:
Stefan Bühler 2008-08-03 22:20:36 +02:00
parent a9cbec1046
commit f86bf3631d
7 changed files with 64 additions and 117 deletions

View File

@ -7,10 +7,43 @@ struct action_stack_element;
typedef struct action_stack_element action_stack_element;
struct action_stack_element {
action_list *list;
action *act;
guint pos;
};
void action_release(server *srv, action *a) {
guint i;
assert(a->refcount > 0);
if (!(--a->refcount)) {
switch (a->type) {
case ACTION_TSETTING:
release_option(srv, &a->value.setting);
break;
case ACTION_TFUNCTION:
if (a->value.function.free) {
a->value.function.free(srv, a->value.function.param);
}
break;
case ACTION_TCONDITION:
condition_release(srv, a->value.condition.cond);
action_release(srv, a->value.condition.target);
break;
case ACTION_TLIST:
for (i = a->value.list->len; i-- > 0; ) {
action_release(srv, g_array_index(a->value.list, action*, i));
}
g_array_free(a->value.list, TRUE);
break;
}
g_slice_free(action, a);
}
}
void action_acquire(action *a) {
assert(a->refcount > 0);
a->refcount++;
}
action *action_new_setting(server *srv, const gchar *name, option *value) {
option_set setting;
if (!parse_option(srv, name, value, &setting)) {
@ -45,95 +78,27 @@ action *action_new_list() {
a = g_slice_new(action);
a->refcount = 1;
a->type = ACTION_TLIST;
a->value.list = action_list_new();
a->value.list = g_array_new(FALSE, TRUE, sizeof(action *));
return a;
}
action *action_new_condition(condition *cond, action_list *al) {
action *action_new_condition(condition *cond, action *target) {
action *a;
a = g_slice_new(action);
a->refcount = 1;
a->type = ACTION_TCONDITION;
a->value.condition.cond = cond;
a->value.condition.target = al;
a->value.condition.target = target;
return a;
}
void action_release(server *srv, action *a) {
assert(a->refcount > 0);
if (!(--a->refcount)) {
switch (a->type) {
case ACTION_TSETTING:
release_option(srv, &a->value.setting);
break;
case ACTION_TFUNCTION:
if (a->value.function.free) {
a->value.function.free(srv, a->value.function.param);
}
break;
case ACTION_TCONDITION:
condition_release(srv, a->value.condition.cond);
action_list_release(srv, a->value.condition.target);
break;
case ACTION_TLIST:
action_list_release(srv, a->value.list);
break;
}
g_slice_free(action, a);
}
}
void action_acquire(action *a) {
assert(a->refcount > 0);
a->refcount++;
}
action_list *action_list_new() {
action_list *al;
al = g_slice_new(action_list);
al->refcount = 1;
al->actions = g_array_new(FALSE, TRUE, sizeof(action *));
return al;
}
action_list *action_list_from_action(action *a) {
action_list *al;
if (a->type == ACTION_TLIST) {
action_list_acquire(a->value.list);
return a->value.list; /* action gets released from lua */
}
action_acquire(a);
al = action_list_new();
g_array_append_val(al->actions, a);
return al;
}
void action_list_release(server *srv, action_list *al) {
assert(al->refcount > 0);
if (!(--al->refcount)) {
guint i;
for (i = al->actions->len; i-- > 0; ) {
action_release(srv, g_array_index(al->actions, action*, i));
}
g_array_free(al->actions, TRUE);
g_slice_free(action_list, al);
}
}
void action_list_acquire(action_list *al) {
assert(al->refcount > 0);
al->refcount++;
}
void action_stack_element_release(server *srv, action_stack_element *ase) {
if (!ase || !ase->list) return;
action_list_release(srv, ase->list);
ase->list = NULL;
if (!ase || !ase->act) return;
action_release(srv, ase->act);
ase->act = NULL;
}
void action_stack_init(action_stack *as) {
@ -154,12 +119,13 @@ void action_stack_clear(server *srv, action_stack *as) {
action_stack_element_release(srv, &g_array_index(as->stack, action_stack_element, i));
}
g_array_free(as->stack, TRUE);
as->stack = NULL;
}
/** handle sublist now, remember current position (stack) */
void action_enter(connection *con, action_list *al) {
action_list_acquire(al);
action_stack_element ase = { al, 0 };
void action_enter(connection *con, action *a) {
action_acquire(a);
action_stack_element ase = { a, 0 };
g_array_append_val(con->action_stack.stack, ase);
}
@ -172,9 +138,13 @@ static void action_stack_pop(server *srv, action_stack *as) {
g_array_set_size(as->stack, as->stack->len - 1);
}
static action* action_stack_element_next(action_stack_element *ase) {
action_list *al = ase->list;
return ase->pos < al->actions->len ? g_array_index(al->actions, action*, ase->pos++) : NULL;
static action* action_stack_element_action(action_stack_element *ase) {
action *a = ase->act;
if (a->type == ACTION_TLIST) {
return ase->pos < a->value.list->len ? g_array_index(a->value.list, action*, ase->pos) : NULL;
} else {
return ase->pos == 0 ? a : NULL;
}
}
action_result action_execute(server *srv, connection *con) {
@ -184,7 +154,7 @@ action_result action_execute(server *srv, connection *con) {
action_result res;
while (NULL != (ase = action_stack_top(as))) {
a = action_stack_element_next(ase);
a = action_stack_element_action(ase);
if (!a) {
action_stack_pop(srv, as);
continue;
@ -212,9 +182,10 @@ action_result action_execute(server *srv, connection *con) {
}
break;
case ACTION_TLIST:
action_enter(con, a->value.list);
action_enter(con, a);
break;
}
ase->pos++;
}
return ACTION_GO_ON;
}

View File

@ -21,9 +21,6 @@ typedef enum {
struct action;
typedef struct action action;
struct action_list;
typedef struct action_list action_list;
struct action_stack;
typedef struct action_stack action_stack;
@ -46,12 +43,6 @@ typedef struct action_func action_func;
#include "plugin.h"
#include "options.h"
struct action_list {
gint refcount;
GArray* actions; /** array of (action*) */
};
struct action {
gint refcount;
action_type type;
@ -61,27 +52,22 @@ struct action {
struct {
condition *cond;
action_list* target; /** action target to jump to if condition is fulfilled */
action* target; /** action target to jump to if condition is fulfilled */
} condition;
action_func function;
action_list *list;
GArray* list; /** array of (action*) */
} value;
};
LI_API void action_list_release(server *srv, action_list *al);
LI_API void action_list_acquire(action_list *al);
LI_API action_list *action_list_new();
LI_API action_list *action_list_from_action(action *a);
/* no new/free function, so just use the struct direct (i.e. not a pointer) */
LI_API void action_stack_init(action_stack *as);
LI_API void action_stack_reset(server *srv, action_stack *as);
LI_API void action_stack_clear(server *srv, action_stack *as);
/** handle sublist now, remember current position (stack) */
LI_API void action_enter(connection *con, action_list *al);
LI_API void action_enter(connection *con, action *a);
LI_API action_result action_execute(server *srv, connection *con);
@ -91,7 +77,6 @@ LI_API void action_acquire(action *a);
LI_API action *action_new_setting(server *srv, const gchar *name, option *value);
LI_API action *action_new_function(ActionFunc func, ActionFree free, gpointer param);
LI_API action *action_new_list();
LI_API action *action_new_condition(condition *cond, action_list *al);
/*LI_API action *action_new_condition_string(comp_key_t comp, comp_operator_t op, GString *str);
LI_API action *action_new_condition_int(comp_key_t comp, comp_operator_t op, guint64 i);*/
LI_API action *action_new_condition(condition *cond, action *target);
#endif

View File

@ -44,11 +44,3 @@ int lua_push_action(server *srv, lua_State *L, action *a) {
lua_setmetatable(L, -2);
return 1;
}
action_list* lua_get_actionlist(lua_State *L) {
action *a;
if (lua_isnil(L, -1)) return NULL;
a = lua_get_action(L, 1);
if (!a) return NULL;
return action_list_from_action(a);
}

View File

@ -6,6 +6,5 @@
action* lua_get_action(lua_State *L, int ndx);
int lua_push_action(server *srv, lua_State *L, action *a);
action_list* lua_get_actionlist(lua_State *L);
#endif

View File

@ -164,7 +164,7 @@ static int handle_option(lua_State *L) {
lua_pushnil(L);
while (lua_next(L, 2) != 0) {
suba = action_from_lua(srv, L);
g_array_append_val(a->value.list->actions, suba);
g_array_append_val(a->value.list, suba);
lua_pop(L, 1);
}
lua_pop(L, 1);
@ -212,7 +212,7 @@ gboolean config_lua_load(server *srv, const gchar *filename) {
lua_pop(L, 1); /* pop the ret-value */
lua_getfield(L, LUA_GLOBALSINDEX, "action");
srv->mainactionlist = lua_get_actionlist(L);
srv->mainaction = lua_get_action(L, -1);
lua_pop(L, 1);
assert(lua_gettop(L) == 0);

View File

@ -21,7 +21,7 @@ static action* core_list(server *srv, plugin* p, option *opt) {
}
assert(srv == oa->value.opt_action.srv);
action_acquire(oa->value.opt_action.action);
g_array_append_val(a->value.list->actions, oa->value.opt_action.action);
g_array_append_val(a->value.list, oa->value.opt_action.action);
}
option_free(opt);
return a;
@ -50,7 +50,7 @@ static action* core_when(server *srv, plugin* p, option *opt) {
ERROR(srv, "expected condition as first parameter, got %s", option_type_string(opt->type));
return NULL;
}
a = action_new_condition(opt_cond->value.opt_cond.cond, action_list_from_action(opt_act->value.opt_action.action));
a = action_new_condition(opt_cond->value.opt_cond.cond, opt_act->value.opt_action.action);
option_free(opt);
return a;
}

View File

@ -14,7 +14,7 @@ struct server {
GHashTable *setups; /**< const gchar* => server_setup* */
gpointer *option_def_values;
struct action_list *mainactionlist;
struct action *mainaction;
gboolean exiting;
GMutex *mutex; /* general mutex for accessing the various members */