Refactoring of file beam.ml.
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / tlib / property.hh
1 #ifndef __PROPERTY__
2 #define __PROPERTY__
3
4 #include "tree.hh"
5
6 template<class P> class property
7 {
8 Tree fKey;
9
10 P* access(Tree t)
11 {
12 Tree d = t->getProperty(fKey);
13 return d ? (P*)(d->node().getPointer()) : 0;
14 }
15
16 public:
17
18 property () : fKey(tree(Node(unique("property_")))) {}
19
20 property (const char* keyname) : fKey(tree(Node(keyname))) {}
21
22 void set(Tree t, const P& data)
23 {
24 P* p = access(t);
25 if (p) {
26 *p = data;
27 } else {
28 t->setProperty(fKey, tree(Node(new P(data))));
29 }
30 }
31
32 bool get(Tree t, P& data)
33 {
34 P* p = access(t);
35 if (p) {
36 data = *p;
37 return true;
38 } else {
39 return false;
40 }
41 }
42
43 void clear(Tree t)
44 {
45 P* p = access(t);
46 if (p) { delete p; }
47 t->clearProperty(fKey);
48 }
49 };
50
51
52 template<> class property<Tree>
53 {
54 Tree fKey;
55
56 public:
57
58 property () : fKey(tree(Node(unique("property_")))) {}
59
60 property (const char* keyname) : fKey(tree(Node(keyname))) {}
61
62 void set(Tree t, Tree data)
63 {
64 t->setProperty(fKey, data);
65 }
66
67 bool get(Tree t, Tree& data)
68 {
69 Tree d = t->getProperty(fKey);
70 if (d) {
71 data = d;
72 return true;
73 } else {
74 return false;
75 }
76 }
77
78 void clear(Tree t)
79 {
80 t->clearProperty(fKey);
81 }
82 };
83
84
85 template<> class property<int>
86 {
87 Tree fKey;
88
89 public:
90
91 property () : fKey(tree(Node(unique("property_")))) {}
92
93 property (const char* keyname) : fKey(tree(Node(keyname))) {}
94
95 void set(Tree t, int i)
96 {
97 t->setProperty(fKey, tree(Node(i)));
98 }
99
100 bool get(Tree t, int& i)
101 {
102 Tree d = t->getProperty(fKey);
103 if (d) {
104 i = d->node().getInt();
105 return true;
106 } else {
107 return false;
108 }
109 }
110
111 void clear(Tree t)
112 {
113 t->clearProperty(fKey);
114 }
115 };
116
117
118 template<> class property<double>
119 {
120 Tree fKey;
121
122 public:
123
124 property () : fKey(tree(Node(unique("property_")))) {}
125
126 property (const char* keyname) : fKey(tree(Node(keyname))) {}
127
128 void set(Tree t, double x)
129 {
130 t->setProperty(fKey, tree(Node(x)));
131 }
132
133 bool get(Tree t, double& x)
134 {
135 Tree d = t->getProperty(fKey);
136 if (d) {
137 x = d->node().getDouble();
138 return true;
139 } else {
140 return false;
141 }
142 }
143
144 void clear(Tree t)
145 {
146 t->clearProperty(fKey);
147 }
148 };
149
150
151
152 #endif