aboutsummaryrefslogtreecommitdiff
path: root/shared/toxy/plusbob.c
blob: 350b3c80800f870d2e24ca896d1bff300d53cf86 (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
/* Copyright (c) 2003-2005 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include <string.h>
#include "m_pd.h"
#include "common/loud.h"
#include "plusbob.h"

#ifdef KRZYSZCZ
//#define PLUSBOB_DEBUG
#endif

/* The main failure of the current implementation is when a foreign object
   stores a faked symbol beyond lifetime of a wrappee.  There is no obvious
   way of protecting against stale pointers, other than leaking small
   portions of memory (four words) with every new faked symbol.  In case of
   plustot, this is not a very big deal, since for each [+tot] object the
   number of wrapped tcl objects is small and constant.

   Another failure is when a foreign object binds something to a faked
   symbol (for example, when a faked symbol is passed to an array's rename
   method).  This should not happen in usual contexts, and even if it does,
   it will unlikely cause any real harm.

   LATER let there be a choice of using either fake-symbols, or gpointers.
   The gpointer layout would be such:  gs_un points to a plusbob-like
   structure (without the bob_stub field), a unique integer code has to be
   reserved for gs_which, the fields gp_un and gp_valid are ignored.
   Using bob_refcount instead of gs_refcount is likely to simplify code. */

typedef struct _plusstub
{
    t_symbol    sb_tag;  /* common value for all bob types */
    t_plusbob  *sb_bob;
} t_plusstub;

/* Currently, objects of all +bob types are tagged with the same name: */
static char plustag_name[] = "+bob";

static void plustag_init(t_symbol *tag)
{
    tag->s_name = plustag_name;
    tag->s_thing = 0;
    tag->s_next = 0;
}

/* returns tagged +bob if valid, null otherwise (silent if caller is empty) */
t_plusbob *plustag_isvalid(t_symbol *tag, t_pd *caller)
{
    if (tag->s_name == plustag_name)
	return (((t_plusstub *)tag)->sb_bob);
    else if (caller)
    {
	if (strcmp(tag->s_name, plustag_name))
	    loud_error((caller == PLUSBOB_OWNER ? 0 : caller),
 "does not understand '%s' (check object connections)", tag->s_name);
	else
	    loud_error((caller == PLUSBOB_OWNER ? 0 : caller), "confused...");
    }
    return (0);
}

static t_plusstub *plusstub_create(t_plusbob *bob)
{
    t_plusstub *stub = getbytes(sizeof(*stub));
    plustag_init(&stub->sb_tag);
    stub->sb_bob = bob;
    return (stub);
}

/* +bob is an object tossed around, a bobbing object.  Currently, this is
   a wrapping for Tcl_Interp, Tcl_Obj, or a tcl variable, but the +bob
   interface is abstract enough to be suitable for other types of objects.
   The t_plusbob is kind of a virtual base. */

struct _plustype
{
    t_plustype   *tp_base;  /* empty, if directly derived from t_plusbob */
    t_symbol     *tp_name;
    size_t        tp_size;
    /* constructor is to be called explicitly, from derived constructors,
       or from a public wrapper. */
    t_plustypefn  tp_deletefn;  /* destructor */
    t_plustypefn  tp_preservefn;
    t_plustypefn  tp_releasefn;
    t_plustypefn  tp_attachfn;
};

static t_plustype *plustype_default = 0;

t_plustype *plustype_new(t_plustype *base, t_symbol *name, size_t sz,
			 t_plustypefn deletefn,
			 t_plustypefn preservefn, t_plustypefn releasefn,
			 t_plustypefn attachfn)
{
    t_plustype *tp = getbytes(sizeof(*tp));
    tp->tp_base = base;
    tp->tp_name = name;
    tp->tp_size = sz;
    tp->tp_deletefn = deletefn;
    tp->tp_preservefn = preservefn;
    tp->tp_releasefn = releasefn;
    tp->tp_attachfn = attachfn;
    return (tp);
}

static void plusbob_doattach(t_plusbob *bob, t_plusbob *parent)
{
    if (bob->bob_parent = parent)
    {
	/* become the youngest child: */
	bob->bob_prev = 0;
	if (bob->bob_next = parent->bob_children)
	{
	    if (parent->bob_children->bob_prev)
		loudbug_bug("plusbob_doattach 1");
	    parent->bob_children->bob_prev = bob;
	}
	parent->bob_children = bob;
    }
    else loudbug_bug("plusbob_doattach 2");
}

static void plusbob_dodetach(t_plusbob *bob)
{
    if (bob->bob_parent)
    {
	if (bob->bob_prev)
	{
	    if (bob == bob->bob_parent->bob_children)
		loudbug_bug("plusbob_dodetach 1");
	    bob->bob_prev->bob_next = bob->bob_next;
	}
	if (bob->bob_next)
	    bob->bob_next->bob_prev = bob->bob_prev;
	if (bob == bob->bob_parent->bob_children)
	    bob->bob_parent->bob_children = bob->bob_next;
    }
    else loudbug_bug("plusbob_dodetach 2");
}

/* To be called from derived constructors.
   Preserving is caller's responsibility. */
t_plusbob *plusbob_create(t_plustype *tp, t_plusbob *parent)
{
    t_plusbob *bob;
    if (!tp)
    {
	if (!plustype_default)
	    plustype_default = plustype_new(0, 0, sizeof(t_plusbob),
					    0, 0, 0, 0);
	tp = plustype_default;
    }
    if (bob = getbytes(tp->tp_size))
    {
	bob->bob_stub = (t_symbol *)plusstub_create(bob);
	bob->bob_type = tp;
	while (tp->tp_base) tp = tp->tp_base;
	bob->bob_root = tp;
	bob->bob_owner = 0;
	bob->bob_refcount = 0;
	bob->bob_dorefcount = 1;
	bob->bob_children = 0;
	if (parent)
	    plusbob_doattach(bob, parent);
	else
	    bob->bob_parent = 0;
    }
    return (bob);
}

/* Should never be called, but from plusbob_release().
   Calling from a derived destructor is illegal. */
static void plusbob_free(t_plusbob *bob)
{
    t_plustype *tp;
    if (bob->bob_parent)
	plusbob_dodetach(bob);
    for (tp = bob->bob_type; tp; tp = tp->tp_base)
	if (tp->tp_deletefn) (*tp->tp_deletefn)(bob);
    freebytes(bob, (bob->bob_type ? bob->bob_type->tp_size : sizeof(*bob)));
    /* the stub remains... */
}

void plusbob_preserve(t_plusbob *bob)
{
    if (bob->bob_dorefcount)
    {
	t_plustype *tp;
	for (tp = bob->bob_type; tp; tp = tp->tp_base)
	    if (tp->tp_preservefn) (*tp->tp_preservefn)(bob);
	bob->bob_refcount++;
    }
}

void plusbob_release(t_plusbob *bob)
{
    if (bob->bob_dorefcount)
    {
	t_plustype *tp;
	for (tp = bob->bob_type; tp; tp = tp->tp_base)
	    if (tp->tp_releasefn) (*tp->tp_releasefn)(bob);
	if (--bob->bob_refcount <= 0)
	{
	    if (bob->bob_refcount == 0)
		plusbob_free(bob);
	    else
		loudbug_bug("plusbob_release");
	}
    }
}

t_plusbob *plusbob_getparent(t_plusbob *bob)
{
    return (bob->bob_parent);
}

/* To be called for redirection only.  Bobs created as orphans are a special
   case, and cannot be attached later on.  Likewise, changing non-orphan bobs
   to orphans is illegal. */
void plusbob_attach(t_plusbob *bob, t_plusbob *newparent)
{
    if (bob->bob_parent && newparent)
    {
	t_plustype *tp;
	plusbob_dodetach(bob);
	plusbob_doattach(bob, newparent);
	for (tp = bob->bob_type; tp; tp = tp->tp_base)
	    if (tp->tp_attachfn) (*tp->tp_attachfn)(bob);
    }
    else if (newparent)
	loudbug_bug("plusbob_attach 1");
    else
	loudbug_bug("plusbob_attach 2");
}

t_plusbob *plusbob_getnext(t_plusbob *bob)
{
    return (bob->bob_next);
}

t_plusbob *plusbob_getchildren(t_plusbob *bob)
{
    return (bob->bob_children);
}

/* Redirect all bobs to a replacement parent.
   Assuming replacement exists. */
void plusbob_detachchildren(t_plusbob *bob, t_plusbob *newparent)
{
    while (bob->bob_children)
	plusbob_attach(bob->bob_children, newparent);
}

void plusbob_detachownedchildren(t_plusbob *bob, t_plusbob *newparent,
				 t_pd *owner)
{
    t_plusbob *child = bob->bob_children, *next;
    while (child)
    {
	next = child->bob_next;
	if (child->bob_owner == owner)
	    plusbob_attach(child, newparent);
	child = next;
    }
}

void plusbob_setowner(t_plusbob *bob, t_pd *owner)
{
    bob->bob_owner = owner;
}

t_pd *plusbob_getowner(t_plusbob *bob)
{
    return (bob->bob_owner);
}

void outlet_plusbob(t_outlet *o, t_plusbob *bob)
{
    outlet_symbol(o, bob->bob_stub);
}

/* returns tagged +bob if valid, null otherwise (silent if caller is empty) */
t_plusbob *plustag_validtype(t_symbol *tag, t_symbol *tname, t_pd *caller)
{
    if (tag->s_name == plustag_name)
    {
	t_plusbob *bob = ((t_plusstub *)tag)->sb_bob;
	if (bob->bob_type->tp_name == tname)
	    return (bob);
	else if (caller)
	{
	    t_symbol *s = bob->bob_type->tp_name;
	    loud_error((caller == PLUSBOB_OWNER ? bob->bob_owner : caller),
		       "invalid type '%s' ('%s' expected)",
		       (s ? s->s_name : "<unknown>"),
		       (tname ? tname->s_name : "<unknown>"));
	}
    }
    else if (plustag_isvalid(tag, caller))  /* print the error there */
	loudbug_bug("plustag_validtype");
    return (0);
}

/* returns tagged +bob if valid, null otherwise (silent if caller is empty) */
t_plusbob *plustag_validroot(t_symbol *tag, t_symbol *rname, t_pd *caller)
{
    if (tag->s_name == plustag_name)
    {
	t_plusbob *bob = ((t_plusstub *)tag)->sb_bob;
	if (bob->bob_root->tp_name == rname)
	    return (bob);
	else if (caller)
	{
	    t_symbol *s = bob->bob_root->tp_name;
	    loud_error((caller == PLUSBOB_OWNER ? bob->bob_owner : caller),
		       "invalid base type '%s' ('%s' expected)",
		       (s ? s->s_name : "<unknown>"),
		       (rname ? rname->s_name : "<unknown>"));
	}
    }
    else if (plustag_isvalid(tag, caller))  /* print the error there */
	loudbug_bug("plustag_validroot");
    return (0);
}

t_symbol *plustag_typename(t_symbol *tag, int validate, t_pd *caller)
{
    if (!validate || tag->s_name == plustag_name)
	return (((t_plusstub *)tag)->sb_bob->bob_type->tp_name);
    else if (plustag_isvalid(tag, caller))  /* print the error there */
	loudbug_bug("plustag_typename");
    return (0);
}

t_symbol *plustag_rootname(t_symbol *tag, int validate, t_pd *caller)
{
    if (!validate || tag->s_name == plustag_name)
	return (((t_plusstub *)tag)->sb_bob->bob_root->tp_name);
    else if (plustag_isvalid(tag, caller))  /* print the error there */
	loudbug_bug("plustag_rootname");
    return (0);
}

/* Plusenv (aka +env) is the base for an `environment' +bob.  Environment
   encapsulates data common for a collection of +bobs.  This is the standard
   way of grouping +bobs, according to a parent/children relationship. */

static t_plustype *plusenv_type = 0;
static t_plusbob *plusenv_parent = 0;  /* the parent of all environments */

/* To be called from derived constructors (or, LATER, plusenv's provider). */
t_plusenv *plusenv_create(t_plustype *tp, t_plusbob *parent, t_symbol *id)
{
    t_plusenv *env = 0;
    if (env = (t_plusenv *)plusbob_create(tp, parent))
    {
	if (!id)
	    /* LATER design a public interface for bob_dorefcount */
	    ((t_plusbob *)env)->bob_dorefcount = 0;
	env->env_id = id;  /* LATER rethink */
    }
    return (env);
}

t_plusenv *plusenv_find(t_symbol *id, t_plusenv *defenv)
{
    if (plusenv_parent && id)
    {
	t_plusbob *bob;
	for (bob = plusenv_parent->bob_children; bob; bob = bob->bob_next)
	    if (((t_plusenv *)bob)->env_id == id)
		break;
	return ((t_plusenv *)bob);
    }
    else return (defenv);
}

t_symbol *plusenv_getid(t_plusenv *env)
{
    return (env->env_id);
}

/* Type ignored, LATER rethink. */
t_plusbob *plusenv_getparent(t_plustype *tp)
{
    if (!plusenv_parent) plusenv_parent = plusbob_create(0, 0);
    return (plusenv_parent);
}

t_plustype *plusenv_setup(void)
{
    if (!plusenv_type)
    {
	plusenv_type = plustype_new(0, gensym("+env"),
				    sizeof(t_plusenv), 0, 0, 0, 0);
    }
    return (plusenv_type);
}