3 // This module changes SWIG to place constant values into a Tcl
8 %subsection "Hash Constants",pre
12 This module changes SWIG so that constant values are placed into a Tcl
13 hash table in addition to normal Tcl variables. When working with systems
14 involving large numbers of constants, the use of a hash table
15 simplifies use because it is no longer necessary to declare constants
16 using the 'global' statement.
18 This module should generally be included at the top of an interface
19 file before any declarations appear. Furthermore, this module changes
20 the default handling of basic datatypes including integers, floats,
21 and character strings.
23 When this module is used, constants are simply accessed by name
24 without the associated dollar sign. For example :
28 would be accessed as 'FOO' in Tcl, not '$FOO'.
30 Note : This module only affects integer, float, and character
31 constants. Pointer constants are not currently affected. This module
32 should not break existing Tcl scripts that rely on the normal SWIG
38 static Tcl_HashTable intHash, doubleHash, charHash;
39 static Tcl_HashEntry *entryPtr;
40 static int init_dummy;
44 Tcl_InitHashTable(&intHash, TCL_STRING_KEYS);
45 Tcl_InitHashTable(&doubleHash, TCL_STRING_KEYS);
46 Tcl_InitHashTable(&charHash, TCL_STRING_KEYS);
49 %typemap(tcl,const) int SWIG_DEFAULT_TYPE,
50 unsigned int SWIG_DEFAULT_TYPE,
51 long SWIG_DEFAULT_TYPE,
52 unsigned long SWIG_DEFAULT_TYPE,
53 short SWIG_DEFAULT_TYPE,
54 unsigned short SWIG_DEFAULT_TYPE,
55 unsigned char SWIG_DEFAULT_TYPE,
56 signed char SWIG_DEFAULT_TYPE
58 static int ivalue = (int) $source;
59 entryPtr = Tcl_CreateHashEntry(&intHash, "$target", &init_dummy);
60 Tcl_SetHashValue(entryPtr, &ivalue);
61 Tcl_LinkVar(interp, SWIG_prefix "$target",(char *) &ivalue, TCL_LINK_INT | TCL_LINK_READ_ONLY);
64 %typemap(tcl,const) float SWIG_DEFAULT_TYPE,
65 double SWIG_DEFAULT_TYPE
67 static double dvalue = (double) $source;
68 entryPtr = Tcl_CreateHashEntry(&doubleHash, "$target", &init_dummy);
69 Tcl_SetHashValue(entryPtr, &dvalue);
70 Tcl_LinkVar(interp, SWIG_prefix "$target",(char *) &dvalue, TCL_LINK_DOUBLE | TCL_LINK_READ_ONLY);
73 %typemap(tcl,const) char *SWIG_DEFAULT_TYPE
75 static char *cvalue = $source;
76 entryPtr = Tcl_CreateHashEntry(&charHash, "$target", &init_dummy);
77 Tcl_SetHashValue(entryPtr, &cvalue);
78 Tcl_LinkVar(interp, SWIG_prefix "$target",(char *) &cvalue, TCL_LINK_STRING | TCL_LINK_READ_ONLY);
81 // Change input handling to look for names
83 %typemap(tcl,in) int SWIG_DEFAULT_TYPE,
84 unsigned int SWIG_DEFAULT_TYPE,
85 long SWIG_DEFAULT_TYPE,
86 unsigned long SWIG_DEFAULT_TYPE,
87 short SWIG_DEFAULT_TYPE,
88 unsigned short SWIG_DEFAULT_TYPE,
89 unsigned char SWIG_DEFAULT_TYPE,
90 signed char SWIG_DEFAULT_TYPE
93 entry = Tcl_FindHashEntry(&intHash,$source);
95 $target = ($type) (*((int *) Tcl_GetHashValue(entry)));
98 if (Tcl_GetInt(interp, $source, &temp) == TCL_ERROR) return TCL_ERROR;
99 $target = ($type) temp;
103 %typemap(tcl,in) float SWIG_DEFAULT_TYPE,
104 double SWIG_DEFAULT_TYPE
106 Tcl_HashEntry *entry;
107 entry = Tcl_FindHashEntry(&doubleHash,$source);
109 $target = ($type) (*((double *) Tcl_GetHashValue(entry)));
110 } else if (entry = Tcl_FindHashEntry(&intHash,$source)) {
111 $target = ($type) (*((int *) Tcl_GetHashValue(entry)));
114 if (Tcl_GetDouble(interp,$source,&temp) == TCL_ERROR) return TCL_ERROR;
115 $target = ($type) temp;
119 %typemap(tcl,in) char *SWIG_DEFAULT_TYPE
121 Tcl_HashEntry *entry;
122 entry = Tcl_FindHashEntry(&charHash,$source);
124 $target = ($type) (*((char **) Tcl_GetHashValue(entry)));
130 // ----------------------------------------------------------------------------------
131 // Tcl 8 Object versions
132 // ----------------------------------------------------------------------------------
134 %typemap(tcl8,const) int SWIG_DEFAULT_TYPE,
135 unsigned int SWIG_DEFAULT_TYPE,
136 long SWIG_DEFAULT_TYPE,
137 unsigned long SWIG_DEFAULT_TYPE,
138 short SWIG_DEFAULT_TYPE,
139 unsigned short SWIG_DEFAULT_TYPE,
140 unsigned char SWIG_DEFAULT_TYPE,
141 signed char SWIG_DEFAULT_TYPE
143 static int ivalue = (int) $source;
144 entryPtr = Tcl_CreateHashEntry(&intHash, "$target", &init_dummy);
145 Tcl_SetHashValue(entryPtr, &ivalue);
146 Tcl_LinkVar(interp, SWIG_prefix "$target",(char *) &ivalue, TCL_LINK_INT | TCL_LINK_READ_ONLY);
149 %typemap(tcl8,const) float SWIG_DEFAULT_TYPE,
150 double SWIG_DEFAULT_TYPE
152 static double dvalue = (double) $source;
153 entryPtr = Tcl_CreateHashEntry(&doubleHash, "$target", &init_dummy);
154 Tcl_SetHashValue(entryPtr, &dvalue);
155 Tcl_LinkVar(interp, SWIG_prefix "$target",(char *) &dvalue, TCL_LINK_DOUBLE | TCL_LINK_READ_ONLY);
158 %typemap(tcl8,const) char *SWIG_DEFAULT_TYPE
160 static char *cvalue = $source;
161 entryPtr = Tcl_CreateHashEntry(&charHash, "$target", &init_dummy);
162 Tcl_SetHashValue(entryPtr, &cvalue);
163 Tcl_LinkVar(interp, SWIG_prefix "$target",(char *) &cvalue, TCL_LINK_STRING | TCL_LINK_READ_ONLY);
166 // Change input handling to look for names
168 %typemap(tcl8,in) int SWIG_DEFAULT_TYPE,
169 unsigned int SWIG_DEFAULT_TYPE,
170 long SWIG_DEFAULT_TYPE,
171 unsigned long SWIG_DEFAULT_TYPE,
172 short SWIG_DEFAULT_TYPE,
173 unsigned short SWIG_DEFAULT_TYPE,
174 unsigned char SWIG_DEFAULT_TYPE,
175 signed char SWIG_DEFAULT_TYPE
177 Tcl_HashEntry *entry;
179 char *_str = Tcl_GetStringFromObj($source,&_len);
180 entry = Tcl_FindHashEntry(&intHash,_str);
182 $target = ($type) (*((int *) Tcl_GetHashValue(entry)));
185 if (Tcl_GetIntFromObj(interp, $source, &temp) == TCL_ERROR) return TCL_ERROR;
186 $target = ($type) temp;
190 %typemap(tcl8,in) float SWIG_DEFAULT_TYPE,
191 double SWIG_DEFAULT_TYPE
193 Tcl_HashEntry *entry;
195 char *_str = Tcl_GetStringFromObj($source,&_len);
196 entry = Tcl_FindHashEntry(&doubleHash,_str);
198 $target = ($type) (*((double *) Tcl_GetHashValue(entry)));
199 } else if (entry = Tcl_FindHashEntry(&intHash,_str)) {
200 $target = ($type) (*((int *) Tcl_GetHashValue(entry)));
203 if (Tcl_GetDoubleFromObj(interp,$source,&temp) == TCL_ERROR) return TCL_ERROR;
204 $target = ($type) temp;
208 %typemap(tcl8,in) char *SWIG_DEFAULT_TYPE
210 Tcl_HashEntry *entry;
212 char *_str = Tcl_GetStringFromObj($source,&_len);
213 entry = Tcl_FindHashEntry(&charHash,_str);
215 $target = ($type) (*((char **) Tcl_GetHashValue(entry)));