Code

object: Be more specific about requirements for the 'destroy' callback.
[sysdb.git] / src / include / core / object.h
index cab77d0816973ca242620c8320c22c8eca07682c..9169c441383b292803a67efc35e91147528aa1e2 100644 (file)
@@ -52,8 +52,10 @@ struct sdb_type {
 struct sdb_object {
        sdb_type_t type;
        int ref_cnt;
+       char *name;
 };
-#define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1 }
+#define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1, NULL }
+#define SDB_OBJECT_TYPED_INIT(t) { (t), 1, NULL }
 
 typedef struct {
        sdb_object_t super;
@@ -62,19 +64,23 @@ typedef struct {
 } sdb_object_wrapper_t;
 
 #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))
 
 /*
  * sdb_object_create:
- * Allocates a new sdb_object_t of the specified 'type'. The object will be
- * initialized to zero and then passed on to the 'init' function (if
+ * 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.
  *
  * 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.
  *
@@ -83,7 +89,9 @@ typedef struct {
  *  - NULL on error
  */
 sdb_object_t *
-sdb_object_create(sdb_type_t type, ...);
+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_wrapper:
@@ -93,7 +101,8 @@ sdb_object_create(sdb_type_t type, ...);
  * of the SysDB object system.
  */
 sdb_object_t *
-sdb_object_create_wrapper(void *data, void (*destructor)(void *));
+sdb_object_create_wrapper(const char *name,
+               void *data, void (*destructor)(void *));
 
 #define SDB_OBJECT_WRAPPER_STATIC(obj, destructor) \
        { SDB_OBJECT_INIT, (obj), (destructor) }
@@ -115,6 +124,18 @@ sdb_object_deref(sdb_object_t *obj);
 void
 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