]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
fb5d6b449915c385572abe1ff07feb49fdb2e5dd
[lilypond.git] / lily / translator-group.cc
1 /*
2   Translator_group.cc -- implement Translator_group
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
12 #include "debug.hh"
13 #include "moment.hh"
14 #include "dictionary-iter.hh"
15
16 #include "killing-cons.tcc"
17
18 Translator_group::Translator_group (Translator_group const&s)
19   : Translator(s)
20 {
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_;
24   iterator_count_ =0;
25   properties_dict_ = s.properties_dict_;
26 }
27
28 Translator_group::~Translator_group ()
29 {
30   assert (removable_b());
31   trans_p_list_.junk ();
32 }
33
34
35 Translator_group::Translator_group()
36 {
37   iterator_count_  = 0;
38 }
39
40 void
41 Translator_group::check_removal()
42 {
43   Cons<Translator> *next =0;
44   for (Cons<Translator> *p = trans_p_list_.head_; p; p = next)
45     {
46       next = p->next_;
47       if (Translator_group *trg =  dynamic_cast <Translator_group *> (p->car_))
48         {
49           trg->check_removal ();
50           if (trg->removable_b())
51             terminate_translator (trg);
52         }
53     }
54 }
55
56 void
57 Translator_group::add_translator (Translator *trans_p)
58 {
59   trans_p_list_.append (new Killing_cons<Translator> (trans_p,0));
60   
61   trans_p->daddy_trans_l_ = this;
62   trans_p->output_def_l_ = output_def_l_;
63   trans_p->add_processing ();
64 }
65
66 void
67 Translator_group::set_acceptor (String accepts, bool add)
68 {
69   if (add)
70     accepts_str_arr_.push (accepts);
71   else
72     for (int i=accepts_str_arr_.size (); i--; )
73       if (accepts_str_arr_[i] == accepts)
74         accepts_str_arr_.del (i);
75 }
76
77 void
78 Translator_group::add_last_element (String s)
79 {
80   if (!get_translator_l (s))
81     error (_ ("Program has no such type"));
82
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));
86       
87   consists_end_str_arr_.push (s);
88 }
89
90 void
91 Translator_group::set_element (String s, bool add)
92 {
93   if (!get_translator_l (s))
94     error (_ ("Program has no such type"));
95
96   if (add)
97     {
98       for (int i=consists_str_arr_.size (); i--; )
99         if (consists_str_arr_[i] == s)
100           warning (_f("Already contains: `%s'", s));
101       
102       consists_str_arr_.push (s);
103     }
104   else
105     {
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);
112     }
113 }
114 bool
115 Translator_group::removable_b() const
116 {
117   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
118     {
119       if (dynamic_cast <Translator_group *> (p->car_))
120         return false;
121     }
122
123   return !iterator_count_;
124 }
125
126 Translator_group *
127 Translator_group::find_existing_translator_l (String n, String id)
128 {
129   if (is_alias_b (n) && (id_str_ == id || id.empty_b ()))
130     return this;
131
132
133   Translator_group* r = 0;
134   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
135     {
136       if (Translator_group *trg =  dynamic_cast <Translator_group *> (p->car_))
137         r = trg->find_existing_translator_l (n, id);
138     }
139
140   return r;
141 }
142
143 Link_array<Translator_group>
144 Translator_group::path_to_acceptable_translator (String type) const
145 {
146  Link_array<Translator_group> accepted_arr;
147   for (int i=0; i < accepts_str_arr_.size (); i++)
148     {
149       Translator *t = output_def_l ()->find_translator_l (accepts_str_arr_[i]);
150       if (!t || !dynamic_cast <Translator_group *> (t))
151         continue;
152       accepted_arr.push (dynamic_cast <Translator_group *> (t));
153     }
154
155
156  for (int i=0; i < accepted_arr.size (); i++)
157     if (accepted_arr[i]->type_str_ == type)
158       {
159         Link_array<Translator_group> retval;
160         retval.push (accepted_arr[i]);
161         return retval;
162       }
163
164   Link_array<Translator_group> best_result;
165   int best_depth= INT_MAX;
166   for (int i=0; i < accepted_arr.size (); i++)
167     {
168       Translator_group * g = accepted_arr[i];
169
170       Link_array<Translator_group> result
171         = g->path_to_acceptable_translator (type);
172       if (result.size () && result.size () < best_depth)
173         {
174           result.insert (g,0);
175           best_result = result;
176         }
177     }
178
179   return best_result;
180 }
181
182 Translator_group*
183 Translator_group::find_create_translator_l (String n, String id)
184 {
185   Translator_group * existing = find_existing_translator_l (n,id);
186   if (existing)
187     return existing;
188
189   Link_array<Translator_group> path = path_to_acceptable_translator (n);
190
191   if (path.size ())
192     {
193       Translator_group * current = this;
194
195       // start at 1.  The first one (index 0) will be us.
196       for (int i=0; i < path.size (); i++)
197         {
198           Translator_group * new_group = dynamic_cast<Translator_group*>(path[i]->clone ());
199           current->add_translator (new_group);
200           current = new_group;
201         }
202       current->id_str_ = id;
203       return current;
204     }
205
206   Translator_group *ret = 0;
207   if (daddy_trans_l_)
208     ret = daddy_trans_l_->find_create_translator_l (n,id);
209   else
210     {
211       warning (_f ("Can't find or create `%s' called `%s'", n, id));
212       ret =0;
213     }
214   return ret;
215 }
216
217 bool
218 Translator_group::try_music_on_nongroup_children (Music *m)
219 {
220   bool hebbes_b =false;
221
222   for (Cons<Translator> *p = trans_p_list_.head_; !hebbes_b && p; p = p->next_)
223     {
224       if (!dynamic_cast <Translator_group *> (p->car_))
225         {
226           hebbes_b = p->car_->try_music (m);
227         }
228     }
229   return hebbes_b;
230 }
231
232 bool
233 Translator_group::do_try_music (Music* m)
234 {
235   bool hebbes_b = try_music_on_nongroup_children (m);
236   
237   if (!hebbes_b && daddy_trans_l_)
238     hebbes_b = daddy_trans_l_->try_music (m);
239   return hebbes_b ;
240 }
241
242 int
243 Translator_group::depth_i() const
244 {
245   return (daddy_trans_l_) ? daddy_trans_l_->depth_i()  + 1 : 0;
246 }
247
248 Translator_group*
249 Translator_group::ancestor_l (int level)
250 {
251   if (!level || !daddy_trans_l_)
252     return this;
253
254   return daddy_trans_l_->ancestor_l (level-1);
255 }
256
257
258
259
260
261 void
262 Translator_group::terminate_translator (Translator*r_l)
263 {
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);
267
268   delete trans_p;
269 }
270
271
272 /**
273    Remove a translator from the hierarchy.
274  */
275 Translator *
276 Translator_group::remove_translator_p (Translator*trans_l)
277 {
278   assert (trans_l);
279   
280   for (Cons<Translator> **pp = &trans_p_list_.head_; *pp; pp = &(*pp)->next_)
281     if ((*pp)->car_ == trans_l)
282       {
283         Cons<Translator> *r = trans_p_list_.remove_cons (pp);
284         r->car_ =0;
285         trans_l->daddy_trans_l_ =0;
286         delete r;
287         return trans_l;
288       }
289
290   return 0;
291 }
292
293
294 Translator*
295 Translator_group::get_simple_translator (String type) const
296 {
297   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
298     {
299       if (classname (p->car_) == type)
300         return p->car_;
301     }
302   if (daddy_trans_l_)
303     return daddy_trans_l_->get_simple_translator (type);
304   return 0;
305 }
306
307
308 bool
309 Translator_group::is_bottom_translator_b () const
310 {
311   return !accepts_str_arr_.size ();
312 }
313
314
315
316 Translator_group*
317 Translator_group::get_default_interpreter()
318 {
319   if (accepts_str_arr_.size())
320     {
321       Translator*t = output_def_l ()->find_translator_l (accepts_str_arr_[0]);
322       if (!t)
323         {
324           warning (_f ("Can't find or create: `%s'", accepts_str_arr_[0]));
325           t = this;
326         }
327       Translator_group * g= dynamic_cast <Translator_group*>(t->clone ());
328       add_translator (g);
329
330       if (!g->is_bottom_translator_b ())
331         return g->get_default_interpreter ();
332       else
333         return g;
334     }
335   return this;
336 }
337
338 void
339 Translator_group::each (Method_pointer method)
340 {
341   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
342     (p->car_->*method) ();
343 }
344
345
346 void
347 Translator_group::each (Const_method_pointer method) const
348 {
349   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
350     (p->car_->*method) ();
351 }
352
353 void
354 Translator_group::do_print() const
355 {
356 #ifndef NPRINT
357   if (!flower_dstream)
358     return ;
359
360   gh_display (properties_dict_.self_scm_);
361   if (status == ORPHAN)
362     {
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] << ", ";
369     }
370   else
371     {
372       if (id_str_.length_i ())
373         DEBUG_OUT << "ID: " << id_str_ ;
374       DEBUG_OUT << " iterators: " << iterator_count_<< '\n';
375     }
376   each (&Translator::print);
377 #endif
378 }
379
380 void
381 Translator_group::do_pre_move_processing ()
382 {
383   each (&Translator::pre_move_processing);
384 }
385
386 void
387 Translator_group::do_post_move_processing ()
388 {
389   each (&Translator::post_move_processing);
390 }
391
392 void
393 Translator_group::do_process_requests ()
394 {
395   each (&Translator::process_requests);
396 }
397
398 void
399 Translator_group::do_creation_processing ()
400 {
401   each (&Translator::creation_processing);
402 }
403
404 void
405 Translator_group::do_removal_processing ()
406 {
407   each (&Translator::removal_processing);
408 }
409
410 void
411 Translator_group::do_add_processing ()
412 {
413    for (int i=0; i < consists_str_arr_.size(); i++)
414     {
415       String s = consists_str_arr_[i];
416       Translator * t = output_def_l ()->find_translator_l (s);
417       if (!t)
418         warning (_f ("Can't find: `%s'", s));
419       else
420         add_translator (t->clone ());
421     }
422    for (int i=0; i-- < consists_end_str_arr_.size (); i++)
423      {
424        String s = consists_end_str_arr_[i];
425        Translator * t = output_def_l ()->find_translator_l (s);
426        if (!t)
427          warning (_f ("Can't find: `%s'", s));
428        else
429          add_translator (t->clone ());
430     }
431 }
432
433 SCM
434 Translator_group::get_property (SCM sym, Translator_group **where_l) const
435 {
436   if (properties_dict_.elem_b (sym))
437     {
438       if (where_l)
439         *where_l = (Translator_group*) this; // ugh
440       return properties_dict_[sym];
441     }
442
443   if (daddy_trans_l_)
444     return daddy_trans_l_->get_property (sym, where_l);
445   
446   if (where_l)
447     *where_l = 0;
448
449   return SCM_UNDEFINED;
450 }
451
452 void
453 Translator_group::set_property (String id, SCM val)
454 {
455   properties_dict_[ly_symbol2scm (id.ch_C())] = val;
456 }