GnuCash c935c2f+
Loading...
Searching...
No Matches
qofclass.cpp
1/********************************************************************\
2 * qofclass.c -- provide QOF parameterized data objects *
3 * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21 * *
22\********************************************************************/
23
24#include <config.h>
25#include <glib.h>
26
27#include "qof.h"
28#include "qofclass-p.h"
29
30static QofLogModule log_module = QOF_MOD_CLASS;
31
32static GHashTable *classTable = NULL;
33static GHashTable *sortTable = NULL;
34static gboolean initialized = FALSE;
35
36static gboolean clear_table (gpointer key, gpointer value, gpointer user_data)
37{
38 g_hash_table_destroy (static_cast<GHashTable*>(value));
39 return TRUE;
40}
41
42/* *******************************************************************/
43/* PRIVATE FUNCTIONS */
44
45static gboolean check_init (void)
46{
47 if (initialized) return TRUE;
48
49 PERR("You must call qof_class_init() before using qof_class.");
50 return FALSE;
51}
52
53void
54qof_class_init(void)
55{
56 if (initialized) return;
57 initialized = TRUE;
58
59 classTable = g_hash_table_new (g_str_hash, g_str_equal);
60 sortTable = g_hash_table_new (g_str_hash, g_str_equal);
61}
62
63void
64qof_class_shutdown (void)
65{
66 if (!initialized) return;
67 initialized = FALSE;
68
69 g_hash_table_foreach_remove (classTable, clear_table, NULL);
70 g_hash_table_destroy (classTable);
71 g_hash_table_destroy (sortTable);
72}
73
75qof_class_get_default_sort (QofIdTypeConst obj_name)
76{
77 if (!obj_name) return NULL;
78 return reinterpret_cast<QofSortFunc>(g_hash_table_lookup (sortTable,
79 obj_name));
80}
81
82/* *******************************************************************/
83/* PUBLISHED API FUNCTIONS */
84
85void
87 QofSortFunc default_sort_function,
88 const QofParam *params)
89{
90 GHashTable *ht;
91 int i;
92
93 if (!obj_name) return;
94 if (!check_init()) return;
95
96 if (default_sort_function)
97 {
98 g_hash_table_insert (sortTable, (char *)obj_name,
99 reinterpret_cast<void*>(default_sort_function));
100 }
101
102 ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
103
104 /* If it doesn't already exist, create a new table for this object */
105 if (!ht)
106 {
107 ht = g_hash_table_new (g_str_hash, g_str_equal);
108 g_hash_table_insert (classTable, (char *)obj_name, ht);
109 }
110
111 /* At least right now, we allow dummy, parameterless objects,
112 * for testing purposes. Although I suppose that should be
113 * an error.. */
114 /* Now insert all the parameters */
115 if (params)
116 {
117 for (i = 0; params[i].param_name; i++)
118 g_hash_table_insert (ht,
119 (char *)params[i].param_name,
120 (gpointer)&(params[i]));
121 }
122}
123
124gboolean
126{
127 if (!obj_name) return FALSE;
128 if (!check_init()) return FALSE;
129
130 if (g_hash_table_lookup (classTable, obj_name)) return TRUE;
131
132 return FALSE;
133}
134
135const QofParam *
137 const char *parameter)
138{
139 GHashTable *ht;
140
141 g_return_val_if_fail (obj_name, NULL);
142 g_return_val_if_fail (parameter, NULL);
143 if (!check_init()) return NULL;
144
145 ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
146 if (!ht)
147 {
148 PWARN ("no object of type %s", obj_name);
149 return NULL;
150 }
151
152 return static_cast<QofParam*>(g_hash_table_lookup (ht, parameter));
153}
154
157 const char *parameter)
158{
159 const QofParam *prm;
160
161 g_return_val_if_fail (obj_name, NULL);
162 g_return_val_if_fail (parameter, NULL);
163
164 prm = qof_class_get_parameter (obj_name, parameter);
165 if (prm)
166 return prm->param_getfcn;
167
168 return NULL;
169}
170
173 const char *parameter)
174{
175 const QofParam *prm;
176
177 g_return_val_if_fail (obj_name, NULL);
178 g_return_val_if_fail (parameter, NULL);
179
180 prm = qof_class_get_parameter (obj_name, parameter);
181 if (prm)
182 return prm->param_setfcn;
183
184 return NULL;
185}
186
189 const char *param_name)
190{
191 const QofParam *prm;
192
193 if (!obj_name || !param_name) return NULL;
194
195 prm = qof_class_get_parameter (obj_name, param_name);
196 if (!prm) return NULL;
197
198 return (prm->param_type);
199}
200
201/* ================================================================ */
202
204{
206 gpointer data;
207};
208
209static void
210class_foreach_cb (gpointer key, gpointer item, gpointer arg)
211{
212 struct class_iterate *iter = static_cast<class_iterate*>(arg);
213 QofIdTypeConst id = static_cast<QofIdTypeConst>(key);
214
215 iter->fcn (id, iter->data);
216}
217
218void
219qof_class_foreach (QofClassForeachCB cb, gpointer user_data)
220{
221 struct class_iterate iter;
222
223 if (!cb) return;
224 if (!classTable) return;
225
226 iter.fcn = cb;
227 iter.data = user_data;
228
229 g_hash_table_foreach (classTable, class_foreach_cb, &iter);
230}
231
232/* ================================================================ */
233
235{
237 gpointer data;
238};
239
240static void
241param_foreach_cb (gpointer key, gpointer item, gpointer arg)
242{
243 struct parm_iterate *iter = static_cast<parm_iterate*>(arg);
244 QofParam *parm = static_cast<QofParam*>(item);
245
246 iter->fcn (parm, iter->data);
247}
248
249void
251 QofParamForeachCB cb, gpointer user_data)
252{
253 struct parm_iterate iter;
254 GHashTable *param_ht;
255
256 if (!obj_name || !cb) return;
257 if (!classTable) return;
258 param_ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
259 if (!param_ht) return;
260
261 iter.fcn = cb;
262 iter.data = user_data;
263
264 g_hash_table_foreach (param_ht, param_foreach_cb, &iter);
265}
266
268{
269 GList *list;
270};
271
272static void
273find_reference_param_cb(QofParam *param, gpointer user_data)
274{
275 struct param_ref_list *b;
276
277 b = (struct param_ref_list*)user_data;
278 if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL))
279 {
280 return;
281 }
282 if (0 == g_strcmp0(param->param_type, QOF_TYPE_STRING))
283 {
284 return;
285 }
286 if (0 == g_strcmp0(param->param_type, QOF_TYPE_NUMERIC))
287 {
288 return;
289 }
290 if (0 == g_strcmp0(param->param_type, QOF_TYPE_DATE))
291 {
292 return;
293 }
294 if (0 == g_strcmp0(param->param_type, QOF_TYPE_CHAR))
295 {
296 return;
297 }
298 if (0 == g_strcmp0(param->param_type, QOF_TYPE_DEBCRED))
299 {
300 return;
301 }
302 if (0 == g_strcmp0(param->param_type, QOF_TYPE_GUID))
303 {
304 return;
305 }
306 if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT32))
307 {
308 return;
309 }
310 if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT64))
311 {
312 return;
313 }
314 if (0 == g_strcmp0(param->param_type, QOF_TYPE_DOUBLE))
315 {
316 return;
317 }
318 if (0 == g_strcmp0(param->param_type, QOF_TYPE_KVP))
319 {
320 return;
321 }
322 if (0 == g_strcmp0(param->param_type, QOF_TYPE_BOOLEAN))
323 {
324 return;
325 }
326 if (0 == g_strcmp0(param->param_type, QOF_ID_BOOK))
327 {
328 return;
329 }
330 b->list = g_list_append(b->list, param);
331}
332
333GList*
335{
336 GList *ref_list;
337 struct param_ref_list b;
338
339 ref_list = NULL;
340 b.list = NULL;
341 qof_class_param_foreach(type, find_reference_param_cb, &b);
342 ref_list = g_list_copy(b.list);
343 return ref_list;
344}
345
346
347/* ============================= END OF FILE ======================== */
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
Definition qofclass.h:123
void qof_class_param_foreach(QofIdTypeConst obj_name, QofParamForeachCB cb, gpointer user_data)
Call the callback once for each parameter on the indicated object class.
Definition qofclass.cpp:250
GList * qof_class_get_referenceList(QofIdTypeConst type)
List of the parameters that could be references.
Definition qofclass.cpp:334
void qof_class_foreach(QofClassForeachCB cb, gpointer user_data)
Call the callback once for each object class that is registered with the system.
Definition qofclass.cpp:219
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
Definition qofclass.h:130
QofType qof_class_get_parameter_type(QofIdTypeConst obj_name, const char *param_name)
Return the core datatype of the specified object's parameter.
Definition qofclass.cpp:188
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_function, const QofParam *params)
This function registers a new object class with the Qof subsystem.
Definition qofclass.cpp:86
QofSetterFunc qof_class_get_parameter_setter(QofIdTypeConst obj_name, const char *parameter)
Return the object's parameter setter function.
Definition qofclass.cpp:172
void(* QofClassForeachCB)(QofIdTypeConst, gpointer)
Type definition for the class callback function.
Definition qofclass.h:228
QofAccessFunc qof_class_get_parameter_getter(QofIdTypeConst obj_name, const char *parameter)
Return the object's parameter getter function.
Definition qofclass.cpp:156
const QofParam * qof_class_get_parameter(QofIdTypeConst obj_name, const char *parameter)
Return the registered Parameter Definition for the requested parameter.
Definition qofclass.cpp:136
int(* QofSortFunc)(gconstpointer, gconstpointer)
This function is the default sort function for a particular object type.
Definition qofclass.h:168
const char * QofType
Type of Parameters (String, Date, Numeric, GncGUID, etc.)
Definition qofclass.h:104
void(* QofParamForeachCB)(QofParam *, gpointer user_data)
Type definition for the parameter callback function.
Definition qofclass.h:236
gboolean qof_class_is_registered(QofIdTypeConst obj_name)
An example:
Definition qofclass.cpp:125
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
Definition qofid.h:82
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250