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_3 (mutable_property_alist_,
98 immutable_property_alist_,
102 extern void check_interfaces_for_property (Grob const *me, SCM sym);
105 Grob::internal_set_property (SCM sym, SCM v)
107 internal_set_value_on_alist (&mutable_property_alist_,
113 Grob::internal_set_value_on_alist (SCM *alist, SCM sym, SCM v)
115 /* Perhaps we simply do the assq_set, but what the heck. */
119 if (do_internal_type_checking_global)
121 if (!ly_is_procedure (v)
122 && !unsmob<Unpure_pure_container> (v)
123 && !scm_is_eq (v, ly_symbol2scm ("calculation-in-progress")))
124 type_check_assignment (sym, v, ly_symbol2scm ("backend-type?"));
126 check_interfaces_for_property (this, sym);
129 *alist = scm_assq_set_x (*alist, sym, v);
133 Grob::internal_get_property_data (SCM sym) const
136 if (profile_property_accesses)
137 note_property_access (&grob_property_lookup_table, sym);
140 SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
141 if (scm_is_true (handle))
142 return scm_cdr (handle);
144 handle = scm_sloppy_assq (sym, immutable_property_alist_);
146 if (do_internal_type_checking_global && scm_is_pair (handle))
148 SCM val = scm_cdr (handle);
149 if (!ly_is_procedure (val)
150 && !unsmob<Unpure_pure_container> (val))
151 type_check_assignment (sym, val, ly_symbol2scm ("backend-type?"));
153 check_interfaces_for_property (this, sym);
156 return scm_is_false (handle) ? SCM_EOL : scm_cdr (handle);
160 Grob::internal_get_property (SCM sym) const
162 SCM val = get_property_data (sym);
165 if (scm_is_eq (val, ly_symbol2scm ("calculation-in-progress")))
167 programming_error (to_string ("cyclic dependency: calculation-in-progress encountered for #'%s (%s)",
168 ly_symbol2string (sym).c_str (),
169 name ().c_str ()));//assert (1==0);
170 if (debug_property_callbacks)
172 message ("backtrace: ");
173 print_property_callback_stack ();
178 if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (val))
179 val = upc->unpure_part ();
181 if (ly_is_procedure (val))
183 Grob *me = ((Grob *)this);
184 val = me->try_callback_on_alist (&me->mutable_property_alist_, sym, val);
190 /* Unlike internal_get_property, this function does no caching. Use it, therefore, with caution. */
192 Grob::internal_get_pure_property (SCM sym, int start, int end) const
194 SCM val = internal_get_property_data (sym);
195 if (ly_is_procedure (val))
196 return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
198 if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (val)) {
199 // Do cache, if the function ignores 'start' and 'end'
200 if (upc->is_unchanging ())
201 return internal_get_property (sym);
203 return call_pure_function (val, scm_list_1 (self_scm ()), start, end);
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 ());
235 if (debug_property_callbacks)
236 grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
239 if (scm_is_eq (value, SCM_UNSPECIFIED))
241 value = get_property_data (sym);
242 assert (scm_is_null (value) || scm_is_eq (value, marker));
243 if (scm_is_eq (value, marker))
244 *alist = scm_assq_remove_x (*alist, sym);
249 if (ly_is_procedure (cache_callback))
250 scm_call_4 (cache_callback,
256 internal_set_value_on_alist (alist, sym, value);
263 Grob::internal_set_object (SCM s, SCM v)
265 /* Perhaps we simply do the assq_set, but what the heck. */
269 object_alist_ = scm_assq_set_x (object_alist_, s, v);
273 Grob::internal_del_property (SCM sym)
275 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
279 Grob::internal_get_object (SCM sym) const
281 if (profile_property_accesses)
282 note_property_access (&grob_property_lookup_table, sym);
284 SCM s = scm_sloppy_assq (sym, object_alist_);
288 SCM val = scm_cdr (s);
289 if (ly_is_procedure (val)
290 || unsmob<Unpure_pure_container> (val))
292 Grob *me = ((Grob *)this);
293 val = me->try_callback_on_alist (&me->object_alist_, sym, val);
303 Grob::is_live () const
305 return scm_is_pair (immutable_property_alist_);
309 Grob::internal_has_interface (SCM k)
311 return scm_is_true (scm_c_memq (k, interfaces_));
315 call_pure_function (SCM value, SCM args, int start, int end)
317 if (Unpure_pure_container *upc = unsmob<Unpure_pure_container> (value))
319 if (upc->is_unchanging ())
321 // Don't bother forming an Unpure_pure_call here.
322 value = upc->unpure_part ();
324 if (ly_is_procedure (value))
325 return scm_apply_0 (value, args);
329 value = upc->pure_part ();
331 if (ly_is_procedure (value))
332 return scm_apply_3 (value,
334 scm_from_int (start),
341 if (!ly_is_procedure (value))