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