GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cell-factory.c
1 /********************************************************************\
2  * cell-factory.c -- register cell creation object *
3  * Copyright 2001 Free Software Foundation *
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 [email protected] *
21  * *
22 \********************************************************************/
23 
24 #include "config.h"
25 
26 #include <glib.h>
27 
28 #include "cell-factory.h"
29 #include "gnc-engine.h"
30 
31 
32 typedef struct cell_record
33 {
34  char *cell_type_name;
35 
36  CellCreateFunc creator;
37 } CellRecord;
38 
40 {
41  GHashTable *cell_table;
42 };
43 
44 
46 gnc_cell_factory_new (void)
47 {
48  CellFactory *cf;
49 
50  cf = g_new0 (CellFactory, 1);
51 
52  cf->cell_table = g_hash_table_new (g_str_hash, g_str_equal);
53 
54  return cf;
55 }
56 
57 static void
58 cell_table_destroy_helper (gpointer key, gpointer value, gpointer user_data)
59 {
60  CellRecord *cr = value;
61 
62  g_free (cr->cell_type_name);
63  g_free (cr);
64 }
65 
66 void
67 gnc_cell_factory_destroy (CellFactory *cf)
68 {
69  if (!cf) return;
70 
71  g_hash_table_foreach (cf->cell_table, cell_table_destroy_helper, NULL);
72 
73  g_free (cf);
74 }
75 
76 void
77 gnc_cell_factory_add_cell_type (CellFactory *cf,
78  const char *cell_type_name,
79  CellCreateFunc cell_creator)
80 {
81  CellRecord *cr;
82 
83  g_return_if_fail (cell_type_name != NULL);
84  g_return_if_fail (cell_creator != NULL);
85 
86  cr = g_hash_table_lookup (cf->cell_table, cell_type_name);
87 
88  if (cr)
89  {
90  g_hash_table_remove (cf->cell_table, cell_type_name);
91  g_free (cr->cell_type_name);
92  }
93  else
94  cr = g_new0 (CellRecord, 1);
95 
96  cr->cell_type_name = g_strdup (cell_type_name);
97  cr->creator = cell_creator;
98 
99  g_hash_table_insert (cf->cell_table, cr->cell_type_name, cr);
100 }
101 
102 BasicCell *
103 gnc_cell_factory_make_cell (CellFactory *cf, const char *cell_type_name)
104 {
105  CellRecord *cr;
106 
107  g_return_val_if_fail (cf != NULL, NULL);
108  g_return_val_if_fail (cell_type_name != NULL, NULL);
109 
110  cr = g_hash_table_lookup (cf->cell_table, cell_type_name);
111  g_return_val_if_fail (cr != NULL, NULL);
112 
113  return cr->creator ();
114 }
All type declarations for the whole Gnucash engine.