28 #include "qofclass-p.h" 30 static QofLogModule log_module = QOF_MOD_CLASS;
32 static GHashTable *classTable = NULL;
33 static GHashTable *sortTable = NULL;
34 static gboolean initialized = FALSE;
36 static gboolean clear_table (gpointer key, gpointer value, gpointer user_data)
38 g_hash_table_destroy (static_cast<GHashTable*>(value));
45 static gboolean check_init (
void)
47 if (initialized)
return TRUE;
49 PERR(
"You must call qof_class_init() before using qof_class.");
56 if (initialized)
return;
59 classTable = g_hash_table_new (g_str_hash, g_str_equal);
60 sortTable = g_hash_table_new (g_str_hash, g_str_equal);
64 qof_class_shutdown (
void)
66 if (!initialized)
return;
69 g_hash_table_foreach_remove (classTable, clear_table, NULL);
70 g_hash_table_destroy (classTable);
71 g_hash_table_destroy (sortTable);
77 if (!obj_name)
return NULL;
78 return reinterpret_cast<QofSortFunc>(g_hash_table_lookup (sortTable,
88 const QofParam *params)
93 if (!obj_name)
return;
94 if (!check_init())
return;
96 if (default_sort_function)
98 g_hash_table_insert (sortTable, (
char *)obj_name,
99 reinterpret_cast<void*>(default_sort_function));
102 ht =
static_cast<GHashTable*
>(g_hash_table_lookup (classTable, obj_name));
107 ht = g_hash_table_new (g_str_hash, g_str_equal);
108 g_hash_table_insert (classTable, (
char *)obj_name, ht);
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]));
127 if (!obj_name)
return FALSE;
128 if (!check_init())
return FALSE;
130 if (g_hash_table_lookup (classTable, obj_name))
return TRUE;
137 const char *parameter)
141 g_return_val_if_fail (obj_name, NULL);
142 g_return_val_if_fail (parameter, NULL);
143 if (!check_init())
return NULL;
145 ht =
static_cast<GHashTable*
>(g_hash_table_lookup (classTable, obj_name));
148 PWARN (
"no object of type %s", obj_name);
152 return static_cast<QofParam*
>(g_hash_table_lookup (ht, parameter));
157 const char *parameter)
161 g_return_val_if_fail (obj_name, NULL);
162 g_return_val_if_fail (parameter, NULL);
166 return prm->param_getfcn;
173 const char *parameter)
177 g_return_val_if_fail (obj_name, NULL);
178 g_return_val_if_fail (parameter, NULL);
182 return prm->param_setfcn;
189 const char *param_name)
193 if (!obj_name || !param_name)
return NULL;
196 if (!prm)
return NULL;
198 return (prm->param_type);
210 class_foreach_cb (gpointer key, gpointer item, gpointer arg)
215 iter->fcn (
id, iter->data);
224 if (!classTable)
return;
227 iter.data = user_data;
229 g_hash_table_foreach (classTable, class_foreach_cb, &iter);
241 param_foreach_cb (gpointer key, gpointer item, gpointer arg)
244 QofParam *parm =
static_cast<QofParam*
>(item);
246 iter->fcn (parm, iter->data);
254 GHashTable *param_ht;
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;
262 iter.data = user_data;
264 g_hash_table_foreach (param_ht, param_foreach_cb, &iter);
273 find_reference_param_cb(QofParam *param, gpointer user_data)
278 if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL))
282 if (0 == g_strcmp0(param->param_type, QOF_TYPE_STRING))
286 if (0 == g_strcmp0(param->param_type, QOF_TYPE_NUMERIC))
290 if (0 == g_strcmp0(param->param_type, QOF_TYPE_DATE))
294 if (0 == g_strcmp0(param->param_type, QOF_TYPE_CHAR))
298 if (0 == g_strcmp0(param->param_type, QOF_TYPE_DEBCRED))
302 if (0 == g_strcmp0(param->param_type, QOF_TYPE_GUID))
306 if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT32))
310 if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT64))
314 if (0 == g_strcmp0(param->param_type, QOF_TYPE_DOUBLE))
318 if (0 == g_strcmp0(param->param_type, QOF_TYPE_KVP))
322 if (0 == g_strcmp0(param->param_type, QOF_TYPE_BOOLEAN))
326 if (0 == g_strcmp0(param->param_type, QOF_ID_BOOK))
330 b->list = g_list_append(b->list, param);
342 ref_list = g_list_copy(b.list);
gboolean qof_class_is_registered(QofIdTypeConst obj_name)
An example:
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.
const char * QofType
Type of Parameters (String, Date, Numeric, GncGUID, etc.)
const gchar * QofIdTypeConst
QofIdTypeConst declaration.
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.
void(* QofClassForeachCB)(QofIdTypeConst, gpointer)
Type definition for the class callback function.
int(* QofSortFunc)(gconstpointer, gconstpointer)
This function is the default sort function for a particular object type.
#define PERR(format, args...)
Log a serious error.
QofAccessFunc qof_class_get_parameter_getter(QofIdTypeConst obj_name, const char *parameter)
Return the object's parameter getter function.
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
#define PWARN(format, args...)
Log a warning.
const QofParam * qof_class_get_parameter(QofIdTypeConst obj_name, const char *parameter)
Return the registered Parameter Definition for the requested parameter.
void qof_class_foreach(QofClassForeachCB cb, gpointer user_data)
Call the callback once for each object class that is registered with the system.
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
GList * qof_class_get_referenceList(QofIdTypeConst type)
List of the parameters that could be references.
QofSetterFunc qof_class_get_parameter_setter(QofIdTypeConst obj_name, const char *parameter)
Return the object's parameter setter function.
void(* QofParamForeachCB)(QofParam *, gpointer user_data)
Type definition for the parameter callback function.
QofType qof_class_get_parameter_type(QofIdTypeConst obj_name, const char *param_name)
Return the core datatype of the specified object's parameter.