Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / lily / music-iterator.cc
blob057d4f01ebcf9618739b282b0d28e8b715b3aa9e
1 /*
2 music-iterator.cc -- implement Music_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
10 #include <cstdio>
11 using namespace std;
13 #include "warn.hh"
14 #include "music.hh"
15 #include "context.hh"
16 #include "event-iterator.hh"
17 #include "input.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 ()
27 music_ = 0;
28 smobify_self ();
31 Music_iterator::Music_iterator (Music_iterator const &)
33 assert (false);
36 Music_iterator::~Music_iterator ()
40 Context *
41 Music_iterator::get_outlet () const
43 return handle_.get_outlet ();
46 void
47 Music_iterator::set_context (Context *trans)
49 handle_.set_context (trans);
52 void
53 Music_iterator::construct_children ()
57 Moment
58 Music_iterator::pending_moment () const
60 return 0;
63 void
64 Music_iterator::process (Moment)
68 bool
69 Music_iterator::ok () const
71 return false;
74 SCM
75 Music_iterator::get_static_get_iterator (Music *m)
77 Music_iterator *p = 0;
79 SCM ctor = m->get_property ("iterator-ctor");
80 SCM iter = SCM_EOL;
81 if (ly_is_procedure (ctor))
83 iter = scm_call_0 (ctor);
84 p = unsmob_iterator (iter);
86 else
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;
92 else
93 p = new Simple_music_iterator;
95 iter = p->self_scm ();
96 p->unprotect ();
99 p->music_ = m;
100 assert (m);
101 p->music_length_ = m->get_length ();
102 p->start_mom_ = m->start_mom ();
104 return iter;
107 Moment
108 Music_iterator::music_get_length () const
110 return music_length_;
113 Moment
114 Music_iterator::music_start_mom ()const
116 return start_mom_;
119 void
120 Music_iterator::init_context (Music *m, Context *report)
122 music_ = m;
123 assert (m);
124 if (! get_outlet ())
125 set_context (report);
128 void
129 Music_iterator::substitute_outlet (Context *f, Context *t)
131 if (get_outlet () == f)
132 set_context (t);
133 derived_substitute (f, t);
136 void
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 ();
150 return ip;
153 /* Descend to a bottom context; implicitly create a new one if necessary */
154 void
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 ());
162 void
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);
178 Music *
179 Music_iterator::get_music () const
181 return music_;
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 ());
202 if (mus->music_)
203 scm_gc_mark (mus->music_->self_scm ());
205 return SCM_EOL;
209 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state*)
211 char s[1000];
213 Music_iterator *iter = unsmob_iterator (sm);
214 sprintf (s, "#<%s>", iter->class_name ());
215 scm_puts (s, port);
216 return 1;
219 void
220 Music_iterator::derived_mark ()const
224 void
225 Music_iterator::quit ()
227 do_quit ();
228 handle_.set_context (0);
231 void
232 Music_iterator::do_quit ()
236 bool
237 Music_iterator::run_always ()const
239 return false;
242 bool
243 is_child_context (Context *me, Context *child)
245 while (child && child != me)
246 child = child->get_parent_context ();
248 return child == me;
252 move to context of child iterator if it is deeper down in the
253 hierarchy.
255 void
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);