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