]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
Doc-it: update translation
[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 #define TRANSLATOR_FAMILY_DECLARATIONS(NAME)                            \
32   public:                                                               \
33   VIRTUAL_COPY_CONSTRUCTOR (Translator, NAME);                          \
34   static Drul_array<vector<Acknowledge_information> > acknowledge_static_array_drul_; \
35   virtual void fetch_precomputable_methods (Callback methods[]);        \
36   static Grob_info_callback static_get_acknowledger (SCM sym);          \
37   static Grob_info_callback static_get_end_acknowledger(SCM);           \
38   virtual Grob_info_callback get_acknowledger (SCM sym)                 \
39   {                                                                     \
40     return static_get_acknowledger (sym);                               \
41   }                                                                     \
42   virtual Grob_info_callback get_end_acknowledger (SCM sym)             \
43   {                                                                     \
44     return static_get_end_acknowledger (sym);                           \
45   }                                                                     \
46   /* end #define */
47
48 /*
49   Each translator class has a static alist of event class symbols
50   mapping to callbacks that are called with a translator instance and
51   a stream event when an event of the appropriate event class is
52   announced in a context.
53 */
54
55 #define TRANSLATOR_DECLARATIONS(NAME)                                   \
56   TRANSLATOR_FAMILY_DECLARATIONS(NAME)                                  \
57   static SCM static_description_;                                       \
58   static Protected_scm listener_list_;                                  \
59 public:                                                                 \
60   NAME ();                                                              \
61   virtual SCM static_translator_description () const;                   \
62   virtual SCM translator_description () const;                          \
63   virtual SCM get_listener_list () const                                \
64   {                                                                     \
65     return listener_list_;                                              \
66   }                                                                     \
67   /* end #define */
68
69 #define DECLARE_TRANSLATOR_LISTENER(m)                  \
70 public:                                                 \
71 inline void listen_ ## m (Stream_event *);              \
72 /* Should be private */                                 \
73 static void _internal_declare_ ## m ();
74
75 #define DECLARE_ACKNOWLEDGER(x) public : void acknowledge_ ## x (Grob_info); protected:
76 #define DECLARE_END_ACKNOWLEDGER(x) public : void acknowledge_end_ ## x (Grob_info); protected:
77
78 enum Translator_precompute_index
79 {
80   START_TRANSLATION_TIMESTEP,
81   STOP_TRANSLATION_TIMESTEP,
82   PROCESS_MUSIC,
83   PROCESS_ACKNOWLEDGED,
84   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
85 };
86
87 /*
88   Translate music into grobs.
89 */
90 class Translator : public Smob<Translator>
91 {
92 public:
93   // We don't make Grob_info_callback specific to Engraver since we
94   // otherwise get into a circular mess with regard to the definitions
95   // as the timing of Engraver is exercised from within Translator
96   typedef void (Translator::*Grob_info_callback) (Grob_info);
97   typedef void (Translator::*Callback) (void);
98   int print_smob (SCM, scm_print_state *) const;
99   SCM mark_smob () const;
100   static const char type_p_name_[];
101   virtual ~Translator ();
102 private:
103   void init ();
104
105 public:
106   Context *context () const { return daddy_context_; }
107
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   TRANSLATOR_DECLARATIONS (Translator);
132
133 protected:                      // should be private.
134   Context *daddy_context_;
135   void protect_event (SCM ev);
136
137   template <class T, void (T::*callback)(Stream_event *)>
138   static SCM trampoline (SCM target, SCM event)
139   {
140     T *t = unsmob<T> (target);
141     LY_ASSERT_SMOB (T, target, 1);
142     LY_ASSERT_SMOB (Stream_event, event, 2);
143
144     t->protect_event (event);
145     (t->*callback) (unsmob<Stream_event> (event));
146     return SCM_UNSPECIFIED;
147   }
148
149   virtual void derived_mark () const;
150   static SCM event_class_symbol (const char *ev_class);
151   SCM static_translator_description (const char *grobs,
152                                      const char *desc,
153                                      SCM listener_list,
154                                      const char *read,
155                                      const char *write) const;
156
157   friend class Translator_group;
158 };
159
160 struct Acknowledge_information
161 {
162   SCM symbol_;
163   Translator::Grob_info_callback function_;
164
165   Acknowledge_information ()
166   {
167     symbol_ = SCM_EOL;
168     function_ = 0;
169   }
170 };
171
172
173 void add_translator (Translator *trans);
174
175 Translator *get_translator (SCM s);
176 Moment get_event_length (Stream_event *s, Moment now);
177 Moment get_event_length (Stream_event *s);
178
179 /*
180   This helper is only meaningful inside listen_* methods.
181 */
182 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
183 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
184
185 #endif // TRANSLATOR_HH