2 music-iterator.cc -- implement Music_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "music-iterator.hh"
17 #include "event-iterator.hh"
19 #include "international.hh"
20 #include "music-wrapper.hh"
21 #include "music-wrapper-iterator.hh"
22 #include "simple-music-iterator.hh"
24 #include "ly-smobs.icc"
26 Music_iterator::Music_iterator ()
32 Music_iterator::Music_iterator (Music_iterator const &)
37 Music_iterator::~Music_iterator ()
42 Music_iterator::get_outlet () const
44 return handle_.get_outlet ();
48 Music_iterator::set_context (Context *trans)
50 handle_.set_context (trans);
54 Music_iterator::construct_children ()
59 Music_iterator::pending_moment () const
65 Music_iterator::process (Moment)
70 Music_iterator::ok () const
76 Music_iterator::get_static_get_iterator (Music *m)
78 Music_iterator *p = 0;
80 SCM ctor = m->get_property ("iterator-ctor");
82 if (ly_is_procedure (ctor))
84 iter = scm_call_0 (ctor);
85 p = unsmob_iterator (iter);
89 if (dynamic_cast<Music_wrapper *> (m))
90 p = new Music_wrapper_iterator;
91 else if (m->is_mus_type ("event"))
92 p = new Event_iterator;
94 p = new Simple_music_iterator;
96 iter = p->self_scm ();
102 p->music_length_ = m->get_length ();
103 p->start_mom_ = m->start_mom ();
109 Music_iterator::music_get_length () const
111 return music_length_;
115 Music_iterator::music_start_mom ()const
121 Music_iterator::init_context (Music *m, Context *report)
126 set_context (report);
130 Music_iterator::substitute_outlet (Context *f, Context *t)
132 if (get_outlet () == f)
134 derived_substitute (f, t);
138 Music_iterator::derived_substitute (Context *, Context *)
143 Music_iterator::get_iterator (Music *m) const
145 SCM ip = get_static_get_iterator (m);
146 Music_iterator *p = unsmob_iterator (ip);
148 p->init_context (m, get_outlet ());
150 p->construct_children ();
154 /* Descend to a bottom context; implicitly create a new one if necessary */
156 Music_iterator::descend_to_bottom_context ()
158 assert (get_outlet ());
159 if (!get_outlet ()->is_bottom_context ())
160 set_context (get_outlet ()->get_default_interpreter ());
164 Music_iterator::report_event (Music *m)
166 descend_to_bottom_context ();
169 FIXME: then don't do it.
171 if (!m->is_mus_type ("event"))
172 m->origin ()->programming_error (_f ("Sending non-event to context"));
174 m->send_to_context (get_outlet ());
177 IMPLEMENT_CTOR_CALLBACK (Music_iterator);
180 Music_iterator::get_music () const
185 /****************************************************************/
187 IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
188 IMPLEMENT_SMOBS (Music_iterator);
189 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator);
192 Music_iterator::mark_smob (SCM smob)
194 Music_iterator *mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
196 mus->derived_mark ();
198 Careful with GC, although we intend the following as pointers
199 only, we _must_ mark them.
201 if (mus->get_outlet ())
202 scm_gc_mark (mus->get_outlet ()->self_scm ());
204 scm_gc_mark (mus->music_->self_scm ());
210 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state*)
214 Music_iterator *iter = unsmob_iterator (sm);
215 sprintf (s, "#<%s>", iter->class_name ());
221 Music_iterator::derived_mark ()const
226 Music_iterator::quit ()
229 handle_.set_context (0);
233 Music_iterator::do_quit ()
238 Music_iterator::run_always ()const
244 is_child_context (Context *me, Context *child)
246 while (child && child != me)
247 child = child->get_parent_context ();
253 move to context of child iterator if it is deeper down in the
257 Music_iterator::descend_to_child (Context *child_report)
259 Context *me_report = get_outlet ();
260 if (is_child_context (me_report, child_report))
261 set_context (child_report);