]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
99026ca051c48b3b1c9b2a66983dc6568376f19e
[lilypond.git] / lily / translator-group.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>,
5                  Erik Sandberg <mandolaerik@gmail.com>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "translator-group.hh"
22
23 #include "context-def.hh"
24 #include "context.hh"
25 #include "dispatcher.hh"
26 #include "engraver.hh"
27 #include "engraver-group.hh"
28 #include "international.hh"
29 #include "main.hh"
30 #include "music.hh"
31 #include "output-def.hh"
32 #include "performer.hh"
33 #include "performer-group.hh"
34 #include "scheme-engraver.hh"
35 #include "scm-hash.hh"
36 #include "warn.hh"
37
38 using std::vector;
39
40 void
41 translator_each (SCM list, Translator_method method)
42 {
43   for (SCM p = list; scm_is_pair (p); p = scm_cdr (p))
44     (unsmob<Translator> (scm_car (p))->*method) ();
45 }
46
47 void
48 Translator_group::initialize ()
49 {
50   precompute_method_bindings ();
51 }
52
53 void
54 Translator_group::connect_to_context (Context *c)
55 {
56   if (context_)
57     {
58       programming_error ("translator group is already connected to context "
59                          + context_->context_name ());
60     }
61
62   context_ = c;
63   c->event_source ()->add_listener (GET_LISTENER (Translator_group, create_child_translator),
64                                     ly_symbol2scm ("AnnounceNewContext"));
65   for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list))
66     {
67       Translator *tr = unsmob<Translator> (scm_car (tr_list));
68       tr->connect_to_context (c);
69     }
70 }
71
72 void
73 Translator_group::disconnect_from_context ()
74 {
75   for (SCM tr_list = simple_trans_list_; scm_is_pair (tr_list); tr_list = scm_cdr (tr_list))
76     {
77       Translator *tr = unsmob<Translator> (scm_car (tr_list));
78       tr->disconnect_from_context (context_);
79     }
80   context_->event_source ()->remove_listener (GET_LISTENER (Translator_group, create_child_translator),
81                                               ly_symbol2scm ("AnnounceNewContext"));
82   context_ = 0;
83   protected_events_ = SCM_EOL;
84 }
85
86 void
87 Translator_group::finalize ()
88 {
89 }
90
91 /*
92   Both filter_performers and filter_engravers used to use a direct dynamic_cast
93   on the unsmobbed translator to be filtered, i.e.,
94
95   if (unsmob<Performer> (scm_car (*tail)))
96
97   but this caused mysterious optimisation issues in several GUB builds.  See
98   issue #818 for the background to this change.
99 */
100 SCM
101 filter_performers (SCM ell)
102 {
103   SCM *tail = &ell;
104   for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
105     {
106       if (unsmob<Performer> (scm_car (*tail)))
107         *tail = scm_cdr (*tail);
108       else
109         tail = SCM_CDRLOC (*tail);
110     }
111   return ell;
112 }
113
114 SCM
115 filter_engravers (SCM ell)
116 {
117   SCM *tail = &ell;
118   for (SCM p = ell; scm_is_pair (p); p = scm_cdr (p))
119     {
120       if (unsmob<Engraver> (scm_car (*tail)))
121         *tail = scm_cdr (*tail);
122       else
123         tail = SCM_CDRLOC (*tail);
124     }
125   return ell;
126 }
127
128 /*
129   Protects the parameter from being garbage collected. The object is
130   protected until the next disconnect_from_context call.
131
132   Whenever a child translator hears an event, the event is added to
133   this list. This eliminates the need for derived_mark methods in most
134   translators; all incoming events are instead protected by the
135   translator group.
136
137   TODO: Should the list also be flushed at the beginning of each new
138   moment?
139  */
140 void
141 Translator_group::protect_event (SCM ev)
142 {
143   protected_events_ = scm_cons (ev, protected_events_);
144 }
145
146 /*
147   Create a new translator for a newly created child context. Triggered
148   by AnnounceNewContext events.
149  */
150 void
151 Translator_group::create_child_translator (SCM sev)
152 {
153   Stream_event *ev = unsmob<Stream_event> (sev);
154   // get from AnnounceNewContext
155   SCM cs = ev->get_property ("context");
156   Context *new_context = unsmob<Context> (cs);
157   Context_def *def = unsmob<Context_def> (new_context->get_definition ());
158   SCM ops = new_context->get_definition_mods ();
159
160   SCM trans_names = def->get_translator_names (ops);
161
162   Translator_group *g = get_translator_group (def->get_translator_group_type ());
163   SCM trans_list = SCM_EOL;
164
165   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
166     {
167       SCM definition = scm_car (s);
168       bool is_scheme = false;
169
170       Translator *type = 0;
171       if (ly_is_symbol (definition))
172         type = get_translator (definition);
173       else if (ly_is_pair (definition))
174         {
175           type = get_translator (ly_symbol2scm ("Scheme_engraver"));
176           is_scheme = true;
177         }
178       else if (ly_is_procedure (definition))
179         {
180           // `definition' is a procedure, which takes the context as
181           // an argument and evaluates to an a-list scheme engraver
182           // definition.
183           definition = scm_call_1 (definition, cs);
184           type = get_translator (ly_symbol2scm ("Scheme_engraver"));
185           is_scheme = true;
186         }
187
188       if (!type)
189         warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s)).c_str ()));
190       else
191         {
192           Translator *instance = type->clone ();
193           if (is_scheme)
194             dynamic_cast<Scheme_engraver *> (instance)->init_from_scheme (definition);
195
196           SCM str = instance->self_scm ();
197
198           if (instance->must_be_last ())
199             {
200               SCM cons = scm_cons (str, SCM_EOL);
201               if (scm_is_pair (trans_list))
202                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
203               else
204                 trans_list = cons;
205             }
206           else
207             trans_list = scm_cons (str, trans_list);
208
209           instance->daddy_context_ = new_context;
210           instance->unprotect ();
211         }
212     }
213
214   /* Filter unwanted translator types. Required to make
215      \with { \consists "..." } work. */
216   if (dynamic_cast<Engraver_group *> (g))
217     g->simple_trans_list_ = filter_performers (trans_list);
218   else if (dynamic_cast<Performer_group *> (g))
219     g->simple_trans_list_ = filter_engravers (trans_list);
220
221   // TODO: scrap Context::implementation
222   new_context->implementation_ = g;
223
224   g->connect_to_context (new_context);
225   g->unprotect ();
226
227   recurse_over_translators (new_context,
228                             &Translator::initialize,
229                             &Translator_group::initialize,
230                             DOWN);
231 }
232
233 SCM
234 Translator_group::get_simple_trans_list ()
235 {
236   return simple_trans_list_;
237 }
238
239 void
240 precomputed_recurse_over_translators (Context *c, Translator_precompute_index idx, Direction dir)
241 {
242   Translator_group *tg
243     = dynamic_cast<Translator_group *> (c->implementation ());
244
245   if (tg && dir == DOWN)
246     {
247       tg->precomputed_translator_foreach (idx);
248       tg->call_precomputed_self_method (idx);
249     }
250
251   for (SCM s = c->children_contexts (); scm_is_pair (s);
252        s = scm_cdr (s))
253     precomputed_recurse_over_translators (unsmob<Context> (scm_car (s)), idx, dir);
254
255   if (tg && dir == UP)
256     {
257       tg->precomputed_translator_foreach (idx);
258       tg->call_precomputed_self_method (idx);
259     }
260 }
261
262 void
263 recurse_over_translators (Context *c, Translator_method ptr,
264                           Translator_group_method tg_ptr, Direction dir)
265 {
266   Translator_group *tg
267     = dynamic_cast<Translator_group *> (c->implementation ());
268
269   if (tg && dir == DOWN)
270     {
271       (tg->*tg_ptr) ();
272       translator_each (tg->get_simple_trans_list (), ptr);
273     }
274
275   for (SCM s = c->children_contexts (); scm_is_pair (s);
276        s = scm_cdr (s))
277     recurse_over_translators (unsmob<Context> (scm_car (s)), ptr, tg_ptr, dir);
278
279   if (tg && dir == UP)
280     {
281       translator_each (tg->get_simple_trans_list (),
282                        ptr);
283
284       (tg->*tg_ptr) ();
285     }
286 }
287
288 Translator_group::Translator_group ()
289 {
290   simple_trans_list_ = SCM_EOL;
291   protected_events_ = SCM_EOL;
292   context_ = 0;
293   smobify_self ();
294 }
295
296 void
297 Translator_group::derived_mark () const
298 {
299 }
300
301 void
302 Translator_group::precompute_method_bindings ()
303 {
304   for (SCM s = simple_trans_list_; scm_is_pair (s); s = scm_cdr (s))
305     {
306       Translator *tr = unsmob<Translator> (scm_car (s));
307       Translator::Callback ptrs[TRANSLATOR_METHOD_PRECOMPUTE_COUNT];
308       tr->fetch_precomputable_methods (ptrs);
309
310       assert (tr);
311       for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
312         {
313           if (ptrs[i])
314             precomputed_method_bindings_[i].push_back (Translator_method_binding (tr, ptrs[i]));
315         }
316     }
317
318   fetch_precomputable_methods (precomputed_self_method_bindings_);
319 }
320
321 void
322 Translator_group::precomputed_translator_foreach (Translator_precompute_index idx)
323 {
324   vector<Translator_method_binding> &bindings (precomputed_method_bindings_[idx]);
325   for (vsize i = 0; i < bindings.size (); i++)
326     bindings[i].invoke ();
327 }
328
329 void
330 Translator_group::fetch_precomputable_methods (Translator_group_void_method ptrs[])
331 {
332   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
333     ptrs[i] = 0;
334 }
335
336 void
337 Translator_group::call_precomputed_self_method (Translator_precompute_index idx)
338 {
339   if (precomputed_self_method_bindings_[idx])
340     (*precomputed_self_method_bindings_[idx]) (this);
341 }
342
343 Translator_group::~Translator_group ()
344 {
345 }
346
347
348 const char Translator_group::type_p_name_[] = "ly:translator-group?";
349
350 int
351 Translator_group::print_smob (SCM port, scm_print_state *) const
352 {
353   scm_puts ("#<Translator_group ", port);
354   scm_puts (class_name (), port);
355   scm_display (simple_trans_list_, port);
356   scm_puts (" >", port);
357   return 1;
358 }
359
360 SCM
361 Translator_group::mark_smob () const
362 {
363   derived_mark ();
364   scm_gc_mark (protected_events_);
365   return simple_trans_list_;
366 }