]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
Issue 4360: Reorganize smob initialization to make it more reliable
[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 ADD_SMOB_INIT (Translator_group);
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     (Translator::unsmob (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 (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 = Translator::unsmob (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 = Translator::unsmob (scm_car (tr_list));
78       tr->disconnect_from_context (context_);
79     }
80   context_->event_source ()->remove_listener (GET_LISTENER (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 (dynamic_cast<Performer *> (Translator::unsmob (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 (Performer::is_smob (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 (Engraver::is_smob (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 IMPLEMENT_LISTENER (Translator_group, create_child_translator);
151 void
152 Translator_group::create_child_translator (SCM sev)
153 {
154   Stream_event *ev = Stream_event::unsmob (sev);
155   // get from AnnounceNewContext
156   SCM cs = ev->get_property ("context");
157   Context *new_context = Context::unsmob (cs);
158   Context_def *def = Context_def::unsmob (new_context->get_definition ());
159   SCM ops = new_context->get_definition_mods ();
160
161   SCM trans_names = def->get_translator_names (ops);
162
163   Translator_group *g = get_translator_group (def->get_translator_group_type ());
164   SCM trans_list = SCM_EOL;
165
166   for (SCM s = trans_names; scm_is_pair (s); s = scm_cdr (s))
167     {
168       SCM definition = scm_car (s);
169       bool is_scheme = false;
170
171       Translator *type = 0;
172       if (ly_is_symbol (definition))
173         type = get_translator (definition);
174       else if (ly_is_pair (definition))
175         {
176           type = get_translator (ly_symbol2scm ("Scheme_engraver"));
177           is_scheme = true;
178         }
179       else if (ly_is_procedure (definition))
180         {
181           // `definition' is a procedure, which takes the context as
182           // an argument and evaluates to an a-list scheme engraver
183           // definition.
184           definition = scm_call_1 (definition, cs);
185           type = get_translator (ly_symbol2scm ("Scheme_engraver"));
186           is_scheme = true;
187         }
188
189       if (!type)
190         warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s)).c_str ()));
191       else
192         {
193           Translator *instance = type->clone ();
194           if (is_scheme)
195             dynamic_cast<Scheme_engraver *> (instance)->init_from_scheme (definition);
196
197           SCM str = instance->self_scm ();
198
199           if (instance->must_be_last ())
200             {
201               SCM cons = scm_cons (str, SCM_EOL);
202               if (scm_is_pair (trans_list))
203                 scm_set_cdr_x (scm_last_pair (trans_list), cons);
204               else
205                 trans_list = cons;
206             }
207           else
208             trans_list = scm_cons (str, trans_list);
209
210           instance->daddy_context_ = new_context;
211           instance->unprotect ();
212         }
213     }
214
215   /* Filter unwanted translator types. Required to make
216      \with { \consists "..." } work. */
217   if (dynamic_cast<Engraver_group *> (g))
218     g->simple_trans_list_ = filter_performers (trans_list);
219   else if (dynamic_cast<Performer_group *> (g))
220     g->simple_trans_list_ = filter_engravers (trans_list);
221
222   // TODO: scrap Context::implementation
223   new_context->implementation_ = g;
224
225   g->connect_to_context (new_context);
226   g->unprotect ();
227
228   recurse_over_translators (new_context,
229                             &Translator::initialize,
230                             &Translator_group::initialize,
231                             DOWN);
232 }
233
234 SCM
235 Translator_group::get_simple_trans_list ()
236 {
237   return simple_trans_list_;
238 }
239
240 void
241 precomputed_recurse_over_translators (Context *c, Translator_precompute_index idx, Direction dir)
242 {
243   Translator_group *tg
244     = dynamic_cast<Translator_group *> (c->implementation ());
245
246   if (tg && dir == DOWN)
247     {
248       tg->precomputed_translator_foreach (idx);
249       tg->call_precomputed_self_method (idx);
250     }
251
252   for (SCM s = c->children_contexts (); scm_is_pair (s);
253        s = scm_cdr (s))
254     precomputed_recurse_over_translators (Context::unsmob (scm_car (s)), idx, dir);
255
256   if (tg && dir == UP)
257     {
258       tg->precomputed_translator_foreach (idx);
259       tg->call_precomputed_self_method (idx);
260     }
261 }
262
263 void
264 recurse_over_translators (Context *c, Translator_method ptr,
265                           Translator_group_method tg_ptr, Direction dir)
266 {
267   Translator_group *tg
268     = dynamic_cast<Translator_group *> (c->implementation ());
269
270   if (tg && dir == DOWN)
271     {
272       (tg->*tg_ptr) ();
273       translator_each (tg->get_simple_trans_list (), ptr);
274     }
275
276   for (SCM s = c->children_contexts (); scm_is_pair (s);
277        s = scm_cdr (s))
278     recurse_over_translators (Context::unsmob (scm_car (s)), ptr, tg_ptr, dir);
279
280   if (tg && dir == UP)
281     {
282       translator_each (tg->get_simple_trans_list (),
283                        ptr);
284
285       (tg->*tg_ptr) ();
286     }
287 }
288
289 Translator_group::Translator_group ()
290 {
291   simple_trans_list_ = SCM_EOL;
292   protected_events_ = SCM_EOL;
293   context_ = 0;
294   smobify_self ();
295 }
296
297 void
298 Translator_group::derived_mark () const
299 {
300 }
301
302 void
303 Translator_group::precompute_method_bindings ()
304 {
305   for (SCM s = simple_trans_list_; scm_is_pair (s); s = scm_cdr (s))
306     {
307       Translator *tr = Translator::unsmob (scm_car (s));
308       Translator::Callback ptrs[TRANSLATOR_METHOD_PRECOMPUTE_COUNT];
309       tr->fetch_precomputable_methods (ptrs);
310
311       assert (tr);
312       for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
313         {
314           if (ptrs[i])
315             precomputed_method_bindings_[i].push_back (Translator_method_binding (tr, ptrs[i]));
316         }
317     }
318
319   fetch_precomputable_methods (precomputed_self_method_bindings_);
320 }
321
322 void
323 Translator_group::precomputed_translator_foreach (Translator_precompute_index idx)
324 {
325   vector<Translator_method_binding> &bindings (precomputed_method_bindings_[idx]);
326   for (vsize i = 0; i < bindings.size (); i++)
327     bindings[i].invoke ();
328 }
329
330 void
331 Translator_group::fetch_precomputable_methods (Translator_group_void_method ptrs[])
332 {
333   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
334     ptrs[i] = 0;
335 }
336
337 void
338 Translator_group::call_precomputed_self_method (Translator_precompute_index idx)
339 {
340   if (precomputed_self_method_bindings_[idx])
341     (*precomputed_self_method_bindings_[idx]) (this);
342 }
343
344 Translator_group::~Translator_group ()
345 {
346 }
347
348
349 const char Translator_group::type_p_name_[] = "ly:translator-group?";
350
351 int
352 Translator_group::print_smob (SCM port, scm_print_state *)
353 {
354   scm_puts ("#<Translator_group ", port);
355   scm_puts (class_name (), port);
356   scm_display (simple_trans_list_, port);
357   scm_puts (" >", port);
358   return 1;
359 }
360
361 SCM
362 Translator_group::mark_smob ()
363 {
364   derived_mark ();
365   scm_gc_mark (protected_events_);
366   return simple_trans_list_;
367 }