2 Implement storage and manipulation of grob properties.
9 #include "pointer-group-interface.hh"
11 #include "paper-score.hh"
12 #include "output-def.hh"
14 #include "international.hh"
18 #include "program-option.hh"
20 #include "simple-closure.hh"
21 #include "unpure-pure-container.hh"
23 #include "protected-scm.hh"
25 Protected_scm grob_property_callback_stack = SCM_EOL;
27 extern bool debug_property_callbacks;
31 print_property_callback_stack ()
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 ()));
39 static Protected_scm modification_callback = SCM_EOL;
40 static Protected_scm cache_callback = SCM_EOL;
43 FIXME: this should use ly:set-option interface instead.
46 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
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"
57 modification_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
58 return SCM_UNSPECIFIED;
61 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
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"
70 cache_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
71 return SCM_UNSPECIFIED;
75 Grob::instrumented_set_property (SCM sym, SCM v,
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),
86 scm_from_locale_string (fun),
87 sym, v, SCM_UNDEFINED));
94 internal_set_property (sym, v);
98 Grob::get_property_alist_chain (SCM def) const
100 return scm_list_n (mutable_property_alist_,
101 immutable_property_alist_,
106 extern void check_interfaces_for_property (Grob const *me, SCM sym);
109 Grob::internal_set_property (SCM sym, SCM v)
111 internal_set_value_on_alist (&mutable_property_alist_,
117 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
119 /* Perhaps we simply do the assq_set, but what the heck. */
123 if (do_internal_type_checking_global)
125 if (!ly_is_procedure (v)
126 && !is_simple_closure (v)
127 && !is_unpure_pure_container (v)
128 && v != ly_symbol2scm ("calculation-in-progress"))
129 type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
131 check_interfaces_for_property (this, sym);
134 *alist = scm_assq_set_x (*alist, sym, v);
138 Grob::internal_get_property_data (SCM sym) const
141 if (profile_property_accesses)
142 note_property_access (&grob_property_lookup_table, sym);
145 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
146 if (handle != SCM_BOOL_F)
147 return scm_cdr (handle);
149 handle = scm_sloppy_assq (sym, immutable_property_alist_);
151 if (do_internal_type_checking_global && scm_is_pair (handle))
153 SCM val = scm_cdr (handle);
154 if (!ly_is_procedure (val) && !is_simple_closure (val) && !is_unpure_pure_container (val))
155 type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
157 check_interfaces_for_property (this, sym);
160 return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
164 Grob::internal_get_property (SCM sym) const
166 SCM val = get_property_data (sym);
169 if (val == ly_symbol2scm ("calculation-in-progress"))
171 programming_error (to_string ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
172 ly_symbol2string (sym).c_str (),
173 name ().c_str ()));//assert (1==0);
174 if (debug_property_callbacks)
176 message ("backtrace: ");
177 print_property_callback_stack ();
182 if (is_unpure_pure_container (val))
183 val = unpure_pure_container_unpure_part (val);
185 if (ly_is_procedure (val)
186 || is_simple_closure (val))
188 Grob *me = ((Grob *)this);
189 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
195 /* Unlike internal_get_property, this function does no caching. Use it, therefore, with caution. */
197 Grob::internal_get_pure_property (SCM sym, int start, int end) const
199 SCM val = internal_get_property_data (sym);
200 if (ly_is_procedure (val) || is_unpure_pure_container (val))
201 return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
202 if (is_simple_closure (val))
203 return evaluate_with_simple_closure (self_scm (),
204 simple_closure_expression (val),
210 Grob::internal_get_maybe_pure_property (SCM sym, bool pure, int start, int end) const
212 return pure ? internal_get_pure_property (sym, start, end) : internal_get_property (sym);
216 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
218 SCM marker = ly_symbol2scm ("calculation-in-progress");
220 need to put a value in SYM to ensure that we don't get a
223 *alist = scm_assq_set_x (*alist, sym, marker);
226 if (debug_property_callbacks)
227 grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
231 if (ly_is_procedure (proc))
232 value = scm_call_1 (proc, self_scm ());
233 else if (is_simple_closure (proc))
235 value = evaluate_with_simple_closure (self_scm (),
236 simple_closure_expression (proc),
241 if (debug_property_callbacks)
242 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
245 if (value == SCM_UNSPECIFIED)
247 value = get_property_data (sym);
248 assert (value == SCM_EOL || value == marker);
250 *alist = scm_assq_remove_x (*alist, sym);
255 if (ly_is_procedure (cache_callback))
256 scm_apply_0 (cache_callback,
257 scm_list_n (self_scm (),
263 internal_set_value_on_alist (alist, sym, value);
270 Grob::internal_set_object (SCM s, SCM v)
272 /* Perhaps we simply do the assq_set, but what the heck. */
276 object_alist_ = scm_assq_set_x (object_alist_, s, v);
280 Grob::internal_del_property (SCM sym)
282 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
286 Grob::internal_get_object (SCM sym) const
288 if (profile_property_accesses)
289 note_property_access (&grob_property_lookup_table, sym);
291 SCM s = scm_sloppy_assq (sym, object_alist_);
295 SCM val = scm_cdr (s);
296 if (ly_is_procedure (val)
297 || is_simple_closure (val)
298 || is_unpure_pure_container (val))
300 Grob *me = ((Grob *)this);
301 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
311 Grob::is_live () const
313 return scm_is_pair (immutable_property_alist_);
317 Grob::internal_has_interface (SCM k)
319 return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
323 call_pure_function (SCM unpure, SCM args, int start, int end)
325 if (is_unpure_pure_container (unpure))
327 SCM pure = unpure_pure_container_pure_part (unpure);
329 if (is_simple_closure (pure))
331 SCM expr = simple_closure_expression (pure);
332 return evaluate_with_simple_closure (scm_car (args), expr, true, start, end);
335 if (ly_is_procedure (pure))
336 return scm_apply_0 (pure,
337 scm_append (scm_list_2 (scm_list_3 (scm_car (args),
338 scm_from_int (start),
345 if (is_simple_closure (unpure))
347 SCM expr = simple_closure_expression (unpure);
348 return evaluate_with_simple_closure (scm_car (args), expr, true, start, end);
351 if (!ly_is_procedure (unpure))