I'd be nice to include this info in source comments somewhere... :)
On Aug 5, 2019, at 10:44 PM, pd-dev-request@lists.iem.at wrote:
Date: Mon, 5 Aug 2019 22:39:26 +0200 From: "Christof Ressi" <christof.ressi@gmx.at mailto:christof.ressi@gmx.at> To: "x nor" <x37v.alex@gmail.com mailto:x37v.alex@gmail.com>, pd-dev <pd-dev@lists.iem.at mailto:pd-dev@lists.iem.at> Subject: Re: [PD-dev] can externals create data structures/templates? Message-ID: trinity-77b48c6e-2e1c-4b37-9700-523e7c7d675d-1565037566616@3c-app-gmx-bs18
Content-Type: text/plain; charset=UTF-8
is there documentation for what binbufs are and how they're used?
I think you just have to read the source code in m_binbuf.c. But here's a general guide:
Binbufs are containers for atoms. Unlike Pd lists, it can contain "special" atoms like A_SEMI or A_DOLLSYM.
binbuf_text() converts a C string to a list of atoms. bufbuf_gettext() converts the atoms back to a C string.
binbuf_add() simply adds any kind of atoms to the binbuf binbuf_getnatom() returns the size binbuf_getvec() returns a pointer to the atoms.
binbuf_addbinbuf() adds the content of another binbuf (possible containing "special" atoms) but bashes everything to A_FLOAT and A_SYMBOL binbuf_restore() adds a list of atoms but restores "special" atoms from symbols, e.g. ";" -> A_SEMI, "\$0-foo" -> A_DOLLSYM, etc.
binbuf_eval() evaluates the stored binbuf by sending it as a message to the given target. You can also pass an atom list as arguments, e.g. for a dollar symbols like "$1-foo". "$0" is always substituted with the dollar zero of the current canvas (see canvas_getdollarzero, canvas_getcurrent and canvas_setcurrent). Note that a A_SEMI atom will denote the start of a new message and the following atom will be taken as the new message target (must be a symbol).
Here's an example (taken from x_text.c):
a) The patch file:
static char text_templatefile[] = "\ canvas 0 0 458 153 10;\n\ #X obj 43 31 struct text float x float y text t;\n\ ";
b) create a binbuf
binbuf_text(b, text_templatefile, strlen(text_templatefile));
c) evaluate
binbuf_eval(b, &pd_canvasmaker, 0, 0);
Here's what happens:
- "canvas 0 0 458 153 10;" goes to canvas maker -> create a new canvas, push it on the loading stack and bind #X to the canvas.
- the ";" terminates the messages and begins a new one. "#X" (= our canvas) will be the new target.
- "obj 43 31 struct text float x float y text t;" asks the canvas to create an object (like in dynamic patching)
- vmess(s__X.s_thing, gensym("pop"), "i", 0) sends the "pop" messages to the canvas. This effectively pops the canvas from the stack and #X is restored to whatever it has been bound before.
Hope that helps.
Christof
-------- Dan Wilcox @danomatika http://twitter.com/danomatika danomatika.com http://danomatika.com/ robotcowboy.com http://robotcowboy.com/