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