Skip to content

Commit

Permalink
Merge pull request #359 from alltilla/filterx-expr-init
Browse files Browse the repository at this point in the history
filterx: add `Expr::init()` and `Expr::deinit()` and optimize `update_metric()` in `init()`
  • Loading branch information
bazsi authored Nov 12, 2024
2 parents 2d00a2f + 63bf016 commit d82c236
Show file tree
Hide file tree
Showing 33 changed files with 972 additions and 15 deletions.
38 changes: 38 additions & 0 deletions lib/filterx/expr-compound.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,42 @@ _eval(FilterXExpr *s)
return result;
}

static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXCompoundExpr *self = (FilterXCompoundExpr *) s;

for (gint i = 0; i < self->exprs->len; i++)
{
FilterXExpr *expr = g_ptr_array_index(self->exprs, i);
if (!filterx_expr_init(expr, cfg))
{
for (gint j = 0; j < i; j++)
{
expr = g_ptr_array_index(self->exprs, j);
filterx_expr_deinit(expr, cfg);
}
return FALSE;
}
}

return filterx_expr_init_method(s, cfg);
}

static void
_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXCompoundExpr *self = (FilterXCompoundExpr *) s;

for (gint i = 0; i < self->exprs->len; i++)
{
FilterXExpr *expr = g_ptr_array_index(self->exprs, i);
filterx_expr_deinit(expr, cfg);
}

filterx_expr_deinit_method(s, cfg);
}

static void
_free(FilterXExpr *s)
{
Expand Down Expand Up @@ -165,6 +201,8 @@ filterx_compound_expr_new(gboolean return_value_of_last_expr)

filterx_expr_init_instance(&self->super);
self->super.eval = _eval;
self->super.init = _init;
self->super.deinit = _deinit;
self->super.free_fn = _free;
self->exprs = g_ptr_array_new_with_free_func((GDestroyNotify) filterx_expr_unref);
self->return_value_of_last_expr = return_value_of_last_expr;
Expand Down
38 changes: 38 additions & 0 deletions lib/filterx/expr-condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ struct _FilterXConditional
FilterXExpr *false_branch;
};

static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXConditional *self = (FilterXConditional *) s;

if (!filterx_expr_init(self->condition, cfg))
return FALSE;

if (!filterx_expr_init(self->true_branch, cfg))
{
filterx_expr_deinit(self->condition, cfg);
return FALSE;
}

if (!filterx_expr_init(self->false_branch, cfg))
{
filterx_expr_deinit(self->condition, cfg);
filterx_expr_deinit(self->true_branch, cfg);
return FALSE;
}

return filterx_expr_init_method(s, cfg);
}


static void
_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXConditional *self = (FilterXConditional *) s;

filterx_expr_deinit(self->condition, cfg);
filterx_expr_deinit(self->true_branch, cfg);
filterx_expr_deinit(self->false_branch, cfg);
filterx_expr_deinit_method(s, cfg);
}

static void
_free(FilterXExpr *s)
{
Expand Down Expand Up @@ -119,6 +155,8 @@ filterx_conditional_new(FilterXExpr *condition)
FilterXConditional *self = g_new0(FilterXConditional, 1);
filterx_expr_init_instance(&self->super);
self->super.eval = _eval;
self->super.init = _init;
self->super.deinit = _deinit;
self->super.free_fn = _free;
self->super.suppress_from_trace = TRUE;
self->condition = condition;
Expand Down
62 changes: 62 additions & 0 deletions lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,42 @@ _simple_eval(FilterXExpr *s)
return res;
}

static gboolean
_simple_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXSimpleFunction *self = (FilterXSimpleFunction *) s;

for (guint64 i = 0; i < self->args->len; i++)
{
FilterXExpr *arg = g_ptr_array_index(self->args, i);
if (!filterx_expr_init(arg, cfg))
{
for (gint j = 0; j < i; j++)
{
arg = g_ptr_array_index(self->args, j);
filterx_expr_deinit(arg, cfg);
}
return FALSE;
}
}

return filterx_function_init_method(&self->super, cfg);
}

static void
_simple_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXSimpleFunction *self = (FilterXSimpleFunction *) s;

for (guint64 i = 0; i < self->args->len; i++)
{
FilterXExpr *arg = g_ptr_array_index(self->args, i);
filterx_expr_deinit(arg, cfg);
}

filterx_function_deinit_method(&self->super, cfg);
}

static void
_simple_free(FilterXExpr *s)
{
Expand Down Expand Up @@ -169,6 +205,8 @@ filterx_simple_function_new(const gchar *function_name, FilterXFunctionArgs *arg

filterx_function_init_instance(&self->super, function_name);
self->super.super.eval = _simple_eval;
self->super.super.init = _simple_init;
self->super.super.deinit = _simple_deinit;
self->super.super.free_fn = _simple_free;
self->function_proto = function_proto;

Expand All @@ -186,6 +224,18 @@ filterx_simple_function_new(const gchar *function_name, FilterXFunctionArgs *arg
return NULL;
}

gboolean
filterx_function_init_method(FilterXFunction *s, GlobalConfig *cfg)
{
return filterx_expr_init_method(&s->super, cfg);
}

void
filterx_function_deinit_method(FilterXFunction *s, GlobalConfig *cfg)
{
filterx_expr_deinit_method(&s->super, cfg);
}

void
filterx_function_free_method(FilterXFunction *s)
{
Expand All @@ -208,6 +258,18 @@ filterx_function_init_instance(FilterXFunction *s, const gchar *function_name)
s->super.free_fn = _function_free;
}

gboolean
filterx_generator_function_init_method(FilterXGeneratorFunction *s, GlobalConfig *cfg)
{
return filterx_generator_init_method(&s->super.super, cfg);
}

void
filterx_generator_function_deinit_method(FilterXGeneratorFunction *s, GlobalConfig *cfg)
{
filterx_generator_deinit_method(&s->super.super, cfg);
}

void
filterx_generator_function_free_method(FilterXGeneratorFunction *s)
{
Expand Down
4 changes: 4 additions & 0 deletions lib/filterx/expr-function.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ enum FilterXFunctionError
};

void filterx_function_init_instance(FilterXFunction *s, const gchar *function_name);
gboolean filterx_function_init_method(FilterXFunction *s, GlobalConfig *cfg);
void filterx_function_deinit_method(FilterXFunction *s, GlobalConfig *cfg);
void filterx_function_free_method(FilterXFunction *s);
gboolean filterx_generator_function_init_method(FilterXGeneratorFunction *s, GlobalConfig *cfg);
void filterx_generator_function_deinit_method(FilterXGeneratorFunction *s, GlobalConfig *cfg);
void filterx_generator_function_init_instance(FilterXGeneratorFunction *s, const gchar *function_name);
void filterx_generator_function_free_method(FilterXGeneratorFunction *s);

Expand Down
44 changes: 44 additions & 0 deletions lib/filterx/expr-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,32 @@ void
filterx_generator_init_instance(FilterXExpr *s)
{
filterx_expr_init_instance(s);
s->init = filterx_generator_init_method;
s->deinit = filterx_generator_deinit_method;
s->eval = _eval;
s->ignore_falsy_result = TRUE;
}

gboolean
filterx_generator_init_method(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXExprGenerator *self = (FilterXExprGenerator *) s;

if (!filterx_expr_init(self->fillable, cfg))
return FALSE;

return filterx_expr_init_method(s, cfg);
}

void
filterx_generator_deinit_method(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXExprGenerator *self = (FilterXExprGenerator *) s;

filterx_expr_deinit(self->fillable, cfg);
filterx_expr_deinit_method(s, cfg);
}

void
filterx_generator_free_method(FilterXExpr *s)
{
Expand All @@ -87,6 +109,26 @@ _create_container_eval(FilterXExpr *s)
return self->generator->create_container(self->generator, self->fillable_parent);
}

static gboolean
_create_container_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXExprGeneratorCreateContainer *self = (FilterXExprGeneratorCreateContainer *) s;

if (!filterx_expr_init(self->fillable_parent, cfg))
return FALSE;

return filterx_expr_init_method(s, cfg);
}

static void
_create_container_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXExprGeneratorCreateContainer *self = (FilterXExprGeneratorCreateContainer *) s;

filterx_expr_deinit(self->fillable_parent, cfg);
filterx_expr_deinit_method(s, cfg);
}

static void
_create_container_free(FilterXExpr *s)
{
Expand Down Expand Up @@ -130,6 +172,8 @@ filterx_generator_create_container_new(FilterXExpr *g, FilterXExpr *fillable_par
filterx_expr_init_instance(&self->super);
self->generator = (FilterXExprGenerator *) g;
self->fillable_parent = fillable_parent;
self->super.init = _create_container_init;
self->super.deinit = _create_container_deinit;
self->super.eval = _create_container_eval;
self->super.free_fn = _create_container_free;

Expand Down
2 changes: 2 additions & 0 deletions lib/filterx/expr-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct FilterXExprGenerator_

void filterx_generator_set_fillable(FilterXExpr *s, FilterXExpr *fillable);
void filterx_generator_init_instance(FilterXExpr *s);
gboolean filterx_generator_init_method(FilterXExpr *s, GlobalConfig *cfg);
void filterx_generator_deinit_method(FilterXExpr *s, GlobalConfig *cfg);
void filterx_generator_free_method(FilterXExpr *s);
gboolean filterx_expr_is_generator(FilterXExpr *s);

Expand Down
29 changes: 29 additions & 0 deletions lib/filterx/expr-get-subscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ _unset(FilterXExpr *s)
return result;
}

static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXGetSubscript *self = (FilterXGetSubscript *) s;

if (!filterx_expr_init(self->operand, cfg))
return FALSE;

if (!filterx_expr_init(self->key, cfg))
{
filterx_expr_deinit(self->operand, cfg);
return FALSE;
}

return filterx_expr_init_method(s, cfg);
}

static void
_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXGetSubscript *self = (FilterXGetSubscript *) s;

filterx_expr_deinit(self->operand, cfg);
filterx_expr_deinit(self->key, cfg);
filterx_expr_deinit_method(s, cfg);
}

static void
_free(FilterXExpr *s)
{
Expand All @@ -122,6 +149,8 @@ filterx_get_subscript_new(FilterXExpr *operand, FilterXExpr *key)
self->super.eval = _eval;
self->super.is_set = _isset;
self->super.unset = _unset;
self->super.init = _init;
self->super.deinit = _deinit;
self->super.free_fn = _free;
self->operand = operand;
self->key = key;
Expand Down
21 changes: 21 additions & 0 deletions lib/filterx/expr-getattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ _isset(FilterXExpr *s)
return result;
}

static gboolean
_init(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXGetAttr *self = (FilterXGetAttr *) s;

if (!filterx_expr_init(self->operand, cfg))
return FALSE;

return filterx_expr_init_method(s, cfg);
}

static void
_deinit(FilterXExpr *s, GlobalConfig *cfg)
{
FilterXGetAttr *self = (FilterXGetAttr *) s;

filterx_expr_deinit(self->operand, cfg);
filterx_expr_deinit_method(s, cfg);
}

static void
_free(FilterXExpr *s)
Expand All @@ -110,6 +129,8 @@ filterx_getattr_new(FilterXExpr *operand, FilterXString *attr_name)
self->super.eval = _eval;
self->super.unset = _unset;
self->super.is_set = _isset;
self->super.init = _init;
self->super.deinit = _deinit;
self->super.free_fn = _free;
self->operand = operand;

Expand Down
Loading

0 comments on commit d82c236

Please sign in to comment.