]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.hh
Issue 4878: Make type_p_name_ always char pointer
[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   /* 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   public:                                                               \
57   TRANSLATOR_FAMILY_DECLARATIONS (NAME);                                \
58   static Drul_array<Protected_scm> acknowledge_static_array_drul_;      \
59   static SCM static_description_;                                       \
60   static Protected_scm listener_list_;                                  \
61   static SCM static_get_acknowledger (SCM sym, Direction start_end);    \
62   virtual SCM get_acknowledger (SCM sym, Direction start_end)           \
63   {                                                                     \
64     return static_get_acknowledger (sym, start_end);                    \
65   }                                                                     \
66 public:                                                                 \
67   NAME ();                                                              \
68   static void boot ();                                                  \
69   virtual SCM static_translator_description () const;                   \
70   virtual SCM translator_description () const;                          \
71   virtual SCM get_listener_list () const                                \
72   {                                                                     \
73     return listener_list_;                                              \
74   }                                                                     \
75   /* end #define */
76
77 enum Translator_precompute_index
78 {
79   START_TRANSLATION_TIMESTEP,
80   STOP_TRANSLATION_TIMESTEP,
81   PROCESS_MUSIC,
82   PROCESS_ACKNOWLEDGED,
83   TRANSLATOR_METHOD_PRECOMPUTE_COUNT,
84 };
85
86 /*
87   Translate music into grobs.
88 */
89 class Translator : public Smob<Translator>
90 {
91 public:
92   int print_smob (SCM, scm_print_state *) const;
93   SCM mark_smob () const;
94   static const char * const type_p_name_;
95   virtual ~Translator ();
96 private:
97   void init ();
98
99 public:
100   Context *context () const { return daddy_context_; }
101
102   Translator ();
103   Translator (Translator const &);
104
105   SCM internal_get_property (SCM symbol) const;
106
107   virtual Output_def *get_output_def () const;
108   virtual Translator_group *get_daddy_translator ()const;
109   virtual Moment now_mom () const;
110   virtual bool must_be_last () const;
111
112   virtual void initialize ();
113   virtual void finalize ();
114
115   virtual void connect_to_context (Context *c);
116   virtual void disconnect_from_context (Context *c);
117
118   void stop_translation_timestep ();
119   void start_translation_timestep ();
120   void process_music ();
121   void process_acknowledged ();
122
123   Context *get_score_context () const;
124   Global_context *get_global_context () const;
125
126   DECLARE_CLASSNAME (Translator);
127   virtual Translator *clone () const = 0;
128   virtual void fetch_precomputable_methods (SCM methods[]) = 0;
129   virtual SCM get_listener_list () const = 0;
130   virtual SCM translator_description () const = 0;
131   virtual SCM get_acknowledger (SCM sym, Direction start_end) = 0;
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   template <class T, void (T::*mf)()>
150   static SCM
151   method_find_base () { return Callback0_wrapper::make_smob<T, mf> (); }
152
153   // Fallback for non-overriden callbacks for which &T::x degrades to
154   // &Translator::x
155   template <void (Translator::*)()>
156   static SCM
157   method_finder () { return SCM_UNDEFINED; }
158
159   virtual void derived_mark () const;
160   static SCM event_class_symbol (const char *ev_class);
161   SCM static_translator_description (const char *grobs,
162                                      const char *desc,
163                                      SCM listener_list,
164                                      const char *read,
165                                      const char *write) const;
166
167   friend class Translator_group;
168 };
169
170 void add_translator (Translator *trans);
171
172 Translator *get_translator (SCM s);
173
174 SCM
175 generic_get_acknowledger (SCM sym, SCM ack_hash);
176
177 Moment get_event_length (Stream_event *s, Moment now);
178 Moment get_event_length (Stream_event *s);
179
180 /*
181   This helper is only meaningful inside listen_* methods.
182 */
183 extern bool internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function);
184 #define ASSIGN_EVENT_ONCE(o,n) internal_event_assignment (&o, n, __FUNCTION__)
185
186 #endif // TRANSLATOR_HH