2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
26 #include "event-iterator.hh"
28 #include "international.hh"
29 #include "music-wrapper.hh"
30 #include "music-wrapper-iterator.hh"
31 #include "simple-music-iterator.hh"
33 #include "ly-smobs.icc"
35 Music_iterator::Music_iterator ()
41 Music_iterator::Music_iterator (Music_iterator const &)
46 Music_iterator::~Music_iterator ()
51 Music_iterator::get_outlet () const
53 return handle_.get_context ();
57 Music_iterator::set_context (Context *trans)
59 handle_.set_context (trans);
63 Music_iterator::construct_children ()
68 Music_iterator::pending_moment () const
74 Music_iterator::process (Moment)
79 Music_iterator::ok () const
85 Music_iterator::get_static_get_iterator (Music *m)
87 Music_iterator *p = 0;
89 SCM ctor = m->get_property ("iterator-ctor");
91 if (ly_is_procedure (ctor))
93 iter = scm_call_0 (ctor);
94 p = unsmob_iterator (iter);
98 if (dynamic_cast<Music_wrapper *> (m))
99 p = new Music_wrapper_iterator;
100 else if (m->is_mus_type ("event"))
101 p = new Event_iterator;
103 p = new Simple_music_iterator;
105 iter = p->self_scm ();
111 p->music_length_ = m->get_length ();
112 p->start_mom_ = m->start_mom ();
118 Music_iterator::music_get_length () const
120 return music_length_;
124 Music_iterator::music_start_mom ()const
130 Music_iterator::init_context (Music *m, Context *report)
135 set_context (report);
139 Music_iterator::substitute_outlet (Context *f, Context *t)
141 if (get_outlet () == f)
143 derived_substitute (f, t);
147 Music_iterator::derived_substitute (Context *, Context *)
152 Music_iterator::get_iterator (Music *m) const
154 SCM ip = get_static_get_iterator (m);
155 Music_iterator *p = unsmob_iterator (ip);
157 p->init_context (m, get_outlet ());
159 p->construct_children ();
163 /* Descend to a bottom context; implicitly create a new one if necessary */
165 Music_iterator::descend_to_bottom_context ()
167 assert (get_outlet ());
168 if (!get_outlet ()->is_bottom_context ())
169 set_context (get_outlet ()->get_default_interpreter ());
173 Music_iterator::report_event (Music *m)
175 descend_to_bottom_context ();
178 FIXME: then don't do it.
180 if (!m->is_mus_type ("event"))
181 m->origin ()->programming_error ("Sending non-event to context");
183 m->send_to_context (get_outlet ());
186 IMPLEMENT_CTOR_CALLBACK (Music_iterator);
189 Music_iterator::get_music () const
194 /****************************************************************/
196 IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
197 IMPLEMENT_SMOBS (Music_iterator);
198 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator);
201 Music_iterator::mark_smob (SCM smob)
203 Music_iterator *mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
205 mus->derived_mark ();
207 Careful with GC, although we intend the following as pointers
208 only, we _must_ mark them.
210 if (mus->get_outlet ())
211 scm_gc_mark (mus->get_outlet ()->self_scm ());
213 scm_gc_mark (mus->music_->self_scm ());
219 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state *)
223 Music_iterator *iter = unsmob_iterator (sm);
224 sprintf (s, "#<%s>", iter->class_name ());
230 Music_iterator::derived_mark ()const
235 Music_iterator::quit ()
238 handle_.set_context (0);
242 Music_iterator::do_quit ()
247 Music_iterator::run_always ()const
253 is_child_context (Context *me, Context *child)
255 while (child && child != me)
256 child = child->get_parent_context ();
262 move to context of child iterator if it is deeper down in the
266 Music_iterator::descend_to_child (Context *child_report)
268 Context *me_report = get_outlet ();
269 if (is_child_context (me_report, child_report))
270 set_context (child_report);