]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-iterator.cc
c01a5d76e2d9328f73018907a9aee967ea84d491
[lilypond.git] / lily / music-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20
21 #include <cstdio>
22 using namespace std;
23
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"
33
34 #include "ly-smobs.icc"
35
36 Music_iterator::Music_iterator ()
37 {
38   music_ = 0;
39   smobify_self ();
40 }
41
42 Music_iterator::Music_iterator (Music_iterator const &)
43 {
44   assert (false);
45 }
46
47 Music_iterator::~Music_iterator ()
48 {
49 }
50
51 Context *
52 Music_iterator::get_outlet () const
53 {
54   return handle_.get_outlet ();
55 }
56
57 void
58 Music_iterator::set_context (Context *trans)
59 {
60   handle_.set_context (trans);
61 }
62
63 void
64 Music_iterator::construct_children ()
65 {
66 }
67
68 Moment
69 Music_iterator::pending_moment () const
70 {
71   return 0;
72 }
73
74 void
75 Music_iterator::process (Moment)
76 {
77 }
78
79 bool
80 Music_iterator::ok () const
81 {
82   return false;
83 }
84
85 SCM
86 Music_iterator::get_static_get_iterator (Music *m)
87 {
88   Music_iterator *p = 0;
89
90   SCM ctor = m->get_property ("iterator-ctor");
91   SCM iter = SCM_EOL;
92   if (ly_is_procedure (ctor))
93     {
94       iter = scm_call_0 (ctor);
95       p = unsmob_iterator (iter);
96     }
97   else
98     {
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;
105
106       iter = p->self_scm ();
107       p->unprotect ();
108     }
109
110   p->music_ = m;
111   assert (m);
112   p->music_length_ = m->get_length ();
113   p->start_mom_ = m->start_mom ();
114
115   return iter;
116 }
117
118 Moment
119 Music_iterator::music_get_length () const
120 {
121   return music_length_;
122 }
123
124 Moment
125 Music_iterator::music_start_mom ()const
126 {
127   return start_mom_;
128 }
129
130 void
131 Music_iterator::init_context (Music *m, Context *report)
132 {
133   music_ = m;
134   assert (m);
135   if (! get_outlet ())
136     set_context (report);
137 }
138
139 void
140 Music_iterator::substitute_outlet (Context *f, Context *t)
141 {
142   if (get_outlet () == f)
143     set_context (t);
144   derived_substitute (f, t);
145 }
146
147 void
148 Music_iterator::derived_substitute (Context *, Context *)
149 {
150 }
151
152 SCM
153 Music_iterator::get_iterator (Music *m) const
154 {
155   SCM ip = get_static_get_iterator (m);
156   Music_iterator *p = unsmob_iterator (ip);
157
158   p->init_context (m, get_outlet ());
159
160   p->construct_children ();
161   return ip;
162 }
163
164 /* Descend to a bottom context; implicitly create a new one if necessary */
165 void
166 Music_iterator::descend_to_bottom_context ()
167 {
168   assert (get_outlet ());
169   if (!get_outlet ()->is_bottom_context ())
170     set_context (get_outlet ()->get_default_interpreter ());
171 }
172
173 void 
174 Music_iterator::report_event (Music *m)
175 {
176   descend_to_bottom_context ();
177
178   /*
179     FIXME: then don't do it. 
180   */
181   if (!m->is_mus_type ("event"))
182     m->origin ()->programming_error (_ ("Sending non-event to context"));
183
184   m->send_to_context (get_outlet ());
185 }
186
187 IMPLEMENT_CTOR_CALLBACK (Music_iterator);
188
189 Music *
190 Music_iterator::get_music () const
191 {
192   return music_;
193 }
194
195 /****************************************************************/
196
197 IMPLEMENT_TYPE_P (Music_iterator, "ly:iterator?");
198 IMPLEMENT_SMOBS (Music_iterator);
199 IMPLEMENT_DEFAULT_EQUAL_P (Music_iterator);
200
201 SCM
202 Music_iterator::mark_smob (SCM smob)
203 {
204   Music_iterator *mus = (Music_iterator *)SCM_CELL_WORD_1 (smob);
205
206   mus->derived_mark ();
207   /*
208     Careful with GC, although we intend the following as pointers
209     only, we _must_ mark them.
210   */
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 ());
215
216   return SCM_EOL;
217 }
218
219 int
220 Music_iterator::print_smob (SCM sm, SCM port, scm_print_state*)
221 {
222   char s[1000];
223
224   Music_iterator *iter = unsmob_iterator (sm);
225   sprintf (s, "#<%s>", iter->class_name ());
226   scm_puts (s, port);
227   return 1;
228 }
229
230 void
231 Music_iterator::derived_mark ()const
232 {
233 }
234
235 void
236 Music_iterator::quit ()
237 {
238   do_quit ();
239   handle_.set_context (0);
240 }
241
242 void
243 Music_iterator::do_quit ()
244 {
245 }
246
247 bool
248 Music_iterator::run_always ()const
249 {
250   return false;
251 }
252
253 bool
254 is_child_context (Context *me, Context *child)
255 {
256   while (child && child != me)
257     child = child->get_parent_context ();
258
259   return child == me;
260 }
261
262 /*
263   move to context of child iterator if it is deeper down in the
264   hierarchy.
265 */
266 void
267 Music_iterator::descend_to_child (Context *child_report)
268 {
269   Context *me_report = get_outlet ();
270   if (is_child_context (me_report, child_report))
271     set_context (child_report);
272 }