Skip to content

Commit

Permalink
filterx: propagate modified_in_place through refs
Browse files Browse the repository at this point in the history
Signed-off-by: László Várady <[email protected]>
  • Loading branch information
MrAnno committed Oct 19, 2024
1 parent e5f6b7c commit 7bb7dc8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
24 changes: 23 additions & 1 deletion lib/filterx/filterx-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct _FilterXType
gboolean (*len)(FilterXObject *self, guint64 *len);
FilterXObject *(*add)(FilterXObject *self, FilterXObject *object);
void (*make_readonly)(FilterXObject *self);
gboolean (*is_modified_in_place)(FilterXObject *self);
void (*set_modified_in_place)(FilterXObject *self, gboolean modified);
void (*free_fn)(FilterXObject *self);
};

Expand Down Expand Up @@ -83,7 +85,9 @@ struct _FilterXObject
/* NOTE:
*
* modified_in_place -- set to TRUE in case the value in this
* FilterXObject was changed
* FilterXObject was changed.
* don't use it directly, use
* filterx_object_{is,set}_modified_in_place()
* readonly -- marks the object as unmodifiable,
* propagates to the inner elements lazily
*
Expand Down Expand Up @@ -296,4 +300,22 @@ filterx_object_add_object(FilterXObject *self, FilterXObject *object)
return self->type->add(self, object);
}

static inline gboolean
filterx_object_is_modified_in_place(FilterXObject *self)
{
if (G_UNLIKELY(self->type->is_modified_in_place))
return self->type->is_modified_in_place(self);

return self->modified_in_place;
}

static inline void
filterx_object_set_modified_in_place(FilterXObject *self, gboolean modified)
{
if (G_UNLIKELY(self->type->set_modified_in_place))
return self->type->set_modified_in_place(self, modified);

self->modified_in_place = modified;
}

#endif
18 changes: 17 additions & 1 deletion lib/filterx/filterx-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,26 @@ _filterx_ref_free(FilterXObject *s)
filterx_object_free_method(s);
}

void
static void
_prohibit_readonly(FilterXObject *s)
{
g_assert_not_reached();
}

static gboolean
_is_modified_in_place(FilterXObject *s)
{
FilterXRef *self = (FilterXRef *) s;
return filterx_object_is_modified_in_place(self->value);
}

static void
_set_modified_in_place(FilterXObject *s, gboolean modified)
{
FilterXRef *self = (FilterXRef *) s;
filterx_object_set_modified_in_place(self->value, modified);
}

/* readonly methods */

static FilterXObject *
Expand Down Expand Up @@ -267,5 +281,7 @@ FILTERX_DEFINE_TYPE(ref, FILTERX_TYPE_NAME(object),
.len = _filterx_ref_len,
.add = _filterx_ref_add,
.make_readonly = _prohibit_readonly,
.is_modified_in_place = _is_modified_in_place,
.set_modified_in_place = _set_modified_in_place,
.free_fn = _filterx_ref_free,
);
4 changes: 2 additions & 2 deletions lib/filterx/filterx-scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ filterx_scope_sync(FilterXScope *self, LogMessage *msg)
log_msg_unset_value(msg, filterx_variable_get_nv_handle(v));
filterx_variable_unassign(v);
}
else if (filterx_variable_is_assigned(v) || v->value->modified_in_place)
else if (filterx_variable_is_assigned(v) || filterx_object_is_modified_in_place(v->value))
{
LogMessageValueType t;

Expand All @@ -245,7 +245,7 @@ filterx_scope_sync(FilterXScope *self, LogMessage *msg)
if (!filterx_object_marshal(v->value, buffer, &t))
g_assert_not_reached();
log_msg_set_value_with_type(msg, filterx_variable_get_nv_handle(v), buffer->str, buffer->len, t);
v->value->modified_in_place = FALSE;
filterx_object_set_modified_in_place(v->value, FALSE);
filterx_variable_unassign(v);
}
else
Expand Down
12 changes: 6 additions & 6 deletions lib/filterx/object-json-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ _append(FilterXList *s, FilterXObject **new_value)
return FALSE;
}

self->super.super.modified_in_place = TRUE;
filterx_object_set_modified_in_place(&self->super.super, TRUE);
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_set_modified_in_place(root_container, TRUE);
filterx_object_unref(root_container);
}

Expand Down Expand Up @@ -203,11 +203,11 @@ _set_subscript(FilterXList *s, guint64 index, FilterXObject **new_value)
return FALSE;
}

self->super.super.modified_in_place = TRUE;
filterx_object_set_modified_in_place(&self->super.super, TRUE);
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_set_modified_in_place(root_container, TRUE);
filterx_object_unref(root_container);
}

Expand All @@ -228,11 +228,11 @@ _unset_index(FilterXList *s, guint64 index)
if (json_object_array_del_idx(self->jso, index, 1) != 0)
return FALSE;

self->super.super.modified_in_place = TRUE;
filterx_object_set_modified_in_place(&self->super.super, TRUE);
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_set_modified_in_place(root_container, TRUE);
filterx_object_unref(root_container);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/filterx/object-json-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ _set_subscript(FilterXDict *s, FilterXObject *key, FilterXObject **new_value)
return FALSE;
}

self->super.super.modified_in_place = TRUE;
filterx_object_set_modified_in_place(&self->super.super, TRUE);
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_set_modified_in_place(root_container, TRUE);
filterx_object_unref(root_container);
}

Expand All @@ -177,11 +177,11 @@ _unset_key(FilterXDict *s, FilterXObject *key)

json_object_object_del(self->jso, key_str);

self->super.super.modified_in_place = TRUE;
filterx_object_set_modified_in_place(&self->super.super, TRUE);
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_set_modified_in_place(root_container, TRUE);
filterx_object_unref(root_container);
}

Expand Down

0 comments on commit 7bb7dc8

Please sign in to comment.