]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
ad8765fd54c1bf902b83c14f12d0ef2c5795c6ef
[lilypond.git] / lily / include / translator.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #ifndef TRANSLATOR_HH
21 #define TRANSLATOR_HH
22
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
28 #include "smobs.hh"
29 #include "std-vector.hh"
30 #include "protected-scm.hh"
31
32 #define TRANSLATOR_FAMILY_DECLARATIONS(NAME)                            \
33   public:                                                               \
34   VIRTUAL_COPY_CONSTRUCTOR (Translator, NAME);                          \
35   static Drul_array<vector<Acknowledge_information> > acknowledge_static_array_drul_; \
36   virtual void fetch_precomputable_methods (SCM methods[]);             \
37   DECLARE_TRANSLATOR_CALLBACKS (NAME);                                  \
38   TRANSLATOR_INHERIT (Translator)                                       \
39   static SCM static_get_acknowledger (SCM sym);                         \
40   static SCM static_get_end_acknowledger(SCM);                          \
41   virtual SCM get_acknowledger (SCM sym)                                \
42   {                                                                     \
43     return static_get_acknowledger (sym);                               \
44   }                                                                     \
45   virtual SCM get_end_acknowledger (SCM sym)                            \
46   {                                                                     \
47     return static_get_end_acknowledger (sym);                           \
48   }                                                                     \
49   /* end #define */
50
51 #define TRANSLATOR_INHERIT(BASE)                                        \
52   using BASE::method_finder;                                            \
53   using BASE::ack_finder;
54
55 #define DECLARE_TRANSLATOR_CALLBACKS(NAME)                              \
56   template <void (NAME::*mf)()>                                         \
57   static SCM method_finder () { return method_find_base<NAME, mf> (); } \
58   template <void (NAME::*callback)(Grob_info)>                          \
59   static SCM ack_finder () { return ack_find_base<NAME, callback> (); } \
60   /* end #define */
61
62 /*
63   Each translator class has a static alist of event class symbols
64   mapping to callbacks that are called with a translator instance and
65   a stream event when an event of the appropriate event class is
66   announced in a context.
67 */
68
69 #define TRANSLATOR_DECLARATIONS(NAME)                                   \
70   public:                                                               \
71   TRANSLATOR_FAMILY_DECLARATIONS (NAME);                                \
72   static SCM static_description_;                                       \
73   static Protected_scm listener_list_;                                  \
74 public:                                                                 \
75   NAME ();                                                              \
76   virtual SCM static_translator_description () const;                   \
77   virtual SCM translator_description () const;                          \
78   virtual SCM get_listener_list () const                                \
79   {                                                                     \
80     return listener_list_;                                              \
81   }                                                                     \
82   /* end #define */
83
84 #define DECLARE_TRANSLATOR_LISTENER(m)                  \
85 public:                                                 \
86 inline void listen_ ## m (Stream_event *);              \
87 /* Should be private */                                 \
88 static void _internal_declare_ ## m ();
89
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:
92
93 enum Translator_precompute_index
94 {
95   START_TRANSLATION_TIMESTEP,
96   STOP_TRANSLATION_TIMESTEP,
97   PROCESS_MUSIC,
98   PROCESS_ACKNOWLEDGED,
99   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
100 };
101
102 /*
103   Translate music into grobs.
104 */
105 class Translator : public Smob<Translator>
106 {
107 public:
108   int print_smob (SCM, scm_print_state *) const;
109   SCM mark_smob () const;
110   static const char type_p_name_[];
111   virtual ~Translator ();
112 private:
113   void init ();
114
115 public:
116   Context *context () const { return daddy_context_; }
117
118   Translator ();
119   Translator (Translator const &);
120
121   SCM internal_get_property (SCM symbol) const;
122
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;
127
128   virtual void initialize ();
129   virtual void finalize ();
130
131   virtual void connect_to_context (Context *c);
132   virtual void disconnect_from_context (Context *c);
133
134   void stop_translation_timestep ();
135   void start_translation_timestep ();
136   void process_music ();
137   void process_acknowledged ();
138
139   Context *get_score_context () const;
140   Global_context *get_global_context () const;
141
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;
149
150 protected:                      // should be private.
151   Context *daddy_context_;
152   void protect_event (SCM ev);
153
154   template <class T, void (T::*callback)(Stream_event *)>
155   static SCM trampoline (SCM target, SCM event)
156   {
157     T *t = unsmob<T> (target);
158     LY_ASSERT_SMOB (T, target, 1);
159     LY_ASSERT_SMOB (Stream_event, event, 2);
160
161     t->protect_event (event);
162     (t->*callback) (unsmob<Stream_event> (event));
163     return SCM_UNSPECIFIED;
164   }
165
166   template <class T, void (T::*mf)()>
167   static SCM
168   method_find_base () { return Callback0_wrapper::make_smob<T, mf> (); }
169
170   // Fallback for non-overriden callbacks for which &T::x degrades to
171   // &Translator::x
172   template <void (Translator::*)()>
173   static SCM
174   method_finder () { return SCM_UNDEFINED; }
175
176   // Overriden in Engraver.
177   template <class T, void (T::*callback)(Grob_info)>
178   static SCM
179   ack_find_base () { return SCM_UNDEFINED; }
180
181   template <void (Translator::*)(Grob_info)>
182   static SCM
183   ack_finder () { return SCM_UNDEFINED; }
184
185   virtual void derived_mark () const;
186   static SCM event_class_symbol (const char *ev_class);
187   SCM static_translator_description (const char *grobs,
188                                      const char *desc,
189                                      SCM listener_list,
190                                      const char *read,
191                                      const char *write) const;
192
193   friend class Translator_group;
194 };
195
196 struct Acknowledge_information
197 {
198   SCM symbol_;
199   SCM function_;
200
201   Acknowledge_information ()
202   {
203     symbol_ = SCM_EOL;
204     function_ = SCM_UNDEFINED;
205   }
206 };
207
208
209 void add_translator (Translator *trans);
210
211 Translator *get_translator (SCM s);
212 Moment get_event_length (Stream_event *s, Moment now);
213 Moment get_event_length (Stream_event *s);
214
215 /*
216   This helper is only meaningful inside listen_* methods.
217 */
218 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
219 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
220
221 #endif // TRANSLATOR_HH