2 music-iterator.cc -- implement {Music,Chord,Voice}_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9 #include "music-list.hh"
10 #include "music-iterator.hh"
11 #include "translator.hh"
15 IMPLEMENT_STATIC_NAME(Music_iterator);
16 IMPLEMENT_IS_TYPE_B(Music_iterator);
18 Chord_iterator::~Chord_iterator(){}
20 Music_iterator::do_print()const
26 Music_iterator::print() const
29 mtor << name() << "{";
30 mtor << "report to " << report_to_l() << " (" << report_to_l()->name() << ")\n";
31 mtor << "next at " << next_moment() << " ";
38 Music_iterator::get_req_translator_l()
40 assert(report_to_l());
41 if (report_to_l()->is_bottom_engraver_b() )
44 set_translator( report_to_l()->get_default_interpreter() );
49 Music_iterator::push_translator(Translator*t)
52 report_to_l_arr_.push(t);
53 t->iterator_count_ ++;
58 Music_iterator::pop_translator()
61 report_to_l()->iterator_count_ --;
62 report_to_l_arr_.pop();
67 Music_iterator::report_to_l()const
69 if (! report_to_l_arr_.size() )
71 return report_to_l_arr_.top();
76 Music_iterator::set_translator(Translator*reg)
78 if (report_to_l()==reg)
85 Music_iterator::construct_children()
90 Music_iterator::~Music_iterator()
96 Music_iterator::next_moment()const
102 Music_iterator::process_and_next(Moment)
108 Music_iterator::ok()const
114 Music_iterator::static_get_iterator_p(Music *m,
115 Translator *report_l)
117 Music_iterator * p =0;
118 if (m->is_type_b( Change_reg::static_name()))
119 p = new Change_iterator((Change_reg*)m);
120 else if (m->is_type_b( Voice_element::static_name()))
121 p = new Voice_element_iterator( (Voice_element*) m);
122 else if (m->is_type_b( Chord::static_name()))
123 p = new Chord_iterator( (Chord*) m);
124 else if (m->is_type_b( Voice::static_name()))
125 p = new Voice_iterator( (Voice*) m);
127 if ( m->is_type_b( Music_list::static_name())) {
128 Music_list* ml = (Music_list*) m;
129 if (ml -> type_str_ != "") {
130 Translator * a =report_l->
131 find_get_translator_l(ml-> type_str_, ml->id_str_);
134 p->set_translator( a);
138 if (! p->report_to_l() )
139 p ->set_translator(report_l);
145 Music_iterator::get_iterator_p(Music*m)const
147 Music_iterator*p = static_get_iterator_p(m,report_to_l());
148 p->daddy_iter_l_ = (Music_iterator*)this;
149 p->construct_children();
153 Music_iterator::Music_iterator()
161 Chord_iterator::Chord_iterator(Chord const *chord_C)
167 Chord_iterator::construct_children()
170 for(PCursor<Music*> i(chord_C_->music_p_list_.top()); //, int j = 0;
172 Music_iterator * mi = get_iterator_p( i.ptr());
173 set_translator(mi->report_to_l()->ancestor_l( chord_C_->multi_level_i_ ));
175 children_p_list_.bottom().add( mi );
181 Chord_iterator::do_print() const
184 for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++) {
191 Chord_iterator::process_and_next(Moment until)
193 for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); ) {
194 if (i->next_moment() == until) {
195 i->process_and_next(until);
202 Music_iterator::process_and_next(until);
204 // assert(!ok() || next_moment() > until);
207 IMPLEMENT_STATIC_NAME(Chord_iterator);
208 IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
211 Chord_iterator::next_moment()const
213 Moment next_ = INFTY;
214 for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++)
215 next_ = next_ <? i->next_moment() ;
222 Chord_iterator::ok()const
224 return children_p_list_.size();
230 Voice_iterator::do_print()const
236 Voice_iterator::Voice_iterator(Voice const*v)
237 : PCursor<Music*> ( v->music_p_list_)
239 here_mom_ = v->offset_mom_;
245 Voice_iterator::construct_children()
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());
255 Voice_iterator::next_element()
259 here_mom_ += ptr()->time_int().length();
260 PCursor<Music*>::next();
261 construct_children();
264 Voice_iterator::~Voice_iterator()
269 IMPLEMENT_STATIC_NAME(Voice_iterator);
270 IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
273 Voice_iterator::process_and_next(Moment until)
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)
281 iter_p_->process_and_next(local_until);
284 iter_p_ = Music_iterator::get_iterator_p( ptr() );
285 else if (!iter_p_->ok() )
288 Music_iterator::process_and_next(until);
289 assert(!ok() || next_moment() > until);
293 Voice_iterator::next_moment()const
295 return iter_p_->next_moment() + here_mom_;
299 Voice_iterator::ok()const
301 return PCursor<Music*>::ok();
304 /* ***************** */
307 Change_iterator::Change_iterator(Change_reg * ch)
312 IMPLEMENT_STATIC_NAME(Change_iterator);
313 IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator);
319 Change_iterator::process_and_next(Moment mom)
325 Engraver_group_engraver *group_l =
326 report_to_l()->find_get_translator_l(change_l_->type_str_,
329 report_to_l()->daddy_grav_l_->remove_engraver_p(report_to_l());
330 group_l->add(report_to_l());
332 Music_iterator::process_and_next(mom);
337 /* ******************** */
339 IMPLEMENT_STATIC_NAME(Voice_element_iterator);
340 IMPLEMENT_IS_TYPE_B1(Voice_element_iterator,Music_iterator);
343 Voice_element_iterator::construct_children()
345 get_req_translator_l();
348 Voice_element_iterator::Voice_element_iterator(Voice_element*el_l)
351 elt_duration_ = el_l->time_int().length();
357 Voice_element_iterator::ok()const
359 return (elt_duration_ && !last_b_) || first_b_;
365 Voice_element_iterator::next_moment()const
374 Voice_element_iterator::do_print() const
377 mtor << "duration: " << elt_duration_;
381 Voice_element_iterator::process_and_next(Moment mom)
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);
389 req_l->warning("Junking request: " + String(req_l->name()));
395 if ( mom >= elt_duration_ )