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