]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
Web: Fix broken link to LSR website
[lilypond.git] / lily / grob-property.cc
1 /*
2   Implement storage and manipulation of grob properties.
3 */
4
5 #include <cstring>
6
7 #include "main.hh"
8 #include "input.hh"
9 #include "pointer-group-interface.hh"
10 #include "misc.hh"
11 #include "paper-score.hh"
12 #include "output-def.hh"
13 #include "spanner.hh"
14 #include "international.hh"
15 #include "item.hh"
16 #include "misc.hh"
17 #include "item.hh"
18 #include "program-option.hh"
19 #include "profile.hh"
20 #include "simple-closure.hh"
21 #include "unpure-pure-container.hh"
22 #include "warn.hh"
23 #include "protected-scm.hh"
24
25 Protected_scm grob_property_callback_stack = SCM_EOL;
26
27 extern bool debug_property_callbacks;
28
29 #ifndef NDEBUG
30 static void
31 print_property_callback_stack ()
32 {
33   int frame = 0;
34   for (SCM s = grob_property_callback_stack; scm_is_pair (s); s = scm_cdr (s))
35     message (_f ("%d: %s", frame++, ly_scm_write_string (scm_car (s)).c_str ()));
36 }
37 #endif
38
39 static Protected_scm modification_callback = SCM_EOL;
40 static Protected_scm cache_callback = SCM_EOL;
41
42 /*
43 FIXME: this should use ly:set-option interface instead.
44 */
45
46 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
47            1, 0, 0, (SCM cb),
48            "Specify a procedure that will be called every time LilyPond"
49            " modifies a grob property.  The callback will receive as"
50            " arguments the grob that is being modified, the name of the"
51            " C++ file in which the modification was requested, the line"
52            " number in the C++ file in which the modification was requested,"
53            " the name of the function in which the modification was"
54            " requested, the property to be changed, and the new value for"
55            " the property.")
56 {
57   modification_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
58   return SCM_UNSPECIFIED;
59 }
60
61 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
62            1, 0, 0, (SCM cb),
63            "Specify a procedure that will be called whenever lilypond"
64            " calculates a callback function and caches the result.  The"
65            " callback will receive as arguments the grob whose property it"
66            " is, the name of the property, the name of the callback that"
67            " calculated the property, and the new (cached) value of the"
68            " property.")
69 {
70   cache_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
71   return SCM_UNSPECIFIED;
72 }
73
74 void
75 Grob::instrumented_set_property (SCM sym, SCM v,
76                                  char const *file,
77                                  int line,
78                                  char const *fun)
79 {
80 #ifndef NDEBUG
81   if (ly_is_procedure (modification_callback))
82     scm_apply_0 (modification_callback,
83                  scm_list_n (self_scm (),
84                              scm_from_locale_string (file),
85                              scm_from_int (line),
86                              scm_from_locale_string (fun),
87                              sym, v, SCM_UNDEFINED));
88 #else
89   (void) file;
90   (void) line;
91   (void) fun;
92 #endif
93
94   internal_set_property (sym, v);
95 }
96
97 SCM
98 Grob::get_property_alist_chain (SCM def) const
99 {
100   return scm_list_n (mutable_property_alist_,
101                      immutable_property_alist_,
102                      def,
103                      SCM_UNDEFINED);
104 }
105
106 extern void check_interfaces_for_property (Grob const *me, SCM sym);
107
108 void
109 Grob::internal_set_property (SCM sym, SCM v)
110 {
111   internal_set_value_on_alist (&mutable_property_alist_,
112                                sym, v);
113
114 }
115
116 void
117 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
118 {
119   /* Perhaps we simply do the assq_set, but what the heck. */
120   if (!is_live ())
121     return;
122
123   if (do_internal_type_checking_global)
124     {
125       if (!ly_is_procedure (v)
126           && !Simple_closure::is_smob (v)
127           && !Unpure_pure_container::is_smob (v)
128           && v != ly_symbol2scm ("calculation-in-progress"))
129         type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
130
131       check_interfaces_for_property (this, sym);
132     }
133
134   *alist = scm_assq_set_x (*alist, sym, v);
135 }
136
137 SCM
138 Grob::internal_get_property_data (SCM sym) const
139 {
140 #ifndef NDEBUG
141   if (profile_property_accesses)
142     note_property_access (&grob_property_lookup_table, sym);
143 #endif
144
145   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
146   if (handle != SCM_BOOL_F)
147     return scm_cdr (handle);
148
149   handle = scm_sloppy_assq (sym, immutable_property_alist_);
150
151   if (do_internal_type_checking_global && scm_is_pair (handle))
152     {
153       SCM val = scm_cdr (handle);
154       if (!ly_is_procedure (val) && !Simple_closure::is_smob (val)
155           && !Unpure_pure_container::is_smob (val))
156         type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
157
158       check_interfaces_for_property (this, sym);
159     }
160
161   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
162 }
163
164 SCM
165 Grob::internal_get_property (SCM sym) const
166 {
167   SCM val = get_property_data (sym);
168
169 #ifndef NDEBUG
170   if (val == ly_symbol2scm ("calculation-in-progress"))
171     {
172       programming_error (to_string ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
173                                     ly_symbol2string (sym).c_str (),
174                                     name ().c_str ()));//assert (1==0);
175       if (debug_property_callbacks)
176         {
177           message ("backtrace: ");
178           print_property_callback_stack ();
179         }
180     }
181 #endif
182
183   if (Unpure_pure_container *upc = Unpure_pure_container::unsmob (val))
184     val = upc->unpure_part ();
185
186   if (ly_is_procedure (val)
187       || Simple_closure::is_smob (val))
188     {
189       Grob *me = ((Grob *)this);
190       val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
191     }
192
193   return val;
194 }
195
196 /* Unlike internal_get_property, this function does no caching. Use it, therefore, with caution. */
197 SCM
198 Grob::internal_get_pure_property (SCM sym, int start, int end) const
199 {
200   SCM val = internal_get_property_data (sym);
201   if (ly_is_procedure (val))
202     return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
203
204   if (Unpure_pure_container *upc = Unpure_pure_container::unsmob (val)) {
205     // Do cache, if the function ignores 'start' and 'end'
206     if (upc->is_unchanging ())
207       return internal_get_property (sym);
208     else
209       return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
210   }
211
212   if (Simple_closure *sc = Simple_closure::unsmob (val))
213     return evaluate_with_simple_closure (self_scm (),
214                                          sc->expression (),
215                                          true, start, end);
216   return val;
217 }
218
219 SCM
220 Grob::internal_get_maybe_pure_property (SCM sym, bool pure, int start, int end) const
221 {
222   return pure ? internal_get_pure_property (sym, start, end) : internal_get_property (sym);
223 }
224
225 SCM
226 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
227 {
228   SCM marker = ly_symbol2scm ("calculation-in-progress");
229   /*
230     need to put a value in SYM to ensure that we don't get a
231     cyclic call chain.
232   */
233   *alist = scm_assq_set_x (*alist, sym, marker);
234
235 #ifndef NDEBUG
236   if (debug_property_callbacks)
237     grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
238 #endif
239
240   SCM value = SCM_EOL;
241   if (ly_is_procedure (proc))
242     value = scm_call_1 (proc, self_scm ());
243   else if (Simple_closure *sc = Simple_closure::unsmob (proc))
244     {
245       value = evaluate_with_simple_closure (self_scm (),
246                                             sc->expression (),
247                                             false, 0, 0);
248     }
249
250 #ifndef NDEBUG
251   if (debug_property_callbacks)
252     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
253 #endif
254
255   if (value == SCM_UNSPECIFIED)
256     {
257       value = get_property_data (sym);
258       assert (value == SCM_EOL || value == marker);
259       if (value == marker)
260         *alist = scm_assq_remove_x (*alist, sym);
261     }
262   else
263     {
264 #ifndef NDEBUG
265       if (ly_is_procedure (cache_callback))
266         scm_apply_0 (cache_callback,
267                      scm_list_n (self_scm (),
268                                  sym,
269                                  proc,
270                                  value,
271                                  SCM_UNDEFINED));
272 #endif
273       internal_set_value_on_alist (alist, sym, value);
274     }
275
276   return value;
277 }
278
279 void
280 Grob::internal_set_object (SCM s, SCM v)
281 {
282   /* Perhaps we simply do the assq_set, but what the heck. */
283   if (!is_live ())
284     return;
285
286   object_alist_ = scm_assq_set_x (object_alist_, s, v);
287 }
288
289 void
290 Grob::internal_del_property (SCM sym)
291 {
292   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
293 }
294
295 SCM
296 Grob::internal_get_object (SCM sym) const
297 {
298   if (profile_property_accesses)
299     note_property_access (&grob_property_lookup_table, sym);
300
301   SCM s = scm_sloppy_assq (sym, object_alist_);
302
303   if (s != SCM_BOOL_F)
304     {
305       SCM val = scm_cdr (s);
306       if (ly_is_procedure (val)
307           || Simple_closure::is_smob (val)
308           || Unpure_pure_container::is_smob (val))
309         {
310           Grob *me = ((Grob *)this);
311           val = me->try_callback_on_alist (&me->object_alist_, sym, val);
312         }
313
314       return val;
315     }
316
317   return SCM_EOL;
318 }
319
320 bool
321 Grob::is_live () const
322 {
323   return scm_is_pair (immutable_property_alist_);
324 }
325
326 bool
327 Grob::internal_has_interface (SCM k)
328 {
329   return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
330 }
331
332 SCM
333 call_pure_function (SCM unpure, SCM args, int start, int end)
334 {
335   if (Unpure_pure_container *upc = Unpure_pure_container::unsmob (unpure))
336     {
337       SCM pure = upc->pure_part ();
338
339       if (Simple_closure *sc = Simple_closure::unsmob (pure))
340         {
341           SCM expr = sc->expression ();
342           return evaluate_with_simple_closure (scm_car (args), expr, true, start, end);
343         }
344
345       if (ly_is_procedure (pure))
346         return scm_apply_0 (pure,
347                             scm_append (scm_list_2 (scm_list_3 (scm_car (args),
348                                                                 scm_from_int (start),
349                                                                 scm_from_int (end)),
350                                                     scm_cdr (args))));
351
352       return pure;
353     }
354
355   if (Simple_closure *sc = Simple_closure::unsmob (unpure))
356     {
357       SCM expr = sc->expression ();
358       return evaluate_with_simple_closure (scm_car (args), expr, true, start, end);
359     }
360
361   if (!ly_is_procedure (unpure))
362     return unpure;
363
364   return SCM_BOOL_F;
365 }
366