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