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