]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator.cc
Don't assign 'safe' values to self_scm_ outside of smobify_self
[lilypond.git] / lily / translator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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 "translator.hh"
21
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "translator-group.hh"
27 #include "warn.hh"
28
29 #include "translator.icc"
30 #include "ly-smobs.icc"
31
32 Translator::~Translator ()
33 {
34 }
35
36 void
37 Translator::init ()
38 {
39   daddy_context_ = 0;
40   smobify_self ();
41 }
42
43 void
44 Translator::process_music ()
45 {
46 }
47
48 void
49 Translator::process_acknowledged ()
50 {
51 }
52
53 Translator::Translator ()
54 {
55   init ();
56 }
57
58 Translator::Translator (Translator const &src)
59 {
60   (void) src;
61   init ();
62 }
63
64 Moment
65 Translator::now_mom () const
66 {
67   return daddy_context_->now_mom ();
68 }
69
70 Output_def *
71 Translator::get_output_def () const
72 {
73   return daddy_context_->get_output_def ();
74 }
75
76 Translator_group *
77 Translator::get_daddy_translator () const
78 {
79   return daddy_context_->implementation ();
80 }
81
82 void
83 Translator::protect_event (SCM ev)
84 {
85   get_daddy_translator ()->protect_event (ev);
86 }
87
88 SCM
89 Translator::internal_get_property (SCM sym) const
90 {
91   return daddy_context_->internal_get_property (sym);
92 }
93
94 void
95 Translator::stop_translation_timestep ()
96 {
97 }
98
99 /*
100   this function is called once each moment, before any user
101   information enters the translators.  (i.e. no \property or event has
102   been processed yet.)
103 */
104 void
105 Translator::start_translation_timestep ()
106 {
107 }
108
109 void
110 Translator::initialize ()
111 {
112 }
113
114 void
115 Translator::finalize ()
116 {
117 }
118
119 void
120 Translator::connect_to_context (Context *c)
121 {
122   for (translator_listener_record *r = get_listener_list (); r; r = r->next_)
123     c->events_below ()->add_listener (r->get_listener_ (this, r->event_class_),
124                                       r->event_class_);
125 }
126
127 void
128 Translator::disconnect_from_context (Context *c)
129 {
130   for (translator_listener_record *r = get_listener_list (); r; r = r->next_)
131     c->events_below ()->remove_listener (r->get_listener_ (this, r->event_class_),
132                                          r->event_class_);
133 }
134
135 /*
136   internally called once, statically, for each translator
137   listener. Connects the name of an event class with a procedure that
138   fetches the corresponding listener.
139
140   The method should only be called from the macro
141   IMPLEMENT_TRANSLATOR_LISTENER.
142  */
143 void
144 Translator::add_translator_listener (translator_listener_record **listener_list,
145                                      translator_listener_record *r,
146                                      Listener (*get_listener) (void *, SCM),
147                                      const char *ev_class)
148 {
149   /* ev_class is the C++ identifier name. Convert to scm symbol */
150   string name = string (ev_class);
151   name = replace_all (&name, '_', '-');
152   name += "-event";
153
154   // we make the symbol permanent in order not to have to bother about
155   // the static translator_listener_record chains while garbage
156   // collecting.
157
158   SCM class_sym = scm_permanent_object (scm_from_locale_symbol (name.c_str ()));
159
160   r->event_class_ = class_sym;
161   r->get_listener_ = get_listener;
162   r->next_ = *listener_list;
163   *listener_list = r;
164 }
165
166 /*
167  Helps the individual static_translator_description methods of translators.
168 */
169 SCM
170 Translator::static_translator_description (const char *grobs,
171                                            const char *desc,
172                                            translator_listener_record *listener_list,
173                                            const char *read,
174                                            const char *write) const
175 {
176   SCM static_properties = SCM_EOL;
177
178   static_properties = scm_acons (ly_symbol2scm ("grobs-created"),
179                                  parse_symbol_list (grobs), static_properties);
180
181   static_properties = scm_acons (ly_symbol2scm ("description"),
182                                  scm_from_locale_string (desc), static_properties);
183
184   SCM list = SCM_EOL;
185   for (; listener_list; listener_list = listener_list->next_)
186     list = scm_cons (listener_list->event_class_, list);
187   static_properties = scm_acons (ly_symbol2scm ("events-accepted"),
188                                  list, static_properties);
189
190   static_properties = scm_acons (ly_symbol2scm ("properties-read"),
191                                  parse_symbol_list (read), static_properties);
192
193   static_properties = scm_acons (ly_symbol2scm ("properties-written"),
194                                  parse_symbol_list (write), static_properties);
195
196   return static_properties;
197 }
198
199 /*
200   SMOBS
201 */
202 SCM
203 Translator::mark_smob (SCM sm)
204 {
205   Translator *me = (Translator *) SCM_CELL_WORD_1 (sm);
206   me->derived_mark ();
207   return SCM_EOL;
208 }
209
210 Global_context *
211 Translator::get_global_context () const
212 {
213   return daddy_context_->get_global_context ();
214 }
215
216 Context *
217 Translator::get_score_context () const
218 {
219   return daddy_context_->get_score_context ();
220 }
221
222 IMPLEMENT_SMOBS (Translator);
223 IMPLEMENT_DEFAULT_EQUAL_P (Translator);
224 IMPLEMENT_TYPE_P (Translator, "ly:translator?");
225
226 bool
227 Translator::must_be_last () const
228 {
229   return false;
230 }
231
232 void
233 Translator::derived_mark () const
234 {
235 }
236
237 int
238 Translator::print_smob (SCM s, SCM port, scm_print_state *)
239 {
240   Translator *me = (Translator *) SCM_CELL_WORD_1 (s);
241   scm_puts ("#<Translator ", port);
242   scm_puts (me->class_name (), port);
243   scm_puts (" >", port);
244   return 1;
245 }
246
247 void
248 add_acknowledger (Engraver_void_function_engraver_grob_info ptr,
249                   char const *func_name,
250                   vector<Acknowledge_information> *ack_array)
251 {
252   Acknowledge_information inf;
253   inf.function_ = ptr;
254
255   string interface_name (func_name);
256
257   interface_name = replace_all (&interface_name, '_', '-');
258   interface_name += "-interface";
259
260   /*
261     this is only called during program init, so safe to use scm_gc_protect_object ()
262   */
263   inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
264   ack_array->push_back (inf);
265 }
266
267 Engraver_void_function_engraver_grob_info
268 generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
269 {
270   for (vsize i = 0; i < ack_array->size (); i++)
271     {
272       if (ack_array->at (i).symbol_ == sym)
273         return ack_array->at (i).function_;
274     }
275   return 0;
276 }
277
278 Moment
279 get_event_length (Stream_event *e)
280 {
281   Moment *m = Moment::unsmob (e->get_property ("length"));
282   if (m)
283     return *m;
284   else
285     return Moment (0);
286 }
287
288 Moment
289 get_event_length (Stream_event *e, Moment now)
290 {
291   Moment len = get_event_length (e);
292
293   if (now.grace_part_)
294     {
295       len.grace_part_ = len.main_part_;
296       len.main_part_ = Rational (0);
297     }
298   return len;
299 }
300
301 /*
302   Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
303   simultaneous events. The helper is only useful in listen_* methods
304   of translators.
305 */
306 bool
307 internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
308 {
309   if (*old_ev
310       && !to_boolean (scm_equal_p ((*old_ev)->self_scm (),
311                                    new_ev->self_scm ())))
312     {
313       /* extract event class from function name */
314       string ev_class = function;
315
316       /* This assertion fails if EVENT_ASSIGNMENT was called outside a
317          translator listener. Don't do that. */
318       const char *prefix = "listen_";
319       assert (0 == ev_class.find (prefix));
320
321       /* "listen_foo_bar" -> "foo-bar" */
322       ev_class.erase (0, strlen (prefix));
323       replace_all (&ev_class, '_', '-');
324
325       new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class.c_str ()));
326       (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class.c_str ()));
327       return false;
328     }
329   else
330     {
331       *old_ev = new_ev;
332       return true;
333     }
334 }
335
336 ADD_TRANSLATOR (Translator,
337                 /* doc */
338                 "Base class.  Not instantiated.",
339
340                 /* create */
341                 "",
342
343                 /* read */
344                 "",
345
346                 /* write */
347                 ""
348                );