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