2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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/>.
23 #include "global-ctor.hh"
24 #include "lily-proto.hh"
25 #include "virtual-methods.hh"
26 #include "callback.hh"
27 #include "input.hh" // for error reporting
29 #include "std-vector.hh"
30 #include "protected-scm.hh"
32 #define TRANSLATOR_FAMILY_DECLARATIONS(NAME) \
34 VIRTUAL_COPY_CONSTRUCTOR (Translator, NAME); \
35 virtual void fetch_precomputable_methods (SCM methods[]); \
36 DECLARE_TRANSLATOR_CALLBACKS (NAME); \
37 TRANSLATOR_INHERIT (Translator) \
40 #define TRANSLATOR_INHERIT(BASE) \
41 using BASE::method_finder; \
42 using BASE::ack_finder;
44 #define DECLARE_TRANSLATOR_CALLBACKS(NAME) \
45 template <void (NAME::*mf)()> \
46 static SCM method_finder () { return method_find_base<NAME, mf> (); } \
47 template <void (NAME::*callback)(Grob_info)> \
48 static SCM ack_finder () { return ack_find_base<NAME, callback> (); } \
52 Each translator class has a static alist of event class symbols
53 mapping to callbacks that are called with a translator instance and
54 a stream event when an event of the appropriate event class is
55 announced in a context.
58 #define TRANSLATOR_DECLARATIONS(NAME) \
60 TRANSLATOR_FAMILY_DECLARATIONS (NAME); \
61 static Drul_array<Protected_scm> acknowledge_static_array_drul_; \
62 static SCM static_description_; \
63 static Protected_scm listener_list_; \
64 static SCM static_get_acknowledger (SCM sym); \
65 static SCM static_get_end_acknowledger(SCM); \
66 virtual SCM get_acknowledger (SCM sym) \
68 return static_get_acknowledger (sym); \
70 virtual SCM get_end_acknowledger (SCM sym) \
72 return static_get_end_acknowledger (sym); \
76 virtual SCM static_translator_description () const; \
77 virtual SCM translator_description () const; \
78 virtual SCM get_listener_list () const \
80 return listener_list_; \
84 #define DECLARE_TRANSLATOR_LISTENER(m) \
86 inline void listen_ ## m (Stream_event *); \
87 /* Should be private */ \
88 static void _internal_declare_ ## m ();
90 #define DECLARE_ACKNOWLEDGER(x) public : void acknowledge_ ## x (Grob_info); protected:
91 #define DECLARE_END_ACKNOWLEDGER(x) public : void acknowledge_end_ ## x (Grob_info); protected:
93 enum Translator_precompute_index
95 START_TRANSLATION_TIMESTEP,
96 STOP_TRANSLATION_TIMESTEP,
99 TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
103 Translate music into grobs.
105 class Translator : public Smob<Translator>
108 int print_smob (SCM, scm_print_state *) const;
109 SCM mark_smob () const;
110 static const char type_p_name_[];
111 virtual ~Translator ();
116 Context *context () const { return daddy_context_; }
119 Translator (Translator const &);
121 SCM internal_get_property (SCM symbol) const;
123 virtual Output_def *get_output_def () const;
124 virtual Translator_group *get_daddy_translator ()const;
125 virtual Moment now_mom () const;
126 virtual bool must_be_last () const;
128 virtual void initialize ();
129 virtual void finalize ();
131 virtual void connect_to_context (Context *c);
132 virtual void disconnect_from_context (Context *c);
134 void stop_translation_timestep ();
135 void start_translation_timestep ();
136 void process_music ();
137 void process_acknowledged ();
139 Context *get_score_context () const;
140 Global_context *get_global_context () const;
142 DECLARE_CLASSNAME (Translator);
143 virtual Translator *clone () const = 0;
144 virtual void fetch_precomputable_methods (SCM methods[]) = 0;
145 virtual SCM get_listener_list () const = 0;
146 virtual SCM translator_description () const = 0;
147 virtual SCM get_acknowledger (SCM sym) = 0;
148 virtual SCM get_end_acknowledger (SCM sym) = 0;
150 protected: // should be private.
151 Context *daddy_context_;
152 void protect_event (SCM ev);
154 template <class T, void (T::*callback)(Stream_event *)>
155 static SCM trampoline (SCM target, SCM event)
157 T *t = unsmob<T> (target);
158 LY_ASSERT_SMOB (T, target, 1);
159 LY_ASSERT_SMOB (Stream_event, event, 2);
161 t->protect_event (event);
162 (t->*callback) (unsmob<Stream_event> (event));
163 return SCM_UNSPECIFIED;
166 template <class T, void (T::*mf)()>
168 method_find_base () { return Callback0_wrapper::make_smob<T, mf> (); }
170 // Fallback for non-overriden callbacks for which &T::x degrades to
172 template <void (Translator::*)()>
174 method_finder () { return SCM_UNDEFINED; }
176 // Overriden in Engraver.
177 template <class T, void (T::*callback)(Grob_info)>
179 ack_find_base () { return SCM_UNDEFINED; }
181 template <void (Translator::*)(Grob_info)>
183 ack_finder () { return SCM_UNDEFINED; }
185 virtual void derived_mark () const;
186 static SCM event_class_symbol (const char *ev_class);
187 SCM static_translator_description (const char *grobs,
191 const char *write) const;
193 friend class Translator_group;
196 void add_translator (Translator *trans);
198 Translator *get_translator (SCM s);
201 generic_get_acknowledger (SCM sym, SCM ack_hash);
203 Moment get_event_length (Stream_event *s, Moment now);
204 Moment get_event_length (Stream_event *s);
207 This helper is only meaningful inside listen_* methods.
209 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
210 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
212 #endif // TRANSLATOR_HH