2 translator.cc -- implement Translator
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "translator.hh"
11 #include "context-def.hh"
12 #include "dispatcher.hh"
13 #include "global-context.hh"
14 #include "international.hh"
15 #include "translator-group.hh"
18 #include "translator.icc"
19 #include "ly-smobs.icc"
21 Translator::~Translator ()
28 must_be_last_ = false;
35 Translator::process_music ()
40 Translator::process_acknowledged ()
44 Translator::Translator ()
49 Translator::Translator (Translator const &src)
52 must_be_last_ = src.must_be_last_;
56 Translator::try_music (Music *)
62 Translator::now_mom () const
64 return daddy_context_->now_mom ();
68 Translator::get_output_def () const
70 return daddy_context_->get_output_def ();
74 Translator::get_daddy_translator () const
76 return daddy_context_->implementation ();
80 Translator::protect_event (SCM ev)
82 get_daddy_translator ()->protect_event (ev);
86 Translator::internal_get_property (SCM sym) const
88 return daddy_context_->internal_get_property (sym);
92 Translator::stop_translation_timestep ()
97 this function has 2 properties
99 - It is called before try_music ()
101 - It is called before any user information enters the translators.
102 (i.e. any \property or event is not processed yet.)
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_);
127 Translator::disconnect_from_context (Context *c)
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_);
133 static SCM listened_event_class_table;
135 ensure_listened_hash ()
137 if (!listened_event_class_table)
138 listened_event_class_table = scm_permanent_object (scm_c_make_hash_table (61));
142 LY_DEFINE (ly_get_listened_event_classes, "ly:get-listened-event-classes",
144 "Returns a list of all event classes that some translator listens to.")
146 ensure_listened_hash ();
147 return ly_hash_table_keys (listened_event_class_table);
150 LY_DEFINE (ly_is_listened_event_class, "ly:is-listened-event-class",
152 "Is @var{sym} a listened event class?")
154 ensure_listened_hash ();
155 return scm_hashq_ref (listened_event_class_table, sym, SCM_BOOL_F);
159 add_listened_event_class (SCM sym)
161 ensure_listened_hash ();
163 scm_hashq_set_x (listened_event_class_table, sym, SCM_BOOL_T);
168 internally called once, statically, for each translator
169 listener. Connects the name of an event class with a procedure that
170 fetches the corresponding listener.
172 The method should only be called from the macro
173 IMPLEMENT_TRANSLATOR_LISTENER.
176 Translator::add_translator_listener (translator_listener_record **listener_list,
177 translator_listener_record *r,
178 Listener (*get_listener) (void *),
179 const char *ev_class)
181 /* ev_class is the C++ identifier name. Convert to scm symbol */
182 string name = string (ev_class);
183 name = replace_all (name, '_', '-');
186 SCM class_sym = scm_str2symbol (name.c_str ());
188 add_listened_event_class (class_sym);
189 r->event_class_ = class_sym;
190 r->get_listener_ = get_listener;
191 r->next_ = *listener_list;
199 Translator::mark_smob (SCM sm)
201 Translator *me = (Translator *) SCM_CELL_WORD_1 (sm);
207 Translator::get_global_context () const
209 return daddy_context_->get_global_context ();
213 Translator::get_score_context () const
215 return daddy_context_->get_score_context ();
218 IMPLEMENT_SMOBS (Translator);
219 IMPLEMENT_DEFAULT_EQUAL_P (Translator);
220 IMPLEMENT_TYPE_P (Translator, "ly:translator?");
223 Translator::must_be_last () const
225 return must_be_last_;
229 Translator::derived_mark () const
234 Translator::print_smob (SCM s, SCM port, scm_print_state *)
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);
244 add_acknowledger (Engraver_void_function_engraver_grob_info ptr,
245 char const *func_name,
246 vector<Acknowledge_information> *ack_array)
248 Acknowledge_information inf;
251 string interface_name (func_name);
253 interface_name = replace_all (interface_name, '_', '-');
254 interface_name += "-interface";
256 inf.symbol_ = ly_symbol2scm (interface_name.c_str ());
257 ack_array->push_back (inf);
260 Engraver_void_function_engraver_grob_info
261 generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
263 for (vsize i = 0; i < ack_array->size (); i++)
265 if (ack_array->at (i).symbol_ == sym)
266 return ack_array->at (i).function_;
272 get_event_length (Stream_event *e)
274 Moment *m = unsmob_moment (e->get_property ("length"));
282 Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
283 simultaneous events. The helper is only useful in listen_* methods
287 internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
291 /* extract event class from function name */
292 const char *prefix = "listen_";
293 assert (!strncmp (function, "listen_", strlen (prefix)));
294 function += strlen (prefix);
295 char ev_class[strlen (function) + 1];
296 strcpy (ev_class, function);
297 for (char *c = ev_class; *c; c++)
301 new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class));
302 (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class));
312 ADD_TRANSLATOR (Translator,
313 "Base class. Unused",