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