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