LCOV - code coverage report
Current view: top level - libgnucash/engine - qofclass.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 45.3 % 128 58
Test Date: 2026-11-02 18:19:43 Functions: 58.8 % 17 10
Legend: Lines:     hit not hit

            Line data    Source code
       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              : 
      30              : static QofLogModule log_module = QOF_MOD_CLASS;
      31              : 
      32              : static GHashTable *classTable = NULL;
      33              : static GHashTable *sortTable = NULL;
      34              : static gboolean initialized = FALSE;
      35              : 
      36          559 : static gboolean clear_table (gpointer key, gpointer value, gpointer user_data)
      37              : {
      38          559 :     g_hash_table_destroy (static_cast<GHashTable*>(value));
      39          559 :     return TRUE;
      40              : }
      41              : 
      42              : /* *******************************************************************/
      43              : /* PRIVATE FUNCTIONS */
      44              : 
      45        10876 : static gboolean check_init (void)
      46              : {
      47        10876 :     if (initialized) return TRUE;
      48              : 
      49            0 :     PERR("You must call qof_class_init() before using qof_class.");
      50            0 :     return FALSE;
      51              : }
      52              : 
      53              : void
      54          118 : qof_class_init(void)
      55              : {
      56          118 :     if (initialized) return;
      57          110 :     initialized = TRUE;
      58              : 
      59          110 :     classTable = g_hash_table_new (g_str_hash, g_str_equal);
      60          110 :     sortTable = g_hash_table_new (g_str_hash, g_str_equal);
      61              : }
      62              : 
      63              : void
      64           43 : qof_class_shutdown (void)
      65              : {
      66           43 :     if (!initialized) return;
      67           43 :     initialized = FALSE;
      68              : 
      69           43 :     g_hash_table_foreach_remove (classTable, clear_table, NULL);
      70           43 :     g_hash_table_destroy (classTable);
      71           43 :     g_hash_table_destroy (sortTable);
      72              : }
      73              : 
      74              : QofSortFunc
      75          411 : qof_class_get_default_sort (QofIdTypeConst obj_name)
      76              : {
      77          411 :     if (!obj_name) return NULL;
      78          411 :     return reinterpret_cast<QofSortFunc>(g_hash_table_lookup (sortTable,
      79          411 :                                                               obj_name));
      80              : }
      81              : 
      82              : /* *******************************************************************/
      83              : /* PUBLISHED API FUNCTIONS */
      84              : 
      85              : void
      86         2168 : qof_class_register (QofIdTypeConst obj_name,
      87              :                     QofSortFunc default_sort_function,
      88              :                     const QofParam *params)
      89              : {
      90              :     GHashTable *ht;
      91              :     int i;
      92              : 
      93         2168 :     if (!obj_name) return;
      94         2168 :     if (!check_init()) return;
      95              : 
      96         2168 :     if (default_sort_function)
      97              :     {
      98         1424 :         g_hash_table_insert (sortTable, (char *)obj_name,
      99              :                              reinterpret_cast<void*>(default_sort_function));
     100              :     }
     101              : 
     102         2168 :     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         2168 :     if (!ht)
     106              :     {
     107         1889 :         ht = g_hash_table_new (g_str_hash, g_str_equal);
     108         1889 :         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         2168 :     if (params)
     116              :     {
     117        21219 :         for (i = 0; params[i].param_name; i++)
     118        19300 :             g_hash_table_insert (ht,
     119        19300 :                                  (char *)params[i].param_name,
     120        19300 :                                  (gpointer)&(params[i]));
     121              :     }
     122              : }
     123              : 
     124              : gboolean
     125            0 : qof_class_is_registered (QofIdTypeConst obj_name)
     126              : {
     127            0 :     if (!obj_name) return FALSE;
     128            0 :     if (!check_init()) return FALSE;
     129              : 
     130            0 :     if (g_hash_table_lookup (classTable, obj_name)) return TRUE;
     131              : 
     132            0 :     return FALSE;
     133              : }
     134              : 
     135              : const QofParam *
     136         8711 : qof_class_get_parameter (QofIdTypeConst obj_name,
     137              :                          const char *parameter)
     138              : {
     139              :     GHashTable *ht;
     140              : 
     141         8711 :     g_return_val_if_fail (obj_name, NULL);
     142         8709 :     g_return_val_if_fail (parameter, NULL);
     143         8708 :     if (!check_init()) return NULL;
     144              : 
     145         8708 :     ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
     146         8708 :     if (!ht)
     147              :     {
     148            0 :         PWARN ("no object of type %s", obj_name);
     149            0 :         return NULL;
     150              :     }
     151              : 
     152         8708 :     return static_cast<QofParam*>(g_hash_table_lookup (ht, parameter));
     153              : }
     154              : 
     155              : QofAccessFunc
     156           80 : qof_class_get_parameter_getter (QofIdTypeConst obj_name,
     157              :                                 const char *parameter)
     158              : {
     159              :     const QofParam *prm;
     160              : 
     161           80 :     g_return_val_if_fail (obj_name, NULL);
     162           80 :     g_return_val_if_fail (parameter, NULL);
     163              : 
     164           80 :     prm = qof_class_get_parameter (obj_name, parameter);
     165           80 :     if (prm)
     166           80 :         return prm->param_getfcn;
     167              : 
     168            0 :     return NULL;
     169              : }
     170              : 
     171              : QofSetterFunc
     172          140 : qof_class_get_parameter_setter (QofIdTypeConst obj_name,
     173              :                                 const char *parameter)
     174              : {
     175              :     const QofParam *prm;
     176              : 
     177          140 :     g_return_val_if_fail (obj_name, NULL);
     178          140 :     g_return_val_if_fail (parameter, NULL);
     179              : 
     180          140 :     prm = qof_class_get_parameter (obj_name, parameter);
     181          140 :     if (prm)
     182          140 :         return prm->param_setfcn;
     183              : 
     184            0 :     return NULL;
     185              : }
     186              : 
     187              : QofType
     188            1 : qof_class_get_parameter_type (QofIdTypeConst obj_name,
     189              :                               const char *param_name)
     190              : {
     191              :     const QofParam *prm;
     192              : 
     193            1 :     if (!obj_name || !param_name) return NULL;
     194              : 
     195            1 :     prm = qof_class_get_parameter (obj_name, param_name);
     196            1 :     if (!prm) return NULL;
     197              : 
     198            1 :     return (prm->param_type);
     199              : }
     200              : 
     201              : /* ================================================================ */
     202              : 
     203              : struct class_iterate
     204              : {
     205              :     QofClassForeachCB   fcn;
     206              :     gpointer            data;
     207              : };
     208              : 
     209              : static void
     210            0 : class_foreach_cb (gpointer key, gpointer item, gpointer arg)
     211              : {
     212            0 :     struct class_iterate *iter = static_cast<class_iterate*>(arg);
     213            0 :     QofIdTypeConst id = static_cast<QofIdTypeConst>(key);
     214              : 
     215            0 :     iter->fcn (id, iter->data);
     216            0 : }
     217              : 
     218              : void
     219            0 : qof_class_foreach (QofClassForeachCB cb, gpointer user_data)
     220              : {
     221              :     struct class_iterate iter;
     222              : 
     223            0 :     if (!cb) return;
     224            0 :     if (!classTable) return;
     225              : 
     226            0 :     iter.fcn = cb;
     227            0 :     iter.data = user_data;
     228              : 
     229            0 :     g_hash_table_foreach (classTable, class_foreach_cb, &iter);
     230              : }
     231              : 
     232              : /* ================================================================ */
     233              : 
     234              : struct parm_iterate
     235              : {
     236              :     QofParamForeachCB   fcn;
     237              :     gpointer            data;
     238              : };
     239              : 
     240              : static void
     241            0 : param_foreach_cb (gpointer key, gpointer item, gpointer arg)
     242              : {
     243            0 :     struct parm_iterate *iter = static_cast<parm_iterate*>(arg);
     244            0 :     QofParam *parm = static_cast<QofParam*>(item);
     245              : 
     246            0 :     iter->fcn (parm, iter->data);
     247            0 : }
     248              : 
     249              : void
     250            0 : qof_class_param_foreach (QofIdTypeConst obj_name,
     251              :                          QofParamForeachCB cb, gpointer user_data)
     252              : {
     253              :     struct parm_iterate iter;
     254              :     GHashTable *param_ht;
     255              : 
     256            0 :     if (!obj_name || !cb) return;
     257            0 :     if (!classTable) return;
     258            0 :     param_ht = static_cast<GHashTable*>(g_hash_table_lookup (classTable, obj_name));
     259            0 :     if (!param_ht) return;
     260              : 
     261            0 :     iter.fcn = cb;
     262            0 :     iter.data = user_data;
     263              : 
     264            0 :     g_hash_table_foreach (param_ht, param_foreach_cb, &iter);
     265              : }
     266              : 
     267              : struct param_ref_list
     268              : {
     269              :     GList *list;
     270              : };
     271              : 
     272              : static void
     273            0 : find_reference_param_cb(QofParam *param, gpointer user_data)
     274              : {
     275              :     struct param_ref_list *b;
     276              : 
     277            0 :     b = (struct param_ref_list*)user_data;
     278            0 :     if ((param->param_getfcn == NULL) || (param->param_setfcn == NULL))
     279              :     {
     280            0 :         return;
     281              :     }
     282            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_STRING))
     283              :     {
     284            0 :         return;
     285              :     }
     286            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_NUMERIC))
     287              :     {
     288            0 :         return;
     289              :     }
     290            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_DATE))
     291              :     {
     292            0 :         return;
     293              :     }
     294            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_CHAR))
     295              :     {
     296            0 :         return;
     297              :     }
     298            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_DEBCRED))
     299              :     {
     300            0 :         return;
     301              :     }
     302            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_GUID))
     303              :     {
     304            0 :         return;
     305              :     }
     306            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT32))
     307              :     {
     308            0 :         return;
     309              :     }
     310            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_INT64))
     311              :     {
     312            0 :         return;
     313              :     }
     314            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_DOUBLE))
     315              :     {
     316            0 :         return;
     317              :     }
     318            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_KVP))
     319              :     {
     320            0 :         return;
     321              :     }
     322            0 :     if (0 == g_strcmp0(param->param_type, QOF_TYPE_BOOLEAN))
     323              :     {
     324            0 :         return;
     325              :     }
     326            0 :     if (0 == g_strcmp0(param->param_type, QOF_ID_BOOK))
     327              :     {
     328            0 :         return;
     329              :     }
     330            0 :     b->list = g_list_append(b->list, param);
     331              : }
     332              : 
     333              : GList*
     334            0 : qof_class_get_referenceList(QofIdTypeConst type)
     335              : {
     336              :     GList *ref_list;
     337              :     struct param_ref_list b;
     338              : 
     339            0 :     ref_list = NULL;
     340            0 :     b.list = NULL;
     341            0 :     qof_class_param_foreach(type, find_reference_param_cb, &b);
     342            0 :     ref_list = g_list_copy(b.list);
     343            0 :     return ref_list;
     344              : }
     345              : 
     346              : 
     347              : /* ============================= END OF FILE ======================== */
        

Generated by: LCOV version 2.0-1