2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 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/>.
27 #include "event-iterator.hh"
29 #include "international.hh"
30 #include "music-wrapper.hh"
31 #include "music-wrapper-iterator.hh"
32 #include "simple-music-iterator.hh"
34 #include "ly-smobs.icc"
36 Music_iterator::Music_iterator ()
42 Music_iterator::Music_iterator (Music_iterator
const &)
47 Music_iterator::~Music_iterator ()
52 Music_iterator::get_outlet () const
54 return handle_
.get_outlet ();
58 Music_iterator::set_context (Context
*trans
)
60 handle_
.set_context (trans
);
64 Music_iterator::construct_children ()
69 Music_iterator::pending_moment () const
75 Music_iterator::process (Moment
)
80 Music_iterator::ok () const
86 Music_iterator::get_static_get_iterator (Music
*m
)
88 Music_iterator
*p
= 0;
90 SCM ctor
= m
->get_property ("iterator-ctor");
92 if (ly_is_procedure (ctor
))
94 iter
= scm_call_0 (ctor
);
95 p
= unsmob_iterator (iter
);
99 if (dynamic_cast<Music_wrapper
*> (m
))
100 p
= new Music_wrapper_iterator
;
101 else if (m
->is_mus_type ("event"))
102 p
= new Event_iterator
;
104 p
= new Simple_music_iterator
;
106 iter
= p
->self_scm ();
112 p
->music_length_
= m
->get_length ();
113 p
->start_mom_
= m
->start_mom ();
119 Music_iterator::music_get_length () const
121 return music_length_
;
125 Music_iterator::music_start_mom ()const
131 Music_iterator::init_context (Music
*m
, Context
*report
)
136 set_context (report
);
140 Music_iterator::substitute_outlet (Context
*f
, Context
*t
)
142 if (get_outlet () == f
)
144 derived_substitute (f
, t
);
148 Music_iterator::derived_substitute (Context
*, Context
*)
153 Music_iterator::get_iterator (Music
*m
) const
155 SCM ip
= get_static_get_iterator (m
);
156 Music_iterator
*p
= unsmob_iterator (ip
);
158 p
->init_context (m
, get_outlet ());
160 p
->construct_children ();
164 /* Descend to a bottom context; implicitly create a new one if necessary */
166 Music_iterator::descend_to_bottom_context ()
168 assert (get_outlet ());
169 if (!get_outlet ()->is_bottom_context ())
170 set_context (get_outlet ()->get_default_interpreter ());
174 Music_iterator::report_event (Music
*m
)
176 descend_to_bottom_context ();
179 FIXME: then don't do it.
181 if (!m
->is_mus_type ("event"))
182 m
->origin ()->programming_error (_ ("Sending non-event to context"));
184 m
->send_to_context (get_outlet ());
187 IMPLEMENT_CTOR_CALLBACK (Music_iterator
);
190 Music_iterator::get_music () const
195 /****************************************************************/
197 IMPLEMENT_TYPE_P (Music_iterator
, "ly:iterator?");
198 IMPLEMENT_SMOBS (Music_iterator
);
199 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator
);
202 Music_iterator::mark_smob (SCM smob
)
204 Music_iterator
*mus
= (Music_iterator
*)SCM_CELL_WORD_1 (smob
);
206 mus
->derived_mark ();
208 Careful with GC, although we intend the following as pointers
209 only, we _must_ mark them.
211 if (mus
->get_outlet ())
212 scm_gc_mark (mus
->get_outlet ()->self_scm ());
214 scm_gc_mark (mus
->music_
->self_scm ());
220 Music_iterator::print_smob (SCM sm
, SCM port
, scm_print_state
*)
224 Music_iterator
*iter
= unsmob_iterator (sm
);
225 sprintf (s
, "#<%s>", iter
->class_name ());
231 Music_iterator::derived_mark ()const
236 Music_iterator::quit ()
239 handle_
.set_context (0);
243 Music_iterator::do_quit ()
248 Music_iterator::run_always ()const
254 is_child_context (Context
*me
, Context
*child
)
256 while (child
&& child
!= me
)
257 child
= child
->get_parent_context ();
263 move to context of child iterator if it is deeper down in the
267 Music_iterator::descend_to_child (Context
*child_report
)
269 Context
*me_report
= get_outlet ();
270 if (is_child_context (me_report
, child_report
))
271 set_context (child_report
);