2 listener.hh -- declare Listener
4 source file of the GNU LilyPond music typesetter
6 (c) 2005 Erik Sandberg <mandolaerik@gmail.com>
15 Listeners are used for stream event dispatching. If you want to
16 register a method as an event handler in a dispatcher, then you
19 - declare the method using the DECLARE_LISTENER macro.
22 DECLARE_LISTENER (method);
25 This macro declares the method to take a SCM as parameter, and to
26 return void. It also declares some other stuff that shouldn't be
29 - implement the method using IMPLEMENT_LISTENER:
30 IMPLEMENT_LISTENER (Foo, method)
33 write ("Foo hears an event!");
36 - Extract a listener using GET_LISTENER (Foo->method)
37 - Register the method to the dispatcher using Dispatcher::register
42 Stream_distributor *d = (...);
43 Listener l = GET_LISTENER (foo->method);
44 d->register_listener (l, "EventClass");
46 Whenever d hears a stream-event ev of class "EventClass",
47 the implemented procedure is called.
50 - DECLARE_LISTENER currently only works inside smob classes.
56 void (*listen_callback) (void *, SCM);
57 void (*mark_callback) (void *);
58 } Listener_function_table;
62 Listener_function_table *type_;
64 Listener (const void *target, Listener_function_table *type);
65 Listener (Listener const &other);
66 void listen (SCM ev) const;
68 bool operator == (Listener const &other) const
69 { return target_ == other.target_ && type_ == other.type_; }
71 DECLARE_SIMPLE_SMOBS (Listener,);
73 DECLARE_UNSMOB (Listener, listener);
75 #define IMPLEMENT_LISTENER(cl, method) \
77 cl :: method ## _callback (void *self, SCM ev) \
83 cl :: method ## _mark (void *self) \
86 scm_gc_mark (s->self_scm ()); \
89 cl :: method ## _listener () const \
91 static Listener_function_table callbacks; \
92 callbacks.listen_callback = &cl::method ## _callback; \
93 callbacks.mark_callback = &cl::method ## _mark; \
94 return Listener (this, &callbacks); \
97 #define GET_LISTENER(proc) ( proc ## _listener ())
99 #define DECLARE_LISTENER(name) \
100 inline void name (SCM); \
101 static void name ## _callback (void *self, SCM ev); \
102 static void name ## _mark (void *self); \
103 Listener name ## _listener () const
105 #endif /* LISTENER_HH */