]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
Issue 4351/2: Rename Translator_void_method_ptr to Translator::Callback
[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 "input.hh"             // for error reporting
27 #include "smobs.hh"
28 #include "std-vector.hh"
29 #include "protected-scm.hh"
30
31 /*
32   Each translator class has a static list of listener records. Each
33   record makes one explains how to register one of the class's stream event
34   listeners to a context.
35 */
36 typedef struct translator_listener_record
37 {
38   Listener (*get_listener_) (void *, SCM event_class);
39   SCM event_class_;
40   struct translator_listener_record *next_;
41
42   translator_listener_record ()
43   {
44     next_ = 0;
45     event_class_ = SCM_EOL;
46     get_listener_ = 0;
47   }
48
49 } translator_listener_record;
50
51 #define TRANSLATOR_DECLARATIONS_NO_LISTENER(NAME)                       \
52   public:                                                               \
53   NAME ();                                                              \
54   VIRTUAL_COPY_CONSTRUCTOR (Translator, NAME);                          \
55   static SCM static_description_;                                       \
56   static Drul_array<vector<Acknowledge_information> > acknowledge_static_array_drul_; \
57   virtual void fetch_precomputable_methods (Callback methods[]); \
58   virtual SCM static_translator_description () const;                   \
59   virtual SCM translator_description () const;                          \
60   static Grob_info_callback static_get_acknowledger (SCM sym);          \
61   static Grob_info_callback static_get_end_acknowledger(SCM);           \
62   virtual Grob_info_callback get_acknowledger (SCM sym)                 \
63   {                                                                     \
64     return static_get_acknowledger (sym);                               \
65   }                                                                     \
66   virtual Grob_info_callback get_end_acknowledger (SCM sym)             \
67   {                                                                     \
68     return static_get_end_acknowledger (sym);                           \
69   } \
70   /* end #define */
71
72 #define TRANSLATOR_DECLARATIONS(NAME)                                   \
73   TRANSLATOR_DECLARATIONS_NO_LISTENER(NAME)                             \
74 private:                                                                \
75   static translator_listener_record *listener_list_;                    \
76 public:                                                                 \
77   virtual translator_listener_record *get_listener_list () const        \
78   {                                                                     \
79     return listener_list_;                                              \
80   }                                                                     \
81   /* end #define */
82
83 #define DECLARE_TRANSLATOR_LISTENER(m)                  \
84 public:                                                 \
85 inline void listen_ ## m (Stream_event *);              \
86 /* Should be private */                                 \
87 static void _internal_declare_ ## m ();                 \
88 private:                                                \
89  static Listener _get_ ## m ## _listener (void *, SCM); \
90 DECLARE_LISTENER (_listen_scm_ ## m);
91
92 #define DECLARE_ACKNOWLEDGER(x) public : void acknowledge_ ## x (Grob_info); protected:
93 #define DECLARE_END_ACKNOWLEDGER(x) public : void acknowledge_end_ ## x (Grob_info); protected:
94
95 enum Translator_precompute_index
96 {
97   START_TRANSLATION_TIMESTEP,
98   STOP_TRANSLATION_TIMESTEP,
99   PROCESS_MUSIC,
100   PROCESS_ACKNOWLEDGED,
101   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
102 };
103
104 /*
105   Translate music into grobs.
106 */
107 class Translator : public Smob<Translator>
108 {
109 public:
110   // We don't make Grob_info_callback specific to Engraver since we
111   // otherwise get into a circular mess with regard to the definitions
112   // as the timing of Engraver is exercised from within Translator
113   typedef void (Translator::*Grob_info_callback) (Grob_info);
114   typedef void (Translator::*Callback) (void);
115   int print_smob (SCM, scm_print_state *);
116   SCM mark_smob ();
117   static const char type_p_name_[];
118   virtual ~Translator ();
119 private:
120   void init ();
121
122 public:
123   Context *context () const { return daddy_context_; }
124
125   Translator (Translator const &);
126
127   SCM internal_get_property (SCM symbol) const;
128
129   virtual Output_def *get_output_def () const;
130   virtual Translator_group *get_daddy_translator ()const;
131   virtual Moment now_mom () const;
132   virtual bool must_be_last () const;
133
134   virtual void initialize ();
135   virtual void finalize ();
136
137   virtual void connect_to_context (Context *c);
138   virtual void disconnect_from_context (Context *c);
139
140   void stop_translation_timestep ();
141   void start_translation_timestep ();
142   void process_music ();
143   void process_acknowledged ();
144
145   Context *get_score_context () const;
146   Global_context *get_global_context () const;
147
148   TRANSLATOR_DECLARATIONS (Translator);
149
150 protected:                      // should be private.
151   Context *daddy_context_;
152   void protect_event (SCM ev);
153   virtual void derived_mark () const;
154   static void add_translator_listener (translator_listener_record **listener_list,
155                                        translator_listener_record *r,
156                                        Listener (*get_listener) (void *, SCM),
157                                        const char *ev_class);
158   SCM static_translator_description (const char *grobs,
159                                      const char *desc,
160                                      translator_listener_record *listener_list,
161                                      const char *read,
162                                      const char *write) const;
163
164   friend class Translator_group;
165 };
166
167 struct Acknowledge_information
168 {
169   SCM symbol_;
170   Translator::Grob_info_callback function_;
171
172   Acknowledge_information ()
173   {
174     symbol_ = SCM_EOL;
175     function_ = 0;
176   }
177 };
178
179
180 void add_translator (Translator *trans);
181
182 Translator *get_translator (SCM s);
183 Moment get_event_length (Stream_event *s, Moment now);
184 Moment get_event_length (Stream_event *s);
185
186 /*
187   This helper is only meaningful inside listen_* methods.
188 */
189 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
190 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
191
192 #endif // TRANSLATOR_HH