Improve handling of nested overrides using list syntax.
[lilypond/mpolesky.git] / lily / music-iterator.cc
blobecb1172c8acbc8b80238fd60c154fa690ba067f0
1 /*
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/>.
21 #include <cstdio>
22 using namespace std;
24 #include "warn.hh"
25 #include "music.hh"
26 #include "context.hh"
27 #include "event-iterator.hh"
28 #include "input.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 ()
38 music_ = 0;
39 smobify_self ();
42 Music_iterator::Music_iterator (Music_iterator const &)
44 assert (false);
47 Music_iterator::~Music_iterator ()
51 Context *
52 Music_iterator::get_outlet () const
54 return handle_.get_outlet ();
57 void
58 Music_iterator::set_context (Context *trans)
60 handle_.set_context (trans);
63 void
64 Music_iterator::construct_children ()
68 Moment
69 Music_iterator::pending_moment () const
71 return 0;
74 void
75 Music_iterator::process (Moment)
79 bool
80 Music_iterator::ok () const
82 return false;
85 SCM
86 Music_iterator::get_static_get_iterator (Music *m)
88 Music_iterator *p = 0;
90 SCM ctor = m->get_property ("iterator-ctor");
91 SCM iter = SCM_EOL;
92 if (ly_is_procedure (ctor))
94 iter = scm_call_0 (ctor);
95 p = unsmob_iterator (iter);
97 else
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;
103 else
104 p = new Simple_music_iterator;
106 iter = p->self_scm ();
107 p->unprotect ();
110 p->music_ = m;
111 assert (m);
112 p->music_length_ = m->get_length ();
113 p->start_mom_ = m->start_mom ();
115 return iter;
118 Moment
119 Music_iterator::music_get_length () const
121 return music_length_;
124 Moment
125 Music_iterator::music_start_mom ()const
127 return start_mom_;
130 void
131 Music_iterator::init_context (Music *m, Context *report)
133 music_ = m;
134 assert (m);
135 if (! get_outlet ())
136 set_context (report);
139 void
140 Music_iterator::substitute_outlet (Context *f, Context *t)
142 if (get_outlet () == f)
143 set_context (t);
144 derived_substitute (f, t);
147 void
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 ();
161 return ip;
164 /* Descend to a bottom context; implicitly create a new one if necessary */
165 void
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 ());
173 void
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);
189 Music *
190 Music_iterator::get_music () const
192 return music_;
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 ());
213 if (mus->music_)
214 scm_gc_mark (mus->music_->self_scm ());
216 return SCM_EOL;
220 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state*)
222 char s[1000];
224 Music_iterator *iter = unsmob_iterator (sm);
225 sprintf (s, "#<%s>", iter->class_name ());
226 scm_puts (s, port);
227 return 1;
230 void
231 Music_iterator::derived_mark ()const
235 void
236 Music_iterator::quit ()
238 do_quit ();
239 handle_.set_context (0);
242 void
243 Music_iterator::do_quit ()
247 bool
248 Music_iterator::run_always ()const
250 return false;
253 bool
254 is_child_context (Context *me, Context *child)
256 while (child && child != me)
257 child = child->get_parent_context ();
259 return child == me;
263 move to context of child iterator if it is deeper down in the
264 hierarchy.
266 void
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);