2 Translator_group.cc -- implement Translator_group
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
14 #include "dictionary-iter.hh"
16 #include "killing-cons.tcc"
18 Translator_group::Translator_group (Translator_group const&s)
21 consists_str_arr_ = s.consists_str_arr_;
22 consists_end_str_arr_ = s.consists_end_str_arr_;
23 accepts_str_arr_ = s.accepts_str_arr_;
25 properties_dict_ = s.properties_dict_;
28 Translator_group::~Translator_group ()
30 assert (removable_b());
31 trans_p_list_.junk ();
35 Translator_group::Translator_group()
41 Translator_group::check_removal()
43 Cons<Translator> *next =0;
44 for (Cons<Translator> *p = trans_p_list_.head_; p; p = next)
47 if (Translator_group *trg = dynamic_cast <Translator_group *> (p->car_))
49 trg->check_removal ();
50 if (trg->removable_b())
51 terminate_translator (trg);
57 Translator_group::add_translator (Translator *trans_p)
59 trans_p_list_.append (new Killing_cons<Translator> (trans_p,0));
61 trans_p->daddy_trans_l_ = this;
62 trans_p->output_def_l_ = output_def_l_;
63 trans_p->add_processing ();
67 Translator_group::set_acceptor (String accepts, bool add)
70 accepts_str_arr_.push (accepts);
72 for (int i=accepts_str_arr_.size (); i--; )
73 if (accepts_str_arr_[i] == accepts)
74 accepts_str_arr_.del (i);
78 Translator_group::add_last_element (String s)
80 if (!get_translator_l (s))
81 error (_ ("Program has no such type"));
83 for (int i=consists_end_str_arr_.size (); i--; )
84 if (consists_end_str_arr_[i] == s)
85 warning (_f ("Already contains: `%s'", s));
87 consists_end_str_arr_.push (s);
91 Translator_group::set_element (String s, bool add)
93 if (!get_translator_l (s))
94 error (_ ("Program has no such type"));
98 for (int i=consists_str_arr_.size (); i--; )
99 if (consists_str_arr_[i] == s)
100 warning (_f("Already contains: `%s'", s));
102 consists_str_arr_.push (s);
106 for (int i=consists_str_arr_.size (); i--; )
107 if (consists_str_arr_[i] == s)
108 consists_str_arr_.del (i);
109 for (int i=consists_end_str_arr_.size (); i--; )
110 if (consists_end_str_arr_[i] == s)
111 consists_end_str_arr_.del (i);
115 Translator_group::removable_b() const
117 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
119 if (dynamic_cast <Translator_group *> (p->car_))
123 return !iterator_count_;
127 Translator_group::find_existing_translator_l (String n, String id)
129 if (is_alias_b (n) && (id_str_ == id || id.empty_b ()))
133 Translator_group* r = 0;
134 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
136 if (Translator_group *trg = dynamic_cast <Translator_group *> (p->car_))
137 r = trg->find_existing_translator_l (n, id);
143 Link_array<Translator_group>
144 Translator_group::path_to_acceptable_translator (String type) const
146 Link_array<Translator_group> accepted_arr;
147 for (int i=0; i < accepts_str_arr_.size (); i++)
149 Translator *t = output_def_l ()->find_translator_l (accepts_str_arr_[i]);
150 if (!t || !dynamic_cast <Translator_group *> (t))
152 accepted_arr.push (dynamic_cast <Translator_group *> (t));
156 for (int i=0; i < accepted_arr.size (); i++)
157 if (accepted_arr[i]->type_str_ == type)
159 Link_array<Translator_group> retval;
160 retval.push (accepted_arr[i]);
164 Link_array<Translator_group> best_result;
165 int best_depth= INT_MAX;
166 for (int i=0; i < accepted_arr.size (); i++)
168 Translator_group * g = accepted_arr[i];
170 Link_array<Translator_group> result
171 = g->path_to_acceptable_translator (type);
172 if (result.size () && result.size () < best_depth)
175 best_result = result;
183 Translator_group::find_create_translator_l (String n, String id)
185 Translator_group * existing = find_existing_translator_l (n,id);
189 Link_array<Translator_group> path = path_to_acceptable_translator (n);
193 Translator_group * current = this;
195 // start at 1. The first one (index 0) will be us.
196 for (int i=0; i < path.size (); i++)
198 Translator_group * new_group = dynamic_cast<Translator_group*>(path[i]->clone ());
199 current->add_translator (new_group);
202 current->id_str_ = id;
206 Translator_group *ret = 0;
208 ret = daddy_trans_l_->find_create_translator_l (n,id);
211 warning (_f ("Can't find or create `%s' called `%s'", n, id));
218 Translator_group::try_music_on_nongroup_children (Music *m)
220 bool hebbes_b =false;
222 for (Cons<Translator> *p = trans_p_list_.head_; !hebbes_b && p; p = p->next_)
224 if (!dynamic_cast <Translator_group *> (p->car_))
226 hebbes_b = p->car_->try_music (m);
233 Translator_group::do_try_music (Music* m)
235 bool hebbes_b = try_music_on_nongroup_children (m);
237 if (!hebbes_b && daddy_trans_l_)
238 hebbes_b = daddy_trans_l_->try_music (m);
243 Translator_group::depth_i() const
245 return (daddy_trans_l_) ? daddy_trans_l_->depth_i() + 1 : 0;
249 Translator_group::ancestor_l (int level)
251 if (!level || !daddy_trans_l_)
254 return daddy_trans_l_->ancestor_l (level-1);
262 Translator_group::terminate_translator (Translator*r_l)
264 DEBUG_OUT << "Removing " << classname (r_l) << " at " << now_mom () << '\n';
265 r_l->removal_processing();
266 Translator * trans_p =remove_translator_p (r_l);
273 Remove a translator from the hierarchy.
276 Translator_group::remove_translator_p (Translator*trans_l)
280 for (Cons<Translator> **pp = &trans_p_list_.head_; *pp; pp = &(*pp)->next_)
281 if ((*pp)->car_ == trans_l)
283 Cons<Translator> *r = trans_p_list_.remove_cons (pp);
285 trans_l->daddy_trans_l_ =0;
295 Translator_group::get_simple_translator (String type) const
297 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
299 if (classname (p->car_) == type)
303 return daddy_trans_l_->get_simple_translator (type);
309 Translator_group::is_bottom_translator_b () const
311 return !accepts_str_arr_.size ();
317 Translator_group::get_default_interpreter()
319 if (accepts_str_arr_.size())
321 Translator*t = output_def_l ()->find_translator_l (accepts_str_arr_[0]);
324 warning (_f ("Can't find or create: `%s'", accepts_str_arr_[0]));
327 Translator_group * g= dynamic_cast <Translator_group*>(t->clone ());
330 if (!g->is_bottom_translator_b ())
331 return g->get_default_interpreter ();
339 Translator_group::each (Method_pointer method)
341 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
342 (p->car_->*method) ();
347 Translator_group::each (Const_method_pointer method) const
349 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
350 (p->car_->*method) ();
354 Translator_group::do_print() const
360 gh_display (properties_dict_.self_scm_);
361 if (status == ORPHAN)
363 DEBUG_OUT << "consists of: ";
364 for (int i=0; i < consists_str_arr_.size (); i++)
365 DEBUG_OUT << consists_str_arr_[i] << ", ";
366 DEBUG_OUT << "\naccepts: ";
367 for (int i=0; i < accepts_str_arr_.size (); i++)
368 DEBUG_OUT << accepts_str_arr_[i] << ", ";
372 if (id_str_.length_i ())
373 DEBUG_OUT << "ID: " << id_str_ ;
374 DEBUG_OUT << " iterators: " << iterator_count_<< '\n';
376 each (&Translator::print);
381 Translator_group::do_pre_move_processing ()
383 each (&Translator::pre_move_processing);
387 Translator_group::do_post_move_processing ()
389 each (&Translator::post_move_processing);
393 Translator_group::do_process_requests ()
395 each (&Translator::process_requests);
399 Translator_group::do_creation_processing ()
401 each (&Translator::creation_processing);
405 Translator_group::do_removal_processing ()
407 each (&Translator::removal_processing);
411 Translator_group::do_add_processing ()
413 for (int i=0; i < consists_str_arr_.size(); i++)
415 String s = consists_str_arr_[i];
416 Translator * t = output_def_l ()->find_translator_l (s);
418 warning (_f ("Can't find: `%s'", s));
420 add_translator (t->clone ());
422 for (int i=0; i-- < consists_end_str_arr_.size (); i++)
424 String s = consists_end_str_arr_[i];
425 Translator * t = output_def_l ()->find_translator_l (s);
427 warning (_f ("Can't find: `%s'", s));
429 add_translator (t->clone ());
434 Translator_group::get_property (SCM sym, Translator_group **where_l) const
436 if (properties_dict_.elem_b (sym))
439 *where_l = (Translator_group*) this; // ugh
440 return properties_dict_[sym];
444 return daddy_trans_l_->get_property (sym, where_l);
449 return SCM_UNDEFINED;
453 Translator_group::set_property (String id, SCM val)
455 properties_dict_[ly_symbol2scm (id.ch_C())] = val;