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