source: branches/blt4/src/core/RpBindingsDict.cc @ 2305

Last change on this file since 2305 was 1084, checked in by dkearney, 16 years ago

adding new general purpose dictionary to store void*'s instead of specific objects like RpLibrary?*'s or RpDXWriter*'s
we'll try this out in fortran, matlab and octave bindings

File size: 8.6 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  INTERFACE: Common Rappture Dictionary Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick S. Kearney, Purdue University
7 *  Copyright (c) 2004-2008  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13#include "RpBindingsDict.h"
14
15RpDict DICT_TEMPLATE_L ObjDict_Lib;
16RpDict DICT_TEMPLATE_U ObjDictUnits;
17RpDict DICT_TEMPLATE_V ObjDict_Void;
18
19/**********************************************************************/
20// FUNCTION: storeObject_Lib()
21/// Store an object into the library dictionary.
22/**
23 * This function stores the RpLibrary object pointed to by 'objectName'
24 * into the library dictionary. This is helpful for writing bindings
25 * for languages that can not accept pointers to provide back to the
26 * function's caller.
27 *
28 * Returns the key of the object in the dictionary
29 * On Error, returns 0 (which also means nothing can be stored at 0)
30 */
31
32int
33storeObject_Lib(RpLibrary* objectName, int key) {
34
35    int retVal = 0;
36    int dictKey = key;
37    int newEntry = 0;
38    bool ci = false;
39
40    if (objectName) {
41        // dictionary returns a reference to the inserted value
42        // no error checking to make sure it was successful in entering
43        // the new entry.
44
45        if (dictKey == 0) {
46            dictKey = ObjDict_Lib.size() + 1;
47        }
48        long int dictKey_long = dictKey;
49        ObjDict_Lib.set(dictKey_long,objectName,NULL,&newEntry,ci);
50        retVal = dictKey;
51    }
52
53    return retVal;
54}
55
56/**********************************************************************/
57// FUNCTION: getObject_Lib()
58/// Get an object from the library dictionary.
59/**
60 * This function retrieves the RpLibrary object associated with the key
61 * 'objKey' from the library dictionary and returns its address to the
62 * caller. This is helpful for writing bindings for languages that can
63 * not accept pointers to provide back to the function's caller.
64 *
65 * Returns the address of the RpLibrary object in the dictionary
66 */
67
68RpLibrary*
69getObject_Lib(int objKey) {
70
71
72    RpDictEntry DICT_TEMPLATE_L* libEntry = &(ObjDict_Lib.getNullEntry());
73    RpDictEntry DICT_TEMPLATE_L* nullEntry = &(ObjDict_Lib.getNullEntry());
74
75    long int objKey_long = objKey;
76    libEntry = &(ObjDict_Lib.find(objKey_long));
77
78
79    if ( (!libEntry->isValid()) || (libEntry == nullEntry) ) {
80        return NULL;
81    }
82
83   return *(libEntry->getValue());
84
85}
86
87/**********************************************************************/
88// FUNCTION: cleanLibDict()
89/// Clean the library dictionary, removing all entries in the dictionary
90/**
91 * This function removes all entries from the library dictionary.
92 *
93 * \sa {storeObject_Lib,getObject_Lib}
94 */
95
96void
97cleanLibDict () {
98    RpDictEntry DICT_TEMPLATE_L *hPtr;
99    // should rp_quit clean up the dict or some function in RpBindingsCommon.h
100    RpDictIterator DICT_TEMPLATE_L iter(ObjDict_Lib);
101
102    hPtr = iter.first();
103
104    while (hPtr) {
105        hPtr->erase();
106        hPtr = iter.next();
107    }
108
109    if (ObjDict_Lib.size()) {
110        // probably want to change the warning sometime
111        // printf("\nWARNING: internal dictionary is not empty..deleting\n");
112    }
113
114}
115
116/**********************************************************************/
117// FUNCTION: storeObject_UnitsStr()
118/// Store an object into the UnitsStr dictionary.
119/**
120 * This function stores the RpUnits names specified by 'objectName'
121 * into the UnitsStr dictionary. This is helpful for writing bindings
122 * for languages that can not accept pointers to provide back to the
123 * function's caller.
124 *
125 * Returns the key of the object in the dictionary
126 */
127
128int
129storeObject_UnitsStr(std::string objectName) {
130
131    int retVal = -1;
132    int dictNextKey = ObjDictUnits.size() + 1;
133    int newEntry = 0;
134    bool ci = false;
135
136    if (objectName != "") {
137        // dictionary returns a reference to the inserted value
138        // no error checking to make sure it was successful in entering
139        // the new entry.
140        long int dictNextKey_long = dictNextKey;
141        ObjDictUnits.set(dictNextKey_long,objectName,NULL,&newEntry,ci);
142        retVal = dictNextKey;
143    }
144
145    return retVal;
146}
147
148/**********************************************************************/
149// FUNCTION: getObject_UnitsStr()
150/// Get an object from the UnitsStr dictionary.
151/**
152 * This function retrieves the RpUnits name referenced to by 'objKey'
153 * from the UnitsStr dictionary. This is helpful for writing bindings
154 * for languages that can not accept pointers to provide back to the
155 * function's caller.
156 *
157 * Returns the key of the object in the dictionary
158 */
159
160const RpUnits*
161getObject_UnitsStr(int objKey) {
162
163    RpDictEntry DICT_TEMPLATE_U* unitEntry = &(ObjDictUnits.getNullEntry());
164    RpDictEntry DICT_TEMPLATE_U* nullEntry = &(ObjDictUnits.getNullEntry());
165
166    long int objKey_long = objKey;
167    unitEntry = &(ObjDictUnits.find(objKey_long));
168
169
170    if ( (!unitEntry->isValid()) || (unitEntry == nullEntry) ) {
171        return NULL;
172    }
173
174    return RpUnits::find(*(unitEntry->getValue()));
175
176}
177
178/**********************************************************************/
179// FUNCTION: cleanUnitsDict()
180/// Clean the UnitsStr dictionary, removing all entries.
181/**
182 * This function removes all entries from the UnitsStr dictionary
183 *
184 * \sa {storeObject_UnitsStr,getObject_UnitsStr}
185 */
186
187void
188cleanUnitsDict () {
189    RpDictEntry DICT_TEMPLATE_U *hPtr;
190    // should rp_quit clean up the dict or some function in RpBindingsCommon.h
191    RpDictIterator DICT_TEMPLATE_U iter(ObjDictUnits);
192
193    hPtr = iter.first();
194
195    while (hPtr) {
196        hPtr->erase();
197        hPtr = iter.next();
198    }
199
200    if (ObjDictUnits.size()) {
201        // probably want to change the warning sometime
202        // printf("\nWARNING: internal dictionary is not empty..deleting\n");
203    }
204}
205
206/**********************************************************************/
207// FUNCTION: storeObject_Void()
208/// Store an object into the general dictionary.
209/**
210 * This function stores a void* object pointed to by 'objectName'
211 * into the general dictionary. This is helpful for writing bindings
212 * for languages that can not accept pointers to provide back to the
213 * function's caller.
214 *
215 * Returns the key of the object in the dictionary
216 * On Error, returns 0 (which also means nothing can be stored at 0)
217 */
218
219size_t
220storeObject_Void(void* objectName, size_t key) {
221
222    size_t retVal = 0;
223    size_t dictKey = key;
224    int newEntry = 0;
225    bool ci = false;
226
227    if (objectName) {
228        // dictionary returns a reference to the inserted value
229        // no error checking to make sure it was successful in entering
230        // the new entry.
231
232        if (dictKey == 0) {
233            dictKey = ObjDict_Void.size() + 1;
234        }
235        ObjDict_Void.set(dictKey,objectName,NULL,&newEntry,ci);
236        retVal = dictKey;
237    }
238
239    return retVal;
240}
241
242/**********************************************************************/
243// FUNCTION: getObject_Void()
244/// Get an object from the general dictionary.
245/**
246 * This function retrieves the void* object associated with the key
247 * 'objKey' from the general dictionary and returns its address to the
248 * caller. This is helpful for writing bindings for languages that can
249 * not accept pointers to provide back to the function's caller.
250 *
251 * Returns the address of the void* object in the dictionary
252 */
253
254void*
255getObject_Void(size_t objKey) {
256
257    RpDictEntry DICT_TEMPLATE_V* voidEntry = &(ObjDict_Void.getNullEntry());
258    RpDictEntry DICT_TEMPLATE_V* nullEntry = &(ObjDict_Void.getNullEntry());
259
260    voidEntry = &(ObjDict_Void.find(objKey));
261
262    if ( (!voidEntry->isValid()) || (voidEntry == nullEntry) ) {
263        return NULL;
264    }
265
266   return *(voidEntry->getValue());
267
268}
269
270/**********************************************************************/
271// FUNCTION: cleanVoidDict()
272/// Clean the library dictionary, removing all entries in the dictionary
273/**
274 * This function removes all entries from the library dictionary.
275 *
276 * \sa {storeObject_Lib,getObject_Lib}
277 */
278
279void
280cleanVoidDict () {
281    RpDictEntry DICT_TEMPLATE_V *hPtr;
282    // should rp_quit clean up the dict or some function in RpBindingsCommon.h
283    RpDictIterator DICT_TEMPLATE_V iter(ObjDict_Void);
284
285    hPtr = iter.first();
286
287    while (hPtr) {
288        hPtr->erase();
289        hPtr = iter.next();
290    }
291
292    if (ObjDict_Lib.size()) {
293        // probably want to change the warning sometime
294        // printf("\nWARNING: internal dictionary is not empty..deleting\n");
295    }
296
297}
298
Note: See TracBrowser for help on using the repository browser.