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 ()))
132 Translator_group* r = 0;
133 for (Cons<Translator> *p = trans_p_list_.head_; !r && p; p = p->next_)
135 if (Translator_group *trg = dynamic_cast <Translator_group *> (p->car_))
136 r = trg->find_existing_translator_l (n, id);
142 Link_array<Translator_group>
143 Translator_group::path_to_acceptable_translator (String type) const
145 Link_array<Translator_group> accepted_arr;
146 for (int i=0; i < accepts_str_arr_.size (); i++)
148 Translator *t = output_def_l ()->find_translator_l (accepts_str_arr_[i]);
149 if (!t || !dynamic_cast <Translator_group *> (t))
151 accepted_arr.push (dynamic_cast <Translator_group *> (t));
155 for (int i=0; i < accepted_arr.size (); i++)
156 if (accepted_arr[i]->type_str_ == type)
158 Link_array<Translator_group> retval;
159 retval.push (accepted_arr[i]);
163 Link_array<Translator_group> best_result;
164 int best_depth= INT_MAX;
165 for (int i=0; i < accepted_arr.size (); i++)
167 Translator_group * g = accepted_arr[i];
169 Link_array<Translator_group> result
170 = g->path_to_acceptable_translator (type);
171 if (result.size () && result.size () < best_depth)
174 best_result = result;
182 Translator_group::find_create_translator_l (String n, String id)
184 Translator_group * existing = find_existing_translator_l (n,id);
188 Link_array<Translator_group> path = path_to_acceptable_translator (n);
192 Translator_group * current = this;
194 // start at 1. The first one (index 0) will be us.
195 for (int i=0; i < path.size (); i++)
197 Translator_group * new_group = dynamic_cast<Translator_group*>(path[i]->clone ());
198 current->add_translator (new_group);
201 current->id_str_ = id;
205 Translator_group *ret = 0;
207 ret = daddy_trans_l_->find_create_translator_l (n,id);
210 warning (_f ("Can't find or create `%s' called `%s'", n, id));
217 Translator_group::try_music_on_nongroup_children (Music *m)
219 bool hebbes_b =false;
221 for (Cons<Translator> *p = trans_p_list_.head_; !hebbes_b && p; p = p->next_)
223 if (!dynamic_cast <Translator_group *> (p->car_))
225 hebbes_b = p->car_->try_music (m);
232 Translator_group::do_try_music (Music* m)
234 bool hebbes_b = try_music_on_nongroup_children (m);
236 if (!hebbes_b && daddy_trans_l_)
237 hebbes_b = daddy_trans_l_->try_music (m);
242 Translator_group::depth_i() const
244 return (daddy_trans_l_) ? daddy_trans_l_->depth_i() + 1 : 0;
248 Translator_group::ancestor_l (int level)
250 if (!level || !daddy_trans_l_)
253 return daddy_trans_l_->ancestor_l (level-1);
261 Translator_group::terminate_translator (Translator*r_l)
263 DEBUG_OUT << "Removing " << classname (r_l) << " at " << now_mom () << '\n';
264 r_l->removal_processing();
265 Translator * trans_p =remove_translator_p (r_l);
272 Remove a translator from the hierarchy.
275 Translator_group::remove_translator_p (Translator*trans_l)
279 for (Cons<Translator> **pp = &trans_p_list_.head_; *pp; pp = &(*pp)->next_)
280 if ((*pp)->car_ == trans_l)
282 Cons<Translator> *r = trans_p_list_.remove_cons (pp);
284 trans_l->daddy_trans_l_ =0;
294 Translator_group::get_simple_translator (String type) const
296 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
298 if (classname (p->car_) == type)
302 return daddy_trans_l_->get_simple_translator (type);
308 Translator_group::is_bottom_translator_b () const
310 return !accepts_str_arr_.size ();
316 Translator_group::get_default_interpreter()
318 if (accepts_str_arr_.size())
320 Translator*t = output_def_l ()->find_translator_l (accepts_str_arr_[0]);
323 warning (_f ("Can't find or create: `%s'", accepts_str_arr_[0]));
326 Translator_group * g= dynamic_cast <Translator_group*>(t->clone ());
329 if (!g->is_bottom_translator_b ())
330 return g->get_default_interpreter ();
338 Translator_group::each (Method_pointer method)
340 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
341 (p->car_->*method) ();
346 Translator_group::each (Const_method_pointer method) const
348 for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
349 (p->car_->*method) ();
353 Translator_group::do_print() const
359 gh_display (properties_dict_.self_scm_);
360 if (status == ORPHAN)
362 DEBUG_OUT << "consists of: ";
363 for (int i=0; i < consists_str_arr_.size (); i++)
364 DEBUG_OUT << consists_str_arr_[i] << ", ";
365 DEBUG_OUT << "\naccepts: ";
366 for (int i=0; i < accepts_str_arr_.size (); i++)
367 DEBUG_OUT << accepts_str_arr_[i] << ", ";
371 if (id_str_.length_i ())
372 DEBUG_OUT << "ID: " << id_str_ ;
373 DEBUG_OUT << " iterators: " << iterator_count_<< '\n';
375 each (&Translator::print);
380 Translator_group::do_pre_move_processing ()
382 each (&Translator::pre_move_processing);
386 Translator_group::do_post_move_processing ()
388 each (&Translator::post_move_processing);
392 Translator_group::do_process_requests ()
394 each (&Translator::process_requests);
398 Translator_group::do_creation_processing ()
400 each (&Translator::creation_processing);
404 Translator_group::do_removal_processing ()
406 each (&Translator::removal_processing);
410 Translator_group::do_add_processing ()
412 for (int i=0; i < consists_str_arr_.size(); i++)
414 String s = consists_str_arr_[i];
415 Translator * t = output_def_l ()->find_translator_l (s);
417 warning (_f ("Can't find: `%s'", s));
419 add_translator (t->clone ());
421 for (int i=0; i-- < consists_end_str_arr_.size (); i++)
423 String s = consists_end_str_arr_[i];
424 Translator * t = output_def_l ()->find_translator_l (s);
426 warning (_f ("Can't find: `%s'", s));
428 add_translator (t->clone ());
433 Translator_group::get_property (SCM sym, Translator_group **where_l) const
435 if (properties_dict_.elem_b (sym))
438 *where_l = (Translator_group*) this; // ugh
439 return properties_dict_[sym];
443 return daddy_trans_l_->get_property (sym, where_l);
448 return SCM_UNDEFINED;
452 Translator_group::set_property (String id, SCM val)
454 properties_dict_[ly_symbol2scm (id.ch_C())] = val;