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