]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
* lily/include/scm-hash.hh (class Scheme_hash_table): idem.
[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 "translator-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   assert (tg);
93
94   trans_group_list_ = add_translator (trans_group_list_,t); 
95   unsmob_translator_def (tg->definition_)->apply_property_operations (tg);
96   t->initialize ();
97   
98 }
99
100
101 bool
102 Translator_group::is_removable () const
103 {
104   return trans_group_list_ == SCM_EOL && ! iterator_count_;
105 }
106
107 Translator_group *
108 Translator_group::find_existing_translator (SCM n, String id)
109 {
110   if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
111     return this;
112
113   Translator_group* r = 0;
114   for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
115     {
116       Translator *  t = unsmob_translator (ly_car (p));
117       
118       r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id);
119     }
120
121   return r;
122 }
123
124
125 Translator_group*
126 Translator_group::find_create_translator (SCM n, String id)
127 {
128   Translator_group * existing = find_existing_translator (n,id);
129   if (existing)
130     return existing;
131
132   Link_array<Translator_def> path
133     = unsmob_translator_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
134
135   if (path.size ())
136     {
137       Translator_group * current = this;
138
139       // start at 1.  The first one (index 0) will be us.
140       for (int i=0; i < path.size (); i++)
141         {
142           Translator_group * new_group = path[i]->instantiate (output_def_);
143
144           if (i == path.size () -1)
145             new_group->id_string_ = id;
146           current->add_fresh_group_translator (new_group);
147           current = new_group;
148         }
149
150       return current;
151     }
152
153   Translator_group *ret = 0;
154   if (daddy_trans_)
155     ret = daddy_trans_->find_create_translator (n,id);
156   else
157     {
158       warning (_f ("can't find or create `%s' called `%s'", ly_symbol2string (n).to_str0 (), id));
159       ret =0;
160     }
161   return ret;
162 }
163
164 bool
165 Translator_group::try_music (Music* m)
166 {
167   bool hebbes_b = try_music_on_nongroup_children (m);
168   
169   if (!hebbes_b && daddy_trans_)
170     hebbes_b = daddy_trans_->try_music (m);
171   
172   return hebbes_b ;
173 }
174
175 int
176 Translator_group::get_depth () const
177 {
178   return (daddy_trans_) ? daddy_trans_->get_depth ()  + 1 : 0;
179 }
180
181 Translator_group*
182 Translator_group::get_ancestor (int level)
183 {
184   if (!level || !daddy_trans_)
185     return this;
186
187   return daddy_trans_->get_ancestor (level-1);
188 }
189
190 void
191 Translator_group::terminate_translator (Translator*r)
192 {
193   r->finalize ();
194   /*
195     Return value ignored. GC does the rest.
196    */
197   remove_translator (r);
198 }
199
200
201 /**
202    Remove a translator from the hierarchy.
203  */
204 Translator *
205 Translator_group::remove_translator (Translator*trans)
206 {
207   assert (trans);
208
209   trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
210   trans->daddy_trans_ = 0;
211   return trans;
212 }
213
214 bool
215 Translator_group::is_bottom_translator_b () const
216 {
217   return !gh_symbol_p (unsmob_translator_def (definition_)->default_child_context_name ());
218 }
219
220 Translator_group*
221 Translator_group::get_default_interpreter ()
222 {
223   if (!is_bottom_translator_b ())
224     {
225       SCM nm = unsmob_translator_def (definition_)->default_child_context_name ();
226       SCM st = get_output_def ()->find_translator (nm);
227
228       Translator_def *t = unsmob_translator_def (st);
229       if (!t)
230         {
231           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
232           t = unsmob_translator_def (this->definition_);
233         }
234       Translator_group *tg = t->instantiate (output_def_);
235       add_fresh_group_translator (tg);
236
237       if (!tg->is_bottom_translator_b ())
238         return tg->get_default_interpreter ();
239       else
240         return tg;
241     }
242   return this;
243 }
244
245 static void
246 static_each (SCM list, Method_pointer method)
247 {
248   for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
249     (unsmob_translator (ly_car (p))->*method) ();
250   
251 }
252
253 void
254 Translator_group::each (Method_pointer method) 
255 {
256   static_each (simple_trans_list_, method);
257   static_each (trans_group_list_, method);
258 }
259
260
261 /*
262   PROPERTIES
263  */
264 Translator_group*
265 Translator_group::where_defined (SCM sym) const
266 {
267   if (properties_dict ()->contains (sym))
268     {
269       return (Translator_group*)this;
270     }
271
272   return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
273 }
274
275 /*
276   return SCM_EOL when not found.
277 */
278 SCM
279 Translator_group::internal_get_property (SCM sym) const
280 {
281   SCM val =SCM_EOL;
282   if (properties_dict ()->try_retrieve (sym, &val))
283     return val;
284
285   if (daddy_trans_)
286     return daddy_trans_->internal_get_property (sym);
287   
288   return val;
289 }
290
291 void
292 Translator_group::internal_set_property (SCM sym, SCM val)
293 {
294 #ifndef NDEBUG
295   if (internal_type_checking_global_b)
296     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
297 #endif
298   
299   properties_dict ()->set (sym, val);
300 }
301
302 /*
303   TODO: look up to check whether we have inherited var? 
304  */
305 void
306 Translator_group::unset_property (SCM sym)
307 {
308   properties_dict ()->remove (sym);
309 }
310
311
312 /*
313   Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
314   entry from a translator property list by name of PROP
315 */
316 void
317 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
318 {
319   if (gh_symbol_p (prop))
320     {
321       if (val != SCM_UNDEFINED)
322         {
323           SCM prev = internal_get_property (prop);
324
325           if (gh_pair_p (prev) || prev == SCM_EOL)
326             {
327               bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
328               
329               if (ok)
330                 {
331                   prev = gh_cons (gh_cons (eltprop, val), prev);
332                   internal_set_property (prop, prev);
333                 }
334             }
335           else
336             {
337               // warning here.
338             }
339           
340         }
341       else
342         {
343           SCM prev = internal_get_property (prop);
344
345           /*
346             TODO: should have scm_equal_something () for reverting
347             autobeam properties.
348            */
349           SCM newprops= SCM_EOL ;
350           while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop))
351             {
352               newprops = gh_cons (ly_car (prev), newprops);
353               prev = ly_cdr (prev);
354             }
355           
356           if (gh_pair_p (prev))
357             {
358               newprops = scm_reverse_x (newprops, ly_cdr (prev));
359               internal_set_property (prop, newprops);
360             }
361         }
362     }
363 }
364
365
366
367 /*
368   STUBS
369 */
370 void
371 Translator_group::stop_translation_timestep ()
372 {
373   each (&Translator::stop_translation_timestep);
374 }
375
376 void
377 Translator_group::start_translation_timestep ()
378 {
379   each (&Translator::start_translation_timestep);
380 }
381
382 void
383 Translator_group::do_announces ()
384 {
385   each (&Translator::do_announces);
386 }
387
388 void
389 Translator_group::initialize ()
390 {
391   SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
392   set_property ("acceptHashTable", tab);
393   each (&Translator::initialize);
394 }
395
396 void
397 Translator_group::finalize ()
398 {
399   each (&Translator::finalize);
400 }
401
402
403
404 bool translator_accepts_any_of (Translator*tr, SCM ifaces)
405 {
406   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
407                            tr->translator_description());
408   ack_ifs = gh_cdr (ack_ifs);
409   for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
410     if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
411       return true;
412   return false;
413 }
414
415 SCM
416 find_accept_translators (SCM gravlist, SCM ifaces)
417 {
418   SCM l = SCM_EOL;
419   for (SCM s = gravlist; ly_pair_p (s);  s = ly_cdr (s))
420     {
421       Translator* tr = unsmob_translator (ly_car (s));
422       if (translator_accepts_any_of (tr, ifaces))
423         l = scm_cons (tr->self_scm (), l); 
424     }
425   l = scm_reverse_x (l, SCM_EOL);
426
427   return l;
428 }
429
430 bool
431 Translator_group::try_music_on_nongroup_children (Music *m )
432 {
433   SCM tab = get_property ("acceptHashTable");
434   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
435                               m->get_property_alist (false));
436
437   if (!gh_pair_p (name))
438     return false;
439
440   name = gh_cdr (name);
441   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
442   if (accept_list == SCM_BOOL_F)
443     {
444       accept_list = find_accept_translators (simple_trans_list_,
445                                              m->get_mus_property ("types"));
446       scm_hashq_set_x (tab, name, accept_list);
447     }
448
449   for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
450     {
451       Translator * t = unsmob_translator (ly_car (p));
452       if (t && t->try_music (m))
453         return true;
454     }
455   return false;
456 }
457
458 SCM
459 Translator_group::properties_as_alist () const
460 {
461   return properties_dict()->to_alist();
462 }
463
464 String
465 Translator_group::context_name () const
466 {
467   Translator_def * td = unsmob_translator_def (definition_ );
468   return ly_symbol2string (td->type_name_);
469 }