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