]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/music-iterator.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / music-iterator.cc
index cf3f4f13ea81a3b2d6a5fcc64954d1bab030c572..c875b060a565b1db6cf9bd8d15ac43a17acee364 100644 (file)
 /*
-  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--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
-*/
-
-#include "music-list.hh"
-#include "music-iterator.hh"
-#include "translator.hh"
-#include "request.hh"
-#include "debug.hh"
+  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.
 
+  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.
 
-IMPLEMENT_IS_TYPE_B(Music_iterator);
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-Chord_iterator::~Chord_iterator()
-{
-}
+#include <cstdio>
+using namespace std;
 
-void
-Music_iterator::do_print()const
-{
+#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"
 
-}
+#include "ly-smobs.icc"
 
-void
-Music_iterator::print() const
+Music_iterator::Music_iterator ()
 {
-#ifndef NPRINT
-    if ( !check_debug)
-       return ;
-    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 (Music_iterator const &)
 {
-    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();
+  assert (false);
 }
 
-void
-Music_iterator::push_translator(Translator*t)
-{
-    report_to_l_arr_.push(t);
-    t->iterator_count_ ++;
-}
-
-void
-Music_iterator::pop_translator()
+Music_iterator::~Music_iterator ()
 {
-    report_to_l()->iterator_count_ --;
-    assert (report_to_l()->iterator_count_ >=0);
-    report_to_l_arr_.pop();
 }
 
-Translator* 
-Music_iterator::report_to_l()const
+Context *
+Music_iterator::get_outlet () const
 {
-    if (! report_to_l_arr_.size() )
-       return 0;
-    return report_to_l_arr_.top();
-}
-
-
-void
-Music_iterator::set_translator(Translator*trans)
-{   
-    if (report_to_l()==trans)
-       return;
-    if ( report_to_l() )
-       pop_translator();
-    if (trans)
-       push_translator(trans);
+  return handle_.get_context ();
 }
 
 void
-Music_iterator::construct_children()
+Music_iterator::set_context (Context *trans)
 {
-
+  handle_.set_context (trans);
 }
 
-Music_iterator::~Music_iterator()
+void
+Music_iterator::construct_children ()
 {
-    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
+Music_iterator::ok () const
 {
-    return first_b_;
+  return false;
 }
 
-Music_iterator*
-Music_iterator::static_get_iterator_p(Music *m,
-                                     Translator *report_l)
+SCM
+Music_iterator::get_static_get_iterator (Music *m)
 {
-    Music_iterator * p =0;
-    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;
-}
+  Music_iterator *p = 0;
 
-Music_iterator*
-Music_iterator::get_iterator_p(Music*m)const
-{
-    Music_iterator*p = static_get_iterator_p(m,report_to_l());
-    p->daddy_iter_l_ = (Music_iterator*)this;
-    p->construct_children();
-    return p;
+  SCM ctor = m->get_property ("iterator-ctor");
+  SCM iter = SCM_EOL;
+  if (ly_is_procedure (ctor))
+    {
+      iter = scm_call_0 (ctor);
+      p = unsmob_iterator (iter);
+    }
+  else
+    {
+      if (dynamic_cast<Music_wrapper *> (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 ();
+    }
+
+  p->music_ = m;
+  assert (m);
+  p->music_length_ = m->get_length ();
+  p->start_mom_ = m->start_mom ();
+
+  return iter;
 }
 
-Music_iterator::Music_iterator()
+Moment
+Music_iterator::music_get_length () const
 {
-    daddy_iter_l_ =0;
-    first_b_ = true;
+  return music_length_;
 }
 
-/* ************** */
-
-Chord_iterator::Chord_iterator(Chord const *chord_C)
+Moment
+Music_iterator::music_start_mom ()const
 {
-    chord_C_ = chord_C;
+  return start_mom_;
 }
 
 void
-Chord_iterator::construct_children()
+Music_iterator::init_context (Music *m, Context *report)
 {
-    int j = 0;
-    for(PCursor<Music*> i(chord_C_->music_p_list_.top());  //, int j = 0; 
-       i.ok(); j++, i++) {
-       Music_iterator * mi =  get_iterator_p( i.ptr());
-       if ( mi->ok() ) {
-           set_translator(mi->report_to_l()->ancestor_l( 
-               chord_C_->multi_level_i_ ) );
-           children_p_list_.bottom().add( mi );
-       } else 
-           delete mi;
-    }
+  music_ = m;
+  assert (m);
+  if (! get_outlet ())
+    set_context (report);
 }
+
 void
-Chord_iterator::do_print() const
+Music_iterator::substitute_outlet (Context *f, Context *t)
 {
-#ifndef NPRINT
-    for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++) {
-       i->print();
-    }
-#endif
+  if (get_outlet () == f)
+    set_context (t);
+  derived_substitute (f, t);
 }
 
 void
-Chord_iterator::process_and_next(Moment until)
+Music_iterator::derived_substitute (Context *, Context *)
 {
-    for (PCursor<Music_iterator*> 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);
 }
 
-
-IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
-
-Moment
-Chord_iterator::next_moment()const
+SCM
+Music_iterator::get_iterator (Music *m) const
 {
-    Moment next_ = infinity_mom;
-    for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++)
-       next_ = next_ <? i->next_moment() ;
-    return next_;
-}
+  SCM ip = get_static_get_iterator (m);
+  Music_iterator *p = unsmob_iterator (ip);
 
+  p->init_context (m, get_outlet ());
 
-
-bool
-Chord_iterator::ok()const
-{
-    return children_p_list_.size();
+  p->construct_children ();
+  return ip;
 }
 
-/* ************** */
-
+/* Descend to a bottom context; implicitly create a new one if necessary */
 void
-Voice_iterator::do_print()const
-{
-    if (iter_p_)
-       iter_p_->print();
-}
-
-Voice_iterator::Voice_iterator(Voice const*v)
-    : PCursor<Music*> ( v->music_p_list_)
+Music_iterator::descend_to_bottom_context ()
 {
-    here_mom_ = v->offset_mom_;
-    voice_C_ = v;
-    iter_p_ =0;
+  assert (get_outlet ());
+  if (!get_outlet ()->is_bottom_context ())
+    set_context (get_outlet ()->get_default_interpreter ());
 }
 
 void
-Voice_iterator::construct_children()
+Music_iterator::report_event (Music *m)
 {
-    while (PCursor<Music*>::ok()) {
-       start_next_element();
-       if ( !iter_p_->ok()) {
-           leave_element();
-       } else {
-           set_voice_translator();
-           break;
-       }
-    }
-}
+  descend_to_bottom_context ();
 
-void 
-Voice_iterator::leave_element()
-{
-    delete iter_p_;
-    iter_p_ =0;
-    MInterval elt_time = ptr()->time_int();
-    if (!elt_time.empty_b())
-       here_mom_ += elt_time.length();
-    PCursor<Music*>::next();
-}
+  /*
+    FIXME: then don't do it.
+  */
+  if (!m->is_mus_type ("event"))
+    m->origin ()->programming_error ("Sending non-event to context");
 
-void
-Voice_iterator::start_next_element()
-{
-    assert( !iter_p_);
-    iter_p_ = get_iterator_p( ptr());
+  m->send_to_context (get_outlet ());
 }
 
-void
-Voice_iterator::set_voice_translator()
-{
-    if (iter_p_->report_to_l()->depth_i() > report_to_l()->depth_i())
-       set_translator(iter_p_->report_to_l());
-}
+IMPLEMENT_CTOR_CALLBACK (Music_iterator);
 
-Voice_iterator::~Voice_iterator()
+Music *
+Music_iterator::get_music () const
 {
-    assert(! iter_p_);
+  return music_;
 }
 
+/****************************************************************/
 
-IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
+IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
+IMPLEMENT_SMOBS (Music_iterator);
+IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator);
 
-void
-Voice_iterator::process_and_next(Moment until)
+SCM
+Music_iterator::mark_smob (SCM smob)
 {
-    while (1) {
-       Moment local_until = until - here_mom_;
-       while ( iter_p_->ok() ) {
-           Moment here = iter_p_->next_moment();
-           if (here != local_until)
-               goto loopexit;
-           
-           iter_p_->process_and_next(local_until);
-       }
-
-       if (!iter_p_->ok() ) {
-           leave_element();
-           if ( PCursor<Music*>::ok() ) {
-               start_next_element();
-               set_voice_translator();
-           } else {
-               goto loopexit;
-           }
-       }
-    }
+  Music_iterator *mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
 
-loopexit:
+  mus->derived_mark ();
+  /*
+    Careful with GC, although we intend the following as pointers
+    only, we _must_ mark them.
+  */
+  if (mus->get_outlet ())
+    scm_gc_mark (mus->get_outlet ()->self_scm ());
+  if (mus->music_)
+    scm_gc_mark (mus->music_->self_scm ());
 
-    Music_iterator::process_and_next(until);
+  return SCM_EOL;
 }
 
-Moment
-Voice_iterator::next_moment()const
+int
+Music_iterator::print_smob (SCM sm, SCM port, scm_print_state *)
 {
-    return iter_p_->next_moment() + here_mom_;
+  char s[1000];
+
+  Music_iterator *iter = unsmob_iterator (sm);
+  sprintf (s, "#<%s>", iter->class_name ());
+  scm_puts (s, port);
+  return 1;
 }
 
-bool
-Voice_iterator::ok()const
+void
+Music_iterator::derived_mark ()const
 {
-    return iter_p_;
 }
 
-
-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<Music*> 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);
 }