]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
d4ea41f93418e854fe42df12d8eacb80c5bd1716
[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   virtual void fetch_precomputable_methods (SCM methods[]);             \
36   DECLARE_TRANSLATOR_CALLBACKS (NAME);                                  \
37   TRANSLATOR_INHERIT (Translator);                                      \
38   /* end #define */
39
40 #define TRANSLATOR_INHERIT(BASE)                                        \
41   using BASE::method_finder;                                            \
42   using BASE::ack_finder;
43
44 #define DECLARE_TRANSLATOR_CALLBACKS(NAME)                              \
45   template <void (NAME::*mf)()>                                         \
46   static SCM method_finder ()                                           \
47   {                                                                     \
48     return Callback0_wrapper::make_smob<NAME, mf> ();                   \
49   }                                                                     \
50   template <void (NAME::*mf)(Stream_event *)>                           \
51   static SCM method_finder ()                                           \
52   {                                                                     \
53     return Callback_wrapper::make_smob<trampoline<NAME, mf> > ();       \
54   }                                                                     \
55   template <void (NAME::*callback)(Grob_info)>                          \
56   static SCM ack_finder () { return ack_find_base<NAME, callback> (); } \
57   /* end #define */
58
59 /*
60   Each translator class has a static alist of event class symbols
61   mapping to callbacks that are called with a translator instance and
62   a stream event when an event of the appropriate event class is
63   announced in a context.
64 */
65
66 #define TRANSLATOR_DECLARATIONS(NAME)                                   \
67   public:                                                               \
68   TRANSLATOR_FAMILY_DECLARATIONS (NAME);                                \
69   static Drul_array<Protected_scm> acknowledge_static_array_drul_;      \
70   static SCM static_description_;                                       \
71   static Protected_scm listener_list_;                                  \
72   static SCM static_get_acknowledger (SCM sym, Direction start_end);    \
73   virtual SCM get_acknowledger (SCM sym, Direction start_end)           \
74   {                                                                     \
75     return static_get_acknowledger (sym, start_end);                    \
76   }                                                                     \
77 public:                                                                 \
78   NAME ();                                                              \
79   static void boot ();                                                  \
80   virtual SCM static_translator_description () const;                   \
81   virtual SCM translator_description () const;                          \
82   virtual SCM get_listener_list () const                                \
83   {                                                                     \
84     return listener_list_;                                              \
85   }                                                                     \
86   /* end #define */
87
88 enum Translator_precompute_index
89 {
90   START_TRANSLATION_TIMESTEP,
91   STOP_TRANSLATION_TIMESTEP,
92   PROCESS_MUSIC,
93   PROCESS_ACKNOWLEDGED,
94   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
95 };
96
97 /*
98   Translate music into grobs.
99 */
100 class Translator : public Smob<Translator>
101 {
102 public:
103   int print_smob (SCM, scm_print_state *) const;
104   SCM mark_smob () const;
105   static const char * const type_p_name_;
106   virtual ~Translator ();
107 private:
108   void init ();
109
110 public:
111   Context *context () const { return daddy_context_; }
112
113   Translator ();
114   Translator (Translator const &);
115
116   SCM internal_get_property (SCM symbol) const;
117
118   virtual Output_def *get_output_def () const;
119   virtual Translator_group *get_daddy_translator ()const;
120   virtual Moment now_mom () const;
121   virtual bool must_be_last () const;
122
123   virtual void initialize ();
124   virtual void finalize ();
125
126   virtual void connect_to_context (Context *c);
127   virtual void disconnect_from_context (Context *c);
128
129   void stop_translation_timestep ();
130   void start_translation_timestep ();
131   void process_music ();
132   void process_acknowledged ();
133
134   Context *get_score_context () const;
135   Global_context *get_global_context () const;
136
137   DECLARE_CLASSNAME (Translator);
138   virtual Translator *clone () const = 0;
139   virtual void fetch_precomputable_methods (SCM methods[]) = 0;
140   virtual SCM get_listener_list () const = 0;
141   virtual SCM translator_description () const = 0;
142   virtual SCM get_acknowledger (SCM sym, Direction start_end) = 0;
143
144 protected:                      // should be private.
145   Context *daddy_context_;
146   void protect_event (SCM ev);
147
148   template <class T, void (T::*callback)(Stream_event *)>
149   static SCM trampoline (SCM target, SCM event)
150   {
151     T *t = unsmob<T> (target);
152     LY_ASSERT_SMOB (T, target, 1);
153     LY_ASSERT_SMOB (Stream_event, event, 2);
154
155     t->protect_event (event);
156     (t->*callback) (unsmob<Stream_event> (event));
157     return SCM_UNSPECIFIED;
158   }
159
160   // Fallback for non-overriden callbacks for which &T::x degrades to
161   // &Translator::x
162   template <void (Translator::*)()>
163   static SCM
164   method_finder () { return SCM_UNDEFINED; }
165
166   // Overriden in Engraver.
167   template <class T, void (T::*callback)(Grob_info)>
168   static SCM
169   ack_find_base () { return SCM_UNDEFINED; }
170
171   template <void (Translator::*)(Grob_info)>
172   static SCM
173   ack_finder () { return SCM_UNDEFINED; }
174
175   virtual void derived_mark () const;
176   static SCM event_class_symbol (const char *ev_class);
177   SCM static_translator_description (const char *grobs,
178                                      const char *desc,
179                                      SCM listener_list,
180                                      const char *read,
181                                      const char *write) const;
182
183   friend class Translator_group;
184 };
185
186 void add_translator (Translator *trans);
187
188 Translator *get_translator (SCM s);
189
190 SCM
191 generic_get_acknowledger (SCM sym, SCM ack_hash);
192
193 Moment get_event_length (Stream_event *s, Moment now);
194 Moment get_event_length (Stream_event *s);
195
196 /*
197   This helper is only meaningful inside listen_* methods.
198 */
199 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
200 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
201
202 #endif // TRANSLATOR_HH