X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Ftranslator.cc;h=bdc55f0f05467a4d4cb5fd56b548deb9995329c2;hb=d84b43d9cc76809a2d008419194c0c94486311a4;hp=a5c612a929bdda8f9f4288db93a0598e62a6670e;hpb=ca4a47ffd343a3672c0e13b801eeb2647866ce98;p=lilypond.git diff --git a/lily/translator.cc b/lily/translator.cc index a5c612a929..bdc55f0f05 100644 --- a/lily/translator.cc +++ b/lily/translator.cc @@ -1,21 +1,32 @@ /* - 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 - (c) 1997--2004 Han-Wen Nienhuys -*/ + 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 . +*/ #include "translator.hh" -#include "warn.hh" -#include "translator-group.hh" + #include "context-def.hh" +#include "dispatcher.hh" #include "global-context.hh" -#include "moment.hh" -#include "context.hh" -#include "ly-smobs.icc" +#include "international.hh" +#include "translator-group.hh" +#include "warn.hh" +#include "translator.icc" Translator::~Translator () { @@ -24,21 +35,18 @@ Translator::~Translator () void Translator::init () { - must_be_last_ = false; - self_scm_ = SCM_EOL; - daddy_context_ =0; + daddy_context_ = 0; smobify_self (); } void -Translator::do_announces () +Translator::process_music () { } void -Translator::process_music () +Translator::process_acknowledged () { - } Translator::Translator () @@ -46,18 +54,11 @@ Translator::Translator () init (); } -Translator::Translator (Translator const &src) +Translator::Translator (Translator const &) + : Smob () { init (); - must_be_last_ = src.must_be_last_; -} - -bool -Translator::try_music (Music *) -{ - return false; } - Moment Translator::now_mom () const @@ -71,13 +72,17 @@ Translator::get_output_def () const return daddy_context_->get_output_def (); } - -Translator_group* +Translator_group * 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 @@ -91,14 +96,10 @@ 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 () { @@ -114,60 +115,202 @@ Translator::finalize () { } +void +Translator::connect_to_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 ()->add_listener (get_listener (callback), + event_class); + } +} -/* +void +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); + } +} - SMOBS +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 ()); +} + +/* + Helps the individual static_translator_description methods of translators. */ SCM -Translator::mark_smob (SCM sm) +Translator::static_translator_description (const char *grobs, + const char *desc, + SCM listener_list, + const char *read, + const char *write) const { - Translator * me = (Translator*) SCM_CELL_WORD_1 (sm); - me->derived_mark (); - return SCM_EOL; + 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; } +/* + SMOBS +*/ SCM -Translator::translator_description () const +Translator::mark_smob () const { + derived_mark (); return SCM_EOL; } - Global_context * Translator::get_global_context () const { - return daddy_context_ ->get_global_context (); + return daddy_context_->get_global_context (); } - -Score_context * +Context * Translator::get_score_context () const { return daddy_context_->get_score_context (); -} - - -SCM -Translator::static_translator_description ()const -{ - return SCM_EOL; } - -IMPLEMENT_SMOBS (Translator); -IMPLEMENT_DEFAULT_EQUAL_P (Translator); -IMPLEMENT_TYPE_P (Translator,"ly:translator?"); +const char * const Translator::type_p_name_ = "ly:translator?"; bool Translator::must_be_last () const { - return must_be_last_; + return false; } void Translator::derived_mark () const { - } + +int +Translator::print_smob (SCM port, scm_print_state *) const +{ + scm_puts ("#", port); + return 1; +} + +void +add_acknowledger (SCM ptr, + char const *func_name, + SCM &ack_hash) +{ + 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 (ack_hash) + ->set (ly_symbol2scm (interface_name.c_str ()), ptr); +} + +SCM +generic_get_acknowledger (SCM sym, SCM ack_hash) +{ + if (SCM_UNBNDP (ack_hash)) + return SCM_UNDEFINED; + + return unsmob (ack_hash)->get (sym); +} + +Moment +get_event_length (Stream_event *e) +{ + Moment *m = unsmob (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; + } +} + +// Base class. Not instantiated. No ADD_TRANSLATOR call.