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);
68 void listen (SCM ev) const;
70 bool operator == (Listener const &other) const
71 { return target_ == other.target_ && type_ == other.type_; }
73 DECLARE_SIMPLE_SMOBS (Listener,);
75 DECLARE_UNSMOB (Listener, listener);
77 #define IMPLEMENT_LISTENER(cl, method) \
79 cl :: method ## _callback (void *self, SCM ev) \
85 cl :: method ## _mark (void *self) \
88 scm_gc_mark (s->self_scm ()); \
91 cl :: method ## _listener () const \
93 static Listener_function_table callbacks; \
94 callbacks.listen_callback = &cl::method ## _callback; \
95 callbacks.mark_callback = &cl::method ## _mark; \
96 return Listener (this, &callbacks); \
99 #define GET_LISTENER(proc) proc ## _listener ()
101 #define DECLARE_LISTENER(name) \
102 inline void name (SCM); \
103 static void name ## _callback (void *self, SCM ev); \
104 static void name ## _mark (void *self); \
105 Listener name ## _listener () const
107 #endif /* LISTENER_HH */