]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
*** empty log message ***
[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_simple_closure (val))
106     {
107       val = ((Grob*)this)->try_callback (sym, val);
108     }
109   
110   return val;
111 }
112
113 #ifndef NDEBUG
114 #include "protected-scm.hh"
115 Protected_scm grob_property_callback_stack = SCM_EOL;
116 bool debug_property_callbacks = 0;
117 #endif
118
119 SCM
120 Grob::try_callback (SCM sym, SCM proc)
121 {      
122   SCM marker = ly_symbol2scm ("calculation-in-progress");
123   /*
124     need to put a value in SYM to ensure that we don't get a
125     cyclic call chain.
126   */
127   mutable_property_alist_
128     = scm_assq_set_x (mutable_property_alist_, sym, marker);
129
130 #ifndef NDEBUG
131   if (debug_property_callbacks)
132     grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
133 #endif
134
135   SCM value = SCM_EOL;
136   if (ly_is_procedure (proc))
137     value = scm_call_1 (proc, self_scm ());
138   else if (is_simple_closure (proc))
139     {
140       value = evaluate_with_simple_closure (self_scm (),
141                                             simple_closure_expression (proc));
142     }
143 #ifndef NDEBUG
144   if (debug_property_callbacks)
145     grob_property_callback_stack = scm_cdr (grob_property_callback_stack);
146 #endif
147           
148   /*
149     If the function returns SCM_UNSPECIFIED, we assume the
150     property has been set with an explicit set_property()
151     call.
152   */
153   if (value == SCM_UNSPECIFIED)
154     {
155       value = internal_get_property (sym);
156       if (value == marker)
157         mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, marker);
158     }
159   else
160     internal_set_property (sym, value);
161           
162   return value;
163 }
164
165 void
166 Grob::internal_set_object (SCM s, SCM v)
167 {
168   /* Perhaps we simply do the assq_set, but what the heck. */
169   if (!is_live ())
170     return;
171
172   object_alist_ = scm_assq_set_x (object_alist_, s, v);
173 }
174
175 void
176 Grob::del_property (SCM sym)
177 {
178   mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, sym);
179 }
180
181 SCM
182 Grob::internal_get_object (SCM sym) const
183 {
184 #ifdef PROFILE_PROPERTY_ACCESSES
185   note_property_access (&grob_property_lookup_table, sym);
186 #endif
187
188   SCM s = scm_sloppy_assq (sym, object_alist_);
189
190   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
191 }
192
193 void
194 Grob::substitute_object_links (SCM crit, SCM orig)
195 {
196   set_break_subsititution (crit);
197   object_alist_ = substitute_object_alist (orig, object_alist_);
198 }
199
200 bool
201 Grob::is_live () const
202 {
203   return immutable_property_alist_ != SCM_EOL;
204 }