]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* scripts/convert-ly.py (datadir): add prefix switching hack 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 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 (!ly_is_procedure (v)
92           && !type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
93         abort ();
94       check_interfaces_for_property (this, sym);
95     }
96
97   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
98 }
99
100 //#define PROFILE_PROPERTY_ACCESSES
101
102 /*
103   Ugh C&P Coding.
104
105   Retrieve property without triggering callback.
106  */
107 SCM
108 Grob::get_property_data (SCM sym) const
109 {
110 #ifndef NDEBUG
111   if (profile_property_accesses)
112     note_property_access (&grob_property_lookup_table, sym);
113 #endif
114   
115   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
116   if (handle != SCM_BOOL_F)
117     return scm_cdr (handle);
118
119   handle = scm_sloppy_assq (sym, immutable_property_alist_);
120
121   if (do_internal_type_checking_global && scm_is_pair (handle))
122     {
123       SCM val = scm_cdr (handle);
124       if (!ly_is_procedure (val)
125           && !type_check_assignment (sym, val, 
126                                   ly_symbol2scm ("backend-type?")))
127         abort ();
128
129       check_interfaces_for_property (this, sym);
130     }
131   
132   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
133 }
134
135 SCM
136 Grob::internal_get_property (SCM sym) const
137 {
138   SCM val = get_property_data (sym);
139   if (ly_is_procedure (val))
140     {
141       val = ((Grob*)this)->try_callback (sym, val);
142     }
143   
144   return val;
145 }
146
147 #ifndef NDEBUG
148 #include "protected-scm.hh"
149 Protected_scm grob_property_callback_stack = SCM_EOL;
150 bool debug_property_callbacks = 1;
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   SCM value = scm_call_1 (proc, self_scm ());
169 #ifndef NDEBUG
170   if (debug_property_callbacks)
171     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
172 #endif
173           
174   /*
175     If the function returns SCM_UNSPECIFIED, we assume the
176     property has been set with an explicit set_property()
177     call.
178   */
179   if (value == SCM_UNSPECIFIED)
180     {
181       value = internal_get_property (sym);
182       if (value == marker)
183         mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
184     }
185   else
186     internal_set_property (sym, value);
187           
188   return value;
189 }
190
191 void
192 Grob::internal_set_object (SCM s, SCM v)
193 {
194   /* Perhaps we simply do the assq_set, but what the heck. */
195   if (!is_live ())
196     return;
197
198   object_alist_ = scm_assq_set_x (object_alist_, s, v);
199 }
200
201 void
202 Grob::del_property (SCM sym)
203 {
204   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
205 }
206
207 SCM
208 Grob::internal_get_object (SCM sym) const
209 {
210 #ifdef PROFILE_PROPERTY_ACCESSES
211   note_property_access (&grob_property_lookup_table, sym);
212 #endif
213
214   SCM s = scm_sloppy_assq (sym, object_alist_);
215
216   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
217 }
218
219 void
220 Grob::substitute_object_links (SCM crit, SCM orig)
221 {
222   set_break_subsititution (crit);
223   object_alist_ = substitute_object_alist (orig, object_alist_);
224 }
225
226 bool
227 Grob::is_live () const
228 {
229   return immutable_property_alist_ != SCM_EOL;
230 }