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