]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-iterator.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / percent-repeat-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--2015  Han-Wen Nienhuys <hanwen@xs4all.nl>
5                   Erik Sandberg <mandolaerik@gmail.com>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "context.hh"
22 #include "input.hh"
23 #include "repeated-music.hh"
24 #include "sequential-iterator.hh"
25 #include "lily-imports.hh"
26
27 using std::string;
28
29 class Percent_repeat_iterator : public Sequential_iterator
30 {
31 public:
32   DECLARE_CLASSNAME (Percent_repeat_iterator);
33   DECLARE_SCHEME_CALLBACK (constructor, ());
34   Percent_repeat_iterator ();
35 protected:
36   virtual SCM get_music_list () const;
37   virtual void construct_children ();
38   virtual void next_element (bool);
39   virtual void derived_mark () const;
40 private:
41   SCM child_list_;
42   int starting_bar_;
43 };
44
45 IMPLEMENT_CTOR_CALLBACK (Percent_repeat_iterator);
46
47 Percent_repeat_iterator::Percent_repeat_iterator ()
48   : child_list_ (SCM_UNDEFINED), starting_bar_ (-1)
49 {
50 }
51
52 void
53 Percent_repeat_iterator::derived_mark () const
54 {
55   scm_gc_mark (child_list_);
56 }
57
58 void
59 Percent_repeat_iterator::construct_children ()
60 {
61   Music *mus = get_music ();
62
63   Music *child = Repeated_music::body (mus);
64   child_list_ = scm_list_1 (child->self_scm ());
65
66   Sequential_iterator::construct_children ();
67
68   descend_to_bottom_context ();
69   if (!measure_position (get_outlet ()).main_part_)
70     starting_bar_ =
71       robust_scm2int (get_outlet ()->get_property ("internalBarNumber"), 0);
72 }
73
74 SCM
75 Percent_repeat_iterator::get_music_list () const
76 {
77   return child_list_;
78 }
79
80 // Arrive here when child processing has been completed.  At that
81 // point of time, we can determine what kind of percent expression we
82 // are dealing with and extend the child list just in time before the
83 // next iterator is getting fetched.
84 void
85 Percent_repeat_iterator::next_element (bool side_effect)
86 {
87   // Do nothing if we already did our processing here.
88   if (!scm_is_pair (child_list_))
89     {
90       Sequential_iterator::next_element (side_effect);
91       return;
92     }
93
94   Music *mus = get_music ();
95
96   Music *child = Repeated_music::body (mus);
97   SCM length = child->get_length ().smobbed_copy ();
98
99   int current_bar = -1;
100   if (!measure_position (get_outlet ()).main_part_)
101     current_bar =
102       robust_scm2int (get_outlet ()->get_property ("internalBarNumber"), 0);
103
104   SCM child_list = SCM_EOL;
105
106   string event_type;
107   SCM slash_count = SCM_EOL;
108
109   if (starting_bar_ >= 0 && current_bar == starting_bar_ + 1)
110     event_type = "PercentEvent";
111   else if (starting_bar_ >=0 && current_bar == starting_bar_ + 2)
112     event_type = "DoublePercentEvent";
113   else
114     {
115       slash_count = Lily::calc_repeat_slash_count (child->self_scm ());
116       event_type = "RepeatSlashEvent";
117     }
118
119   int repeats = scm_to_int (mus->get_property ("repeat-count"));
120   for (int i = repeats; i > 1; i--)
121     {
122       Music *percent = make_music_by_name (ly_symbol2scm (event_type.c_str ()));
123       percent->set_spot (*mus->origin ());
124       percent->set_property ("length", length);
125       if (repeats > 1)
126         {
127           percent->set_property ("repeat-count", scm_from_int (i));
128           if (event_type == "RepeatSlashEvent")
129             percent->set_property ("slash-count", slash_count);
130         }
131
132       child_list = scm_cons (percent->unprotect (), child_list);
133     }
134
135   scm_set_cdr_x (child_list_, child_list);
136   child_list_ = SCM_EOL;
137
138   Sequential_iterator::next_element (side_effect);
139 }