tilde~
Puredata Qt-based GUI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OPInstance.h
Go to the documentation of this file.
1 // License: GPL3
2 //
3 // OOPD.h
4 //
5 //
6 // Created by Alex Nadzharov on 26/03/17.
7 //
8 //
9 
10 #ifndef OPINSTANCE_H
11 #define OPINSTANCE_H
12 
13 //#include "m_pd.h"
14 #include <map>
15 #include <set>
16 
17 
18 //
19 #include <QStringList>
20 
21 #include <QDebug>
22 #include "OOPDClassBase.h"
23 
24 using namespace std;
25 using namespace xpd;
26 
27 
28 
29 namespace tilde {
30 
31 // ------------------------------------------------------------
32 // typedefs
33 
34 class OPClass;
35 
36 //typedef vector<t_outlet*> OPOutputs; ///< vector of method boxes outputs
37 typedef vector<t_object*> OPProperties;
38 
39 // ------------------------------------------------------------
40 
44 class OPInstance : public OOPDClassBase {
45 private:
46  map<t_symbol*, OPOutputs> _methodOutputs;
47  OPOutputs _instanceOutputs;
48 
49  map<t_symbol*, OPOutputs> _getterOutputs;
50  map<t_symbol*, OPOutputs> _setterOutputs;
51 
52  //new
53  map<t_symbol*, PdArguments> _propertyValues;
54 
55  int _refCount;
56 
57  //signal
58  map<t_symbol*, t_sample*> _signalBuffers;
59 
60  //
61  map<t_symbol*, t_outlet*> _methodPointerOutputs; // todo OPOutputs
62 
63  ~OPInstance();
64 
65 public:
66  //
68 
69  OPInstance(OPClass* opClass);
70 
71 #pragma mark methods
72 
73  void addMethodOutlet(t_symbol* methodName, t_outlet* outlet)
74  {
75  _methodOutputs[methodName].addOutlet(outlet); //.push_back(outlet);
76  }
77 
78  void freeMethod(t_symbol* methodName)
79  {
80  _methodOutputs.erase(methodName);
81  }
82 
83  void addMethodPointerOutlet(t_symbol* methodName, t_outlet* outlet)
84  {
85  _methodPointerOutputs[methodName] = outlet; //.push_back(outlet); TODO
86  }
87 
89  {
90  _methodPointerOutputs.erase(methodName);
91  }
92 
93 #pragma mark signal
94 
95  t_sample* getBufferFor(t_symbol* signalName, int vec_size)
96  {
97  t_sample* ret = _signalBuffers[signalName];
98 
99  if (!ret) {
100  ret = new t_sample[vec_size];
101  _signalBuffers[signalName] = ret;
102  post("new buffer: %s %i\n", signalName->s_name, vec_size);
103  }
104 
105  return ret;
106  }
107 
108  void freeSignal(t_symbol* signalName)
109  {
110  //todo refcounter
111  post("del buffer");
112  delete (_signalBuffers[signalName]);
113  _signalBuffers.erase(signalName);
114  }
115 
116 #pragma mark properties
117 
118  void addProperty(t_symbol* propertyName, t_outlet* getter_out, t_outlet* setter_out)
119  {
120  _getterOutputs[propertyName].addOutlet(getter_out);//.push_back(getter_out);
121  _setterOutputs[propertyName].addOutlet(setter_out);//.push_back(setter_out);
122 
123  OOPDClassBase::addProperty(propertyName->s_name, "");
124  }
125  void freeProperty(t_symbol* propertyName)
126  {
127  _getterOutputs.erase(propertyName);
128  _setterOutputs.erase(propertyName);
129 
130  OOPDClassBase::freeProperty(propertyName->s_name);
131  }
132 
133  void setPdArgumentsProperty(t_symbol* propertyName, PdArguments list)
134  {
135  _propertyValues[propertyName] = list;
136  }
137 
138  PdArguments getPdArgumentsProperty(t_symbol* propertyName)
139  {
140  PdArguments list = _propertyValues[propertyName];
141  return list;
142  }
143 
144 // ---------------
145 #pragma mark methods
146 
147  // XPD-TODO
148 
149  /*
150  void addInstanceOut(t_outlet* outlet)
151  {
152  if (outlet)
153  _instanceOutputs.addOutlet(outlet);//.push_back(outlet);
154  }
155 
156  void freeInstanceOut(t_outlet* outlet)
157  {
158  _instanceOutputs.freeOutlet(outlet);
159  }
160 
161  void multipleOutput(PdArguments list)
162  {
163 // for (OPOutputs::iterator it = _instanceOutputs.begin(); it != _instanceOutputs.end(); ++it) {
164 // list.output(*it);
165 // }
166  _instanceOutputs.outList(list);
167  }
168  */
169 
170 // -----------------
171 #pragma mark method calls
172 
173  void callMethod(PdArguments list)
174  {
175  // XPD-TODO
176 
177  /*
178  t_symbol* method_name = list[0].asSymbol();
179 
180  PdArguments subList = list.slice(1, (int)list.size());
181 
182  OPOutputs* out1 = &_methodOutputs[method_name];
183 
184  if (out1) {
185 
186  out1->outList(subList);
187  }
188 
189  //dynamic. todo
190 
191  t_outlet* out2 = _methodPointerOutputs[method_name];
192  if (out2) {
193  PdArguments objList = AtomList(Atom(gensym("setobject")));
194 
195  objList.append(Atom(gensym(to_string((long)this).c_str())));
196 
197  objList.output(out2);
198  }
199  */
200  }
201 
202  void callSetter(PdArguments list)
203  {
204  // XPD-TODO
205 
206  /*
207  if (!hasProperty(list[0].asString()))
208  return;
209 
210  t_symbol* property_name = list[0].asSymbol();
211 
212  AtomList list2((size_t)list.size() - 1, (t_atom*)list.toPdData() + 1); //TODO
213  setPdArgumentsProperty(property_name, list2);
214 
215  OPOutputs* out1 = &_setterOutputs[property_name];
216 
217 // for (OPOutputs::iterator it = out1->begin(); it != out1->end(); ++it) {
218 // outlet_bang(*it);
219 // }
220  out1->outBang();
221 
222  */
223  }
224 
225  void callSetter(string propertyName, PdArguments list)
226  {
227  // XPD-TODO
228 
229  /*
230 
231  if (!hasProperty(propertyName->s_name))
232  return;
233 
234  setPdArgumentsProperty(propertyName, list);
235 
236  OPOutputs* out1 = &_setterOutputs[propertyName];
237 
238 // for (OPOutputs::iterator it = out1->begin(); it != out1->end(); ++it) {
239 // outlet_bang(*it);
240 // }
241  out1->outBang();
242 
243  */
244  }
245 
246  void callGetter(PdArguments list)
247  {
248  // XPD-TODO
249 
250  /*
251  if (!hasProperty(list[0].asString()))
252  return;
253 
254  t_symbol* property_name = list[0].asSymbol();
255 
256  AtomList list2(list[0]);
257  list2.append(getPdArgumentsProperty(property_name));
258 
259  multipleOutput(list2);
260 
261  OPOutputs* out1 = &_getterOutputs[property_name];
262 
263 // for (OPOutputs::iterator it = out1->begin(); it != out1->end(); ++it) {
264 // outlet_bang(*it);
265 // }
266  out1->outBang();
267 
268  */
269  }
270 
271  void callGetter(string propertyName)
272  {
273 
274  // XPD-TODO
275 
276  /*
277  if (!hasProperty(propertyName->s_name))
278  return;
279 
280  AtomList list2 = AtomList(Atom(propertyName));
281 
282  list2.append(getPdArgumentsProperty(propertyName));
283 
284  multipleOutput(list2);
285 
286  OPOutputs* out1 = &_getterOutputs[propertyName];
287 
288 // for (OPOutputs::iterator it = out1->begin(); it != out1->end(); ++it) {
289 // outlet_bang(*it);
290 // }
291  out1->outBang();
292 
293  //return list2;
294 
295  */
296  }
297 
298 // ------------------------------------------------
299 #pragma mark window
300 
301  void showWindow();
302 
303 // ---------------------------
304 #pragma mark object pointer
305 
306  static t_symbol* toSymbol(OPInstance* inst)
307  {
308  return gensym(to_string((long)inst).c_str());
309  }
310 
312  {
313  return gensym(to_string((long)this).c_str());
314  }
315 
316  static OPInstance* fromObjectSymbol(t_symbol* objSymbol);
317 
318 #pragma mark reference counting
319  // names?
320  void retain()
321  {
322  _refCount++;
323  printf("OPInstance ref count: %i\n", _refCount);
324  }
325 
326  //?
327  void release()
328  {
329  _refCount--;
330  printf("OPInstance ref count: %i\n", _refCount);
331  if (_refCount == 0)
332  delete this;
333  }
334 
335  //debug
337  {
338  return _refCount;
339  }
340 };
341 }
342 
343 #endif /* OOPD_h */
void freeMethodPointerOutlet(t_symbol *methodName)
Definition: OPInstance.h:88
t_sample * getBufferFor(t_symbol *signalName, int vec_size)
Definition: OPInstance.h:95
void freeSignal(t_symbol *signalName)
Definition: OPInstance.h:108
int getRefCount()
Definition: OPInstance.h:336
void freeProperty(t_symbol *propertyName)
Definition: OPInstance.h:125
void callSetter(string propertyName, PdArguments list)
Definition: OPInstance.h:225
void callGetter(string propertyName)
Definition: OPInstance.h:271
t_symbol * getObjectSymbol()
Definition: OPInstance.h:311
PdArguments getPdArgumentsProperty(t_symbol *propertyName)
Definition: OPInstance.h:138
stores pointers to t_outlets of pd objects
Definition: OOPDClassBase.h:36
void freeMethod(t_symbol *methodName)
Definition: OPInstance.h:78
void addProperty(t_symbol *propertyName, t_outlet *getter_out, t_outlet *setter_out)
Definition: OPInstance.h:118
void retain()
Definition: OPInstance.h:320
string to_string(const T &t)
Definition: oop_common.h:32
PD_FLOATTYPE t_sample
Definition: m_pd.h:574
EXTERN t_symbol * gensym(const char *s)
void setPdArgumentsProperty(t_symbol *propertyName, PdArguments list)
Definition: OPInstance.h:133
void release()
Definition: OPInstance.h:327
static t_symbol * toSymbol(OPInstance *inst)
Definition: OPInstance.h:306
void addMethodOutlet(t_symbol *methodName, t_outlet *outlet)
Definition: OPInstance.h:73
#define t_outlet
Definition: m_pd.h:189
void callMethod(PdArguments list)
Definition: OPInstance.h:173
base data structure for class and instance: name, canvas, symbol
Definition: OOPDClassBase.h:77
OPInstance * _parent
Definition: OPInstance.h:67
OOPD class structure.
Definition: OPClass.h:38
Definition: oop_common.h:81
Definition: m_pd.h:110
EXTERN void post(const char *fmt,...)
void callGetter(PdArguments list)
Definition: OPInstance.h:246
char * s_name
Definition: m_pd.h:112
The OOPD class instance.
Definition: OPInstance.h:44
void addMethodPointerOutlet(t_symbol *methodName, t_outlet *outlet)
Definition: OPInstance.h:83
vector< t_object * > OPProperties
vector of property boxes
Definition: oop_common.h:72
void callSetter(PdArguments list)
Definition: OPInstance.h:202