source: trunk/src/objects/RpObject.cc @ 1566

Last change on this file since 1566 was 1566, checked in by dkearney, 15 years ago

updates to Rappture::Library and Rappture::Number objects to demo how the library should store objects and generate the xml for those objects upon request. opting for configure() and dump() functions for objects. the object should know how to configure it self from a string of xml, tree object or (in the future) blob of hdf5. added some rudimentary examples of using the library object.

File size: 5.8 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  RpObject.cc
4 *
5 *   Rappture 2.0 Object member functions
6 *
7 * ======================================================================
8 *  AUTHOR:  Derrick Kearney, Purdue University
9 *  Copyright (c) 2005-2009  Purdue Research Foundation
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15
16#include "RpObject.h"
17#include "RpHashHelper.h"
18
19using namespace Rappture;
20
21Object::Object()
22{
23    __init();
24}
25
26Object::Object (
27    const char *name,
28    const char *path,
29    const char *label,
30    const char *desc,
31    const char *hints,
32    const char *color   )
33{
34    this->name(name);
35    this->path(path);
36    this->label(label);
37    this->desc(desc);
38    this->hints(hints);
39    this->color(color);
40}
41
42Object::Object(const Object& o)
43{
44    name(o.name());
45    path(o.path());
46    label(o.label());
47    desc(o.desc());
48    hints(o.hints());
49    color(o.color());
50    // icon(o.icon());
51
52    if (o._h != NULL) {
53        Rp_HashCopy(_h,o._h,charCpyFxn);
54    }
55}
56
57Object::~Object()
58{
59    __clear();
60}
61
62const char*
63Object::name() const
64{
65    return propstr("name");
66}
67
68void
69Object::name(const char *p)
70{
71    propstr("name",p);
72}
73
74const char*
75Object::path() const
76{
77    return propstr("path");
78}
79
80void
81Object::path(const char *p)
82{
83    propstr("path",p);
84}
85
86const char*
87Object::label() const
88{
89    return propstr("label");
90}
91
92void
93Object::label(
94    const char *p)
95{
96    propstr("label",p);
97}
98
99const char*
100Object::desc() const
101{
102    return propstr("desc");
103}
104
105void
106Object::desc(const char *p)
107{
108    propstr("desc",p);
109}
110
111const char*
112Object::hints() const
113{
114    return propstr("hints");
115}
116
117void
118Object::hints(const char *p)
119{
120    propstr("hints",p);
121}
122
123const char*
124Object::color() const
125{
126    return propstr("color");
127}
128
129void
130Object::color(const char *p)
131{
132    propstr("color",p);
133}
134
135// get a general property
136const void *
137Object::property(const char *key) const
138{
139    if (key == NULL) {
140        // no key to search for
141        return NULL;
142    }
143
144    if (_h == NULL) {
145        // hash table does not exist, value is not in table
146        return NULL;
147    }
148
149    // get the value
150    return Rp_HashSearchNode(_h,key);
151}
152
153// set a general property
154void
155Object::property(const char *key, const void *val, size_t nbytes)
156{
157    if (key == NULL) {
158        // no key for value
159        return;
160    }
161
162    if (_h == NULL) {
163        // hash table does not exist, create it
164        _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable));
165        Rp_InitHashTable(_h,RP_STRING_KEYS);
166    }
167
168    // FIXME: this is suspect,
169    // want to use void's and void*'s but c++ wont let me
170    // g++ says there is no delete[] for void*
171    void *tmp = new char[nbytes];
172    memcpy(tmp,val,nbytes);
173
174    // set the value
175    char *old = (char *) Rp_HashRemoveNode(_h,key);
176    delete[] old;
177    old = NULL;
178    Rp_HashAddNode(_h,key,tmp);
179
180    return;
181}
182
183// get a const char* property
184const char *
185Object::propstr(const char *key) const
186{
187    if (key == NULL) {
188        // no key for value
189        return NULL;
190    }
191
192    if (_h == NULL) {
193        // hash table does not exist, value does not exist in table
194        return NULL;
195    }
196
197    // get the value
198    return (const char *) Rp_HashSearchNode(_h,key);
199}
200
201// set a const char* property
202void
203Object::propstr(const char *key, const char *val)
204{
205    char *str = NULL;
206
207    if (key == NULL) {
208        // no key for value
209        return;
210    }
211
212    if (_h == NULL) {
213        // hash table does not exist, create it
214        _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable));
215        Rp_InitHashTable(_h,RP_STRING_KEYS);
216    }
217
218    if (val == NULL) {
219        // no value provided, no value stored.
220        // FIXME: raise error.
221        return;
222    }
223
224    // set the value
225    // FIXME: this is suspect,
226    // want to use void's and void*'s but c++ wont let me
227    // g++ says there is no delete[] for void*
228    // FIXME: can probably just get the length and use property() fxn
229    size_t l = strlen(val);
230    str = new char[l+1];
231    strcpy(str,val);
232
233    char *old = (char *) Rp_HashRemoveNode(_h,key);
234    delete[] old;
235    old = NULL;
236    Rp_HashAddNode(_h,key,str);
237
238    return;
239}
240
241// remove a property from the hash table
242void
243Object::propremove(const char *key)
244{
245    if ((key == NULL) || (_h == NULL)) {
246        // key or hash table does not exist
247        return;
248    }
249
250    char *tmp = (char *) Rp_HashRemoveNode(_h,key);
251    delete[] tmp;
252    tmp = NULL;
253    return;
254}
255
256void
257Object::__init()
258{
259    _h = NULL;
260    label("");
261    desc("");
262    hints("");
263    color("");
264    // icon(NULL);
265    path("");
266
267    _tmpBuf.clear();
268
269    _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable));
270    Rp_InitHashTable(_h,RP_STRING_KEYS);
271}
272
273void
274Object::__clear()
275{
276    _tmpBuf.clear();
277
278    // traverse the hash table to delete values, then delete hash table
279    if (_h != NULL) {
280        Rp_HashEntry *hEntry = NULL;
281        Rp_HashSearch hSearch;
282
283        hEntry = Rp_FirstHashEntry(_h,&hSearch);
284        while (hEntry != NULL) {
285            // FIXME: this is suspect,
286            // want to use void's and void*'s but c++ wont let me
287            // g++ says there is no delete[] for void*
288            char *v = (char *) Rp_GetHashValue(hEntry);
289            delete[] v;
290            v = NULL;
291            hEntry = Rp_NextHashEntry(&hSearch);
292        }
293
294        Rp_DeleteHashTable(_h);
295        _h = NULL;
296    }
297}
298
299const char *
300Object::xml(size_t indent, size_t tabstop) const
301{
302    return NULL;
303}
304
305/*
306void
307Object::xml(const char *xmltext)
308{
309    return;
310}
311*/
312
313void
314Object::configure(size_t as, void *p)
315{
316    return;
317}
318
319void
320Object::dump(size_t as, void *p)
321{
322    return;
323}
324
325const int
326Object::is() const
327{
328    // return "var" in hex
329    return 0x766172;
330}
331
332// -------------------------------------------------------------------- //
333
Note: See TracBrowser for help on using the repository browser.