X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Ftranslator-group.cc;h=5a5b5695f018a5539a3d434f3739e90b2ec02ae2;hb=47db9a3883d726ca53e2133a3b2298f78dd6a32e;hp=83bfdfa0b7be9eeb0839a056a0a20b8103c6eea9;hpb=474c8729dc274a30558102a015a01fa5882673db;p=lilypond.git diff --git a/lily/translator-group.cc b/lily/translator-group.cc index 83bfdfa0b7..5a5b5695f0 100644 --- a/lily/translator-group.cc +++ b/lily/translator-group.cc @@ -1,10 +1,21 @@ /* - translator-group.cc -- implement Translator_group + This file is part of LilyPond, the GNU music typesetter. - source file of the GNU LilyPond music typesetter - - (c) 1997--2007 Han-Wen Nienhuys , + Copyright (C) 1997--2015 Han-Wen Nienhuys , Erik Sandberg + + 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-group.hh" @@ -12,21 +23,23 @@ #include "context-def.hh" #include "context.hh" #include "dispatcher.hh" +#include "engraver.hh" #include "engraver-group.hh" #include "international.hh" #include "main.hh" #include "music.hh" #include "output-def.hh" +#include "performer.hh" #include "performer-group.hh" +#include "scheme-engraver.hh" #include "scm-hash.hh" #include "warn.hh" - void translator_each (SCM list, Translator_method method) { for (SCM p = list; scm_is_pair (p); p = scm_cdr (p)) - (unsmob_translator (scm_car (p))->*method) (); + (Translator::unsmob (scm_car (p))->*method) (); } void @@ -41,15 +54,15 @@ Translator_group::connect_to_context (Context *c) if (context_) { programming_error ("translator group is already connected to context " - + context_->context_name ()); + + context_->context_name ()); } - + context_ = c; c->event_source ()->add_listener (GET_LISTENER (create_child_translator), - ly_symbol2scm ("AnnounceNewContext")); + ly_symbol2scm ("AnnounceNewContext")); for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list)) { - Translator *tr = unsmob_translator (scm_car (tr_list)); + Translator *tr = Translator::unsmob (scm_car (tr_list)); tr->connect_to_context (c); } } @@ -59,11 +72,11 @@ Translator_group::disconnect_from_context () { for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list)) { - Translator *tr = unsmob_translator (scm_car (tr_list)); + Translator *tr = Translator::unsmob (scm_car (tr_list)); tr->disconnect_from_context (context_); } context_->event_source ()->remove_listener (GET_LISTENER (create_child_translator), - ly_symbol2scm ("AnnounceNewContext")); + ly_symbol2scm ("AnnounceNewContext")); context_ = 0; protected_events_ = SCM_EOL; } @@ -73,16 +86,25 @@ Translator_group::finalize () { } +/* + Both filter_performers and filter_engravers used to use a direct dynamic_cast + on the unsmobbed translator to be filtered, i.e., + + if (dynamic_cast (Translator::unsmob (scm_car (*tail)))) + + but this caused mysterious optimisation issues in several GUB builds. See + issue #818 for the background to this change. +*/ SCM filter_performers (SCM ell) { SCM *tail = ℓ for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p)) { - if (dynamic_cast (unsmob_translator (scm_car (*tail)))) - *tail = scm_cdr (*tail); + if (Performer::is_smob (scm_car (*tail))) + *tail = scm_cdr (*tail); else - tail = SCM_CDRLOC (*tail); + tail = SCM_CDRLOC (*tail); } return ell; } @@ -93,15 +115,15 @@ filter_engravers (SCM ell) SCM *tail = ℓ for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p)) { - if (dynamic_cast (unsmob_translator (scm_car (*tail)))) - *tail = scm_cdr (*tail); + if (Engraver::is_smob (scm_car (*tail))) + *tail = scm_cdr (*tail); else - tail = SCM_CDRLOC (*tail); + tail = SCM_CDRLOC (*tail); } return ell; } -/* +/* Protects the parameter from being garbage collected. The object is protected until the next disconnect_from_context call. @@ -109,7 +131,7 @@ filter_engravers (SCM ell) this list. This eliminates the need for derived_mark methods in most translators; all incoming events are instead protected by the translator group. - + TODO: Should the list also be flushed at the beginning of each new moment? */ @@ -127,13 +149,13 @@ IMPLEMENT_LISTENER (Translator_group, create_child_translator); void Translator_group::create_child_translator (SCM sev) { - Stream_event *ev = unsmob_stream_event (sev); + Stream_event *ev = Stream_event::unsmob (sev); // get from AnnounceNewContext SCM cs = ev->get_property ("context"); - Context *new_context = unsmob_context (cs); - Context_def *def = unsmob_context_def (new_context->get_definition ()); + Context *new_context = Context::unsmob (cs); + Context_def *def = Context_def::unsmob (new_context->get_definition ()); SCM ops = new_context->get_definition_mods (); - + SCM trans_names = def->get_translator_names (ops); Translator_group *g = get_translator_group (def->get_translator_group_type ()); @@ -141,32 +163,55 @@ Translator_group::create_child_translator (SCM sev) for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s)) { - Translator *type = get_translator (scm_car (s)); + SCM definition = scm_car (s); + bool is_scheme = false; + + Translator *type = 0; + if (ly_is_symbol (definition)) + type = get_translator (definition); + else if (ly_is_pair (definition)) + { + type = get_translator (ly_symbol2scm ("Scheme_engraver")); + is_scheme = true; + } + else if (ly_is_procedure (definition)) + { + // `definition' is a procedure, which takes the context as + // an argument and evaluates to an a-list scheme engraver + // definition. + definition = scm_call_1 (definition, cs); + type = get_translator (ly_symbol2scm ("Scheme_engraver")); + is_scheme = true; + } + if (!type) - warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s)).c_str ())); + warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s)).c_str ())); else - { - Translator *tr = type->clone (); - SCM str = tr->self_scm (); - - if (tr->must_be_last ()) - { - SCM cons = scm_cons (str, SCM_EOL); - if (scm_is_pair (trans_list)) - scm_set_cdr_x (scm_last_pair (trans_list), cons); - else - trans_list = cons; - } - else - trans_list = scm_cons (str, trans_list); - - tr->daddy_context_ = new_context; - tr->unprotect (); - } + { + Translator *instance = type->clone (); + if (is_scheme) + dynamic_cast (instance)->init_from_scheme (definition); + + SCM str = instance->self_scm (); + + if (instance->must_be_last ()) + { + SCM cons = scm_cons (str, SCM_EOL); + if (scm_is_pair (trans_list)) + scm_set_cdr_x (scm_last_pair (trans_list), cons); + else + trans_list = cons; + } + else + trans_list = scm_cons (str, trans_list); + + instance->daddy_context_ = new_context; + instance->unprotect (); + } } /* Filter unwanted translator types. Required to make - \with {\consists "..."} work. */ + \with { \consists "..." } work. */ if (dynamic_cast (g)) g->simple_trans_list_ = filter_performers (trans_list); else if (dynamic_cast (g)) @@ -179,9 +224,9 @@ Translator_group::create_child_translator (SCM sev) g->unprotect (); recurse_over_translators (new_context, - &Translator::initialize, - &Translator_group::initialize, - DOWN); + &Translator::initialize, + &Translator_group::initialize, + DOWN); } SCM @@ -204,7 +249,7 @@ precomputed_recurse_over_translators (Context *c, Translator_precompute_index id for (SCM s = c->children_contexts (); scm_is_pair (s); s = scm_cdr (s)) - precomputed_recurse_over_translators (unsmob_context (scm_car (s)), idx, dir); + precomputed_recurse_over_translators (Context::unsmob (scm_car (s)), idx, dir); if (tg && dir == UP) { @@ -214,7 +259,8 @@ precomputed_recurse_over_translators (Context *c, Translator_precompute_index id } void -recurse_over_translators (Context *c, Translator_method ptr, Translator_group_method tg_ptr, Direction dir) +recurse_over_translators (Context *c, Translator_method ptr, + Translator_group_method tg_ptr, Direction dir) { Translator_group *tg = dynamic_cast (c->implementation ()); @@ -227,12 +273,12 @@ recurse_over_translators (Context *c, Translator_method ptr, Translator_group_me for (SCM s = c->children_contexts (); scm_is_pair (s); s = scm_cdr (s)) - recurse_over_translators (unsmob_context (scm_car (s)), ptr, tg_ptr, dir); + recurse_over_translators (Context::unsmob (scm_car (s)), ptr, tg_ptr, dir); if (tg && dir == UP) { translator_each (tg->get_simple_trans_list (), - ptr); + ptr); (tg->*tg_ptr) (); } @@ -256,16 +302,16 @@ Translator_group::precompute_method_bindings () { for (SCM s = simple_trans_list_; scm_is_pair (s); s = scm_cdr (s)) { - Translator *tr = unsmob_translator (scm_car (s)); + Translator *tr = Translator::unsmob (scm_car (s)); Translator_void_method_ptr ptrs[TRANSLATOR_METHOD_PRECOMPUTE_COUNT]; tr->fetch_precomputable_methods (ptrs); assert (tr); for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++) - { - if (ptrs[i]) - precomputed_method_bindings_[i].push_back (Translator_method_binding (tr, ptrs[i])); - } + { + if (ptrs[i]) + precomputed_method_bindings_[i].push_back (Translator_method_binding (tr, ptrs[i])); + } } fetch_precomputable_methods (precomputed_self_method_bindings_); @@ -297,29 +343,23 @@ Translator_group::~Translator_group () { } -#include "ly-smobs.icc" -IMPLEMENT_SMOBS (Translator_group); -IMPLEMENT_DEFAULT_EQUAL_P (Translator_group); -IMPLEMENT_TYPE_P (Translator_group, "ly:translator-group?"); +const char Translator_group::type_p_name_[] = "ly:translator-group?"; int -Translator_group::print_smob (SCM s, SCM port, scm_print_state *) +Translator_group::print_smob (SCM port, scm_print_state *) { - Translator_group *me = (Translator_group *) SCM_CELL_WORD_1 (s); scm_puts ("#class_name (), port); - scm_display (me->simple_trans_list_, port); + scm_puts (class_name (), port); + scm_display (simple_trans_list_, port); scm_puts (" >", port); return 1; } SCM -Translator_group::mark_smob (SCM smob) +Translator_group::mark_smob () { - Translator_group *me = (Translator_group *)SCM_CELL_WORD_1 (smob); - - me->derived_mark (); - scm_gc_mark (me->protected_events_); - return me->simple_trans_list_; + derived_mark (); + scm_gc_mark (protected_events_); + return simple_trans_list_; }