]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/translator.cc
Run `make grand-replace'.
[lilypond.git] / lily / translator.cc
index d7c5fdf29c965653fcee357262d7091f7c5f61d4..9ebe2cd4bf97fcc829030087c4b0e0713450e106 100644 (file)
@@ -3,15 +3,17 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include "translator.hh"
 
-#include "warn.hh"
-#include "translator-group.hh"
 #include "context-def.hh"
+#include "dispatcher.hh"
 #include "global-context.hh"
+#include "international.hh"
+#include "translator-group.hh"
+#include "warn.hh"
 
 #include "translator.icc"
 #include "ly-smobs.icc"
@@ -29,7 +31,6 @@ Translator::init ()
   smobify_self ();
 }
 
-
 void
 Translator::process_music ()
 {
@@ -51,12 +52,6 @@ Translator::Translator (Translator const &src)
   must_be_last_ = src.must_be_last_;
 }
 
-bool
-Translator::try_music (Music *)
-{
-  return false;
-}
-
 Moment
 Translator::now_mom () const
 {
@@ -75,6 +70,12 @@ Translator::get_daddy_translator () const
   return daddy_context_->implementation ();
 }
 
+void
+Translator::protect_event (SCM ev)
+{
+  get_daddy_translator ()->protect_event (ev);
+}
+
 SCM
 Translator::internal_get_property (SCM sym) const
 {
@@ -87,12 +88,9 @@ Translator::stop_translation_timestep ()
 }
 
 /*
-  this function has 2 properties
-
-  - It is called before try_music ()
-
-  - It is called before any user information enters the translators.
-  (i.e. any \property or event is not processed yet.)
+  this function is called once each moment, before any user
+  information enters the translators.  (i.e. no \property or event has
+  been processed yet.)
 */
 void
 Translator::start_translation_timestep ()
@@ -109,6 +107,116 @@ Translator::finalize ()
 {
 }
 
+void
+Translator::connect_to_context (Context *c)
+{
+  for (translator_listener_record *r = get_listener_list (); r; r=r->next_)
+    c->events_below ()->add_listener (r->get_listener_ (this), r->event_class_);
+}
+
+void
+Translator::disconnect_from_context (Context *c)
+{
+  for (translator_listener_record *r = get_listener_list (); r; r=r->next_)
+    c->events_below ()->remove_listener (r->get_listener_ (this), r->event_class_);
+}
+
+static SCM listened_event_class_table;
+void
+ensure_listened_hash ()
+{
+  if (!listened_event_class_table)
+    listened_event_class_table = scm_permanent_object (scm_c_make_hash_table (61));
+}
+
+
+LY_DEFINE (ly_get_listened_event_classes, "ly:get-listened-event-classes",
+          0, 0, 0, (),
+          "Return a list of all event classes that some translator listens"
+          " to.")
+{
+  ensure_listened_hash ();
+  return ly_hash_table_keys (listened_event_class_table);
+}
+
+LY_DEFINE (ly_is_listened_event_class, "ly:is-listened-event-class",
+          1, 0, 0, (SCM sym),
+          "Is @var{sym} a listened event class?")
+{
+  ensure_listened_hash ();
+  return scm_hashq_ref (listened_event_class_table, sym, SCM_BOOL_F);
+}
+
+void
+add_listened_event_class (SCM sym)
+{
+  ensure_listened_hash ();
+  scm_hashq_set_x (listened_event_class_table, sym, SCM_BOOL_T);
+}
+
+
+/*
+  internally called once, statically, for each translator
+  listener. Connects the name of an event class with a procedure that
+  fetches the corresponding listener.
+
+  The method should only be called from the macro
+  IMPLEMENT_TRANSLATOR_LISTENER.
+ */
+void
+Translator::add_translator_listener (translator_listener_record **listener_list,
+                                    translator_listener_record *r,
+                                    Listener (*get_listener) (void *), 
+                                    const char *ev_class)
+{
+  /* ev_class is the C++ identifier name. Convert to scm symbol */
+  string name = string (ev_class);
+  name = replace_all (&name, '_', '-');
+  name += "-event";
+  
+  SCM class_sym = scm_str2symbol (name.c_str ());
+  
+  add_listened_event_class (class_sym);
+
+  r->event_class_ = class_sym;
+  r->get_listener_ = get_listener;
+  r->next_ = *listener_list;
+  *listener_list = r;
+}
+
+/*
+ Helps the individual static_translator_description methods of translators.
+*/
+SCM
+Translator::static_translator_description (const char *grobs,
+                                          const char *desc,
+                                          translator_listener_record *listener_list,
+                                          const char *read, 
+                                          const char *write) const
+{
+  SCM static_properties = SCM_EOL;                                     
+
+  static_properties = scm_acons (ly_symbol2scm ("grobs-created"),      
+                                parse_symbol_list (grobs), static_properties);
+  
+  static_properties = scm_acons (ly_symbol2scm ("description"),        
+                                scm_from_locale_string (desc), static_properties); 
+  
+  SCM list = SCM_EOL;
+  for (; listener_list; listener_list = listener_list->next_)
+    list = scm_cons (listener_list->event_class_, list);
+  static_properties = scm_acons (ly_symbol2scm ("events-accepted"),
+                                list, static_properties);
+  
+  static_properties = scm_acons (ly_symbol2scm ("properties-read"),    
+                                parse_symbol_list (read), static_properties); 
+  
+  static_properties = scm_acons (ly_symbol2scm ("properties-written"), 
+                                parse_symbol_list (write), static_properties); 
+  
+  return static_properties;                                            
+}
+  
 /*
   SMOBS
 */
@@ -126,7 +234,7 @@ Translator::get_global_context () const
   return daddy_context_->get_global_context ();
 }
 
-Score_context *
+Context *
 Translator::get_score_context () const
 {
   return daddy_context_->get_score_context ();
@@ -152,46 +260,111 @@ Translator::print_smob (SCM s, SCM port, scm_print_state *)
 {
   Translator *me = (Translator *) SCM_CELL_WORD_1 (s);
   scm_puts ("#<Translator ", port);
-  scm_puts (classname (me), port);
+  scm_puts (me->class_name (), port);
   scm_puts (" >", port);
   return 1;
 }
 
 void
-add_acknowledger (Translator_void_method_ptr ptr,
-                 const char *func_name,
-                 Array<Acknowledge_information> *ack_array)
+add_acknowledger (Engraver_void_function_engraver_grob_info ptr,
+                 char const *func_name,
+                 vector<Acknowledge_information> *ack_array)
 {
   Acknowledge_information inf;
   inf.function_ = ptr;
 
-  String interface_name(func_name);
+  string interface_name (func_name);
 
-  interface_name = interface_name.substitute ("acknowledge_", "");
-  interface_name = interface_name.substitute ('_', '-');
+  interface_name = replace_all (&interface_name, '_', '-');
   interface_name += "-interface";
 
-  inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.to_str0 ()));
-  ack_array->push (inf);
+  /*
+    this is only called during program init, so safe to use scm_gc_protect_object ()
+  */
+  inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
+  ack_array->push_back (inf);
 }
 
-Translator_void_method_ptr 
-generic_get_acknowledger (SCM sym, Array<Acknowledge_information> const *ack_array)
+Engraver_void_function_engraver_grob_info
+generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
 {
-  for (int i = 0; i < ack_array->size(); i++)
+  for (vsize i = 0; i < ack_array->size (); i++)
     {
-      if (ack_array->elem (i).symbol_ == sym)
-       {
-         return ack_array->elem(i).function_;
-       }
+      if (ack_array->at (i).symbol_ == sym)
+       return ack_array->at (i).function_;
     }
   return 0;
 }
 
-ADD_TRANSLATOR(Translator,
-              "Base class. Unused",
-              "",
-              "",
-              "",
-              "",
-              "");
+
+Moment
+get_event_length (Stream_event *e)
+{
+  Moment *m = unsmob_moment (e->get_property ("length"));
+  if (m)
+    return *m;
+  else
+    return Moment (0);
+}
+
+Moment
+get_event_length (Stream_event *e, Moment now)
+{
+  Moment len = get_event_length (e);
+  
+  if (now.grace_part_)
+    {
+      len.grace_part_ = len.main_part_;
+      len.main_part_ = Rational (0);
+    }
+  return len;
+}
+
+/*
+  Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
+  simultaneous events. The helper is only useful in listen_* methods
+  of translators.
+*/
+bool
+internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
+{
+  if (*old_ev &&
+      !to_boolean (scm_equal_p ((*old_ev)->self_scm (), 
+                              new_ev->self_scm ())))
+    {
+      /* extract event class from function name */
+      string ev_class = function;
+
+      /* This assertion fails if EVENT_ASSIGNMENT was called outside a
+        translator listener. Don't do that. */
+      const char *prefix = "listen_";
+      assert (0 == ev_class.find (prefix));
+
+      /* "listen_foo_bar" -> "foo-bar" */
+      ev_class.erase (0, strlen (prefix));
+      replace_all (&ev_class, '_', '-');
+
+      new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class.c_str ()));
+      (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class.c_str ()));
+      return false;
+    }
+  else
+    {
+      *old_ev = new_ev;
+      return true;
+    }
+}
+
+ADD_TRANSLATOR (Translator,
+               /* doc */
+               "Base class.  Not instantiated.",
+
+               /* create */
+               "",
+
+               /* read */
+               "",
+
+               /* write */
+               ""
+               );