X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fmusic-iterator.cc;h=b89e92c28ec968f2f13bfbfff070a440d891ce7f;hb=7271ef0cd2204b490c64abc1f77dc2609d81451f;hp=09ee833db0ed22dc3295e198e1d3daf9c13939b4;hpb=50802c7203c8e4a3eea1d7bf23064d60cd0d3ec7;p=lilypond.git diff --git a/lily/music-iterator.cc b/lily/music-iterator.cc index 09ee833db0..b89e92c28e 100644 --- a/lily/music-iterator.cc +++ b/lily/music-iterator.cc @@ -1,397 +1,266 @@ /* - music-iterator.cc -- implement {Music,Chord,Voice}_iterator + This file is part of LilyPond, the GNU music typesetter. - source file of the GNU LilyPond music typesetter + Copyright (C) 1997--2015 Han-Wen Nienhuys - (c) 1997 Han-Wen Nienhuys -*/ + LilyPond is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. -#include "music-list.hh" -#include "music-iterator.hh" -#include "translator.hh" -#include "request.hh" -#include "debug.hh" + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . +*/ -IMPLEMENT_IS_TYPE_B(Music_iterator); +#include +using namespace std; -Chord_iterator::~Chord_iterator() -{ -} +#include "warn.hh" +#include "music.hh" +#include "context.hh" +#include "event-iterator.hh" +#include "input.hh" +#include "international.hh" +#include "music-wrapper.hh" +#include "music-wrapper-iterator.hh" +#include "simple-music-iterator.hh" -void -Music_iterator::do_print()const -{ -} - -void -Music_iterator::print() const +Music_iterator::Music_iterator () { -#ifndef NPRINT - mtor << name() << "{"; - mtor << "report to " << - report_to_l() << " (" << report_to_l()->name() << ")\n"; - mtor << "next at " << next_moment() << " "; - do_print(); - mtor << "}\n"; -#endif + music_ = 0; + smobify_self (); } -Translator * -Music_iterator::get_req_translator_l() +Music_iterator::~Music_iterator () { - assert(report_to_l()); - if (report_to_l()->is_bottom_engraver_b() ) - return report_to_l(); - - set_translator( report_to_l()->get_default_interpreter() ); - return report_to_l(); } -void -Music_iterator::push_translator(Translator*t) +Context * +Music_iterator::get_outlet () const { - if (t) { - report_to_l_arr_.push(t); - t->iterator_count_ ++; - } + return handle_.get_context (); } void -Music_iterator::pop_translator() -{ - if (report_to_l()) { - report_to_l()->iterator_count_ --; - report_to_l_arr_.pop(); - } -} - -Translator* -Music_iterator::report_to_l()const +Music_iterator::set_context (Context *trans) { - if (! report_to_l_arr_.size() ) - return 0; - return report_to_l_arr_.top(); + handle_.set_context (trans); } - void -Music_iterator::set_translator(Translator*reg) -{ - if (report_to_l()==reg) - return; - pop_translator(); - push_translator(reg); -} - -void -Music_iterator::construct_children() +Music_iterator::construct_children () { - -} - -Music_iterator::~Music_iterator() -{ - set_translator(0); } Moment -Music_iterator::next_moment()const +Music_iterator::pending_moment () const { - return 0; + return 0; } void -Music_iterator::process_and_next(Moment) +Music_iterator::process (Moment) { - first_b_ = false; } bool -Music_iterator::ok()const -{ - return first_b_; -} - -Music_iterator* -Music_iterator::static_get_iterator_p(Music *m, - Translator *report_l) +Music_iterator::ok () const { - Music_iterator * p =0; - if (m->is_type_b( Change_reg::static_name())) - p = new Change_iterator((Change_reg*)m); - else if (m->is_type_b( Request_chord::static_name())) - p = new Request_chord_iterator( (Request_chord*) m); - else if (m->is_type_b( Chord::static_name())) - p = new Chord_iterator( (Chord*) m); - else if (m->is_type_b( Voice::static_name())) - p = new Voice_iterator( (Voice*) m); - - if (m -> type_str_ != "") { - Translator * a =report_l-> - find_get_translator_l(m-> type_str_, m->id_str_); - p->set_translator( a); - } - - - if (! p->report_to_l() ) - p ->set_translator(report_l); - - return p; + return false; } -Music_iterator* -Music_iterator::get_iterator_p(Music*m)const +SCM +Music_iterator::get_static_get_iterator (Music *m) { - Music_iterator*p = static_get_iterator_p(m,report_to_l()); - p->daddy_iter_l_ = (Music_iterator*)this; - p->construct_children(); - return p; -} + Music_iterator *p = 0; -Music_iterator::Music_iterator() -{ - daddy_iter_l_ =0; - first_b_ = true; -} - -/* ************** */ - -Chord_iterator::Chord_iterator(Chord const *chord_C) -{ - chord_C_ = chord_C; -} - -void -Chord_iterator::construct_children() -{ - int j = 0; - for(PCursor i(chord_C_->music_p_list_.top()); //, int j = 0; - i.ok(); j++, i++) { - Music_iterator * mi = get_iterator_p( i.ptr()); - set_translator(mi->report_to_l()->ancestor_l( chord_C_->multi_level_i_ )); - if ( mi->ok() ) - children_p_list_.bottom().add( mi ); - else - delete mi; + SCM ctor = m->get_property ("iterator-ctor"); + SCM iter = SCM_EOL; + if (ly_is_procedure (ctor)) + { + iter = scm_call_0 (ctor); + p = unsmob (iter); } -} -void -Chord_iterator::do_print() const -{ -#ifndef NPRINT - for (PCursor i(children_p_list_.top()); i.ok(); i++) { - i->print(); + else + { + if (dynamic_cast (m)) + p = new Music_wrapper_iterator; + else if (m->is_mus_type ("event")) + p = new Event_iterator; + else + p = new Simple_music_iterator; + + iter = p->self_scm (); + p->unprotect (); } -#endif -} -void -Chord_iterator::process_and_next(Moment until) -{ - for (PCursor i(children_p_list_.top()); i.ok(); ) { - if (i->next_moment() == until) { - i->process_and_next(until); - } - if (!i->ok()) - delete i.remove_p(); - else - i++; - } - Music_iterator::process_and_next(until); + p->music_ = m; + assert (m); + p->music_length_ = m->get_length (); + p->start_mom_ = m->start_mom (); -// assert(!ok() || next_moment() > until); + return iter; } - -IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator); - Moment -Chord_iterator::next_moment()const +Music_iterator::music_get_length () const { - Moment next_ = INFTY_f; - for (PCursor i(children_p_list_.top()); i.ok(); i++) - next_ = next_ next_moment() ; - return next_; + return music_length_; } - - -bool -Chord_iterator::ok()const +Moment +Music_iterator::music_start_mom ()const { - return children_p_list_.size(); + return start_mom_; } -/* ************** */ - void -Voice_iterator::do_print()const -{ - if (iter_p_) - iter_p_->print(); -} - -Voice_iterator::Voice_iterator(Voice const*v) - : PCursor ( v->music_p_list_) +Music_iterator::init_context (Music *m, Context *report) { - here_mom_ = v->offset_mom_; - voice_C_ = v; - iter_p_ =0; + music_ = m; + assert (m); + if (! get_outlet ()) + set_context (report); } void -Voice_iterator::construct_children() +Music_iterator::substitute_outlet (Context *f, Context *t) { - if (ok()) { - iter_p_ = Music_iterator::get_iterator_p( ptr() ); - if (iter_p_->report_to_l()->depth_i() > report_to_l()->depth_i()) - set_translator(iter_p_->report_to_l()); + if (f != t) + { + if (get_outlet () == f) + set_context (t); + derived_substitute (f, t); } } void -Voice_iterator::next_element() +Music_iterator::derived_substitute (Context *, Context *) { - delete iter_p_ ; - iter_p_ =0; - here_mom_ += ptr()->time_int().length(); - PCursor::next(); - construct_children(); } -Voice_iterator::~Voice_iterator() +SCM +Music_iterator::get_iterator (Music *m) const { - delete iter_p_; -} + SCM ip = get_static_get_iterator (m); + Music_iterator *p = unsmob (ip); + p->init_context (m, get_outlet ()); -IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator); + p->construct_children (); + return ip; +} +/* Descend to a bottom context; implicitly create a new one if necessary */ void -Voice_iterator::process_and_next(Moment until) +Music_iterator::descend_to_bottom_context () { - while (ok()) { - Moment local_until = until - here_mom_; - while ( iter_p_ && iter_p_->ok() ) { - Moment here = iter_p_->next_moment(); - if (here != local_until) - return; - iter_p_->process_and_next(local_until); - } - if (!iter_p_) - iter_p_ = Music_iterator::get_iterator_p( ptr() ); - else if (!iter_p_->ok() ) - next_element(); - } - Music_iterator::process_and_next(until); - assert(!ok() || next_moment() > until); + assert (get_outlet ()); + if (!get_outlet ()->is_bottom_context ()) + set_context (get_outlet ()->get_default_interpreter ()); } -Moment -Voice_iterator::next_moment()const +void +Music_iterator::report_event (Music *m) { - return iter_p_->next_moment() + here_mom_; + descend_to_bottom_context (); + + /* + FIXME: then don't do it. + */ + if (!m->is_mus_type ("event")) + m->origin ()->programming_error ("Sending non-event to context"); + + m->send_to_context (get_outlet ()); } -bool -Voice_iterator::ok()const +IMPLEMENT_CTOR_CALLBACK (Music_iterator); + +Music * +Music_iterator::get_music () const { - return PCursor::ok(); + return music_; } -/* ***************** */ +/****************************************************************/ +const char Music_iterator::type_p_name_[] = "ly:iterator?"; -Change_iterator::Change_iterator(Change_reg * ch) +SCM +Music_iterator::mark_smob () const { - change_l_ = ch; + derived_mark (); + /* + Careful with GC, although we intend the following as pointers + only, we _must_ mark them. + */ + /* Use handle_ directly as get_outlet is a virtual function and we + need to protect the context until Music_iterator::quit is being + run. */ + if (handle_.get_context ()) + scm_gc_mark (handle_.get_context ()->self_scm ()); + if (music_) + scm_gc_mark (music_->self_scm ()); + + return SCM_EOL; } +int +Music_iterator::print_smob (SCM port, scm_print_state *) const +{ + char s[1000]; -IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator); + sprintf (s, "#<%s>", class_name ()); + scm_puts (s, port); + return 1; +} -/* - TODO: pop/pushgroup - */ void -Change_iterator::process_and_next(Moment mom) +Music_iterator::derived_mark ()const { -#if 0 - if ( id[0] == '-') { - - - Engraver_group_engraver *group_l = - report_to_l()->find_get_translator_l(change_l_->type_str_, - change_l_->id_str_); - - report_to_l()->daddy_grav_l_->remove_engraver_p(report_to_l()); - group_l->add(report_to_l()); -#endif - Music_iterator::process_and_next(mom); } - - -/* ******************** */ - - -IMPLEMENT_IS_TYPE_B1(Request_chord_iterator,Music_iterator); - void -Request_chord_iterator::construct_children() +Music_iterator::quit () { - get_req_translator_l(); + do_quit (); + handle_.set_context (0); } -Request_chord_iterator::Request_chord_iterator(Request_chord*el_l) +void +Music_iterator::do_quit () { - elt_l_ = el_l; - elt_duration_ = el_l->time_int().length(); - last_b_ = false; } - bool -Request_chord_iterator::ok()const +Music_iterator::run_always ()const { - return (elt_duration_ && !last_b_) || first_b_; + return false; } - - -Moment -Request_chord_iterator::next_moment()const +bool +is_child_context (Context *me, Context *child) { - Moment m(0); - if (!first_b_) - m = elt_duration_; - return m; -} + while (child && child != me) + child = child->get_parent_context (); -void -Request_chord_iterator::do_print() const -{ -#ifndef NPRINT - mtor << "duration: " << elt_duration_; -#endif + return child == me; } + +/* + move to context of child iterator if it is deeper down in the + hierarchy. +*/ void -Request_chord_iterator::process_and_next(Moment mom) +Music_iterator::descend_to_child (Context *child_report) { - if ( first_b_ ) { - for (PCursor i(elt_l_->music_p_list_); i.ok(); i++) { - assert(i->is_type_b(Request::static_name())); - Request * req_l = (Request*)i.ptr(); - bool gotcha = report_to_l()->try_request(req_l); - if (!gotcha) - req_l->warning("Junking request: " + String(req_l->name())); - - } - first_b_ = false; - } - - if ( mom >= elt_duration_ ) - last_b_ = true; + Context *me_report = get_outlet (); + if (is_child_context (me_report, child_report)) + set_context (child_report); }