g_traversal.c 40.4 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* This file defines Text objects which traverse data contained in scalars
and arrays:

pointer - point to an object belonging to a template
get -     get numeric fields
set -     change numeric fields
element - get an array element
getsize - get the size of an array
setsize - change the size of an array
append -  add an element to a list
sublist - get a pointer into a list which is an element of another scalar
16
field -   get numeric/symbolic field while within a canvas field
Miller Puckette's avatar
Miller Puckette committed
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

*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>      /* for read/write to files */
#include "m_pd.h"
#include "g_canvas.h"

/* ------------- gstubs and gpointers - safe pointing --------------- */

/* create a gstub which is "owned" by a glist (gl) or an array ("a"). */

t_gstub *gstub_new(t_glist *gl, t_array *a)
{
    t_gstub *gs = t_getbytes(sizeof(*gs));
    if (gl)
    {
        gs->gs_which = GP_GLIST;
        gs->gs_un.gs_glist = gl;
    }
    else
    {
        gs->gs_which = GP_ARRAY;
        gs->gs_un.gs_array = a;
    }
    gs->gs_refcount = 0;
    return (gs);
}

/* when a "gpointer" is set to point to this stub (so we can later chase
down the owner) we increase a reference count.  The following routine is called
whenever a gpointer is unset from pointing here.  If the owner is
gone and the refcount goes to zero, we can free the gstub safely. */

static void gstub_dis(t_gstub *gs)
{
    int refcount = --gs->gs_refcount;
    if ((!refcount) && gs->gs_which == GP_NONE)
        t_freebytes(gs, sizeof (*gs));
    else if (refcount < 0) bug("gstub_dis");
}

/* this routing is called by the owner to inform the gstub that it is
being deleted.  If no gpointers are pointing here, we can free the gstub;
otherwise we wait for the last gstub_dis() to free it. */

void gstub_cutoff(t_gstub *gs)
{
    gs->gs_which = GP_NONE;
    if (gs->gs_refcount < 0) bug("gstub_cutoff");
    if (!gs->gs_refcount) t_freebytes(gs, sizeof (*gs));
}


72
int gpointer_docheck(const t_gpointer *gp, int headok, int gobjok)
Miller Puckette's avatar
Miller Puckette committed
73
74
75
76
77
78
79
80
81
82
{
    t_gstub *gs = gp->gp_stub;
    if (!gs) return (0);
    if (gs->gs_which == GP_ARRAY)
    {
        if (gs->gs_un.gs_array->a_valid != gp->gp_valid) return (0);
        else return (1);
    }
    else if (gs->gs_which == GP_GLIST)
    {
83
84
        if (!headok && !((t_scalar *)(gp->gp_un.gp_gobj)))
            return (0);
85
        else if (!gobjok && gp->gp_un.gp_gobj &&
86
87
88
89
90
91
                 pd_class(&gp->gp_un.gp_gobj->g_pd) != scalar_class)
            return (0);
        else if (gs->gs_un.gs_glist->gl_valid != gp->gp_valid)
            return (0);
        else
            return (1);
Miller Puckette's avatar
Miller Puckette committed
92
93
94
95
    }
    else return (0);
}

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* call this to verify that a pointer is fresh, i.e., that it either
points to real data or to the head of a list, and that in either case
the object hasn't disappeared since this pointer was generated. 
Unless "headok" is set,  the routine also fails for the head of a list.*/

int gpointer_check(const t_gpointer *gp, int headok)
{
    return (gpointer_docheck(gp, headok, 0));
}

/* more general form for checking for pointers to gobjs */
int gpointer_check_gobj(const t_gpointer *gp)
{
    return (gpointer_docheck(gp, 0, 1));
}

Miller Puckette's avatar
Miller Puckette committed
112
113
114
115
116
117
118
119
/* get the template for the object pointer to.  Assumes we've already checked
freshness.  Returns 0 if head of list. */

static t_symbol *gpointer_gettemplatesym(const t_gpointer *gp)
{
    t_gstub *gs = gp->gp_stub;
    if (gs->gs_which == GP_GLIST)
    {
120
        t_scalar *sc = (t_scalar *)(gp->gp_un.gp_gobj);
Miller Puckette's avatar
Miller Puckette committed
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
        if (sc)
            return (sc->sc_template);
        else return (0);
    }
    else
    {
        t_array *a = gs->gs_un.gs_array;
        return (a->a_templatesym);
    }
}

    /* copy a pointer to another, assuming the second one hasn't yet been
    initialized.  New gpointers should be initialized either by this
    routine or by gpointer_init below. */
void gpointer_copy(const t_gpointer *gpfrom, t_gpointer *gpto)
{
    *gpto = *gpfrom;
    if (gpto->gp_stub)
        gpto->gp_stub->gs_refcount++;
    else bug("gpointer_copy");
}

    /* clear a gpointer that was previously set, releasing the associted
    gstub if this was the last reference to it. */
void gpointer_unset(t_gpointer *gp)
{
    t_gstub *gs;
    if (gs = gp->gp_stub)
    {
        gstub_dis(gs);
        gp->gp_stub = 0;
    }
}

155
void gpointer_setglist(t_gpointer *gp, t_glist *glist, t_gobj *x)
Miller Puckette's avatar
Miller Puckette committed
156
157
158
159
160
{
    t_gstub *gs;
    if (gs = gp->gp_stub) gstub_dis(gs);
    gp->gp_stub = gs = glist->gl_stub;
    gp->gp_valid = glist->gl_valid;
161
    gp->gp_un.gp_gobj = x;
Miller Puckette's avatar
Miller Puckette committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    gs->gs_refcount++;
}

void gpointer_setarray(t_gpointer *gp, t_array *array, t_word *w)
{
    t_gstub *gs;
    if (gs = gp->gp_stub) gstub_dis(gs);
    gp->gp_stub = gs = array->a_stub;
    gp->gp_valid = array->a_valid;
    gp->gp_un.gp_w = w;
    gs->gs_refcount++;
}

void gpointer_init(t_gpointer *gp)
{
    gp->gp_stub = 0;
    gp->gp_valid = 0;
179
    gp->gp_un.gp_gobj = 0;
Miller Puckette's avatar
Miller Puckette committed
180
181
}

182
183
184
185
186
187
188
189
190
191
192
193
194
    /* templates are named using the name-bashing by which canvases bind
    thenselves, with a leading "pd-".  LATER see if we can have templates
    occupy their real names.  Meanwhile, if a template has an empty name
    or is named "-" (as when passed as a "-" argument to "get", etc.), just
    return &s_; objects should check for this and allow it as a wild
    card when appropriate. */
static t_symbol *template_getbindsym(t_symbol *s)
{
    if (!*s->s_name || !strcmp(s->s_name, "-"))
        return (&s_);
    else return (canvas_makebindsym(s));
}

Miller Puckette's avatar
Miller Puckette committed
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
/* ---------------------- pointers ----------------------------- */

static t_class *ptrobj_class;

typedef struct 
{
    t_symbol *to_type;
    t_outlet *to_outlet;
} t_typedout;

typedef struct _ptrobj
{
    t_object x_obj;
    t_gpointer x_gp;
    t_typedout *x_typedout;
    int x_ntypedout;
    t_outlet *x_otherout;
    t_outlet *x_bangout;
} t_ptrobj;

static void *ptrobj_new(t_symbol *classname, int argc, t_atom *argv)
{
    t_ptrobj *x = (t_ptrobj *)pd_new(ptrobj_class);
    t_typedout *to;
    int n;
    gpointer_init(&x->x_gp);
    x->x_typedout = to = (t_typedout *)getbytes(argc * sizeof (*to));
    x->x_ntypedout = n = argc;
    for (; n--; to++)
    {
        to->to_outlet = outlet_new(&x->x_obj, &s_pointer);
226
        to->to_type = template_getbindsym(atom_getsymbol(argv++));
Miller Puckette's avatar
Miller Puckette committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    }
    x->x_otherout = outlet_new(&x->x_obj, &s_pointer);
    x->x_bangout = outlet_new(&x->x_obj, &s_bang);
    pointerinlet_new(&x->x_obj, &x->x_gp);
    return (x);
}

static void ptrobj_traverse(t_ptrobj *x, t_symbol *s)
{
    t_glist *glist = (t_glist *)pd_findbyclass(s, canvas_class);
    if (glist) gpointer_setglist(&x->x_gp, glist, 0);
    else pd_error(x, "pointer: list '%s' not found", s->s_name);
}

241
static void ptrobj_vnext(t_ptrobj *x, t_float f)
Miller Puckette's avatar
Miller Puckette committed
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
{
    t_gobj *gobj;
    t_gpointer *gp = &x->x_gp;
    t_gstub *gs = gp->gp_stub;
    t_glist *glist;
    int wantselected = (f != 0);

    if (!gs)
    {
        pd_error(x, "ptrobj_next: no current pointer");
        return;
    }
    if (gs->gs_which != GP_GLIST)
    {
        pd_error(x, "ptrobj_next: lists only, not arrays");
        return;
    }
    glist = gs->gs_un.gs_glist;
    if (glist->gl_valid != gp->gp_valid)
    {
        pd_error(x, "ptrobj_next: stale pointer");
        return;
    }
    if (wantselected && !glist_isvisible(glist))
    {
        pd_error(x,
            "ptrobj_vnext: next-selected only works for a visible window");
        return;
    }
271
    gobj = gp->gp_un.gp_gobj;
Miller Puckette's avatar
Miller Puckette committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    
    if (!gobj) gobj = glist->gl_list;
    else gobj = gobj->g_next;
    while (gobj && ((pd_class(&gobj->g_pd) != scalar_class) ||
        (wantselected && !glist_isselected(glist, gobj))))
            gobj = gobj->g_next;
    
    if (gobj)
    {
        t_typedout *to;
        int n;
        t_scalar *sc = (t_scalar *)gobj;
        t_symbol *templatesym = sc->sc_template;

286
        gp->gp_un.gp_gobj = &sc->sc_gobj; 
Miller Puckette's avatar
Miller Puckette committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        for (n = x->x_ntypedout, to = x->x_typedout; n--; to++)
        {
            if (to->to_type == templatesym)
            {
                outlet_pointer(to->to_outlet, &x->x_gp);
                return;
            }
        }
        outlet_pointer(x->x_otherout, &x->x_gp);
    }
    else
    {
        gpointer_unset(gp);
        outlet_bang(x->x_bangout);
    }
}

static void ptrobj_next(t_ptrobj *x)
{
    ptrobj_vnext(x, 0);
}
308
309

    /* send a message to the window containing the object pointed to */ 
Miller Puckette's avatar
Miller Puckette committed
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
static void ptrobj_sendwindow(t_ptrobj *x, t_symbol *s, int argc, t_atom *argv)
{
    t_glist *glist;
    t_pd *canvas;
    t_gstub *gs;
    if (!gpointer_check(&x->x_gp, 1))
    {
        pd_error(x, "ptrobj_bang: empty pointer");
        return;
    }
    gs = x->x_gp.gp_stub;
    if (gs->gs_which == GP_GLIST)
        glist = gs->gs_un.gs_glist;  
    else
    {
        t_array *owner_array = gs->gs_un.gs_array;
        while (owner_array->a_gp.gp_stub->gs_which == GP_ARRAY)
            owner_array = owner_array->a_gp.gp_stub->gs_un.gs_array;
        glist = owner_array->a_gp.gp_stub->gs_un.gs_glist;  
    }
    canvas = (t_pd *)glist_getcanvas(glist);
    if (argc && argv->a_type == A_SYMBOL)
        pd_typedmess(canvas, argv->a_w.w_symbol, argc-1, argv+1);
    else pd_error(x, "send-window: no message?");
}

336
337
338
339
340
341
342
343
344
345
    /* send the pointer to the named object */
static void ptrobj_send(t_ptrobj *x, t_symbol *s)
{
    if (!s->s_thing)
        pd_error(x, "%s: no such object", s->s_name);
    else if (!gpointer_check(&x->x_gp, 1))
        pd_error(x, "pointer_send: empty pointer");
    else pd_pointer(s->s_thing, &x->x_gp);
}

Miller Puckette's avatar
Miller Puckette committed
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
static void ptrobj_bang(t_ptrobj *x)
{
    t_symbol *templatesym;
    int n;
    t_typedout *to;
    if (!gpointer_check(&x->x_gp, 1))
    {
        pd_error(x, "ptrobj_bang: empty pointer");
        return;
    }
    templatesym = gpointer_gettemplatesym(&x->x_gp);
    for (n = x->x_ntypedout, to = x->x_typedout; n--; to++)
    {
        if (to->to_type == templatesym)
        {
            outlet_pointer(to->to_outlet, &x->x_gp);
            return;
        }
    }
    outlet_pointer(x->x_otherout, &x->x_gp);
}


static void ptrobj_pointer(t_ptrobj *x, t_gpointer *gp)
{
    gpointer_unset(&x->x_gp);
    gpointer_copy(gp, &x->x_gp);
    ptrobj_bang(x);
}


static void ptrobj_rewind(t_ptrobj *x)
{
    t_glist *glist;
    t_gstub *gs;
    if (!gpointer_check(&x->x_gp, 1))
    {
        pd_error(x, "pointer_rewind: empty pointer");
        return;
    }
    gs = x->x_gp.gp_stub;
    if (gs->gs_which != GP_GLIST)
    {
        pd_error(x, "pointer_rewind: sorry, unavailable for arrays");
        return;
    }
    glist = gs->gs_un.gs_glist;  
    gpointer_setglist(&x->x_gp, glist, 0);
    ptrobj_bang(x);
}

static void ptrobj_free(t_ptrobj *x)
{
    freebytes(x->x_typedout, x->x_ntypedout * sizeof (*x->x_typedout));
    gpointer_unset(&x->x_gp);
}

static void ptrobj_setup(void)
{
    ptrobj_class = class_new(gensym("pointer"), (t_newmethod)ptrobj_new,
        (t_method)ptrobj_free, sizeof(t_ptrobj), 0, A_GIMME, 0);
407
408
409
    class_addmethod(ptrobj_class, (t_method)ptrobj_next, gensym("next"), 0); 
    class_addmethod(ptrobj_class, (t_method)ptrobj_send, gensym("send"),
        A_SYMBOL, 0);
Miller Puckette's avatar
Miller Puckette committed
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
    class_addmethod(ptrobj_class, (t_method)ptrobj_traverse, gensym("traverse"),
        A_SYMBOL, 0); 
    class_addmethod(ptrobj_class, (t_method)ptrobj_vnext, gensym("vnext"), 
        A_DEFFLOAT, 0); 
    class_addmethod(ptrobj_class, (t_method)ptrobj_sendwindow,
        gensym("send-window"), A_GIMME, 0); 
    class_addmethod(ptrobj_class, (t_method)ptrobj_rewind,
        gensym("rewind"), 0); 
    class_addpointer(ptrobj_class, ptrobj_pointer); 
    class_addbang(ptrobj_class, ptrobj_bang); 
}

/* ---------------------- get ----------------------------- */

static t_class *get_class;

typedef struct _getvariable
{
    t_symbol *gv_sym;
    t_outlet *gv_outlet;
} t_getvariable;

typedef struct _get
{
    t_object x_obj;
    t_symbol *x_templatesym;
    int x_nout;
    t_getvariable *x_variables;
} t_get;

static void *get_new(t_symbol *why, int argc, t_atom *argv)
{
    t_get *x = (t_get *)pd_new(get_class);
443
444
    int varcount, i;
    t_atom at, *varvec;
Miller Puckette's avatar
Miller Puckette committed
445
    t_getvariable *sp;
446

Miller Puckette's avatar
Miller Puckette committed
447
    x->x_templatesym = canvas_makebindsym(atom_getsymbolarg(0, argc, argv));
448
449
450
451
452
453
454
    if (argc < 2) /* have at least one outlet */
    {
        varcount = 1;
        varvec = &at;
        SETSYMBOL(&at, &s_); /* set field to empty symbol */
    }
    else varcount = argc - 1, varvec = argv + 1;
Miller Puckette's avatar
Miller Puckette committed
455
    x->x_variables
456
457
458
        = (t_getvariable *)getbytes(varcount * sizeof (*x->x_variables));
    x->x_nout = varcount;
    for (i = 0, sp = x->x_variables; i < varcount; i++, sp++)
Miller Puckette's avatar
Miller Puckette committed
459
    {
460
        sp->gv_sym = atom_getsymbolarg(i, varcount, varvec);
Miller Puckette's avatar
Miller Puckette committed
461
462
463
464
465
466
467
468
        sp->gv_outlet = outlet_new(&x->x_obj, 0);
            /* LATER connect with the template and set the outlet's type
            correctly.  We can't yet guarantee that the template is there
            before we hit this routine. */
    }
    return (x);
}

469
470
471
472
473
474
static void get_set(t_get *x, t_symbol *templatesym, t_symbol *field)
{
    if (x->x_nout != 1)
        pd_error(x, "get: cannot set multiple fields.");
    else
    {
475
       x->x_templatesym = template_getbindsym(templatesym); 
476
477
478
479
       x->x_variables->gv_sym = field;
    }
}

Miller Puckette's avatar
Miller Puckette committed
480
481
482
static void get_pointer(t_get *x, t_gpointer *gp)
{
    int nitems = x->x_nout, i;
483
484
    t_symbol *templatesym;
    t_template *template;
Miller Puckette's avatar
Miller Puckette committed
485
    t_gstub *gs = gp->gp_stub;
486
    t_word *vec;
Miller Puckette's avatar
Miller Puckette committed
487
    t_getvariable *vp;
488
489

    if (!gpointer_check(gp, 0))
Miller Puckette's avatar
Miller Puckette committed
490
    {
491
        pd_error(x, "get: stale or empty pointer");
Miller Puckette's avatar
Miller Puckette committed
492
493
        return;
    }
494
    if (*x->x_templatesym->s_name)
Miller Puckette's avatar
Miller Puckette committed
495
    {
496
497
498
499
500
501
502
503
504
505
506
        if ((templatesym = x->x_templatesym) != gpointer_gettemplatesym(gp))
        {
            pd_error(x, "get %s: got wrong template (%s)",
                templatesym->s_name, gpointer_gettemplatesym(gp)->s_name);
            return;
        }
    }
    else templatesym = gpointer_gettemplatesym(gp);
    if (!(template = template_findbyname(templatesym)))
    {
        pd_error(x, "get: couldn't find template %s", templatesym->s_name);
Miller Puckette's avatar
Miller Puckette committed
507
508
509
        return;
    }
    if (gs->gs_which == GP_ARRAY) vec = gp->gp_un.gp_w;
510
    else vec = ((t_scalar *)(gp->gp_un.gp_gobj))->sc_vec;
Miller Puckette's avatar
Miller Puckette committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
    for (i = nitems - 1, vp = x->x_variables + i; i >= 0; i--, vp--)
    {
        int onset, type;
        t_symbol *arraytype;
        if (template_find_field(template, vp->gv_sym, &onset, &type, &arraytype))
        {
            if (type == DT_FLOAT)
                outlet_float(vp->gv_outlet,
                    *(t_float *)(((char *)vec) + onset));
            else if (type == DT_SYMBOL)
                outlet_symbol(vp->gv_outlet,
                    *(t_symbol **)(((char *)vec) + onset));
            else pd_error(x, "get: %s.%s is not a number or symbol",
                    template->t_sym->s_name, vp->gv_sym->s_name);
        }
        else pd_error(x, "get: %s.%s: no such field",
            template->t_sym->s_name, vp->gv_sym->s_name);
    }
}

static void get_free(t_get *x)
{
    freebytes(x->x_variables, x->x_nout * sizeof (*x->x_variables));
}

static void get_setup(void)
{
    get_class = class_new(gensym("get"), (t_newmethod)get_new,
        (t_method)get_free, sizeof(t_get), 0, A_GIMME, 0);
    class_addpointer(get_class, get_pointer); 
541
542
    class_addmethod(get_class, (t_method)get_set, gensym("set"),
        A_SYMBOL, A_SYMBOL, 0); 
Miller Puckette's avatar
Miller Puckette committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
}

/* ---------------------- set ----------------------------- */

static t_class *set_class;

typedef struct _setvariable
{
    t_symbol *gv_sym;
    union word gv_w;
} t_setvariable;

typedef struct _set
{
    t_object x_obj;
    t_gpointer x_gp;
    t_symbol *x_templatesym;
    int x_nin;
    int x_issymbol;
    t_setvariable *x_variables;
} t_set;

static void *set_new(t_symbol *why, int argc, t_atom *argv)
{
    t_set *x = (t_set *)pd_new(set_class);
568
    int i, varcount;
Miller Puckette's avatar
Miller Puckette committed
569
    t_setvariable *sp;
570
    t_atom at, *varvec;
Miller Puckette's avatar
Miller Puckette committed
571
572
573
574
575
576
577
578
    if (argc && (argv[0].a_type == A_SYMBOL) &&
        !strcmp(argv[0].a_w.w_symbol->s_name, "-symbol"))
    {
        x->x_issymbol = 1;
        argc--;
        argv++;
    }
    else x->x_issymbol = 0;
579
580
581
582
583
584
585
586
    x->x_templatesym = template_getbindsym(atom_getsymbolarg(0, argc, argv));
    if (argc < 2)
    {
        varcount = 1;
        varvec = &at;
        SETSYMBOL(&at, &s_);
    }
    else varcount = argc - 1, varvec = argv + 1;
Miller Puckette's avatar
Miller Puckette committed
587
    x->x_variables
588
589
590
        = (t_setvariable *)getbytes(varcount * sizeof (*x->x_variables));
    x->x_nin = varcount;
    for (i = 0, sp = x->x_variables; i < varcount; i++, sp++)
Miller Puckette's avatar
Miller Puckette committed
591
    {
592
        sp->gv_sym = atom_getsymbolarg(i, varcount, varvec);
593
594
595
596
597
        if (x->x_issymbol)
            sp->gv_w.w_symbol = &s_;
        else sp->gv_w.w_float = 0;
        if (i)
        {
Miller Puckette's avatar
Miller Puckette committed
598
            if (x->x_issymbol)
599
600
                symbolinlet_new(&x->x_obj, &sp->gv_w.w_symbol);
            else floatinlet_new(&x->x_obj, &sp->gv_w.w_float);
Miller Puckette's avatar
Miller Puckette committed
601
602
603
604
605
606
607
        }
    }
    pointerinlet_new(&x->x_obj, &x->x_gp);
    gpointer_init(&x->x_gp);
    return (x);
}

608
609
610
611
612
613
static void set_set(t_set *x, t_symbol *templatesym, t_symbol *field)
{
    if (x->x_nin != 1)
        pd_error(x, "set: cannot set multiple fields.");
    else
    {
614
       x->x_templatesym = template_getbindsym(templatesym); 
615
616
617
618
619
620
621
622
       x->x_variables->gv_sym = field;
       if (x->x_issymbol)
           x->x_variables->gv_w.w_symbol = &s_;
       else
           x->x_variables->gv_w.w_float = 0;
    }
}

623
extern void scalar_configure(t_scalar *x, t_glist *owner);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
624

Miller Puckette's avatar
Miller Puckette committed
625
626
627
static void set_bang(t_set *x)
{
    int nitems = x->x_nin, i;
628
629
    t_symbol *templatesym;
    t_template *template;
Miller Puckette's avatar
Miller Puckette committed
630
631
632
633
634
635
636
637
638
    t_setvariable *vp;
    t_gpointer *gp = &x->x_gp;
    t_gstub *gs = gp->gp_stub;
    t_word *vec;
    if (!gpointer_check(gp, 0))
    {
        pd_error(x, "set: empty pointer");
        return;
    }
639
640
641
642
643
644
645
646
647
648
649
    if (*x->x_templatesym->s_name)
    {
        if ((templatesym = x->x_templatesym) != gpointer_gettemplatesym(gp))
        {
            pd_error(x, "set %s: got wrong template (%s)",
                templatesym->s_name, gpointer_gettemplatesym(gp)->s_name);
            return;
        }
    }
    else templatesym = gpointer_gettemplatesym(gp);
    if (!(template = template_findbyname(templatesym)))
Miller Puckette's avatar
Miller Puckette committed
650
    {
651
        pd_error(x, "set: couldn't find template %s", templatesym->s_name);
Miller Puckette's avatar
Miller Puckette committed
652
653
654
655
656
657
        return;
    }
    if (!nitems)
        return;
    if (gs->gs_which == GP_ARRAY)
        vec = gp->gp_un.gp_w;
658
    else vec = ((t_scalar *)(gp->gp_un.gp_gobj))->sc_vec;
Miller Puckette's avatar
Miller Puckette committed
659
660
661
662
663
664
    if (x->x_issymbol)
        for (i = 0, vp = x->x_variables; i < nitems; i++, vp++)
            template_setsymbol(template, vp->gv_sym, vec, vp->gv_w.w_symbol, 1);
    else for (i = 0, vp = x->x_variables; i < nitems; i++, vp++)
        template_setfloat(template, vp->gv_sym, vec, vp->gv_w.w_float, 1);
    if (gs->gs_which == GP_GLIST)
Jonathan Wilkes's avatar
Jonathan Wilkes committed
665
        scalar_configure((t_scalar *)(gp->gp_un.gp_gobj), gs->gs_un.gs_glist);  
Miller Puckette's avatar
Miller Puckette committed
666
667
668
669
670
    else
    {
        t_array *owner_array = gs->gs_un.gs_array;
        while (owner_array->a_gp.gp_stub->gs_which == GP_ARRAY)
            owner_array = owner_array->a_gp.gp_stub->gs_un.gs_array;
671
        scalar_redraw((t_scalar *)(owner_array->a_gp.gp_un.gp_gobj),
Miller Puckette's avatar
Miller Puckette committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
            owner_array->a_gp.gp_stub->gs_un.gs_glist);  
    }
}

static void set_float(t_set *x, t_float f)
{
    if (x->x_nin && !x->x_issymbol)
    {
        x->x_variables[0].gv_w.w_float = f;
        set_bang(x);
    }
    else pd_error(x, "type mismatch or no field specified");
}

static void set_symbol(t_set *x, t_symbol *s)
{
    if (x->x_nin && x->x_issymbol)
    {
        x->x_variables[0].gv_w.w_symbol = s;
        set_bang(x);
    }
    else pd_error(x, "type mismatch or no field specified");
}

static void set_free(t_set *x)
{
    freebytes(x->x_variables, x->x_nin * sizeof (*x->x_variables));
    gpointer_unset(&x->x_gp);
}

static void set_setup(void)
{
    set_class = class_new(gensym("set"), (t_newmethod)set_new,
        (t_method)set_free, sizeof(t_set), 0, A_GIMME, 0);
    class_addfloat(set_class, set_float); 
    class_addsymbol(set_class, set_symbol); 
    class_addbang(set_class, set_bang); 
709
710
    class_addmethod(set_class, (t_method)set_set, gensym("set"),
        A_SYMBOL, A_SYMBOL, 0); 
Miller Puckette's avatar
Miller Puckette committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
}

/* ---------------------- elem ----------------------------- */

static t_class *elem_class;

typedef struct _elem
{
    t_object x_obj;
    t_symbol *x_templatesym;
    t_symbol *x_fieldsym;
    t_gpointer x_gp;
    t_gpointer x_gparent;
} t_elem;

static void *elem_new(t_symbol *templatesym, t_symbol *fieldsym)
{
    t_elem *x = (t_elem *)pd_new(elem_class);
729
    x->x_templatesym = template_getbindsym(templatesym);
Miller Puckette's avatar
Miller Puckette committed
730
731
732
733
734
735
736
737
    x->x_fieldsym = fieldsym;
    gpointer_init(&x->x_gp);
    gpointer_init(&x->x_gparent);
    pointerinlet_new(&x->x_obj, &x->x_gparent);
    outlet_new(&x->x_obj, &s_pointer);
    return (x);
}

738
739
static void elem_set(t_elem *x, t_symbol *templatesym, t_symbol *fieldsym)
{
740
    x->x_templatesym = template_getbindsym(templatesym);
741
742
743
    x->x_fieldsym = fieldsym;
}

Miller Puckette's avatar
Miller Puckette committed
744
745
746
static void elem_float(t_elem *x, t_float f)
{
    int indx = f, nitems, onset;
747
748
    t_symbol *templatesym, *fieldsym = x->x_fieldsym, *elemtemplatesym;
    t_template *template;
Miller Puckette's avatar
Miller Puckette committed
749
750
751
752
753
754
755
756
757
758
759
    t_template *elemtemplate;
    t_gpointer *gparent = &x->x_gparent;
    t_word *w;
    t_array *array;
    int elemsize, type;
    
    if (!gpointer_check(gparent, 0))
    {
        pd_error(x, "element: empty pointer");
        return;
    }
760
761
762
763
764
765
766
767
768
769
770
771
    if (*x->x_templatesym->s_name)
    {
        if ((templatesym = x->x_templatesym) !=
            gpointer_gettemplatesym(gparent))
        {
            pd_error(x, "elem %s: got wrong template (%s)",
                templatesym->s_name, gpointer_gettemplatesym(gparent)->s_name);
            return;
        }
    }
    else templatesym = gpointer_gettemplatesym(gparent);
    if (!(template = template_findbyname(templatesym)))
Miller Puckette's avatar
Miller Puckette committed
772
    {
773
        pd_error(x, "elem: couldn't find template %s", templatesym->s_name);
Miller Puckette's avatar
Miller Puckette committed
774
775
776
        return;
    }
    if (gparent->gp_stub->gs_which == GP_ARRAY) w = gparent->gp_un.gp_w;
777
    else w = ((t_scalar *)(gparent->gp_un.gp_gobj))->sc_vec;
Miller Puckette's avatar
Miller Puckette committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
    if (!template)
    {
        pd_error(x, "element: couldn't find template %s", templatesym->s_name);
        return;
    }
    if (!template_find_field(template, fieldsym,
        &onset, &type, &elemtemplatesym))
    {
        pd_error(x, "element: couldn't find array field %s", fieldsym->s_name);
        return;
    }
    if (type != DT_ARRAY)
    {
        pd_error(x, "element: field %s not of type array", fieldsym->s_name);
        return;
    }
    if (!(elemtemplate = template_findbyname(elemtemplatesym)))
    {
        pd_error(x, "element: couldn't find field template %s",
            elemtemplatesym->s_name);
        return;
    }

    elemsize = elemtemplate->t_n * sizeof(t_word);

    array = *(t_array **)(((char *)w) + onset);

    nitems = array->a_n;
    if (indx < 0) indx = 0;
    if (indx >= nitems) indx = nitems-1;

    gpointer_setarray(&x->x_gp, array, 
        (t_word *)((char *)(array->a_vec) + indx * elemsize));
    outlet_pointer(x->x_obj.ob_outlet, &x->x_gp);
}

static void elem_free(t_elem *x, t_gpointer *gp)
{
    gpointer_unset(&x->x_gp);
    gpointer_unset(&x->x_gparent);
}

static void elem_setup(void)
{
    elem_class = class_new(gensym("element"), (t_newmethod)elem_new,
        (t_method)elem_free, sizeof(t_elem), 0, A_DEFSYM, A_DEFSYM, 0);
    class_addfloat(elem_class, elem_float); 
825
826
    class_addmethod(elem_class, (t_method)elem_set, gensym("set"),
        A_SYMBOL, A_SYMBOL, 0); 
Miller Puckette's avatar
Miller Puckette committed
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
}

/* ---------------------- getsize ----------------------------- */

static t_class *getsize_class;

typedef struct _getsize
{
    t_object x_obj;
    t_symbol *x_templatesym;
    t_symbol *x_fieldsym;
} t_getsize;

static void *getsize_new(t_symbol *templatesym, t_symbol *fieldsym)
{
    t_getsize *x = (t_getsize *)pd_new(getsize_class);
843
    x->x_templatesym = template_getbindsym(templatesym);
Miller Puckette's avatar
Miller Puckette committed
844
845
846
847
848
    x->x_fieldsym = fieldsym;
    outlet_new(&x->x_obj, &s_float);
    return (x);
}

849
850
static void getsize_set(t_getsize *x, t_symbol *templatesym, t_symbol *fieldsym)
{
851
    x->x_templatesym = template_getbindsym(templatesym);
852
853
854
    x->x_fieldsym = fieldsym;
}

Miller Puckette's avatar
Miller Puckette committed
855
856
static void getsize_pointer(t_getsize *x, t_gpointer *gp)
{
857
858
859
    int nitems, onset, type;
    t_symbol *templatesym, *fieldsym = x->x_fieldsym, *elemtemplatesym;
    t_template *template;
Miller Puckette's avatar
Miller Puckette committed
860
861
    t_word *w;
    t_array *array;
862
    int elemsize;
Miller Puckette's avatar
Miller Puckette committed
863
    t_gstub *gs = gp->gp_stub;
864
    if (!gpointer_check(gp, 0))
Miller Puckette's avatar
Miller Puckette committed
865
    {
866
        pd_error(x, "get: stale or empty pointer");
Miller Puckette's avatar
Miller Puckette committed
867
868
        return;
    }
869
    if (*x->x_templatesym->s_name)
Miller Puckette's avatar
Miller Puckette committed
870
    {
871
872
873
874
875
876
877
        if ((templatesym = x->x_templatesym) !=
            gpointer_gettemplatesym(gp))
        {
            pd_error(x, "elem %s: got wrong template (%s)",
                templatesym->s_name, gpointer_gettemplatesym(gp)->s_name);
            return;
        }
Miller Puckette's avatar
Miller Puckette committed
878
    }
879
880
    else templatesym = gpointer_gettemplatesym(gp);
    if (!(template = template_findbyname(templatesym)))
Miller Puckette's avatar
Miller Puckette committed
881
    {
882
        pd_error(x, "elem: couldn't find template %s", templatesym->s_name);
Miller Puckette's avatar
Miller Puckette committed
883
884
        return;
    }
885
886
    if (!template_find_field(template, fieldsym,
        &onset, &type, &elemtemplatesym))
Miller Puckette's avatar
Miller Puckette committed
887
    {
888
        pd_error(x, "getsize: couldn't find array field %s", fieldsym->s_name);
Miller Puckette's avatar
Miller Puckette committed
889
890
        return;
    }
891
    if (type != DT_ARRAY)
Miller Puckette's avatar
Miller Puckette committed
892
    {
893
        pd_error(x, "getsize: field %s not of type array", fieldsym->s_name);
Miller Puckette's avatar
Miller Puckette committed
894
895
896
        return;
    }
    if (gs->gs_which == GP_ARRAY) w = gp->gp_un.gp_w;
897
    else w = ((t_scalar *)(gp->gp_un.gp_gobj))->sc_vec;
Miller Puckette's avatar
Miller Puckette committed
898
899
    
    array = *(t_array **)(((char *)w) + onset);
900
    outlet_float(x->x_obj.ob_outlet, (t_float)(array->a_n));
Miller Puckette's avatar
Miller Puckette committed
901
902
903
904
905
906
907
}

static void getsize_setup(void)
{
    getsize_class = class_new(gensym("getsize"), (t_newmethod)getsize_new, 0,
        sizeof(t_getsize), 0, A_DEFSYM, A_DEFSYM, 0);
    class_addpointer(getsize_class, getsize_pointer); 
908
909
    class_addmethod(getsize_class, (t_method)getsize_set, gensym("set"),
        A_SYMBOL, A_SYMBOL, 0); 
Miller Puckette's avatar
Miller Puckette committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
}

/* ---------------------- setsize ----------------------------- */

static t_class *setsize_class;

typedef struct _setsize
{
    t_object x_obj;
    t_symbol *x_templatesym;
    t_symbol *x_fieldsym;
    t_gpointer x_gp;
} t_setsize;

static void *setsize_new(t_symbol *templatesym, t_symbol *fieldsym,
    t_floatarg newsize)
{
    t_setsize *x = (t_setsize *)pd_new(setsize_class);
928
    x->x_templatesym = template_getbindsym(templatesym);
Miller Puckette's avatar
Miller Puckette committed
929
930
931
932
933
934
935
    x->x_fieldsym = fieldsym;
    gpointer_init(&x->x_gp);
    
    pointerinlet_new(&x->x_obj, &x->x_gp);
    return (x);
}

936
937
static void setsize_set(t_setsize *x, t_symbol *templatesym, t_symbol *fieldsym)
{
938
    x->x_templatesym = template_getbindsym(templatesym);
939
940
941
    x->x_fieldsym = fieldsym;
}

Miller Puckette's avatar
Miller Puckette committed
942
943
944
static void setsize_float(t_setsize *x, t_float f)
{
    int nitems, onset, type;
945
946
    t_symbol *templatesym, *fieldsym = x->x_fieldsym, *elemtemplatesym;
    t_template *template;
Miller Puckette's avatar
Miller Puckette committed
947
948
    t_template *elemtemplate;
    t_word *w;
949
    t_atom *at;
Miller Puckette's avatar
Miller Puckette committed
950
951
952
953
954
955
956
957
958
959
    t_array *array;
    int elemsize;
    int newsize = f;
    t_gpointer *gp = &x->x_gp;
    t_gstub *gs = gp->gp_stub;
    if (!gpointer_check(&x->x_gp, 0))
    {
        pd_error(x, "setsize: empty pointer");
        return;
    }
960
    if (*x->x_templatesym->s_name)
Miller Puckette's avatar
Miller Puckette committed
961
    {
962
963
964
965
966
967
968
        if ((templatesym = x->x_templatesym) !=
            gpointer_gettemplatesym(gp))
        {
            pd_error(x, "elem %s: got wrong template (%s)",
                templatesym->s_name, gpointer_gettemplatesym(gp)->s_name);
            return;
        }
Miller Puckette's avatar
Miller Puckette committed
969
    }
970
971
    else templatesym = gpointer_gettemplatesym(gp);
    if (!(template = template_findbyname(templatesym)))
Miller Puckette's avatar
Miller Puckette committed
972
    {
973
        pd_error(x, "elem: couldn't find template %s", templatesym->s_name);
Miller Puckette's avatar
Miller Puckette committed
974
975
976
977
978
        return;
    }
    if (!template_find_field(template, fieldsym,
        &onset, &type, &elemtemplatesym))
    {
979
        pd_error(x, "setsize: couldn't find array field %s", fieldsym->s_name);
Miller Puckette's avatar
Miller Puckette committed
980
981
982
983
984
985
986
        return;
    }
    if (type != DT_ARRAY)
    {
        pd_error(x,"setsize: field %s not of type array", fieldsym->s_name);
        return;
    }
987
988
    if (gs->gs_which == GP_ARRAY) w = gp->gp_un.gp_w;
    else w = ((t_scalar *)(gp->gp_un.gp_gobj))->sc_vec;
Miller Puckette's avatar
Miller Puckette committed
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015

    if (!(elemtemplate = template_findbyname(elemtemplatesym)))
    {
        pd_error(x,"element: couldn't find field template %s",
            elemtemplatesym->s_name);
        return;
    }

    elemsize = elemtemplate->t_n * sizeof(t_word);

    array = *(t_array **)(((char *)w) + onset);

    if (elemsize != array->a_elemsize) bug("setsize_gpointer");

    nitems = array->a_n;
    if (newsize < 1) newsize = 1;
    if (newsize == nitems) return;
    
        /* erase the array before resizing it.  If we belong to a
        scalar it's easy, but if we belong to an element of another
        array we have to search back until we get to a scalar to erase.
        When graphics updates become queueable this may fall apart... */


    if (gs->gs_which == GP_GLIST)
    {
        if (glist_isvisible(gs->gs_un.gs_glist))
1016
            gobj_vis(gp->gp_un.gp_gobj, gs->gs_un.gs_glist, 0);  
Miller Puckette's avatar
Miller Puckette committed
1017
1018
1019
1020
1021
1022
1023
    }
    else
    {
        t_array *owner_array = gs->gs_un.gs_array;
        while (owner_array->a_gp.gp_stub->gs_which == GP_ARRAY)
            owner_array = owner_array->a_gp.gp_stub->gs_un.gs_array;
        if (glist_isvisible(owner_array->a_gp.gp_stub->gs_un.gs_glist))
1024
            gobj_vis(owner_array->a_gp.gp_un.gp_gobj,
Miller Puckette's avatar
Miller Puckette committed
1025
1026
                owner_array->a_gp.gp_stub->gs_un.gs_glist, 0);  
    }
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
        /* if shrinking, free the scalars that will disappear */
    if (newsize < nitems)
    {
        char *elem;
        int count;
        for (elem = ((char *)array->a_vec) + newsize * elemsize,
            count = nitems = newsize; count--; elem += elemsize)
                word_free((t_word *)elem, elemtemplate);
    } 
        /* resize the array */
Miller Puckette's avatar
Miller Puckette committed
1037
1038
1039
    array->a_vec = (char *)resizebytes(array->a_vec,
        elemsize * nitems, elemsize * newsize);
    array->a_n = newsize;
1040
        /* if growing, initialize new scalars */
Miller Puckette's avatar
Miller Puckette committed
1041
1042
    if (newsize > nitems)
    {
1043
1044
1045
1046
1047
        char *elem;
        int count;
        for (elem = ((char *)array->a_vec) + nitems * elemsize,
            count = newsize - nitems; count--; elem += elemsize)
                word_init((t_word *)elem, elemtemplate, gp);
Miller Puckette's avatar
Miller Puckette committed
1048
1049
1050
1051
1052
1053
    }

    /* redraw again. */
    if (gs->gs_which == GP_GLIST)
    {
        if (glist_isvisible(gs->gs_un.gs_glist))
1054
            gobj_vis(gp->gp_un.gp_gobj, gs->gs_un.gs_glist, 1);  
Miller Puckette's avatar
Miller Puckette committed
1055
1056
1057
1058
1059
1060
1061
    }
    else
    {
        t_array *owner_array = gs->gs_un.gs_array;
        while (owner_array->a_gp.gp_stub->gs_which == GP_ARRAY)
            owner_array = owner_array->a_gp.gp_stub->gs_un.gs_array;
        if (glist_isvisible(owner_array->a_gp.gp_stub->gs_un.gs_glist))
1062
            gobj_vis(owner_array->a_gp.gp_un.gp_gobj,
Miller Puckette's avatar
Miller Puckette committed
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
                owner_array->a_gp.gp_stub->gs_un.gs_glist, 1);  
    }
}


static void setsize_free(t_setsize *x)
{
    gpointer_unset(&x->x_gp);
}

static void setsize_setup(void)
{
    setsize_class = class_new(gensym("setsize"), (t_newmethod)setsize_new,
        (t_method)setsize_free, sizeof(t_setsize), 0,
        A_DEFSYM, A_DEFSYM, A_DEFFLOAT, 0);
    class_addfloat(setsize_class, setsize_float);
1079
1080
1081
    class_addmethod(setsize_class, (t_method)setsize_set, gensym("set"),
        A_SYMBOL, A_SYMBOL, 0); 

Miller Puckette's avatar
Miller Puckette committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
}

/* ---------------------- append ----------------------------- */

static t_class *append_class;

typedef struct _appendvariable
{
    t_symbol *gv_sym;
    t_float gv_f;
} t_appendvariable;

typedef struct _append
{
    t_object x_obj;
    t_gpointer x_gp;
    t_symbol *x_templatesym;
    int x_nin;
    t_appendvariable *x_variables;
} t_append;

static void *append_new(t_symbol *why, int argc, t_atom *argv)
{
    t_append *x = (t_append *)pd_new(append_class);
1106
1107
    int varcount, i;
    t_atom at, *varvec;
Miller Puckette's avatar
Miller Puckette committed
1108
    t_appendvariable *sp;
1109
1110
1111
1112
1113
1114
1115
1116
1117

    x->x_templatesym = template_getbindsym(atom_getsymbolarg(0, argc, argv));
    if (argc < 2)
    {
        varcount = 1;
        varvec = &at;
        SETSYMBOL(&at, &s_);
    }
    else varcount = argc - 1, varvec = argv + 1;
Miller Puckette's avatar
Miller Puckette committed
1118
    x->x_variables
1119
1120
        = (t_appendvariable *)getbytes(varcount * sizeof (*x->x_variables));
    x->x_nin = varcount;
1121
    for (i = 0, sp = x->x_variables; i < varcount; i++, sp++)
Miller Puckette's avatar
Miller Puckette committed
1122
    {
1123
        sp->gv_sym = atom_getsymbolarg(i, varcount, varvec);
1124
1125
        sp->gv_f = 0;
        if (i) floatinlet_new(&x->x_obj, &sp->gv_f);
Miller Puckette's avatar
Miller Puckette committed
1126
1127
1128
1129
1130
1131
1132
    }
    pointerinlet_new(&x->x_obj, &x->x_gp);
    outlet_new(&x->x_obj, &s_pointer);
    gpointer_init(&x->x_gp);
    return (x);
}

1133
1134
1135
1136
1137
1138
1139
1140
static void append_set(t_append *x, t_symbol *templatesym, t_symbol *field)
{
    if (x->x_nin != 1)
    {
        pd_error(x, "set: cannot set multiple fields.");
    }
    else
    {
1141
       x->x_templatesym = template_getbindsym(templatesym); 
1142
1143
1144
1145
1146
       x->x_variables->gv_sym = field;
       x->x_variables->gv_f = 0;
    }
}

Miller Puckette's avatar
Miller Puckette committed
1147
1148
1149
1150
static void append_float(t_append *x, t_float f)
{
    int nitems = x->x_nin, i;
    t_symbol *templatesym = x->x_templatesym;
1151
    t_template *template;
Miller Puckette's avatar
Miller Puckette committed
1152
1153
1154
1155
1156
1157
    t_appendvariable *vp;
    t_gpointer *gp = &x->x_gp;
    t_gstub *gs = gp->gp_stub;
    t_word *vec;
    t_scalar *sc, *oldsc;
    t_glist *glist;
1158
1159
1160
1161
1162
1163
1164

    if (!templatesym->s_name)
    {
        pd_error(x, "append: no template supplied");
        return;
    }
    template = template_findbyname(templatesym);
Miller Puckette's avatar
Miller Puckette committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
    if (!template)
    {
        pd_error(x, "append: couldn't find template %s", templatesym->s_name);
        return;
    }
    if (!gs)
    {
        pd_error(x, "append: no current pointer");
        return;
    }
    if (gs->gs_which != GP_GLIST)
    {
        pd_error(x, "append: lists only, not arrays");
        return;
    }
    glist = gs->gs_un.gs_glist;
    if (glist->gl_valid != gp->gp_valid)
    {
        pd_error(x, "append: stale pointer");
        return;
    }
    if (!nitems) return;
    x->x_variables[0].gv_f = f;

    sc = scalar_new(glist, templatesym);
    if (!sc)
    {
        pd_error(x, "%s: couldn't create scalar", templatesym->s_name);
        return;
    }
1195
    oldsc = (t_scalar *)(gp->gp_un.gp_gobj);
Miller Puckette's avatar
Miller Puckette committed
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
    
    if (oldsc)
    {
        sc->sc_gobj.g_next = oldsc->sc_gobj.g_next;
        oldsc->sc_gobj.g_next = &sc->sc_gobj;
    }
    else
    {
        sc->sc_gobj.g_next = glist->gl_list;
        glist->gl_list = &sc->sc_gobj;
    }

1208
    gp->gp_un.gp_gobj = (t_gobj *)sc;
Miller Puckette's avatar
Miller Puckette committed
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
    vec = sc->sc_vec;
    for (i = 0, vp = x->x_variables; i < nitems; i++, vp++)
    {
        template_setfloat(template, vp->gv_sym, vec, vp->gv_f, 1);
    }
 
    if (glist_isvisible(glist_getcanvas(glist)))
        gobj_vis(&sc->sc_gobj, glist, 1);
    /*  scalar_redraw(sc, glist);  ... have to do 'vis' instead here because
    redraw assumes we're already visible??? ... */

    outlet_pointer(x->x_obj.ob_outlet, gp);
}

static void append_free(t_append *x)
{
    freebytes(x->x_variables, x->x_nin * sizeof (*x->x_variables));
    gpointer_unset(&x->x_gp);
}

static void append_setup(void)
{
    append_class = class_new(gensym("append"), (t_newmethod)append_new,
        (t_method)append_free, sizeof(t_append), 0, A_GIMME, 0);
    class_addfloat(append_class, append_float); 
1234
1235
    class_addmethod(append_class, (t_method)append_set, gensym("set"),
        A_SYMBOL, A_SYMBOL, 0); 
Miller Puckette's avatar
Miller Puckette committed
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
}

/* ---------------------- sublist ----------------------------- */

static t_class *sublist_class;

typedef struct _sublist
{
    t_object x_obj;
    t_symbol *x_templatesym;
    t_symbol *x_fieldsym;
    t_gpointer x_gp;
} t_sublist;

static void *sublist_new(t_symbol *templatesym, t_symbol *fieldsym)
{
    t_sublist *x = (t_sublist *)pd_new(sublist_class);
    x->x_templatesym = canvas_makebindsym(templatesym);
    x->x_fieldsym = fieldsym;
    gpointer_init(&x->x_gp);
    outlet_new(&x->x_obj, &s_pointer);
    return (x);
}

static void sublist_pointer(t_sublist *x, t_gpointer *gp)
{
    t_symbol *templatesym = x->x_templatesym, *dummy;
    t_template *template = template_findbyname(templatesym);
    t_gstub *gs = gp->gp_stub;
    int onset, type;
    t_word *w;

    if (!template)
    {
        pd_error(x, "sublist: couldn't find template %s", templatesym->s_name);
        return;
    }
    if (!gpointer_check(gp, 0))
    {
        pd_error(x, "get: stale or empty pointer");
        return;
    }
    if (!template_find_field(template, x->x_fieldsym,
        &onset, &type, &dummy))
    {
        pd_error(x, "sublist: couldn't find field %s", x->x_fieldsym->s_name);
        return;
    }
    if (type != DT_LIST)
    {
        pd_error(x, "sublist: field %s not of type list", x->x_fieldsym->s_name);
        return;
    }
    if (gs->gs_which == GP_ARRAY) w = gp->gp_un.gp_w;
1290
    else w = ((t_scalar *)(gp->gp_un.gp_gobj))->sc_vec;
Miller Puckette's avatar
Miller Puckette committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
    
    gpointer_setglist(&x->x_gp, *(t_glist **)(((char *)w) + onset), 0);

    outlet_pointer(x->x_obj.ob_outlet, &x->x_gp);
}

static void sublist_free(t_sublist *x, t_gpointer *gp)
{
    gpointer_unset(&x->x_gp);
}

static void sublist_setup(void)
{
    sublist_class = class_new(gensym("sublist"), (t_newmethod)sublist_new,
        (t_method)sublist_free, sizeof(t_sublist), 0, A_DEFSYM, A_DEFSYM, 0);
    class_addpointer(sublist_class, sublist_pointer); 
}

1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
/* ---------------------- field ----------------------------- */

static t_class *field_class;

typedef struct _field
{
    t_object x_obj;
    t_symbol *x_s;
    t_template *x_template;
    t_canvas *x_canvas;
} t_field;

static void *field_new(t_symbol *s)
{
    t_field *x = (t_field *)pd_new(field_class);
    x->x_s = s;
1325
    x->x_canvas = canvas_getrootfor(canvas_getcurrent());
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1326
    x->x_template = NULL;
1327
1328
1329
1330
1331
1332
1333
1334
1335
    outlet_new(&x->x_obj, &s_list);
    return (x);
}

static void field_set(t_field *x, t_symbol *s)
{
    x->x_s = s;
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
    /* Contortions to get and possibly cache template after
       the first lookup. We can't get it in field_new because
       word_init only sets the template after we've been created. */
static t_template *field_gettemplate(t_field *x)
{
    if (x->x_template)
        return x->x_template;
    else
    {
        if (x->x_canvas->gl_templatesym)
        {
            x->x_template = template_findbyname(x->x_canvas->gl_templatesym);
            return x->x_template;
        }
        else
            return 0;
    }
}

1355
1356
static void field_bang(t_field *x)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
    t_template *template = field_gettemplate(x);
    if (template)
    {
        t_word *vec = x->x_canvas->gl_vec;
        t_symbol *fieldsym = x->x_s;
        int onset, type;
        t_symbol *arraytype;
        if (template_find_field(template, fieldsym, &onset, &type, &arraytype))
        {
            if (type == DT_FLOAT)
                outlet_float(x->x_obj.ob_outlet,
                    *(t_float *)(((char *)vec) + onset));
            else if (type == DT_SYMBOL)
                outlet_symbol(x->x_obj.ob_outlet,
                    *(t_symbol **)(((char *)vec)+ onset));
            else pd_error(x, "field: %s.%s is not a number or symbol",
                    template->t_sym->s_name, fieldsym->s_name);
        }
        else pd_error(x, "field: %s.%s: no such field",
            template->t_sym->s_name, fieldsym->s_name);
    }
    else /* send a bang to signal that we're not in a scalar */
        outlet_bang(x->x_obj.ob_outlet);
1380
1381
1382
1383
}

static void field_setvalue(t_field *x, t_symbol *s, int argc, t_atom *argv)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1384
1385
    t_template *template = field_gettemplate(x);
    if (!template) return;
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
    t_word *vec = x->x_canvas->gl_vec;
    t_gpointer *gp = &x->x_canvas->gl_gp;
    t_gstub *gs = gp->gp_stub;
    int onset, type;
    t_symbol *arraytype, *fieldsym = x->x_s;
    if (argc)
    {
        if (argv->a_type == A_FLOAT)
            template_setfloat(template, fieldsym, vec,
                atom_getfloatarg(0, argc, argv), 1);
        else if (argv->a_type == A_SYMBOL)
            template_setsymbol(template, fieldsym, vec,
                atom_getsymbolarg(0, argc, argv), 1);
        else pd_error(x, "field: incoming value must be float or symbol");
    }
    if (gp->gp_stub->gs_which == GP_GLIST)
        scalar_configure((t_scalar *)(gp->gp_un.gp_gobj), gs->gs_un.gs_glist);  
    else
    {
        t_array *owner_array = gs->gs_un.gs_array;
        while (owner_array->a_gp.gp_stub->gs_which == GP_ARRAY)
            owner_array = owner_array->a_gp.gp_stub->gs_un.gs_array;
        scalar_redraw((t_scalar *)(owner_array->a_gp.gp_un.gp_gobj),
            owner_array->a_gp.gp_stub->gs_un.gs_glist);  
    }
}

static void field_symbol(t_field *x, t_symbol *s)
{
    t_atom at[1];
    SETSYMBOL(at, s);
    field_setvalue(x, gensym("symbol"), 1, at);
}

static void field_float(t_field *x, t_floatarg f)
{
    t_atom at[1];
    SETFLOAT(at, f);
    field_setvalue(x, gensym("float"), 1, at);
}

static void field_setup(void)
{
    field_class = class_new(gensym("field"), (t_newmethod)field_new,
        0, sizeof(t_field), 0, A_DEFSYM, 0);
    class_addfloat(field_class, field_float); 
    class_addsymbol(field_class, field_symbol); 
    class_addbang(field_class, field_bang); 
    class_addmethod(field_class, (t_method)field_set, gensym("set"),
        A_SYMBOL, 0); 
}

Miller Puckette's avatar
Miller Puckette committed
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
/* ----------------- setup function ------------------- */

void g_traversal_setup(void)
{
    ptrobj_setup();
    get_setup();
    set_setup();
    elem_setup();
    getsize_setup();
    setsize_setup();
    append_setup();
    sublist_setup();
1450
    field_setup();
Miller Puckette's avatar
Miller Puckette committed
1451
}