]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
5d4d03fe34edb969f25b768fcec50a401936e3a8
[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_simple_closure (proc))
140     {
141       value = evaluate_with_simple_closure (self_scm (),
142                                             simple_closure_expression (proc));
143     }
144 #ifndef NDEBUG
145   if (debug_property_callbacks)
146     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
147 #endif
148           
149   /*
150     If the function returns SCM_UNSPECIFIED, we assume the
151     property has been set with an explicit set_property()
152     call.
153   */
154   if (value == SCM_UNSPECIFIED)
155     {
156       value = internal_get_property (sym);
157       if (value == marker)
158         mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
159     }
160   else
161     internal_set_property (sym, value);
162           
163   return value;
164 }
165
166 void
167 Grob::internal_set_object (SCM s, SCM v)
168 {
169   /* Perhaps we simply do the assq_set, but what the heck. */
170   if (!is_live ())
171     return;
172
173   object_alist_ = scm_assq_set_x (object_alist_, s, v);
174 }
175
176 void
177 Grob::del_property (SCM sym)
178 {
179   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
180 }
181
182 SCM
183 Grob::internal_get_object (SCM sym) const
184 {
185 #ifdef PROFILE_PROPERTY_ACCESSES
186   note_property_access (&grob_property_lookup_table, sym);
187 #endif
188
189   SCM s = scm_sloppy_assq (sym, object_alist_);
190
191   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
192 }
193
194 void
195 Grob::substitute_object_links (SCM crit, SCM orig)
196 {
197   set_break_subsititution (crit);
198   object_alist_ = substitute_object_alist (orig, object_alist_);
199 }
200
201 bool
202 Grob::is_live () const
203 {
204   return immutable_property_alist_ != SCM_EOL;
205 }