1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/*
flext - C++ layer for Max/MSP and pd (pure data) externals
Copyright (c) 2001-2003 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.
*/
/*! \file flattr.cpp
\brief Attribute handling for the flext base class
*/
#include "flext.h"
#include <string.h>
#include <ctype.h>
#include <set>
#ifdef __MWERKS__
#define STD std
#else
#define STD
#endif
flext_base::AttrItem::AttrItem(metharg tp,methfun f,int fl):
Item(NULL),index(0),
flags(fl|afl_shown),
argtp(tp),fun(f),
counter(NULL)
{}
flext_base::AttrItem::~AttrItem()
{
// if(nxt) delete nxt;
}
//! Add get and set attributes
void flext_base::AddAttrib(ItemCont *aa,ItemCont *ma,const char *attr,metharg tp,methfun gfun,methfun sfun)
{
const t_symbol *asym = MakeSymbol(attr);
AttrItem *a,*b;
if(sfun) // if commented out, there will be a warning at run-time (more user-friendly)
{
a = new AttrItem(tp,sfun,AttrItem::afl_set);
a->index = aa->Members();
aa->Add(a,asym);
// bind attribute to a method
MethItem *mi = new MethItem(a);
mi->SetArgs(sfun,1,new metharg(tp));
ma->Add(mi,asym);
}
else
a = NULL;
if(gfun) // if commented out, there will be a warning at run-time (more user-friendly)
{
b = new AttrItem(tp,gfun,AttrItem::afl_get);
b->index = aa->Members();
aa->Add(b,asym);
static char tmp[256] = "get";
strcpy(tmp+3,attr);
// bind attribute to a method
MethItem *mi = new MethItem(b);
mi->SetArgs(gfun,0,NULL);
ma->Add(mi,MakeSymbol(tmp));
}
else
b = NULL;
if(a && b) {
a->counter = b;
b->counter = a;
}
}
void flext_base::AddAttrib(const char *attr,metharg tp,methfun gfun,methfun sfun)
{
if(procattr)
AddAttrib(ThAttrs(),ThMeths(),attr,tp,gfun,sfun);
else
error("%s - attribute procession is not enabled!",thisName());
}
void flext_base::AddAttrib(t_classid c,const char *attr,metharg tp,methfun gfun,methfun sfun)
{
AddAttrib(ClAttrs(c),ClMeths(c),attr,tp,gfun,sfun);
}
void flext_base::ListAttrib(AtomList &la) const
{
typedef std::map<int,const t_symbol *> AttrList;
AttrList list[2];
int i;
for(i = 0; i <= 1; ++i) {
ItemCont *a = i?attrhead:clattrhead;
if(a && a->Contained(0)) {
ItemSet &ai = a->GetInlet();
for(ItemSet::iterator as = ai.begin(); as != ai.end(); ++as) {
for(ItemList::iterator al = as->second.begin(); al != as->second.end(); ++al) {
AttrItem *aa = (AttrItem *)*al;
list[i][aa->index] = as->first;
break;
}
}
}
}
la(list[0].size()+list[1].size());
int ix = 0;
AttrList::iterator it;
for(i = 0; i <= 1; ++i)
for(it = list[i].begin(); it != list[i].end(); ++it)
SetSymbol(la[ix++],it->second);
}
int flext_base::CheckAttrib(int argc,const t_atom *argv)
{
int offs = 0;
for(; offs < argc; ++offs)
if(IsString(argv[offs]) && *GetString(argv[offs]) == '@') break;
return offs;
}
bool flext_base::InitAttrib(int argc,const t_atom *argv)
{
int cur,nxt;
for(cur = 0; cur < argc; cur = nxt) {
// find next @symbol
for(nxt = cur+1; nxt < argc; ++nxt)
if(IsString(argv[nxt]) && *GetString(argv[nxt]) == '@') break;
const t_symbol *tag = MakeSymbol(GetString(argv[cur])+1);
// find puttable attribute
AttrItem *attr = FindAttrib(tag,false,true);
if(attr) {
// make an entry (there are none beforehand...)
AttrData &a = (*attrdata)[tag];
a.SetInit(true);
a.SetInitValue(nxt-cur-1,argv+cur+1);
// pass value to object
SetAttrib(tag,attr,a.GetInitValue());
}
}
return true;
}
bool flext_base::ListAttrib() const
{
if(procattr) {
AtomList la;
ListAttrib(la);
ToOutAnything(GetOutAttr(),MakeSymbol("attributes"),la.Count(),la.Atoms());
return true;
}
else
return false;
}
flext_base::AttrItem *flext_base::FindAttrib(const t_symbol *tag,bool get,bool msg) const
{
// first search within object scope
AttrItem *a = NULL;
{
ItemList *lst = attrhead->FindList(tag);
if(lst) {
for(ItemList::iterator it = lst->begin(); it != lst->end(); ++it) {
AttrItem *b = (AttrItem *)*it;
if(get?b->IsGet():b->IsSet()) { a = b; break; }
}
}
}
// then (if nothing found) search within class scope
if(!a) {
ItemList *lst = clattrhead->FindList(tag);
if(lst) {
for(ItemList::iterator it = lst->begin(); it != lst->end(); ++it) {
AttrItem *b = (AttrItem *)*it;
if(get?b->IsGet():b->IsSet()) { a = b; break; }
}
}
}
if(!a && msg) {
// print a message
error("%s - %s: attribute not found",thisName(),GetString(tag));
}
return a;
}
bool flext_base::SetAttrib(const t_symbol *tag,int argc,const t_atom *argv)
{
// search for matching attribute
AttrItem *a = FindAttrib(tag,false,true);
if(a)
return SetAttrib(tag,a,argc,argv);
else
return true;
}
bool flext_base::SetAttrib(const t_symbol *tag,AttrItem *a,int argc,const t_atom *argv)
{
if(a->fun) {
bool ok = true;
t_any any;
switch(a->argtp) {
case a_float:
if(argc == 1 && CanbeFloat(argv[0])) {
any.ft = GetAFloat(argv[0]);
((methfun_1)a->fun)(this,any);
}
else ok = false;
break;
case a_int:
if(argc == 1 && CanbeInt(argv[0])) {
any.it = GetAInt(argv[0]);
((methfun_1)a->fun)(this,any);
}
else ok = false;
break;
case a_symbol:
if(argc == 1 && IsSymbol(argv[0])) {
any.st = const_cast<t_symbol *>(GetParamSym(GetSymbol(argv[0]),thisCanvas()));
((methfun_1)a->fun)(this,any);
}
else ok = false;
break;
case a_bool:
if(argc == 1 && CanbeBool(argv[0])) {
any.bt = GetABool(argv[0]);
((methfun_1)a->fun)(this,any);
}
else ok = false;
break;
case a_LIST: {
AtomList la(argc);
for(int i = 0; i < argc; ++i)
if(IsSymbol(argv[i]))
SetSymbol(la[i],GetParamSym(GetSymbol(argv[i]),thisCanvas()));
else
la[i] = argv[i];
any.vt = &la;
((methfun_1)a->fun)(this,any);
break;
}
default:
ERRINTERNAL();
}
if(!ok)
post("%s - wrong arguments for attribute %s",thisName(),GetString(tag));
}
else
post("%s - attribute %s has no get method",thisName(),GetString(tag));
return true;
}
bool flext_base::GetAttrib(const t_symbol *tag,AttrItem *a,AtomList &la) const
{
bool ok = true;
// main attribute tag
if(a) {
if(a->fun) {
t_any any;
switch(a->argtp) {
case a_float: {
((methfun_1)a->fun)(const_cast<flext_base *>(this),any);
la(1);
SetFloat(la[0],any.ft);
break;
}
case a_int: {
((methfun_1)a->fun)(const_cast<flext_base *>(this),any);
la(1);
SetInt(la[0],any.it);
break;
}
case a_bool: {
((methfun_1)a->fun)(const_cast<flext_base *>(this),any);
la(1);
SetBool(la[0],any.bt);
break;
}
case a_symbol: {
((methfun_1)a->fun)(const_cast<flext_base *>(this),any);
la(1);
SetSymbol(la[0],any.st);
break;
}
case a_LIST: {
any.vt = &la;
((methfun_1)a->fun)(const_cast<flext_base *>(this),any);
break;
}
default:
ERRINTERNAL();
ok = false;
}
}
else {
post("%s - attribute %s has no get method",thisName(),GetString(tag));
ok = false;
}
}
else {
error("%s - %s: attribute not found",thisName(),GetString(tag));
ok = false;
}
return ok;
}
bool flext_base::GetAttrib(const t_symbol *s,AtomList &a) const
{
AttrItem *attr = FindAttrib(s,true);
return attr && GetAttrib(s,attr,a);
}
bool flext_base::DumpAttrib(const t_symbol *tag,AttrItem *a) const
{
AtomList la;
bool ret = GetAttrib(tag,a,la);
if(ret) ToOutAnything(GetOutAttr(),tag,la.Count(),la.Atoms());
return ret;
}
bool flext_base::DumpAttrib(const t_symbol *attr) const
{
AttrItem *item = FindAttrib(attr,true);
return item && DumpAttrib(attr,item);
}
bool flext_base::BangAttrib(const t_symbol *attr,AttrItem *item)
{
AtomList val;
AttrItem *item2;
if(!item->IsGet())
item = item->Counterpart();
if(item) {
item2 = item->Counterpart();
return item2 && GetAttrib(attr,item,val) && SetAttrib(attr,item2,val);
}
else
return false;
}
bool flext_base::BangAttrib(const t_symbol *attr)
{
AttrItem *item = FindAttrib(attr,true);
return item && BangAttrib(attr,item);
}
bool flext_base::BangAttribAll()
{
for(int i = 0; i <= 1; ++i) {
ItemCont *a = i?attrhead:clattrhead;
if(a) {
ItemSet &ai = a->GetInlet(); // \todo need to check for presence of inlet 0?
for(ItemSet::iterator as = ai.begin(); as != ai.end(); ++as) {
for(ItemList::iterator al = as->second.begin(); al != as->second.end(); ++al) {
AttrItem *a = (AttrItem *)*al;
if(a->IsGet() && a->BothExist()) BangAttrib(as->first,a);
}
}
}
}
return true;
}
bool flext_base::ShowAttrib(AttrItem *a,bool show) const
{
if(show) a->flags |= AttrItem::afl_shown;
else a->flags &= ~AttrItem::afl_shown;
// also change counterpart, if present
AttrItem *ca = a->Counterpart();
if(ca) {
if(show) ca->flags |= AttrItem::afl_shown;
else ca->flags &= ~AttrItem::afl_shown;
}
return true;
}
bool flext_base::ShowAttrib(const t_symbol *attr,bool show) const
{
AttrItem *item = FindAttrib(attr,true);
return item && ShowAttrib(item,show);
}
|