]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.icc
Issue 1375/1: Let Translator constructor take a Context argument
[lilypond.git] / lily / include / translator.icc
1 /* -*- c++ -*-
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--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_ICC
21 #define TRANSLATOR_ICC
22
23 #include "callback.hh"
24 #include "std-vector.hh"
25 #include "engraver.hh"
26
27 /*
28   TODO: derive "foo-bar-interface" from Foo_bar classname.
29  */
30
31 /**
32    A macro to automate administration of translators.
33 */
34 #define ADD_THIS_TRANSLATOR(T)                                          \
35   SCM T::static_description_ = SCM_EOL;                                 \
36   static void _ ## T ## _adder ()                                       \
37   {                                                                     \
38     T::boot ();                                                         \
39     T *t = new T(0);                                                    \
40     T::static_description_ =                                            \
41       scm_permanent_object (t->static_translator_description ());       \
42     add_translator (t);                                                 \
43   }                                                                     \
44   SCM T::translator_description () const                                \
45   {                                                                     \
46     return static_description_;                                         \
47   }                                                                     \
48   ADD_GLOBAL_CTOR (_ ## T ## _adder);                                   \
49   /* end define */
50
51 #define DEFINE_TRANSLATOR_LISTENER_LIST(T) \
52   Protected_scm T::listener_list_ (SCM_EOL)
53
54 #define DEFINE_ACKNOWLEDGERS(classname) \
55   Drul_array<Protected_scm> classname::acknowledge_static_array_drul_;  \
56   SCM                                                                   \
57   classname::static_get_acknowledger (SCM sym, Direction start_end)     \
58   {                                                                     \
59     return generic_get_acknowledger                                     \
60       (sym, acknowledge_static_array_drul_[start_end]);                 \
61   }                                                                     \
62   /* end define */
63
64 #define DEFINE_TRANSLATOR_DOC(classname, desc, grobs, read, write)              \
65   SCM                                                                   \
66   classname::static_translator_description () const                     \
67   {                                                                     \
68     return Translator::static_translator_description (grobs, desc, listener_list_, read, write); \
69   }
70
71 #define ADD_TRANSLATOR(classname, desc, grobs, read, write)             \
72   IMPLEMENT_FETCH_PRECOMPUTABLE_METHODS (classname);                    \
73   DEFINE_ACKNOWLEDGERS(classname);                                      \
74   ADD_THIS_TRANSLATOR (classname);                                      \
75   DEFINE_TRANSLATOR_DOC(classname, desc, grobs, read, write);           \
76   DEFINE_TRANSLATOR_LISTENER_LIST(classname);
77
78 #define IMPLEMENT_FETCH_PRECOMPUTABLE_METHODS(T)                        \
79   void                                                                  \
80   T::fetch_precomputable_methods (SCM ptrs[])                           \
81   {                                                                     \
82     ptrs[START_TRANSLATION_TIMESTEP] =                                  \
83       method_finder <&T::start_translation_timestep> ();                \
84                                                                         \
85     ptrs[STOP_TRANSLATION_TIMESTEP] =                                   \
86       method_finder <&T::stop_translation_timestep> ();                 \
87                                                                         \
88     ptrs[PROCESS_MUSIC] =                                               \
89       method_finder <&T::process_music> ();                             \
90                                                                         \
91     ptrs[PROCESS_ACKNOWLEDGED] =                                        \
92       method_finder <&T::process_acknowledged> ();                      \
93   }
94
95 void add_acknowledger (SCM ptr,
96                        char const *func_name,
97                        SCM &ack_hash);
98
99 #define ADD_ACKNOWLEDGER_FOR(CLASS, NAME, GROB)                         \
100   add_acknowledger (method_finder<&CLASS::acknowledge_ ## NAME> (),     \
101                     #GROB, acknowledge_static_array_drul_[START])
102
103 #define ADD_ACKNOWLEDGER(CLASS, NAME) ADD_ACKNOWLEDGER_FOR (CLASS, NAME, NAME)
104
105 // ADD_END_ACKNOWLEDGER_FOR requires your NAME to actually be
106 // end_whatever if you are utilizing acknowledge_end_whatever as a
107 // different end acknowledger.
108 #define ADD_END_ACKNOWLEDGER_FOR(CLASS, NAME, GROB)                     \
109   add_acknowledger (method_finder<&CLASS::acknowledge_ ## NAME> (),     \
110                     #GROB, acknowledge_static_array_drul_[STOP])
111
112 #define ADD_END_ACKNOWLEDGER(CLASS, NAME)               \
113   ADD_END_ACKNOWLEDGER_FOR (CLASS, end_ ## NAME, NAME)
114
115 /*
116   Implement the method cl::listen_##m, and make it listen to stream
117   events of class m.
118  */
119 #define ADD_LISTENER_FOR(cl, m, ev)                                     \
120   listener_list_ = scm_acons                                            \
121     (event_class_symbol (#ev),                                          \
122      method_finder<&cl::listen_ ## m> (),                               \
123      listener_list_)
124
125 #define ADD_LISTENER(cl, m) ADD_LISTENER_FOR (cl, m, m)
126
127 #endif /* TRANSLATOR_ICC */