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