2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 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"
34 Music_iterator::Music_iterator ()
40 Music_iterator::Music_iterator (Music_iterator const &)
45 Music_iterator::~Music_iterator ()
50 Music_iterator::get_outlet () const
52 return handle_.get_context ();
56 Music_iterator::set_context (Context *trans)
58 handle_.set_context (trans);
62 Music_iterator::construct_children ()
67 Music_iterator::pending_moment () const
73 Music_iterator::process (Moment)
78 Music_iterator::ok () const
84 Music_iterator::get_static_get_iterator (Music *m)
86 Music_iterator *p = 0;
88 SCM ctor = m->get_property ("iterator-ctor");
90 if (ly_is_procedure (ctor))
92 iter = scm_call_0 (ctor);
93 p = Music_iterator::unsmob (iter);
97 if (dynamic_cast<Music_wrapper *> (m))
98 p = new Music_wrapper_iterator;
99 else if (m->is_mus_type ("event"))
100 p = new Event_iterator;
102 p = new Simple_music_iterator;
104 iter = p->self_scm ();
110 p->music_length_ = m->get_length ();
111 p->start_mom_ = m->start_mom ();
117 Music_iterator::music_get_length () const
119 return music_length_;
123 Music_iterator::music_start_mom ()const
129 Music_iterator::init_context (Music *m, Context *report)
134 set_context (report);
138 Music_iterator::substitute_outlet (Context *f, Context *t)
140 if (get_outlet () == f)
142 derived_substitute (f, t);
146 Music_iterator::derived_substitute (Context *, Context *)
151 Music_iterator::get_iterator (Music *m) const
153 SCM ip = get_static_get_iterator (m);
154 Music_iterator *p = Music_iterator::unsmob (ip);
156 p->init_context (m, get_outlet ());
158 p->construct_children ();
162 /* Descend to a bottom context; implicitly create a new one if necessary */
164 Music_iterator::descend_to_bottom_context ()
166 assert (get_outlet ());
167 if (!get_outlet ()->is_bottom_context ())
168 set_context (get_outlet ()->get_default_interpreter ());
172 Music_iterator::report_event (Music *m)
174 descend_to_bottom_context ();
177 FIXME: then don't do it.
179 if (!m->is_mus_type ("event"))
180 m->origin ()->programming_error ("Sending non-event to context");
182 m->send_to_context (get_outlet ());
185 IMPLEMENT_CTOR_CALLBACK (Music_iterator);
188 Music_iterator::get_music () const
193 /****************************************************************/
195 const char Music_iterator::type_p_name_[] = "ly:iterator?";
198 Music_iterator::mark_smob ()
202 Careful with GC, although we intend the following as pointers
203 only, we _must_ mark them.
205 /* Use handle_ directly as get_outlet is a virtual function and we
206 need to protect the context until Music_iterator::quit is being
208 if (handle_.get_context ())
209 scm_gc_mark (handle_.get_context ()->self_scm ());
211 scm_gc_mark (music_->self_scm ());
217 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state *)
221 Music_iterator *iter = Music_iterator::unsmob (sm);
222 sprintf (s, "#<%s>", iter->class_name ());
228 Music_iterator::derived_mark ()const
233 Music_iterator::quit ()
236 handle_.set_context (0);
240 Music_iterator::do_quit ()
245 Music_iterator::run_always ()const
251 is_child_context (Context *me, Context *child)
253 while (child && child != me)
254 child = child->get_parent_context ();
260 move to context of child iterator if it is deeper down in the
264 Music_iterator::descend_to_child (Context *child_report)
266 Context *me_report = get_outlet ();
267 if (is_child_context (me_report, child_report))
268 set_context (child_report);