On Tuesday 04 April 2006 21.37, Han-Wen Nienhuys wrote:
> Erik Sandberg wrote:
> > On Tuesday 04 April 2006 20.46, Han-Wen Nienhuys wrote:
> >> Han-Wen Nienhuys wrote:
> >>> I'd start with 4. because they're independent from the rest, and we can
> >>> readily test the rest of those.
> >
> > The reason for my ordering, is that 3 can be used to verify that 4 works.
> >
> > BTW, (1-3) are completely independent of (4), and I have just finished
> > step (1) locally. If you insist on getting (4) done before (1), then
> > that's perfectly OK with me; but is it OK for you to look at my patch for
> > (1) while I do (4)? I guess (1) is the step which requires the longest
> > discussion, so it might be good to start the discussion early.
>
> If you have a working patch, let's see it.

It's just a bunch of new files; all are attached.

Some known issues:
- scm/define-event-classes.scm contains rather unsorted functions which are 
related to music streams. Most of them are probably in the wrong place. I 
guess simplify-scheme should be in music-functions.scm, but where should I 
place car< ?
- The Stream_event class duplicates its 'context property with a context_ 
member; this was originally intended to give speedups, but it is broken in 
this version and requires some modifications to Context in order to work. 
I'll probably remove the context_ member altogether in the next revision.

The dispatcher system has been tested with the following code:

#(begin
; event -> disp -> list
  (define disp (ly:make-dispatcher))
  (define list (ly:make-listener (lambda (ev) (display "received: \"") 
(display ev) (display "\"\n"))))
  (define ev (ly:make-stream-event '((class . StreamEvent) (id . 2) (foo . 
"bar"))))
  (ly:add-listener list disp 'StreamEvent)
  (ly:broadcast disp ev)

; ev -> disp2 -> disp -> list
  (define disp2 (ly:make-dispatcher))
  (ly:connect-dispatchers disp disp2)
  (ly:broadcast disp2 ev)
)

-- 
Erik
/*
  dispatcher.cc -- implement Dispatcher

  source file of the GNU LilyPond music typesetter

  (c) 2005-2006 Erik Sandberg  <[EMAIL PROTECTED]>
*/

#include "dispatcher.hh"
#include "international.hh"
#include "ly-smobs.icc"
#include "stream-event.hh"
#include "warn.hh"

// ES todo: move to lily-guile.hh
SCM appendable_list ();
void appendable_list_append (SCM l, SCM elt);

IMPLEMENT_SMOBS (Dispatcher);
IMPLEMENT_TYPE_P (Dispatcher, "dispatcher");
IMPLEMENT_DEFAULT_EQUAL_P (Dispatcher);

Dispatcher::~Dispatcher ()
{
}

Dispatcher::Dispatcher ()
{
  self_scm_ = SCM_EOL;
  listeners_ = SCM_EOL;
  dispatchers_ = SCM_EOL;
  listen_classes_ = SCM_EOL;
  smobify_self ();
  //TODO use resizable hash
  listeners_ = scm_c_make_hash_table (17);
  priority_count_ = 0;
}

SCM
Dispatcher::mark_smob (SCM sm)
{
  Dispatcher *me = (Dispatcher *) SCM_CELL_WORD_1 (sm);
  scm_gc_mark (me->dispatchers_);
  scm_gc_mark (me->listen_classes_);
  return me->listeners_;
}

int
Dispatcher::print_smob (SCM s, SCM p, scm_print_state*)
{
  Dispatcher *me = (Dispatcher *) SCM_CELL_WORD_1 (s);
  scm_puts ("#<Dispatcher ", p);
  scm_write (scm_vector_to_list (me->listeners_), p);
  scm_puts (">", p);
  return 1;
}

/*
Event dispatching:
- Collect a list of listeners for each relevant class
- Send the event to each of these listeners, in increasing priority order.
  This is done by keeping a bubble-sorted temporary list of listener lists,
  and iteratively send the event to the lowest-priority listener.
- An event is never sent twice to listeners with equal priority.
*/
IMPLEMENT_LISTENER (Dispatcher, dispatch) (Stream_event *ev)
{
  SCM class_symbol = ev->get_property ("class");
  if (!scm_symbol_p (class_symbol))
    {
      warning (_f ("Unknown event class %s", ly_symbol2string (class_symbol).c_str ()));
      return;
    }

  SCM class_list = scm_primitive_eval (class_symbol);
  bool sent = false;

  // TODO: fix this loop.
  int num_classes = 0;
  for (SCM cl = class_list; cl != SCM_EOL; cl = scm_cdr (cl))
    num_classes++;

  // Collect all listener lists.
  struct { int prio; SCM list; } lists[num_classes+1];
  int i = 0;
  for (SCM cl = class_list; cl != SCM_EOL; cl = scm_cdr (cl))
    {
      SCM list = scm_hashq_ref (listeners_, scm_car (cl), SCM_EOL);
      if (list == SCM_EOL)
	num_classes--;
      else 
	{
          // bubblesort.
          int prio = scm_to_int (scm_caar (list));
	  int j;
	  for (j = i; j > 0 && lists[j-1].prio > prio; j--)
	    lists[j] = lists[j-1];
	  lists[j].prio = prio;
	  lists[j].list = list;
	  i++;
        }
    }
  lists[num_classes].prio = INT_MAX;

  // Never send an event to two listeners with equal priority.
  int last_priority = -1;
  // Iteratively process all event classes, in increasing priority.
  while (num_classes)
    {
      // Send the event, if we haven't already sent it to this target.
      if (lists[0].prio != last_priority)
        {
          // process the listener
          assert (lists[0].prio > last_priority);
          last_priority = lists[0].prio;

          Listener *l = unsmob_listener (scm_cdar (lists[0].list));
          l->listen (ev);
          sent = true;
        }
      // go to the next listener; bubble-sort the class list.
      SCM next = scm_cdr (lists[0].list);
      if (next == SCM_EOL)
        num_classes--;
      int prio = (next == SCM_EOL) ? INT_MAX : scm_to_int (scm_caar (next));
      for (i = 0; prio > lists[i+1].prio; i++)
        lists[i] = lists[i+1];
      lists[i].prio = prio;
      lists[i].list = next;
    }

  if (!sent)
    warning (_f ("Junking event: %s", ly_symbol2string (class_symbol).c_str ()));
}

void
Dispatcher::add_listener (Listener l, SCM ev_class)
{
  internal_add_listener (l, ev_class, ++priority_count_);
}

#if 0
/*
 New listeners are appended to the end of the list.
 This way, listeners will listen to an event in the order they were added.
*/
inline void
Dispatcher::internal_add_listener (Listener l, SCM ev_class, int priority)
{
  SCM list = scm_hashq_ref (listeners_, ev_class, SCM_EOL);
  if (list == SCM_EOL)
    {
      list = appendable_list ();
      scm_hashq_set_x (listeners_, ev_class, list);
      /* Register with all dispatchers. */
      for (SCM disp = dispatchers_; disp != SCM_EOL; disp = scm_cdr (disp))
	{
	  int priority = scm_cdar (disp);
	  Dispatcher *d = unsmob_dispatcher (scm_caar (disp));
	  d->internal_add_listener (dispatch (), ev_class, priority);
	}
      listen_classes_ = scm_cons (ev_class, listen_classes_);
    }

  appendable_list_append (list, l.smobbed_copy ());
}
#endif

inline void
Dispatcher::internal_add_listener (Listener l, SCM ev_class, int priority)
{
  SCM list = scm_hashq_ref (listeners_, ev_class, SCM_EOL);
  if (list == SCM_EOL)
    {
      /* Register with all dispatchers. */
      for (SCM disp = dispatchers_; disp != SCM_EOL; disp = scm_cdr (disp))
	{
	  int priority = scm_to_int (scm_cdar (disp));
	  Dispatcher *d = unsmob_dispatcher (scm_caar (disp));
	  d->internal_add_listener (dispatch (), ev_class, priority);
	}
      listen_classes_ = scm_cons (ev_class, listen_classes_);
    }
  SCM entry = scm_cons (scm_int2num (priority), l.smobbed_copy ());
  list = scm_merge_x (list, scm_list_1 (entry), scm_primitive_eval (ly_symbol2scm ("car<")));
  scm_hashq_set_x (listeners_, ev_class, list);
}

void
Dispatcher::remove_listener (Listener l, SCM ev_class)
{
  SCM list = scm_hashq_ref (listeners_, ev_class, SCM_EOL);

  if (list == SCM_EOL)
    {
      programming_error ("remove_listener called with incorrect class.");
      return;
    }

  // We just remove the listener once.
  bool first = true;

  SCM dummy = scm_cons (SCM_EOL, list);
  SCM e = dummy;
  while (scm_cdr (e) != SCM_EOL)
    if (*unsmob_listener (scm_cdadr (e)) == l && first)
      {
	scm_set_cdr_x (e, scm_cddr(e));
	first = false;
	break;
      }
    else
      e = scm_cdr (e);
  list = scm_cdr (dummy);

#if 0 // obsolete appandable-list code
  SCM e = list;
  while (scm_cdr (e) != SCM_EOL)
    if (*unsmob_listener (scm_cadr (e)) == l && first)
      {
	scm_set_cdr_x (e, scm_cddr(e));
	first = false;
      }
    else
      e = scm_cdr (e);
  scm_set_car_x (list, e);
#endif

  if (first)
    warning ("Attempting to remove nonexisting listener.");
  else if (list == SCM_EOL)
    {
      /* Unregister with all dispatchers. */
      for (SCM disp = dispatchers_; disp != SCM_EOL; disp = scm_cdr (disp))
	{
	  Dispatcher *d = unsmob_dispatcher (scm_caar (disp));
	  d->remove_listener (dispatch (), ev_class);
	}
      listen_classes_ = scm_delq_x (ev_class, listen_classes_);
    }
}

/* Register as a listener to another dispatcher. */
void
Dispatcher::register_as_listener (Dispatcher *disp)
{
  int priority = ++disp->priority_count_;

  // Don't register twice to the same dispatcher.
  if (scm_assq (disp->self_scm (), dispatchers_) != SCM_BOOL_F)
    {
      warning ("Already listening to dispatcher, ignoring request");
      return;
    }

  dispatchers_ = scm_acons (disp->self_scm (), scm_int2num (priority), dispatchers_);

  Listener list = dispatch ();
  for (SCM cl = listen_classes_; cl != SCM_EOL; cl = scm_cdr (cl))
    {
      disp->internal_add_listener (list, scm_car (cl), priority);
    }
}

/* Unregister as a listener to another dispatcher. */
void
Dispatcher::unregister_as_listener (Dispatcher *disp)
{
#ifndef NDEBUG
//  assert (SCM_EOL == scm_hashq_ref (listeners_, ly_symbol2scm ("StreamEvent"), SCM_EOL));
#endif

  dispatchers_ = scm_assq_remove_x (dispatchers_, disp->self_scm ());

  Listener list = dispatch ();
  for (SCM cl = listen_classes_; cl != SCM_EOL; cl = scm_cdr (cl))
    {
      disp->remove_listener (list, scm_car (cl));
    }
}

LY_DEFINE (ly_make_dispatcher, "ly:make-dispatcher",
	   0, 0, 0, (),
	   "Returns a newly created dispatcher.")
{
  return (new Dispatcher ())->unprotect ();
}

LY_DEFINE (ly_register_dispatcher, "ly:connect-dispatchers",
	   2, 0, 0, (SCM to, SCM from),
	   "Makes the dispatcher @var{to} listen to events from @var{from}." )
{
  Dispatcher *t = unsmob_dispatcher (to);
  Dispatcher *f = unsmob_dispatcher (from);
  SCM_ASSERT_TYPE (t, from, SCM_ARG1, __FUNCTION__, "dispatcher");
  SCM_ASSERT_TYPE (f, to, SCM_ARG2, __FUNCTION__, "dispatcher");
  t->register_as_listener (f);

  return SCM_UNDEFINED;
}

LY_DEFINE (ly_add_listener, "ly:add-listener",
	   2, 0, 1, (SCM list, SCM disp, SCM cl),
	   "Adds the listener @var{list} to the dispatcher @var{disp}.\n"
	   " Whenever @var{disp} hears an event of class @var{cl}, it will be forwarded to @var{list}.\n" )
{
  Listener *l = unsmob_listener (list);
  Dispatcher *d = unsmob_dispatcher (disp);
  SCM_ASSERT_TYPE (l, list, SCM_ARG1, __FUNCTION__, "listener");
  SCM_ASSERT_TYPE (d, disp, SCM_ARG2, __FUNCTION__, "dispatcher");
  
  for (int arg=SCM_ARG3; cl != SCM_EOL; cl = scm_cdr (cl), arg++)
    {
      SCM_ASSERT_TYPE (scm_symbol_p (cl), cl, arg, __FUNCTION__, "symbol");
      d->add_listener (*l, scm_car (cl));
    }

  return SCM_UNDEFINED;
}

LY_DEFINE (ly_broadcast, "ly:broadcast",
	   2, 0, 0, (SCM disp, SCM ev),
	   "Sends the stream event @var{ev} to the dispatcher\n"
	   "@var{disp}.")
{
  Dispatcher *d = unsmob_dispatcher (disp);
  Stream_event *e = unsmob_stream_event (ev);
  SCM_ASSERT_TYPE (d, disp, SCM_ARG1, __FUNCTION__, "dispatcher");
  SCM_ASSERT_TYPE (e, ev, SCM_ARG2, __FUNCTION__, "stream event");
  d->broadcast (e);
  return SCM_UNDEFINED;
}
/*
  listener-scheme.cc -- Connect listeners to Scheme through Scm_listener

  source file of the GNU LilyPond music typesetter

  (c) 2005-2006 Erik Sandberg  <[EMAIL PROTECTED]>
*/

#include "listener.hh"
#include "ly-smobs.icc"
#include "stream-event.hh"

class Scm_listener
{
public:
  Scm_listener (SCM callback);
  DECLARE_LISTENER (listener);
protected:
  DECLARE_SMOBS (Scm_listener,);
private:
  SCM callback_;
};

IMPLEMENT_LISTENER (Scm_listener, listener) (Stream_event *ev)
{
  scm_call_1 (callback_, ev->self_scm ());
}

IMPLEMENT_SMOBS (Scm_listener);
IMPLEMENT_DEFAULT_EQUAL_P (Scm_listener);

Scm_listener::Scm_listener (SCM c)
{
  callback_ = SCM_EOL;
  self_scm_ = SCM_EOL;
  smobify_self ();
  callback_ = c; 
}

SCM
Scm_listener::mark_smob (SCM obj)
{
  Scm_listener *me = (Scm_listener *) SCM_CELL_WORD_1 (obj);
  return me->callback_;
}

int
Scm_listener::print_smob (SCM obj, SCM p, scm_print_state*)
{
  Scm_listener *me = (Scm_listener *) SCM_CELL_WORD_1 (obj);
  scm_puts ("#<Scm_listener ", p);
  scm_write (me->callback_, p);
  scm_puts (">", p);
  return 1;
}

Scm_listener::~Scm_listener ()
{
}

LY_DEFINE (ly_make_listener, "ly:make-listener",
	   1, 0, 0, (SCM callback),
	   "Creates a stream-event listener. Any time the listener hears\n"
	   " a @code{stream-event} object, it will call @var{callback}\n"
	   " with that object. @var{callback} should take exactly one argument." )
{
  SCM_ASSERT_TYPE (scm_procedure_p (callback), callback, SCM_ARG1, __FUNCTION__, "procedure");
  Scm_listener *l = new Scm_listener (callback);
  SCM listener = l->listener ().smobbed_copy ();
  l->unprotect ();
  return listener;
}
/*
  listener.cc -- implement Listener and Listener_target

  source file of the GNU LilyPond music typesetter

  (c) 2005 Erik Sandberg  <[EMAIL PROTECTED]>
*/

#include "listener.hh"
#include "ly-smobs.icc"
#include "warn.hh"

/*
Listener_target::~Listener_target ()
{
}
*/

Listener::Listener (const void *target, listener_vtable *type)
{
  target_ = (void *)target;
  type_ = type;
}

Listener::Listener (Listener const &other)
{
  target_ = other.target_;
  type_ = other.type_; 
}

void Listener::listen (Stream_event *ev) const {
  (type_->listen_callback) (target_, ev);
}

SCM
Listener::mark_smob (SCM sm)
{
  Listener *me = (Listener *) SCM_CELL_WORD_1 (sm);
  (me->type_->mark_callback) (me->target_);
  return SCM_EOL;
}

int
Listener::print_smob (SCM s, SCM p, scm_print_state*)
{
  scm_puts ("#<Listener>", p);
  return 1;
}

SCM
Listener::equal_p (SCM a, SCM b)
{
  Listener *l1 = unsmob_listener (a);
  Listener *l2 = unsmob_listener (b);

  return (*l1 == *l2) ? SCM_BOOL_T : SCM_BOOL_F;
}

IMPLEMENT_SIMPLE_SMOBS (Listener);
IMPLEMENT_TYPE_P (Listener, "listener");
/*
  stream-event.cc -- implement Stream_event

  source file of the GNU LilyPond music typesetter

  (c) 2005-2006 Erik Sandberg  <[EMAIL PROTECTED]>
*/

#include "stream-event.hh"

#include "ly-smobs.icc"
#include "context.hh"
#include "input.hh"
#include "input-smob.hh"

// ES todo: Add stuff to lily-proto.hh: Stream_event and its subclasses, Stream_creator, etc.

Stream_event::~Stream_event ()
{
}

void
Stream_event::init ()
{
  self_scm_ = SCM_EOL;
  context_ = -1;
  property_alist_ = SCM_EOL;
  origin_ = 0;

  smobify_self ();
}

Stream_event::Stream_event ()
{
  init ();
}

Stream_event::Stream_event (Context *c, Input *origin)
{
  init ();
  //TODO: re-implement  context_ = c->get_unique ();
  set_property ("context", scm_int2num (context_));
  origin_ = origin;
}

Stream_event::Stream_event (SCM property_alist)
{
  init ();
  property_alist_ = property_alist;
  origin_ = &dummy_input_global;
}

Stream_event::Stream_event (Context *c, SCM class_name)
{
  init ();
  //TODO:re-implement  context_ = c->get_unique ();
  set_property ("context", scm_int2num (context_));
  set_property ("class", class_name);
  origin_ = &dummy_input_global;
}

Stream_event::Stream_event (Stream_event *ev)
{
  init ();
  property_alist_ = scm_copy_tree (ev->property_alist_);
  origin_ = ev->origin_;
  context_ = ev->context_;
}

Input *
Stream_event::origin () const
{
  return origin_;
}

SCM
Stream_event::mark_smob (SCM sm)
{
  Stream_event *me = (Stream_event *) SCM_CELL_WORD_1 (sm);
  return me->property_alist_;
}

int
Stream_event::print_smob (SCM s, SCM port, scm_print_state *)
{
  scm_puts ("#<Stream_event ", port);
  scm_write (dump (s), port);
  scm_puts (" >", port);
  return 1;
}

IMPLEMENT_SMOBS (Stream_event);
IMPLEMENT_DEFAULT_EQUAL_P (Stream_event);
IMPLEMENT_TYPE_P (Stream_event, "ly:stream-event?");

MAKE_SCHEME_CALLBACK (Stream_event, undump, 1);
MAKE_SCHEME_CALLBACK (Stream_event, dump, 1);

SCM
Stream_event::dump (SCM self)
{
  Stream_event *ev = unsmob_stream_event (self);
  // Reversed alists look prettier.
  return scm_reverse (ev->property_alist_);
}

SCM
Stream_event::undump (SCM data)
{
  Stream_event *obj = new Stream_event ();
  obj->property_alist_ = scm_reverse (data);
  obj->context_ = scm_to_int (obj->get_property ("context"));
  return obj->unprotect ();
}

SCM
Stream_event::internal_get_property (SCM sym) const
{
  SCM s = scm_sloppy_assq (sym, property_alist_);
  if (s != SCM_BOOL_F)
    return scm_cdr (s);
  return SCM_EOL;
}

void 
Stream_event::internal_set_property (SCM prop, SCM val)
{
  property_alist_ = scm_assq_set_x (property_alist_, prop, val);
}

/* ------------------------------------ */

LY_DEFINE (ly_make_stream_event, "ly:make-stream-event",
	   1, 0, 0, (SCM proplist),
	   "Creates a stream event, with the given property list.\n" )
{
  SCM_ASSERT_TYPE (scm_list_p (proplist), proplist, SCM_ARG1, __FUNCTION__, "association list");
  Stream_event *e = new Stream_event (proplist);
  return e->unprotect ();
}

LY_DEFINE (ly_stream_event_context, "ly:stream-event-context",
	   1, 0, 0, (SCM sev),
	   "Returns the context number of the given stream event.")
{
  Stream_event *e = unsmob_stream_event (sev);
  SCM_ASSERT_TYPE (e, sev, SCM_ARG1, __FUNCTION__, "stream event");
  return scm_int2num (e->get_context ());
}

LY_DEFINE (ly_stream_event_property, "ly:stream-event-property", 
           2, 0, 0, (SCM sev, SCM sym),
	   "Get the property @var{sym} of stream event @var{mus}.\n"
	   "If @var{sym} is undefined, return @code{' ()}.\n")
{
  Stream_event *e = unsmob_stream_event (sev);
  SCM_ASSERT_TYPE (e, sev, SCM_ARG1, __FUNCTION__, "stream event");
  SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");

  return e->internal_get_property (sym);
}
/*
  dispatcher.hh -- declare Dispatcher

  source file of the GNU LilyPond music typesetter

  (c) 2005 Erik Sandberg  <[EMAIL PROTECTED]>
*/

#ifndef DISPATCHER_HH
#define DISPATCHER_HH

#include "listener.hh"
#include "stream-event.hh"

class Dispatcher
{
  /* Hash table. Each event-class maps to a list of listeners. */
  SCM listeners_;
  /* alist of dispatchers that we listen to. Each entry is a
     (dist . priority) pair. */
  SCM dispatchers_;
  SCM listen_classes_;
  DECLARE_LISTENER (dispatch);
  /* priority counter. Listeners with low priority receive events
     first. */
  int priority_count_;
  void internal_add_listener (Listener, SCM event_class, int priority);
public:
  Dispatcher ();
  void broadcast (Stream_event *ev) { dispatch_proc (ev); }
  void add_listener (Listener, SCM event_class);
  void remove_listener (Listener, SCM event_class);
  void register_as_listener (Dispatcher *dist);
  void unregister_as_listener (Dispatcher *dist);
protected:
  DECLARE_SMOBS (Dispatcher,);
};

DECLARE_UNSMOB (Dispatcher, dispatcher);

#endif // DISPATCHER_HH
/*
  listener.hh -- declare Listener

  source file of the GNU LilyPond music typesetter

  (c) 2005 Erik Sandberg <[EMAIL PROTECTED]>
*/

#ifndef LISTENER_HH
#define LISTENER_HH

/*
  Listeners

  Listeners are used for stream event dispatching. If you want to
  register a method as an event handler in a dispatcher, then you
  must:

  - declare the method using DECLARE_LISTENER:
  class Foo
  {
    DECLARE_LISTENER (method);
    ...
  };

  - implement the method using IMPLEMENT_LISTENER:
  IMPLEMENT_LISTENER (Foo, method) (Stream_event *e)
  {
    write ("Foo hears an event!");
  }

  - register the method to the dispatcher using Dispatcher::register

  Foo *foo = (...);
  Stream_distributor *d = (...);
  Listener l = foo->method ();
  d->register_listener (l, "EventClass");
  
  Whenever d hears a stream-event ev of class "EventClass",
  the implemented procedure is called.

  DECLARE_LISTENER currently only works inside smob classes.

*/

#include "smobs.hh"

/*TODO: to lily-proto*/
class Stream_event;

typedef struct {
  void (*listen_callback) (void *, Stream_event *);
  void (*mark_callback) (void *);
} listener_vtable;

class Listener {
  void *target_;
  listener_vtable *type_;
public:
  Listener (const void *target, listener_vtable *type);
  Listener (Listener const &other);
  void listen (Stream_event *ev) const;

  bool operator == (Listener const &other) const
  { return target_ == other.target_ && type_ == other.type_; }

  DECLARE_SIMPLE_SMOBS (Listener,);
};
DECLARE_UNSMOB (Listener, listener);

#define IMPLEMENT_LISTENER(cl, method)				\
void								\
cl :: method ## _callback (void *self, Stream_event *ev)	\
{								\
  cl *s = (cl *)self;						\
  s->method ## _proc (ev);					\
}								\
void								\
cl :: method ## _mark (void *self) 				\
{								\
  cl *s = (cl *)self;						\
  scm_gc_mark (s->self_scm ());					\
}								\
Listener							\
cl :: method () const						\
{								\
  static listener_vtable callbacks;				\
  callbacks.listen_callback = &cl::method ## _callback;		\
  callbacks.mark_callback = &cl::method ## _mark;		\
  return Listener (this, &callbacks);				\
}								\
void								\
cl :: method ## _proc

#define DECLARE_LISTENER(name)					\
  inline void name ## _proc (Stream_event *);		       	\
  static void name ## _callback (void *self, Stream_event *ev);	\
  static void name ## _mark (void *self);			\
  Listener name () const

#endif /* LISTENER_HH */
/*
  stream-event.hh -- declare Stream_event

  source file of the GNU LilyPond music typesetter

  (c) 2005-2006 Erik Sandberg <[EMAIL PROTECTED]>
*/

#ifndef STREAM_EVENT_HH
#define STREAM_EVENT_HH

#include "lily-proto.hh"
#include "smobs.hh"
#include "prob.hh"

class Stream_event
{
  void init ();
  int context_;
  SCM property_alist_;
  Input *origin_;

public:
  Stream_event ();
  Input *origin () const;
  int get_context () {return context_; }

  DECLARE_SCHEME_CALLBACK (undump, (SCM));
  DECLARE_SCHEME_CALLBACK (dump, (SCM));

  // todo: make Input mandatory.
  Stream_event (SCM property_alist);
  Stream_event (Context *c, SCM class_name);
  Stream_event (Context *c, Input *);
  Stream_event (Stream_event *ev);

  SCM internal_get_property (SCM) const;
  void internal_set_property (SCM prop, SCM val);

protected:
  DECLARE_SMOBS (Stream_event,);
};

DECLARE_UNSMOB (Stream_event, stream_event);
DECLARE_TYPE_P (Stream_event);

#define SEND_EVENT_TO_CONTEXT(ctx, cl, ...)				\
  {									\
    Stream_event *_e_ = new Stream_event (ctx, ly_symbol2scm (cl));	\
    __VA_ARGS__;							\
    ctx->event_source ()->distribute (_e_);				\
    scm_gc_unprotect_object (_e_->self_scm ());				\
  }
  
#define EVENT_PROPERTY(prop, val)	\
  (_e_->set_property (prop, val))

#endif /* STREAM_EVENT_HH */
_______________________________________________
lilypond-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/lilypond-devel

Reply via email to