lilypond-1.4.4
[lilypond.git] / lily / simultaneous-music-iterator.cc
blob3265bee03d2c01f7f229cdcb1c0ce61ca99dec2d
1 /*
2 Simultaneous_music-iterator.cc -- implement Simultaneous_music_iterator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "translator-group.hh"
10 #include "debug.hh"
11 #include "simultaneous-music-iterator.hh"
12 #include "music-list.hh"
13 #include "killing-cons.tcc"
15 Simultaneous_music_iterator::Simultaneous_music_iterator ()
17 separate_contexts_b_ = false;
20 Simultaneous_music_iterator::Simultaneous_music_iterator (Simultaneous_music_iterator const& src)
21 : Music_iterator (src)
23 separate_contexts_b_ = src.separate_contexts_b_;
24 for (Cons<Music_iterator> *p = src.children_p_list_.head_; p; p = p->next_)
26 Music_iterator *i = p->car_;
27 children_p_list_.append (new Killing_cons<Music_iterator> (i->clone (), 0));
31 Simultaneous_music_iterator::~Simultaneous_music_iterator ()
33 children_p_list_.junk ();
36 SCM
37 Simultaneous_music_iterator::get_music (Moment m)const
39 SCM s = SCM_EOL;
40 for (Cons<Music_iterator> *p = children_p_list_.head_; p; p = p->next_)
42 s = gh_append2 (p->car_->get_music (m), s);
44 return s;
47 void
48 Simultaneous_music_iterator::construct_children ()
50 int j = 0;
51 Music_sequence const *sim = dynamic_cast<Music_sequence const*> (music_l_);
53 SCM i = sim->music_list ();
54 for (; gh_pair_p (i); i = gh_cdr (i), j++)
56 Music *mus = unsmob_music (gh_car (i));
57 Music_iterator * mi = static_get_iterator_p (mus);
59 /* if separate_contexts_b_ is set, create a new context with the
60 number number as name */
62 Translator_group * t = (j && separate_contexts_b_)
63 ? report_to_l ()->find_create_translator_l (report_to_l ()->type_str_,
64 to_str (j))
65 : report_to_l ();
67 if (!t)
68 t = report_to_l ();
70 mi->init_translator (mus, t);
71 mi->construct_children ();
73 if (mi->ok ())
75 children_p_list_.append (new Killing_cons<Music_iterator> (mi,0));
77 else
78 delete mi;
83 void
84 Simultaneous_music_iterator::process (Moment until)
86 for (Cons<Music_iterator> **pp = &children_p_list_.head_; *pp;)
88 Music_iterator * i = (*pp)->car_;
89 if (i->pending_moment () == until)
91 i->process (until);
93 if (!i->ok ())
94 delete children_p_list_.remove_cons (pp);
95 else
96 pp = & (*pp)->next_;
100 void
101 Simultaneous_music_iterator::skip (Moment until)
103 for (Cons<Music_iterator> **pp = &children_p_list_.head_; *pp;)
105 Music_iterator * i = (*pp)->car_;
106 if (i->pending_moment () <= until)
108 i->skip (until);
110 if (!i->ok ())
111 delete children_p_list_.remove_cons (pp);
112 else
113 pp = & (*pp)->next_;
117 Moment
118 Simultaneous_music_iterator::pending_moment () const
120 Moment next;
121 next.set_infinite (1);
123 for (Cons<Music_iterator> *p = children_p_list_.head_; p; p = p->next_)
124 next = next <? p->car_->pending_moment () ;
125 return next;
130 bool
131 Simultaneous_music_iterator::ok () const
133 return children_p_list_.head_;
136 Music_iterator*
137 Simultaneous_music_iterator::try_music_in_children (Music *m) const
139 Music_iterator * b=0;
140 for (Cons<Music_iterator> *p = children_p_list_.head_; !b && p; p = p->next_)
141 b =p->car_->try_music (m);
142 return b;
147 IMPLEMENT_CTOR_CALLBACK (Simultaneous_music_iterator);