]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
ea8b9782ae7bbceb3a21c0a3c0dfec88606bd40c
[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 (Callback 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::ack_finder;
53
54 #define DECLARE_TRANSLATOR_CALLBACKS(NAME)                              \
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 SCM static_description_;                                       \
70   static Protected_scm listener_list_;                                  \
71 public:                                                                 \
72   NAME ();                                                              \
73   virtual SCM static_translator_description () const;                   \
74   virtual SCM translator_description () const;                          \
75   virtual SCM get_listener_list () const                                \
76   {                                                                     \
77     return listener_list_;                                              \
78   }                                                                     \
79   /* end #define */
80
81 #define DECLARE_TRANSLATOR_LISTENER(m)                  \
82 public:                                                 \
83 inline void listen_ ## m (Stream_event *);              \
84 /* Should be private */                                 \
85 static void _internal_declare_ ## m ();
86
87 #define DECLARE_ACKNOWLEDGER(x) public : void acknowledge_ ## x (Grob_info); protected:
88 #define DECLARE_END_ACKNOWLEDGER(x) public : void acknowledge_end_ ## x (Grob_info); protected:
89
90 enum Translator_precompute_index
91 {
92   START_TRANSLATION_TIMESTEP,
93   STOP_TRANSLATION_TIMESTEP,
94   PROCESS_MUSIC,
95   PROCESS_ACKNOWLEDGED,
96   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
97 };
98
99 /*
100   Translate music into grobs.
101 */
102 class Translator : public Smob<Translator>
103 {
104 public:
105   typedef void (Translator::*Callback) (void);
106   int print_smob (SCM, scm_print_state *) const;
107   SCM mark_smob () const;
108   static const char type_p_name_[];
109   virtual ~Translator ();
110 private:
111   void init ();
112
113 public:
114   Context *context () const { return daddy_context_; }
115
116   Translator ();
117   Translator (Translator const &);
118
119   SCM internal_get_property (SCM symbol) const;
120
121   virtual Output_def *get_output_def () const;
122   virtual Translator_group *get_daddy_translator ()const;
123   virtual Moment now_mom () const;
124   virtual bool must_be_last () const;
125
126   virtual void initialize ();
127   virtual void finalize ();
128
129   virtual void connect_to_context (Context *c);
130   virtual void disconnect_from_context (Context *c);
131
132   void stop_translation_timestep ();
133   void start_translation_timestep ();
134   void process_music ();
135   void process_acknowledged ();
136
137   Context *get_score_context () const;
138   Global_context *get_global_context () const;
139
140   DECLARE_CLASSNAME (Translator);
141   virtual Translator *clone () const = 0;
142   virtual void fetch_precomputable_methods (Callback methods[]) = 0;
143   virtual SCM get_listener_list () const = 0;
144   virtual SCM translator_description () const = 0;
145   virtual SCM get_acknowledger (SCM sym) = 0;
146   virtual SCM get_end_acknowledger (SCM sym) = 0;
147
148 protected:                      // should be private.
149   Context *daddy_context_;
150   void protect_event (SCM ev);
151
152   template <class T, void (T::*callback)(Stream_event *)>
153   static SCM trampoline (SCM target, SCM event)
154   {
155     T *t = unsmob<T> (target);
156     LY_ASSERT_SMOB (T, target, 1);
157     LY_ASSERT_SMOB (Stream_event, event, 2);
158
159     t->protect_event (event);
160     (t->*callback) (unsmob<Stream_event> (event));
161     return SCM_UNSPECIFIED;
162   }
163
164   // Overriden in Engraver.
165   template <class T, void (T::*callback)(Grob_info)>
166   static SCM
167   ack_find_base () { return SCM_UNDEFINED; }
168
169   template <void (Translator::*)(Grob_info)>
170   static SCM
171   ack_finder () { return SCM_UNDEFINED; }
172
173   virtual void derived_mark () const;
174   static SCM event_class_symbol (const char *ev_class);
175   SCM static_translator_description (const char *grobs,
176                                      const char *desc,
177                                      SCM listener_list,
178                                      const char *read,
179                                      const char *write) const;
180
181   friend class Translator_group;
182 };
183
184 struct Acknowledge_information
185 {
186   SCM symbol_;
187   SCM function_;
188
189   Acknowledge_information ()
190   {
191     symbol_ = SCM_EOL;
192     function_ = SCM_UNDEFINED;
193   }
194 };
195
196
197 void add_translator (Translator *trans);
198
199 Translator *get_translator (SCM s);
200 Moment get_event_length (Stream_event *s, Moment now);
201 Moment get_event_length (Stream_event *s);
202
203 /*
204   This helper is only meaningful inside listen_* methods.
205 */
206 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
207 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
208
209 #endif // TRANSLATOR_HH