]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/music-iterator.cc
release: 1.0.1
[lilypond.git] / lily / music-iterator.cc
index 3e8befb193c4b9bcf1305835fad9544e9adf879b..97352020af20712f53b0566f4e7d7d0fe54ae7cd 100644 (file)
@@ -1,23 +1,31 @@
 /*
-  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--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
-
+#include "debug.hh"
 #include "music-list.hh"
 #include "music-iterator.hh"
-#include "acceptor.hh"
-#include "request.hh"
-#include "interpreter.hh"
-#include "debug.hh"
+#include "voice-iterator.hh"
+#include "property-iterator.hh"
+#include "chord-iterator.hh"
+#include "request-iterator.hh"
+#include "translator-group.hh"
+#include "translation-property.hh"
+#include "change-iterator.hh"
+#include "change-translator.hh"
+
+#include "music-wrapper.hh"
+#include "music-wrapper-iterator.hh"
+
 
-IMPLEMENT_STATIC_NAME(Music_iterator);
 IMPLEMENT_IS_TYPE_B(Music_iterator);
 
+
 void
-Music_iterator::do_print()const
+Music_iterator::do_print() const
 {
 
 }
@@ -26,361 +34,141 @@ void
 Music_iterator::print() const
 {
 #ifndef NPRINT
-    mtor << name() << "{";
-    mtor << "report to " << report_to_l_ << " (" << report_to_l_->name() << ")\n";
-    mtor << "next at " << next_moment() << " ";
-    do_print();
-    mtor << "}\n";
+  if (!check_debug)
+    return ;
+  DOUT << name() << "{";
+  DOUT << "report to " << 
+    report_to_l() << " (" << report_to_l ()->name () << ")\n";
+  if (ok())
+    DOUT << "next at " << next_moment() << " ";
+  else
+    DOUT << "not feeling well today..";
+  do_print();
+  DOUT << "}\n";
 #endif
 }
 
-Acceptor*
-Music_iterator::get_req_acceptor_l()
+Translator_group*
+Music_iterator::get_req_translator_l()
 {
-    assert(report_to_l_);
-    if (report_to_l_->interpreter_l() )
-       return report_to_l_;
+  assert (report_to_l());
+  if (report_to_l()->is_bottom_translator_b ())
+    return report_to_l();
 
-    set_acceptor( report_to_l_->get_default_interpreter() );
-    return report_to_l_;
+  set_translator (report_to_l()->get_default_interpreter ());
+  return report_to_l();
 }
 
 void
-Music_iterator::set_acceptor(Acceptor*reg)
-{    
-    if (report_to_l_==reg)
-       return;
-
-    if (report_to_l_)
-       report_to_l_->iterator_count_ --;
-
-    report_to_l_ = reg;
-    if (report_to_l_)
-       report_to_l_->iterator_count_ ++;
-}
-
-void
-Music_iterator::construct_children()
-{
-
-}
-
-Music_iterator::~Music_iterator()
-{
-    set_acceptor(0);
-}
-
-Moment
-Music_iterator::next_moment()const
+Music_iterator::push_translator (Translator_group*t)
 {
-    return 0;
+  report_to_l_arr_.push (t);
+  t->iterator_count_ ++;
 }
 
 void
-Music_iterator::next(Moment)
-{
-    first_b_ = false;
-}
-
-bool
-Music_iterator::ok()const
-{
-    return first_b_;
-}
-
-Music_iterator*
-Music_iterator::static_get_iterator_p(Music *m,
-                                     Acceptor *report_l)
+Music_iterator::pop_translator()
 {
-    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( Voice_element::static_name()))
-       p = new Voice_element_iterator( (Voice_element*) 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);
-    else if (m->is_type_b( Request::static_name() ))
-       p =  new Request_iterator(  (Request*) m );
-    
-     if ( m->is_type_b( Music_list::static_name())) {
-       Music_list* ml = (Music_list*) m;
-       if (ml -> type_str_ != "") {
-           Acceptor * a =report_l->
-               find_get_acceptor_l(ml-> type_str_, ml->id_str_);
-
-               
-           p->set_acceptor( a);
-           
-       } 
-     } 
-     if (! p->report_to_l_ )
-        p ->set_acceptor(report_l);
-    
-    return p;
-}
-
-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;
+  report_to_l()->iterator_count_ --;
+  assert (report_to_l()->iterator_count_ >=0);
+  report_to_l_arr_.pop();
 }
 
-Music_iterator::Music_iterator()
+Translator_group* 
+Music_iterator::report_to_l() const
 {
-    daddy_iter_l_ =0;
-    report_to_l_ = 0;
-    first_b_ = true;
+  if (! report_to_l_arr_.size())
+    return 0;
+  return report_to_l_arr_.top();
 }
 
-/* ************** */
-
-Chord_iterator::Chord_iterator(Chord const *chord_C)
-{
-    chord_C_ = chord_C;
-}
 
 void
-Chord_iterator::construct_children()
-{
-    int j =0;
-    for(iter(chord_C_->music_p_list_.top(), i); i.ok(); j++, i++) {
-       
-       Music_iterator * mi =  get_iterator_p( i.ptr());
-       set_acceptor(mi->report_to_l_->ancestor_l( chord_C_->multi_level_i_ ));
-       if ( mi->ok() )
-           children_p_list_.bottom().add( mi );
-       else 
-           delete mi;
-    }
-}
-void
-Chord_iterator::do_print() const
-{
-#ifndef NPRINT
-     for (iter(children_p_list_.top(), i); i.ok(); i++ ) {
-       i->print();
-     }
-#endif
+Music_iterator::set_translator (Translator_group*trans)
+{   
+  if (report_to_l()==trans)
+    return;
+  if (report_to_l())
+    pop_translator();
+  if (trans)
+    push_translator (trans);
 }
 
 void
-Chord_iterator::next(Moment until)
-{
-    for (iter(children_p_list_.top(), i); i.ok(); ) {
-       if  (i->next_moment() == until) {
-           i->next(until);
-       }
-       if (!i->ok()) 
-           delete i.remove_p();
-       else
-           i++;
-    }
-    Music_iterator::next(until);
-
-//    assert(!ok() || next_moment() > until);
-}
-
-IMPLEMENT_STATIC_NAME(Chord_iterator);
-IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
-
-Moment
-Chord_iterator::next_moment()const
-{
-    Moment next_ = INFTY;
-    for (iter(children_p_list_.top(), i); 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
-{
-    if (iter_p_)
-       iter_p_->print();
-}
-
-Voice_iterator::Voice_iterator(Voice const*v)
-    : PCursor<Music*> ( v->music_p_list_)
+Music_iterator::construct_children()
 {
-    here_mom_ = v->offset_mom_;
-    voice_C_ = v;
-    iter_p_ =0;
-}
 
-void
-Voice_iterator::construct_children()
-{
-    if (ok()) {
-       iter_p_ = Music_iterator::get_iterator_p( ptr() );      
-       if (iter_p_->report_to_l_->depth_i() > report_to_l_->depth_i())
-           set_acceptor(iter_p_->report_to_l_);
-    }
 }
 
-void
-Voice_iterator::next_element()
+Music_iterator::~Music_iterator()
 {
-    delete iter_p_ ;
-    iter_p_ =0;
-    here_mom_ += ptr()->time_int().length();
-    PCursor<Music*>::next();
-    construct_children();
+  set_translator (0);
 }
 
-Voice_iterator::~Voice_iterator()
+Moment
+Music_iterator::next_moment() const
 {
-    delete iter_p_;
+  return 0;
 }
 
-IMPLEMENT_STATIC_NAME(Voice_iterator);
-IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
-
 void
-Voice_iterator::next(Moment until)
+Music_iterator::process_and_next (Moment)
 {
-    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_->next(local_until);
-       }
-       if (!iter_p_)
-           iter_p_ = Music_iterator::get_iterator_p( ptr() );
-       else if (!iter_p_->ok() )
-           next_element();
-    }
-    Music_iterator::next(until);
-    assert(!ok() || next_moment() > until);
+  first_b_ = false;
 }
 
-Moment
-Voice_iterator::next_moment()const
-{
-    return iter_p_->next_moment() + here_mom_;
-}
 
 bool
-Voice_iterator::ok()const
+Music_iterator::ok() const
 {
-    return PCursor<Music*>::ok();
+  return first_b_;
 }
 
-/* ***************** */
-void
-Request_iterator::do_print()const
-{
-    mtor << req_l_->name() ;
-}
-Request_iterator::Request_iterator(Request const*c)
-{
-    req_l_ = (Request*)c;
-    last_b_ = false;
-}
-
-void
-Request_iterator::next(Moment m)
-{
-    if ( first_b_) {
-       bool gotcha = daddy_iter_l_->report_to_l_->
-           interpreter_l()->interpret_request_b(req_l_);
-       if (!gotcha)
-           req_l_->warning("Junking request: " + String(req_l_->name()));
-       first_b_ = false;
+Music_iterator*
+Music_iterator::static_get_iterator_p (Music *m,
+                                      Translator_group*report_l)
+{
+  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);
+  else if (m->is_type_b (Translation_property::static_name ()))
+    p = new Property_iterator((Translation_property *) m);
+  else if (m->is_type_b (Change_translator::static_name ()))
+    p = new Change_iterator((Change_translator*) m);
+  else if (m->is_type_b (Music_wrapper::static_name ()))
+    p = new Music_wrapper_iterator ((Music_wrapper *)m);
+          
+  
+  if (m -> translator_type_str_.length_i ())
+    {
+      Translator_group* a =report_l->
+       find_create_translator_l (m-> translator_type_str_, m->translator_id_str_);
+      p->set_translator (a);
     }
 
-    if ( m >= req_l_->duration() )
-       last_b_ = true;
+  if (! p->report_to_l())
+    p ->set_translator (report_l);
+  
+  return p;
 }
 
-Moment
-Request_iterator::next_moment()const
-{
-
-    Moment m(0);
-    if  (!first_b_) 
-       m = req_l_->duration();
-    return m;
-}
-
-bool
-Request_iterator::ok()const
+Music_iterator*
+Music_iterator::get_iterator_p (Music*m) const
 {
-    return (req_l_->duration() && !last_b_) || first_b_; // ugh
+  Music_iterator*p = static_get_iterator_p (m,report_to_l());
+  p->daddy_iter_l_ = (Music_iterator*)this;
+  p->construct_children();
+  return p;
 }
-IMPLEMENT_STATIC_NAME(Request_iterator);
-IMPLEMENT_IS_TYPE_B1(Request_iterator, Music_iterator);
-
-/* ****************** */
 
-Change_iterator::Change_iterator(Change_reg * ch)
+Music_iterator::Music_iterator()
 {
-    change_l_ = ch;
+  daddy_iter_l_ =0;
+  first_b_ = true;
 }
 
-IMPLEMENT_STATIC_NAME(Change_iterator);
-IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator);
 
-/*
-  TODO: pop/pushgroup
- */
-void
-Change_iterator::next(Moment mom)
-{
-#if 0
-    Register_group_register *group_l =
-       report_to_l_->find_get_reg_l(change_l_->type_str_, 
-                                        change_l_->id_str_);
-
-    report_to_l_->daddy_reg_l_->remove_register_p(report_to_l_);
-    group_l->add(report_to_l_);
-#endif
-    Music_iterator::next(mom);
-}
-
-
-
-/* ******************** */
-
-IMPLEMENT_STATIC_NAME(Voice_element_iterator);
-IMPLEMENT_IS_TYPE_B1(Voice_element_iterator,Chord_iterator);
-
-void
-Voice_element_iterator::construct_children()
-{
-    get_req_acceptor_l();
-/*
-    if ( daddy_iter_l_ 
-        && daddy_iter_l_->is_type_b(Voice_iterator::static_name() )) {
-       set_acceptor(daddy_iter_l_-> get_req_acceptor_l());
-    } else if (daddy_iter_l_
-              && daddy_iter_l_-> is_type_b( Chord_iterator::static_name() )) {
-
-       get_req_acceptor_l();
-    }
-    */
-    Chord_iterator::construct_children();
-}
-
-Voice_element_iterator::Voice_element_iterator(Voice_element*el_l)
-    : Chord_iterator(el_l)
-{
-    
-}