]> git.donarmstrong.com Git - lilypond.git/blob - lily/scheme-engraver.cc
Merge branch 'master' into dev/johngourlay/issue-4751
[lilypond.git] / lily / scheme-engraver.cc
1 /*
2   scheme-engraver.cc -- implement Scheme_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   Copyright (c) 2009--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "scheme-engraver.hh"
23
24 #include "grob.hh"
25
26 #include "translator.icc"
27
28 #include "scm-hash.hh"
29
30 Scheme_engraver::Scheme_engraver (SCM definition)
31 {
32   initialize_function_ = SCM_EOL;
33   finalize_function_ = SCM_EOL;
34
35   interface_acknowledger_hash_ = SCM_EOL;
36   interface_end_acknowledger_hash_ = SCM_EOL;
37
38   must_be_last_ = false;
39   per_instance_listeners_ = SCM_EOL;
40   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
41     precomputable_methods_[i] = SCM_UNDEFINED;
42
43   init_from_scheme (definition);
44 }
45
46 Scheme_engraver::~Scheme_engraver ()
47 {
48 }
49
50 // Extracts the value if callable, if not return SCM_UNDEFINED;
51 static SCM
52 callable (SCM symbol, SCM defn)
53 {
54   SCM val = ly_assoc_get (symbol, defn, SCM_BOOL_F);
55   return ly_is_procedure (val) ? val : SCM_UNDEFINED;
56 }
57
58 bool
59 Scheme_engraver::must_be_last () const
60 {
61   return must_be_last_;
62 }
63
64 void
65 Scheme_engraver::fetch_precomputable_methods (SCM ptrs[])
66 {
67   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
68     ptrs[i] = precomputable_methods_[i];
69 }
70
71 void
72 Scheme_engraver::init_from_scheme (SCM definition)
73 {
74   precomputable_methods_[START_TRANSLATION_TIMESTEP]
75     = callable (ly_symbol2scm ("start-translation-timestep"), definition);
76   precomputable_methods_[STOP_TRANSLATION_TIMESTEP]
77     = callable (ly_symbol2scm ("stop-translation-timestep"), definition);
78   precomputable_methods_[PROCESS_MUSIC]
79     = callable (ly_symbol2scm ("process-music"), definition);
80   precomputable_methods_[PROCESS_ACKNOWLEDGED]
81     = callable (ly_symbol2scm ("process-acknowledged"), definition);
82   initialize_function_ = callable (ly_symbol2scm ("initialize"), definition);
83   finalize_function_ = callable (ly_symbol2scm ("finalize"), definition);
84
85   SCM listeners = ly_assoc_get (ly_symbol2scm ("listeners"), definition, SCM_EOL);
86
87   per_instance_listeners_ = SCM_EOL;
88
89   must_be_last_ = to_boolean (ly_assoc_get (ly_symbol2scm ("must-be-last"),
90                                             definition,
91                                             SCM_BOOL_F));
92
93   for (SCM p = listeners; scm_is_pair (p); p = scm_cdr (p))
94     {
95       SCM event_class = scm_caar (p);
96       SCM proc = scm_cdar (p);
97
98       if (!(ly_is_procedure (proc) && ly_is_symbol (event_class)))
99         continue;
100
101       // We should check the arity of the function?
102
103       // Record for later lookup.
104       per_instance_listeners_ = scm_acons (event_class, proc, per_instance_listeners_);
105     }
106
107   init_acknowledgers (ly_assoc_get (ly_symbol2scm ("acknowledgers"),
108                                     definition, SCM_EOL),
109                       &interface_acknowledger_hash_);
110
111   init_acknowledgers (ly_assoc_get (ly_symbol2scm ("end-acknowledgers"),
112                                     definition, SCM_EOL),
113                       &interface_end_acknowledger_hash_);
114
115   // TODO: hook up description, props read/written, grobs created
116   // etc. to provide automatic documentation.
117 }
118
119 void
120 Scheme_engraver::init_acknowledgers (SCM alist,
121                                      SCM *hash)
122 {
123   *hash = Scheme_hash_table::make_smob ();
124   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
125     {
126       SCM iface = scm_caar (p);
127       SCM proc = scm_cdar (p);
128
129       if (!(ly_is_procedure (proc) && ly_is_symbol (iface)))
130         continue;
131
132       unsmob<Scheme_hash_table>(*hash)->set (iface, proc);
133     }
134 }
135
136 SCM
137 Scheme_engraver::get_listener_list () const
138 {
139   return per_instance_listeners_;
140 }
141
142 void
143 Scheme_engraver::initialize ()
144 {
145   if (!SCM_UNBNDP (initialize_function_))
146     scm_call_1 (initialize_function_, self_scm ());
147 }
148
149 void
150 Scheme_engraver::finalize ()
151 {
152   if (!SCM_UNBNDP (finalize_function_))
153     scm_call_1 (finalize_function_, self_scm ());
154 }
155
156 void
157 Scheme_engraver::derived_mark () const
158 {
159   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
160     scm_gc_mark (precomputable_methods_[i]);
161
162   scm_gc_mark (initialize_function_);
163   scm_gc_mark (finalize_function_);
164   scm_gc_mark (per_instance_listeners_);
165   scm_gc_mark (interface_acknowledger_hash_);
166   scm_gc_mark (interface_end_acknowledger_hash_);
167 }