]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-iterator.cc
release: 0.0.78
[lilypond.git] / lily / music-iterator.cc
1 /*
2   music-iterator.cc -- implement {Music,Chord,Voice}_iterator
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "music-list.hh"
10 #include "music-iterator.hh"
11 #include "translator.hh"
12 #include "request.hh"
13 #include "debug.hh"
14
15
16 IMPLEMENT_IS_TYPE_B(Music_iterator);
17
18 Chord_iterator::~Chord_iterator()
19 {
20 }
21
22 void
23 Music_iterator::do_print()const
24 {
25
26 }
27
28 void
29 Music_iterator::print() const
30 {
31 #ifndef NPRINT
32     mtor << name() << "{";
33     mtor << "report to " << 
34         report_to_l() << " (" << report_to_l()->name() << ")\n";
35     mtor << "next at " << next_moment() << " ";
36     do_print();
37     mtor << "}\n";
38 #endif
39 }
40
41 Translator *
42 Music_iterator::get_req_translator_l()
43 {
44     assert(report_to_l());
45     if (report_to_l()->is_bottom_engraver_b() )
46         return report_to_l();
47
48     set_translator( report_to_l()->get_default_interpreter() );
49     return report_to_l();
50 }
51
52 void
53 Music_iterator::push_translator(Translator*t)
54 {
55     if (t) {
56         report_to_l_arr_.push(t);
57         t->iterator_count_ ++;
58     }
59 }
60
61 void
62 Music_iterator::pop_translator()
63 {
64     if (report_to_l()) {
65         report_to_l()->iterator_count_ --;
66         report_to_l_arr_.pop();
67     }
68 }
69
70 Translator* 
71 Music_iterator::report_to_l()const
72 {
73     if (! report_to_l_arr_.size() )
74         return 0;
75     return report_to_l_arr_.top();
76 }
77
78
79 void
80 Music_iterator::set_translator(Translator*reg)
81 {   
82     if (report_to_l()==reg)
83         return;
84     pop_translator();
85     push_translator(reg);
86 }
87
88 void
89 Music_iterator::construct_children()
90 {
91
92 }
93
94 Music_iterator::~Music_iterator()
95 {
96     set_translator(0);
97 }
98
99 Moment
100 Music_iterator::next_moment()const
101 {
102     return 0;
103 }
104
105 void
106 Music_iterator::process_and_next(Moment)
107 {
108     first_b_ = false;
109 }
110
111 bool
112 Music_iterator::ok()const
113 {
114     return first_b_;
115 }
116
117 Music_iterator*
118 Music_iterator::static_get_iterator_p(Music *m,
119                                       Translator *report_l)
120 {
121     Music_iterator * p =0;
122     if (m->is_type_b( Change_reg::static_name()))
123         p = new Change_iterator((Change_reg*)m);
124     else if (m->is_type_b( Request_chord::static_name()))
125         p = new Request_chord_iterator( (Request_chord*) m);
126     else if (m->is_type_b( Chord::static_name())) 
127         p =  new Chord_iterator( (Chord*) m);
128     else if (m->is_type_b( Voice::static_name())) 
129         p =  new Voice_iterator(  (Voice*) m);
130     
131      if ( m->is_type_b( Music_list::static_name())) {
132         Music_list* ml = (Music_list*) m;
133         if (ml -> type_str_ != "") {
134             Translator * a =report_l->
135                 find_get_translator_l(ml-> type_str_, ml->id_str_);
136
137                 
138             p->set_translator( a);
139             
140         } 
141      } 
142      if (! p->report_to_l() )
143          p ->set_translator(report_l);
144     
145     return p;
146 }
147
148 Music_iterator*
149 Music_iterator::get_iterator_p(Music*m)const
150 {
151     Music_iterator*p = static_get_iterator_p(m,report_to_l());
152     p->daddy_iter_l_ = (Music_iterator*)this;
153     p->construct_children();
154     return p;
155 }
156
157 Music_iterator::Music_iterator()
158 {
159     daddy_iter_l_ =0;
160     first_b_ = true;
161 }
162
163 /* ************** */
164
165 Chord_iterator::Chord_iterator(Chord const *chord_C)
166 {
167     chord_C_ = chord_C;
168 }
169
170 void
171 Chord_iterator::construct_children()
172 {
173     int j = 0;
174     for(PCursor<Music*> i(chord_C_->music_p_list_.top());  //, int j = 0; 
175         i.ok(); j++, i++) {
176         Music_iterator * mi =  get_iterator_p( i.ptr());
177         set_translator(mi->report_to_l()->ancestor_l( chord_C_->multi_level_i_ ));
178         if ( mi->ok() )
179             children_p_list_.bottom().add( mi );
180         else 
181             delete mi;
182     }
183 }
184 void
185 Chord_iterator::do_print() const
186 {
187 #ifndef NPRINT
188     for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++) {
189         i->print();
190     }
191 #endif
192 }
193
194 void
195 Chord_iterator::process_and_next(Moment until)
196 {
197     for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); ) {
198         if  (i->next_moment() == until) {
199             i->process_and_next(until);
200         }
201         if (!i->ok()) 
202             delete i.remove_p();
203         else
204             i++;
205     }
206     Music_iterator::process_and_next(until);
207
208 //    assert(!ok() || next_moment() > until);
209 }
210
211
212 IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
213
214 Moment
215 Chord_iterator::next_moment()const
216 {
217     Moment next_ = INFTY;
218     for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++)
219         next_ = next_ <? i->next_moment() ;
220     return next_;
221 }
222
223
224
225 bool
226 Chord_iterator::ok()const
227 {
228     return children_p_list_.size();
229 }
230
231 /* ************** */
232
233 void
234 Voice_iterator::do_print()const
235 {
236     if (iter_p_)
237         iter_p_->print();
238 }
239
240 Voice_iterator::Voice_iterator(Voice const*v)
241     : PCursor<Music*> ( v->music_p_list_)
242 {
243     here_mom_ = v->offset_mom_;
244     voice_C_ = v;
245     iter_p_ =0;
246 }
247
248 void
249 Voice_iterator::construct_children()
250 {
251     if (ok()) {
252         iter_p_ = Music_iterator::get_iterator_p( ptr() );      
253         if (iter_p_->report_to_l()->depth_i() > report_to_l()->depth_i())
254             set_translator(iter_p_->report_to_l());
255     }
256 }
257
258 void
259 Voice_iterator::next_element()
260 {
261     delete iter_p_ ;
262     iter_p_ =0;
263     here_mom_ += ptr()->time_int().length();
264     PCursor<Music*>::next();
265     construct_children();
266 }
267
268 Voice_iterator::~Voice_iterator()
269 {
270     delete iter_p_;
271 }
272
273
274 IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
275
276 void
277 Voice_iterator::process_and_next(Moment until)
278 {
279     while (ok()) {
280         Moment local_until = until - here_mom_;
281         while ( iter_p_ && iter_p_->ok() ) {
282             Moment here = iter_p_->next_moment();
283             if (here != local_until)
284                 return;
285             iter_p_->process_and_next(local_until);
286         }
287         if (!iter_p_)
288             iter_p_ = Music_iterator::get_iterator_p( ptr() );
289         else if (!iter_p_->ok() )
290             next_element();
291     }
292     Music_iterator::process_and_next(until);
293     assert(!ok() || next_moment() > until);
294 }
295
296 Moment
297 Voice_iterator::next_moment()const
298 {
299     return iter_p_->next_moment() + here_mom_;
300 }
301
302 bool
303 Voice_iterator::ok()const
304 {
305     return PCursor<Music*>::ok();
306 }
307
308 /* ***************** */
309
310
311 Change_iterator::Change_iterator(Change_reg * ch)
312 {
313     change_l_ = ch;
314 }
315
316
317 IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator);
318
319 /*
320   TODO: pop/pushgroup
321  */
322 void
323 Change_iterator::process_and_next(Moment mom)
324 {
325 #if 0
326     if ( id[0] == '-') {
327         
328     
329     Engraver_group_engraver *group_l =
330         report_to_l()->find_get_translator_l(change_l_->type_str_, 
331                                              change_l_->id_str_);
332
333     report_to_l()->daddy_grav_l_->remove_engraver_p(report_to_l());
334     group_l->add(report_to_l());
335 #endif
336     Music_iterator::process_and_next(mom);
337 }
338
339
340
341 /* ******************** */
342
343
344 IMPLEMENT_IS_TYPE_B1(Request_chord_iterator,Music_iterator);
345
346 void
347 Request_chord_iterator::construct_children()
348 {
349     get_req_translator_l();
350 }
351
352 Request_chord_iterator::Request_chord_iterator(Request_chord*el_l)
353 {
354     elt_l_ = el_l;
355     elt_duration_ = el_l->time_int().length(); 
356     last_b_ = false;
357 }
358
359
360 bool
361 Request_chord_iterator::ok()const
362 {
363     return (elt_duration_ && !last_b_) || first_b_; 
364 }
365
366
367
368 Moment
369 Request_chord_iterator::next_moment()const
370 {
371     Moment m(0);
372     if  (!first_b_) 
373         m = elt_duration_;
374     return m;
375 }
376
377 void
378 Request_chord_iterator::do_print() const
379 {
380 #ifndef NPRINT
381     mtor << "duration: " << elt_duration_;
382 #endif
383 }
384 void
385 Request_chord_iterator::process_and_next(Moment mom)
386 {
387     if ( first_b_ ) {
388         for (PCursor<Music*> i(elt_l_->music_p_list_); i.ok(); i++) {
389             assert(i->is_type_b(Request::static_name()));
390             Request * req_l = (Request*)i.ptr();
391             bool gotcha = report_to_l()->try_request(req_l);
392             if (!gotcha)
393                 req_l->warning("Junking request: " + String(req_l->name()));
394
395         }
396         first_b_ = false;
397     }
398
399     if ( mom >= elt_duration_ )
400         last_b_ = true;  
401 }