cell-factory.c

00001 /********************************************************************\
00002  * cell-factory.c -- register cell creation object                  *
00003  * Copyright 2001 Free Software Foundation                          *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021  *                                                                  *
00022 \********************************************************************/
00023 
00024 #include "config.h"
00025 
00026 #include <glib.h>
00027 
00028 #include "cell-factory.h"
00029 #include "gnc-engine.h"
00030 
00031 
00032 typedef struct cell_record
00033 {
00034   char *cell_type_name;
00035 
00036   CellCreateFunc creator;
00037 } CellRecord;
00038 
00039 struct cell_factory
00040 {
00041   GHashTable *cell_table;
00042 };
00043 
00044 
00045 CellFactory *
00046 gnc_cell_factory_new (void)
00047 {
00048   CellFactory *cf;
00049 
00050   cf = g_new0 (CellFactory, 1);
00051 
00052   cf->cell_table = g_hash_table_new (g_str_hash, g_str_equal);
00053 
00054   return cf;
00055 }
00056 
00057 static void
00058 cell_table_destroy_helper (gpointer key, gpointer value, gpointer user_data)
00059 {
00060   CellRecord *cr = value;
00061 
00062   g_free (cr->cell_type_name);
00063   g_free (cr);
00064 }
00065 
00066 void
00067 gnc_cell_factory_destroy (CellFactory *cf)
00068 {
00069   if (!cf) return;
00070 
00071   g_hash_table_foreach (cf->cell_table, cell_table_destroy_helper, NULL);
00072 
00073   g_free (cf);
00074 }
00075 
00076 void
00077 gnc_cell_factory_add_cell_type (CellFactory *cf,
00078                                 const char *cell_type_name,
00079                                 CellCreateFunc cell_creator)
00080 {
00081   CellRecord *cr;
00082 
00083   g_return_if_fail (cell_type_name != NULL);
00084   g_return_if_fail (cell_creator != NULL);
00085 
00086   cr = g_hash_table_lookup (cf->cell_table, cell_type_name);
00087 
00088   if (cr)
00089   {
00090     g_hash_table_remove (cf->cell_table, cell_type_name);
00091     g_free (cr->cell_type_name);
00092   }
00093   else
00094     cr = g_new0 (CellRecord, 1);
00095 
00096   cr->cell_type_name = g_strdup (cell_type_name);
00097   cr->creator = cell_creator;
00098 
00099   g_hash_table_insert (cf->cell_table, cr->cell_type_name, cr);
00100 }
00101 
00102 BasicCell *
00103 gnc_cell_factory_make_cell (CellFactory *cf, const char *cell_type_name)
00104 {
00105   CellRecord *cr;
00106 
00107   g_return_val_if_fail (cf != NULL, NULL);
00108   g_return_val_if_fail (cell_type_name != NULL, NULL);
00109 
00110   cr = g_hash_table_lookup (cf->cell_table, cell_type_name);
00111   g_return_val_if_fail (cr != NULL, NULL);
00112 
00113   return cr->creator ();
00114 }

Generated on Thu Jul 3 05:06:28 2008 for GnuCash by  doxygen 1.5.2