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