]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
9d0ab733be6c62360bb519622c2c2e4652360157
[lilypond.git] / lily / translator-group.cc
1 /*
2   Translator_group.cc -- implement Translator_group
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
12 #include "warn.hh"
13 #include "moment.hh"
14 #include "scm-hash.hh"
15 #include "context-def.hh"
16 #include "main.hh"
17 #include "music.hh"
18
19 Translator_group::Translator_group (Translator_group const&s)
20   : Translator (s)
21 {
22   iterator_count_ =0;
23   
24   Scheme_hash_table * tab =  new Scheme_hash_table (*s.properties_dict ());
25   properties_scm_ = tab->self_scm ();
26   scm_gc_unprotect_object (tab->self_scm ());
27 }
28
29 Scheme_hash_table*
30 Translator_group::properties_dict () const
31 {
32   return Scheme_hash_table::unsmob (properties_scm_);
33 }
34
35 Translator_group::~Translator_group ()
36 {
37   
38   //assert (is_removable ());
39 }
40
41
42 Translator_group::Translator_group ()
43 {
44   iterator_count_  = 0;
45   Scheme_hash_table *tab = new Scheme_hash_table ;
46   properties_scm_ = tab->self_scm ();
47
48   scm_gc_unprotect_object (tab->self_scm ());
49 }
50
51 void
52 Translator_group::check_removal ()
53 {
54   SCM next = SCM_EOL; 
55   for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
56     {
57       next = ly_cdr (p);
58
59       Translator_group *trg =  dynamic_cast<Translator_group*> (unsmob_translator (ly_car (p)));
60
61       trg->check_removal ();
62       if (trg->is_removable ())
63         terminate_translator (trg);
64     }
65 }
66
67 SCM
68 Translator_group::add_translator (SCM list, Translator *t)
69 {
70   /*
71     Must append, since list ordering must be preserved.
72    */
73   list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL));
74   t->daddy_trans_ = this;
75   t->output_def_ = output_def_;
76
77   return list;
78 }
79
80
81 void
82 Translator_group::add_used_group_translator (Translator *t)
83 {
84   trans_group_list_ = add_translator (trans_group_list_,t);
85 }
86
87
88 void
89 Translator_group::add_fresh_group_translator (Translator*t)
90 {
91   Translator_group*tg = dynamic_cast<Translator_group*> (t);
92   trans_group_list_ = add_translator (trans_group_list_,t); 
93   Context_def * td = unsmob_context_def (tg->definition_);
94
95   /*
96     this can not move before add_translator(), because \override
97     operations require that we are in the hierarchy.
98    */
99   td->apply_default_property_operations (tg);
100
101   t->initialize ();
102 }
103
104 bool
105 Translator_group::is_removable () const
106 {
107   return trans_group_list_ == SCM_EOL && ! iterator_count_;
108 }
109
110 Translator_group *
111 Translator_group::find_existing_translator (SCM n, String id)
112 {
113   if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
114     return this;
115
116   Translator_group* r = 0;
117   for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
118     {
119       Translator *  t = unsmob_translator (ly_car (p));
120       
121       r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id);    }
122
123   return r;
124 }
125
126
127 Translator_group*
128 Translator_group::find_create_translator (SCM n, String id, SCM operations)
129 {
130   Translator_group * existing = find_existing_translator (n,id);
131   if (existing)
132     return existing;
133
134
135   /*
136     TODO: use accepts_list_.
137    */
138   Link_array<Context_def> path
139     = unsmob_context_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
140
141   if (path.size ())
142     {
143       Translator_group * current = this;
144
145       // start at 1.  The first one (index 0) will be us.
146       for (int i=0; i < path.size (); i++)
147         {
148           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
149
150           Translator_group * new_group
151             = path[i]->instantiate (output_def_, ops);
152
153           if (i == path.size () -1)
154             {
155               new_group->id_string_ = id;
156             }
157
158           current->add_fresh_group_translator (new_group);
159           apply_property_operations (new_group, ops);
160           
161           current = new_group;
162         }
163
164       return current;
165     }
166
167   Translator_group *ret = 0;
168   if (daddy_trans_)
169     ret = daddy_trans_->find_create_translator (n, id, operations);
170   else
171     {
172       warning (_f ("Cannot find or create `%s' called `%s'",
173                    ly_symbol2string (n).to_str0 (), id));
174       ret =0;
175     }
176   return ret;
177 }
178
179 bool
180 Translator_group::try_music (Music* m)
181 {
182   bool hebbes_b = try_music_on_nongroup_children (m);
183   
184   if (!hebbes_b && daddy_trans_)
185     hebbes_b = daddy_trans_->try_music (m);
186   
187   return hebbes_b ;
188 }
189
190 int
191 Translator_group::get_depth () const
192 {
193   return (daddy_trans_) ? daddy_trans_->get_depth ()  + 1 : 0;
194 }
195
196 void
197 Translator_group::terminate_translator (Translator*r)
198 {
199   r->finalize ();
200   /*
201     Return value ignored. GC does the rest.
202    */
203   remove_translator (r);
204 }
205
206
207 /**
208    Remove a translator from the hierarchy.
209  */
210 Translator *
211 Translator_group::remove_translator (Translator*trans)
212 {
213   assert (trans);
214
215   trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
216   trans->daddy_trans_ = 0;
217   return trans;
218 }
219
220
221 /*
222   Default child context as a SCM string, or something else if there is
223   none.
224 */
225 SCM
226 default_child_context_name (Translator_group const *tg)
227 {
228   return gh_pair_p (tg->accepts_list_)
229     ? ly_car (scm_last_pair (tg->accepts_list_))
230     : SCM_EOL;
231 }
232
233
234 bool
235 Translator_group::is_bottom_context () const
236 {
237   return !gh_symbol_p (default_child_context_name (this));
238 }
239
240 Translator_group*
241 Translator_group::get_default_interpreter ()
242 {
243   if (!is_bottom_context ())
244     {
245       SCM nm = default_child_context_name (this);
246       SCM st = get_output_def ()->find_translator (nm);
247
248       Context_def *t = unsmob_context_def (st);
249       if (!t)
250         {
251           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
252           t = unsmob_context_def (this->definition_);
253         }
254       Translator_group *tg = t->instantiate (output_def_, SCM_EOL);
255       add_fresh_group_translator (tg);
256
257       if (!tg->is_bottom_context ())
258         return tg->get_default_interpreter ();
259       else
260         return tg;
261     }
262   return this;
263 }
264
265 static void
266 static_each (SCM list, Method_pointer method)
267 {
268   for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
269     (unsmob_translator (ly_car (p))->*method) ();
270   
271 }
272
273 void
274 Translator_group::each (Method_pointer method) 
275 {
276   static_each (get_simple_trans_list (), method);
277   static_each (trans_group_list_, method);
278 }
279
280
281 /*
282   PROPERTIES
283  */
284 Translator_group*
285 Translator_group::where_defined (SCM sym) const
286 {
287   if (properties_dict ()->contains (sym))
288     {
289       return (Translator_group*)this;
290     }
291
292   return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
293 }
294
295 /*
296   return SCM_EOL when not found.
297 */
298 SCM
299 Translator_group::internal_get_property (SCM sym) const
300 {
301   SCM val =SCM_EOL;
302   if (properties_dict ()->try_retrieve (sym, &val))
303     return val;
304
305   if (daddy_trans_)
306     return daddy_trans_->internal_get_property (sym);
307   
308   return val;
309 }
310
311 void
312 Translator_group::internal_set_property (SCM sym, SCM val)
313 {
314 #ifndef NDEBUG
315   if (internal_type_checking_global_b)
316     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
317 #endif
318   
319   properties_dict ()->set (sym, val);
320 }
321
322 /*
323   TODO: look up to check whether we have inherited var? 
324  */
325 void
326 Translator_group::unset_property (SCM sym)
327 {
328   properties_dict ()->remove (sym);
329 }
330
331
332 /*
333   Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
334   entry from a translator property list by name of PROP
335 */
336 void
337 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
338 {
339   if (gh_symbol_p (prop))
340     {
341       if (val != SCM_UNDEFINED)
342         {
343           SCM prev = internal_get_property (prop);
344
345           if (gh_pair_p (prev) || prev == SCM_EOL)
346             {
347               bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
348               
349               if (ok)
350                 {
351                   prev = gh_cons (gh_cons (eltprop, val), prev);
352                   internal_set_property (prop, prev);
353                 }
354             }
355           else
356             {
357               // warning here.
358             }
359           
360         }
361       else
362         {
363           SCM prev = internal_get_property (prop);
364
365           /*
366             TODO: should have scm_equal_something () for reverting
367             autobeam properties.
368            */
369           SCM newprops= SCM_EOL ;
370           while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop))
371             {
372               newprops = gh_cons (ly_car (prev), newprops);
373               prev = ly_cdr (prev);
374             }
375           
376           if (gh_pair_p (prev))
377             {
378               newprops = scm_reverse_x (newprops, ly_cdr (prev));
379               internal_set_property (prop, newprops);
380             }
381         }
382     }
383 }
384
385
386
387 /*
388   STUBS
389 */
390 void
391 Translator_group::stop_translation_timestep ()
392 {
393   each (&Translator::stop_translation_timestep);
394 }
395
396 void
397 Translator_group::start_translation_timestep ()
398 {
399   each (&Translator::start_translation_timestep);
400 }
401
402 void
403 Translator_group::do_announces ()
404 {
405   each (&Translator::do_announces);
406 }
407
408 void
409 Translator_group::initialize ()
410 {
411   SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
412   set_property ("acceptHashTable", tab);
413   each (&Translator::initialize);
414 }
415
416 void
417 Translator_group::finalize ()
418 {
419   each (&Translator::finalize);
420 }
421
422 bool
423 translator_accepts_any_of (Translator*tr, SCM ifaces)
424 {
425   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
426                            tr->translator_description());
427   ack_ifs = gh_cdr (ack_ifs);
428   for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
429     if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
430       return true;
431   return false;
432 }
433
434 SCM
435 find_accept_translators (SCM gravlist, SCM ifaces)
436 {
437   SCM l = SCM_EOL;
438   for (SCM s = gravlist; ly_pair_p (s);  s = ly_cdr (s))
439     {
440       Translator* tr = unsmob_translator (ly_car (s));
441       if (translator_accepts_any_of (tr, ifaces))
442         l = scm_cons (tr->self_scm (), l); 
443     }
444   l = scm_reverse_x (l, SCM_EOL);
445
446   return l;
447 }
448
449 bool
450 Translator_group::try_music_on_nongroup_children (Music *m )
451 {
452   SCM tab = get_property ("acceptHashTable");
453   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
454                               m->get_property_alist (false));
455
456   if (!gh_pair_p (name))
457     return false;
458
459   name = gh_cdr (name);
460   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
461   if (accept_list == SCM_BOOL_F)
462     {
463       accept_list = find_accept_translators (get_simple_trans_list (),
464                                              m->get_mus_property ("types"));
465       scm_hashq_set_x (tab, name, accept_list);
466     }
467
468   for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
469     {
470       Translator * t = unsmob_translator (ly_car (p));
471       if (t && t->try_music (m))
472         return true;
473     }
474   return false;
475 }
476
477 SCM
478 Translator_group::properties_as_alist () const
479 {
480   return properties_dict()->to_alist();
481 }
482
483 String
484 Translator_group::context_name () const
485 {
486   Context_def * td = unsmob_context_def (definition_ );
487   return ly_symbol2string (td->get_context_name ());
488 }
489
490 /*
491   PRE_INIT_OPS is in the order specified, and hence must be reversed.
492  */
493 void
494 apply_property_operations (Translator_group*tg, SCM pre_init_ops)
495 {
496   SCM correct_order = scm_reverse (pre_init_ops);
497   for (SCM s = correct_order; gh_pair_p (s); s = ly_cdr (s))
498     {
499       SCM entry = ly_car (s);
500       SCM type = ly_car (entry);
501       entry = ly_cdr (entry); 
502       
503       if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush"))
504         {
505           SCM val = ly_cddr (entry);
506           val = gh_pair_p (val) ? ly_car (val) : SCM_UNDEFINED;
507
508           tg->execute_pushpop_property (ly_car (entry), ly_cadr (entry), val);
509         }
510       else if (type == ly_symbol2scm ("assign"))
511         {
512           tg->internal_set_property (ly_car (entry), ly_cadr (entry));
513         }
514     }
515 }
516
517 SCM
518 names_to_translators (SCM namelist, Translator_group*tg)
519 {
520   SCM l = SCM_EOL;
521   for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s))
522     {
523       Translator * t = get_translator (ly_car (s));
524       if (!t)
525         warning (_f ("can't find: `%s'", s));
526       else
527         {
528           Translator * tr = t->clone ();
529           SCM str = tr->self_scm ();
530           l = gh_cons (str, l);
531
532           tr->daddy_trans_ = tg;
533           tr->output_def_  = tg->output_def_;
534
535           scm_gc_unprotect_object (str);
536         }
537     }
538   return l;
539 }
540
541
542 SCM
543 Translator_group::get_simple_trans_list ()
544 {
545   return simple_trans_list_;
546
547 }
548
549
550   
551 #if 0
552 SCM
553 Translator_group::get_simple_trans_list ()
554 {
555   if (simple_trans_list_ != SCM_BOOL_F)
556     return simple_trans_list_;
557   
558   Context_def * td = unsmob_context_def (definition_);
559
560   /*
561     The following cannot work, since start_translation_timestep ()
562     triggers this code, and start_translation_timestep happens before
563     \property Voice.Voice =#'()
564     
565    */
566       trans_names = td->get_translator_names (SCM_EOL); 
567   
568   SCM trans_names = internal_get_property (td->get_context_name ());
569   if (!gh_pair_p (trans_names))
570     {
571     }
572
573   simple_trans_list_ = names_to_translators (trans_names, this);
574
575   
576   static_each (simple_trans_list_, &Translator::initialize);
577   return simple_trans_list_;
578 }
579 #endif