Forums > Dev

notify when attribute changes


Nov 29 2008 | 1:30 pm

hi

i need my object to be notified when one of its attributes changes.
this seems to work fine if i write custom getter and setter methods,
however it’s a little overkill in the simple example i’m working on.

so i tried the "notify" message, cause the doc says:
"As an alternative to writing a custom setter, you can take advantage
of the fact that objects receive a "notify" message whenever one of
their attributes is changed."

the example below compiles fine, but i can’t get any notification
when changing the "test" attribute in max.
what am i doing wrong?

thanks,
volker.

/***** testnotify **********/

#include "ext.h"
#include "ext_obex.h"

static t_class *s_myObj_class;

typedef struct _myObj
{
t_object ob;
long testattr;
} t_myObj;

void myObj_bang(t_myObj *x);
void myObj_int(t_myObj *x, long n);
void *myObj_new(t_symbol *s, long argc, t_atom *argv);
t_max_err myObj_notify(t_myObj *x, t_symbol *s, t_symbol *msg, void
*sender, void *data);

int main(void)
{
t_class *c;
c = class_new("testnotify", (method)myObj_new, 0, sizeof(t_myObj),
0L, A_GIMME, 0);

class_addmethod(c, (method)myObj_bang, "bang", 0);
class_addmethod(c, (method)myObj_int, "int", A_LONG, 0);
class_addmethod(c, (method)myObj_notify, "notify", A_CANT, 0);

// attributes
CLASS_ATTR_LONG( c, "test", 0, t_myObj, testattr );

class_register(CLASS_BOX, c);
s_myObj_class = c;
return 0;
}

t_max_err myObj_notify(t_myObj *x, t_symbol *s, t_symbol *msg, void
*sender, void *data) {
// look for all messages
post("notify");
//object_post((t_object *)x, "i got msg: %s", msg->s_name);

t_symbol *attrname;
if(msg == gensym("attr_modified")) {
attrname = (t_symbol *)object_method((t_object *)data, gensym
("getname"));
object_post((t_object *)x, "i got a %s notification from the %s
attribute", msg->s_name, attrname->s_name);
}
return 0;
}

void myObj_bang(t_myObj *x) {
post("test: %ld", x->testattr);
}

void myObj_int(t_myObj *x, long n) {
x->testattr = n;
}

void *myObj_new(t_symbol *s, long argc, t_atom *argv)
{
t_myObj *x = (t_myObj *)object_alloc(s_myObj_class);
x->testattr = 77;
return (x);
}

Nov 29 2008 | 2:00 pm

You need to attach the object to itself (ensuring that it gets registered) in order to receive attr notifications from itself:

in myObject_new():

object_attach_byptr_register(x, x, CLASS_BOX);

jb

Nov 29 2008 | 6:22 pm

On 29 Nov 2008, at 15:00, Jeremy Bernstein wrote:
>
> You need to attach the object to itself (ensuring that it gets
> registered) in order to receive attr notifications from itself:
>
> in myObject_new():
>
> object_attach_byptr_register(x, x, CLASS_BOX);
>

ah, thanks jeremy, now it works.
might be an idea to add this line to the documentation in "Sending
Messages, Calling Methods".
maybe it’s already somewhere else.

thanks,
volker.

Oct 27 2016 | 10:07 pm

This is very old, but I agree this should really be documented in that particular section of the API, it’s still not!

Viewing 4 posts - 1 through 4 (of 4 total)

Forums > Dev