]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* lily/main.cc (LY_DEFINE): add gmane address.
[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
31 extern void check_interfaces_for_property (Grob const *me, SCM sym);
32
33 void
34 Grob::internal_set_property (SCM sym, SCM v)
35 {
36 #ifndef NDEBUG
37   SCM grob_p = ly_lily_module_constant ("ly:grob?");
38   SCM grob_list_p = ly_lily_module_constant ("grob-list?");
39   SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
40
41   if (type == grob_p
42       || type == grob_list_p
43       || (unsmob_grob (v) && ly_symbol2scm ("cause") != sym))
44     {
45       scm_display (scm_list_2 (sym, type), scm_current_output_port ());
46       assert (0);
47     }
48 #endif
49
50   /* Perhaps we simply do the assq_set, but what the heck. */
51   if (!is_live ())
52     return;
53
54   if (do_internal_type_checking_global)
55     {
56       if (!ly_is_procedure (v)
57           && !is_simple_closure (v)
58           && !type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
59         abort ();
60       check_interfaces_for_property (this, sym);
61     }
62
63   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
64 }
65
66 //#define PROFILE_PROPERTY_ACCESSES
67 SCM
68 Grob::get_property_data (SCM sym) const
69 {
70 #ifndef NDEBUG
71   if (profile_property_accesses)
72     note_property_access (&grob_property_lookup_table, sym);
73 #endif
74   
75   SCM handle = scm_sloppy_assq (sym, mutable_property_alist_);
76   if (handle != SCM_BOOL_F)
77     return scm_cdr (handle);
78
79   handle = scm_sloppy_assq (sym, immutable_property_alist_);
80
81   if (do_internal_type_checking_global && scm_is_pair (handle))
82     {
83       SCM val = scm_cdr (handle);
84       if (!ly_is_procedure (val)
85           && !is_simple_closure (val)
86           && !type_check_assignment (sym, val, 
87                                   ly_symbol2scm ("backend-type?")))
88         abort ();
89
90       check_interfaces_for_property (this, sym);
91     }
92   
93   return (handle == SCM_BOOL_F) ? SCM_EOL : scm_cdr (handle);
94 }
95
96 SCM
97 Grob::internal_get_property (SCM sym) const
98 {
99   SCM val = get_property_data (sym);
100   if (ly_is_procedure (val)
101       || is_simple_closure (val))
102     {
103       val = ((Grob*)this)->try_callback (sym, val);
104     }
105   
106   return val;
107 }
108
109 #ifndef NDEBUG
110 #include "protected-scm.hh"
111 Protected_scm grob_property_callback_stack = SCM_EOL;
112 bool debug_property_callbacks = 0;
113 #endif
114
115 SCM
116 Grob::try_callback (SCM sym, SCM proc)
117 {      
118   SCM marker = ly_symbol2scm ("calculation-in-progress");
119   /*
120     need to put a value in SYM to ensure that we don't get a
121     cyclic call chain.
122   */
123   mutable_property_alist_
124     = scm_assq_set_x (mutable_property_alist_, sym, marker);
125
126 #ifndef NDEBUG
127   if (debug_property_callbacks)
128     grob_property_callback_stack = scm_acons (sym, proc, grob_property_callback_stack);
129 #endif
130
131   SCM value = SCM_EOL;
132   if (ly_is_procedure (proc))
133     value = scm_call_1 (proc, self_scm ());
134   else if (is_simple_closure (proc))
135     {
136       value = evaluate_with_simple_closure (self_scm (),
137                                             simple_closure_expression (proc));
138     }
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 bool
190 Grob::is_live () const
191 {
192   return immutable_property_alist_ != SCM_EOL;
193 }
194
195
196 bool
197 Grob::internal_has_interface (SCM k)
198 {
199   return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
200 }