source: trunk/src/objects/RpVariable.cc @ 1386

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

adding a few object prototypes we can play with for future developement. the plot object is probably the most interesting. examples are located in examples/objects dirs

File size: 3.1 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  RpVariable.cc
4 *
5 *   Rappture 2.0 Variable 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 "RpVariable.h"
17#include "RpHashHelper.h"
18
19using namespace Rappture;
20
21Variable::Variable()
22{
23    __init();
24}
25
26Variable::Variable (
27    const char *path,
28    const char *label,
29    const char *desc,
30    const char *hints,
31    const char *color   )
32{
33    this->path(path);
34    this->label(label);
35    this->desc(desc);
36    this->hints(hints);
37    this->color(color);
38}
39
40Variable::Variable(const Variable& o)
41{
42    path(o.path());
43    label(o.label());
44    desc(o.desc());
45    hints(o.hints());
46    color(o.color());
47    icon(o.icon());
48
49    if (o._h != NULL) {
50        Rp_HashCopy(_h,o._h,charCpyFxn);
51    }
52}
53
54Variable::~Variable()
55{
56    __clear();
57}
58
59const void *
60Variable::property(
61    const char *key,
62    const void *val)
63{
64    const void *r = NULL;
65    if (_h == NULL) {
66        // hash table does not exist, create it
67        _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable));
68        Rp_InitHashTable(_h,RP_STRING_KEYS);
69    }
70
71    if (val == NULL) {
72        // get the value
73        r = Rp_HashSearchNode(_h,key);
74    } else {
75        // set the value
76        Rp_HashRemoveNode(_h,key);
77        Rp_HashAddNode(_h,key,val);
78        r = val;
79    }
80
81    return r;
82}
83
84const char *
85Variable::propstr(
86    const char *key,
87    const char *val)
88{
89    const char *r = NULL;
90    char *str = NULL;
91    if (_h == NULL) {
92        // hash table does not exist, create it
93        _h = (Rp_HashTable*) malloc(sizeof(Rp_HashTable));
94        Rp_InitHashTable(_h,RP_STRING_KEYS);
95    }
96
97    if (val == NULL) {
98        // get the value
99        r = (const char *) Rp_HashSearchNode(_h,key);
100    } else {
101        // set the value
102        //FIXME: users responsibility to free value with propremove()
103        size_t l = strlen(val);
104        str = new char[l+1];
105        strcpy(str,val);
106
107        // FIXME: possible memory leak here
108        // we dont free the removed item,
109        // it is user's responsibility to use propremove() to free it
110        // we don't know whether to use delete or delete[].
111        Rp_HashRemoveNode(_h,key);
112        Rp_HashAddNode(_h,key,str);
113        r = val;
114    }
115
116    return r;
117}
118
119void *
120Variable::propremove(
121    const char *key)
122{
123    if ((key == NULL) || (_h == NULL)) {
124        // key or hash table does not exist
125        return NULL;
126    }
127
128    return Rp_HashRemoveNode(_h,key);
129}
130
131void
132Variable::__init()
133{
134    _h = NULL;
135    label("");
136    desc("");
137    hints("");
138    color("");
139    // icon(NULL);
140    path("");
141}
142
143void
144Variable::__clear()
145{
146    // who frees the Accessors?
147
148    if (_h != NULL) {
149        Rp_DeleteHashTable(_h);
150    }
151
152    __init();
153}
154
155
156// -------------------------------------------------------------------- //
157
Note: See TracBrowser for help on using the repository browser.