1 /*
2 * SysDB - src/include/core/object.h
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #ifndef SDB_CORE_OBJECT_H
29 #define SDB_CORE_OBJECT_H 1
31 #include <stdarg.h>
32 #include <stddef.h>
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 struct sdb_type;
39 typedef struct sdb_type sdb_type_t;
41 struct sdb_object;
42 typedef struct sdb_object sdb_object_t;
44 struct sdb_type {
45 size_t size;
47 int (*init)(sdb_object_t *, va_list);
48 void (*destroy)(sdb_object_t *);
49 sdb_object_t *(*clone)(const sdb_object_t *);
50 };
51 #define SDB_TYPE_INIT { 0, NULL, NULL, NULL }
53 struct sdb_object {
54 sdb_type_t type;
55 int ref_cnt;
56 char *name;
57 };
58 #define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1, NULL }
59 #define SDB_OBJECT_TYPED_INIT(t) { (t), 1, NULL }
61 typedef struct {
62 sdb_object_t super;
63 void *data;
64 void (*destructor)(void *);
65 } sdb_object_wrapper_t;
67 #define SDB_OBJ(obj) ((sdb_object_t *)(obj))
68 #define SDB_CONST_OBJ(obj) ((const sdb_object_t *)(obj))
69 #define SDB_OBJ_WRAPPER(obj) ((sdb_object_wrapper_t *)(obj))
70 #define SDB_CONST_OBJ_WRAPPER(obj) ((const sdb_object_wrapper_t *)(obj))
72 /*
73 * sdb_object_create:
74 * Allocates a new sdb_object_t of the specified 'name' and 'type'. The object
75 * will be initialized to zero and then passed on to the 'init' function (if
76 * specified). If specified, the 'destroy' callback will be called, when the
77 * reference count drops to zero and before freeing the memory allocated by
78 * the object itself.
79 *
80 * The init function will be called with the remaining arguments passed to
81 * sdb_object_create. If the init function fails (returns a non-zero value),
82 * the object will be destructed and destroyed.
83 *
84 * The reference count of the new object will be 1.
85 *
86 * Returns:
87 * - the newly allocated object
88 * - NULL on error
89 */
90 sdb_object_t *
91 sdb_object_create(const char *name, sdb_type_t type, ...);
92 sdb_object_t *
93 sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap);
95 /*
96 * sdb_object_create_wrapper:
97 * Create a new sdb_object_t wrapping some arbitrary other object.
98 *
99 * Creation and initialization of the wrapped object needs to happen outside
100 * of the SysDB object system.
101 */
102 sdb_object_t *
103 sdb_object_create_wrapper(const char *name,
104 void *data, void (*destructor)(void *));
106 #define SDB_OBJECT_WRAPPER_STATIC(obj, destructor) \
107 { SDB_OBJECT_INIT, (obj), (destructor) }
109 /*
110 * sdb_object_deref:
111 * Dereference the object and free the allocated memory in case the ref-count
112 * drops to zero. In case a 'destructor' had been registered with the object,
113 * it will be called before freeing the memory.
114 */
115 void
116 sdb_object_deref(sdb_object_t *obj);
118 /*
119 * sdb_object_ref:
120 * Take ownership of the specified object, that is, increment the reference
121 * count by one.
122 */
123 void
124 sdb_object_ref(sdb_object_t *obj);
126 /*
127 * sdb_object_clone:
128 * Clone an existing object using its type's 'clone' callback. The callback is
129 * responsible for correctly initializing a new object (which may be done
130 * using the object create function or the object's type's init function).
131 *
132 * Returns:
133 * - the cloned object on success
134 * - NULL on error or if no clone callback is available
135 */
136 sdb_object_t *
137 sdb_object_clone(const sdb_object_t *obj);
139 /*
140 * sdb_object_cmp_by_name:
141 * Compare two objects by their name ignoring the case of the characters.
142 *
143 * Returns:
144 * - a negative value if o1 compares less than o2
145 * - zero if o1 matches o2
146 * - a positive value if o1 compares greater than o2
147 */
148 int
149 sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2);
151 #ifdef __cplusplus
152 } /* extern "C" */
153 #endif
155 #endif /* ! SDB_CORE_OBJECT_H */
157 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */