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