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