]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/translator.cc
Add two bug numbers for guile-2.0 to d/changelog
[lilypond.git] / lily / translator.cc
index ae5562579e8d694e530a0f8f21a61629c65cc101..bdc55f0f05467a4d4cb5fd56b548deb9995329c2 100644 (file)
 /*
-  translator.cc -- implement Translator
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the GNU LilyPond music typesetter
+  Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
-  (c)  1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
-*/
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
 
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "translator.hh"
-#include "debug.hh"
+
+#include "context-def.hh"
+#include "dispatcher.hh"
+#include "global-context.hh"
+#include "international.hh"
 #include "translator-group.hh"
-#include "dictionary-iter.hh"
-#include "rational.hh"
+#include "warn.hh"
+
+#include "translator.icc"
 
 Translator::~Translator ()
 {
 }
 
-Translator::Translator ()
+void
+Translator::init ()
 {
-  status = ORPHAN;
-  daddy_trans_l_ = 0;
-  output_def_l_ = 0;
+  daddy_context_ = 0;
+  smobify_self ();
 }
 
-Translator::Translator (Translator const &s)
-  : Input (s)
+void
+Translator::process_music ()
 {
-  status = ORPHAN;
-  daddy_trans_l_ =0;
-  output_def_l_ = s.output_def_l_;
-  properties_dict_ = s.properties_dict_;
-  type_str_ = s.type_str_;
 }
 
-bool
-Translator::is_alias_b (String s) const
+void
+Translator::process_acknowledged ()
 {
-  return s == type_str_;
 }
 
-bool
-Translator::do_try_request (Request *)
+Translator::Translator ()
 {
-  return false;
+  init ();
+}
+
+Translator::Translator (Translator const &)
+  : Smob<Translator> ()
+{
+  init ();
 }
-                           
 
 Moment
-Translator::now_moment () const
+Translator::now_mom () const
+{
+  return daddy_context_->now_mom ();
+}
+
+Output_def *
+Translator::get_output_def () const
+{
+  return daddy_context_->get_output_def ();
+}
+
+Translator_group *
+Translator::get_daddy_translator () const
 {
-  return daddy_trans_l_->now_moment ();
+  return daddy_context_->implementation ();
 }
 
+void
+Translator::protect_event (SCM ev)
+{
+  get_daddy_translator ()->protect_event (ev);
+}
+
+SCM
+Translator::internal_get_property (SCM sym) const
+{
+  return daddy_context_->internal_get_property (sym);
+}
+
+void
+Translator::stop_translation_timestep ()
+{
+}
+
+/*
+  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 ()
+{
+}
 
 void
-Translator::add_processing ()
+Translator::initialize ()
 {
-  if (status > ORPHAN)
-    return;
-  
-  do_add_processing ();
-  status = VIRGIN;
 }
 
 void
-Translator::do_add_processing ()
+Translator::finalize ()
 {
 }
 
 void
-Translator::print () const
+Translator::connect_to_context (Context *c)
 {
-#ifndef NPRINT
-  DOUT << name () << " {";
-  if (name () != type_str_)
-    DOUT << "type = " << type_str_;
-  for (Dictionary_iter<Scalar> i (properties_dict_); i.ok (); i++)
+  for (SCM r = get_listener_list (); scm_is_pair (r); r = scm_cdr (r))
     {
-      DOUT << i.key () << "=" << i.val () <<"\n";
+      SCM event_class = scm_caar (r);
+      SCM callback = scm_cdar (r);
+
+      c->events_below ()->add_listener (get_listener (callback),
+                                        event_class);
     }
-  do_print ();
-  DOUT << "}\n";
-#endif
 }
 
 void
-Translator::do_print () const
+Translator::disconnect_from_context (Context *c)
 {
+  for (SCM r = get_listener_list (); scm_is_pair (r); r = scm_cdr (r))
+    {
+      SCM event_class = scm_caar (r);
+      SCM callback = scm_cdar (r);
+
+      c->events_below ()->remove_listener (get_listener (callback),
+                                           event_class);
+    }
 }
 
-IMPLEMENT_IS_TYPE_B(Translator);
+SCM
+Translator::event_class_symbol (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";
 
+  return scm_from_ascii_symbol (name.c_str ());
+}
 
-void
-Translator::creation_processing ()
+/*
+ Helps the individual static_translator_description methods of translators.
+*/
+SCM
+Translator::static_translator_description (const char *grobs,
+                                           const char *desc,
+                                           SCM listener_list,
+                                           const char *read,
+                                           const char *write) const
 {
-  if (status >= CREATION_INITED)
-    return ;
-  
-  do_creation_processing ();
-  status = CREATION_INITED;
+  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_utf8_string (desc), static_properties);
+
+  SCM list = SCM_EOL;
+  for (; scm_is_pair (listener_list); listener_list = scm_cdr (listener_list))
+    list = scm_cons (scm_caar (listener_list), 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;
 }
 
-void
-Translator::post_move_processing ()
+/*
+  SMOBS
+*/
+SCM
+Translator::mark_smob () const
 {
-  if (status >= MOVE_INITED)
-    return;
+  derived_mark ();
+  return SCM_EOL;
+}
 
-  creation_processing ();
-  do_post_move_processing ();
-  status = MOVE_INITED;
+Global_context *
+Translator::get_global_context () const
+{
+  return daddy_context_->get_global_context ();
 }
 
-void
-Translator::removal_processing ()
+Context *
+Translator::get_score_context () const
 {
-  if (status == ORPHAN)
-    return;
-  creation_processing ();
-  do_removal_processing ();
-  // elegancy ...
-  // status = ORPHAN;
+  return daddy_context_->get_score_context ();
 }
 
+const char * const Translator::type_p_name_ = "ly:translator?";
 
 bool
-Translator::try_request (Request * r)
+Translator::must_be_last () const
 {
-  if (status < MOVE_INITED)
-    post_move_processing ();
-
-  return do_try_request (r);
+  return false;
 }
 
 void
-Translator::process_requests ()
+Translator::derived_mark () const
 {
-  if (status < PROCESSED_REQS)
-    post_move_processing ();
-  else if (status >= PROCESSED_REQS)
-    return; 
-  
-  status = PROCESSED_REQS;
-  do_process_requests ();
+}
+
+int
+Translator::print_smob (SCM port, scm_print_state *) const
+{
+  scm_puts ("#<Translator ", port);
+  scm_puts (class_name (), port);
+  scm_puts (" >", port);
+  return 1;
 }
 
 void
-Translator::pre_move_processing ()
+add_acknowledger (SCM ptr,
+                  char const *func_name,
+                  SCM &ack_hash)
 {
-  do_pre_move_processing ();
-  status = CREATION_INITED;
+  if (SCM_UNBNDP (ack_hash))
+    ack_hash = Scheme_hash_table::make_smob ();
+
+  string interface_name (func_name);
+
+  interface_name = replace_all (&interface_name, '_', '-');
+  interface_name += "-interface";
+
+  unsmob<Scheme_hash_table> (ack_hash)
+    ->set (ly_symbol2scm (interface_name.c_str ()), ptr);
 }
 
-Scalar
-Translator::get_property (String id)
+SCM
+generic_get_acknowledger (SCM sym, SCM ack_hash)
 {
-  if (properties_dict_.elt_b (id))
-    {
-      return properties_dict_[id];
-    }
-  
-  if (daddy_trans_l_)
-    return daddy_trans_l_->get_property (id);
+  if (SCM_UNBNDP (ack_hash))
+    return SCM_UNDEFINED;
 
-  return "";
+  return unsmob<Scheme_hash_table> (ack_hash)->get (sym);
 }
 
-void
-Translator::set_property (String id, Scalar val)
+Moment
+get_event_length (Stream_event *e)
 {
-  properties_dict_[id] = val;
+  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;
+}
 
-Music_output_def *
-Translator::output_def_l () const
+/*
+  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)
 {
-  return output_def_l_;
+  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;
+    }
 }
+
+// Base class.  Not instantiated.  No ADD_TRANSLATOR call.