Commit 03031d71 authored by Brice Videau's avatar Brice Videau

Support for arbitrary number of destroy callbacks.

parent 643abc3b
......@@ -33,8 +33,14 @@ ccs_release_object(ccs_object_t object) {
return -CCS_INVALID_OBJECT;
obj->refcount -= 1;
if (obj->refcount == 0) {
if (obj->cb)
obj->cb(object, obj->cb_user_data);
if (obj->callbacks) {
_ccs_object_callback_t *cb = NULL;
while ( (cb = (_ccs_object_callback_t *)
utarray_next(obj->callbacks, cb)) ) {
cb->callback(object, cb->user_data);
}
utarray_free(obj->callbacks);
}
ccs_result_t err = obj->ops->del(object);
if (err)
return err;
......@@ -65,6 +71,13 @@ ccs_object_get_refcount(ccs_object_t object,
return CCS_SUCCESS;
}
static const UT_icd _object_callback_icd = {
sizeof(_ccs_object_callback_t),
NULL,
NULL,
NULL
};
ccs_result_t
ccs_object_set_destroy_callback(ccs_object_t object,
void (*callback)(
......@@ -74,8 +87,13 @@ ccs_object_set_destroy_callback(ccs_object_t object,
_ccs_object_internal_t *obj = (_ccs_object_internal_t *)object;
if (!obj)
return -CCS_INVALID_OBJECT;
obj->cb = callback;
obj->cb_user_data = user_data;
if (!callback)
return -CCS_INVALID_VALUE;
if (!obj->callbacks)
utarray_new(obj->callbacks, &_object_callback_icd);
_ccs_object_callback_t cb = { callback, user_data };
utarray_push_back(obj->callbacks, &cb);
return CCS_SUCCESS;
}
......@@ -2,6 +2,7 @@
#define _CONFIGSPACE_INTERNAL_H
#include <cconfigspace.h>
#include "utarray.h"
static inline ccs_bool_t
_ccs_interval_include(ccs_interval_t *interval, ccs_numeric_t value) {
......@@ -45,11 +46,16 @@ struct _ccs_object_ops_s {
typedef struct _ccs_object_ops_s _ccs_object_ops_t;
struct _ccs_object_callback_s {
ccs_object_release_callback_t callback;
void *user_data;
};
typedef struct _ccs_object_callback_s _ccs_object_callback_t;
struct _ccs_object_internal_s {
ccs_object_type_t type;
int32_t refcount;
ccs_object_release_callback_t cb;
void *cb_user_data;
UT_array *callbacks;
_ccs_object_ops_t *ops;
};
......@@ -67,8 +73,7 @@ _ccs_object_init(_ccs_object_internal_t *o,
_ccs_object_ops_t *ops) {
o->type = t;
o->refcount = 1;
o->cb = NULL;
o->cb_user_data = NULL;
o->callbacks = NULL;
o->ops = ops;
}
......
......@@ -1317,7 +1317,7 @@ ccs_variable_get_hyperparameter(ccs_expression_t expression,
}
static ccs_result_t _get_hyperparameters(ccs_expression_t expression,
UT_array *array) {
UT_array *array) {
CCS_CHECK_OBJ(expression, CCS_EXPRESSION);
ccs_result_t err;
if (expression->data->type == CCS_VARIABLE) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment