From 3dc69475304fc4d89b298bccec6692cfbe2af286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Mon, 20 Jul 2009 16:04:22 +0000 Subject: "fallback-object": [try foo 10, bar 5] will either behave as [foo 10] (if this is possible) or as [bar 5]. currently only works with objectclasses (as opposed to abstractions) svn path=/trunk/externals/iem/iemguts/; revision=11878 --- src/try.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/try.c diff --git a/src/try.c b/src/try.c new file mode 100644 index 0000000..97feaa3 --- /dev/null +++ b/src/try.c @@ -0,0 +1,93 @@ + +/****************************************************** + * + * try - implementation file + * + * copyleft (c) IOhannes m zmölnig + * + * 2007:forum::für::umläute:2007 + * + * institute of electronic music and acoustics (iem) + * + ****************************************************** + * + * license: GNU General Public License v.2 + * + ******************************************************/ + + +/* + * this object provides a way to create an object with a fallback + * [try bla 13, blu 134] will first try to create an obect [bla 13] and if this fails use [blu 134] instead. + * + * currently this only works for objectclasses (no abstractions) + * currently this doesn't work (well) with [list] + */ + +#include "m_pd.h" +#include "g_canvas.h" + +int glist_getindex(t_glist *x, t_gobj *y); + +/* ------------------------- try ---------------------------- */ + +static t_class *try_class; + +typedef struct _try +{ + t_object x_obj; +} t_try; + + +typedef t_pd *(*t_newgimme)(t_symbol *s, int argc, t_atom *argv); + +t_pd*try_this(int argc, t_atom*argv) { + t_symbol*s=NULL; + if(!argc)return NULL; + + s=atom_getsymbol(argv); + if(A_SYMBOL==argv->a_type) { + argc--; + argv++; + } + + //startpost("[%s] (%x): ", s->s_name, s); postatom(argc, argv); endpost(); + + t_newgimme fun=(t_newgimme)zgetfn(&pd_objectmaker, s); + if(fun) { + //post("found a creator for [%s]", s->s_name); + return fun(s, argc, argv); + } + + return NULL; +} + +static void *try_new(t_symbol*s, int argc, t_atom*argv) +{ + t_pd*x=NULL; + int start=0, i=0; + if(!pd_objectmaker) { + error("[try] could not find pd_objectmaker"); + return NULL; + } + + for(i=0; i