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_translator (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_translator (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 ();
168 if (!m->is_mus_type ("event"))
169 m->origin ()->warning (_f ("Sending non-event to context"));
171 m->send_to_context (get_outlet ());
174 IMPLEMENT_CTOR_CALLBACK (Music_iterator);
177 Music_iterator::get_music () const
182 /****************************************************************/
184 IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
185 IMPLEMENT_SMOBS (Music_iterator);
186 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator);
189 Music_iterator::mark_smob (SCM smob)
191 Music_iterator *mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
193 mus->derived_mark ();
195 Careful with GC, although we intend the following as pointers
196 only, we _must_ mark them.
198 if (mus->get_outlet ())
199 scm_gc_mark (mus->get_outlet ()->self_scm ());
201 scm_gc_mark (mus->music_->self_scm ());
207 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state*)
211 Music_iterator *iter = unsmob_iterator (sm);
212 sprintf (s, "#<%s>", iter->class_name ());
218 Music_iterator::derived_mark ()const
223 Music_iterator::quit ()
226 handle_.set_context (0);
230 Music_iterator::do_quit ()
235 Music_iterator::run_always ()const
241 is_child_context (Context *me, Context *child)
243 while (child && child != me)
244 child = child->get_parent_context ();
250 move to context of child iterator if it is deeper down in the
254 Music_iterator::descend_to_child (Context *child_report)
256 Context *me_report = get_outlet ();
257 if (is_child_context (me_report, child_report))
258 set_context (child_report);