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