Class
[Object: Dynamic Object Class Framework]


Detailed Description

This file defines a class messaging system reminiscent of traditional OO-style setter and getter interfaces to object properties. A C-language object can declare a collection of setters and getters on itself that can then be used to perform run-time (as opposed to compile-time) bindings to the object.

To put it differently, a QOF class is a set of parameter getters and setters that are associated with an object type. Given a pointer to some thing, the setters and getters can be used to get and set values out of that thing. Note that the pointer to "some thing" need not be a pointer to a QOF Entity or Instance (although QOF classes are more interesting when used with Entities/Instances). What "some thing" is defined entirely by the programmer declaring a new QOF Class.

Because a QOF Class associates getters and setters with a type, one can then ask, at run time, what parameters are associated with a given type, even if those parameters were not known at compile time. Thus, a QOF Class is sort-of like a DynAny implementation. QOF classes can be used to provide "object introspection", i.e. asking object to describe itself.

The QOF Query subsystem depends on QOF classes having been declared; the Query uses the getters to get values associated with particular instances.

A QofAccessFunc or QofSetterFunc do not need to be public functions, if you need to add functions to an object with an established API, define the additional QOF routines as static. Only the register routine needs to be public.


Files

file  qofclass.h
 API for registering paramters on objects.

Data Structures

struct  _QofParam

Core types

Core data types for objects that can be used in parameters. Note that QofIdTypes may also be used and will create a single reference between two known objects.

#define QOF_TYPE_STRING   "string"
#define QOF_TYPE_DATE   "date"
#define QOF_TYPE_NUMERIC   "numeric"
#define QOF_TYPE_DEBCRED   "debcred"
#define QOF_TYPE_GUID   "guid"
#define QOF_TYPE_INT32   "gint32"
#define QOF_TYPE_INT64   "gint64"
#define QOF_TYPE_DOUBLE   "double"
#define QOF_TYPE_BOOLEAN   "boolean"
#define QOF_TYPE_KVP   "kvp"
#define QOF_TYPE_CHAR   "character"
#define QOF_TYPE_COLLECT   "collection"

Defines

#define QOF_MOD_CLASS   "qof.class"

Typedefs

typedef const char * QofType
typedef _QofParam QofParam
typedef gpointer(*) QofAccessFunc (gpointer object, const QofParam *param)
typedef void(*) QofSetterFunc (gpointer, gpointer)
typedef gint(*) QofCompareFunc (gpointer a, gpointer b, gint compare_options, QofParam *getter)
typedef int(*) QofSortFunc (gconstpointer, gconstpointer)
typedef void(*) QofClassForeachCB (QofIdTypeConst, gpointer)
typedef void(*) QofParamForeachCB (QofParam *, gpointer user_data)

Functions

void qof_class_register (QofIdTypeConst obj_name, QofSortFunc default_sort_fcn, const QofParam *params)
gboolean qof_class_is_registered (QofIdTypeConst obj_name)
QofType qof_class_get_parameter_type (QofIdTypeConst obj_name, const char *param_name)
const QofParamqof_class_get_parameter (QofIdTypeConst obj_name, const char *parameter)
QofAccessFunc qof_class_get_parameter_getter (QofIdTypeConst obj_name, const char *parameter)
QofSetterFunc qof_class_get_parameter_setter (QofIdTypeConst obj_name, const char *parameter)
void qof_class_foreach (QofClassForeachCB, gpointer user_data)
void qof_class_param_foreach (QofIdTypeConst obj_name, QofParamForeachCB, gpointer user_data)
GList * qof_class_get_referenceList (QofIdTypeConst type)
 List of the parameters that could be references.


Define Documentation

#define QOF_TYPE_COLLECT   "collection"

secondary collections are used for one-to-many references between entities and are implemented using QofCollection. These are NOT the same as the main collections in the QofBook.

  1. Each QofCollection contains one or many entities - *all* of a single type.
  2. The entity type within the collection can be determined at run time.
  3. Easy conversions to GList or whatever in the param_setfcn handler.
  4. Each parameter can have it's own collection.
  5. Each entity can have a different *type* of collection to it's siblings, provided that it is acceptable to the set function.
  6. Each object decides which types are acceptable for which parameter in the set functions. This is then part of the API for that object.

QOF_TYPE_COLLECT has two functions, both related to one-to-many links:

If the set function can handle it, it could also be used for true one-to-many links: one object linked to many entities of many types.

n.b. Always subject to each collection holding only one type at runtime. (otherwise use books).

Definition at line 96 of file qofclass.h.


Typedef Documentation

typedef gpointer(*) QofAccessFunc(gpointer object, const QofParam *param)

The QofAccessFunc defines an arbitrary function pointer for access functions. This is needed because C doesn't have templates, so we just cast a lot. Real functions must be of the form:

param_type getter_func (object_type *self); or param_type getter_func (object_type *self, QofParam *param);

The additional argument 'param' allows generic getter functions to be implemented, because this argument provides for a way to identify the expected getter_func return type at runtime. It also provides a place for the user to hang additional user-defined data.

Definition at line 145 of file qofclass.h.

typedef void(*) QofClassForeachCB(QofIdTypeConst, gpointer)

Type definition for the class callback function.

Definition at line 250 of file qofclass.h.

typedef void(*) QofParamForeachCB(QofParam *, gpointer user_data)

Type definition for the paramter callback function.

Definition at line 258 of file qofclass.h.

typedef void(*) QofSetterFunc(gpointer, gpointer)

The QofSetterFunc defines an function pointer for parameter setters. Real functions must be of the form:

void setter_func (object_type *self, param_type *param);

Definition at line 152 of file qofclass.h.

typedef int(*) QofSortFunc(gconstpointer, gconstpointer)

This function is the default sort function for a particular object type

Definition at line 190 of file qofclass.h.

typedef const char* QofType

Type of Paramters (String, Date, Numeric, GUID, etc.)

Definition at line 126 of file qofclass.h.


Function Documentation

void qof_class_foreach ( QofClassForeachCB  ,
gpointer  user_data 
)

Call the callback once for each object class that is registered with the system. The 'user_data' is passed back to the callback.

Definition at line 217 of file qofclass.c.

00218 {
00219   struct class_iterate iter;
00220 
00221   if (!cb) return;
00222   if (!classTable) return;
00223 
00224   iter.fcn = cb;
00225   iter.data = user_data;
00226 
00227   g_hash_table_foreach (classTable, class_foreach_cb, &iter);
00228 }

const QofParam* qof_class_get_parameter ( QofIdTypeConst  obj_name,
const char *  parameter 
)

Return the registered Parameter Definition for the requested parameter

Definition at line 135 of file qofclass.c.

00137 {
00138   GHashTable *ht;
00139 
00140   g_return_val_if_fail (obj_name, NULL);
00141   g_return_val_if_fail (parameter, NULL);
00142   if (!check_init()) return NULL;
00143 
00144   ht = g_hash_table_lookup (classTable, obj_name);
00145   if (!ht)
00146   {
00147     PWARN ("no object of type %s", obj_name);
00148     return NULL;
00149   }
00150 
00151   return (g_hash_table_lookup (ht, parameter));
00152 }

QofAccessFunc qof_class_get_parameter_getter ( QofIdTypeConst  obj_name,
const char *  parameter 
)

Return the object's parameter getter function

Definition at line 155 of file qofclass.c.

00157 {
00158   const QofParam *prm;
00159 
00160   g_return_val_if_fail (obj_name, NULL);
00161   g_return_val_if_fail (parameter, NULL);
00162 
00163   prm = qof_class_get_parameter (obj_name, parameter);
00164   if (prm)
00165     return prm->param_getfcn;
00166 
00167   return NULL;
00168 }

QofSetterFunc qof_class_get_parameter_setter ( QofIdTypeConst  obj_name,
const char *  parameter 
)

Return the object's parameter setter function

Definition at line 171 of file qofclass.c.

00173 {
00174   const QofParam *prm;
00175 
00176   g_return_val_if_fail (obj_name, NULL);
00177   g_return_val_if_fail (parameter, NULL);
00178 
00179   prm = qof_class_get_parameter (obj_name, parameter);
00180   if (prm)
00181     return prm->param_setfcn;
00182 
00183   return NULL;
00184 }

QofType qof_class_get_parameter_type ( QofIdTypeConst  obj_name,
const char *  param_name 
)

Return the core datatype of the specified object's parameter

Definition at line 187 of file qofclass.c.

00189 {
00190   const QofParam *prm;
00191 
00192   if (!obj_name || !param_name) return NULL;
00193 
00194   prm = qof_class_get_parameter (obj_name, param_name);
00195   if (!prm) return NULL;
00196 
00197   return (prm->param_type);
00198 }

GList* qof_class_get_referenceList ( QofIdTypeConst  type  ) 

List of the parameters that could be references.

Simple check to return a GList of all parameters of this object type that are not known QOF data types. Used for partial QofBook support, see QofInstanceReference

Definition at line 292 of file qofclass.c.

00293 {
00294         GList *ref_list;
00295         struct param_ref_list b;
00296 
00297         ref_list = NULL;
00298         b.list = NULL;
00299         qof_class_param_foreach(type, find_reference_param_cb, &b);
00300         ref_list = g_list_copy(b.list);
00301         return ref_list;
00302 }

gboolean qof_class_is_registered ( QofIdTypeConst  obj_name  ) 

Return true if the the indicated type is registered, else return FALSE.

Definition at line 124 of file qofclass.c.

00125 {
00126   if (!obj_name) return FALSE;
00127   if (!check_init()) return FALSE;
00128 
00129   if (g_hash_table_lookup (classTable, obj_name)) return TRUE;
00130 
00131   return FALSE;
00132 }

void qof_class_param_foreach ( QofIdTypeConst  obj_name,
QofParamForeachCB  ,
gpointer  user_data 
)

Call the callback once for each parameter on the indicated object class. The 'user_data' is passed back to the callback.

Definition at line 247 of file qofclass.c.

00249 {
00250   struct parm_iterate iter;
00251   GHashTable *param_ht;
00252 
00253   if (!obj_name || !cb) return;
00254   if (!classTable) return;
00255   param_ht = g_hash_table_lookup (classTable, obj_name);
00256   if (!param_ht) return;
00257 
00258   iter.fcn = cb;
00259   iter.data = user_data;
00260 
00261   g_hash_table_foreach (param_ht, param_foreach_cb, &iter);
00262 }

void qof_class_register ( QofIdTypeConst  obj_name,
QofSortFunc  default_sort_fcn,
const QofParam params 
)

This function registers a new object class with the Qof subsystem. In particular, it registers the set of setters and getters for controlling the object. The getters are typically used by the query subsystem to query type specific data. Note that there is no particular requirement for there to be a setter for every getter or even vice-versa, nor is there any requirement for these to map 'cleanly' or orthogonally to the underlying object. The parameters are really just a set of value setting and getting routines.

The "params" argument must be a NULL-terminated array of QofParam. It may be NULL if there are no parameters to be registered.

Definition at line 86 of file qofclass.c.

00089 {
00090   GHashTable *ht;
00091   int i;
00092 
00093   if (!obj_name) return;
00094   if (!check_init()) return;
00095 
00096   if (default_sort_function)
00097   {
00098     g_hash_table_insert (sortTable, (char *)obj_name, default_sort_function);
00099   }
00100 
00101   ht = g_hash_table_lookup (classTable, obj_name);
00102 
00103   /* If it doesn't already exist, create a new table for this object */
00104   if (!ht)
00105   {
00106     ht = g_hash_table_new (g_str_hash, g_str_equal);
00107     g_hash_table_insert (classTable, (char *)obj_name, ht);
00108   }
00109 
00110   /* At least right now, we allow dummy, parameterless objects,
00111    * for testing purposes.  Although I suppose that should be
00112    * an error..  */
00113   /* Now insert all the parameters */
00114   if (params)
00115   {
00116     for (i = 0; params[i].param_name; i++)
00117       g_hash_table_insert (ht,
00118                (char *)params[i].param_name,
00119                (gpointer)&(params[i]));
00120   }
00121 }


Generated on Mon Sep 8 05:04:43 2008 for GnuCash by  doxygen 1.5.2