]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/translator.icc
7fc128fe72f68d732fa448f9a42d4755fc17e7c3
[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 "listener.hh"
24 #include "std-vector.hh"
25 #include "translator.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 *t = new T;                                                       \
39     T::static_description_ =                                            \
40       scm_permanent_object (t->static_translator_description ());       \
41     add_translator (t);                                                 \
42   }                                                                     \
43   SCM T::translator_description () const                                \
44   {                                                                     \
45     return static_description_;                                         \
46   }                                                                     \
47   ADD_GLOBAL_CTOR (_ ## T ## _adder); \
48   /* end define */
49
50 #define DEFINE_TRANSLATOR_LISTENER_LIST(T) \
51   Protected_scm T::listener_list_ (SCM_EOL);    \
52   /* end define */
53
54 #define DEFINE_ACKNOWLEDGERS(classname) \
55   Drul_array< vector<Acknowledge_information> > classname::acknowledge_static_array_drul_;      \
56   Translator::Grob_info_callback                                        \
57   classname::static_get_acknowledger (SCM sym)                          \
58   {                                                                     \
59     return generic_get_acknowledger (sym, &acknowledge_static_array_drul_[START]);      \
60   }                                                                     \
61   Translator::Grob_info_callback                                        \
62   classname::static_get_end_acknowledger (SCM sym)                      \
63   {                                                                     \
64     return generic_get_acknowledger (sym, &acknowledge_static_array_drul_[STOP]);       \
65   }                                                                     \
66   /* end define */
67
68 #define DEFINE_TRANSLATOR_DOC(classname, desc, grobs, read, write)              \
69   SCM                                                                   \
70   classname::static_translator_description () const                     \
71   {                                                                     \
72     return Translator::static_translator_description (grobs, desc, listener_list_, read, write); \
73   }
74
75 #define ADD_TRANSLATOR(classname, desc, grobs, read, write)             \
76   IMPLEMENT_FETCH_PRECOMPUTABLE_METHODS (classname);                    \
77   ADD_THIS_TRANSLATOR (classname);                                      \
78   DEFINE_TRANSLATOR_DOC(classname, desc, grobs, read, write)            \
79   DEFINE_ACKNOWLEDGERS(classname) \
80   DEFINE_TRANSLATOR_LISTENER_LIST(classname) \
81
82 #define IMPLEMENT_FETCH_PRECOMPUTABLE_METHODS(T)                        \
83   void                                                                  \
84   T::fetch_precomputable_methods (Translator::Callback ptrs[])          \
85   {                                                                     \
86     ptrs[START_TRANSLATION_TIMESTEP] =                                  \
87       (&T::start_translation_timestep                                   \
88        == &Translator::start_translation_timestep)                      \
89       ? 0                                                               \
90       : static_cast<Callback> (&T::start_translation_timestep);         \
91                                                                         \
92     ptrs[STOP_TRANSLATION_TIMESTEP] =                                   \
93       (& T::stop_translation_timestep == & Translator::stop_translation_timestep) \
94       ? 0                                                               \
95       : static_cast<Callback> (&T::stop_translation_timestep);          \
96                                                                         \
97     ptrs[PROCESS_MUSIC] =                                               \
98       (&T::process_music == &Translator::process_music)                 \
99       ? 0                                                               \
100       : static_cast<Callback> (&T::process_music);                      \
101                                                                         \
102     ptrs[PROCESS_ACKNOWLEDGED] =                                        \
103       (&T::process_acknowledged == &Translator::process_acknowledged)   \
104       ? 0                                                               \
105       : static_cast<Callback> (&T::process_acknowledged);               \
106   }
107
108 void add_acknowledger (Translator::Grob_info_callback ptr,
109                        char const *func_name,
110                        vector<Acknowledge_information> *ack_array);
111
112 Translator::Grob_info_callback
113 generic_get_acknowledger (SCM sym,
114                           vector<Acknowledge_information> const *ack_array);
115
116 #define ADD_ACKNOWLEDGER(CLASS, NAME)                                   \
117   void CLASS ## NAME ## _ack_adder ()                                   \
118   {                                                                     \
119     add_acknowledger (static_cast<Translator::Grob_info_callback> (&CLASS::acknowledge_ ## NAME), #NAME, &CLASS::acknowledge_static_array_drul_[START]); \
120   }                                                                     \
121   ADD_SCM_INIT_FUNC (CLASS ## NAME ## _ack_adder_initclass, CLASS ## NAME ## _ack_adder);
122
123 #define ADD_END_ACKNOWLEDGER(CLASS, NAME)                                       \
124   void CLASS ## NAME ## _end_ack_adder ()                                       \
125   {                                                                     \
126     add_acknowledger (static_cast<Translator::Grob_info_callback> (&CLASS::acknowledge_end_ ## NAME), #NAME, &CLASS::acknowledge_static_array_drul_[STOP]); \
127   }                                                                     \
128   ADD_SCM_INIT_FUNC (CLASS ## NAME ## _end_ack_adder_initclass, CLASS ## NAME ## _end_ack_adder);
129
130 /*
131   Implement the method cl::listen_##m, and make it listen to stream
132   events of class m.
133  */
134 #define IMPLEMENT_TRANSLATOR_LISTENER(cl, m)            \
135 void                                                    \
136 cl :: _internal_declare_ ## m ()                        \
137 {                                                       \
138   listener_list_ = scm_acons                                            \
139     (event_class_symbol (#m),                                           \
140      Callback_wrapper::make_smob<cl, Stream_event *, &cl::listen_ ## m> (), \
141      listener_list_);                                                   \
142 }                                                                       \
143                                                                         \
144 ADD_SCM_INIT_FUNC (cl ## _declare_event_ ## m, cl::_internal_declare_ ## m);
145
146 #endif /* TRANSLATOR_ICC */