aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2009-07-20 16:04:22 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2009-07-20 16:04:22 +0000
commit3dc69475304fc4d89b298bccec6692cfbe2af286 (patch)
tree4b763c6c83d3ba7358a7820e0259f1edb15b1356
parentc654ffa0b748b90ba06a67105fbb979055d72908 (diff)
"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
-rw-r--r--src/try.c93
1 files changed, 93 insertions, 0 deletions
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<argc; i++) {
+ if(atom_getsymbolarg(i,argc, argv)==gensym(",")) {
+ x=try_this(i-start, argv+start);
+ if(x)return x;
+ start=i+1;
+ }
+ }
+
+ x=try_this(argc-start, argv+start);
+
+ return (x);
+}
+
+void try_setup(void)
+{
+ try_class = class_new(gensym("try"),
+ (t_newmethod)try_new, NULL,
+ sizeof(t_try), 0,
+ A_GIMME, 0);
+}