]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/music-iterator.cc
catch GUILE errors
[lilypond.git] / lily / music-iterator.cc
index 25e5f2b63f393d7d38276bb80187513469d27a64..dcb596a117071a3d8fe78610b3c8992e0635a25c 100644 (file)
 /*
-  music-iterator.cc -- implement {Music,Chord,Voice}_iterator
+  music-iterator.cc -- implement Music_iterator
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
-#include "music-list.hh"
+/*
+  UGH. too many includes.
+ */
+#include "warn.hh"
 #include "music-iterator.hh"
-#include "translator.hh"
-#include "request.hh"
-#include "debug.hh"
-
-
-IMPLEMENT_IS_TYPE_B(Music_iterator);
+#include "translator-group.hh"
+#include "music-wrapper.hh"
+#include "music-wrapper-iterator.hh"
+#include "simple-music-iterator.hh"
+#include "context-specced-music.hh"
 
-Chord_iterator::~Chord_iterator()
+Music_iterator::Music_iterator ()
 {
 }
 
-void
-Music_iterator::do_print()const
+Music_iterator::Music_iterator (Music_iterator const& src)
 {
-
+  handle_ = *src.handle_.clone ();
+  music_ = src.music_;
+  music_length_ = src.music_length_;
+  start_mom_ = src.start_mom_;
 }
 
-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
 }
 
-Translator *
-Music_iterator::get_req_translator_l()
-{
-    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)
-{
-    if (t) {
-       report_to_l_arr_.push(t);
-       t->iterator_count_ ++;
-    }
-}
 
-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
+Translator_group
+Music_iterator::report_to () const
 {
-    if (! report_to_l_arr_.size() )
-       return 0;
-    return report_to_l_arr_.top();
+  return handle_.report_to ();
 }
 
 
 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::set_translator (Translator_group *trans)
 {
-
+  handle_.set_translator (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
-{
-    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
+void
+Music_iterator::skip (Moment)
 {
-    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::Music_iterator()
+SCM
+Music_iterator::get_pending_events (Moment)const
 {
-    daddy_iter_l_ =0;
-    first_b_ = true;
+  return SCM_EOL;
 }
 
-/* ************** */
-
-Chord_iterator::Chord_iterator(Chord const *chord_C)
+Music_iterator*
+Music_iterator::get_static_get_iterator (Music *m)
 {
-    chord_C_ = chord_C;
-}
+  Music_iterator * p =0;
 
-void
-Chord_iterator::construct_children()
-{
-    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());
-       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_mus_property ("iterator-ctor") ;
+  if (unsmob_cxx_function (ctor))
+    {
+      Cxx_function f =  unsmob_cxx_function (ctor);
+      
+      p = (Music_iterator*) (*f) (SCM_EOL);
     }
-}
-void
-Chord_iterator::do_print() const
-{
-#ifndef NPRINT
-    for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++) {
-       i->print();
+  else if (dynamic_cast<Music_wrapper   *> (m))
+    p = new Music_wrapper_iterator;
+  else
+    {
+      p = new Simple_music_iterator ;
     }
-#endif
-}
 
-void
-Chord_iterator::process_and_next(Moment until)
-{
-    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);
+  p->music_ = m;
+  assert (m);
+  p->music_length_ = m->length_mom ();
+  p->start_mom_ = m->start_mom ();
+  return p;
 }
 
 
-IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
-
 Moment
-Chord_iterator::next_moment()const
-{
-    Moment next_ = infinity_mom;
-    for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++)
-       next_ = next_ <? i->next_moment() ;
-    return next_;
-}
-
-
-
-bool
-Chord_iterator::ok()const
-{
-    return children_p_list_.size();
-}
-
-/* ************** */
-
-void
-Voice_iterator::do_print()const
+Music_iterator::music_length_mom () const
 {
-    if (iter_p_)
-       iter_p_->print();
-}
+  return music_length_;
 
-Voice_iterator::Voice_iterator(Voice const*v)
-    : PCursor<Music*> ( v->music_p_list_)
-{
-    here_mom_ = v->offset_mom_;
-    voice_C_ = v;
-    iter_p_ =0;
 }
 
-void
-Voice_iterator::construct_children()
+Moment
+Music_iterator::music_start_mom ()const
 {
-    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());
-    }
+  return start_mom_;
 }
 
 void
-Voice_iterator::next_element()
+Music_iterator::init_translator (Music *m, Translator_group *report)
 {
-    delete iter_p_ ;
-    iter_p_ =0;
-    here_mom_ += ptr()->time_int().length();
-    PCursor<Music*>::next();
-    construct_children();
-}
+  music_ = m;
+  assert (m);
+  if (Context_specced_music * csm =dynamic_cast<Context_specced_music *> (m))
+    {
+      SCM ct = csm->get_mus_property ("context-type");
+      String c_type;
+      if (gh_string_p (ct))
+         c_type =  ly_scm2string (ct);
+      
+      String c_id;
+      SCM ci = csm->get_mus_property ("context-id");
+      if (gh_string_p (ci))
+       c_id = ly_scm2string (ci);
+      
+      Translator_group* a
+       =report->find_create_translator (c_type, c_id);
 
-Voice_iterator::~Voice_iterator()
-{
-    delete iter_p_;
-}
-
-
-IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
-
-void
-Voice_iterator::process_and_next(Moment until)
-{
-    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();
+      set_translator (a);
+      
     }
-    Music_iterator::process_and_next(until);
-    assert(!ok() || next_moment() > until);
-}
 
-Moment
-Voice_iterator::next_moment()const
-{
-    return iter_p_->next_moment() + here_mom_;
+  if (! report_to ())
+    set_translator (report);
 }
 
-bool
-Voice_iterator::ok()const
-{
-    return PCursor<Music*>::ok();
-}
 
-/* ***************** */
-
-
-Change_iterator::Change_iterator(Change_reg * ch)
+Music_iterator*
+Music_iterator::get_iterator (Music *m) const
 {
-    change_l_ = ch;
+  Music_iterator*p = get_static_get_iterator (m);
+  p->init_translator (m, report_to ());
+  
+  p->construct_children ();
+  return p;
 }
 
-
-IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator);
-
 /*
-  TODO: pop/pushgroup
+  TODO: rename to prevent confusion between Translator::try_music and
+  Iterator::try_music
+  
  */
-void
-Change_iterator::process_and_next(Moment mom)
-{
-#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()
-{
-    get_req_translator_l();
-}
-
-Request_chord_iterator::Request_chord_iterator(Request_chord*el_l)
+Music_iterator*
+Music_iterator::try_music (Music *m) const
 {
-    elt_l_ = el_l;
-    elt_duration_ = el_l->time_int().length(); 
-    last_b_ = false;
+  bool b = report_to ()->try_music ((Music*)m); // ugh
+  Music_iterator * it = b ? (Music_iterator*) this : 0;        // ugh
+  if (!it)
+    it = try_music_in_children (m);
+  return it;
 }
 
-
-bool
-Request_chord_iterator::ok()const
+Music_iterator*
+Music_iterator::try_music_in_children (Music *) const
 {
-    return (elt_duration_ && !last_b_) || first_b_; 
+  return 0;
 }
 
+IMPLEMENT_CTOR_CALLBACK (Music_iterator);
 
-
-Moment
-Request_chord_iterator::next_moment()const
+Music *
+Music_iterator::get_music () const
 {
-    Moment m(0);
-    if  (!first_b_) 
-       m = elt_duration_;
-    return m;
-}
-
-void
-Request_chord_iterator::do_print() const
-{
-#ifndef NPRINT
-    mtor << "duration: " << elt_duration_;
-#endif
-}
-void
-Request_chord_iterator::process_and_next(Moment mom)
-{
-    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;  
+  return music_;
 }