]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
f80b2c49de598672e9b21968e9fc269d0c48bde8
[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--2000 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   Translator_group* r = 0;
133   for (Cons<Translator> *p = trans_p_list_.head_; !r && p; p = p->next_)
134     {
135       if (Translator_group *trg =  dynamic_cast <Translator_group *> (p->car_))
136         r = trg->find_existing_translator_l (n, id);
137     }
138
139   return r;
140 }
141
142 Link_array<Translator_group>
143 Translator_group::path_to_acceptable_translator (String type) const
144 {
145  Link_array<Translator_group> accepted_arr;
146   for (int i=0; i < accepts_str_arr_.size (); i++)
147     {
148       Translator *t = output_def_l ()->find_translator_l (accepts_str_arr_[i]);
149       if (!t || !dynamic_cast <Translator_group *> (t))
150         continue;
151       accepted_arr.push (dynamic_cast <Translator_group *> (t));
152     }
153
154
155  for (int i=0; i < accepted_arr.size (); i++)
156     if (accepted_arr[i]->type_str_ == type)
157       {
158         Link_array<Translator_group> retval;
159         retval.push (accepted_arr[i]);
160         return retval;
161       }
162
163   Link_array<Translator_group> best_result;
164   int best_depth= INT_MAX;
165   for (int i=0; i < accepted_arr.size (); i++)
166     {
167       Translator_group * g = accepted_arr[i];
168
169       Link_array<Translator_group> result
170         = g->path_to_acceptable_translator (type);
171       if (result.size () && result.size () < best_depth)
172         {
173           result.insert (g,0);
174           best_result = result;
175         }
176     }
177
178   return best_result;
179 }
180
181 Translator_group*
182 Translator_group::find_create_translator_l (String n, String id)
183 {
184   Translator_group * existing = find_existing_translator_l (n,id);
185   if (existing)
186     return existing;
187
188   Link_array<Translator_group> path = path_to_acceptable_translator (n);
189
190   if (path.size ())
191     {
192       Translator_group * current = this;
193
194       // start at 1.  The first one (index 0) will be us.
195       for (int i=0; i < path.size (); i++)
196         {
197           Translator_group * new_group = dynamic_cast<Translator_group*>(path[i]->clone ());
198           current->add_translator (new_group);
199           current = new_group;
200         }
201       current->id_str_ = id;
202       return current;
203     }
204
205   Translator_group *ret = 0;
206   if (daddy_trans_l_)
207     ret = daddy_trans_l_->find_create_translator_l (n,id);
208   else
209     {
210       warning (_f ("Can't find or create `%s' called `%s'", n, id));
211       ret =0;
212     }
213   return ret;
214 }
215
216 bool
217 Translator_group::try_music_on_nongroup_children (Music *m)
218 {
219   bool hebbes_b =false;
220
221   for (Cons<Translator> *p = trans_p_list_.head_; !hebbes_b && p; p = p->next_)
222     {
223       if (!dynamic_cast <Translator_group *> (p->car_))
224         {
225           hebbes_b = p->car_->try_music (m);
226         }
227     }
228   return hebbes_b;
229 }
230
231 bool
232 Translator_group::do_try_music (Music* m)
233 {
234   bool hebbes_b = try_music_on_nongroup_children (m);
235   
236   if (!hebbes_b && daddy_trans_l_)
237     hebbes_b = daddy_trans_l_->try_music (m);
238   return hebbes_b ;
239 }
240
241 int
242 Translator_group::depth_i() const
243 {
244   return (daddy_trans_l_) ? daddy_trans_l_->depth_i()  + 1 : 0;
245 }
246
247 Translator_group*
248 Translator_group::ancestor_l (int level)
249 {
250   if (!level || !daddy_trans_l_)
251     return this;
252
253   return daddy_trans_l_->ancestor_l (level-1);
254 }
255
256
257
258
259
260 void
261 Translator_group::terminate_translator (Translator*r_l)
262 {
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);
266
267   delete trans_p;
268 }
269
270
271 /**
272    Remove a translator from the hierarchy.
273  */
274 Translator *
275 Translator_group::remove_translator_p (Translator*trans_l)
276 {
277   assert (trans_l);
278   
279   for (Cons<Translator> **pp = &trans_p_list_.head_; *pp; pp = &(*pp)->next_)
280     if ((*pp)->car_ == trans_l)
281       {
282         Cons<Translator> *r = trans_p_list_.remove_cons (pp);
283         r->car_ =0;
284         trans_l->daddy_trans_l_ =0;
285         delete r;
286         return trans_l;
287       }
288
289   return 0;
290 }
291
292
293 Translator*
294 Translator_group::get_simple_translator (String type) const
295 {
296   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
297     {
298       if (classname (p->car_) == type)
299         return p->car_;
300     }
301   if (daddy_trans_l_)
302     return daddy_trans_l_->get_simple_translator (type);
303   return 0;
304 }
305
306
307 bool
308 Translator_group::is_bottom_translator_b () const
309 {
310   return !accepts_str_arr_.size ();
311 }
312
313
314
315 Translator_group*
316 Translator_group::get_default_interpreter()
317 {
318   if (accepts_str_arr_.size())
319     {
320       Translator*t = output_def_l ()->find_translator_l (accepts_str_arr_[0]);
321       if (!t)
322         {
323           warning (_f ("Can't find or create: `%s'", accepts_str_arr_[0]));
324           t = this;
325         }
326       Translator_group * g= dynamic_cast <Translator_group*>(t->clone ());
327       add_translator (g);
328
329       if (!g->is_bottom_translator_b ())
330         return g->get_default_interpreter ();
331       else
332         return g;
333     }
334   return this;
335 }
336
337 void
338 Translator_group::each (Method_pointer method)
339 {
340   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
341     (p->car_->*method) ();
342 }
343
344
345 void
346 Translator_group::each (Const_method_pointer method) const
347 {
348   for (Cons<Translator> *p = trans_p_list_.head_; p; p = p->next_)
349     (p->car_->*method) ();
350 }
351
352 void
353 Translator_group::do_print() const
354 {
355 #ifndef NPRINT
356   if (!flower_dstream)
357     return ;
358
359   gh_display (properties_dict_.self_scm_);
360   if (status == ORPHAN)
361     {
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] << ", ";
368     }
369   else
370     {
371       if (id_str_.length_i ())
372         DEBUG_OUT << "ID: " << id_str_ ;
373       DEBUG_OUT << " iterators: " << iterator_count_<< '\n';
374     }
375   each (&Translator::print);
376 #endif
377 }
378
379 void
380 Translator_group::do_pre_move_processing ()
381 {
382   each (&Translator::pre_move_processing);
383 }
384
385 void
386 Translator_group::do_post_move_processing ()
387 {
388   each (&Translator::post_move_processing);
389 }
390
391 void
392 Translator_group::do_process_requests ()
393 {
394   each (&Translator::process_requests);
395 }
396
397 void
398 Translator_group::do_creation_processing ()
399 {
400   each (&Translator::creation_processing);
401 }
402
403 void
404 Translator_group::do_removal_processing ()
405 {
406   each (&Translator::removal_processing);
407 }
408
409 void
410 Translator_group::do_add_processing ()
411 {
412    for (int i=0; i < consists_str_arr_.size(); i++)
413     {
414       String s = consists_str_arr_[i];
415       Translator * t = output_def_l ()->find_translator_l (s);
416       if (!t)
417         warning (_f ("Can't find: `%s'", s));
418       else
419         add_translator (t->clone ());
420     }
421    for (int i=0; i-- < consists_end_str_arr_.size (); i++)
422      {
423        String s = consists_end_str_arr_[i];
424        Translator * t = output_def_l ()->find_translator_l (s);
425        if (!t)
426          warning (_f ("Can't find: `%s'", s));
427        else
428          add_translator (t->clone ());
429     }
430 }
431
432 SCM
433 Translator_group::get_property (SCM sym, Translator_group **where_l) const
434 {
435   if (properties_dict_.elem_b (sym))
436     {
437       if (where_l)
438         *where_l = (Translator_group*) this; // ugh
439       return properties_dict_.get (sym);
440     }
441
442   if (daddy_trans_l_)
443     return daddy_trans_l_->get_property (sym, where_l);
444   
445   if (where_l)
446     *where_l = 0;
447
448   return SCM_UNDEFINED;
449 }
450
451 void
452 Translator_group::set_property (String id, SCM val)
453 {
454   properties_dict_.set (ly_symbol2scm (id.ch_C()), val);
455 }