X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Finclude%2Fcore%2Fobject.h;h=819d11e849b48d4e1e2eb65d35247f4abe22af29;hb=fd3e376f4c7dc24c40717c056548c4e26e4e8b97;hp=cd91086a89377fa2c1a28bb754e1e1ffd285f77a;hpb=bf3b8e60b2fdc493c4e04b05ce67abf69ca9a4ff;p=sysdb.git diff --git a/src/include/core/object.h b/src/include/core/object.h index cd91086..819d11e 100644 --- a/src/include/core/object.h +++ b/src/include/core/object.h @@ -1,5 +1,5 @@ /* - * syscollector - src/include/core/object.h + * SysDB - src/include/core/object.h * Copyright (C) 2012 Sebastian 'tokkee' Harl * All rights reserved. * @@ -25,8 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SC_CORE_OBJECT_H -#define SC_CORE_OBJECT_H 1 +#ifndef SDB_CORE_OBJECT_H +#define SDB_CORE_OBJECT_H 1 #include #include @@ -35,35 +35,52 @@ extern "C" { #endif -struct sc_object; -typedef struct sc_object sc_object_t; +struct sdb_type; +typedef struct sdb_type sdb_type_t; -struct sc_object { - int ref_cnt; - void (*destructor)(sc_object_t *); +struct sdb_object; +typedef struct sdb_object sdb_object_t; + +struct sdb_type { size_t size; + + int (*init)(sdb_object_t *, va_list); + void (*destroy)(sdb_object_t *); }; -#define SC_OBJECT_INIT { 1, NULL, 0 } +#define SDB_TYPE_INIT { 0, NULL, NULL } + +struct sdb_object { + sdb_type_t type; + int ref_cnt; + char *name; +}; +#define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1, NULL } +#define SDB_OBJECT_TYPED_INIT(t) { (t), 1, NULL } typedef struct { - sc_object_t super; + sdb_object_t super; void *data; void (*destructor)(void *); -} sc_object_wrapper_t; +} sdb_object_wrapper_t; -#define SC_OBJ(obj) ((sc_object_t *)(obj)) -#define SC_OBJ_WRAPPER(obj) ((sc_object_wrapper_t *)(obj)) +#define SDB_OBJ(obj) ((sdb_object_t *)(obj)) +#define SDB_CONST_OBJ(obj) ((const sdb_object_t *)(obj)) +#define SDB_OBJ_WRAPPER(obj) ((sdb_object_wrapper_t *)(obj)) +#define SDB_CONST_OBJ_WRAPPER(obj) ((const sdb_object_wrapper_t *)(obj)) /* - * sc_object_create: - * Allocates a new sc_object_t of the specified 'size'. The object will be - * initialized to zero and then passed on to the 'init' function (if - * specified). If specified, the 'destructor' will be called, when the + * sdb_object_create: + * Allocates a new sdb_object_t of the specified 'name' and 'type'. The object + * will be initialized to zero and then passed on to the 'init' function (if + * specified). If specified, the 'destroy' callback will be called, when the * reference count drops to zero and before freeing the memory allocated by * the object itself. * - * If the init function fails (returns a non-zero value), the object will be - * destructed and destroyed. + * 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. 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. * @@ -71,39 +88,82 @@ typedef struct { * - the newly allocated object * - NULL on error */ -sc_object_t * -sc_object_create(size_t size, int (*init)(sc_object_t *, va_list), - void (*destructor)(sc_object_t *), ...); +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) /* - * sc_object_create_wrapper: - * Create a new sc_object_t wrapping some arbitrary other object. + * sdb_object_create_dT: + * Create a simple object of dynamic type 't' using destructor 'd'. */ -sc_object_t * -sc_object_create_wrapper(void *data, void (*destructor)(void *)); +#define sdb_object_create_dT(n,t,d) \ + sdb_object_create_simple((n), sizeof(t), d) /* - * sc_object_deref: + * sdb_object_create_wrapper: + * Create a new sdb_object_t wrapping some arbitrary other object. + * + * Creation and initialization of the wrapped object needs to happen outside + * of the SysDB object system. + */ +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) } + +/* + * sdb_object_deref: * Dereference the object and free the allocated memory in case the ref-count * drops to zero. In case a 'destructor' had been registered with the object, * it will be called before freeing the memory. */ void -sc_object_deref(sc_object_t *obj); +sdb_object_deref(sdb_object_t *obj); /* - * sc_object_ref: + * sdb_object_ref: * Take ownership of the specified object, that is, increment the reference * count by one. */ void -sc_object_ref(sc_object_t *obj); +sdb_object_ref(sdb_object_t *obj); + +/* + * sdb_object_cmp_by_name: + * Compare two objects by their name ignoring the case of the characters. + * + * Returns: + * - a negative value if o1 compares less than o2 + * - zero if o1 matches o2 + * - a positive value if o1 compares greater than o2 + */ +int +sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2); #ifdef __cplusplus } /* extern "C" */ #endif -#endif /* ! SC_CORE_OBJECT_H */ +#endif /* ! SDB_CORE_OBJECT_H */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */