OLD | NEW |
1 /* | 1 /* |
2 This file is part of LilyPond, the GNU music typesetter. | 2 This file is part of LilyPond, the GNU music typesetter. |
3 | 3 |
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl> | 4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl> |
5 | 5 |
6 LilyPond is free software: you can redistribute it and/or modify | 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 | 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 | 8 the Free Software Foundation, either version 3 of the License, or |
9 (at your option) any later version. | 9 (at your option) any later version. |
10 | 10 |
11 LilyPond is distributed in the hope that it will be useful, | 11 LilyPond is distributed in the hope that it will be useful, |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 GNU General Public License for more details. | 14 GNU General Public License for more details. |
15 | 15 |
16 You should have received a copy of the GNU General Public License | 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/>. | 17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>. |
18 */ | 18 */ |
19 | 19 |
20 #include "translator.hh" | 20 #include "translator.hh" |
21 | 21 |
22 #include "international.hh" | 22 #include "international.hh" |
23 #include "scm-hash.hh" | 23 #include "scm-hash.hh" |
24 #include "warn.hh" | 24 #include "warn.hh" |
25 #include "protected-scm.hh" | 25 #include "protected-scm.hh" |
26 | 26 |
27 /* | 27 const char * const Translator_creator::type_p_name_ = 0; |
28 should delete these after exit. | 28 |
29 */ | 29 SCM |
| 30 Translator_creator::mark_smob () const |
| 31 { |
| 32 scm_gc_mark (name_); |
| 33 scm_gc_mark (description_); |
| 34 return listener_list_; |
| 35 } |
| 36 |
| 37 Translator * |
| 38 Translator_creator::get_translator (Context *c) |
| 39 { |
| 40 return allocate_ (this, c); |
| 41 } |
| 42 |
| 43 Translator_creator::Translator_creator (SCM name, SCM description, SCM listener_
list, |
| 44 Translator * (*allocate) |
| 45 (Translator_creator const *, Context *)) |
| 46 : name_ (name), description_ (description), listener_list_ (listener_list), |
| 47 allocate_ (allocate) |
| 48 { |
| 49 smobify_self (); |
| 50 } |
30 | 51 |
31 Protected_scm global_translator_dict; | 52 Protected_scm global_translator_dict; |
32 | 53 |
33 LY_DEFINE (get_all_translators, "ly:get-all-translators", 0, 0, 0, (), | 54 LY_DEFINE (get_all_translators, "ly:get-all-translators", 0, 0, 0, (), |
34 "Return a list of all translator objects that may be" | 55 "Return a list of all translator objects that may be" |
35 " instantiated.") | 56 " instantiated.") |
36 { | 57 { |
37 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict); | 58 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict); |
38 SCM l = dict ? dict->to_alist () : SCM_EOL; | 59 SCM l = dict ? dict->to_alist () : SCM_EOL; |
39 | 60 |
| 61 // Ok, this is a bit of a crutch: so far, the Scheme code base has |
| 62 // no idea about the Translator_creator type and uses of |
| 63 // ly:get-all-translators rely on the output being translators. |
40 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s)) | 64 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s)) |
41 scm_set_car_x (s, scm_cdar (s)); | 65 scm_set_car_x (s, unsmob <Translator_creator> (scm_cdar (s)) |
| 66 ->get_translator (0)->unprotect ()); |
42 | 67 |
43 return l; | 68 return l; |
44 } | 69 } |
45 | 70 |
46 void | 71 void |
47 add_translator (Translator *t) | 72 add_translator_creator (Translator_creator *t) |
48 { | 73 { |
49 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict); | 74 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict); |
50 if (!dict) | 75 if (!dict) |
51 { | 76 { |
52 global_translator_dict = Scheme_hash_table::make_smob (); | 77 global_translator_dict = Scheme_hash_table::make_smob (); |
53 dict = unsmob<Scheme_hash_table> (global_translator_dict); | 78 dict = unsmob<Scheme_hash_table> (global_translator_dict); |
54 } | 79 } |
55 | 80 |
56 SCM k = ly_symbol2scm (t->class_name ()); | 81 dict->set (t->get_name (), t->unprotect ()); |
57 dict->set (k, t->unprotect ()); | |
58 } | 82 } |
59 | 83 |
60 Translator * | 84 Translator_creator * |
61 get_translator (SCM sym) | 85 get_translator_creator (SCM sym) |
62 { | 86 { |
63 SCM v = SCM_BOOL_F; | 87 SCM v = SCM_BOOL_F; |
64 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict); | 88 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict); |
65 if (dict) | 89 if (dict) |
66 dict->try_retrieve (sym, &v); | 90 dict->try_retrieve (sym, &v); |
67 | 91 |
68 if (scm_is_false (v)) | 92 if (scm_is_false (v)) |
69 { | 93 { |
70 warning (_f ("unknown translator: `%s'", ly_symbol2string (sym).c_str ()))
; | 94 warning (_f ("unknown translator: `%s'", ly_symbol2string (sym).c_str ()))
; |
71 return 0; | 95 return 0; |
72 } | 96 } |
73 | 97 |
74 return unsmob<Translator> (v); | 98 return unsmob<Translator_creator> (v); |
75 } | 99 } |
OLD | NEW |