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