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