2 translator-group.cc -- implement Translator_group
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>,
7 Erik Sandberg <mandolaerik@gmail.com>
10 #include "translator-group.hh"
12 #include "context-def.hh"
14 #include "dispatcher.hh"
15 #include "engraver-group.hh"
16 #include "international.hh"
19 #include "output-def.hh"
20 #include "performer-group.hh"
21 #include "scm-hash.hh"
22 #include "stream-event.hh"
26 Translator_group::get_daddy_translator () const
28 return context ()->get_parent_context ()->implementation ();
32 translator_each (SCM list
, Translator_method method
)
34 for (SCM p
= list
; scm_is_pair (p
); p
= scm_cdr (p
))
35 (unsmob_translator (scm_car (p
))->*method
) ();
39 Translator_group::initialize ()
41 precompute_method_bindings ();
45 Translator_group::connect_to_context (Context
*c
)
49 programming_error ("translator group is already connected to context "
50 + context_
->context_name ());
54 c
->event_source ()->add_listener (GET_LISTENER (create_child_translator
),
55 ly_symbol2scm ("AnnounceNewContext"));
56 for (SCM tr_list
= simple_trans_list_
; scm_is_pair (tr_list
); tr_list
= scm_cdr (tr_list
))
58 Translator
*tr
= unsmob_translator (scm_car (tr_list
));
59 tr
->connect_to_context (c
);
64 Translator_group::disconnect_from_context ()
66 for (SCM tr_list
= simple_trans_list_
; scm_is_pair (tr_list
); tr_list
= scm_cdr (tr_list
))
68 Translator
*tr
= unsmob_translator (scm_car (tr_list
));
69 tr
->disconnect_from_context (context_
);
71 context_
->event_source ()->remove_listener (GET_LISTENER (create_child_translator
),
72 ly_symbol2scm ("AnnounceNewContext"));
74 protected_events_
= SCM_EOL
;
78 Translator_group::finalize ()
83 filter_performers (SCM ell
)
86 for (SCM p
= ell
; scm_is_pair (p
); p
= scm_cdr (p
))
88 if (dynamic_cast<Performer
*> (unsmob_translator (scm_car (*tail
))))
89 *tail
= scm_cdr (*tail
);
91 tail
= SCM_CDRLOC (*tail
);
97 filter_engravers (SCM ell
)
100 for (SCM p
= ell
; scm_is_pair (p
); p
= scm_cdr (p
))
102 if (dynamic_cast<Engraver
*> (unsmob_translator (scm_car (*tail
))))
103 *tail
= scm_cdr (*tail
);
105 tail
= SCM_CDRLOC (*tail
);
111 Protects the parameter from being garbage collected. The object is
112 protected until the next disconnect_from_context call.
114 Whenever a child translator hears an event, the event is added to
115 this list. This eliminates the need for derived_mark methods in most
116 translators; all incoming events are instead protected by the
119 TODO: Should the list also be flushed at the beginning of each new
123 Translator_group::protect_event (SCM ev
)
125 protected_events_
= scm_cons (ev
, protected_events_
);
129 Create a new translator for a newly created child context. Triggered
130 by AnnounceNewContext events.
132 IMPLEMENT_LISTENER (Translator_group
, create_child_translator
);
134 Translator_group::create_child_translator (SCM sev
)
136 Stream_event
*ev
= unsmob_stream_event (sev
);
137 // get from AnnounceNewContext
138 SCM cs
= ev
->get_property ("context");
139 Context
*new_context
= unsmob_context (cs
);
140 Context_def
*def
= unsmob_context_def (new_context
->get_definition ());
141 SCM ops
= new_context
->get_definition_mods ();
143 SCM trans_names
= def
->get_translator_names (ops
);
145 Translator_group
*g
= get_translator_group (def
->get_translator_group_type ());
146 SCM trans_list
= SCM_EOL
;
148 for (SCM s
= trans_names
; scm_is_pair (s
); s
= scm_cdr (s
))
150 Translator
*type
= get_translator (scm_car (s
));
152 warning (_f ("can't find: `%s'", ly_symbol2string (scm_car (s
)).c_str ()));
155 Translator
*tr
= type
->clone ();
156 SCM str
= tr
->self_scm ();
158 if (tr
->must_be_last ())
160 SCM cons
= scm_cons (str
, SCM_EOL
);
161 if (scm_is_pair (trans_list
))
162 scm_set_cdr_x (scm_last_pair (trans_list
), cons
);
167 trans_list
= scm_cons (str
, trans_list
);
169 tr
->daddy_context_
= new_context
;
174 /* Filter unwanted translator types. Required to make
175 \with {\consists "..."} work. */
176 if (dynamic_cast<Engraver_group
*> (g
))
177 g
->simple_trans_list_
= filter_performers (trans_list
);
178 else if (dynamic_cast<Performer_group
*> (g
))
179 g
->simple_trans_list_
= filter_engravers (trans_list
);
181 // TODO: scrap Context::implementation
182 new_context
->implementation_
= g
;
184 g
->connect_to_context (new_context
);
187 recurse_over_translators (new_context
,
188 &Translator::initialize
,
189 &Translator_group::initialize
,
194 Translator_group::get_simple_trans_list ()
196 return simple_trans_list_
;
200 precomputed_recurse_over_translators (Context
*c
, Translator_precompute_index idx
, Direction dir
)
203 = dynamic_cast<Translator_group
*> (c
->implementation ());
207 tg
->precomputed_translator_foreach (idx
);
208 tg
->call_precomputed_self_method (idx
);
211 for (SCM s
= c
->children_contexts (); scm_is_pair (s
);
213 precomputed_recurse_over_translators (unsmob_context (scm_car (s
)), idx
, dir
);
217 tg
->precomputed_translator_foreach (idx
);
218 tg
->call_precomputed_self_method (idx
);
223 recurse_over_translators (Context
*c
, Translator_method ptr
, Translator_group_method tg_ptr
, Direction dir
)
226 = dynamic_cast<Translator_group
*> (c
->implementation ());
231 translator_each (tg
->get_simple_trans_list (), ptr
);
234 for (SCM s
= c
->children_contexts (); scm_is_pair (s
);
236 recurse_over_translators (unsmob_context (scm_car (s
)), ptr
, tg_ptr
, dir
);
240 translator_each (tg
->get_simple_trans_list (),
247 Translator_group::Translator_group ()
249 simple_trans_list_
= SCM_EOL
;
250 protected_events_
= SCM_EOL
;
256 Translator_group::derived_mark () const
261 Translator_group::precompute_method_bindings ()
263 for (SCM s
= simple_trans_list_
; scm_is_pair (s
); s
= scm_cdr (s
))
265 Translator
*tr
= unsmob_translator (scm_car (s
));
266 Translator_void_method_ptr ptrs
[TRANSLATOR_METHOD_PRECOMPUTE_COUNT
];
267 tr
->fetch_precomputable_methods (ptrs
);
270 for (int i
= 0; i
< TRANSLATOR_METHOD_PRECOMPUTE_COUNT
; i
++)
273 precomputed_method_bindings_
[i
].push_back (Translator_method_binding (tr
, ptrs
[i
]));
277 fetch_precomputable_methods (precomputed_self_method_bindings_
);
281 Translator_group::precomputed_translator_foreach (Translator_precompute_index idx
)
283 vector
<Translator_method_binding
> &bindings (precomputed_method_bindings_
[idx
]);
284 for (vsize i
= 0; i
< bindings
.size (); i
++)
285 bindings
[i
].invoke ();
289 Translator_group::fetch_precomputable_methods (Translator_group_void_method ptrs
[])
291 for (int i
= 0; i
< TRANSLATOR_METHOD_PRECOMPUTE_COUNT
; i
++)
296 Translator_group::call_precomputed_self_method (Translator_precompute_index idx
)
298 if (precomputed_self_method_bindings_
[idx
])
299 (*precomputed_self_method_bindings_
[idx
]) (this);
302 Translator_group::~Translator_group ()
306 #include "ly-smobs.icc"
308 IMPLEMENT_SMOBS (Translator_group
);
309 IMPLEMENT_DEFAULT_EQUAL_P (Translator_group
);
310 IMPLEMENT_TYPE_P (Translator_group
, "ly:translator-group?");
313 Translator_group::print_smob (SCM s
, SCM port
, scm_print_state
*)
315 Translator_group
*me
= (Translator_group
*) SCM_CELL_WORD_1 (s
);
316 scm_puts ("#<Translator_group ", port
);
317 scm_puts (me
->class_name (), port
);
318 scm_display (me
->simple_trans_list_
, port
);
319 scm_puts (" >", port
);
324 Translator_group::mark_smob (SCM smob
)
326 Translator_group
*me
= (Translator_group
*)SCM_CELL_WORD_1 (smob
);
329 scm_gc_mark (me
->protected_events_
);
330 return me
->simple_trans_list_
;