]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-iterator.cc
release: 0.1.0
[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 -> type_str_ != "") {
132         Translator * a =report_l->
133             find_get_translator_l(m-> type_str_, m->id_str_);
134             p->set_translator( a);
135     } 
136
137
138     if (! p->report_to_l() )
139          p ->set_translator(report_l);
140     
141     return p;
142 }
143
144 Music_iterator*
145 Music_iterator::get_iterator_p(Music*m)const
146 {
147     Music_iterator*p = static_get_iterator_p(m,report_to_l());
148     p->daddy_iter_l_ = (Music_iterator*)this;
149     p->construct_children();
150     return p;
151 }
152
153 Music_iterator::Music_iterator()
154 {
155     daddy_iter_l_ =0;
156     first_b_ = true;
157 }
158
159 /* ************** */
160
161 Chord_iterator::Chord_iterator(Chord const *chord_C)
162 {
163     chord_C_ = chord_C;
164 }
165
166 void
167 Chord_iterator::construct_children()
168 {
169     int j = 0;
170     for(PCursor<Music*> i(chord_C_->music_p_list_.top());  //, int j = 0; 
171         i.ok(); j++, i++) {
172         Music_iterator * mi =  get_iterator_p( i.ptr());
173         set_translator(mi->report_to_l()->ancestor_l( chord_C_->multi_level_i_ ));
174         if ( mi->ok() )
175             children_p_list_.bottom().add( mi );
176         else 
177             delete mi;
178     }
179 }
180 void
181 Chord_iterator::do_print() const
182 {
183 #ifndef NPRINT
184     for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++) {
185         i->print();
186     }
187 #endif
188 }
189
190 void
191 Chord_iterator::process_and_next(Moment until)
192 {
193     for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); ) {
194         if  (i->next_moment() == until) {
195             i->process_and_next(until);
196         }
197         if (!i->ok()) 
198             delete i.remove_p();
199         else
200             i++;
201     }
202     Music_iterator::process_and_next(until);
203
204 //    assert(!ok() || next_moment() > until);
205 }
206
207
208 IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
209
210 Moment
211 Chord_iterator::next_moment()const
212 {
213     Moment next_ = INFTY_f;
214     for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++)
215         next_ = next_ <? i->next_moment() ;
216     return next_;
217 }
218
219
220
221 bool
222 Chord_iterator::ok()const
223 {
224     return children_p_list_.size();
225 }
226
227 /* ************** */
228
229 void
230 Voice_iterator::do_print()const
231 {
232     if (iter_p_)
233         iter_p_->print();
234 }
235
236 Voice_iterator::Voice_iterator(Voice const*v)
237     : PCursor<Music*> ( v->music_p_list_)
238 {
239     here_mom_ = v->offset_mom_;
240     voice_C_ = v;
241     iter_p_ =0;
242 }
243
244 void
245 Voice_iterator::construct_children()
246 {
247     if (ok()) {
248         iter_p_ = Music_iterator::get_iterator_p( ptr() );      
249         if (iter_p_->report_to_l()->depth_i() > report_to_l()->depth_i())
250             set_translator(iter_p_->report_to_l());
251     }
252 }
253
254 void
255 Voice_iterator::next_element()
256 {
257     delete iter_p_ ;
258     iter_p_ =0;
259     here_mom_ += ptr()->time_int().length();
260     PCursor<Music*>::next();
261     construct_children();
262 }
263
264 Voice_iterator::~Voice_iterator()
265 {
266     delete iter_p_;
267 }
268
269
270 IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
271
272 void
273 Voice_iterator::process_and_next(Moment until)
274 {
275     while (ok()) {
276         Moment local_until = until - here_mom_;
277         while ( iter_p_ && iter_p_->ok() ) {
278             Moment here = iter_p_->next_moment();
279             if (here != local_until)
280                 return;
281             iter_p_->process_and_next(local_until);
282         }
283         if (!iter_p_)
284             iter_p_ = Music_iterator::get_iterator_p( ptr() );
285         else if (!iter_p_->ok() )
286             next_element();
287     }
288     Music_iterator::process_and_next(until);
289     assert(!ok() || next_moment() > until);
290 }
291
292 Moment
293 Voice_iterator::next_moment()const
294 {
295     return iter_p_->next_moment() + here_mom_;
296 }
297
298 bool
299 Voice_iterator::ok()const
300 {
301     return PCursor<Music*>::ok();
302 }
303
304 /* ***************** */
305
306
307 Change_iterator::Change_iterator(Change_reg * ch)
308 {
309     change_l_ = ch;
310 }
311
312
313 IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator);
314
315 /*
316   TODO: pop/pushgroup
317  */
318 void
319 Change_iterator::process_and_next(Moment mom)
320 {
321 #if 0
322     if ( id[0] == '-') {
323         
324     
325     Engraver_group_engraver *group_l =
326         report_to_l()->find_get_translator_l(change_l_->type_str_, 
327                                              change_l_->id_str_);
328
329     report_to_l()->daddy_grav_l_->remove_engraver_p(report_to_l());
330     group_l->add(report_to_l());
331 #endif
332     Music_iterator::process_and_next(mom);
333 }
334
335
336
337 /* ******************** */
338
339
340 IMPLEMENT_IS_TYPE_B1(Request_chord_iterator,Music_iterator);
341
342 void
343 Request_chord_iterator::construct_children()
344 {
345     get_req_translator_l();
346 }
347
348 Request_chord_iterator::Request_chord_iterator(Request_chord*el_l)
349 {
350     elt_l_ = el_l;
351     elt_duration_ = el_l->time_int().length(); 
352     last_b_ = false;
353 }
354
355
356 bool
357 Request_chord_iterator::ok()const
358 {
359     return (elt_duration_ && !last_b_) || first_b_; 
360 }
361
362
363
364 Moment
365 Request_chord_iterator::next_moment()const
366 {
367     Moment m(0);
368     if  (!first_b_) 
369         m = elt_duration_;
370     return m;
371 }
372
373 void
374 Request_chord_iterator::do_print() const
375 {
376 #ifndef NPRINT
377     mtor << "duration: " << elt_duration_;
378 #endif
379 }
380 void
381 Request_chord_iterator::process_and_next(Moment mom)
382 {
383     if ( first_b_ ) {
384         for (PCursor<Music*> i(elt_l_->music_p_list_); i.ok(); i++) {
385             assert(i->is_type_b(Request::static_name()));
386             Request * req_l = (Request*)i.ptr();
387             bool gotcha = report_to_l()->try_request(req_l);
388             if (!gotcha)
389                 req_l->warning("Junking request: " + String(req_l->name()));
390
391         }
392         first_b_ = false;
393     }
394
395     if ( mom >= elt_duration_ )
396         last_b_ = true;  
397 }