]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-repeat-iterator.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / volta-repeat-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "music.hh"
21 #include "sequential-iterator.hh"
22 #include "context.hh"
23 #include "lily-imports.hh"
24
25 using std::string;
26
27 class Volta_repeat_iterator : public Sequential_iterator
28 {
29 public:
30   DECLARE_SCHEME_CALLBACK (constructor, ());
31   Volta_repeat_iterator ();
32
33   void add_repeat_command (SCM);
34 protected:
35   virtual SCM get_music_list () const;
36   virtual void next_element (bool);
37   virtual void construct_children ();
38   virtual void process (Moment);
39   virtual void derived_mark () const;
40
41   bool first_time_;
42   int alt_count_;
43   int rep_count_;
44   int done_count_;
45   SCM alt_restores_;
46 };
47
48 Volta_repeat_iterator::Volta_repeat_iterator ()
49 {
50   done_count_ = alt_count_ = rep_count_ = 0;
51   first_time_ = true;
52   alt_restores_ = SCM_EOL;
53 }
54
55 void
56 Volta_repeat_iterator::derived_mark () const
57 {
58   scm_gc_mark (alt_restores_);
59   Sequential_iterator::derived_mark ();
60 }
61
62 SCM
63 Volta_repeat_iterator::get_music_list ()const
64 {
65   return scm_cons (get_music ()->get_property ("element"),
66                    Sequential_iterator::get_music_list ());
67 }
68
69 void
70 Volta_repeat_iterator::construct_children ()
71 {
72   Sequential_iterator::construct_children ();
73
74   SCM alts = get_music ()->get_property ("elements");
75
76   alt_count_ = int (scm_ilength (alts));
77   rep_count_ = scm_to_int (get_music ()->get_property ("repeat-count"));
78   done_count_ = 0;
79 }
80
81 /*
82   TODO: add source information for debugging
83 */
84 void
85 Volta_repeat_iterator::add_repeat_command (SCM what)
86 {
87   SCM reps = ly_symbol2scm ("repeatCommands");
88   SCM current_reps = SCM_EOL;
89   Context *where = get_outlet ()->where_defined (reps, &current_reps);
90
91   if (where && ly_cheap_is_list (current_reps))
92     {
93       current_reps = scm_cons (what, current_reps);
94       where->set_property (reps, current_reps);
95     }
96 }
97
98 void
99 Volta_repeat_iterator::next_element (bool side_effect)
100 {
101   done_count_++;
102
103   Sequential_iterator::next_element (side_effect);
104
105   if (side_effect)
106     {
107       if (alt_count_)
108         {
109           string repstr = ::to_string (rep_count_ - alt_count_ + done_count_) + ".";
110           if (done_count_ <= 1)
111             {
112               alt_restores_ = SCM_EOL;
113               if (to_boolean (get_outlet ()->get_property ("timing")))
114                 {
115                   for (SCM lst = get_outlet ()->get_property ("alternativeRestores");
116                        scm_is_pair (lst);
117                        lst = scm_cdr (lst))
118                     {
119                       SCM res = SCM_EOL;
120                       Context *t = get_outlet ()->where_defined (scm_car (lst),
121                                                                  &res);
122                       if (t)
123                         {
124                           alt_restores_ = scm_cons
125                             (scm_list_3 (t->self_scm (), scm_car (lst), res),
126                              alt_restores_);
127                         }
128                     }
129                 }
130             }
131           else
132             {
133
134               add_repeat_command (scm_list_n (ly_symbol2scm ("volta"), SCM_BOOL_F, SCM_UNDEFINED));
135
136               if (done_count_ - 1 < alt_count_)
137                 {
138                   add_repeat_command (ly_symbol2scm ("end-repeat"));
139
140                   if (to_boolean (get_outlet ()->get_property ("timing")))
141                     {
142                       for (SCM p = alt_restores_; scm_is_pair (p); p = scm_cdr (p))
143                         scm_apply_0 (Lily::ly_context_set_property_x,
144                                      scm_car (p));
145                     }
146                 }
147             }
148
149           if (done_count_ == 1 && alt_count_ < rep_count_)
150             repstr = "1.--" + ::to_string (rep_count_ - alt_count_ + done_count_) + ".";
151
152           if (done_count_ <= alt_count_)
153             add_repeat_command (scm_list_n (ly_symbol2scm ("volta"),
154                                             ly_string2scm (repstr), SCM_UNDEFINED));
155         }
156       else
157         add_repeat_command (ly_symbol2scm ("end-repeat"));
158     }
159 }
160
161 void
162 Volta_repeat_iterator::process (Moment m)
163 {
164   if (first_time_)
165     {
166       add_repeat_command (ly_symbol2scm ("start-repeat"));
167       first_time_ = false;
168     }
169   Sequential_iterator::process (m);
170 }
171
172 IMPLEMENT_CTOR_CALLBACK (Volta_repeat_iterator);