2 translator-group.cc -- implement Translator_group
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 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"
26 translator_each (SCM list
, Translator_method method
)
28 for (SCM p
= list
; scm_is_pair (p
); p
= scm_cdr (p
))
29 (unsmob_translator (scm_car (p
))->*method
) ();
33 Translator_group::initialize ()
35 precompute_method_bindings ();
39 Translator_group::connect_to_context (Context
*c
)
43 programming_error ("translator group is already connected to context "
44 + context_
->context_name ());
48 c
->event_source ()->add_listener (GET_LISTENER (create_child_translator
),
49 ly_symbol2scm ("AnnounceNewContext"));
50 for (SCM tr_list
= simple_trans_list_
; scm_is_pair (tr_list
); tr_list
= scm_cdr (tr_list
))
52 Translator
*tr
= unsmob_translator (scm_car (tr_list
));
53 tr
->connect_to_context (c
);
58 Translator_group::disconnect_from_context ()
60 for (SCM tr_list
= simple_trans_list_
; scm_is_pair (tr_list
); tr_list
= scm_cdr (tr_list
))
62 Translator
*tr
= unsmob_translator (scm_car (tr_list
));
63 tr
->disconnect_from_context (context_
);
65 context_
->event_source ()->remove_listener (GET_LISTENER (create_child_translator
),
66 ly_symbol2scm ("AnnounceNewContext"));
68 protected_events_
= SCM_EOL
;
72 Translator_group::finalize ()
77 filter_performers (SCM ell
)
80 for (SCM p
= ell
; scm_is_pair (p
); p
= scm_cdr (p
))
82 if (dynamic_cast<Performer
*> (unsmob_translator (scm_car (*tail
))))
83 *tail
= scm_cdr (*tail
);
85 tail
= SCM_CDRLOC (*tail
);
91 filter_engravers (SCM ell
)
94 for (SCM p
= ell
; scm_is_pair (p
); p
= scm_cdr (p
))
96 if (dynamic_cast<Engraver
*> (unsmob_translator (scm_car (*tail
))))
97 *tail
= scm_cdr (*tail
);
99 tail
= SCM_CDRLOC (*tail
);
105 Protects the parameter from being garbage collected. The object is
106 protected until the next disconnect_from_context call.
108 Whenever a child translator hears an event, the event is added to
109 this list. This eliminates the need for derived_mark methods in most
110 translators; all incoming events are instead protected by the
113 TODO: Should the list also be flushed at the beginning of each new
117 Translator_group::protect_event (SCM ev
)
119 protected_events_
= scm_cons (ev
, protected_events_
);
123 Create a new translator for a newly created child context. Triggered
124 by AnnounceNewContext events.
126 IMPLEMENT_LISTENER (Translator_group
, create_child_translator
);
128 Translator_group::create_child_translator (SCM sev
)
130 Stream_event
*ev
= unsmob_stream_event (sev
);
131 // get from AnnounceNewContext
132 SCM cs
= ev
->get_property ("context");
133 Context
*new_context
= unsmob_context (cs
);
134 Context_def
*def
= unsmob_context_def (new_context
->get_definition ());
135 SCM ops
= new_context
->get_definition_mods ();
137 SCM trans_names
= def
->get_translator_names (ops
);
139 Translator_group
*g
= get_translator_group (def
->get_translator_group_type ());
140 SCM trans_list
= SCM_EOL
;
142 for (SCM s
= trans_names
; scm_is_pair (s
); s
= scm_cdr (s
))
144 Translator
*type
= get_translator (scm_car (s
));
146 warning (_f ("cannot find: `%s'", ly_symbol2string (scm_car (s
)).c_str ()));
149 Translator
*tr
= type
->clone ();
150 SCM str
= tr
->self_scm ();
152 if (tr
->must_be_last ())
154 SCM cons
= scm_cons (str
, SCM_EOL
);
155 if (scm_is_pair (trans_list
))
156 scm_set_cdr_x (scm_last_pair (trans_list
), cons
);
161 trans_list
= scm_cons (str
, trans_list
);
163 tr
->daddy_context_
= new_context
;
168 /* Filter unwanted translator types. Required to make
169 \with {\consists "..."} work. */
170 if (dynamic_cast<Engraver_group
*> (g
))
171 g
->simple_trans_list_
= filter_performers (trans_list
);
172 else if (dynamic_cast<Performer_group
*> (g
))
173 g
->simple_trans_list_
= filter_engravers (trans_list
);
175 // TODO: scrap Context::implementation
176 new_context
->implementation_
= g
;
178 g
->connect_to_context (new_context
);
181 recurse_over_translators (new_context
,
182 &Translator::initialize
,
183 &Translator_group::initialize
,
188 Translator_group::get_simple_trans_list ()
190 return simple_trans_list_
;
194 precomputed_recurse_over_translators (Context
*c
, Translator_precompute_index idx
, Direction dir
)
197 = dynamic_cast<Translator_group
*> (c
->implementation ());
199 if (tg
&& dir
== DOWN
)
201 tg
->precomputed_translator_foreach (idx
);
202 tg
->call_precomputed_self_method (idx
);
205 for (SCM s
= c
->children_contexts (); scm_is_pair (s
);
207 precomputed_recurse_over_translators (unsmob_context (scm_car (s
)), idx
, dir
);
211 tg
->precomputed_translator_foreach (idx
);
212 tg
->call_precomputed_self_method (idx
);
217 recurse_over_translators (Context
*c
, Translator_method ptr
,
218 Translator_group_method tg_ptr
, Direction dir
)
221 = dynamic_cast<Translator_group
*> (c
->implementation ());
223 if (tg
&& dir
== DOWN
)
226 translator_each (tg
->get_simple_trans_list (), ptr
);
229 for (SCM s
= c
->children_contexts (); scm_is_pair (s
);
231 recurse_over_translators (unsmob_context (scm_car (s
)), ptr
, tg_ptr
, dir
);
235 translator_each (tg
->get_simple_trans_list (),
242 Translator_group::Translator_group ()
244 simple_trans_list_
= SCM_EOL
;
245 protected_events_
= SCM_EOL
;
251 Translator_group::derived_mark () const
256 Translator_group::precompute_method_bindings ()
258 for (SCM s
= simple_trans_list_
; scm_is_pair (s
); s
= scm_cdr (s
))
260 Translator
*tr
= unsmob_translator (scm_car (s
));
261 Translator_void_method_ptr ptrs
[TRANSLATOR_METHOD_PRECOMPUTE_COUNT
];
262 tr
->fetch_precomputable_methods (ptrs
);
265 for (int i
= 0; i
< TRANSLATOR_METHOD_PRECOMPUTE_COUNT
; i
++)
268 precomputed_method_bindings_
[i
].push_back (Translator_method_binding (tr
, ptrs
[i
]));
272 fetch_precomputable_methods (precomputed_self_method_bindings_
);
276 Translator_group::precomputed_translator_foreach (Translator_precompute_index idx
)
278 vector
<Translator_method_binding
> &bindings (precomputed_method_bindings_
[idx
]);
279 for (vsize i
= 0; i
< bindings
.size (); i
++)
280 bindings
[i
].invoke ();
284 Translator_group::fetch_precomputable_methods (Translator_group_void_method ptrs
[])
286 for (int i
= 0; i
< TRANSLATOR_METHOD_PRECOMPUTE_COUNT
; i
++)
291 Translator_group::call_precomputed_self_method (Translator_precompute_index idx
)
293 if (precomputed_self_method_bindings_
[idx
])
294 (*precomputed_self_method_bindings_
[idx
]) (this);
297 Translator_group::~Translator_group ()
301 #include "ly-smobs.icc"
303 IMPLEMENT_SMOBS (Translator_group
);
304 IMPLEMENT_DEFAULT_EQUAL_P (Translator_group
);
305 IMPLEMENT_TYPE_P (Translator_group
, "ly:translator-group?");
308 Translator_group::print_smob (SCM s
, SCM port
, scm_print_state
*)
310 Translator_group
*me
= (Translator_group
*) SCM_CELL_WORD_1 (s
);
311 scm_puts ("#<Translator_group ", port
);
312 scm_puts (me
->class_name (), port
);
313 scm_display (me
->simple_trans_list_
, port
);
314 scm_puts (" >", port
);
319 Translator_group::mark_smob (SCM smob
)
321 Translator_group
*me
= (Translator_group
*)SCM_CELL_WORD_1 (smob
);
324 scm_gc_mark (me
->protected_events_
);
325 return me
->simple_trans_list_
;