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