]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* lily/grob-property.cc (get_interfaces): new function.
[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-smob.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 "item.hh"
15 #include "misc.hh"
16 #include "item.hh"
17 #include "program-option.hh"
18 #include "profile.hh"
19
20 SCM
21 Grob::get_property_alist_chain (SCM def) const
22 {
23   return scm_list_n (mutable_property_alist_,
24                      immutable_property_alist_,
25                      def,
26                      SCM_UNDEFINED);
27 }
28
29 SCM
30 Grob::get_interfaces () const
31 {
32   return interfaces_;
33 }
34
35 /*
36   This special add_thing routine is slightly more efficient than
37
38   set_prop (name, cons (thing, get_prop (name)))
39
40   since it can reuse the handle returned by scm_assq ().
41 */
42 // JUNKME.
43 void
44 Grob::add_to_list_property (SCM sym, SCM thing)
45 {
46   SCM handle
47     = scm_sloppy_assq (sym, mutable_property_alist_);
48
49   if (handle != SCM_BOOL_F)
50     scm_set_cdr_x (handle, scm_cons (thing, scm_cdr (handle)));
51   else
52     {
53       /*
54         There is no mutable prop yet, so create an entry, and put it in front of the
55         mutable prop list.
56       */
57       handle = scm_sloppy_assq (sym, immutable_property_alist_);
58       SCM tail = (handle != SCM_BOOL_F) ? scm_cdr (handle) : SCM_EOL;
59       SCM val = scm_cons (thing, tail);
60
61       mutable_property_alist_ = scm_cons (scm_cons (sym, val),
62                                           mutable_property_alist_);
63     }
64 }
65
66 extern void check_interfaces_for_property (Grob const *me, SCM sym);
67
68 void
69 Grob::internal_set_property (SCM sym, SCM v)
70 {
71 #ifndef NDEBUG
72   SCM grob_p = ly_lily_module_constant ("ly:grob?");
73   SCM grob_list_p = ly_lily_module_constant ("grob-list?");
74   SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
75
76   if (type == grob_p
77       || type == grob_list_p
78       || (unsmob_grob (v) && ly_symbol2scm ("cause") != sym))
79     {
80       scm_display (scm_list_2 (sym, type), scm_current_output_port ());
81       assert (0);
82     }
83 #endif
84
85   /* Perhaps we simply do the assq_set, but what the heck. */
86   if (!is_live ())
87     return;
88
89   if (do_internal_type_checking_global)
90     {
91       if (!type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
92         abort ();
93       check_interfaces_for_property (this, sym);
94     }
95
96   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
97 }
98
99 //#define PROFILE_PROPERTY_ACCESSES
100
101 /*
102   Ugh C&P Coding.
103
104   Retrieve property without triggering callback.
105  */
106 SCM
107 Grob::get_property_data (SCM sym) const
108 {
109 #ifndef NDEBUG
110   if (profile_property_accesses)
111     note_property_access (&grob_property_lookup_table, sym);
112 #endif
113   
114   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
115   if (handle != SCM_BOOL_F)
116     return scm_cdr (handle);
117
118   handle = scm_sloppy_assq (sym, immutable_property_alist_);
119
120   if (do_internal_type_checking_global && scm_is_pair (handle))
121     {
122       if (!type_check_assignment (sym, scm_cdr (handle),
123                                   ly_symbol2scm ("backend-type?")))
124         abort ();
125
126       check_interfaces_for_property (this, sym);
127     }
128   
129   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
130 }
131
132 SCM
133 Grob::internal_get_property (SCM sym) const
134 {
135 #ifndef NDEBUG
136   if (profile_property_accesses)
137     note_property_access (&grob_property_lookup_table, sym);
138 #endif
139   
140   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
141   if (handle != SCM_BOOL_F)
142     return scm_cdr (handle);
143
144   handle = scm_sloppy_assq (sym, immutable_property_alist_);
145
146   if (do_internal_type_checking_global && scm_is_pair (handle))
147     {
148       if (!type_check_assignment (sym, scm_cdr (handle),
149                                   ly_symbol2scm ("backend-type?")))
150         abort ();
151
152       check_interfaces_for_property (this, sym);
153     }
154
155   
156   if (handle == SCM_BOOL_F)
157     {
158       SCM value = ((Grob*)  this)->try_callback (sym);
159       if (value != SCM_UNSPECIFIED)
160         return value;
161     }
162   
163   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
164 }
165
166 #ifndef NDEBUG
167 #include "protected-scm.hh"
168 Protected_scm grob_property_callback_stack = SCM_EOL;
169 bool debug_property_callbacks = 1;
170 #endif
171
172 SCM
173 Grob::try_callback (SCM sym)
174 {      
175   SCM handle = scm_sloppy_assq (sym, property_callbacks_);
176   if (scm_is_pair (handle))
177     {
178       SCM proc = scm_cdr (handle);
179       if (ly_is_procedure (proc))
180         {
181           SCM marker = ly_symbol2scm ("calculation-in-progress");
182           /*
183             need to put a value in SYM to ensure that we don't get a
184             cyclic call chain.
185           */
186           mutable_property_alist_
187             = scm_assq_set_x (mutable_property_alist_, sym, marker);
188
189 #ifndef NDEBUG
190           if (debug_property_callbacks)
191             grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
192 #endif
193           SCM value = scm_call_1 (proc, self_scm ());
194 #ifndef NDEBUG
195           if (debug_property_callbacks)
196             grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
197 #endif
198           
199           /*
200             If the function returns SCM_UNSPECIFIED, we assume the
201             property has been set with an explicit set_property()
202             call.
203            */
204           if (value == SCM_UNSPECIFIED)
205             {
206               value = internal_get_property (sym);
207               if (value == marker)
208                 mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
209             }
210           else
211             internal_set_property (sym, value);
212           
213           return value;
214         }
215       else
216         programming_error ("Callback should be procedure type");
217     }
218
219   return SCM_UNSPECIFIED;
220 }
221
222 void
223 Grob::internal_set_object (SCM s, SCM v)
224 {
225   /* Perhaps we simply do the assq_set, but what the heck. */
226   if (!is_live ())
227     return;
228
229   object_alist_ = scm_assq_set_x (object_alist_, s, v);
230 }
231
232 void
233 Grob::set_callback (SCM s, SCM v)
234 {
235   /* Perhaps we simply do the assq_set, but what the heck. */
236   if (!is_live ())
237     return;
238
239   /*
240     property_callbacks_ is r/o in principle, so we tack it in front.
241    */ 
242   property_callbacks_ = scm_acons (s,v, property_callbacks_);
243 }
244
245
246
247 SCM
248 Grob::internal_get_object (SCM sym) const
249 {
250 #ifdef PROFILE_PROPERTY_ACCESSES
251   note_property_access (&grob_property_lookup_table, sym);
252 #endif
253
254   SCM s = scm_sloppy_assq (sym, object_alist_);
255
256   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
257 }
258
259 void
260 Grob::substitute_object_links (SCM crit, SCM orig)
261 {
262   set_break_subsititution (crit);
263   object_alist_ = substitute_object_alist (orig, object_alist_);
264 }
265
266 bool
267 Grob::is_live () const
268 {
269   return immutable_property_alist_ != SCM_EOL;
270 }