aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/flattr.cpp
blob: e22c15a8a841917256618e536a7205818c36c6fd (plain)
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
/* 

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 <stdlib.h>

#ifdef __MWERKS__
#define STD std
#else
#define STD
#endif

flext_base::attritem::attritem(const t_symbol *t,metharg tp,methfun f,int fl):
	item(t,0,NULL),
	flags(fl),argtp(tp),fun(f)
{}

flext_base::attritem::~attritem()
{
//	if(nxt) delete nxt;
}


//! Add get and set attributes
void flext_base::AddAttrib(itemarr *aa,itemarr *ma,const char *attr,metharg tp,methfun gfun,methfun sfun)
{
	const t_symbol *asym = MakeSymbol(attr);

//	if(sfun) // if commented out, there will be a warning at run-time (more user-friendly)
	{
		attritem *a = new attritem(asym,tp,sfun,attritem::afl_bothexist|attritem::afl_set);
		aa->Add(a); 

		// bind attribute to a method
		methitem *mi = new methitem(0,asym,a);
		mi->SetArgs(sfun,1,new metharg(tp));
		ma->Add(mi);
	}

//	if(gfun) // if commented out, there will be a warning at run-time (more user-friendly)
	{
		attritem *a = new attritem(asym,tp,gfun,attritem::afl_bothexist|attritem::afl_get);
		aa->Add(a); 

		static char tmp[256] = "get";
		strcpy(tmp+3,attr);

		// bind attribute to a method
		methitem *mi = new methitem(0,MakeSymbol(tmp),a);
		mi->SetArgs(gfun,0,NULL);
		ma->Add(mi);
	}
}

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);
}


//! Sorting function for pure symbol atom lists (used with qsort below)
static int sortcmp(const void *a, const void *b) 
{ 
	return strcmp(flext::GetString(*(t_atom *)a),flext::GetString(*(t_atom *)b)); 
}

int flext_base::ListAttr(AtomList &la) const
{
	int cnt = attrhead?attrhead->Count():0;
	int ccnt = clattrhead?clattrhead->Count():0;
	la(ccnt+cnt);

	int ix = 0;
	for(int i = 0; i <= 1; ++i) {
		itemarr *a = i?attrhead:clattrhead;
		if(a) {
			for(int ai = 0; ai < a->Size(); ++ai) {
				for(item *l = a->Item(ai); l; l = l->nxt) 
				{
					attritem *aa = (attritem *)l;

					// only list once!
					if(!aa->BothExist() || aa->IsGet())
						SetSymbol(la[ix++],aa->tag);
				}
			}
		}
	}

	qsort(la.Atoms(),ix,sizeof(t_atom),sortcmp);
	return ix;
}

int flext_base::ListMeth(AtomList &la,int inlet) const
{
	int cnt = methhead?methhead->Count():0;
	int ccnt = clmethhead?clmethhead->Count():0;
	la(ccnt+cnt);

	int ix = 0;
	for(int i = 0; i <= 1; ++i) {
		itemarr *a = i?methhead:clmethhead;
		if(a) {
			for(int ai = 0; ai < a->Size(); ++ai) {
				for(item *l = a->Item(ai); l; l = l->nxt) 
				{
					methitem *aa = (methitem *)l;

					// match inlet and see that it's not related to an attribute
					if(aa->inlet == inlet && !aa->IsAttr())
						SetSymbol(la[ix++],aa->tag);
				}
			}
		}
	}

	qsort(la.Atoms(),ix,sizeof(t_atom),sortcmp);
	return ix;
}


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);
		SetAttrib(tag,nxt-cur-1,argv+cur+1);
	}
	return true;
}

bool flext_base::ListAttrib() const
{
	if(procattr) {
		AtomList la;
		int c = ListAttr(la);
		ToOutAnything(GetOutAttr(),MakeSymbol("attributes"),c,la.Atoms());
		return true;
	}
	else
		return false;
}

bool flext_base::ListMethods(int inlet) const
{
	if(procattr) {
		AtomList la;
		int c = ListMeth(la,inlet);
		ToOutAnything(GetOutAttr(),MakeSymbol("methods"),c,la.Atoms());
		return true;
	}
	else
		return false;
}

bool flext_base::cb_ListMethods(flext_base *c,int argc,const t_atom *argv) 
{ 
	if(argc == 0 || (argc == 1 && CanbeInt(argv[0])))
		return c->ListMethods(argc?GetAInt(argv[0]):0); 
	else
		return false;
}

flext_base::attritem *flext_base::FindAttr(const t_symbol *tag,bool get) const
{
    // first search within object scope
	attritem *a = (attritem *)attrhead->Find(tag);
	while(a && (a->tag != tag || a->inlet != 0 || (get?a->IsSet():a->IsGet()))) a = (attritem *)a->nxt;

    // then (if nothing found) search within class scope
	if(!a) {
		a = (attritem *)clattrhead->Find(tag);	
		while(a && (a->tag != tag || a->inlet != 0 || (get?a->IsSet():a->IsGet()))) a = (attritem *)a->nxt;
	}
	return a;
}

bool flext_base::SetAttrib(const t_symbol *tag,int argc,const t_atom *argv)
{
	// search for matching attribute
	attritem *a = FindAttr(tag,false);
	if(a) 
		return SetAttrib(a,argc,argv);
	else {
		error("%s - %s: attribute not found",thisName(),GetString(tag));
		return true;
	}
}

bool flext_base::SetAttrib(attritem *a,int argc,const t_atom *argv)
{
	if(a->fun) {
		bool ok = true;

		AtomList la;
		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 = GetSymbol(argv[0]);
				((methfun_1)a->fun)(this,any);				
			}
			else ok = false;
			break;
		case a_LIST:
			any.vt = &(la(argc,argv));
			((methfun_1)a->fun)(this,any);				
			break;
		default:
			ERRINTERNAL();
		}

		if(!ok)
			post("%s - wrong arguments for attribute %s",thisName(),GetString(a->tag));
	}
	else
		post("%s - attribute %s has no get method",thisName(),GetString(a->tag));
	return true;
}

bool flext_base::GetAttrib(attritem *a)
{
	// main attribute tag
	if(a) {
		if(a->fun) {
			AtomList la;
			t_any any;
			switch(a->argtp) {
			case a_float: {
				((methfun_1)a->fun)(this,any);				
				la(1);
				SetFloat(la[0],any.ft);
				break;
			}
			case a_int: {
				((methfun_1)a->fun)(this,any);				
				la(1);
				SetInt(la[0],any.it);
				break;
			}
			case a_symbol: {
				((methfun_1)a->fun)(this,any);				
				la(1);
				SetSymbol(la[0],any.st);
				break;
			}
			case a_LIST: {
				any.vt = &la;
				((methfun_1)a->fun)(this,any);				
				break;
			}
			default:
				ERRINTERNAL();
			}
			ToOutAnything(GetOutAttr(),a->tag,la.Count(),la.Atoms());
		}
		else 
			post("%s - attribute %s has no set method",thisName(),GetString(a->tag));
	}
	else
		error("%s - %s: attribute not found",thisName(),GetString(a->tag));
	return true;
}

bool flext_base::DumpAttrib(const t_symbol *attr) const
{
	attritem *item = FindAttr(attr,true);
	return item && const_cast<flext_base *>(this)->GetAttrib(item);
}