*** empty log message ***
[lilypond.git] / lily / translator-group.cc
blobac4e97eb2a5ec8312b9a6b672bcd2602715e619e
1 /*
2 Translator_group.cc -- implement Translator_group
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
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"
19 Translator_group::Translator_group (Translator_group const&s)
20 : Translator (s)
22 iterator_count_ =0;
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 ());
29 Scheme_hash_table*
30 Translator_group::properties_dict () const
32 return Scheme_hash_table::unsmob (properties_scm_);
35 Translator_group::~Translator_group ()
38 //assert (removable_b ());
42 Translator_group::Translator_group ()
44 iterator_count_ = 0;
45 Scheme_hash_table *tab = new Scheme_hash_table ;
46 properties_scm_ = tab->self_scm ();
48 scm_gc_unprotect_object (tab->self_scm ());
51 void
52 Translator_group::check_removal ()
54 SCM next = SCM_EOL;
55 for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
57 next = ly_cdr (p);
59 Translator_group *trg = dynamic_cast<Translator_group*> (unsmob_translator (ly_car (p)));
61 trg->check_removal ();
62 if (trg->removable_b ())
63 terminate_translator (trg);
67 SCM
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_;
77 return list;
81 void
82 Translator_group::add_used_group_translator (Translator *t)
84 trans_group_list_ = add_translator (trans_group_list_,t);
88 void
89 Translator_group::add_fresh_group_translator (Translator*t)
91 Translator_group*tg = dynamic_cast<Translator_group*> (t);
92 assert (tg);
94 trans_group_list_ = add_translator (trans_group_list_,t);
95 unsmob_translator_def (tg->definition_)->apply_property_operations (tg);
96 t->initialize ();
101 bool
102 Translator_group::removable_b () const
104 return trans_group_list_ == SCM_EOL && ! iterator_count_;
107 Translator_group *
108 Translator_group::find_existing_translator (SCM n, String id)
110 if ((is_alias_b (n) && (id_string_ == id || id.empty_b ())) || n == ly_symbol2scm ("Current"))
111 return this;
113 Translator_group* r = 0;
114 for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
116 Translator * t = unsmob_translator (ly_car (p));
118 r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id);
121 return r;
125 Translator_group*
126 Translator_group::find_create_translator (SCM n, String id)
128 Translator_group * existing = find_existing_translator (n,id);
129 if (existing)
130 return existing;
132 Link_array<Translator_def> path
133 = unsmob_translator_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
135 if (path.size ())
137 Translator_group * current = this;
139 // start at 1. The first one (index 0) will be us.
140 for (int i=0; i < path.size (); i++)
142 Translator_group * new_group = path[i]->instantiate (output_def_);
144 if (i == path.size () -1)
145 new_group->id_string_ = id;
146 current->add_fresh_group_translator (new_group);
147 current = new_group;
150 return current;
153 Translator_group *ret = 0;
154 if (daddy_trans_)
155 ret = daddy_trans_->find_create_translator (n,id);
156 else
158 warning (_f ("can't find or create `%s' called `%s'", ly_symbol2string (n).to_str0 (), id));
159 ret =0;
161 return ret;
164 bool
165 Translator_group::try_music (Music* m)
167 bool hebbes_b = try_music_on_nongroup_children (m);
169 if (!hebbes_b && daddy_trans_)
170 hebbes_b = daddy_trans_->try_music (m);
171 return hebbes_b ;
175 Translator_group::get_depth () const
177 return (daddy_trans_) ? daddy_trans_->get_depth () + 1 : 0;
180 Translator_group*
181 Translator_group::get_ancestor (int level)
183 if (!level || !daddy_trans_)
184 return this;
186 return daddy_trans_->get_ancestor (level-1);
189 void
190 Translator_group::terminate_translator (Translator*r)
192 r->removal_processing ();
194 Return value ignored. GC does the rest.
196 remove_translator (r);
201 Remove a translator from the hierarchy.
203 Translator *
204 Translator_group::remove_translator (Translator*trans)
206 assert (trans);
208 trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
209 trans->daddy_trans_ = 0;
210 return trans;
213 bool
214 Translator_group::is_bottom_translator_b () const
216 return !gh_symbol_p (unsmob_translator_def (definition_)->default_child_context_name ());
219 Translator_group*
220 Translator_group::get_default_interpreter ()
222 if (!is_bottom_translator_b ())
224 SCM nm = unsmob_translator_def (definition_)->default_child_context_name ();
225 SCM st = get_output_def ()->find_translator (nm);
227 Translator_def *t = unsmob_translator_def (st);
228 if (!t)
230 warning (_f ("can't find or create: `%s'", ly_scm2string (nm).to_str0 ()));
231 t = unsmob_translator_def (this->definition_);
233 Translator_group *tg = t->instantiate (output_def_);
234 add_fresh_group_translator (tg);
236 if (!tg->is_bottom_translator_b ())
237 return tg->get_default_interpreter ();
238 else
239 return tg;
241 return this;
244 static void
245 static_each (SCM list, Method_pointer method)
247 for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
248 (unsmob_translator (ly_car (p))->*method) ();
252 void
253 Translator_group::each (Method_pointer method)
255 static_each (simple_trans_list_, method);
256 static_each (trans_group_list_, method);
261 PROPERTIES
263 Translator_group*
264 Translator_group::where_defined (SCM sym) const
266 if (properties_dict ()->elem_b (sym))
268 return (Translator_group*)this;
271 return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
275 return SCM_EOL when not found.
278 Translator_group::internal_get_property (SCM sym) const
280 SCM val =SCM_EOL;
281 if (properties_dict ()->try_retrieve (sym, &val))
282 return val;
284 if (daddy_trans_)
285 return daddy_trans_->internal_get_property (sym);
287 return val;
290 void
291 Translator_group::internal_set_property (SCM sym, SCM val)
293 #ifndef NDEBUG
294 if (internal_type_checking_global_b)
295 assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
296 #endif
298 properties_dict ()->set (sym, val);
302 TODO: look up to check whether we have inherited var?
304 void
305 Translator_group::unset_property (SCM sym)
307 properties_dict ()->remove (sym);
312 Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
313 entry from a translator property list by name of PROP
315 void
316 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
318 if (gh_symbol_p (prop))
320 if (val != SCM_UNDEFINED)
322 SCM prev = internal_get_property (prop);
324 if (gh_pair_p (prev) || prev == SCM_EOL)
326 bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
328 if (ok)
330 prev = gh_cons (gh_cons (eltprop, val), prev);
331 internal_set_property (prop, prev);
334 else
336 // warning here.
340 else
342 SCM prev = internal_get_property (prop);
344 SCM newprops= SCM_EOL ;
345 while (gh_pair_p (prev) && ly_caar (prev) != eltprop)
347 newprops = gh_cons (ly_car (prev), newprops);
348 prev = ly_cdr (prev);
351 if (gh_pair_p (prev))
353 newprops = scm_reverse_x (newprops, ly_cdr (prev));
354 internal_set_property (prop, newprops);
363 STUBS
365 void
366 Translator_group::stop_translation_timestep ()
368 each (&Translator::stop_translation_timestep);
371 void
372 Translator_group::start_translation_timestep ()
374 each (&Translator::start_translation_timestep);
377 void
378 Translator_group::do_announces ()
380 each (&Translator::do_announces);
383 void
384 Translator_group::initialize ()
386 SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
387 set_property ("acceptHashTable", tab);
388 each (&Translator::initialize);
391 void
392 Translator_group::finalize ()
394 each (&Translator::removal_processing);
399 bool translator_accepts_any_of (Translator*tr, SCM ifaces)
401 SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
402 tr->translator_description());
403 ack_ifs = gh_cdr (ack_ifs);
404 for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
405 if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
406 return true;
407 return false;
411 find_accept_translators (SCM gravlist, SCM ifaces)
413 SCM l = SCM_EOL;
414 for (SCM s = gravlist; ly_pair_p (s); s = ly_cdr (s))
416 Translator* tr = unsmob_translator (ly_car (s));
417 if (translator_accepts_any_of (tr, ifaces))
418 l = scm_cons (tr->self_scm (), l);
420 l = scm_reverse_x (l, SCM_EOL);
422 return l;
425 bool
426 Translator_group::try_music_on_nongroup_children (Music *m )
428 SCM tab = get_property ("acceptHashTable");
429 SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
430 m->get_property_alist (false));
432 if (!gh_pair_p (name))
433 return false;
435 name = gh_cdr (name);
436 SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
437 if (accept_list == SCM_BOOL_F)
439 accept_list = find_accept_translators (simple_trans_list_,
440 m->get_mus_property ("types"));
441 scm_hashq_set_x (tab, name, accept_list);
444 for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
446 Translator * t = unsmob_translator (ly_car (p));
447 if (t && t->try_music (m))
448 return true;
450 return false;
454 Translator_group::properties_as_alist () const
456 return properties_dict()->to_alist();
459 String
460 Translator_group::context_name () const
462 Translator_def * td = unsmob_translator_def (definition_ );
463 return ly_symbol2string (td->type_name_);