2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 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 ()
45 Translator::process_music ()
50 Translator::process_acknowledged ()
54 Translator::Translator ()
59 Translator::Translator (Translator const &src)
66 Translator::now_mom () const
68 return daddy_context_->now_mom ();
72 Translator::get_output_def () const
74 return daddy_context_->get_output_def ();
78 Translator::get_daddy_translator () const
80 return daddy_context_->implementation ();
84 Translator::protect_event (SCM ev)
86 get_daddy_translator ()->protect_event (ev);
90 Translator::internal_get_property (SCM sym) const
92 return daddy_context_->internal_get_property (sym);
96 Translator::stop_translation_timestep ()
101 this function is called once each moment, before any user
102 information enters the translators. (i.e. no \property or event has
106 Translator::start_translation_timestep ()
111 Translator::initialize ()
116 Translator::finalize ()
121 Translator::connect_to_context (Context *c)
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_),
129 Translator::disconnect_from_context (Context *c)
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_),
136 static SCM listened_event_class_table;
138 ensure_listened_hash ()
140 if (!listened_event_class_table)
141 listened_event_class_table = scm_permanent_object (scm_c_make_hash_table (61));
144 LY_DEFINE (ly_get_listened_event_classes, "ly:get-listened-event-classes",
146 "Return a list of all event classes that some translator listens"
149 ensure_listened_hash ();
150 return ly_hash_table_keys (listened_event_class_table);
153 LY_DEFINE (ly_is_listened_event_class, "ly:is-listened-event-class",
155 "Is @var{sym} a listened event class?")
157 ensure_listened_hash ();
158 return scm_hashq_ref (listened_event_class_table, sym, SCM_BOOL_F);
162 add_listened_event_class (SCM sym)
164 ensure_listened_hash ();
165 scm_hashq_set_x (listened_event_class_table, sym, SCM_BOOL_T);
169 internally called once, statically, for each translator
170 listener. Connects the name of an event class with a procedure that
171 fetches the corresponding listener.
173 The method should only be called from the macro
174 IMPLEMENT_TRANSLATOR_LISTENER.
177 Translator::add_translator_listener (translator_listener_record **listener_list,
178 translator_listener_record *r,
179 Listener (*get_listener) (void *, SCM),
180 const char *ev_class)
182 /* ev_class is the C++ identifier name. Convert to scm symbol */
183 string name = string (ev_class);
184 name = replace_all (&name, '_', '-');
187 SCM class_sym = scm_from_locale_symbol (name.c_str ());
189 add_listened_event_class (class_sym);
191 r->event_class_ = class_sym;
192 r->get_listener_ = get_listener;
193 r->next_ = *listener_list;
198 Helps the individual static_translator_description methods of translators.
201 Translator::static_translator_description (const char *grobs,
203 translator_listener_record *listener_list,
205 const char *write) const
207 SCM static_properties = SCM_EOL;
209 static_properties = scm_acons (ly_symbol2scm ("grobs-created"),
210 parse_symbol_list (grobs), static_properties);
212 static_properties = scm_acons (ly_symbol2scm ("description"),
213 scm_from_locale_string (desc), static_properties);
216 for (; listener_list; listener_list = listener_list->next_)
217 list = scm_cons (listener_list->event_class_, list);
218 static_properties = scm_acons (ly_symbol2scm ("events-accepted"),
219 list, static_properties);
221 static_properties = scm_acons (ly_symbol2scm ("properties-read"),
222 parse_symbol_list (read), static_properties);
224 static_properties = scm_acons (ly_symbol2scm ("properties-written"),
225 parse_symbol_list (write), static_properties);
227 return static_properties;
234 Translator::mark_smob (SCM sm)
236 Translator *me = (Translator *) SCM_CELL_WORD_1 (sm);
242 Translator::get_global_context () const
244 return daddy_context_->get_global_context ();
248 Translator::get_score_context () const
250 return daddy_context_->get_score_context ();
253 IMPLEMENT_SMOBS (Translator);
254 IMPLEMENT_DEFAULT_EQUAL_P (Translator);
255 IMPLEMENT_TYPE_P (Translator, "ly:translator?");
258 Translator::must_be_last () const
264 Translator::derived_mark () const
269 Translator::print_smob (SCM s, SCM port, scm_print_state *)
271 Translator *me = (Translator *) SCM_CELL_WORD_1 (s);
272 scm_puts ("#<Translator ", port);
273 scm_puts (me->class_name (), port);
274 scm_puts (" >", port);
279 add_acknowledger (Engraver_void_function_engraver_grob_info ptr,
280 char const *func_name,
281 vector<Acknowledge_information> *ack_array)
283 Acknowledge_information inf;
286 string interface_name (func_name);
288 interface_name = replace_all (&interface_name, '_', '-');
289 interface_name += "-interface";
292 this is only called during program init, so safe to use scm_gc_protect_object ()
294 inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
295 ack_array->push_back (inf);
298 Engraver_void_function_engraver_grob_info
299 generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
301 for (vsize i = 0; i < ack_array->size (); i++)
303 if (ack_array->at (i).symbol_ == sym)
304 return ack_array->at (i).function_;
310 get_event_length (Stream_event *e)
312 Moment *m = unsmob_moment (e->get_property ("length"));
320 get_event_length (Stream_event *e, Moment now)
322 Moment len = get_event_length (e);
326 len.grace_part_ = len.main_part_;
327 len.main_part_ = Rational (0);
333 Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
334 simultaneous events. The helper is only useful in listen_* methods
338 internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
341 && !to_boolean (scm_equal_p ((*old_ev)->self_scm (),
342 new_ev->self_scm ())))
344 /* extract event class from function name */
345 string ev_class = function;
347 /* This assertion fails if EVENT_ASSIGNMENT was called outside a
348 translator listener. Don't do that. */
349 const char *prefix = "listen_";
350 assert (0 == ev_class.find (prefix));
352 /* "listen_foo_bar" -> "foo-bar" */
353 ev_class.erase (0, strlen (prefix));
354 replace_all (&ev_class, '_', '-');
356 new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class.c_str ()));
357 (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class.c_str ()));
367 ADD_TRANSLATOR (Translator,
369 "Base class. Not instantiated.",