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"
16 #include "program-option.hh"
18 #include "unpure-pure-container.hh"
20 #include "protected-scm.hh"
22 Protected_scm grob_property_callback_stack (SCM_EOL);
24 extern bool debug_property_callbacks;
28 print_property_callback_stack ()
31 for (SCM s = grob_property_callback_stack; scm_is_pair (s); s = scm_cdr (s))
32 message (_f ("%d: %s", frame++, ly_scm_write_string (scm_car (s)).c_str ()));
36 static Protected_scm modification_callback (SCM_EOL);
37 static Protected_scm cache_callback (SCM_EOL);
40 FIXME: this should use ly:set-option interface instead.
43 LY_DEFINE (ly_set_grob_modification_callback, "ly:set-grob-modification-callback",
45 "Specify a procedure that will be called every time LilyPond"
46 " modifies a grob property. The callback will receive as"
47 " arguments the grob that is being modified, the name of the"
48 " C++ file in which the modification was requested, the line"
49 " number in the C++ file in which the modification was requested,"
50 " the name of the function in which the modification was"
51 " requested, the property to be changed, and the new value for"
54 modification_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
55 return SCM_UNSPECIFIED;
58 LY_DEFINE (ly_set_property_cache_callback, "ly:set-property-cache-callback",
60 "Specify a procedure that will be called whenever lilypond"
61 " calculates a callback function and caches the result. The"
62 " callback will receive as arguments the grob whose property it"
63 " is, the name of the property, the name of the callback that"
64 " calculated the property, and the new (cached) value of the"
67 cache_callback = (ly_is_procedure (cb)) ? cb : SCM_BOOL_F;
68 return SCM_UNSPECIFIED;
72 Grob::instrumented_set_property (SCM sym, SCM v,
78 if (ly_is_procedure (modification_callback))
79 scm_apply_0 (modification_callback,
80 scm_list_n (self_scm (),
81 scm_from_locale_string (file),
83 scm_from_ascii_string (fun),
84 sym, v, SCM_UNDEFINED));
91 internal_set_property (sym, v);
95 Grob::get_property_alist_chain (SCM def) const
97 return scm_list_n (mutable_property_alist_,
98 immutable_property_alist_,
103 extern void check_interfaces_for_property (Grob const *me, SCM sym);
106 Grob::internal_set_property (SCM sym, SCM v)
108 internal_set_value_on_alist (&mutable_property_alist_,
114 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
116 /* Perhaps we simply do the assq_set, but what the heck. */
120 if (do_internal_type_checking_global)
122 if (!ly_is_procedure (v)
123 && !unsmob<Unpure_pure_container> (v)
124 && !scm_is_eq (v, ly_symbol2scm ("calculation-in-progress")))
125 type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
127 check_interfaces_for_property (this, sym);
130 *alist = scm_assq_set_x (*alist, sym, v);
134 Grob::internal_get_property_data (SCM sym) const
137 if (profile_property_accesses)
138 note_property_access (&grob_property_lookup_table, sym);
141 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
142 if (scm_is_true (handle))
143 return scm_cdr (handle);
145 handle = scm_sloppy_assq (sym, immutable_property_alist_);
147 if (do_internal_type_checking_global && scm_is_pair (handle))
149 SCM val = scm_cdr (handle);
150 if (!ly_is_procedure (val)
151 && !unsmob<Unpure_pure_container> (val))
152 type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
154 check_interfaces_for_property (this, sym);
157 return scm_is_false (handle) ? SCM_EOL : scm_cdr (handle);
161 Grob::internal_get_property (SCM sym) const
163 SCM val = get_property_data (sym);
166 if (scm_is_eq (val, ly_symbol2scm ("calculation-in-progress")))
168 programming_error (to_string ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
169 ly_symbol2string (sym).c_str (),
170 name ().c_str ()));//assert (1==0);
171 if (debug_property_callbacks)
173 message ("backtrace: ");
174 print_property_callback_stack ();
179 if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (val))
180 val = upc->unpure_part ();
182 if (ly_is_procedure (val))
184 Grob *me = ((Grob *)this);
185 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
191 /* Unlike internal_get_property, this function does no caching. Use it, therefore, with caution. */
193 Grob::internal_get_pure_property (SCM sym, int start, int end) const
195 SCM val = internal_get_property_data (sym);
196 if (ly_is_procedure (val))
197 return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
199 if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (val)) {
200 // Do cache, if the function ignores 'start' and 'end'
201 if (upc->is_unchanging ())
202 return internal_get_property (sym);
204 return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
211 Grob::internal_get_maybe_pure_property (SCM sym, bool pure, int start, int end) const
213 return pure ? internal_get_pure_property (sym, start, end) : internal_get_property (sym);
217 Grob::try_callback_on_alist (SCM *alist, SCM sym, SCM proc)
219 SCM marker = ly_symbol2scm ("calculation-in-progress");
221 need to put a value in SYM to ensure that we don't get a
224 *alist = scm_assq_set_x (*alist, sym, marker);
227 if (debug_property_callbacks)
228 grob_property_callback_stack = scm_cons (scm_list_3 (self_scm (), sym, proc), grob_property_callback_stack);
232 if (ly_is_procedure (proc))
233 value = scm_call_1 (proc, self_scm ());
236 if (debug_property_callbacks)
237 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
240 if (scm_is_eq (value, SCM_UNSPECIFIED))
242 value = get_property_data (sym);
243 assert (scm_is_null (value) || scm_is_eq (value, marker));
244 if (scm_is_eq (value, marker))
245 *alist = scm_assq_remove_x (*alist, sym);
250 if (ly_is_procedure (cache_callback))
251 scm_apply_0 (cache_callback,
252 scm_list_n (self_scm (),
258 internal_set_value_on_alist (alist, sym, value);
265 Grob::internal_set_object (SCM s, SCM v)
267 /* Perhaps we simply do the assq_set, but what the heck. */
271 object_alist_ = scm_assq_set_x (object_alist_, s, v);
275 Grob::internal_del_property (SCM sym)
277 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
281 Grob::internal_get_object (SCM sym) const
283 if (profile_property_accesses)
284 note_property_access (&grob_property_lookup_table, sym);
286 SCM s = scm_sloppy_assq (sym, object_alist_);
290 SCM val = scm_cdr (s);
291 if (ly_is_procedure (val)
292 || unsmob<Unpure_pure_container> (val))
294 Grob *me = ((Grob *)this);
295 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
305 Grob::is_live () const
307 return scm_is_pair (immutable_property_alist_);
311 Grob::internal_has_interface (SCM k)
313 return scm_is_true (scm_c_memq (k, interfaces_));
317 call_pure_function (SCM value, SCM args, int start, int end)
319 if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (value))
321 if (upc->is_unchanging ())
323 // Don't bother forming an Unpure_pure_call here.
324 value = upc->unpure_part ();
326 if (ly_is_procedure (value))
327 return scm_apply_0 (value, args);
331 value = upc->pure_part ();
333 if (ly_is_procedure (value))
334 return scm_apply_3 (value,
336 scm_from_int (start),
343 if (!ly_is_procedure (value))