2 music-iterator.cc -- implement Music_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
16 #include "event-iterator.hh"
18 #include "international.hh"
19 #include "music-wrapper.hh"
20 #include "music-wrapper-iterator.hh"
21 #include "simple-music-iterator.hh"
23 #include "ly-smobs.icc"
25 Music_iterator::Music_iterator ()
31 Music_iterator::Music_iterator (Music_iterator
const &)
36 Music_iterator::~Music_iterator ()
41 Music_iterator::get_outlet () const
43 return handle_
.get_outlet ();
47 Music_iterator::set_context (Context
*trans
)
49 handle_
.set_context (trans
);
53 Music_iterator::construct_children ()
58 Music_iterator::pending_moment () const
64 Music_iterator::process (Moment
)
69 Music_iterator::ok () const
75 Music_iterator::get_static_get_iterator (Music
*m
)
77 Music_iterator
*p
= 0;
79 SCM ctor
= m
->get_property ("iterator-ctor");
81 if (ly_is_procedure (ctor
))
83 iter
= scm_call_0 (ctor
);
84 p
= unsmob_iterator (iter
);
88 if (dynamic_cast<Music_wrapper
*> (m
))
89 p
= new Music_wrapper_iterator
;
90 else if (m
->is_mus_type ("event"))
91 p
= new Event_iterator
;
93 p
= new Simple_music_iterator
;
95 iter
= p
->self_scm ();
101 p
->music_length_
= m
->get_length ();
102 p
->start_mom_
= m
->start_mom ();
108 Music_iterator::music_get_length () const
110 return music_length_
;
114 Music_iterator::music_start_mom ()const
120 Music_iterator::init_context (Music
*m
, Context
*report
)
125 set_context (report
);
129 Music_iterator::substitute_outlet (Context
*f
, Context
*t
)
131 if (get_outlet () == f
)
133 derived_substitute (f
, t
);
137 Music_iterator::derived_substitute (Context
*, Context
*)
142 Music_iterator::get_iterator (Music
*m
) const
144 SCM ip
= get_static_get_iterator (m
);
145 Music_iterator
*p
= unsmob_iterator (ip
);
147 p
->init_context (m
, get_outlet ());
149 p
->construct_children ();
153 /* Descend to a bottom context; implicitly create a new one if necessary */
155 Music_iterator::descend_to_bottom_context ()
157 assert (get_outlet ());
158 if (!get_outlet ()->is_bottom_context ())
159 set_context (get_outlet ()->get_default_interpreter ());
163 Music_iterator::report_event (Music
*m
)
165 descend_to_bottom_context ();
168 FIXME: then don't do it.
170 if (!m
->is_mus_type ("event"))
171 m
->origin ()->programming_error (_ ("Sending non-event to context"));
173 m
->send_to_context (get_outlet ());
176 IMPLEMENT_CTOR_CALLBACK (Music_iterator
);
179 Music_iterator::get_music () const
184 /****************************************************************/
186 IMPLEMENT_TYPE_P (Music_iterator
, "ly:iterator?");
187 IMPLEMENT_SMOBS (Music_iterator
);
188 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator
);
191 Music_iterator::mark_smob (SCM smob
)
193 Music_iterator
*mus
= (Music_iterator
*)SCM_CELL_WORD_1 (smob
);
195 mus
->derived_mark ();
197 Careful with GC, although we intend the following as pointers
198 only, we _must_ mark them.
200 if (mus
->get_outlet ())
201 scm_gc_mark (mus
->get_outlet ()->self_scm ());
203 scm_gc_mark (mus
->music_
->self_scm ());
209 Music_iterator::print_smob (SCM sm
, SCM port
, scm_print_state
*)
213 Music_iterator
*iter
= unsmob_iterator (sm
);
214 sprintf (s
, "#<%s>", iter
->class_name ());
220 Music_iterator::derived_mark ()const
225 Music_iterator::quit ()
228 handle_
.set_context (0);
232 Music_iterator::do_quit ()
237 Music_iterator::run_always ()const
243 is_child_context (Context
*me
, Context
*child
)
245 while (child
&& child
!= me
)
246 child
= child
->get_parent_context ();
252 move to context of child iterator if it is deeper down in the
256 Music_iterator::descend_to_child (Context
*child_report
)
258 Context
*me_report
= get_outlet ();
259 if (is_child_context (me_report
, child_report
))
260 set_context (child_report
);