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