2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "translator.hh"
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "translator-group.hh"
29 #include "translator.icc"
30 #include "ly-smobs.icc"
32 Translator::~Translator ()
44 Translator::process_music ()
49 Translator::process_acknowledged ()
53 Translator::Translator ()
58 Translator::Translator (Translator const &src)
65 Translator::now_mom () const
67 return daddy_context_->now_mom ();
71 Translator::get_output_def () const
73 return daddy_context_->get_output_def ();
77 Translator::get_daddy_translator () const
79 return daddy_context_->implementation ();
83 Translator::protect_event (SCM ev)
85 get_daddy_translator ()->protect_event (ev);
89 Translator::internal_get_property (SCM sym) const
91 return daddy_context_->internal_get_property (sym);
95 Translator::stop_translation_timestep ()
100 this function is called once each moment, before any user
101 information enters the translators. (i.e. no \property or event has
105 Translator::start_translation_timestep ()
110 Translator::initialize ()
115 Translator::finalize ()
120 Translator::connect_to_context (Context *c)
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_),
128 Translator::disconnect_from_context (Context *c)
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_),
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.
140 The method should only be called from the macro
141 IMPLEMENT_TRANSLATOR_LISTENER.
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)
149 /* ev_class is the C++ identifier name. Convert to scm symbol */
150 string name = string (ev_class);
151 name = replace_all (&name, '_', '-');
154 // we make the symbol permanent in order not to have to bother about
155 // the static translator_listener_record chains while garbage
158 SCM class_sym = scm_permanent_object (scm_from_locale_symbol (name.c_str ()));
160 r->event_class_ = class_sym;
161 r->get_listener_ = get_listener;
162 r->next_ = *listener_list;
167 Helps the individual static_translator_description methods of translators.
170 Translator::static_translator_description (const char *grobs,
172 translator_listener_record *listener_list,
174 const char *write) const
176 SCM static_properties = SCM_EOL;
178 static_properties = scm_acons (ly_symbol2scm ("grobs-created"),
179 parse_symbol_list (grobs), static_properties);
181 static_properties = scm_acons (ly_symbol2scm ("description"),
182 scm_from_locale_string (desc), static_properties);
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);
190 static_properties = scm_acons (ly_symbol2scm ("properties-read"),
191 parse_symbol_list (read), static_properties);
193 static_properties = scm_acons (ly_symbol2scm ("properties-written"),
194 parse_symbol_list (write), static_properties);
196 return static_properties;
203 Translator::mark_smob (SCM sm)
205 Translator *me = (Translator *) SCM_CELL_WORD_1 (sm);
211 Translator::get_global_context () const
213 return daddy_context_->get_global_context ();
217 Translator::get_score_context () const
219 return daddy_context_->get_score_context ();
222 IMPLEMENT_SMOBS (Translator);
223 IMPLEMENT_DEFAULT_EQUAL_P (Translator);
224 IMPLEMENT_TYPE_P (Translator, "ly:translator?");
227 Translator::must_be_last () const
233 Translator::derived_mark () const
238 Translator::print_smob (SCM s, SCM port, scm_print_state *)
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);
248 add_acknowledger (Engraver_void_function_engraver_grob_info ptr,
249 char const *func_name,
250 vector<Acknowledge_information> *ack_array)
252 Acknowledge_information inf;
255 string interface_name (func_name);
257 interface_name = replace_all (&interface_name, '_', '-');
258 interface_name += "-interface";
261 this is only called during program init, so safe to use scm_gc_protect_object ()
263 inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
264 ack_array->push_back (inf);
267 Engraver_void_function_engraver_grob_info
268 generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
270 for (vsize i = 0; i < ack_array->size (); i++)
272 if (ack_array->at (i).symbol_ == sym)
273 return ack_array->at (i).function_;
279 get_event_length (Stream_event *e)
281 Moment *m = Moment::unsmob (e->get_property ("length"));
289 get_event_length (Stream_event *e, Moment now)
291 Moment len = get_event_length (e);
295 len.grace_part_ = len.main_part_;
296 len.main_part_ = Rational (0);
302 Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
303 simultaneous events. The helper is only useful in listen_* methods
307 internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
310 && !to_boolean (scm_equal_p ((*old_ev)->self_scm (),
311 new_ev->self_scm ())))
313 /* extract event class from function name */
314 string ev_class = function;
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));
321 /* "listen_foo_bar" -> "foo-bar" */
322 ev_class.erase (0, strlen (prefix));
323 replace_all (&ev_class, '_', '-');
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 ()));
336 ADD_TRANSLATOR (Translator,
338 "Base class. Not instantiated.",