2 Translator_group.cc -- implement Translator_group
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
14 #include "rational.hh"
16 Translator_group::Translator_group (Translator_group const&s)
19 consists_str_arr_ = s.consists_str_arr_;
20 accepts_str_arr_ = s.accepts_str_arr_;
24 Translator_group::~Translator_group ()
26 assert (removable_b());
30 Translator_group::Translator_group()
36 Translator_group::check_removal()
38 for (int i =0; i < group_l_arr ().size();)
40 group_l_arr ()[i]->check_removal();
41 if (group_l_arr ()[i]->removable_b())
42 terminate_translator (group_l_arr ()[i]);
50 IMPLEMENT_IS_TYPE_B1(Translator_group, Translator);
53 Translator_group::add (Translator *trans_p)
55 trans_p_list_.bottom().add (trans_p);
56 trans_p->daddy_trans_l_ = this;
57 trans_p->output_def_l_ = output_def_l_;
58 trans_p->add_processing ();
62 Translator_group::removable_b() const
64 return !(iterator_count_ || group_l_arr ().size());
68 Translator_group::find_existing_translator_l (String n, String id)
70 if (is_alias_b (n) && (id_str_ == id || id.empty_b ()))
72 Translator_group* r = 0;
73 for (int i =0; !r && i < group_l_arr ().size(); i++)
75 r = group_l_arr ()[i]->find_existing_translator_l (n,id);
81 Link_array<Translator_group>
82 Translator_group::path_to_acceptable_translator (String type) const
84 Link_array<Translator_group> accepted_arr;
85 for (int i=0; i < accepts_str_arr_.size (); i++)
87 Translator *t = output_def_l ()->find_translator_l (accepts_str_arr_[i]);
88 if (!t || !t->group_l ())
90 accepted_arr.push (t->group_l());
94 for (int i=0; i < accepted_arr.size (); i++)
95 if (accepted_arr[i]->type_str_ == type)
97 Link_array<Translator_group> retval;
98 retval.push (accepted_arr[i]);
102 Link_array<Translator_group> best_result;
103 int best_depth= INT_MAX;
104 for (int i=0; i < accepted_arr.size (); i++)
106 Translator_group * g = accepted_arr[i];
108 Link_array<Translator_group> result
109 = g->path_to_acceptable_translator (type);
110 if (result.size () && result.size () < best_depth)
113 best_result = result;
121 Translator_group::find_create_translator_l (String n, String id)
123 Translator_group * existing = find_existing_translator_l (n,id);
127 Link_array<Translator_group> path = path_to_acceptable_translator (n);
131 Translator_group * current = this;
133 // start at 1. The first one (index 0) will be us.
134 for (int i=0; i < path.size (); i++)
136 Translator_group * new_group = path[i]->clone ()->group_l ();
137 current->add (new_group);
140 current->id_str_ = id;
144 Translator_group *ret = 0;
146 ret = daddy_trans_l_->find_create_translator_l (n,id);
149 warning (_("Can't find or create `") + n + _("' called `") + id + "'\n");
157 Translator_group::do_try_request (Request* req_l)
159 bool hebbes_b =false;
160 for (int i =0; !hebbes_b && i < nongroup_l_arr ().size() ; i++)
161 hebbes_b =nongroup_l_arr ()[i]->try_request (req_l);
162 if (!hebbes_b && daddy_trans_l_)
163 hebbes_b = daddy_trans_l_->try_request (req_l);
168 Translator_group::depth_i() const
170 return (daddy_trans_l_) ? daddy_trans_l_->depth_i() + 1 : 0;
174 Translator_group::ancestor_l (int level)
176 if (!level || !daddy_trans_l_)
179 return daddy_trans_l_->ancestor_l (level-1);
182 Link_array<Translator_group>
183 Translator_group::group_l_arr () const
185 Link_array<Translator_group> groups;
186 for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
189 groups.push (i->group_l ());
194 Link_array<Translator>
195 Translator_group::nongroup_l_arr () const
197 Link_array<Translator> groups;
198 for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
201 groups.push (i.ptr ());
207 Translator_group::terminate_translator (Translator*r_l)
209 DOUT << "Removing " << r_l->name() << " at " << now_moment () << "\n";
210 r_l->removal_processing();
211 Translator * trans_p =remove_translator_p (r_l);
217 Translator_group::remove_translator_p (Translator*trans_l)
221 PCursor<Translator*> trans_cur (trans_p_list_.find (trans_l));
222 Translator * t = trans_cur.remove_p();
224 For elegant design, we would do this too. Alas, it does not work yet..
226 t-> removal_processing ();
228 t-> daddy_trans_l_ = 0;
234 Translator_group::get_simple_translator (char const *type) const
236 for (int i=0; i < nongroup_l_arr ().size(); i++)
238 if (nongroup_l_arr ()[i]->name() == type)
239 return nongroup_l_arr ()[i];
242 return daddy_trans_l_->get_simple_translator (type);
248 Translator_group::is_bottom_translator_b () const
250 return !accepts_str_arr_.size ();
256 Translator_group::get_default_interpreter()
258 if (accepts_str_arr_.size())
260 Translator*t = output_def_l ()->find_translator_l (accepts_str_arr_[0]);
261 Translator_group * g= t->clone ()->group_l ();
264 if (!g->is_bottom_translator_b ())
265 return g->get_default_interpreter ();
273 Translator_group::each (Method_pointer method)
275 for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
276 (i.ptr()->*method) ();
280 Translator_group::each (Const_method_pointer method) const
282 for (PCursor<Translator*> i (trans_p_list_.top ()); i.ok (); i++)
283 (i.ptr()->*method) ();
287 Translator_group::do_print() const
292 if (status == ORPHAN)
294 DOUT << "consists of: ";
295 for (int i=0; i < consists_str_arr_.size (); i++)
296 DOUT << consists_str_arr_[i] << ", ";
297 DOUT << "\naccepts: ";
298 for (int i=0; i < accepts_str_arr_.size (); i++)
299 DOUT << accepts_str_arr_[i] << ", ";
303 if (id_str_.length_i ())
304 DOUT << "ID: " << id_str_ ;
305 DOUT << " iterators: " << iterator_count_<< "\n";
307 each (&Translator::print);
312 Translator_group::do_pre_move_processing ()
314 each (&Translator::pre_move_processing);
318 Translator_group::do_post_move_processing ()
320 each (&Translator::post_move_processing);
324 Translator_group::do_process_requests ()
326 each (&Translator::process_requests);
330 Translator_group::do_creation_processing ()
332 each (&Translator::creation_processing);
336 Translator_group::do_removal_processing ()
338 each (&Translator::removal_processing);
342 Translator_group::do_add_processing ()
344 for (int i=0; i < consists_str_arr_.size(); i++)
346 Translator * t = output_def_l ()->find_translator_l (consists_str_arr_[i]);
348 warning (_("Could not find `") +consists_str_arr_[i]+ "'");