aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/fllib.cpp
blob: 6b1ccb926bdcdd294fda705e6ea21dc0ae7af738 (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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

Copyright (c) 2001-2005 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

/*! \file fllib.cpp
    \brief Code for handling of object (and library) creation functions.
*/

#include "flext.h"
#include "flinternal.h"

#include <stdarg.h>
#include <string.h>
#include <ctype.h>

#define ALIASDEL ','

#define ALIASSLASHES ":/\\"
#if FLEXT_OS == FLEXT_OS_MAC
	#define ALIASSLASH ':'
#elif FLEXT_OS == FLEXT_OS_WIN
	#if FLEXT_SYS == FLEXT_SYS_PD
		#define ALIASSLASH '/'
	#elif FLEXT_SYS == FLEXT_SYS_MAX
		#define ALIASSLASH '/'
	#else
		#error "Undefined"
	#endif
#else
	// default to "/"
	#define ALIASSLASH '/'
#endif

//! Extract space-delimited words from a string
static const char *extract(const char *name,int ix = 0)
{
	static char tmp[1024];
	const char *n = name;
	
	const char *del = strchr(name,ALIASDEL);

	if(del) {
		if(ix < 0) {
			char *t = tmp;
			while(n < del && (isspace(*n) || strchr(ALIASSLASHES,*n))) ++n;
			while(n < del && !isspace(*n)) {
				char c = *(n++);
				*(t++) = strchr(ALIASSLASHES,c)?ALIASSLASH:c;
			}
			while(*t == ALIASSLASH && t > tmp) --t;
			*t = 0;

			return tmp;
		}

		n = del+1;
	}

	while(*n && isspace(*n)) ++n;
	
	for(int i = 0; n && *n; ++i) {
		if(i == ix) {
			char *t = tmp;

			for(; *n && !isspace(*n); ++t,++n) *t = *n;
			*t = 0;
			return *tmp?tmp:NULL;
		}
		else {
			while(*n && !isspace(*n)) ++n;
			while(*n && isspace(*n)) ++n;		
		}
	}

	return NULL;
}


//! Check if object's name ends with a tilde
bool flext::chktilde(const char *objname)
{
//	int stplen = strlen(setupfun);
	bool tilde = true; //!strncmp(setupfun,"_tilde",6);

	if((objname[strlen(objname)-1] == '~'?1:0)^(tilde?1:0)) {
		if(tilde) 
			error("flext: %s (no trailing ~) is defined as a tilde object",objname);
		else
			error("flext::check_tilde: %s is no tilde object",objname);
		return true;
	} 
	else
		return false;
}



// this class stands for one registered object
// it holds the class, type flags, constructor and destructor of the object and the creation arg types
// it will never be destroyed
class libclass:
    public flext_root
{
public:
	libclass(t_class *&cl,flext_obj *(*newf)(int,t_atom *),void (*freef)(flext_hdr *)); 
	
	flext_obj *(*newfun)(int,t_atom *);
	void (*freefun)(flext_hdr *c);

	t_class *const &clss;
	bool lib,dsp,attr;
	int argc;
	int *argv;
};

libclass::libclass(t_class *&cl,flext_obj *(*newf)(int,t_atom *),void (*freef)(flext_hdr *)): 
	newfun(newf),freefun(freef),
	clss(cl),
	argc(0),argv(NULL) 
{}


typedef TablePtrMap<const t_symbol *,libclass *,8> LibMap;

static LibMap libnames;

//! Store or retrieve registered classes
static libclass *FindName(const t_symbol *s,libclass *o = NULL) 
{
    libclass *cl = libnames.find(s);
    if(!cl) libnames.insert(s,cl = o);
    return cl;
}


// for Max/MSP, the library is represented by a special object (class) registered at startup
// all objects in the library are clones of that library object - they share the same class
#if FLEXT_SYS == FLEXT_SYS_MAX
static t_class *lib_class = NULL;
static const t_symbol *lib_name = NULL;

flext_obj::t_classid flext_obj::thisClassId() const 
{ 
    return FindName(thisNameSym()); 
}

t_class *flext_obj::getClass(t_classid id) { return reinterpret_cast<libclass *>(id)->clss; }
#endif

void flext_obj::lib_init(const char *name,void setupfun(),bool attr)
{
    flext::Setup();

#if FLEXT_SYS == FLEXT_SYS_MAX
	lib_name = MakeSymbol(name);
	::setup(
		(t_messlist **)&lib_class,
		(t_newmethod)obj_new,(t_method)obj_free,
		sizeof(flext_hdr),NULL,A_GIMME,A_NULL);
#endif
	process_attributes = attr;

    try {
	    setupfun();
    }
    catch(std::exception &x) {
        error("%s - %s",name,x.what());
    }
    catch(char *txt) {
    	error("%s - %s",name,txt);
    }
    catch(...) {
    	error("%s - Unknown exception at library setup",name);
    }
}

#if FLEXT_SYS == FLEXT_SYS_JMAX
static void jmax_class_inst(t_class *cl) 
{
	fts_class_init(cl, sizeof(flext_hdr),flext_obj::obj_new,flext_obj::obj_free);
}
#endif

void flext_obj::obj_add(bool lib,bool dsp,bool attr,const char *idname,const char *names,void setupfun(t_classid),flext_obj *(*newfun)(int,t_atom *),void (*freefun)(flext_hdr *),int argtp1,...)
{
	// get first possible object name
	const t_symbol *nsym = MakeSymbol(extract(names));
	
#ifdef FLEXT_DEBUG
	if(dsp) chktilde(GetString(nsym));
#endif

	if(!lib) process_attributes = attr;

	// set dynamic class pointer
	t_class **cl = 
#if FLEXT_SYS == FLEXT_SYS_MAX
		lib?&lib_class:
#endif
		new t_class *;

	// register object class
#if FLEXT_SYS == FLEXT_SYS_PD
    *cl = ::class_new(
		(t_symbol *)nsym,
    	(t_newmethod)obj_new,(t_method)obj_free,
     	sizeof(flext_hdr),CLASS_DEFAULT,A_GIMME,A_NULL);
#elif FLEXT_SYS == FLEXT_SYS_MAX
	if(!lib) {
		::setup(
			(t_messlist **)cl,
    		(t_newmethod)obj_new,(t_method)obj_free,
     		sizeof(flext_hdr),NULL,A_GIMME,A_NULL);
     	// attention: in Max/MSP the *cl variable is not initialized after that call.
     	// just the address is stored, the initialization then occurs with the first object instance!
	}
#elif FLEXT_SYS == FLEXT_SYS_JMAX
	*cl = fts_class_install(nsym, jmax_class_inst);
#else
#error
#endif

	// make new dynamic object
	libclass *lo = new libclass(*cl,newfun,freefun);
	lo->lib = lib;
	lo->dsp = dsp;
	lo->attr = process_attributes;

//	post("ADDCLASS %p -> LIBOBJ %p -> %p",*cl,lo,lo->clss);

	// parse the argument type list and store it with the object
	if(argtp1 == FLEXTTPN_VAR)
		lo->argc = -1;
	else {
		int argtp,i;
		va_list marker;
		
		// parse a first time and count only
		va_start(marker,argtp1);
		for(argtp = argtp1; argtp != FLEXTTPN_NULL; ++lo->argc) argtp = (int)va_arg(marker,int); 
		va_end(marker);

		lo->argv = new int[lo->argc];
	
		// now parse and store
		va_start(marker,argtp1);
		for(argtp = argtp1,i = 0; i < lo->argc; ++i) {
			lo->argv[i] = argtp;
			argtp = (int)va_arg(marker,int); 
		}
		va_end(marker);
	}

	// get unique class id
#if FLEXT_SYS == FLEXT_SYS_PD || FLEXT_SYS == FLEXT_SYS_JMAX
	t_classid clid = lo->clss;
#else
	// in Max/MSP the t_class *value can't be used because it's possible that's it's not yet set!!
	t_classid clid = lo;
#endif

	// make help reference
	flext_obj::DefineHelp(clid,idname,extract(names,-1),dsp);

	for(int ix = 0; ; ++ix) {
		// in this loop register all the possible aliases of the object
	
		const char *c = ix?extract(names,ix):GetString(nsym);
		if(!c || !*c) break;

		// add to name list
        const t_symbol *lsym = MakeSymbol(c);
		/*libclass *lcl =*/ FindName(lsym,lo);
	
#if FLEXT_SYS == FLEXT_SYS_PD
		if(ix > 0) 
			// in PD the first name is already registered with class creation
			::class_addcreator((t_newmethod)obj_new,(t_symbol *)lsym,A_GIMME,A_NULL);
#elif FLEXT_SYS == FLEXT_SYS_MAX
		if(ix > 0 || lib) 
			// in Max/MSP the first alias gets its name from the name of the object file,
			// unless it is a library (then the name can be different)
			::alias(const_cast<char *>(c));  
#elif FLEXT_SYS == FLEXT_SYS_JMAX
		if(ix > 0)  fts_class_alias(lo->clss,lsym);
#else
#error
#endif	
	}

    try {
	    // call class setup function
        setupfun(clid);
    }
    catch(std::exception &x) {
        error("%s - Exception while initializing class: %s",idname,x.what());
    }
    catch(char *txt) {
    	error("%s - Exception while initializing class: %s",idname,txt);
    }
    catch(...) {
    	error("%s - Unknown exception while initializing class",idname);
    }
}
	

#define NEWARGS 256 // must be larger than FLEXT_NEWARGS = 5

typedef flext_obj *(*libfun)(int,t_atom *);

#if FLEXT_SYS == FLEXT_SYS_JMAX
void flext_obj::obj_new(fts_object_t *o, int, fts_symbol_t s, int _argc_, const fts_atom_t *argv)
{
	flext_hdr *obj = (flext_hdr *)o;
#else
#if FLEXT_SYS == FLEXT_SYS_MAX
flext_hdr *flext_obj::obj_new(const t_symbol *s,short _argc_,t_atom *argv)
#else
flext_hdr *flext_obj::obj_new(const t_symbol *s,int _argc_,t_atom *argv)
#endif
{
	flext_hdr *obj = NULL;
#endif
	libclass *lo = FindName(s);
	if(lo) {
		bool ok = true;
		t_atom args[NEWARGS]; 

		int argc = _argc_;
		if(lo->attr) {
			argc = flext_base::CheckAttrib(argc,argv);
		}

		if(lo->argc >= 0) {
#ifdef FLEXT_DEBUG
			if(lo->argc > FLEXT_MAXNEWARGS) { ERRINTERNAL(); ok = false; }
#endif

			int misnum = 0;
			if(argc > lo->argc) { ok = false; misnum = 1; }

			for(int i = 0; ok && i < lo->argc; ++i) {
				switch(lo->argv[i]) {
#if FLEXT_SYS != FLEXT_SYS_PD
				case FLEXTTPN_INT:
				case FLEXTTPN_DEFINT:
					if(i >= argc)
						if(lo->argv[i] == FLEXTTPN_DEFINT) SetInt(args[i],0);
						else { misnum = -1,ok = false; break; }
					else if(IsInt(argv[i])) args[i] = argv[i];
					else if(IsFloat(argv[i])) SetInt(args[i],(int)GetFloat(argv[i]));
					else ok = false;
					break;
#endif
				case FLEXTTPN_FLOAT:
				case FLEXTTPN_DEFFLOAT:
					if(i >= argc)
						if(lo->argv[i] == FLEXTTPN_DEFFLOAT) SetFloat(args[i],0);
						else { misnum = -1,ok = false; break; }
					else if(IsInt(argv[i])) SetFloat(args[i],(float)GetInt(argv[i]));
					else if(IsFloat(argv[i])) args[i] = argv[i];
					else ok = false;
					break;
				case FLEXTTPN_SYM:
				case FLEXTTPN_DEFSYM:
					// \todo shall we analyze the patcher args????... should already be done!
					if(i >= argc)
						if(lo->argv[i] == FLEXTTPN_DEFSYM) SetSymbol(args[i],sym__);
						else { misnum = -1,ok = false; break; }
					else if(IsSymbol(argv[i])) 
//							SetSymbol(args[i],GetParamSym(GetSymbol(argv[i]),NULL));
						args[i] = argv[i];
					else ok = false;
					break;
				}	
			}

			if(!ok)
				if(misnum)
					error("%s: %s creation arguments",GetString(s),misnum < 0?"Not enough":"Too many");
				else
					error("%s: Creation arguments do not match",GetString(s));
		}


		if(ok) {
            try {
			    t_classid clid;

#if FLEXT_SYS == FLEXT_SYS_PD
			    clid = lo->clss;
			    obj = (flext_hdr *)::pd_new(lo->clss);
#elif FLEXT_SYS == FLEXT_SYS_MAX
			    clid = lo;
			    obj = (flext_hdr *)::newobject(lo->clss);
#elif FLEXT_SYS == FLEXT_SYS_JMAX
			    clid = lo->clss;
#else
#error
#endif

                flext_obj::m_holder = obj;
			    flext_obj::m_holdname = s;
			    flext_obj::m_holdattr = lo->attr;
                flext_obj::initing = true;

			    // get actual flext object (newfun calls "new flext_obj()")
			    if(lo->argc >= 0)
				    obj->data = lo->newfun(lo->argc,args); 
			    else
				    obj->data = lo->newfun(argc,argv); 
    	
			    flext_obj::m_holder = NULL;
			    flext_obj::m_holdname = NULL;
			    flext_obj::m_holdattr = false;

			    ok = obj->data &&
				    // check constructor exit flag
				    obj->data->InitOk();

			    if(ok) {
				    if(lo->attr) {
					    // DON'T convert eventual patcher args here... this is done by the actual attribute stuff
					    // so that the initial $- or #- be preserved!

					    // store creation args for attribute initialization (inside flext_base::Init())
					    flext_obj::m_holdaargc = _argc_-argc;
					    flext_obj::m_holdaargv = argv+argc;
				    }
				    else {
					    flext_obj::m_holdaargc = 0;
					    flext_obj::m_holdaargv = NULL;
				    }

				    // call virtual init function 
				    // here, inlets, outlets, methods and attributes can be set up
				    ok = obj->data->Init();

                    flext_obj::initing = false;

				    // call another virtual init function 
				    if(ok) ok = obj->data->Finalize();

				    flext_obj::m_holdaargc = 0;
				    flext_obj::m_holdaargv = NULL;
			    }

            } //try
            catch(std::exception &x) {
                error("%s - Exception while creating object: %s",GetString(s),x.what());
                ok = false;
            }
            catch(char *txt) {
    		    error("%s - Exception while creating object: %s",GetString(s),txt);
                ok = false;
            }
            catch(...) {
    		    error("%s - Unknown exception while creating object",GetString(s));
                ok = false;
            }

            flext_obj::initing = false;

            if(!ok) { 
				// there was some init error, free object
				lo->freefun(obj); 
				obj = NULL; 
			}
		}
	}
#ifdef FLEXT_DEBUG
	else
#if FLEXT_SYS == FLEXT_SYS_MAX
		// in Max/MSP an object with the name of the library exists, even if not explicitly declared!
		if(s != lib_name) 
#endif
		error("Class %s not found in library!",s->s_name);
#endif

#if FLEXT_SYS != FLEXT_SYS_JMAX
	return obj;
#endif
}

#if FLEXT_SYS == FLEXT_SYS_JMAX
void flext_obj::obj_free(fts_object_t *h, int winlet, fts_symbol_t s, int ac, const fts_atom_t *at)
#else
void flext_obj::obj_free(flext_hdr *h)
#endif
{
	flext_hdr *hdr = (flext_hdr *)h;
	const t_symbol *name = hdr->data->thisNameSym();
	libclass *lcl = FindName(name);

	if(lcl) {
        try {
            flext_obj::exiting = true;

		    // call virtual exit function
		    hdr->data->Exit();

		    // now call object destructor and deallocate
		    lcl->freefun(hdr);
        } //try
        catch(std::exception &x) {
            error("%s - Exception while destroying object: %s",GetString(name),x.what());
        }
        catch(char *txt) {
    		error("%s - Exception while destroying object: %s",GetString(name),txt);
        }
        catch(...) {
    		error("%s - Unknown exception while destroying object",GetString(name));
        }

        flext_obj::exiting = false;
    }
#ifdef FLEXT_DEBUG
	else 
#if FLEXT_SYS == FLEXT_SYS_MAX
		// in Max/MSP an object with the name of the library exists, even if not explicitely declared!
		if(name != lib_name) 
#endif
		error("Class %s not found in library!",name);
#endif
}