2 Translator_group.cc -- implement Translator_group
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
14 #include "scm-hash.hh"
15 #include "context-def.hh"
19 Translator_group::Translator_group (Translator_group const&s)
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 ());
30 Translator_group::properties_dict () const
32 return Scheme_hash_table::unsmob (properties_scm_);
35 Translator_group::~Translator_group ()
38 //assert (is_removable ());
42 Translator_group::Translator_group ()
45 Scheme_hash_table *tab = new Scheme_hash_table ;
46 properties_scm_ = tab->self_scm ();
48 scm_gc_unprotect_object (tab->self_scm ());
52 Translator_group::check_removal ()
55 for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
59 Translator_group *trg = dynamic_cast<Translator_group*> (unsmob_translator (ly_car (p)));
61 trg->check_removal ();
62 if (trg->is_removable ())
63 terminate_translator (trg);
68 Translator_group::add_translator (SCM list, Translator *t)
71 Must append, since list ordering must be preserved.
73 list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL));
74 t->daddy_trans_ = this;
75 t->output_def_ = output_def_;
82 Translator_group::add_used_group_translator (Translator *t)
84 trans_group_list_ = add_translator (trans_group_list_,t);
89 Translator_group::add_fresh_group_translator (Translator*t)
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 ());
95 Context_def * td = unsmob_context_def (tg->definition_);
98 this can not move before add_translator(), because \override
99 operations require that we are in the hierarchy.
101 td->apply_default_property_operations (tg);
107 Translator_group::is_removable () const
109 return trans_group_list_ == SCM_EOL && ! iterator_count_;
113 Translator_group::find_existing_translator (SCM n, String id)
115 if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
118 Translator_group* r = 0;
119 for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
121 Translator * t = unsmob_translator (ly_car (p));
123 r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id); }
130 Translator_group::find_create_translator (SCM n, String id, SCM operations)
132 Translator_group * existing = find_existing_translator (n,id);
138 TODO: use accepts_list_.
140 Link_array<Context_def> path
141 = unsmob_context_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
145 Translator_group * current = this;
147 // start at 1. The first one (index 0) will be us.
148 for (int i=0; i < path.size (); i++)
150 SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
152 Translator_group * new_group
153 = path[i]->instantiate (output_def_, ops);
155 if (i == path.size () -1)
157 new_group->id_string_ = id;
160 current->add_fresh_group_translator (new_group);
161 apply_property_operations (new_group, ops);
169 Translator_group *ret = 0;
171 ret = daddy_trans_->find_create_translator (n, id, operations);
174 warning (_f ("Cannot find or create `%s' called `%s'",
175 ly_symbol2string (n).to_str0 (), id));
182 Translator_group::try_music (Music* m)
184 bool hebbes_b = try_music_on_nongroup_children (m);
186 if (!hebbes_b && daddy_trans_)
187 hebbes_b = daddy_trans_->try_music (m);
193 Translator_group::get_depth () const
195 return (daddy_trans_) ? daddy_trans_->get_depth () + 1 : 0;
199 Translator_group::terminate_translator (Translator*r)
203 Return value ignored. GC does the rest.
205 remove_translator (r);
210 Remove a translator from the hierarchy.
213 Translator_group::remove_translator (Translator*trans)
217 trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
218 trans->daddy_trans_ = 0;
224 Default child context as a SCM string, or something else if there is
228 default_child_context_name (Translator_group const *tg)
230 return gh_pair_p (tg->accepts_list_)
231 ? ly_car (scm_last_pair (tg->accepts_list_))
237 Translator_group::is_bottom_context () const
239 return !gh_symbol_p (default_child_context_name (this));
243 Translator_group::get_default_interpreter ()
245 if (!is_bottom_context ())
247 SCM nm = default_child_context_name (this);
248 SCM st = get_output_def ()->find_translator (nm);
250 Context_def *t = unsmob_context_def (st);
253 warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
254 t = unsmob_context_def (this->definition_);
256 Translator_group *tg = t->instantiate (output_def_, SCM_EOL);
257 add_fresh_group_translator (tg);
258 if (!tg->is_bottom_context ())
259 return tg->get_default_interpreter ();
267 static_each (SCM list, Method_pointer method)
269 for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
270 (unsmob_translator (ly_car (p))->*method) ();
275 Translator_group::each (Method_pointer method)
277 static_each (get_simple_trans_list (), method);
278 static_each (trans_group_list_, method);
286 Translator_group::where_defined (SCM sym) const
288 if (properties_dict ()->contains (sym))
290 return (Translator_group*)this;
293 return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
297 return SCM_EOL when not found.
300 Translator_group::internal_get_property (SCM sym) const
303 if (properties_dict ()->try_retrieve (sym, &val))
307 return daddy_trans_->internal_get_property (sym);
313 Translator_group::internal_set_property (SCM sym, SCM val)
316 if (internal_type_checking_global_b)
317 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
320 properties_dict ()->set (sym, val);
324 TODO: look up to check whether we have inherited var?
327 Translator_group::unset_property (SCM sym)
329 properties_dict ()->remove (sym);
334 Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
335 entry from a translator property list by name of PROP
338 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
340 if (gh_symbol_p (prop))
342 if (val != SCM_UNDEFINED)
344 SCM prev = internal_get_property (prop);
346 if (gh_pair_p (prev) || prev == SCM_EOL)
348 bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
352 prev = gh_cons (gh_cons (eltprop, val), prev);
353 internal_set_property (prop, prev);
364 SCM prev = internal_get_property (prop);
367 TODO: should have scm_equal_something () for reverting
370 SCM newprops= SCM_EOL ;
371 while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop))
373 newprops = gh_cons (ly_car (prev), newprops);
374 prev = ly_cdr (prev);
377 if (gh_pair_p (prev))
379 newprops = scm_reverse_x (newprops, ly_cdr (prev));
380 internal_set_property (prop, newprops);
392 Translator_group::stop_translation_timestep ()
394 each (&Translator::stop_translation_timestep);
398 Translator_group::start_translation_timestep ()
400 each (&Translator::start_translation_timestep);
404 Translator_group::do_announces ()
406 each (&Translator::do_announces);
410 Translator_group::initialize ()
412 SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
413 set_property ("acceptHashTable", tab);
414 each (&Translator::initialize);
418 Translator_group::finalize ()
420 each (&Translator::finalize);
424 translator_accepts_any_of (Translator*tr, SCM ifaces)
426 SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
427 tr->translator_description());
428 ack_ifs = gh_cdr (ack_ifs);
429 for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
430 if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
436 find_accept_translators (SCM gravlist, SCM ifaces)
439 for (SCM s = gravlist; ly_pair_p (s); s = ly_cdr (s))
441 Translator* tr = unsmob_translator (ly_car (s));
442 if (translator_accepts_any_of (tr, ifaces))
443 l = scm_cons (tr->self_scm (), l);
445 l = scm_reverse_x (l, SCM_EOL);
451 Translator_group::try_music_on_nongroup_children (Music *m )
453 SCM tab = get_property ("acceptHashTable");
454 SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
455 m->get_property_alist (false));
457 if (!gh_pair_p (name))
460 name = gh_cdr (name);
461 SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
462 if (accept_list == SCM_BOOL_F)
464 accept_list = find_accept_translators (get_simple_trans_list (),
465 m->get_mus_property ("types"));
466 scm_hashq_set_x (tab, name, accept_list);
469 for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
471 Translator * t = unsmob_translator (ly_car (p));
472 if (t && t->try_music (m))
479 Translator_group::properties_as_alist () const
481 return properties_dict()->to_alist();
485 Translator_group::context_name () const
487 Context_def * td = unsmob_context_def (definition_ );
488 return ly_symbol2string (td->get_context_name ());
492 PRE_INIT_OPS is in the order specified, and hence must be reversed.
495 apply_property_operations (Translator_group*tg, SCM pre_init_ops)
497 SCM correct_order = scm_reverse (pre_init_ops);
498 for (SCM s = correct_order; gh_pair_p (s); s = ly_cdr (s))
500 SCM entry = ly_car (s);
501 SCM type = ly_car (entry);
502 entry = ly_cdr (entry);
504 if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush"))
506 SCM val = ly_cddr (entry);
507 val = gh_pair_p (val) ? ly_car (val) : SCM_UNDEFINED;
509 tg->execute_pushpop_property (ly_car (entry), ly_cadr (entry), val);
511 else if (type == ly_symbol2scm ("assign"))
513 tg->internal_set_property (ly_car (entry), ly_cadr (entry));
519 names_to_translators (SCM namelist, Translator_group*tg)
522 for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s))
524 Translator * t = get_translator (ly_car (s));
526 warning (_f ("can't find: `%s'", s));
529 Translator * tr = t->clone ();
530 SCM str = tr->self_scm ();
531 l = gh_cons (str, l);
533 tr->daddy_trans_ = tg;
534 tr->output_def_ = tg->output_def_;
536 scm_gc_unprotect_object (str);
544 Translator_group::get_simple_trans_list ()
546 return simple_trans_list_;
554 Translator_group::get_simple_trans_list ()
556 if (simple_trans_list_ != SCM_BOOL_F)
557 return simple_trans_list_;
559 Context_def * td = unsmob_context_def (definition_);
562 The following cannot work, since start_translation_timestep ()
563 triggers this code, and start_translation_timestep happens before
564 \property Voice.Voice =#'()
567 trans_names = td->get_translator_names (SCM_EOL);
569 SCM trans_names = internal_get_property (td->get_context_name ());
570 if (!gh_pair_p (trans_names))
574 simple_trans_list_ = names_to_translators (trans_names, this);
577 static_each (simple_trans_list_, &Translator::initialize);
578 return simple_trans_list_;