Code

parser: Let the TIMESERIES command accept optional data-source names.
[sysdb.git] / src / include / core / object.h
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 };
50 #define SDB_TYPE_INIT { 0, NULL, NULL }
52 struct sdb_object {
53         sdb_type_t type;
54         int ref_cnt;
55         char *name;
56 };
57 #define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1, NULL }
58 #define SDB_OBJECT_TYPED_INIT(t) { (t), 1, NULL }
60 #define SDB_OBJECT_STATIC(name) { \
61         /* type */ { sizeof(sdb_object_t), NULL, NULL }, \
62         /* ref-cnt */ 1, (name) }
64 typedef struct {
65         sdb_object_t super;
66         void *data;
67         void (*destructor)(void *);
68 } sdb_object_wrapper_t;
70 #define SDB_OBJ(obj) ((sdb_object_t *)(obj))
71 #define SDB_CONST_OBJ(obj) ((const sdb_object_t *)(obj))
72 #define SDB_OBJ_WRAPPER(obj) ((sdb_object_wrapper_t *)(obj))
73 #define SDB_CONST_OBJ_WRAPPER(obj) ((const sdb_object_wrapper_t *)(obj))
75 /*
76  * Callback types for comparing objects or performing object lookup.
77  * Any function of type sdb_object_cmp_cb shall return a negative value, zero,
78  * or a positive value if the first object compares less than, equal to, or
79  * greater than the second object respectively.
80  * Any function of type sdb_object_lookup_cb shall return zero for all
81  * matching objects.
82  */
83 typedef int (*sdb_object_cmp_cb)(const sdb_object_t *, const sdb_object_t *);
84 typedef int (*sdb_object_lookup_cb)(const sdb_object_t *, const void *user_data);
86 /*
87  * sdb_object_create:
88  * Allocates a new sdb_object_t of the specified 'name' and 'type'. The object
89  * will be initialized to zero and then passed on to the 'init' function (if
90  * specified). If specified, the 'destroy' callback will be called, when the
91  * reference count drops to zero and before freeing the memory allocated by
92  * the object itself.
93  *
94  * The init function will be called with the remaining arguments passed to
95  * sdb_object_create. If the init function fails (returns a non-zero value),
96  * the object will be destructed and destroyed. In this case, the 'destroy'
97  * callback may be called on objects that were only half-way initialized. The
98  * callback has to handle that case correctly.
99  *
100  * The reference count of the new object will be 1.
101  *
102  * Returns:
103  *  - the newly allocated object
104  *  - NULL on error
105  */
106 sdb_object_t *
107 sdb_object_create(const char *name, sdb_type_t type, ...);
108 sdb_object_t *
109 sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap);
111 /*
112  * sdb_object_create_simple:
113  * Create a "simple" object without custom initialization and optional
114  * destructor. See the description of sdb_object_create for more details.
115  */
116 sdb_object_t *
117 sdb_object_create_simple(const char *name, size_t size,
118                 void (*destructor)(sdb_object_t *));
120 /*
121  * sdb_object_create_T:
122  * Create a simple object of type 't'.
123  */
124 #define sdb_object_create_T(n,t) \
125         sdb_object_create_simple((n), sizeof(t), NULL)
127 /*
128  * sdb_object_create_dT:
129  * Create a simple object of dynamic type 't' using destructor 'd'.
130  */
131 #define sdb_object_create_dT(n,t,d) \
132         sdb_object_create_simple((n), sizeof(t), d)
134 /*
135  * sdb_object_create_wrapper:
136  * Create a new sdb_object_t wrapping some arbitrary other object.
137  *
138  * Creation and initialization of the wrapped object needs to happen outside
139  * of the SysDB object system.
140  */
141 sdb_object_t *
142 sdb_object_create_wrapper(const char *name,
143                 void *data, void (*destructor)(void *));
145 #define SDB_OBJECT_WRAPPER_STATIC(obj) \
146         { SDB_OBJECT_INIT, (obj), /* destructor */ NULL }
148 /*
149  * sdb_object_deref:
150  * Dereference the object and free the allocated memory in case the ref-count
151  * drops to zero. In case a 'destructor' had been registered with the object,
152  * it will be called before freeing the memory.
153  */
154 void
155 sdb_object_deref(sdb_object_t *obj);
157 /*
158  * sdb_object_ref:
159  * Take ownership of the specified object, that is, increment the reference
160  * count by one.
161  */
162 void
163 sdb_object_ref(sdb_object_t *obj);
165 /*
166  * sdb_object_cmp_by_name:
167  * Compare two objects by their name ignoring the case of the characters.
168  *
169  * Returns:
170  *  - a negative value if o1 compares less than o2
171  *  - zero if o1 matches o2
172  *  - a positive value if o1 compares greater than o2
173  */
174 int
175 sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2);
177 #ifdef __cplusplus
178 } /* extern "C" */
179 #endif
181 #endif /* ! SDB_CORE_OBJECT_H */
183 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */