X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Finclude%2Fcore%2Fobject.h;h=19d6cc33799c68eeefa3a7e72f322fdf2522201c;hb=b75718ea9fe4d6c90f1794e517a0712729553c0c;hp=016e718110090038ad5549e77c28a27aa0f1de34;hpb=1fee763a36a0c9af761244bbce8cd6c233ddbe2a;p=sysdb.git diff --git a/src/include/core/object.h b/src/include/core/object.h index 016e718..19d6cc3 100644 --- a/src/include/core/object.h +++ b/src/include/core/object.h @@ -46,9 +46,8 @@ struct sdb_type { int (*init)(sdb_object_t *, va_list); void (*destroy)(sdb_object_t *); - sdb_object_t *(*clone)(const sdb_object_t *); }; -#define SDB_TYPE_INIT { 0, NULL, NULL, NULL } +#define SDB_TYPE_INIT { 0, NULL, NULL } struct sdb_object { sdb_type_t type; @@ -58,6 +57,10 @@ struct sdb_object { #define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1, NULL } #define SDB_OBJECT_TYPED_INIT(t) { (t), 1, NULL } +#define SDB_OBJECT_STATIC(name) { \ + /* type */ { sizeof(sdb_object_t), NULL, NULL }, \ + /* ref-cnt */ 1, (name) } + typedef struct { sdb_object_t super; void *data; @@ -69,6 +72,17 @@ typedef struct { #define SDB_OBJ_WRAPPER(obj) ((sdb_object_wrapper_t *)(obj)) #define SDB_CONST_OBJ_WRAPPER(obj) ((const sdb_object_wrapper_t *)(obj)) +/* + * Callback types for comparing objects or performing object lookup. + * Any function of type sdb_object_cmp_cb shall return a negative value, zero, + * or a positive value if the first object compares less than, equal to, or + * greater than the second object respectively. + * Any function of type sdb_object_lookup_cb shall return zero for all + * matching objects. + */ +typedef int (*sdb_object_cmp_cb)(const sdb_object_t *, const sdb_object_t *); +typedef int (*sdb_object_lookup_cb)(const sdb_object_t *, const void *user_data); + /* * sdb_object_create: * Allocates a new sdb_object_t of the specified 'name' and 'type'. The object @@ -79,7 +93,9 @@ typedef struct { * * The init function will be called with the remaining arguments passed to * sdb_object_create. If the init function fails (returns a non-zero value), - * the object will be destructed and destroyed. + * the object will be destructed and destroyed. In this case, the 'destroy' + * callback may be called on objects that were only half-way initialized. The + * callback has to handle that case correctly. * * The reference count of the new object will be 1. * @@ -89,6 +105,31 @@ typedef struct { */ sdb_object_t * sdb_object_create(const char *name, sdb_type_t type, ...); +sdb_object_t * +sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap); + +/* + * sdb_object_create_simple: + * Create a "simple" object without custom initialization and optional + * destructor. See the description of sdb_object_create for more details. + */ +sdb_object_t * +sdb_object_create_simple(const char *name, size_t size, + void (*destructor)(sdb_object_t *)); + +/* + * sdb_object_create_T: + * Create a simple object of type 't'. + */ +#define sdb_object_create_T(n,t) \ + sdb_object_create_simple((n), sizeof(t), NULL) + +/* + * sdb_object_create_dT: + * Create a simple object of dynamic type 't' using destructor 'd'. + */ +#define sdb_object_create_dT(n,t,d) \ + sdb_object_create_simple((n), sizeof(t), d) /* * sdb_object_create_wrapper: @@ -101,8 +142,8 @@ sdb_object_t * sdb_object_create_wrapper(const char *name, void *data, void (*destructor)(void *)); -#define SDB_OBJECT_WRAPPER_STATIC(obj, destructor) \ - { SDB_OBJECT_INIT, (obj), (destructor) } +#define SDB_OBJECT_WRAPPER_STATIC(obj) \ + { SDB_OBJECT_INIT, (obj), /* destructor */ NULL } /* * sdb_object_deref: @@ -121,19 +162,6 @@ sdb_object_deref(sdb_object_t *obj); void sdb_object_ref(sdb_object_t *obj); -/* - * sdb_object_clone: - * Clone an existing object using its type's 'clone' callback. The callback is - * responsible for correctly initializing a new object (which may be done - * using the object create function or the object's type's init function). - * - * Returns: - * - the cloned object on success - * - NULL on error or if no clone callback is available - */ -sdb_object_t * -sdb_object_clone(const sdb_object_t *obj); - /* * sdb_object_cmp_by_name: * Compare two objects by their name ignoring the case of the characters.