]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* lily/rest.cc (y_offset_callback): merge function of 3 callbacks.
[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 #include "simple-closure.hh"
20
21 SCM
22 Grob::get_property_alist_chain (SCM def) const
23 {
24   return scm_list_n (mutable_property_alist_,
25                      immutable_property_alist_,
26                      def,
27                      SCM_UNDEFINED);
28 }
29
30 SCM
31 Grob::get_interfaces () const
32 {
33   return interfaces_;
34 }
35
36
37 extern void check_interfaces_for_property (Grob const *me, SCM sym);
38
39 void
40 Grob::internal_set_property (SCM sym, SCM v)
41 {
42 #ifndef NDEBUG
43   SCM grob_p = ly_lily_module_constant ("ly:grob?");
44   SCM grob_list_p = ly_lily_module_constant ("grob-list?");
45   SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
46
47   if (type == grob_p
48       || type == grob_list_p
49       || (unsmob_grob (v) && ly_symbol2scm ("cause") != sym))
50     {
51       scm_display (scm_list_2 (sym, type), scm_current_output_port ());
52       assert (0);
53     }
54 #endif
55
56   /* Perhaps we simply do the assq_set, but what the heck. */
57   if (!is_live ())
58     return;
59
60   if (do_internal_type_checking_global)
61     {
62       if (!ly_is_procedure (v)
63           && !type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
64         abort ();
65       check_interfaces_for_property (this, sym);
66     }
67
68   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
69 }
70
71 //#define PROFILE_PROPERTY_ACCESSES
72 SCM
73 Grob::get_property_data (SCM sym) const
74 {
75 #ifndef NDEBUG
76   if (profile_property_accesses)
77     note_property_access (&grob_property_lookup_table, sym);
78 #endif
79   
80   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
81   if (handle != SCM_BOOL_F)
82     return scm_cdr (handle);
83
84   handle = scm_sloppy_assq (sym, immutable_property_alist_);
85
86   if (do_internal_type_checking_global && scm_is_pair (handle))
87     {
88       SCM val = scm_cdr (handle);
89       if (!ly_is_procedure (val)
90           && !type_check_assignment (sym, val, 
91                                   ly_symbol2scm ("backend-type?")))
92         abort ();
93
94       check_interfaces_for_property (this, sym);
95     }
96   
97   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
98 }
99
100 SCM
101 Grob::internal_get_property (SCM sym) const
102 {
103   SCM val = get_property_data (sym);
104   if (ly_is_procedure (val)
105       || is_callback_chain (val)
106       || is_simple_closure (val))
107     {
108       val = ((Grob*)this)->try_callback (sym, val);
109     }
110   
111   return val;
112 }
113
114 #ifndef NDEBUG
115 #include "protected-scm.hh"
116 Protected_scm grob_property_callback_stack = SCM_EOL;
117 bool debug_property_callbacks = 0;
118 #endif
119
120 SCM
121 Grob::try_callback (SCM sym, SCM proc)
122 {      
123   SCM marker = ly_symbol2scm ("calculation-in-progress");
124   /*
125     need to put a value in SYM to ensure that we don't get a
126     cyclic call chain.
127   */
128   mutable_property_alist_
129     = scm_assq_set_x (mutable_property_alist_, sym, marker);
130
131 #ifndef NDEBUG
132   if (debug_property_callbacks)
133     grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
134 #endif
135
136   SCM value = SCM_EOL;
137   if (ly_is_procedure (proc))
138     value = scm_call_1 (proc, self_scm ());
139   else if (is_callback_chain (proc))
140     {
141       for (SCM s = callback_chain_extract_procedures (proc);
142            scm_is_pair (s); s = scm_cdr (s))
143         {
144           value = scm_call_2  (scm_car (s), self_scm (), value);
145         }
146     }
147   else if (is_simple_closure (proc))
148     {
149       value = evaluate_with_simple_closure (self_scm (),
150                                             simple_closure_expression (proc));
151     }
152 #ifndef NDEBUG
153   if (debug_property_callbacks)
154     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
155 #endif
156           
157   /*
158     If the function returns SCM_UNSPECIFIED, we assume the
159     property has been set with an explicit set_property()
160     call.
161   */
162   if (value == SCM_UNSPECIFIED)
163     {
164       value = internal_get_property (sym);
165       if (value == marker)
166         mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
167     }
168   else
169     internal_set_property (sym, value);
170           
171   return value;
172 }
173
174 void
175 Grob::internal_set_object (SCM s, SCM v)
176 {
177   /* Perhaps we simply do the assq_set, but what the heck. */
178   if (!is_live ())
179     return;
180
181   object_alist_ = scm_assq_set_x (object_alist_, s, v);
182 }
183
184 void
185 Grob::del_property (SCM sym)
186 {
187   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
188 }
189
190 SCM
191 Grob::internal_get_object (SCM sym) const
192 {
193 #ifdef PROFILE_PROPERTY_ACCESSES
194   note_property_access (&grob_property_lookup_table, sym);
195 #endif
196
197   SCM s = scm_sloppy_assq (sym, object_alist_);
198
199   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
200 }
201
202 void
203 Grob::substitute_object_links (SCM crit, SCM orig)
204 {
205   set_break_subsititution (crit);
206   object_alist_ = substitute_object_alist (orig, object_alist_);
207 }
208
209 bool
210 Grob::is_live () const
211 {
212   return immutable_property_alist_ != SCM_EOL;
213 }