/*
- 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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
-#include "music-list.hh"
+/*
+ UGH. too many includes.
+ */
+#include <stdio.h>
+
+#include "warn.hh"
#include "music-iterator.hh"
-#include "translator.hh"
-#include "request.hh"
-#include "debug.hh"
+#include "translator-group.hh"
+#include "music-wrapper.hh"
+#include "music-wrapper-iterator.hh"
+#include "simple-music-iterator.hh"
-IMPLEMENT_STATIC_NAME(Music_iterator);
-IMPLEMENT_IS_TYPE_B(Music_iterator);
+#include "ly-smobs.icc"
-void
-Music_iterator::do_print()const
+Music_iterator::Music_iterator ()
{
-
+ music_ = 0;
+ smobify_self ();
}
-void
-Music_iterator::print() const
+Music_iterator::Music_iterator (Music_iterator const& src)
{
-#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
+ handle_ = *src.handle_.clone ();
+ music_ = src.music_;
+ music_length_ = src.music_length_;
+ start_mom_ = src.start_mom_;
+ 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::set_translator(Translator*trans)
-{
- if (report_to_l_==trans)
- return;
-
- if (report_to_l_)
- report_to_l_->iterator_count_ --;
-
- report_to_l_ = trans;
-
- if (report_to_l_)
- report_to_l_->iterator_count_ ++;
+Translator_group*
+Music_iterator::report_to () const
+{
+ return handle_.report_to ();
}
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( 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_ != "") {
- Translator * a =report_l->
- find_get_translator_l(ml-> type_str_, ml->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;
- report_to_l_ = 0;
- first_b_ = true;
+ return SCM_EOL;
}
-/* ************** */
-
-Chord_iterator::Chord_iterator(Chord const *chord_C)
+SCM
+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;
- }
-}
-void
-Chord_iterator::do_print() const
-{
-#ifndef NPRINT
- for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++) {
- i->print();
+ SCM ctor = m->get_mus_property ("iterator-ctor") ;
+ SCM iter = SCM_EOL;
+ if (gh_procedure_p (ctor))
+ {
+ iter = gh_call0 (ctor);
+ p = unsmob_iterator (iter);
}
-#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++;
+ else
+ {
+ if (dynamic_cast<Music_wrapper *> (m))
+ p = new Music_wrapper_iterator;
+ else
+ p = new Simple_music_iterator;
+
+ iter = p->self_scm();
+ scm_gc_unprotect_object (iter);
}
- Music_iterator::process_and_next(until);
-// assert(!ok() || next_moment() > until);
+ p->music_ = m;
+ assert (m);
+ p->music_length_ = m->get_length ();
+ p->start_mom_ = m->start_mom ();
+ return iter;
}
-IMPLEMENT_STATIC_NAME(Chord_iterator);
-IMPLEMENT_IS_TYPE_B1(Chord_iterator,Music_iterator);
Moment
-Chord_iterator::next_moment()const
+Music_iterator::music_get_length () const
{
- Moment next_ = INFTY;
- for (PCursor<Music_iterator*> i(children_p_list_.top()); i.ok(); i++)
- next_ = next_ <? i->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
+Music_iterator::init_translator (Music *m, Translator_group *report)
{
- if (iter_p_)
- iter_p_->print();
+ music_ = m;
+ assert (m);
+ if (! report_to ())
+ set_translator (report);
}
-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()
-{
- 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_);
- }
-}
-void
-Voice_iterator::next_element()
+SCM
+Music_iterator::get_iterator (Music *m) const
{
- delete iter_p_ ;
- iter_p_ =0;
- here_mom_ += ptr()->time_int().length();
- PCursor<Music*>::next();
- construct_children();
+ SCM ip = get_static_get_iterator (m);
+ Music_iterator*p = unsmob_iterator (ip);
+
+ p->init_translator (m, report_to ());
+
+ p->construct_children ();
+ return ip;
}
-Voice_iterator::~Voice_iterator()
+/*
+ TODO: rename to prevent confusion between Translator::try_music and
+ Iterator::try_music
+
+ */
+Music_iterator*
+Music_iterator::try_music (Music *m) const
{
- delete iter_p_;
+ 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;
}
-IMPLEMENT_STATIC_NAME(Voice_iterator);
-IMPLEMENT_IS_TYPE_B1(Voice_iterator,Music_iterator);
-
-void
-Voice_iterator::process_and_next(Moment until)
+Music_iterator*
+Music_iterator::try_music_in_children (Music *) const
{
- 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);
+ return 0;
}
-Moment
-Voice_iterator::next_moment()const
-{
- return iter_p_->next_moment() + here_mom_;
-}
+IMPLEMENT_CTOR_CALLBACK (Music_iterator);
-bool
-Voice_iterator::ok()const
+Music *
+Music_iterator::get_music () const
{
- return PCursor<Music*>::ok();
+ return music_;
}
-/* ***************** */
+/****************************************************************/
-void
-Request_iterator::do_print()const
-{
- mtor << req_l_->name() ;
-}
+IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
+IMPLEMENT_SMOBS(Music_iterator);
+IMPLEMENT_DEFAULT_EQUAL_P(Music_iterator);
-Request_iterator::Request_iterator(Request const*c)
+SCM
+Music_iterator::mark_smob (SCM smob)
{
- req_l_ = (Request*)c;
- last_b_ = false;
-}
+ Music_iterator * mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
-void
-Request_iterator::process_and_next(Moment m)
-{
- if ( first_b_ ) {
- bool gotcha = daddy_iter_l_->report_to_l_->try_request(req_l_);
- if (!gotcha)
- req_l_->warning("Junking request: " + String(req_l_->name()));
- first_b_ = false;
- }
+ mus->derived_mark ();
+ /*
+ Careful with GC, although we intend the following as pointers
+ only, we _must_ mark them.
+ */
+ if (mus->report_to())
+ scm_gc_mark (mus->report_to()->self_scm());
+ if (mus->music_)
+ scm_gc_mark (mus->music_->self_scm());
+
- if ( m >= req_l_->duration() )
- last_b_ = true;
+ return SCM_EOL;
}
-Moment
-Request_iterator::next_moment()const
+int
+Music_iterator::print_smob (SCM sm , SCM port, scm_print_state*)
{
+ char s[1000];
- Moment m(0);
- if (!first_b_)
- m = req_l_->duration();
- return m;
-}
-
-bool
-Request_iterator::ok()const
-{
- return (req_l_->duration() && !last_b_) || first_b_; // ugh
+ Music_iterator *iter = unsmob_iterator (sm);
+ sprintf (s, "#<%s>", classname (iter));
+ scm_puts (s, port);
+ return 1;
}
-IMPLEMENT_STATIC_NAME(Request_iterator);
-IMPLEMENT_IS_TYPE_B1(Request_iterator, Music_iterator);
-
-/* ****************** */
-
-Change_iterator::Change_iterator(Change_reg * ch)
-{
- change_l_ = ch;
-}
-
-IMPLEMENT_STATIC_NAME(Change_iterator);
-IMPLEMENT_IS_TYPE_B1(Change_iterator,Music_iterator);
-
-/*
- TODO: pop/pushgroup
- */
void
-Change_iterator::process_and_next(Moment mom)
+Music_iterator::derived_mark()const
{
-#if 0
- Engraver_group_engraver *group_l =
- report_to_l_->find_get_grav_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_STATIC_NAME(Voice_element_iterator);
-IMPLEMENT_IS_TYPE_B1(Voice_element_iterator,Chord_iterator);
-
void
-Voice_element_iterator::construct_children()
+Music_iterator::quit ()
{
- get_req_translator_l();
- Chord_iterator::construct_children();
+ do_quit ();
+ handle_.quit ();
}
-Voice_element_iterator::Voice_element_iterator(Voice_element*el_l)
- : Chord_iterator(el_l)
+void
+Music_iterator::do_quit()
{
-
}