1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* GENIUS Calculator
* Copyright (C) 1997-2002 George Lebl
*
* Author: George Lebl
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "calc.h"
#include "mpwrap.h"
#include "eval.h"
#include "dict.h"
#include "util.h"
#include "funclib.h"
#include "matrix.h"
#include "matrixw.h"
#include "matop.h"
#include "compil.h"
#include "utype.h"
typedef struct _UserType UserType;
struct _UserType {
int type;
char *name;
GelUTDestroyFunc destructor;
GelUTCopyFunc copier;
};
static GHashTable *types_ht = NULL;
static GArray *types_ar = NULL;
static int type_cookie = 0;
static void
do_init(void)
{
if(!types_ht)
types_ht = g_hash_table_new(g_str_hash,g_str_equal);
else
types_ar = g_array_new(FALSE,FALSE,sizeof(gpointer));
}
/* make a new unique identifier for a user type and set the destruction
function which is called when a variable of that type is killed */
int
gel_new_user_type(char *name,
GelUTDestroyFunc destructor,
GelUTCopyFunc copier)
{
UserType *ut;
g_return_val_if_fail(name!=NULL,-1);
do_init();
if(g_hash_table_lookup(types_ht,name))
return -1;
ut = g_new0(UserType,1);
ut->type = type_cookie++;
ut->name = g_strdup(name);
ut->destructor = destructor;
ut->copier = copier;
types_ar = g_array_append_val(types_ar,ut);
g_hash_table_insert(types_ht,ut->name,ut);
return ut->type;
}
/* destroy the type itself, not a specific variable */
void
gel_destroy_user_type(int type)
{
UserType *ut;
g_return_if_fail(type<type_cookie);
g_return_if_fail(type>=0);
ut = g_array_index(types_ar,UserType *,type);
if(!ut) return;
g_array_index(types_ar,UserType *,type) = NULL;
g_hash_table_remove(types_ht,ut->name);
g_free(ut->name);
g_free(ut);
}
/* lookup the id of a type */
int
gel_get_user_type(const char *name)
{
UserType *ut;
ut = g_hash_table_lookup (types_ht, name);
if (ut == NULL)
return -1;
else
return ut->type;
}
/* lookup the name of type 'type' */
char *
gel_get_user_type_name(int type)
{
UserType *ut;
g_return_val_if_fail(type<type_cookie,NULL);
g_return_val_if_fail(type>=0,NULL);
ut = g_array_index(types_ar,UserType *,type);
if(!ut) return NULL;
return ut->name;
}
/* make a new variable of type 'type' with data 'data' */
GelETree *
gel_make_new_user_variable(int type, gpointer data)
{
GelETree *n;
UserType *ut;
g_return_val_if_fail(type<type_cookie,NULL);
g_return_val_if_fail(type>=0,NULL);
ut = g_array_index(types_ar,UserType *,type);
if(!ut) return NULL;
GET_NEW_NODE(n);
n->type = USERTYPE_NODE;
n->ut.next = NULL;
n->ut.ttype = ut->type;
n->ut.data = data;
return n;
}
/* free the 'data' of type 'type' using the destructor */
void
gel_free_user_variable_data(int type, gpointer data)
{
UserType *ut;
g_return_if_fail(type<type_cookie);
g_return_if_fail(type>=0);
ut = g_array_index(types_ar,UserType *,type);
if(!ut) return;
if(ut->destructor)
(*ut->destructor)(data);
}
/* copy the 'data' of type 'type' using the copier */
gpointer
gel_copy_user_variable_data(int type, gpointer data)
{
UserType *ut;
g_return_val_if_fail(type<type_cookie,NULL);
g_return_val_if_fail(type>=0,NULL);
ut = g_array_index(types_ar,UserType *,type);
if(!ut) return NULL;
if(ut->copier)
return (*ut->copier)(data);
else
return NULL;
}
|