aboutsummaryrefslogtreecommitdiff
path: root/src/try.c
blob: 816c7de0fa146dc2adca87544f18f348617e2186 (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

/******************************************************
 *
 * 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 (or later)
 *
 ******************************************************/


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