]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[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 "stream-event.hh"
30 #include "std-vector.hh"
31 #include "protected-scm.hh"
32
33 #define TRANSLATOR_FAMILY_DECLARATIONS(NAME)                            \
34   public:                                                               \
35   VIRTUAL_COPY_CONSTRUCTOR (Translator, NAME);                          \
36   virtual void fetch_precomputable_methods (SCM methods[]);             \
37   DECLARE_TRANSLATOR_CALLBACKS (NAME);                                  \
38   TRANSLATOR_INHERIT (Translator);                                      \
39   /* end #define */
40
41 #define TRANSLATOR_INHERIT(BASE)                                        \
42   using BASE::method_finder;                                            \
43   using BASE::ack_finder;
44
45 #define DECLARE_TRANSLATOR_CALLBACKS(NAME)                              \
46   template <void (NAME::*mf)()>                                         \
47   static SCM method_finder ()                                           \
48   {                                                                     \
49     return Callback0_wrapper::make_smob<NAME, mf> ();                   \
50   }                                                                     \
51   template <void (NAME::*mf)(Stream_event *)>                           \
52   static SCM method_finder ()                                           \
53   {                                                                     \
54     return Callback_wrapper::make_smob<trampoline<NAME, mf> > ();       \
55   }                                                                     \
56   template <void (NAME::*callback)(Grob_info)>                          \
57   static SCM ack_finder () {                                            \
58     return Callback2_wrapper::make_smob<ack_trampoline <NAME, callback> > (); \
59   }                                                                     \
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 Drul_array<Protected_scm> acknowledge_static_array_drul_;      \
73   static SCM static_description_;                                       \
74   static Protected_scm listener_list_;                                  \
75   static SCM static_get_acknowledger (SCM sym, Direction start_end);    \
76   virtual SCM get_acknowledger (SCM sym, Direction start_end)           \
77   {                                                                     \
78     return static_get_acknowledger (sym, start_end);                    \
79   }                                                                     \
80 public:                                                                 \
81   NAME ();                                                              \
82   static void boot ();                                                  \
83   virtual SCM static_translator_description () const;                   \
84   virtual SCM translator_description () const;                          \
85   virtual SCM get_listener_list () const                                \
86   {                                                                     \
87     return listener_list_;                                              \
88   }                                                                     \
89   /* end #define */
90
91 enum Translator_precompute_index
92 {
93   START_TRANSLATION_TIMESTEP,
94   STOP_TRANSLATION_TIMESTEP,
95   PROCESS_MUSIC,
96   PROCESS_ACKNOWLEDGED,
97   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
98 };
99
100 /*
101   Translate music into grobs.
102 */
103 class Translator : public Smob<Translator>
104 {
105 public:
106   int print_smob (SCM, scm_print_state *) const;
107   SCM mark_smob () const;
108   static const char * const 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 (SCM methods[]) = 0;
143   virtual SCM get_listener_list () const = 0;
144   virtual SCM translator_description () const = 0;
145   virtual SCM get_acknowledger (SCM sym, Direction start_end) = 0;
146
147 protected:                      // should be private.
148   Context *daddy_context_;
149   void protect_event (SCM ev);
150
151   template <class T, void (T::*callback)(Stream_event *)>
152   static SCM trampoline (SCM target, SCM event)
153   {
154     T *t = unsmob<T> (target);
155     LY_ASSERT_SMOB (T, target, 1);
156     LY_ASSERT_SMOB (Stream_event, event, 2);
157
158     t->protect_event (event);
159     (t->*callback) (unsmob<Stream_event> (event));
160     return SCM_UNSPECIFIED;
161   }
162
163   // Fallback for non-overriden callbacks for which &T::x degrades to
164   // &Translator::x
165   template <void (Translator::*)()>
166   static SCM
167   method_finder () { return SCM_UNDEFINED; }
168
169   // Overriden in Engraver.  Don't instantiate.
170   template <class T, void (T::*)(Grob_info)>
171   static SCM ack_trampoline (SCM, SCM, SCM);
172
173   // Overriden in Engraver.  Don't instantiate.
174   template <void (Translator::*)(Grob_info)>
175   static SCM ack_finder ();
176
177   virtual void derived_mark () const;
178   static SCM event_class_symbol (const char *ev_class);
179   SCM static_translator_description (const char *grobs,
180                                      const char *desc,
181                                      SCM listener_list,
182                                      const char *read,
183                                      const char *write) const;
184
185   friend class Translator_group;
186 };
187
188 void add_translator (Translator *trans);
189
190 Translator *get_translator (SCM s);
191
192 SCM
193 generic_get_acknowledger (SCM sym, SCM ack_hash);
194
195 Moment get_event_length (Stream_event *s, Moment now);
196 Moment get_event_length (Stream_event *s);
197
198 /*
199   This helper is only meaningful inside listen_* methods.
200 */
201 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
202 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
203
204 #endif // TRANSLATOR_HH