]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
Merge branch 'jneeman' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman
[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 void
57 #ifndef NDEBUG
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 #else
72 Grob::internal_set_property (SCM sym, SCM v)
73 {
74 #endif
75
76   /* Perhaps we simply do the assq_set, but what the heck. */
77   if (!is_live ())
78     return;
79
80   if (do_internal_type_checking_global)
81     {
82       if (!ly_is_procedure (v)
83           && !is_simple_closure (v)
84           && v != ly_symbol2scm ("calculation-in-progress") 
85           && !type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
86         abort ();
87       check_interfaces_for_property (this, sym);
88     }
89
90 #ifndef NDEBUG
91   if (ly_is_procedure (modification_callback))
92       scm_apply_0 (modification_callback,
93                    scm_list_n (self_scm (),
94                                scm_makfrom0str (file),
95                                scm_from_int (line),
96                                scm_makfrom0str (fun),
97                                sym, v, SCM_UNDEFINED));
98 #endif
99
100   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
101 }
102
103 //#define PROFILE_PROPERTY_ACCESSES
104 SCM
105 Grob::internal_get_property_data (SCM sym) const
106 {
107 #ifndef NDEBUG
108   if (profile_property_accesses)
109     note_property_access (&grob_property_lookup_table, sym);
110 #endif
111   
112   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
113   if (handle != SCM_BOOL_F)
114     return scm_cdr (handle);
115
116   handle = scm_sloppy_assq (sym, immutable_property_alist_);
117
118   if (do_internal_type_checking_global && scm_is_pair (handle))
119     {
120       SCM val = scm_cdr (handle);
121       if (!ly_is_procedure (val)
122           && !is_simple_closure (val)
123           && !type_check_assignment (sym, val, 
124                                   ly_symbol2scm ("backend-type?")))
125         abort ();
126
127       check_interfaces_for_property (this, sym);
128     }
129   
130   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
131 }
132
133 SCM
134 Grob::internal_get_property (SCM sym) const
135 {
136   SCM val = get_property_data (sym);
137   if (ly_is_procedure (val)
138       || is_simple_closure (val))
139     {
140       val = ((Grob*)this)->try_callback (sym, val);
141     }
142   
143   return val;
144 }
145
146 #ifndef NDEBUG
147 #include "protected-scm.hh"
148
149 Protected_scm grob_property_callback_stack = SCM_EOL;
150 bool debug_property_callbacks = 0;
151 #endif
152
153 SCM
154 Grob::try_callback (SCM sym, SCM proc)
155 {      
156   SCM marker = ly_symbol2scm ("calculation-in-progress");
157   /*
158     need to put a value in SYM to ensure that we don't get a
159     cyclic call chain.
160   */
161   mutable_property_alist_
162     = scm_assq_set_x (mutable_property_alist_, sym, marker);
163
164 #ifndef NDEBUG
165   if (debug_property_callbacks)
166     grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
167 #endif
168
169   SCM value = SCM_EOL;
170   if (ly_is_procedure (proc))
171     value = scm_call_1 (proc, self_scm ());
172   else if (is_simple_closure (proc))
173     {
174       value = evaluate_with_simple_closure (self_scm (),
175                                             simple_closure_expression (proc),
176                                             false, 0, 0);
177     }
178 #ifndef NDEBUG
179   if (debug_property_callbacks)
180     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
181 #endif
182           
183   /*
184     If the function returns SCM_UNSPECIFIED, we assume the
185     property has been set with an explicit set_property()
186     call.
187   */
188   if (value == SCM_UNSPECIFIED)
189     {
190       value = internal_get_property (sym);
191       if (value == marker)
192         mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
193     }
194   else
195     set_property (sym, value);
196           
197   return value;
198 }
199
200 void
201 Grob::internal_set_object (SCM s, SCM v)
202 {
203   /* Perhaps we simply do the assq_set, but what the heck. */
204   if (!is_live ())
205     return;
206
207   object_alist_ = scm_assq_set_x (object_alist_, s, v);
208 }
209
210 void
211 Grob::internal_del_property (SCM sym)
212 {
213   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
214 }
215
216 SCM
217 Grob::internal_get_object (SCM sym) const
218 {
219 #ifdef PROFILE_PROPERTY_ACCESSES
220   note_property_access (&grob_property_lookup_table, sym);
221 #endif
222
223   SCM s = scm_sloppy_assq (sym, object_alist_);
224
225   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
226 }
227
228 bool
229 Grob::is_live () const
230 {
231   return scm_is_pair (immutable_property_alist_);
232 }
233
234 bool
235 Grob::internal_has_interface (SCM k)
236 {
237   return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
238 }
239
240 SCM
241 call_pure_function (SCM unpure, SCM args, int start, int end)
242 {
243   SCM scm_call_pure_function = ly_lily_module_constant ("call-pure-function");
244
245   return scm_apply_0 (scm_call_pure_function,
246                       scm_list_4 (unpure, args, scm_from_int (start), scm_from_int (end)));
247 }