]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* lily/grob.cc: remove X-extent-callback / Y-extent-callback.
[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   SCM val = get_property_data (sym);
136   if (ly_is_procedure (val))
137     {
138       val = ((Grob*)this)->try_callback (sym, val);
139     }
140   
141   return val;
142 }
143
144 #ifndef NDEBUG
145 #include "protected-scm.hh"
146 Protected_scm grob_property_callback_stack = SCM_EOL;
147 bool debug_property_callbacks = 1;
148 #endif
149
150 SCM
151 Grob::try_callback (SCM sym, SCM proc)
152 {      
153   SCM marker = ly_symbol2scm ("calculation-in-progress");
154   /*
155     need to put a value in SYM to ensure that we don't get a
156     cyclic call chain.
157   */
158   mutable_property_alist_
159     = scm_assq_set_x (mutable_property_alist_, sym, marker);
160
161 #ifndef NDEBUG
162   if (debug_property_callbacks)
163     grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
164 #endif
165   SCM value = scm_call_1 (proc, self_scm ());
166 #ifndef NDEBUG
167   if (debug_property_callbacks)
168     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
169 #endif
170           
171   /*
172     If the function returns SCM_UNSPECIFIED, we assume the
173     property has been set with an explicit set_property()
174     call.
175   */
176   if (value == SCM_UNSPECIFIED)
177     {
178       value = internal_get_property (sym);
179       if (value == marker)
180         mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
181     }
182   else
183     internal_set_property (sym, value);
184           
185   return value;
186 }
187
188 void
189 Grob::internal_set_object (SCM s, SCM v)
190 {
191   /* Perhaps we simply do the assq_set, but what the heck. */
192   if (!is_live ())
193     return;
194
195   object_alist_ = scm_assq_set_x (object_alist_, s, v);
196 }
197
198 void
199 Grob::del_property (SCM sym)
200 {
201   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
202 }
203
204 SCM
205 Grob::internal_get_object (SCM sym) const
206 {
207 #ifdef PROFILE_PROPERTY_ACCESSES
208   note_property_access (&grob_property_lookup_table, sym);
209 #endif
210
211   SCM s = scm_sloppy_assq (sym, object_alist_);
212
213   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
214 }
215
216 void
217 Grob::substitute_object_links (SCM crit, SCM orig)
218 {
219   set_break_subsititution (crit);
220   object_alist_ = substitute_object_alist (orig, object_alist_);
221 }
222
223 bool
224 Grob::is_live () const
225 {
226   return immutable_property_alist_ != SCM_EOL;
227 }