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