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